| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
package grpc |
| 20 |
|
| 21 |
import ( |
| 22 |
"bufio" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"net" |
| 27 |
"net/http" |
| 28 |
"net/http/httputil" |
| 29 |
"net/url" |
| 30 |
|
| 31 |
"golang.org/x/net/context" |
| 32 |
) |
| 33 |
|
| 34 |
var ( |
| 35 |
// errDisabled indicates that proxy is disabled for the address. |
| 36 |
errDisabled = errors.New("proxy is disabled for the address") |
| 37 |
// The following variable will be overwritten in the tests. |
| 38 |
httpProxyFromEnvironment = http.ProxyFromEnvironment |
| 39 |
) |
| 40 |
|
| 41 |
func mapAddress(ctx context.Context, address string) (string, error) { |
| 42 |
req := &http.Request{ |
| 43 |
URL: &url.URL{ |
| 44 |
Scheme: "https", |
| 45 |
Host: address, |
| 46 |
}, |
| 47 |
} |
| 48 |
url, err := httpProxyFromEnvironment(req) |
| 49 |
if err != nil { |
| 50 |
return "", err |
| 51 |
} |
| 52 |
if url == nil { |
| 53 |
return "", errDisabled |
| 54 |
} |
| 55 |
return url.Host, nil |
| 56 |
} |
| 57 |
|
| 58 |
// To read a response from a net.Conn, http.ReadResponse() takes a bufio.Reader. |
| 59 |
// It's possible that this reader reads more than what's need for the response and stores |
| 60 |
// those bytes in the buffer. |
| 61 |
// bufConn wraps the original net.Conn and the bufio.Reader to make sure we don't lose the |
| 62 |
// bytes in the buffer. |
| 63 |
type bufConn struct { |
| 64 |
net.Conn |
| 65 |
r io.Reader |
| 66 |
} |
| 67 |
|
| 68 |
func (c *bufConn) Read(b []byte) (int, error) { |
| 69 |
return c.r.Read(b) |
| 70 |
} |
| 71 |
|
| 72 |
func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, addr string) (_ net.Conn, err error) { |
| 73 |
defer func() { |
| 74 |
if err != nil { |
| 75 |
conn.Close() |
| 76 |
} |
| 77 |
}() |
| 78 |
|
| 79 |
req := (&http.Request{ |
| 80 |
Method: http.MethodConnect, |
| 81 |
URL: &url.URL{Host: addr}, |
| 82 |
Header: map[string][]string{"User-Agent": {grpcUA}}, |
| 83 |
}) |
| 84 |
|
| 85 |
req = req.WithContext(ctx) |
| 86 |
if err := req.Write(conn); err != nil { |
| 87 |
return nil, fmt.Errorf("failed to write the HTTP request: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
r := bufio.NewReader(conn) |
| 91 |
resp, err := http.ReadResponse(r, req) |
| 92 |
if err != nil { |
| 93 |
return nil, fmt.Errorf("reading server HTTP response: %v", err) |
| 94 |
} |
| 95 |
defer resp.Body.Close() |
| 96 |
if resp.StatusCode != http.StatusOK { |
| 97 |
dump, err := httputil.DumpResponse(resp, true) |
| 98 |
if err != nil { |
| 99 |
return nil, fmt.Errorf("failed to do connect handshake, status code: %s", resp.Status) |
| 100 |
} |
| 101 |
return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump) |
| 102 |
} |
| 103 |
|
| 104 |
return &bufConn{Conn: conn, r: r}, nil |
| 105 |
} |
| 106 |
|
| 107 |
// newProxyDialer returns a dialer that connects to proxy first if necessary. |
| 108 |
// The returned dialer checks if a proxy is necessary, dial to the proxy with the |
| 109 |
// provided dialer, does HTTP CONNECT handshake and returns the connection. |
| 110 |
func newProxyDialer(dialer func(context.Context, string) (net.Conn, error)) func(context.Context, string) (net.Conn, error) { |
| 111 |
return func(ctx context.Context, addr string) (conn net.Conn, err error) { |
| 112 |
var skipHandshake bool |
| 113 |
newAddr, err := mapAddress(ctx, addr) |
| 114 |
if err != nil { |
| 115 |
if err != errDisabled { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
skipHandshake = true |
| 119 |
newAddr = addr |
| 120 |
} |
| 121 |
|
| 122 |
conn, err = dialer(ctx, newAddr) |
| 123 |
if err != nil { |
| 124 |
return |
| 125 |
} |
| 126 |
if !skipHandshake { |
| 127 |
conn, err = doHTTPConnectHandshake(ctx, conn, addr) |
| 128 |
} |
| 129 |
return |
| 130 |
} |
| 131 |
} |