-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialer_http.go
176 lines (149 loc) · 3.55 KB
/
dialer_http.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
package main
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
)
var _ Dialer = (*HTTPDialer)(nil)
type HTTPDialer struct {
Username string
Password string
Host string
Port string
IsTLS bool
Insecure bool
UserAgent string
CACert string
ClientKey string
ClientCert string
Dialer Dialer
mu sync.Mutex
tlsConfig *tls.Config
}
func (d *HTTPDialer) init() error {
if d.Dialer != nil && d.UserAgent != "" {
return nil
}
d.mu.Lock()
defer d.mu.Unlock()
if d.Dialer == nil {
d.Dialer = &LocalDialer{}
}
if d.UserAgent == "" {
d.UserAgent = DefaultUserAgent
}
if d.IsTLS {
d.tlsConfig = &tls.Config{
InsecureSkipVerify: d.Insecure,
ServerName: d.Host,
ClientSessionCache: tls.NewLRUClientSessionCache(1024),
}
if d.CACert != "" && d.ClientKey != "" && d.ClientCert != "" {
caData, err := os.ReadFile(d.CACert)
if err != nil {
return err
}
cert, err := tls.LoadX509KeyPair(d.ClientCert, d.ClientKey)
if err != nil {
return err
}
d.tlsConfig.RootCAs = x509.NewCertPool()
d.tlsConfig.RootCAs.AppendCertsFromPEM(caData)
d.tlsConfig.Certificates = []tls.Certificate{cert}
}
}
return nil
}
var CRLFCRLF = []byte{'\r', '\n', '\r', '\n'}
func (d *HTTPDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
if err := d.init(); err != nil {
return nil, err
}
switch network {
case "tcp", "tcp6", "tcp4":
default:
return nil, errors.New("proxy: no support for HTTP proxy connections of type " + network)
}
conn, err := d.Dialer.DialContext(ctx, network, net.JoinHostPort(d.Host, d.Port))
if err != nil {
return nil, err
}
closeConn := &conn
defer func() {
if closeConn != nil {
(*closeConn).Close()
}
}()
if d.IsTLS {
if d.tlsConfig == nil {
return nil, errors.New("empty tls config")
}
tlsConn := tls.Client(conn, d.tlsConfig)
err = tlsConn.HandshakeContext(ctx)
if err != nil {
return nil, err
}
conn = tlsConn
}
buf := make([]byte, 0, 2048)
buf = fmt.Appendf(buf, "CONNECT %s HTTP/1.1\r\n", addr)
buf = fmt.Appendf(buf, "Host: %s\r\n", addr)
buf = fmt.Appendf(buf, "User-Agent: %s\r\n", d.UserAgent)
if d.Username != "" {
buf = fmt.Appendf(buf, "Proxy-Authorization: Basic %s\r\n", base64.StdEncoding.EncodeToString([]byte(d.Username+":"+d.Password)))
}
buf = fmt.Appendf(buf, "\r\n")
if _, err := conn.Write(buf); err != nil {
return nil, errors.New("proxy: failed to write greeting to HTTP proxy at " + d.Host + ": " + err.Error())
}
// see https://github.com/golang/go/issues/5373
buf = buf[:cap(buf)]
for i := range buf {
buf[i] = 0
}
b := buf
total := 0
if deadline, ok := ctx.Deadline(); ok {
conn.SetDeadline(deadline)
defer conn.SetDeadline(time.Time{})
}
for {
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
total += n
buf = buf[n:]
if i := bytes.Index(b, CRLFCRLF); i > 0 {
if i+4 < total {
conn = &ConnWithData{conn, b[i+4 : total]}
}
break
}
}
status := 0
n := bytes.IndexByte(b, ' ')
if n < 0 {
return nil, fmt.Errorf("proxy: failed to connect %s via %s: %s", addr, d.Host, bytes.TrimRight(b, "\x00"))
}
for i, c := range b[n+1:] {
if i == 3 || c < '0' || c > '9' {
break
}
status = status*10 + int(c-'0')
}
if status != http.StatusOK && status != http.StatusSwitchingProtocols {
return nil, fmt.Errorf("proxy: failed to connect %s via %s: %s", addr, d.Host, bytes.TrimRight(b, "\x00"))
}
closeConn = nil
return conn, nil
}