-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks.go
333 lines (291 loc) · 7.46 KB
/
socks.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package protocol
import (
"encoding/binary"
"io"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors"
"github.com/v2ray/v2ray-core/transport"
)
const (
socksVersion = byte(0x05)
socks4Version = byte(0x04)
AuthNotRequired = byte(0x00)
AuthGssApi = byte(0x01)
AuthUserPass = byte(0x02)
AuthNoMatchingMethod = byte(0xFF)
Socks4RequestGranted = byte(90)
Socks4RequestRejected = byte(91)
)
// Authentication request header of Socks5 protocol
type Socks5AuthenticationRequest struct {
version byte
nMethods byte
authMethods [256]byte
}
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
for i := 0; i < int(request.nMethods); i++ {
if request.authMethods[i] == method {
return true
}
}
return false
}
func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, auth4 Socks4AuthenticationRequest, err error) {
buffer := alloc.NewSmallBuffer()
defer buffer.Release()
nBytes, err := reader.Read(buffer.Value)
if err != nil {
return
}
if nBytes < 2 {
log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
err = transport.CorruptedPacket
return
}
if buffer.Value[0] == socks4Version {
auth4.Version = buffer.Value[0]
auth4.Command = buffer.Value[1]
auth4.Port = binary.BigEndian.Uint16(buffer.Value[2:4])
copy(auth4.IP[:], buffer.Value[4:8])
err = Socks4Downgrade
return
}
auth.version = buffer.Value[0]
if auth.version != socksVersion {
log.Warning("Unknown protocol version %d", auth.version)
err = proxyerrors.InvalidProtocolVersion
return
}
auth.nMethods = buffer.Value[1]
if auth.nMethods <= 0 {
log.Info("Zero length of authentication methods")
err = transport.CorruptedPacket
return
}
if nBytes-2 != int(auth.nMethods) {
log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
err = transport.CorruptedPacket
return
}
copy(auth.authMethods[:], buffer.Value[2:nBytes])
return
}
type Socks5AuthenticationResponse struct {
version byte
authMethod byte
}
func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
return &Socks5AuthenticationResponse{
version: socksVersion,
authMethod: authMethod,
}
}
func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
_, err := writer.Write([]byte{r.version, r.authMethod})
return err
}
type Socks5UserPassRequest struct {
version byte
username string
password string
}
func (request Socks5UserPassRequest) Username() string {
return request.username
}
func (request Socks5UserPassRequest) Password() string {
return request.password
}
func (request Socks5UserPassRequest) AuthDetail() string {
return request.username + ":" + request.password
}
func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
buffer := alloc.NewSmallBuffer()
defer buffer.Release()
_, err = reader.Read(buffer.Value[0:2])
if err != nil {
return
}
request.version = buffer.Value[0]
nUsername := buffer.Value[1]
nBytes, err := reader.Read(buffer.Value[:nUsername])
if err != nil {
return
}
request.username = string(buffer.Value[:nBytes])
_, err = reader.Read(buffer.Value[0:1])
if err != nil {
return
}
nPassword := buffer.Value[0]
nBytes, err = reader.Read(buffer.Value[:nPassword])
if err != nil {
return
}
request.password = string(buffer.Value[:nBytes])
return
}
type Socks5UserPassResponse struct {
version byte
status byte
}
func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
return Socks5UserPassResponse{
version: socksVersion,
status: status,
}
}
func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
_, err := writer.Write([]byte{response.version, response.status})
return err
}
const (
AddrTypeIPv4 = byte(0x01)
AddrTypeIPv6 = byte(0x04)
AddrTypeDomain = byte(0x03)
CmdConnect = byte(0x01)
CmdBind = byte(0x02)
CmdUdpAssociate = byte(0x03)
)
type Socks5Request struct {
Version byte
Command byte
AddrType byte
IPv4 [4]byte
Domain string
IPv6 [16]byte
Port uint16
}
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
buffer := alloc.NewSmallBuffer()
defer buffer.Release()
nBytes, err := reader.Read(buffer.Value[:4])
if err != nil {
return
}
if nBytes < 4 {
err = transport.CorruptedPacket
return
}
request = &Socks5Request{
Version: buffer.Value[0],
Command: buffer.Value[1],
// buffer[2] is a reserved field
AddrType: buffer.Value[3],
}
switch request.AddrType {
case AddrTypeIPv4:
nBytes, err = reader.Read(request.IPv4[:])
if err != nil {
return
}
if nBytes != 4 {
err = transport.CorruptedPacket
return
}
case AddrTypeDomain:
nBytes, err = reader.Read(buffer.Value[0:1])
if err != nil {
return
}
domainLength := buffer.Value[0]
nBytes, err = reader.Read(buffer.Value[:domainLength])
if err != nil {
return
}
if nBytes != int(domainLength) {
log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
err = transport.CorruptedPacket
return
}
request.Domain = string(buffer.Value[:domainLength])
case AddrTypeIPv6:
nBytes, err = reader.Read(request.IPv6[:])
if err != nil {
return
}
if nBytes != 16 {
err = transport.CorruptedPacket
return
}
default:
log.Info("Unexpected address type %d", request.AddrType)
err = transport.CorruptedPacket
return
}
nBytes, err = reader.Read(buffer.Value[:2])
if err != nil {
return
}
if nBytes != 2 {
err = transport.CorruptedPacket
return
}
request.Port = binary.BigEndian.Uint16(buffer.Value[:2])
return
}
func (request *Socks5Request) Destination() v2net.Destination {
var address v2net.Address
switch request.AddrType {
case AddrTypeIPv4:
address = v2net.IPAddress(request.IPv4[:], request.Port)
case AddrTypeIPv6:
address = v2net.IPAddress(request.IPv6[:], request.Port)
case AddrTypeDomain:
address = v2net.DomainAddress(request.Domain, request.Port)
default:
panic("Unknown address type")
}
return v2net.NewTCPDestination(address)
}
const (
ErrorSuccess = byte(0x00)
ErrorGeneralFailure = byte(0x01)
ErrorConnectionNotAllowed = byte(0x02)
ErrorNetworkUnreachable = byte(0x03)
ErrorHostUnUnreachable = byte(0x04)
ErrorConnectionRefused = byte(0x05)
ErrorTTLExpired = byte(0x06)
ErrorCommandNotSupported = byte(0x07)
ErrorAddressTypeNotSupported = byte(0x08)
)
type Socks5Response struct {
Version byte
Error byte
AddrType byte
IPv4 [4]byte
Domain string
IPv6 [16]byte
Port uint16
}
func NewSocks5Response() *Socks5Response {
return &Socks5Response{
Version: socksVersion,
}
}
func (r *Socks5Response) SetIPv4(ipv4 []byte) {
r.AddrType = AddrTypeIPv4
copy(r.IPv4[:], ipv4)
}
func (r *Socks5Response) SetIPv6(ipv6 []byte) {
r.AddrType = AddrTypeIPv6
copy(r.IPv6[:], ipv6)
}
func (r *Socks5Response) SetDomain(domain string) {
r.AddrType = AddrTypeDomain
r.Domain = domain
}
func (r *Socks5Response) Write(buffer *alloc.Buffer) {
buffer.AppendBytes(r.Version, r.Error, 0x00 /* reserved */, r.AddrType)
switch r.AddrType {
case 0x01:
buffer.Append(r.IPv4[:])
case 0x03:
buffer.AppendBytes(byte(len(r.Domain)))
buffer.Append([]byte(r.Domain))
case 0x04:
buffer.Append(r.IPv6[:])
}
buffer.AppendBytes(byte(r.Port>>8), byte(r.Port))
}