-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient4_test.go
More file actions
2243 lines (1974 loc) · 58.4 KB
/
client4_test.go
File metadata and controls
2243 lines (1974 loc) · 58.4 KB
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// nolint
package socksgo_test
import (
"bytes"
"context"
"errors"
"io"
"net"
"testing"
"time"
"github.com/asciimoth/gonnect"
socksgo "github.com/asciimoth/socksgo"
"github.com/asciimoth/socksgo/protocol"
)
// mockConnForClient4 implements net.Conn with configurable behavior for client4 tests.
type mockConnForClient4 struct {
readData []byte
readOffset int
readErr error
writeErr error
closeErr error
deadlineErr error
closed bool
remoteAddr net.Addr
setDeadlineFn func(t time.Time) error
}
func (m *mockConnForClient4) Read(p []byte) (n int, err error) {
if m.readOffset >= len(m.readData) {
if m.readErr != nil {
return 0, m.readErr
}
return 0, io.EOF
}
n = copy(p, m.readData[m.readOffset:])
m.readOffset += n
return n, nil
}
func (m *mockConnForClient4) Write(p []byte) (n int, err error) {
if m.writeErr != nil {
return 0, m.writeErr
}
return len(p), nil
}
func (m *mockConnForClient4) Close() error {
m.closed = true
return m.closeErr
}
func (m *mockConnForClient4) LocalAddr() net.Addr {
return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345}
}
func (m *mockConnForClient4) RemoteAddr() net.Addr {
if m.remoteAddr != nil {
return m.remoteAddr
}
return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 54321}
}
func (m *mockConnForClient4) SetDeadline(t time.Time) error {
if m.deadlineErr != nil {
return m.deadlineErr
}
if m.setDeadlineFn != nil {
return m.setDeadlineFn(t)
}
return nil
}
func (m *mockConnForClient4) SetReadDeadline(t time.Time) error {
return m.SetDeadline(t)
}
func (m *mockConnForClient4) SetWriteDeadline(t time.Time) error {
return m.SetDeadline(t)
}
// Test request4 when Connect fails.
func TestRequest4_ConnectFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
dialErr := errors.New("dial failed")
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return nil, dialErr
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when Connect fails")
}
if !errors.Is(err, dialErr) && err.Error() != dialErr.Error() {
t.Fatalf("expected dial error, got %v", err)
}
if proxy != nil {
t.Fatal("expected nil proxy on Connect failure")
}
if addr.Type != 0 {
t.Fatal("expected zero addr on Connect failure")
}
}
// Test request4 when BuildSocsk4TCPRequest would fail (user too long).
func TestRequest4_BuildRequestFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
c := &socksgo.Client{
SocksVersion: "4a",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{}, nil
},
Auth: (&protocol.AuthMethods{}).Add(&protocol.PassAuthMethod{
User: string(make([]byte, 300)), // Very long username
Pass: "pass",
}),
}
// Use a very long FQDN to trigger error in BuildSocsk4TCPRequest for socks4a
longHost := string(make([]byte, 300))
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort(longHost+":80", "tcp"),
)
if err == nil {
t.Fatal("expected error when BuildSocsk4TCPRequest fails")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed")
}
// The proxy should be closed
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on BuildSocsk4TCPRequest failure")
}
}
// Test request4 when io.Copy fails (write error).
func TestRequest4_CopyFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
writeErr := errors.New("write failed")
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{writeErr: writeErr}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when io.Copy fails")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed on copy failure")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed on copy failure")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on copy failure")
}
}
// Test request4 when ReadSocks4TCPReply fails (read error).
func TestRequest4_ReadReplyFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
readErr := errors.New("read failed")
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readErr: readErr}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when ReadSocks4TCPReply fails")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed on read reply failure")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed on read reply failure")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on read reply failure")
}
}
// Test request4 when server returns rejection (stat.Ok() is false).
func TestRequest4_ServerRejects(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=91 (rejected), PORT=0, IP=0.0.0.0
replyData := []byte{0, 91, 0, 0, 0, 0, 0, 0}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when server rejects")
}
var rejectErr socksgo.RejectdError
if !errors.As(err, &rejectErr) {
t.Fatalf("expected RejectdError, got %T: %v", err, err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed on server rejection")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed on server rejection")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on server rejection")
}
}
// Test request4 when server returns 0.0.0.0:0 (use proxy addr).
func TestRequest4_UseProxyAddr(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=0, IP=0.0.0.0
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0}
remoteAddr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 1), Port: 9999}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{
readData: replyData,
remoteAddr: remoteAddr,
}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy on success")
}
// Address should be taken from proxy remote addr
if addr.Port != 9999 {
t.Fatalf("expected port 9999, got %d", addr.Port)
}
if !addr.ToIP().Equal(net.IPv4(10, 0, 0, 1)) {
t.Fatalf("expected IP 10.0.0.1, got %v", addr.ToIP())
}
_ = proxy.Close()
}
// Test request4 success path.
func TestRequest4_Success(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=80, IP=93.184.216.34
replyData := []byte{0, 90, 0, 80, 93, 184, 216, 34}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy on success")
}
if addr.Port != 80 {
t.Fatalf("expected port 80, got %d", addr.Port)
}
if !addr.ToIP().Equal(net.IPv4(93, 184, 216, 34)) {
t.Fatalf("expected IP 93.184.216.34, got %v", addr.ToIP())
}
_ = proxy.Close()
}
// Test request4 with socks4a (FQDN).
func TestRequest4a_Success(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=443, IP=1.2.3.4
replyData := []byte{0, 90, 1, 187, 1, 2, 3, 4}
c := &socksgo.Client{
SocksVersion: "4a",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:443", "tcp"),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy on success")
}
if addr.Port != 443 {
t.Fatalf("expected port 443, got %d", addr.Port)
}
_ = proxy.Close()
}
// Test clientListener4.Addr.
func TestClientListener4_Addr(t *testing.T) {
t.Parallel()
// We can't directly create clientListener4 from public API easily,
// but we can test it via Listen which creates it for socks4
}
// Test clientListener4.Close.
func TestClientListener4_Close(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=0, IP=0.0.0.0
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
// Use Listen to create clientListener4
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
err = ln.Close()
if err != nil {
t.Fatalf("Close failed: %v", err)
}
}
// Test clientListener4.Accept on second call (should fail).
func TestClientListener4_AcceptSecondCallFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=0, IP=0.0.0.0
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
// First accept should succeed (reads the reply)
conn1, err := ln.Accept()
if err != nil {
t.Fatalf("first Accept failed: %v", err)
}
if conn1 == nil {
t.Fatal("expected non-nil conn on first accept")
}
// Second accept should fail with OpError
_, err = ln.Accept()
if err == nil {
t.Fatal("expected error on second Accept")
}
var opErr *net.OpError
if !errors.As(err, &opErr) {
t.Fatalf("expected *net.OpError, got %T: %v", err, err)
}
if opErr.Op != "accept" {
t.Fatalf("expected Op=accept, got %s", opErr.Op)
}
}
// Test clientListener4.Accept when read fails.
// Note: The current implementation closes the conn on read error but doesn't
// return the error (this may be a bug). This test verifies the conn is closed.
func TestClientListener4_AcceptReadFails(t *testing.T) {
t.Parallel()
ctx := context.Background()
// First reply for Listen() success (8 bytes), then explicit read error for Accept()
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0}
readErr := errors.New("read error on second reply")
conn := &mockConnForClient4{readData: replyData, readErr: readErr}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Filter: gonnect.FalseFilter,
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
// Create conn that succeeds for first 8 bytes, then fails
return conn, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
// Accept will try to read second reply but get error
// The implementation closes the conn but may not return the error
_, _ = ln.Accept()
// Verify the connection was closed on read error
if !conn.closed {
t.Fatal("expected connection to be closed on read error")
}
}
// Test clientListener4.Accept when server rejects (on second reply).
func TestClientListener4_AcceptServerRejects(t *testing.T) {
t.Parallel()
ctx := context.Background()
// First SOCKS4 reply for BIND (success): VN=0, CD=90, PORT=0, IP=0.0.0.0
// Second reply (rejection): VN=0, CD=91, PORT=0, IP=0.0.0.0
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Filter: gonnect.FalseFilter,
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
_, err = ln.Accept()
if err == nil {
t.Fatal("expected error when server rejects")
}
var rejectErr socksgo.RejectdError
if !errors.As(err, &rejectErr) {
t.Fatalf("expected RejectdError, got %T: %v", err, err)
}
}
// Test clientListener4.Accept success path.
func TestClientListener4_AcceptSuccess(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=8080, IP=10.0.0.1
replyData := []byte{0, 90, 31, 160, 10, 0, 0, 1}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
conn, err := ln.Accept()
if err != nil {
t.Fatalf("Accept failed: %v", err)
}
if conn == nil {
t.Fatal("expected non-nil conn on success")
}
defer func() { _ = conn.Close() }()
// Verify the connection works (can write/read)
testData := []byte("hello")
n, err := conn.Write(testData)
if err != nil {
t.Fatalf("Write failed: %v", err)
}
if n != len(testData) {
t.Fatalf("expected to write %d bytes, wrote %d", len(testData), n)
}
}
// Test that clientListener4.Addr returns correct address.
func TestClientListener4_AddrReturnsCorrect(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=9999, IP=192.168.1.1
replyData := []byte{0, 90, 39, 15, 192, 168, 1, 1}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
addr := ln.Addr()
if addr == nil {
t.Fatal("expected non-nil addr")
}
// Addr() returns protocol.Addr which implements net.Addr
if addr.String() != "192.168.1.1:9999" {
t.Fatalf("expected addr 192.168.1.1:9999, got %s", addr.String())
}
}
// Test with wrong SOCKS4 reply version.
func TestRequest4_WrongReplyVersion(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply with wrong VN (should be 0)
replyData := []byte{1, 90, 0, 80, 93, 184, 216, 34}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error for wrong reply version")
}
var wrongVerErr protocol.Wrong4ReplyVerError
if !errors.As(err, &wrongVerErr) {
t.Fatalf("expected protocol.Wrong4ReplyVerError, got %T: %v", err, err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on error")
}
}
// Test request4 with context cancellation.
func TestRequest4_ContextCancelled(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Filter: gonnect.FalseFilter, // Disable filter to ensure dialer is called
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
// Simulate context cancellation check
<-ctx.Done()
return nil, ctx.Err()
},
}
_, _, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("127.0.0.1:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when context is cancelled")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context.Canceled, got %v", err)
}
}
// Test request4 with partial reply data (incomplete read).
func TestRequest4_IncompleteReply(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Incomplete SOCKS4 reply (only 4 bytes instead of 8)
replyData := []byte{0, 90, 0, 80}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error for incomplete reply")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on error")
}
}
// Test that request4 properly handles bytes.NewReader with empty request.
// This tests the io.Copy path more thoroughly.
func TestRequest4_EmptyRequestData(t *testing.T) {
t.Parallel()
ctx := context.Background()
// This test ensures that even with minimal data, the flow works
// SOCKS4 reply: VN=0, CD=90 (success), PORT=80, IP=93.184.216.34
replyData := []byte{0, 90, 0, 80, 93, 184, 216, 34}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy on success")
}
if addr.Port != 80 {
t.Fatalf("expected port 80, got %d", addr.Port)
}
_ = proxy.Close()
}
// Test clientListener4 with custom remote addr.
func TestClientListener4_CustomRemoteAddr(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=1234, IP=1.1.1.1
replyData := []byte{0, 90, 4, 210, 1, 1, 1, 1}
customRemoteAddr := &net.TCPAddr{IP: net.IPv4(2, 2, 2, 2), Port: 5678}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{
readData: replyData,
remoteAddr: customRemoteAddr,
}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
conn, err := ln.Accept()
if err != nil {
t.Fatalf("Accept failed: %v", err)
}
defer func() { _ = conn.Close() }()
// Verify remote addr is the custom one
remoteAddr, ok := conn.RemoteAddr().(*net.TCPAddr)
if !ok {
t.Fatalf("expected *net.TCPAddr, got %T", conn.RemoteAddr())
}
if !remoteAddr.IP.Equal(net.IPv4(2, 2, 2, 2)) {
t.Fatalf("expected remote IP 2.2.2.2, got %v", remoteAddr.IP)
}
if remoteAddr.Port != 5678 {
t.Fatalf("expected remote port 5678, got %d", remoteAddr.Port)
}
}
// Test request4 with BIND command.
func TestRequest4_BindCommand(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=5555 (0x15B3), IP=10.10.10.10
replyData := []byte{0, 90, 21, 179, 10, 10, 10, 10}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Filter: gonnect.FalseFilter,
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
// Use Listen which sends BIND command for socks4
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer func() { _ = ln.Close() }()
addr := ln.Addr()
if addr.String() != "10.10.10.10:5555" {
t.Fatalf("expected addr 10.10.10.10:5555, got %s", addr.String())
}
}
// Test request4 with no auth (empty user).
func TestRequest4_NoAuth(t *testing.T) {
t.Parallel()
ctx := context.Background()
// SOCKS4 reply: VN=0, CD=90 (success), PORT=80, IP=93.184.216.34
replyData := []byte{0, 90, 0, 80, 93, 184, 216, 34}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Auth: &protocol.AuthMethods{}, // No auth methods
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: replyData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if proxy == nil {
t.Fatal("expected non-nil proxy on success")
}
if addr.Port != 80 {
t.Fatalf("expected port 80, got %d", addr.Port)
}
_ = proxy.Close()
}
// Test that close error on request4 is handled (proxy.Close returns error).
func TestRequest4_CloseErrorIgnoredOnBuildFailure(t *testing.T) {
t.Parallel()
ctx := context.Background()
closeErr := errors.New("close failed")
c := &socksgo.Client{
SocksVersion: "4a",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{closeErr: closeErr}, nil
},
Auth: (&protocol.AuthMethods{}).Add(&protocol.PassAuthMethod{
User: string(
make([]byte, 300),
), // Very long username to trigger build error
Pass: "pass",
}),
}
longHost := string(make([]byte, 300))
_, _, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort(longHost+":80", "tcp"),
)
if err == nil {
t.Fatal("expected error when BuildSocsk4TCPRequest fails")
}
// Close error should be ignored, but the connection should still be closed
}
// Test clientListener4.Close with error.
func TestClientListener4_CloseWithError(t *testing.T) {
t.Parallel()
ctx := context.Background()
closeErr := errors.New("close failed")
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{
readData: replyData,
closeErr: closeErr,
}, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
err = ln.Close()
if err == nil {
t.Fatal("expected close error")
}
if err.Error() != closeErr.Error() {
t.Fatalf("expected close error %v, got %v", closeErr, err)
}
}
// Test request4 with copy error after partial write.
func TestRequest4_CopyErrorAfterPartialWrite(t *testing.T) {
t.Parallel()
ctx := context.Background()
copyErr := errors.New("copy failed after partial write")
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
// Return conn that fails on write (io.Copy uses Write)
return &mockConnForClient4{writeErr: copyErr}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when copy fails")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed on copy failure")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on copy failure")
}
}
// Test request4 with read reply error after successful write.
func TestRequest4_ReadErrorAfterSuccessfulWrite(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Make read fail after some successful reads (simulate timeout after request sent)
readData := []byte{0} // Only 1 byte, will cause EOF when reading full reply
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return &mockConnForClient4{readData: readData}, nil
},
}
proxy, addr, err := c.Request(
ctx,
protocol.CmdConnect,
protocol.AddrFromHostPort("example.com:80", "tcp"),
)
if err == nil {
t.Fatal("expected error when read reply fails")
}
if proxy == nil {
t.Fatal("expected non-nil proxy to be closed")
}
if m, ok := proxy.(*mockConnForClient4); ok {
if !m.closed {
t.Fatal("expected proxy to be closed on read reply failure")
}
}
if addr.Type != 0 {
t.Fatal("expected zero addr on read reply failure")
}
}
// Test clientListener4.Accept with wrong reply version (on second reply).
// Note: The current implementation has a bug where read errors are not returned.
// This test verifies the behavior as-is.
func TestClientListener4_AcceptWrongReplyVersion(t *testing.T) {
t.Parallel()
ctx := context.Background()
// First reply for Listen() success: VN=0, CD=90, PORT=0, IP=0.0.0.0
// Second reply for Accept() with wrong VN: VN=5, CD=90, PORT=0, IP=0.0.0.0
replyData := []byte{0, 90, 0, 0, 0, 0, 0, 0, 5, 90, 0, 0, 0, 0, 0, 0}
conn := &mockConnForClient4{readData: replyData}
c := &socksgo.Client{
SocksVersion: "4",
ProxyAddr: "127.0.0.1:1080",
Filter: gonnect.FalseFilter,
Dialer: func(ctx context.Context, network, address string) (net.Conn, error) {
return conn, nil
},
}
ln, err := c.Listen(ctx, "tcp", "0.0.0.0:0")
if err != nil {