-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
718 lines (640 loc) · 16.5 KB
/
protocol.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package socksy5
import (
"encoding/binary"
"fmt"
"io"
"net"
"net/netip"
"reflect"
"strconv"
"sync"
"github.com/google/uuid"
)
// SOCKS5 VER byte
const VerSOCKS5 byte = 0x05
// Authentication METHOD codes.
const (
MethodNoAuth byte = 0x00
MethodGSSAPI byte = 0x01
MethodUsrPwd byte = 0x02
MethodCHAP byte = 0x03
MethodCRAM byte = 0x05
MethodSSL byte = 0x06
MethodNDS byte = 0x07
MethodMAF byte = 0x08
MethodJRB byte = 0x09
MethodNoAccepted byte = 0xFF
)
// CMD codes.
const (
CmdCONNECT byte = 0x01
CmdBIND byte = 0x02
CmdASSOC byte = 0x03
)
// Value of reserved bytes
const RSV byte = 0x00
// ATYP codes (address types)
const (
ATYPV4 byte = 0x01 // IPv4
ATYPDOMAIN byte = 0x03 // Fully-qualified domain name
ATYPV6 byte = 0x04 // IPv6
)
// REP reply codes.
const (
RepSucceeded byte = 0x00
RepGeneralFailure byte = 0x01
RepConnNotAllowedByRuleset byte = 0x02
RepNetworkUnreachable byte = 0x03
RepHostUnreachable byte = 0x04
RepConnRefused byte = 0x05
RepTtlExpired byte = 0x06
RepCmdNotSupported byte = 0x07
RepAddrTypeNotSupported byte = 0x08
)
// An AddrPort represents the address and the port
// sent in SOCKS5 requests and replies.
type AddrPort struct {
// ATYP byte value
Type byte
// If ATYP is ATYPDOMAIN,
// the first byte that specifies the FQDN length is omitted,
// length of Addr represents it implicitly.
Addr []byte
Port uint16
// One of "tcp" and "udp", [AddrPort.Network] relies on this field.
Protocol string
}
var emptyAddr = &AddrPort{Type: ATYPDOMAIN, Addr: nil, Port: 0}
func readAddrPort(reader io.Reader) (*AddrPort, error) {
atyp, err := readByte(reader)
if err != nil {
return nil, err
}
var addr []byte
switch atyp {
case ATYPV4:
addr = make([]byte, 4)
case ATYPV6:
addr = make([]byte, 16)
case ATYPDOMAIN:
l, err := readByte(reader)
if err != nil {
return nil, err
}
addr = make([]byte, l)
default:
return nil, ATYPNotSupportedError(atyp)
}
if _, err := io.ReadFull(reader, addr); err != nil {
return nil, err
}
port, err := readUInt16BigEndian(reader)
if err != nil {
return nil, err
}
return &AddrPort{
Type: atyp,
Addr: addr,
Port: port,
}, nil
}
// ParseAddrPort parses s to [AddrPort].
// If s is not a valid IP address, ParseAddrPort will try parse it
// as <host name>:<port>, WITHOUT syntax checking on the host name.
//
// In the returned [AddrPort], [AddrPort.Protocol] will be empty.
func ParseAddrPort(s string) (*AddrPort, error) {
a := new(AddrPort)
ipPort, err := netip.ParseAddrPort(s)
if err != nil { // then consider it a domain name
host, portS, err := net.SplitHostPort(s)
if err != nil {
return nil, err
}
port, err := parseUint16(portS)
if err != nil {
return nil, err
}
a.Type = ATYPDOMAIN
a.Addr = []byte(host)
a.Port = uint16(port)
return a, nil
}
ip := ipPort.Addr()
if ip.Is4() {
a.Type = ATYPV4
} else {
a.Type = ATYPV6
}
a.Addr, _ = ip.MarshalBinary()
a.Port = ipPort.Port()
return a, nil
}
// Network returns the network of a.
// If a is nil, "<nil>" is returned.
// If a.Type is one of [ATYPV4] or [ATYPV6],
// Network will append "4" or "6" to a.Protocol accordingly.
// If a.Type is [ATYPDOMAIN], Network will just return a.Protocol.
// Otherwise, Network returns the hex of a.Type.
func (a *AddrPort) Network() string {
if a == nil {
return "<nil>"
}
switch a.Type {
case ATYPV4:
return a.Protocol + "4"
case ATYPV6:
return a.Protocol + "6"
case ATYPDOMAIN:
return a.Protocol
}
return fmt.Sprintf("0x%02X", a.Type)
}
func (a *AddrPort) String() string {
if a == nil {
return "<nil>"
}
host := a.Host()
portStr := strconv.Itoa(int(a.Port))
return net.JoinHostPort(host, portStr)
}
// Host returns the string form of a, without the port part.
func (a *AddrPort) Host() string {
// Although actually String relies on Host, lol
if a == nil {
return "<nil>"
}
if a.Type == ATYPV4 || a.Type == ATYPV6 {
return net.IP(a.Addr).String()
}
if a.Type == ATYPDOMAIN {
return string(a.Addr)
}
return fmt.Sprintf("0x%02X", a.Addr)
}
// Equal tests whether a and x are the same address,
// returns false if either a or x is nil.
// Both a.Protocol and x.Protocol are ignored.
func (a *AddrPort) Equal(x *AddrPort) bool {
if a == nil || x == nil {
return false
}
if a == x {
return true
}
return a.Type == x.Type && a.Port == x.Port && reflect.DeepEqual(a.Addr, x.Addr)
}
func (a *AddrPort) cpy() *AddrPort {
b := new(AddrPort)
b.Type = a.Type
b.Addr = make([]byte, len(a.Addr))
copy(b.Addr, a.Addr)
b.Port = a.Port
b.Protocol = a.Protocol
return b
}
// MarshalBinary returns raw bytes used in SOCKS5 traffic. (ATYP+ADDR+PORT)
func (a *AddrPort) MarshalBinary() (data []byte, err error) {
var l int
if a.Type == ATYPDOMAIN {
if len(a.Addr) > 0xFF {
return nil, fmt.Errorf("%w: domain name length too long (%d Bytes)", ErrMalformed, len(a.Addr))
}
l = 1 + 1 + len(a.Addr) + 2
data = make([]byte, l)
data[1] = byte(l - 4)
copy(data[2:], a.Addr)
} else if a.Type == ATYPV4 || a.Type == ATYPV6 {
if a.Type == ATYPV4 && len(a.Addr) != 4 || a.Type == ATYPV6 && len(a.Addr) != 16 {
return nil, fmt.Errorf("%w: address length incorrect (%d Bytes)", ErrMalformed, len(a.Addr))
}
l = 1 + len(a.Addr) + 2
data = make([]byte, l)
copy(data[1:], a.Addr)
} else {
return nil, ATYPNotSupportedError(a.Type)
}
data[0] = a.Type
binary.BigEndian.PutUint16(data[l-2:], a.Port)
return
}
// A Handshake represents the version identifier/method selection message.
// The message is called handshake in this entire module because...its full name
// is just too long.
// Handshake will be denied automatically if it's not accepted or denied
// after [PeriodAutoDeny].
//
// All methods of Handshake can be called simultainously.
type Handshake struct {
ver byte
nmethods byte
methods []byte
methodChosen byte
laddr net.Addr
raddr net.Addr
uuid uuid.UUID
wg *sync.WaitGroup // TODO Why references??
once *sync.Once
neg Subnegotiator
timeoutDeny bool
}
func readHandshake(reader io.Reader) (*Handshake, error) {
hs := &Handshake{
wg: new(sync.WaitGroup),
once: new(sync.Once),
}
var err error
hs.ver, err = readByte(reader)
if err != nil {
return nil, err
}
if hs.ver != VerSOCKS5 {
return nil, VerIncorrectError(hs.ver)
}
hs.nmethods, err = readByte(reader)
if err != nil {
return nil, err
}
hs.methods = make([]byte, hs.nmethods)
_, err = io.ReadFull(reader, hs.methods)
if err != nil {
return nil, err
}
return hs, nil
}
// Accept accepts the handshake, but also instead denies the request
// if params are invalid, e.g. when method is MethodNoAccepted.
//
// Can be called only once, furthur calls are no-op.
func (hs *Handshake) Accept(method byte, neg Subnegotiator) (ok bool) {
if neg == nil || method == MethodNoAccepted {
hs.deny(false)
return
}
if isByteOneOf(method, hs.methods...) {
return hs.accept(method, neg)
}
hs.deny(false)
return
}
func (hs *Handshake) accept(method byte, neg Subnegotiator) (ok bool) {
hs.once.Do(func() {
hs.methodChosen = method
hs.neg = neg
hs.wg.Done()
ok = true
})
return
}
// Deny denies the handshake by returning NO ACCEPTABLE METHODS.
//
// Can be called only once, furthur calls are no-op.
func (hs *Handshake) Deny() (ok bool) {
return hs.deny(false)
}
func (hs *Handshake) deny(timeoutDeny bool) (ok bool) {
hs.once.Do(func() {
hs.methodChosen = MethodNoAccepted
hs.timeoutDeny = timeoutDeny
hs.wg.Done()
ok = true
})
return
}
// Methods returns client's supported auth methods.
// Methods might return 0 method,
// or include method code 0xFF if the client did send so.
func (hs *Handshake) Methods() []byte {
s := make([]byte, len(hs.methods))
copy(s, hs.methods)
return s
}
func (hs *Handshake) LocalAddr() net.Addr {
return hs.laddr
}
func (hs *Handshake) RemoteAddr() net.Addr {
return hs.raddr
}
// UUID returns the UUID of the session of hs.
// As soon as the [MidLayer] read the handshake message, the connection is
// considered as a valid session and is bound with a UUID.
// You can use the UUID to tell which handshake and which request belongs to
// which connection.
func (hs *Handshake) UUID() uuid.UUID {
return hs.uuid
}
// A Request represents a client request.
// Use [ConnectRequest], [BindRequest] and [AssocRequest] for handling.
//
// Accept / Deny methods of different request types can be called only once.
// Furthur calls are no-op and return ok being false.
//
// Requests are denied if params passed to Accept funcs are invalid, e.g.
// addr string doesn't contain a port number,
// net.Addr returned by conn params is invalid.
// However, port 0 is considered valid and will be sent as is.
//
// A Request will be denied automatically if it's not accepted or denied
// after [PeriodAutoDeny], with exception being [BindRequest], see
// [BindRequest.Bind].
//
// All methods of all request types can be called simultainously.
type Request struct {
cmd byte
dst *AddrPort
capper Capsulator
uuid uuid.UUID
raddr net.Addr
laddr net.Addr
once *sync.Once // We use references here because Request will be value-copied.
wg *sync.WaitGroup // See the request wrapping part in MidLayer.ServeClient.
reply *reply
timeoutDeny bool
}
func readRequest(reader io.Reader) (*Request, error) {
ver, err := readByte(reader)
if err != nil {
return nil, err
}
if ver != VerSOCKS5 {
return nil, VerIncorrectError(ver)
}
req := new(Request)
req.cmd, err = readByte(reader)
if err != nil {
return nil, err
}
rsv, err := readByte(reader)
if err != nil {
return nil, err
}
if rsv != RSV {
return nil, RsvViolationError(rsv)
}
req.dst, err = readAddrPort(reader)
if err != nil {
return nil, err
}
req.once = new(sync.Once)
req.wg = new(sync.WaitGroup)
req.reply = new(reply)
return req, nil
}
func (r *Request) LocalAddr() net.Addr {
return r.laddr
}
func (r *Request) RemoteAddr() net.Addr {
return r.raddr
}
// Dst returns the DST fields in the request message.
func (r *Request) Dst() *AddrPort {
return r.dst.cpy()
}
// Deny denies the request with REP byte code.
// If rep is RepSucceeded, it's replaced by RepGeneralFailure.
//
// addr is used for BND fields. If addr is invalid, BND.ADDR
// will be set to empty domain name, and BND.PORT will be set to 0.
func (r *Request) Deny(rep byte, addr string) (ok bool) {
a, err := ParseAddrPort(addr)
if err != nil {
return r.deny(rep, emptyAddr, false)
}
return r.deny(rep, a, false)
}
func (r *Request) deny(rep byte, addr *AddrPort, timeoutDeny bool) (ok bool) {
r.once.Do(func() {
if rep == RepSucceeded {
r.reply.code = RepGeneralFailure
} else {
r.reply.code = rep
}
r.reply.addr = addr
r.timeoutDeny = timeoutDeny
r.wg.Done()
ok = true
})
return
}
// Capsulation returns the [Capsulator] in use.
func (r *Request) Capsulation() Capsulator {
return r.capper
}
// UUID returns the UUID of the session of r.
// As soon as the [MidLayer] read the handshake message, the connection is
// considered as a valid session and is bound with a UUID.
// You can use the UUID to tell which handshake and which request belongs to
// which connection.
func (r *Request) UUID() uuid.UUID {
return r.uuid
}
type ConnectRequest struct {
Request
outbound net.Conn
}
func (r *ConnectRequest) Accept(conn net.Conn) (ok bool) {
if conn == nil || conn.LocalAddr() == nil {
r.deny(RepGeneralFailure, emptyAddr.cpy(), false)
return
}
addr, err := ParseAddrPort(conn.LocalAddr().String())
if err != nil {
r.deny(RepGeneralFailure, emptyAddr, false)
return
}
return r.accept(conn, addr)
}
func (r *ConnectRequest) accept(conn net.Conn, addr *AddrPort) (ok bool) {
r.once.Do(func() {
r.outbound = conn
r.reply.addr = addr
r.wg.Done()
ok = true
})
return
}
type BindRequest struct {
Request
hostConn net.Conn
bindMux sync.Mutex // To avoid simultainous rw on reply field, Bind uses it to check if the request is accepted.
bindWg sync.WaitGroup
bindOnce sync.Once
bindReply *reply
}
// Accept accepts the request, and tells the client which address the SOCKS server
// will listen on. This is the first reply from the server.
func (r *BindRequest) Accept(addr string) (ok bool) {
a, err := ParseAddrPort(addr)
if err != nil {
r.deny(RepGeneralFailure, emptyAddr, false)
return
}
return r.accept(a)
}
func (r *BindRequest) accept(addr *AddrPort) (ok bool) {
r.once.Do(func() {
r.bindMux.Lock()
defer r.bindMux.Unlock()
r.reply = new(reply)
r.reply.addr = addr
r.wg.Done()
ok = true
})
return
}
// Bind binds the client. This is the second reply from the server.
//
// No-op if the first reply is not decided, once it is, Bind can be called again.
//
// Once r is accepted,
// r will wait for the decision on the second reply WITHOUT timeout,
// even if the connection to the client is closed.
func (r *BindRequest) Bind(conn net.Conn) (ok bool) {
r.bindMux.Lock()
defer r.bindMux.Unlock()
if r.reply == nil {
return
}
if conn == nil || conn.LocalAddr() == nil {
r.denyBind(RepGeneralFailure, emptyAddr, false)
return
}
addr, err := ParseAddrPort(conn.LocalAddr().String())
if err != nil {
r.denyBind(RepGeneralFailure, emptyAddr, false)
return
}
return r.bind(conn, addr)
}
func (r *BindRequest) bind(conn net.Conn, addr *AddrPort) (ok bool) {
r.bindOnce.Do(func() {
r.hostConn = conn
r.bindReply = new(reply)
r.bindReply.code = RepSucceeded
r.bindReply.addr = addr
ok = true
})
return
}
// DenyBind denies the request.
// No-op if the first reply is not decided, once it is, DenyBind can be called again.
func (r *BindRequest) DenyBind(rep byte, addr string) (ok bool) {
r.bindMux.Lock()
defer r.bindMux.Unlock()
if r.reply == nil {
return
}
a, err := ParseAddrPort(addr)
if err != nil {
r.deny(rep, emptyAddr, false)
} else {
ok = r.deny(rep, a, false)
}
return
}
func (r *BindRequest) denyBind(rep byte, addr *AddrPort, timeoutDeny bool) (ok bool) {
r.bindOnce.Do(func() {
if rep == RepSucceeded {
r.bindReply.code = RepGeneralFailure
} else {
r.bindReply.code = rep
}
r.bindReply = new(reply)
r.bindReply.addr = addr
r.bindWg.Done()
ok = true
})
return
}
type AssocRequest struct {
Request
notifyOnce sync.Once
notify func(error)
terminate func() error
// The true reason why the association is terminated.
// See Midlayer.handleAssoc and AssocRequest.Accept.
finalErr error
}
// Accept accepts the request.
//
// terminate can be used to terminate the association by closing the control
// connection. Be aware it is nil if ok is false.
//
// notify is called when the association terminates, e.g. TCP disconnection,
// IO error, call on terminate.
// If the client closed the control connection, reason will be [io.EOF].
// If terminate is called, reason will be nil.
// Otherwise, reason will be the read error on the control connection.
// notify will only be called once, if it's not nil.
func (r *AssocRequest) Accept(addr string, notify func(reason error)) (terminate func() error, ok bool) {
a, err := ParseAddrPort(addr)
if err != nil {
r.deny(RepGeneralFailure, emptyAddr, false)
return nil, false
}
return r.accept(a, notify)
}
func (r *AssocRequest) accept(addr *AddrPort, notify func(error)) (terminate func() error, ok bool) {
r.once.Do(func() {
r.notify = notify
r.reply.addr = addr
terminate = r.terminate
ok = true
r.wg.Done()
})
return
}
type reply struct {
code byte
addr *AddrPort
}
// Guarantees to return nil error.
func (r *reply) MarshalBinary() (data []byte, err error) {
aBytes, _ := r.addr.MarshalBinary()
l := 1 + 1 + 1 + len(aBytes)
data = make([]byte, l)
data[0] = VerSOCKS5
data[1] = r.code
data[2] = RSV
copy(data[3:], aBytes)
return data, nil
}
// Used in Associator
type udpPacket struct {
frag byte
dst *AddrPort
data []byte
}
func (p *udpPacket) UnmarshalBinary(data []byte) error {
// We use ErrMalformed here instead of specific err type, because
// the packet will be dropped by Associator silently.
if len(data) < 3 {
return ErrMalformed
}
if data[0] != RSV || data[1] != RSV {
return ErrMalformed
}
r := &sliceReader{
bytes: data[3:],
}
dst, err := readAddrPort(r)
if err != nil {
return err
}
dst.Protocol = "udp"
p.dst = dst
p.data = cpySlice(data[3+r.n:])
p.frag = data[2]
return nil
}
func (p *udpPacket) MarshalBinary() ([]byte, error) {
addrRaw, err := p.dst.MarshalBinary()
if err != nil {
return nil, err
}
result := make([]byte, 3+len(addrRaw)+len(p.data))
result[2] = p.frag
copy(result[3:], addrRaw)
copy(result[3+len(addrRaw):], p.data)
return result, nil
}