-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprotocol_socks5.go
383 lines (331 loc) · 7.32 KB
/
protocol_socks5.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package socks5
import (
"errors"
"fmt"
"io"
)
const (
MethodNone = 0x00
MethodUserPass = 0x02
MethodNoAcceptable = 0xff
VerAuthUserPass = 0x01
AuthStatusSuccess = 0x00
AuthStatusFailure = 0x01
CmdConnect = 0x01
CmdBind = 0x02
CmdUDP = 0x03
ATypIPV4 = 0x01
ATypDomainname = 0x03
ATypIPV6 = 0x04
RepSuccess = 0x00
RepServerFailure = 0x01
RepRuleFailure = 0x02
RepNetworkUnreachable = 0x03
RepHostUnreachable = 0x04
RepConnectionRefused = 0x05
RepTTLExpired = 0x06
RepCmdNotSupported = 0x07
RepAddrTypeNotSupported = 0x08
)
var (
ErrMethodNoAcceptable = errors.New("no acceptable method")
ErrAuthFailed = errors.New("User authentication failed")
NoSupportedAuth = errors.New("no supported auth")
ErrAuthUserPassVer = errors.New("auth user pass version")
ErrCmdNotSupport = errors.New("cmd not support")
ErrAddrType = fmt.Errorf("Unrecognized address type")
ErrSocksVersion = fmt.Errorf("not socks version 5")
ErrMethod = fmt.Errorf("Unsupport method")
ErrBadRequest = fmt.Errorf("bad request")
ErrUDPFrag = fmt.Errorf("Frag !=0 not supported")
)
type MethodSelectReq struct {
Ver byte
NMethods byte
Methods []byte
}
func NewMethodSelectReq(methods []byte) *MethodSelectReq {
return &MethodSelectReq{
Ver: VerSocks5,
NMethods: byte(len(methods)),
Methods: methods,
}
}
func NewMethodSelectReqFrom(r io.Reader) (*MethodSelectReq, error) {
b := make([]byte, 1)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
nMethod := int(b[0])
methods := make([]byte, nMethod)
_, err = io.ReadFull(r, methods)
if err != nil {
return nil, err
}
return &MethodSelectReq{
Ver: 0x05,
NMethods: b[0],
Methods: methods,
}, nil
}
func (p *MethodSelectReq) ToBytes() []byte {
ret := []byte{p.Ver, p.NMethods}
ret = append(ret, p.Methods...)
return ret
}
type MethodSelectReply struct {
Ver byte
Method byte
}
func NewMethodSelectReply(method byte) *MethodSelectReply {
return &MethodSelectReply{
Ver: VerSocks5,
Method: method,
}
}
func NewMethodSelectReplyFrom(r io.Reader) (*MethodSelectReply, error) {
b := make([]byte, 2)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
return &MethodSelectReply{
Ver: b[0],
Method: b[1],
}, nil
}
func (p *MethodSelectReply) ToBytes() []byte {
return []byte{p.Ver, p.Method}
}
type UserPassAuthReq struct {
Ver byte
ULen byte
UserName []byte
PLen byte
Password []byte
}
func NewUserPassAuthReq(username []byte, password []byte) *UserPassAuthReq {
return &UserPassAuthReq{
Ver: VerAuthUserPass,
ULen: byte(len(username)),
UserName: username,
PLen: byte(len(password)),
Password: password,
}
}
func NewUserPassAuthReqFrom(r io.Reader) (*UserPassAuthReq, error) {
b := make([]byte, 1)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
ver := b[0]
_, err = io.ReadFull(r, b)
if err != nil {
return nil, err
}
uLen := int(b[0])
userName := make([]byte, uLen)
_, err = io.ReadFull(r, userName)
if err != nil {
return nil, err
}
_, err = io.ReadFull(r, b)
if err != nil {
return nil, err
}
pLen := int(b[0])
password := make([]byte, pLen)
_, err = io.ReadFull(r, password)
if err != nil {
return nil, err
}
return &UserPassAuthReq{
Ver: ver,
ULen: byte(uLen),
UserName: userName,
PLen: byte(pLen),
Password: password,
}, nil
}
func (p *UserPassAuthReq) ToBytes() []byte {
ret := []byte{p.Ver, p.ULen}
ret = append(ret, p.UserName...)
ret = append(ret, p.PLen)
ret = append(ret, p.Password...)
return ret
}
type UserPassAuthReply struct {
Ver byte
Status byte
}
func (p *UserPassAuthReply) ToBytes() []byte {
return []byte{p.Ver, p.Status}
}
func NewUserPassAuthReply(status byte) *UserPassAuthReply {
return &UserPassAuthReply{
Ver: VerAuthUserPass,
Status: status,
}
}
func NewUserPassAuthReplyFrom(r io.Reader) (*UserPassAuthReply, error) {
b := make([]byte, 2)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
return &UserPassAuthReply{
Ver: b[0],
Status: b[1],
}, nil
}
type Request struct {
Ver byte
Cmd byte
Rsv byte //0x00
Atyp byte
DstAddr []byte
DstPort []byte //2 bytes
}
func NewRequestFrom(r io.Reader) (*Request, error) {
b := []byte{0, 0, 0}
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
addrByte, err := NewAddrByteFrom(r)
if err != nil {
return nil, err
}
aType, addr, port := addrByte.Split()
return &Request{
Ver: b[0],
Cmd: b[1],
Rsv: b[2],
Atyp: aType,
DstAddr: addr,
DstPort: port,
}, nil
}
func NewRequest(cmd byte, addrByte AddrByte) *Request {
aType, addr, port := addrByte.Split()
return &Request{
Ver: VerSocks5,
Cmd: cmd,
Rsv: 0,
Atyp: aType,
DstAddr: addr,
DstPort: port,
}
}
func (p *Request) Address() string {
var bAddr []byte
bAddr = append(bAddr, p.Atyp)
bAddr = append(bAddr, p.DstAddr...)
bAddr = append(bAddr, p.DstPort...)
return AddrByte(bAddr).String()
}
func (p *Request) ToBytes() []byte {
ret := []byte{p.Ver, p.Cmd, p.Rsv, p.Atyp}
ret = append(ret, p.DstAddr...)
ret = append(ret, p.DstPort...)
return ret
}
type Reply struct {
Ver byte
Rep byte
Rsv byte
Atyp byte
BndAddr []byte
BndPort []byte //2 bytes
}
func (p *Reply) Address() string {
var bAddr []byte
bAddr = append(bAddr, p.Atyp)
bAddr = append(bAddr, p.BndAddr...)
bAddr = append(bAddr, p.BndPort...)
return AddrByte(bAddr).String()
}
func (p *Reply) ToBytes() []byte {
ret := []byte{p.Ver, p.Rep, p.Rsv, p.Atyp}
ret = append(ret, p.BndAddr...)
ret = append(ret, p.BndPort...)
return ret
}
func NewReply(rep byte, addrByte AddrByte) *Reply {
aType, addr, port := addrByte.Split()
return &Reply{
Ver: VerSocks5,
Rep: rep,
Rsv: 0,
Atyp: aType,
BndAddr: addr,
BndPort: port,
}
}
func NewReplyFrom(r io.Reader) (*Reply, error) {
b := []byte{0, 0, 0}
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
addrByte, err := NewAddrByteFrom(r)
if err != nil {
return nil, err
}
aType, addr, port := addrByte.Split()
return &Reply{
Ver: b[0],
Rep: b[1],
Rsv: b[2],
Atyp: aType,
BndAddr: addr,
BndPort: port,
}, nil
}
type UDPDatagram struct {
Rsv []byte //0x00,0x00
Frag byte
AType byte
DstAddr []byte
DstPort []byte
Data []byte
}
func (p *UDPDatagram) ToBytes() []byte {
b := []byte{}
b = append(b, p.Rsv...)
b = append(b, p.Frag)
b = append(b, p.AType)
b = append(b, p.DstAddr...)
b = append(b, p.DstPort...)
b = append(b, p.Data...)
return b
}
func (p *UDPDatagram) Address() string {
var bAddr []byte
bAddr = append(bAddr, p.AType)
bAddr = append(bAddr, p.DstAddr...)
bAddr = append(bAddr, p.DstPort...)
return AddrByte(bAddr).String()
}
func NewUDPDatagram(addrByte AddrByte, data []byte) *UDPDatagram {
atype, addr, port := addrByte.Split()
return &UDPDatagram{
Rsv: []byte{0, 0},
Frag: 0,
AType: atype,
DstAddr: addr,
DstPort: port,
Data: data,
}
}
func NewUDPDatagramFromBytes(b []byte) (*UDPDatagram, error) {
if len(b) < 4 {
return nil, ErrBadRequest
}
bAddr, err := NewAddrByteFromByte(b[3:])
if err != nil {
return nil, err
}
data := b[3+len(bAddr):]
return NewUDPDatagram(bAddr, data), nil
}