This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
271 lines (233 loc) · 6.28 KB
/
client.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
package gosocks5
import (
"context"
"fmt"
"io"
"net"
"net/url"
"strconv"
)
type contextDialer interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}
type proxyAuth struct {
username string
password string
usernameLen int
passwordLen int
}
type Dialer struct {
proxyAddr string
proxyAuth *proxyAuth
// socks5h delegates DNS resolution to proxy server but
// socks5 does that locally
// https://superuser.com/a/1762355/956392
localResolve bool
}
func NewDialer(addr string) (*Dialer, error) {
dialer := &Dialer{}
parsedUrl, err := url.Parse(addr)
if err != nil {
return nil, err
}
switch parsedUrl.Scheme {
case "socks5h":
case "socks5":
dialer.localResolve = true
default:
return nil, fmt.Errorf("invalid url scheme %s", parsedUrl.Scheme)
}
if parsedUrl.User != nil {
dialer.proxyAuth = &proxyAuth{}
dialer.proxyAuth.username = parsedUrl.User.Username()
dialer.proxyAuth.usernameLen = len(dialer.proxyAuth.username)
dialer.proxyAuth.password, _ = parsedUrl.User.Password()
dialer.proxyAuth.passwordLen = len(dialer.proxyAuth.password)
if dialer.proxyAuth.usernameLen > 255 {
return nil, fmt.Errorf("too long username length %d", len(dialer.proxyAuth.username))
}
if dialer.proxyAuth.passwordLen > 255 {
return nil, fmt.Errorf("too long password length %d", len(dialer.proxyAuth.password))
}
}
dialer.proxyAddr = parsedUrl.Host
if parsedUrl.Port() == "" {
dialer.proxyAddr = net.JoinHostPort(parsedUrl.Host, strconv.Itoa(defaultPort))
}
return dialer, nil
}
var _ contextDialer = &Dialer{}
func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := d.dialProxyServer(ctx)
if err != nil {
return nil, err
}
err = d.negotiateAuthentication(conn)
if err != nil {
return nil, err
}
var cmd int
switch network {
case "tcp", "tcp4", "tcp6":
cmd = cmdConnect
case "udp", "udp4", "udp6":
cmd = cmdUDPAssociate
default:
return nil, fmt.Errorf("unsupported network %q", network)
}
conn, err = d.negotiateCmd(cmd, conn, addr)
return conn, err
}
func (d *Dialer) Listen(ctx context.Context, network, address string) (net.Listener, error) {
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return nil, fmt.Errorf("network is not supported")
}
return &listener{
dialer: d,
ctx: ctx,
network: network,
address: address,
}, nil
}
func (d *Dialer) negotiateCmd(cmd int, conn net.Conn, addr string) (net.Conn, error) {
// write VER,CMD,RSV
_, err := conn.Write([]byte{socks5Version, byte(cmd), 0})
if err != nil {
return nil, err
}
// write ATYP,DST.ADDR,DST.PORT
if cmd == cmdUDPAssociate {
// If the client is not in possession of the information at the time of the
// UDP ASSOCIATE, the client MUST use a port number and address of all zeros.
err = writeAddrPort(conn, ":0", d.localResolve)
} else {
err = writeAddrPort(conn, addr, d.localResolve)
}
if err != nil {
return nil, err
}
// read VER,REP,RSV
respHeaderBytes := make([]byte, 3)
_, err = io.ReadFull(conn, respHeaderBytes)
if err != nil {
return nil, err
}
if respHeaderBytes[0] != socks5Version {
return nil, fmt.Errorf("invalid proxy version :%d", respHeaderBytes[0])
}
if respHeaderBytes[1] != replySucceeded {
return nil, fmt.Errorf("proxy request failed with reply :%d", respHeaderBytes[1])
}
// read ATYP,BND.ADDR,BND.PORT
proxyAddr, err := readAddrPort(conn)
if err != nil {
return nil, err
}
// no more action for CONNECT and BIND as they use the underlying connection
switch cmd {
case cmdConnect, cmdBind:
return conn, nil
case cmdUDPAssociate:
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
portInt, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
udpConn, err := net.ListenPacket("udp", ":0")
if err != nil {
return nil, err
}
associateConn, err := NewUDPAssociateConn(udpConn, proxyAddr, &net.UDPAddr{
IP: net.ParseIP(host),
Port: portInt,
})
if err != nil {
return nil, err
}
go monitorConn(conn, associateConn)
return associateConn, nil
default:
return nil, fmt.Errorf("invalid cmd %d", cmd)
}
}
func (d *Dialer) dialProxyServer(ctx context.Context) (net.Conn, error) {
var netDialer net.Dialer
return netDialer.DialContext(ctx, "tcp", d.proxyAddr)
}
func (d *Dialer) negotiateAuthentication(conn net.Conn) error {
var err error
if d.proxyAuth == nil {
_, err = conn.Write([]byte{socks5Version, 1, noAuthMethod})
} else {
_, err = conn.Write([]byte{socks5Version, 2, userPassMethod, noAuthMethod})
}
if err != nil {
return err
}
methodResponseBuf := make([]byte, 2)
_, err = io.ReadFull(conn, methodResponseBuf)
if err != nil {
return err
}
if methodResponseBuf[0] != socks5Version {
return fmt.Errorf("invalid proxy version: %d", methodResponseBuf[0])
}
switch methodResponseBuf[1] {
case noAuthMethod:
case userPassMethod:
reqBytes := make([]byte, 0)
reqBytes = append(reqBytes, subnegotiationVersion)
reqBytes = append(reqBytes, byte(d.proxyAuth.usernameLen))
reqBytes = append(reqBytes, []byte(d.proxyAuth.username)...)
reqBytes = append(reqBytes, byte(d.proxyAuth.passwordLen))
reqBytes = append(reqBytes, []byte(d.proxyAuth.password)...)
_, err = conn.Write(reqBytes)
if err != nil {
return err
}
respBuf := make([]byte, 2)
_, err = io.ReadFull(conn, respBuf)
if err != nil {
return err
}
if respBuf[0] != subnegotiationVersion {
return fmt.Errorf("invalid subnegotiation version %d", respBuf[0])
}
if respBuf[1] != authStatusSuccess {
return fmt.Errorf("authentication failed with status %d", respBuf[1])
}
default:
return fmt.Errorf("invalid method: %d", methodResponseBuf[1])
}
return nil
}
type listener struct {
ctx context.Context
dialer *Dialer
network string
address string
}
func (l *listener) Accept() (net.Conn, error) {
proxyConn, err := l.dialer.dialProxyServer(l.ctx)
if err != nil {
return nil, err
}
err = l.dialer.negotiateAuthentication(proxyConn)
if err != nil {
return nil, err
}
conn, err := l.dialer.negotiateCmd(cmdBind, proxyConn, l.address)
if err != nil {
return nil, fmt.Errorf("failed to make initial negotiation, %w", err)
}
return conn, nil
}
func (l *listener) Close() error {
return nil
}
func (l *listener) Addr() net.Addr {
return nil
}