|
| 1 | +// NOTE: This source file contains the internal grpc-go proxy implementation |
| 2 | +// found in google.golang.org/grpc/internal/transport, with minor |
| 3 | +// modifications for use in grpcurl. Below is the original license: |
| 4 | + |
| 5 | +/* |
| 6 | + * |
| 7 | + * Copyright 2017 gRPC authors. |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +package grpcurl |
| 24 | + |
| 25 | +import ( |
| 26 | + "bufio" |
| 27 | + "context" |
| 28 | + "encoding/base64" |
| 29 | + "fmt" |
| 30 | + "io" |
| 31 | + "net" |
| 32 | + "net/http" |
| 33 | + "net/http/httputil" |
| 34 | + "net/url" |
| 35 | + // "google.golang.org/grpc/internal" |
| 36 | +) |
| 37 | + |
| 38 | +const proxyAuthHeaderKey = "Proxy-Authorization" |
| 39 | + |
| 40 | +var ( |
| 41 | + // The following variable will be overwritten in the tests. |
| 42 | + httpProxyFromEnvironment = http.ProxyFromEnvironment |
| 43 | +) |
| 44 | + |
| 45 | +func mapAddress(address string) (*url.URL, error) { |
| 46 | + req := &http.Request{ |
| 47 | + URL: &url.URL{ |
| 48 | + Scheme: "https", |
| 49 | + Host: address, |
| 50 | + }, |
| 51 | + } |
| 52 | + url, err := httpProxyFromEnvironment(req) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + return url, nil |
| 57 | +} |
| 58 | + |
| 59 | +// To read a response from a net.Conn, http.ReadResponse() takes a bufio.Reader. |
| 60 | +// It's possible that this reader reads more than what's need for the response and stores |
| 61 | +// those bytes in the buffer. |
| 62 | +// bufConn wraps the original net.Conn and the bufio.Reader to make sure we don't lose the |
| 63 | +// bytes in the buffer. |
| 64 | +type bufConn struct { |
| 65 | + net.Conn |
| 66 | + r io.Reader |
| 67 | +} |
| 68 | + |
| 69 | +func (c *bufConn) Read(b []byte) (int, error) { |
| 70 | + return c.r.Read(b) |
| 71 | +} |
| 72 | + |
| 73 | +func basicAuth(username, password string) string { |
| 74 | + auth := username + ":" + password |
| 75 | + return base64.StdEncoding.EncodeToString([]byte(auth)) |
| 76 | +} |
| 77 | + |
| 78 | +func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, backendAddr string, proxyURL *url.URL, grpcUA string) (_ net.Conn, err error) { |
| 79 | + defer func() { |
| 80 | + if err != nil { |
| 81 | + conn.Close() |
| 82 | + } |
| 83 | + }() |
| 84 | + |
| 85 | + req := &http.Request{ |
| 86 | + Method: http.MethodConnect, |
| 87 | + URL: &url.URL{Host: backendAddr}, |
| 88 | + Header: map[string][]string{"User-Agent": {grpcUA}}, |
| 89 | + } |
| 90 | + if t := proxyURL.User; t != nil { |
| 91 | + u := t.Username() |
| 92 | + p, _ := t.Password() |
| 93 | + req.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(u, p)) |
| 94 | + } |
| 95 | + |
| 96 | + if err := sendHTTPRequest(ctx, req, conn); err != nil { |
| 97 | + return nil, fmt.Errorf("failed to write the HTTP request: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + r := bufio.NewReader(conn) |
| 101 | + resp, err := http.ReadResponse(r, req) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("reading server HTTP response: %v", err) |
| 104 | + } |
| 105 | + defer resp.Body.Close() |
| 106 | + if resp.StatusCode != http.StatusOK { |
| 107 | + dump, err := httputil.DumpResponse(resp, true) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("failed to do connect handshake, status code: %s", resp.Status) |
| 110 | + } |
| 111 | + return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump) |
| 112 | + } |
| 113 | + // The buffer could contain extra bytes from the target server, so we can't |
| 114 | + // discard it. However, in many cases where the server waits for the client |
| 115 | + // to send the first message (e.g. when TLS is being used), the buffer will |
| 116 | + // be empty, so we can avoid the overhead of reading through this buffer. |
| 117 | + if r.Buffered() != 0 { |
| 118 | + return &bufConn{Conn: conn, r: r}, nil |
| 119 | + } |
| 120 | + return conn, nil |
| 121 | +} |
| 122 | + |
| 123 | +// proxyDial dials, connecting to a proxy first if necessary. Checks if a proxy |
| 124 | +// is necessary, dials, does the HTTP CONNECT handshake, and returns the |
| 125 | +// connection. |
| 126 | +func proxyDial(ctx context.Context, addr string, grpcUA string) (net.Conn, error) { |
| 127 | + newAddr := addr |
| 128 | + proxyURL, err := mapAddress(addr) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + if proxyURL != nil { |
| 133 | + newAddr = proxyURL.Host |
| 134 | + } |
| 135 | + |
| 136 | + // NOTE: Use net.Dialer to avoid dependency on grpc-go's internal package |
| 137 | + // conn, err := internal.NetDialerWithTCPKeepalive().DialContext(ctx, "tcp", newAddr) |
| 138 | + |
| 139 | + conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", newAddr) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + if proxyURL == nil { |
| 144 | + // proxy is disabled if proxyURL is nil. |
| 145 | + return conn, err |
| 146 | + } |
| 147 | + return doHTTPConnectHandshake(ctx, conn, addr, proxyURL, grpcUA) |
| 148 | +} |
| 149 | + |
| 150 | +func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error { |
| 151 | + req = req.WithContext(ctx) |
| 152 | + if err := req.Write(conn); err != nil { |
| 153 | + return fmt.Errorf("failed to write the HTTP request: %v", err) |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
0 commit comments