forked from samuel/go-socks
/
dial.go
244 lines (212 loc) · 5.45 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
// 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 (
"errors"
"io"
"net"
"strconv"
)
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 = errors.New("authentication failed")
ErrInvalidProxyResponse = errors.New("invalid proxy response")
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"),
}
)
type Proxy struct {
Addr string
Username string
Password string
}
func (p *Proxy) Dial(network, addr string) (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.Dial("tcp", p.Addr)
if err != nil {
return nil, err
}
buf := make([]byte, 32+len(host)+len(p.Username)+len(p.Password))
// Initial greeting
buf[0] = protocolVersion
if p.Username != "" {
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(p.Username)+len(p.Password)]
buf[0] = 1 // version
buf[1] = byte(len(p.Username))
copy(buf[2:], p.Username)
buf[2+len(p.Username)] = byte(len(p.Password))
copy(buf[3+len(p.Username):], p.Password)
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
}