-
Notifications
You must be signed in to change notification settings - Fork 43
/
dial.go
274 lines (243 loc) · 6.44 KB
/
dial.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
// Copyright 2012 Samuel Stauffer. All rights reserved.
// Use of this source code is governed by a 3-clause BSD
// license that can be found in the LICENSE file.
// Current limitations:
//
// - GSS-API authentication is not supported
// - only SOCKS version 5 is supported
// - TCP bind and UDP not yet supported
//
// Example http client over SOCKS5:
//
// proxy := &socks.Proxy{"127.0.0.1:1080"}
// tr := &http.Transport{
// Dial: proxy.Dial,
// }
// client := &http.Client{Transport: tr}
// resp, err := client.Get("https://example.com")
package socks
import (
"crypto/rand"
"encoding/hex"
"errors"
"io"
"net"
"strconv"
"time"
)
const (
protocolVersion = 5
defaultPort = 1080
authNone = 0
authGssAPI = 1
authUsernamePassword = 2
authUnavailable = 0xff
commandTCPConnect = 1
commandTCPBind = 2
commandUDPAssociate = 3
addressTypeIPv4 = 1
addressTypeDomain = 3
addressTypeIPv6 = 4
statusRequestGranted = 0
statusGeneralFailure = 1
statusConnectionNotAllowed = 2
statusNetworkUnreachable = 3
statusHostUnreachable = 4
statusConnectionRefused = 5
statusTTLExpired = 6
statusCommandNotSupport = 7
statusAddressTypeNotSupported = 8
)
var (
// ErrAuthFailed returns error information when auth failed.
ErrAuthFailed = errors.New("authentication failed")
// ErrInvalidProxyResponse returns error information when catch an invalid proxy response.
ErrInvalidProxyResponse = errors.New("invalid proxy response")
// ErrNoAcceptableAuthMethod returns error information when there is no acceptable authentication method.
ErrNoAcceptableAuthMethod = errors.New("no acceptable authentication method")
statusErrors = map[byte]error{
statusGeneralFailure: errors.New("general failure"),
statusConnectionNotAllowed: errors.New("connection not allowed by ruleset"),
statusNetworkUnreachable: errors.New("network unreachable"),
statusHostUnreachable: errors.New("host unreachable"),
statusConnectionRefused: errors.New("connection refused by destination host"),
statusTTLExpired: errors.New("TTL expired"),
statusCommandNotSupport: errors.New("command not supported / protocol error"),
statusAddressTypeNotSupported: errors.New("address type not supported"),
}
)
// Proxy defines the data structure.
type Proxy struct {
Addr string
Username string
Password string
TorIsolation bool
}
// Dial calls to given address and returns its connection.
func (p *Proxy) Dial(network, addr string) (net.Conn, error) {
return p.dial(network, addr, 0)
}
// DialTimeout calls to given address and returns its connection within the limit time.
func (p *Proxy) DialTimeout(network, addr string, timeout time.Duration) (net.Conn, error) {
return p.dial(network, addr, timeout)
}
func (p *Proxy) dial(network, addr string, timeout time.Duration) (net.Conn, error) {
host, strPort, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(strPort)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout("tcp", p.Addr, timeout)
if err != nil {
return nil, err
}
var user, pass string
if p.TorIsolation {
var b [16]byte
_, err := io.ReadFull(rand.Reader, b[:])
if err != nil {
conn.Close()
return nil, err
}
user = hex.EncodeToString(b[0:8])
pass = hex.EncodeToString(b[8:16])
} else {
user = p.Username
pass = p.Password
}
buf := make([]byte, 32+len(host)+len(user)+len(pass))
// Initial greeting
buf[0] = protocolVersion
if user != "" {
buf = buf[:4]
buf[1] = 2 // num auth methods
buf[2] = authNone
buf[3] = authUsernamePassword
} else {
buf = buf[:3]
buf[1] = 1 // num auth methods
buf[2] = authNone
}
_, err = conn.Write(buf)
if err != nil {
conn.Close()
return nil, err
}
// Server's auth choice
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
conn.Close()
return nil, err
}
if buf[0] != protocolVersion {
conn.Close()
return nil, ErrInvalidProxyResponse
}
err = nil
switch buf[1] {
default:
err = ErrInvalidProxyResponse
case authUnavailable:
err = ErrNoAcceptableAuthMethod
case authGssAPI:
err = ErrNoAcceptableAuthMethod
case authUsernamePassword:
buf = buf[:3+len(user)+len(pass)]
buf[0] = 1 // version
buf[1] = byte(len(user))
copy(buf[2:], user)
buf[2+len(user)] = byte(len(pass))
copy(buf[3+len(user):], pass)
if _, err = conn.Write(buf); err != nil {
conn.Close()
return nil, err
}
if _, err = io.ReadFull(conn, buf[:2]); err != nil {
conn.Close()
return nil, err
}
if buf[0] != 1 { // version
err = ErrInvalidProxyResponse
} else if buf[1] != 0 { // 0 = succes, else auth failed
err = ErrAuthFailed
}
case authNone:
// Do nothing
}
if err != nil {
conn.Close()
return nil, err
}
// Command / connection request
buf = buf[:7+len(host)]
buf[0] = protocolVersion
buf[1] = commandTCPConnect
buf[2] = 0 // reserved
buf[3] = addressTypeDomain
buf[4] = byte(len(host))
copy(buf[5:], host)
buf[5+len(host)] = byte(port >> 8)
buf[6+len(host)] = byte(port & 0xff)
if _, err := conn.Write(buf); err != nil {
conn.Close()
return nil, err
}
// Server response
if _, err := io.ReadFull(conn, buf[:4]); err != nil {
conn.Close()
return nil, err
}
if buf[0] != protocolVersion {
conn.Close()
return nil, ErrInvalidProxyResponse
}
if buf[1] != statusRequestGranted {
conn.Close()
err := statusErrors[buf[1]]
if err == nil {
err = ErrInvalidProxyResponse
}
return nil, err
}
paddr := &ProxiedAddr{Net: network}
switch buf[3] {
default:
conn.Close()
return nil, ErrInvalidProxyResponse
case addressTypeIPv4:
if _, err := io.ReadFull(conn, buf[:4]); err != nil {
conn.Close()
return nil, err
}
paddr.Host = net.IP(buf).String()
case addressTypeIPv6:
if _, err := io.ReadFull(conn, buf[:16]); err != nil {
conn.Close()
return nil, err
}
paddr.Host = net.IP(buf).String()
case addressTypeDomain:
if _, err := io.ReadFull(conn, buf[:1]); err != nil {
conn.Close()
return nil, err
}
domainLen := buf[0]
if _, err := io.ReadFull(conn, buf[:domainLen]); err != nil {
conn.Close()
return nil, err
}
paddr.Host = string(buf[:domainLen])
}
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
conn.Close()
return nil, err
}
paddr.Port = int(buf[0])<<8 | int(buf[1])
return &proxiedConn{
conn: conn,
boundAddr: paddr,
remoteAddr: &ProxiedAddr{network, host, port},
}, nil
}