-
Notifications
You must be signed in to change notification settings - Fork 28
/
upstream.go
166 lines (148 loc) · 3.67 KB
/
upstream.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
package main
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
xproxy "golang.org/x/net/proxy"
)
type HTTPProxyDialer struct {
address string
tls bool
userinfo *url.Userinfo
next ContextDialer
}
func NewHTTPProxyDialer(address string, tls bool, userinfo *url.Userinfo, next Dialer) *HTTPProxyDialer {
return &HTTPProxyDialer{
address: address,
tls: tls,
next: maybeWrapWithContextDialer(next),
userinfo: userinfo,
}
}
func HTTPProxyDialerFromURL(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) {
host := u.Hostname()
port := u.Port()
tls := false
switch strings.ToLower(u.Scheme) {
case "http":
if port == "" {
port = "80"
}
case "https":
tls = true
if port == "" {
port = "443"
}
default:
return nil, errors.New("unsupported proxy type")
}
address := net.JoinHostPort(host, port)
return NewHTTPProxyDialer(address, tls, u.User, next), nil
}
func (d *HTTPProxyDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *HTTPProxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return nil, errors.New("only \"tcp\" network is supported")
}
conn, err := d.next.DialContext(ctx, "tcp", d.address)
if err != nil {
return nil, fmt.Errorf("proxy dialer is unable to make connection: %w", err)
}
if d.tls {
hostname, _, err := net.SplitHostPort(d.address)
if err != nil {
hostname = address
}
conn = tls.Client(conn, &tls.Config{
ServerName: hostname,
})
}
stopGuardEvent := make(chan struct{})
guardErr := make(chan error, 1)
go func() {
select {
case <-stopGuardEvent:
close(guardErr)
case <-ctx.Done():
conn.Close()
guardErr <- ctx.Err()
}
}()
var stopGuardOnce sync.Once
stopGuard := func() {
stopGuardOnce.Do(func() {
close(stopGuardEvent)
})
}
defer stopGuard()
var reqBuf bytes.Buffer
fmt.Fprintf(&reqBuf, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", address, address)
if d.userinfo != nil {
fmt.Fprintf(&reqBuf, "Proxy-Authorization: %s\r\n", basicAuthHeader(d.userinfo))
}
fmt.Fprintf(&reqBuf, "User-Agent: dumbproxy/%s\r\n\r\n", version)
_, err = io.Copy(conn, &reqBuf)
if err != nil {
conn.Close()
return nil, fmt.Errorf("unable to write proxy request for remote connection: %w", err)
}
resp, err := readResponse(conn)
if err != nil {
conn.Close()
return nil, fmt.Errorf("reading proxy response failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
conn.Close()
return nil, fmt.Errorf("bad status code from proxy: %d", resp.StatusCode)
}
stopGuard()
if err := <-guardErr; err != nil {
return nil, fmt.Errorf("context error: %w", err)
}
return conn, nil
}
var (
responseTerminator = []byte("\r\n\r\n")
)
func readResponse(r io.Reader) (*http.Response, error) {
var respBuf bytes.Buffer
b := make([]byte, 1)
for !bytes.HasSuffix(respBuf.Bytes(), responseTerminator) {
n, err := r.Read(b)
if err != nil {
return nil, fmt.Errorf("unable to read HTTP response: %w", err)
}
if n == 0 {
continue
}
_, err = respBuf.Write(b)
if err != nil {
return nil, fmt.Errorf("unable to store byte into buffer: %w", err)
}
}
resp, err := http.ReadResponse(bufio.NewReader(&respBuf), nil)
if err != nil {
return nil, fmt.Errorf("unable to decode proxy response: %w", err)
}
return resp, nil
}
func basicAuthHeader(userinfo *url.Userinfo) string {
username := userinfo.Username()
password, _ := userinfo.Password()
return "Basic " + base64.StdEncoding.EncodeToString(
[]byte(username+":"+password))
}