-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathproxy.go
More file actions
181 lines (170 loc) · 5.83 KB
/
proxy.go
File metadata and controls
181 lines (170 loc) · 5.83 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package api
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"io"
"net"
"net/http"
"net/url"
"sync"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type connWithBufferedReader struct {
net.Conn
r *bufio.Reader
mu sync.Mutex
}
func (c *connWithBufferedReader) Read(p []byte) (int, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.r.Read(p)
}
// proxyDialAddr returns proxyURL.Host with a default port appended if one is
// not already present (443 for https, 80 for http).
func proxyDialAddr(proxyURL *url.URL) string {
// net.SplitHostPort returns an error when the input doesn't contain a port
if _, _, err := net.SplitHostPort(proxyURL.Host); err == nil {
return proxyURL.Host
}
if proxyURL.Scheme == "https" {
return net.JoinHostPort(proxyURL.Hostname(), "443")
}
return net.JoinHostPort(proxyURL.Hostname(), "80")
}
// withProxyTransport modifies the given transport to handle proxying of unix, socks5 and http connections.
//
// Note: baseTransport is considered to be a clone created with transport.Clone()
//
// - If proxyPath is not empty, a unix socket proxy is created.
// - Otherwise, proxyURL is used to determine if we should proxy socks5 / http connections
func withProxyTransport(baseTransport *http.Transport, proxyURL *url.URL, proxyPath string) *http.Transport {
handshakeTLS := func(ctx context.Context, conn net.Conn, addr string) (net.Conn, error) {
// Extract the hostname (without the port) for TLS SNI
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
cfg := baseTransport.TLSClientConfig.Clone()
if cfg.ServerName == "" {
cfg.ServerName = host
}
// Preserve HTTP/2 negotiation to the origin when ForceAttemptHTTP2
// is enabled. Without this, the manual TLS handshake would not
// advertise h2 via ALPN, silently forcing HTTP/1.1.
if baseTransport.ForceAttemptHTTP2 && len(cfg.NextProtos) == 0 {
cfg.NextProtos = []string{"h2", "http/1.1"}
}
tlsConn := tls.Client(conn, cfg)
if err := tlsConn.HandshakeContext(ctx); err != nil {
tlsConn.Close()
return nil, err
}
return tlsConn, nil
}
if proxyPath != "" {
dial := func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "unix", proxyPath)
}
dialTLS := func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := dial(ctx, network, addr)
if err != nil {
return nil, err
}
return handshakeTLS(ctx, conn, addr)
}
baseTransport.DialContext = dial
baseTransport.DialTLSContext = dialTLS
// clear out any system proxy settings
baseTransport.Proxy = nil
} else if proxyURL != nil {
switch proxyURL.Scheme {
case "http", "socks5", "socks5h":
// HTTP and SOCKS proxies work out of the box - no need to manually dial
baseTransport.Proxy = http.ProxyURL(proxyURL)
case "https":
dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
// Dial the proxy. For https:// proxies, we TLS-connect to the
// proxy itself and force ALPN to HTTP/1.1 to prevent Go from
// negotiating HTTP/2 for the CONNECT tunnel. Many proxy servers
// don't support HTTP/2 CONNECT, and Go's default Transport.Proxy
// would negotiate h2 via ALPN when TLS-connecting to an https://
// proxy, causing "bogus greeting" errors. For http:// proxies,
// CONNECT is always HTTP/1.1 over plain TCP so this isn't needed.
// The target connection (e.g. to sourcegraph.com) still negotiates
// HTTP/2 normally through the established tunnel.
proxyAddr := proxyDialAddr(proxyURL)
var conn net.Conn
var err error
if proxyURL.Scheme == "https" {
raw, dialErr := (&net.Dialer{}).DialContext(ctx, "tcp", proxyAddr)
if dialErr != nil {
return nil, dialErr
}
cfg := baseTransport.TLSClientConfig.Clone()
cfg.NextProtos = []string{"http/1.1"}
if cfg.ServerName == "" {
cfg.ServerName = proxyURL.Hostname()
}
tlsConn := tls.Client(raw, cfg)
if err := tlsConn.HandshakeContext(ctx); err != nil {
raw.Close()
return nil, err
}
conn = tlsConn
} else {
conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", proxyAddr)
}
if err != nil {
return nil, err
}
connectReq := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: addr},
Host: addr,
Header: make(http.Header),
}
if proxyURL.User != nil {
password, _ := proxyURL.User.Password()
auth := base64.StdEncoding.EncodeToString([]byte(proxyURL.User.Username() + ":" + password))
connectReq.Header.Set("Proxy-Authorization", "Basic "+auth)
}
if err := connectReq.Write(conn); err != nil {
conn.Close()
return nil, err
}
br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, nil)
if err != nil {
conn.Close()
return nil, err
}
if resp.StatusCode != http.StatusOK {
// For non-200, it's safe/appropriate to close the body (it’s a real response body here).
// Try to read a bit (4k bytes) to include in the error message.
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
resp.Body.Close()
conn.Close()
return nil, errors.Newf("failed to connect to proxy %s: %s: %q", proxyURL.Redacted(), resp.Status, b)
}
// 200 CONNECT: do NOT resp.Body.Close(); it would interfere with the tunnel.
return &connWithBufferedReader{Conn: conn, r: br}, nil
}
dialTLS := func(ctx context.Context, network, addr string) (net.Conn, error) {
// Dial the underlying connection through the proxy
conn, err := dial(ctx, network, addr)
if err != nil {
return nil, err
}
return handshakeTLS(ctx, conn, addr)
}
baseTransport.DialContext = dial
baseTransport.DialTLSContext = dialTLS
// clear out the system proxy because we're defining our own dialers
baseTransport.Proxy = nil
}
}
return baseTransport
}