-
Notifications
You must be signed in to change notification settings - Fork 164
/
connect.go
399 lines (340 loc) · 9.97 KB
/
connect.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package tls_client
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
"sync"
"time"
http "github.com/bogdanfinn/fhttp"
"golang.org/x/net/proxy"
"github.com/bogdanfinn/fhttp/http2"
)
type directDialer struct {
dialer net.Dialer
}
func newDirectDialer(timeout time.Duration, localAddr *net.TCPAddr, _dialer net.Dialer) proxy.ContextDialer {
_dialer.Timeout = timeout
if nil != localAddr {
_dialer.LocalAddr = localAddr
}
return &directDialer{
dialer: _dialer,
}
}
func (d *directDialer) Dial(network, addr string) (net.Conn, error) {
return d.dialer.Dial(network, addr)
}
func (d *directDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return d.dialer.DialContext(ctx, network, addr)
}
type socksContextDialer struct {
socksDialer proxy.Dialer
}
func newSocksContextDialer(socksDialer proxy.Dialer) socksContextDialer {
return socksContextDialer{
socksDialer: socksDialer,
}
}
func (s *socksContextDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
return s.socksDialer.Dial(network, address)
}
// Copyright 2018 Google Inc.
//
// 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.
// stolen from https://github.com/caddyserver/forwardproxy/blob/master/httpclient/httpclient.go
// connectDialer allows to configure one-time use HTTP CONNECT client
type connectDialer struct {
logger Logger
cachedH2RawConn net.Conn
DefaultHeader http.Header
// overridden DialTLS allows user to control establishment of TLS connection
// MUST return connection with completed Handshake, and NegotiatedProtocol
DialTLS func(network string, address string) (net.Conn, string, error)
cachedH2ClientConn *http2.ClientConn
ProxyUrl url.URL
Dialer net.Dialer // overridden dialer allow to control establishment of TCP connection
Timeout time.Duration
cacheH2Mu sync.Mutex
EnableH2ConnReuse bool
}
// newConnectDialer creates a dialer to issue CONNECT requests and tunnel traffic via HTTP/S proxy.
// proxyUrlStr must provide Scheme and Host, may provide credentials and port.
// Example: https://username:[email protected]:443
func newConnectDialer(proxyUrlStr string, timeout time.Duration, localAddr *net.TCPAddr, configDialer net.Dialer, connectHeaders http.Header, logger Logger) (proxy.ContextDialer, error) {
proxyUrl, err := url.Parse(proxyUrlStr)
if err != nil {
return nil, err
}
if proxyUrl.Host == "" {
return nil, errors.New("invalid url `" + proxyUrlStr +
"`, make sure to specify full url like https://username:[email protected]:443/")
}
switch proxyUrl.Scheme {
case "http":
if proxyUrl.Port() == "" {
proxyUrl.Host = net.JoinHostPort(proxyUrl.Host, "80")
}
case "https":
if proxyUrl.Port() == "" {
proxyUrl.Host = net.JoinHostPort(proxyUrl.Host, "443")
}
case "socks5", "socks5h":
return handleSocks5ProxyDialer(proxyUrl, localAddr)
case "":
return nil, errors.New("specify scheme explicitly (https://)")
default:
return nil, errors.New("scheme " + proxyUrl.Scheme + " is not supported")
}
_dialer := configDialer
_dialer.Timeout = timeout
if nil != localAddr {
_dialer.LocalAddr = localAddr
}
dialer := &connectDialer{
logger: logger,
ProxyUrl: *proxyUrl,
Dialer: _dialer,
Timeout: timeout,
DefaultHeader: connectHeaders.Clone(),
EnableH2ConnReuse: true,
}
if proxyUrl.User != nil {
if proxyUrl.User.Username() != "" {
//example format (with credentials): http://root:[email protected]:12312
password, _ := proxyUrl.User.Password()
dialer.DefaultHeader.Set("Proxy-Authorization", "Basic "+
base64.StdEncoding.EncodeToString([]byte(proxyUrl.User.Username()+":"+password)))
}
}
return dialer, nil
}
func handleSocks5ProxyDialer(proxyUrl *url.URL, localAddr *net.TCPAddr) (proxy.ContextDialer, error) {
var proxyAuth *proxy.Auth
if proxyUrl.User != nil {
password, _ := proxyUrl.User.Password()
proxyAuth = &proxy.Auth{
User: proxyUrl.User.Username(),
Password: password,
}
} else {
proxyAuth = nil
}
_dialer := proxy.Dialer(proxy.Direct)
if nil != localAddr {
_dialer = &net.Dialer{
LocalAddr: localAddr,
}
}
socksDialer, err := proxy.SOCKS5("tcp", proxyUrl.Host, proxyAuth, _dialer)
if err != nil {
return nil, fmt.Errorf("failed to create socks5 proxy: %w", err)
}
scd := newSocksContextDialer(socksDialer)
return &scd, nil
}
func (c *connectDialer) Dial(network, address string) (net.Conn, error) {
return c.DialContext(context.Background(), network, address)
}
// Users of context.WithValue should define their own types for keys
type ContextKeyHeader struct{}
// ctx.Value will be inspected for optional ContextKeyHeader{} key, with `http.Header` value,
// which will be added to outgoing request headers, overriding any colliding c.DefaultHeader
func (c *connectDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
req := (&http.Request{
Method: "CONNECT",
URL: &url.URL{Host: address},
Header: make(http.Header),
Host: address,
}).WithContext(ctx)
for k, v := range c.DefaultHeader {
req.Header[k] = v
}
if ctxHeader, ctxHasHeader := ctx.Value(ContextKeyHeader{}).(http.Header); ctxHasHeader {
for k, v := range ctxHeader {
req.Header[k] = v
}
}
connectHttp2 := func(rawConn net.Conn, h2clientConn *http2.ClientConn) (net.Conn, error) {
req.Proto = "HTTP/2.0"
req.ProtoMajor = 2
req.ProtoMinor = 0
pr, pw := io.Pipe()
req.Body = pr
resp, err := h2clientConn.RoundTrip(req)
if err != nil {
_ = rawConn.Close()
return nil, err
}
if resp.StatusCode != http.StatusOK {
_ = rawConn.Close()
return nil, errors.New("Proxy responded with non 200 code: " + resp.Status)
}
return newHttp2Conn(rawConn, pw, resp.Body), nil
}
connectHttp1 := func(rawConn net.Conn) (net.Conn, error) {
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
deadline := time.Now().Add(c.Timeout)
err := rawConn.SetDeadline(deadline)
if err != nil {
_ = rawConn.Close()
return nil, err
}
err = req.Write(rawConn)
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
c.logger.Error("deadline exceeded while trying to write proxy connection")
}
_ = rawConn.Close()
return nil, err
}
resp, err := http.ReadResponse(bufio.NewReader(rawConn), req)
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
c.logger.Error("deadline exceeded while trying to read proxy connection")
}
_ = rawConn.Close()
return nil, err
}
if resp.StatusCode != http.StatusOK {
_ = rawConn.Close()
return nil, errors.New("Proxy responded with non 200 code: " + resp.Status)
}
rawConn.SetDeadline(time.Time{})
return rawConn, nil
}
if c.EnableH2ConnReuse {
c.cacheH2Mu.Lock()
unlocked := false
if c.cachedH2ClientConn != nil && c.cachedH2RawConn != nil {
if c.cachedH2ClientConn.CanTakeNewRequest() {
rc := c.cachedH2RawConn
cc := c.cachedH2ClientConn
c.cacheH2Mu.Unlock()
unlocked = true
proxyConn, err := connectHttp2(rc, cc)
if err == nil {
return proxyConn, err
}
// else: carry on and try again
}
}
if !unlocked {
c.cacheH2Mu.Unlock()
}
}
var err error
var rawConn net.Conn
negotiatedProtocol := ""
switch c.ProxyUrl.Scheme {
case "http":
rawConn, err = c.Dialer.DialContext(ctx, network, c.ProxyUrl.Host)
if err != nil {
return nil, err
}
case "https":
if c.DialTLS != nil {
rawConn, negotiatedProtocol, err = c.DialTLS(network, c.ProxyUrl.Host)
if err != nil {
return nil, err
}
} else {
tlsConf := tls.Config{
NextProtos: []string{"h2", "http/1.1"},
ServerName: c.ProxyUrl.Hostname(),
}
tlsConn, err := tls.Dial(network, c.ProxyUrl.Host, &tlsConf)
if err != nil {
return nil, err
}
err = tlsConn.HandshakeContext(ctx)
if err != nil {
return nil, err
}
negotiatedProtocol = tlsConn.ConnectionState().NegotiatedProtocol
rawConn = tlsConn
}
default:
return nil, errors.New("scheme " + c.ProxyUrl.Scheme + " is not supported")
}
switch negotiatedProtocol {
case "":
fallthrough
case "http/1.1":
return connectHttp1(rawConn)
case "h2":
t := http2.Transport{}
h2clientConn, err := t.NewClientConn(rawConn)
if err != nil {
_ = rawConn.Close()
return nil, err
}
proxyConn, err := connectHttp2(rawConn, h2clientConn)
if err != nil {
_ = rawConn.Close()
return nil, err
}
if c.EnableH2ConnReuse {
c.cacheH2Mu.Lock()
c.cachedH2ClientConn = h2clientConn
c.cachedH2RawConn = rawConn
c.cacheH2Mu.Unlock()
}
return proxyConn, err
default:
_ = rawConn.Close()
return nil, errors.New("negotiated unsupported application layer protocol: " +
negotiatedProtocol)
}
}
func newHttp2Conn(c net.Conn, pipedReqBody *io.PipeWriter, respBody io.ReadCloser) net.Conn {
return &http2Conn{Conn: c, in: pipedReqBody, out: respBody}
}
type http2Conn struct {
net.Conn
in *io.PipeWriter
out io.ReadCloser
}
func (h *http2Conn) Read(p []byte) (n int, err error) {
return h.out.Read(p)
}
func (h *http2Conn) Write(p []byte) (n int, err error) {
return h.in.Write(p)
}
func (h *http2Conn) Close() error {
var retErr error = nil
if err := h.in.Close(); err != nil {
retErr = err
}
if err := h.out.Close(); err != nil {
retErr = err
}
return retErr
}
func (h *http2Conn) CloseConn() error {
return h.Conn.Close()
}
func (h *http2Conn) CloseWrite() error {
return h.in.Close()
}
func (h *http2Conn) CloseRead() error {
return h.out.Close()
}