-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
782 lines (700 loc) · 20.7 KB
/
server.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package socks5
import (
"errors"
"io"
"log"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
)
// Server defines parameters for running socks server.
// The zero value for Server is a valid configuration(tcp listen on :1080).
type Server struct {
// Addr optionally specifies the TCP address for the server to listen on,
// in the form "host:port". If empty, ":1080" (port 1080) is used.
Addr string
// BindIP specific UDP relay server IP and bind listen IP.
// It shouldn't be ipv4zero like "0,0,0,0" or ipv6zero like [:]
// If empty, localhost used.
BindIP string
// ReadTimeout is the maximum duration for reading from socks client.
// it's only effective to socks server handshake process.
//
// If zero, there is no timeout.
ReadTimeout time.Duration
// WriteTimeout is the maximum duration for writing to socks client.
// it's only effective to socks server handshake process.
//
// If zero, there is no timeout.
WriteTimeout time.Duration
// method mapping to the authenticator
// if nil server provide NO_AUTHENTICATION_REQUIRED method by default
Authenticators map[METHOD]Authenticator
// The server select method to use policy
//MethodSelector
// Server transmit data between client and dest server.
// if nil, DefaultTransport is used.
Transporter
// ErrorLog specifics an options logger for errors accepting
// connections, unexpected socks protocol handshake process,
// and server to remote connection errors.
// If nil, logging is done via log package's standard logger.
ErrorLog *log.Logger
// DisableSocks4, disable socks4 server, default enable socks4 compatible.
DisableSocks4 bool
// 1 indicate server is shutting down.
// 0 indicate server is running.
// Atomic operation must be guaranteed.
isShutdown int32
mu sync.Mutex
listeners map[*net.Listener]struct{}
activeConn map[net.Conn]struct{}
doneCh chan struct{}
}
func (srv *Server) getDoneChan() <-chan struct{} {
srv.mu.Lock()
defer srv.mu.Unlock()
return srv.getDoneChanLocked()
}
func (srv *Server) getDoneChanLocked() chan struct{} {
if srv.doneCh == nil {
srv.doneCh = make(chan struct{})
}
return srv.doneCh
}
func (srv *Server) closeDoneChanLocked() {
ch := srv.getDoneChanLocked()
select {
case <-ch:
default:
close(srv.doneCh)
}
}
func (srv *Server) Close() error {
atomic.StoreInt32(&srv.isShutdown, 1)
srv.mu.Lock()
defer srv.mu.Unlock()
// close all listeners.
err := srv.closeListenerLocked()
if err != nil {
return err
}
// close doneCh to broadcast close message.
srv.closeDoneChanLocked()
// Close all open connections.
for conn, _ := range srv.activeConn {
conn.Close()
}
return nil
}
func (srv *Server) inShuttingDown() bool {
return atomic.LoadInt32(&srv.isShutdown) != 0
}
func (srv *Server) closeListenerLocked() error {
var err error
for ln := range srv.listeners {
if cerr := (*ln).Close(); cerr != nil {
err = cerr
}
}
return err
}
// trackListener adds or removes a net.Listener to the set of tracked
// listeners.
//
// We store a pointer to interface in the map set, in case the
// net.Listener is not comparable. This is safe because we only call
// trackListener via Serve and can track+defer untrack the same
// pointer to local variable there. We never need to compare a
// Listener from another caller.
//
// It reports whether the server is still up (not Shutdown or Closed).
func (srv *Server) trackListener(l *net.Listener, add bool) bool {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.listeners == nil {
srv.listeners = make(map[*net.Listener]struct{})
}
if add {
if srv.inShuttingDown() {
return false
}
srv.listeners[l] = struct{}{}
} else {
delete(srv.listeners, l)
}
return true
}
func (srv *Server) trackConn(c net.Conn, add bool) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.activeConn == nil {
srv.activeConn = make(map[net.Conn]struct{})
}
if add {
srv.activeConn[c] = struct{}{}
} else {
delete(srv.activeConn, c)
}
}
// ListenAndServe listens on the TCP network address srv.Addr and then
// calls serve to handle requests on incoming connections.
//
// If srv.Addr is blank, ":1080" is used.
func (srv *Server) ListenAndServe() error {
if srv.inShuttingDown() {
return ErrServerClosed
}
addr := srv.Addr
if addr == "" {
addr = "0.0.0.0:1080"
}
if srv.BindIP == "" {
srv.BindIP = "localhost"
} else if srv.BindIP == net.IPv4zero.String() || srv.BindIP == net.IPv6zero.String() {
return errors.New("socks: server bindIP shouldn't be zero")
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return srv.Serve(ln)
}
// ErrServerClosed is returned by the Server's Serve, ListenAndServe methods after a call to Shutdown or Close.
var ErrServerClosed = errors.New("socks: Server closed")
// Serve accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutine select client
// list methods and reply client. Then process authentication and reply
// to them. At then end of handshake, read socks request from client and
// establish a connection to the target.
func (srv *Server) Serve(l net.Listener) error {
srv.trackListener(&l, true)
defer srv.trackListener(&l, false)
var tempDelay time.Duration
for {
client, err := l.Accept()
if err != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := time.Second; tempDelay > max {
tempDelay = max
}
srv.logf()("socks: Accept error: %v, retrying in %v", err, tempDelay)
time.Sleep(tempDelay)
continue
}
return err
}
go srv.serveconn(client)
}
}
func (srv *Server) serveconn(client net.Conn) {
if srv.ReadTimeout != 0 {
client.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
}
if srv.WriteTimeout != 0 {
client.SetWriteDeadline(time.Now().Add(srv.WriteTimeout))
}
// handshake
request, err := srv.handShake(client)
if err != nil {
srv.logf()(err.Error())
client.Close()
return
}
// establish connection to remote
remote, err := srv.establish(client, request)
if err != nil {
srv.logf()(err.Error())
client.Close()
return
}
// establish over, reset deadline.
client.SetReadDeadline(time.Time{})
client.SetWriteDeadline(time.Time{})
// transport data
switch request.CMD {
case CONNECT, BIND:
srv.trackConn(client, true)
defer srv.trackConn(client, false)
srv.trackConn(remote, true)
defer srv.trackConn(remote, false)
errCh := srv.transport().TransportTCP(client.(*net.TCPConn), remote.(*net.TCPConn))
for err := range errCh {
if err != nil {
srv.logf()(err.Error())
}
}
case UDP_ASSOCIATE:
relay := NewUDPConn(remote.(*net.UDPConn), client.(*net.TCPConn))
srv.trackConn(relay, true)
defer srv.trackConn(relay, false)
err = srv.transport().TransportUDP(relay, request)
if err != nil {
srv.logf()(err.Error())
}
}
}
func (srv *Server) transport() Transporter {
if srv.Transporter == nil {
return DefaultTransporter
}
return srv.Transporter
}
var errDisableSocks4 = errors.New("socks4 server has been disabled")
// handShake socks protocol handshake process
func (srv *Server) handShake(client net.Conn) (*Request, error) {
//validate socks version message
version, err := checkVersion(client)
if err != nil {
return nil, &OpError{Version5, "read", client.RemoteAddr(), "\"check version\"", err}
}
//socks4 protocol process
if version == Version4 {
if srv.DisableSocks4 {
//send server reject reply
address := &Address{net.IPv4zero, IPV4_ADDRESS, 0}
addr, err := address.Bytes(Version4)
if err != nil {
return nil, &OpError{Version4, "", client.RemoteAddr(), "\"authentication\"", err}
}
_, err = client.Write(append([]byte{0, Rejected}, addr...))
if err != nil {
return nil, &OpError{Version4, "write", client.RemoteAddr(), "\"authentication\"", err}
}
return nil, errDisableSocks4
}
//handle socks4 request
return srv.readSocks4Request(client)
}
//socks5 protocol authentication
err = srv.authentication(client)
if err != nil {
return nil, err
}
//handle socks5 request
return srv.readSocks5Request(client)
}
// authentication socks5 authentication process
func (srv *Server) authentication(client net.Conn) error {
//get nMethods
nMethods, err := ReadNBytes(client, 1)
if err != nil {
return err
}
//Get methods
methods, err := ReadNBytes(client, int(nMethods[0]))
if err != nil {
return err
}
return srv.MethodSelect(methods, client)
}
// readSocks4Request receive socks4 protocol client request.
func (srv *Server) readSocks4Request(client net.Conn) (*Request, error) {
reply := &Reply{
VER: Version4,
Address: &Address{net.IPv4zero, IPV4_ADDRESS, 0},
}
req := &Request{
VER: Version4,
}
// CMD
cmd, err := ReadNBytes(client, 1)
if err != nil {
return nil, &OpError{req.VER, "read", client.RemoteAddr(), "\"process request command\"", err}
}
req.CMD = cmd[0]
// DST.PORT, DST.IP
addr, rep, err := readAddress(client, req.VER)
if err != nil {
reply.REP = rep
err := srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request address type\"", err}
}
}
req.Address = addr
return req, nil
}
// readSocks5Request read socks5 protocol client request.
func (srv *Server) readSocks5Request(client net.Conn) (*Request, error) {
reply := &Reply{
VER: Version5,
Address: &Address{net.IPv4zero, IPV4_ADDRESS, 0},
}
req := &Request{}
//VER, CMD, RSV
cmd, err := ReadNBytes(client, 3)
if err != nil {
return nil, &OpError{req.VER, "read", client.RemoteAddr(), "\"process request ver,cmd,rsv\"", err}
}
req.VER = cmd[0]
req.CMD = cmd[1]
req.RSV = cmd[2]
// ATYPE, DST.IP, DST.PORT
addr, rep, err := readAddress(client, req.VER)
if err != nil {
reply.REP = rep
err := srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request address\"", err}
}
}
req.Address = addr
return req, nil
}
// IsAllowNoAuthRequired return true if server enable NO_AUTHENTICATION_REQUIRED.
// Or the server doesn't has no Authenticator return true. Otherwise return false.
func (srv *Server) IsAllowNoAuthRequired() bool {
if len(srv.Authenticators) == 0 {
return true
}
for method := range srv.Authenticators {
if method == NO_AUTHENTICATION_REQUIRED {
return true
}
}
return false
}
// establish tcp connection to remote host if command is CONNECT or
// start listen on udp socket when command is UDP_ASSOCIATE. Listen
// and accept host connection when command is BIND. Finally, send
// corresponding reply to client.
func (srv *Server) establish(client net.Conn, req *Request) (dest net.Conn, err error) {
reply := &Reply{
VER: req.VER,
Address: &Address{net.IPv4zero, IPV4_ADDRESS, 0},
}
// version4
if req.VER == Version4 {
switch req.CMD {
case CONNECT:
// dial to dest host.
dest, err = net.Dial("tcp", req.Address.String())
if err != nil {
reply.REP = Rejected
err2 := srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
// parse remote host address.
remoteAddr, err := ParseAddress(dest.RemoteAddr().String())
if err != nil {
reply.REP = Rejected
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
reply.Address = remoteAddr
// success
reply.REP = Granted
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
case BIND:
bindAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(srv.BindIP, "0"))
if err != nil {
return nil, err
}
// start listening on random port.
bindServer, err := net.ListenTCP("tcp", bindAddr)
if err != nil {
reply.REP = Rejected
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
defer bindServer.Close()
reply.REP = Granted
reply.Address, err = ParseAddress(bindServer.Addr().String())
if err != nil {
return nil, err
}
// send first reply to client.
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
// waiting target host connect.
dest, err = bindServer.Accept()
if err != nil {
reply.REP = Rejected
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
// send second reply to client.
if req.Address.String() == dest.RemoteAddr().String() {
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
} else {
reply.REP = Rejected
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
}
default:
reply.REP = Rejected
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
return nil, &OpError{req.VER, "", client.RemoteAddr(), "\"process request\"", &CMDError{req.CMD}}
}
} else if req.VER == Version5 { // version5
switch req.CMD {
case CONNECT:
// dial dest host.
dest, err = net.Dial("tcp", req.Address.String())
if err != nil {
reply.REP = HOST_UNREACHABLE
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
// parse remote host address.
remoteAddr, err := ParseAddress(dest.RemoteAddr().String())
if err != nil {
reply.REP = GENERAL_SOCKS_SERVER_FAILURE
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
reply.Address = remoteAddr
// success
reply.REP = SUCCESSED
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
case UDP_ASSOCIATE:
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(srv.BindIP, "0"))
if err != nil {
return nil, err
}
// start udp listening on random port.
dest, err = net.ListenUDP("udp", addr)
if err != nil {
reply.REP = GENERAL_SOCKS_SERVER_FAILURE
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
// success
reply.REP = SUCCESSED
relayAddr, err := ParseAddress(dest.LocalAddr().String())
if err != nil {
return nil, err
}
reply.Address = relayAddr
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
case BIND:
bindAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(srv.BindIP, "0"))
if err != nil {
return nil, err
}
// start tcp listening on random port.
bindServer, err := net.ListenTCP("tcp", bindAddr)
if err != nil {
reply.REP = GENERAL_SOCKS_SERVER_FAILURE
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
defer bindServer.Close()
reply.REP = SUCCESSED
reply.Address, err = ParseAddress(bindServer.Addr().String())
if err != nil {
return nil, err
}
// send first reply to client.
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
dest, err = bindServer.Accept()
if err != nil {
reply.REP = GENERAL_SOCKS_SERVER_FAILURE
err2 := srv.sendReply(client, reply)
if err2 != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err2}
}
return nil, err
}
// send second reply to client.
if req.Address.String() == dest.RemoteAddr().String() {
reply.Address, err = ParseAddress(dest.RemoteAddr().String())
if err != nil {
return nil, err
}
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
} else {
reply.REP = GENERAL_SOCKS_SERVER_FAILURE
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
}
default:
reply.REP = COMMAND_NOT_SUPPORTED
err = srv.sendReply(client, reply)
if err != nil {
return nil, &OpError{req.VER, "write", client.RemoteAddr(), "\"process request\"", err}
}
return nil, &OpError{Version5, "", client.RemoteAddr(), "\"process request\"", &CMDError{req.CMD}}
}
} else { // unknown version
return nil, &VersionError{req.VER}
}
return
}
var errErrorATPE = errors.New("socks4 server bind address type should be ipv4")
// sendReply The server send socks protocol reply to client
func (srv *Server) sendReply(out io.Writer, r *Reply) error {
var reply []byte
var err error
if r.VER == Version4 {
if r.Address.ATYPE != IPV4_ADDRESS {
return errErrorATPE
}
addr, err := r.Address.Bytes(r.VER)
if err != nil {
return err
}
reply = append(reply, 0, r.REP)
// Remove NULL at the end. Please see Address.Bytes() Method.
reply = append(reply, addr[:len(addr)-1]...)
} else if r.VER == Version5 {
addr, err := r.Address.Bytes(r.VER)
if err != nil {
return err
}
reply = append(reply, r.VER, r.REP, r.RSV)
reply = append(reply, addr...)
} else {
return &VersionError{r.VER}
}
_, err = out.Write(reply)
return err
}
// MethodSelect select authentication method and reply to client.
func (srv *Server) MethodSelect(methods []CMD, client net.Conn) error {
// Select method to authenticate, then send selected method to client.
for _, method := range methods {
//Preferred to use NO_AUTHENTICATION_REQUIRED method
if method == NO_AUTHENTICATION_REQUIRED && srv.IsAllowNoAuthRequired() {
reply := []byte{Version5, NO_AUTHENTICATION_REQUIRED}
_, err := client.Write(reply)
if err != nil {
return &OpError{Version5, "write", client.RemoteAddr(), "authentication", err}
}
return nil
}
for m := range srv.Authenticators {
//Select the first matched method to authenticate
if m == method {
reply := []byte{Version5, method}
_, err := client.Write(reply)
if err != nil {
return &OpError{Version5, "write", client.RemoteAddr(), "authentication", err}
}
err = srv.Authenticators[m].Authenticate(client, client)
if err != nil {
return &OpError{Version5, "", client.RemoteAddr(), "authentication", err}
}
return nil
}
}
}
// There are no Methods can use
reply := []byte{Version5, NO_ACCEPTABLE_METHODS}
_, err := client.Write(reply)
if err != nil {
return &OpError{Version5, "write", client.RemoteAddr(), "authentication", err}
}
return &OpError{Version5, "", client.RemoteAddr(), "authentication", &MethodError{methods[0]}}
}
func (srv *Server) logf() func(format string, args ...interface{}) {
if srv.ErrorLog == nil {
return log.Printf
}
return srv.ErrorLog.Printf
}
// checkVersion check version is 4 or 5.
func checkVersion(in io.Reader) (VER, error) {
version, err := ReadNBytes(in, 1)
if err != nil {
return 0, err
}
if (version[0] != Version5) && (version[0] != Version4) {
return 0, &VersionError{version[0]}
}
return version[0], nil
}
// OpError is the error type usually returned by functions in the socks5
// package. It describes the socks version, operation, client address,
// and address of an error.
type OpError struct {
// VER describe the socks server version on process.
VER
// Op is the operation which caused the error, such as
// "read", "write".
Op string
// Addr define client's address which caused the error.
Addr net.Addr
// Step is the client's current connection stage, such as
// "check version", "authentication", "process request",
Step string
// Err is the error that occurred during the operation.
// The Error method panics if the error is nil.
Err error
}
func (o *OpError) Error() string {
str := "socks" + strconv.Itoa(int(o.VER))
str += " " + o.Op
if o.Addr == nil {
str += " "
} else {
str += " " + o.Addr.String()
}
str += " " + o.Step
str += ":" + o.Err.Error()
return str
}