forked from spiffe/spire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
145 lines (127 loc) · 3.93 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// The following source code has been taken, with minor modification, from the
// gRPC codebase. We'd rather use their dialer, which supports HTTP-CONNECT
// proxies, but need richer error logging on handshake failure.
/*
*
* Copyright 2017 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package grpcutil
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const grpcUA = "grpc-go/" + grpc.Version
func getProxyURL(ctx context.Context, address string) (*url.URL, error) {
req := &http.Request{
URL: &url.URL{
Scheme: "https",
Host: address,
},
}
url, err := http.ProxyFromEnvironment(req)
if err != nil {
return nil, err
}
if url == nil {
return nil, nil
}
switch url.Scheme {
case "http", "https":
default:
return nil, fmt.Errorf("unsupported proxy scheme: %q", url.Scheme)
}
return url, nil
}
// To read a response from a net.Conn, http.ReadResponse() takes a bufio.Reader.
// It's possible that this reader reads more than what's need for the response and stores
// those bytes in the buffer.
// bufConn wraps the original net.Conn and the bufio.Reader to make sure we don't lose the
// bytes in the buffer.
type bufConn struct {
net.Conn
r io.Reader
}
func (c *bufConn) Read(b []byte) (int, error) {
return c.r.Read(b)
}
func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, addr string) (_ net.Conn, err error) {
defer func() {
if err != nil {
conn.Close()
}
}()
req := (&http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: addr},
Header: map[string][]string{"User-Agent": {grpcUA}},
})
req = req.WithContext(ctx)
if err := req.Write(conn); err != nil {
return nil, fmt.Errorf("failed to write the HTTP request: %v", err)
}
r := bufio.NewReader(conn)
resp, err := http.ReadResponse(r, req)
if err != nil {
return nil, fmt.Errorf("reading server HTTP response: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return nil, fmt.Errorf("failed to do connect handshake, status code: %s", resp.Status)
}
return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump)
}
return &bufConn{Conn: conn, r: r}, nil
}
// proxyDial dials to a proxy and does an HTTP CONNECT handshake if proxying is
// enabled. Otherwise, it just does a regular TCP dial. It is based on the
// newProxyDialer wrapper implementation from the gRPC codebase.
func proxyDial(ctx context.Context, log logrus.StdLogger, addr string) (conn net.Conn, err error) {
proxyURL, err := getProxyURL(ctx, addr)
if err != nil {
log.Printf("Failed to obtain proxy url for address %s: %v", addr, err)
return nil, err
}
if proxyURL == nil {
// no proxy; dial the address directly
return new(net.Dialer).DialContext(ctx, "tcp", addr)
}
// Dial the proxy
log.Printf("Proxying via %s to reach %s", proxyURL.String(), addr)
conn, err = new(net.Dialer).DialContext(ctx, "tcp", proxyURL.Host)
if err != nil {
return nil, err
}
// if the proxy is over HTTPS, wrap the connection in a TLS connection
// before doing the HTTP CONNECT handshake.
if proxyURL.Scheme == "https" {
conn = tls.Client(conn, &tls.Config{
ServerName: proxyURL.Hostname(),
})
}
// Do the HTTP-CONNECT handshake. If unsuccessful, conn is closed.
return doHTTPConnectHandshake(ctx, conn, addr)
}