| 1 | package socks5 |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // TestClientPerClientDialTCP verifies that a per-client DialTCP is used in |
| 10 | // preference to the package-level DialTCP, without affecting the global. |
| 11 | func TestClientPerClientDialTCP(t *testing.T) { |
| 12 | sentinel := errors.New("per-client dialTCP called") |
| 13 | var gotNetwork, gotRaddr string |
| 14 | |
| 15 | c := &Client{ |
| 16 | Server: "192.0.2.1:1080", |
| 17 | DialTCP: func(network, laddr, raddr string) (net.Conn, error) { |
| 18 | gotNetwork, gotRaddr = network, raddr |
| 19 | return nil, sentinel |
| 20 | }, |
| 21 | } |
| 22 | |
| 23 | err := c.Negotiate(nil) |
| 24 | if !errors.Is(err, sentinel) { |
| 25 | t.Fatalf("expected per-client DialTCP to be used, got err=%v", err) |
| 26 | } |
| 27 | if gotNetwork != "tcp" { |
| 28 | t.Errorf("network = %q, want %q", gotNetwork, "tcp") |
| 29 | } |
| 30 | if gotRaddr != c.Server { |
| 31 | t.Errorf("raddr = %q, want %q", gotRaddr, c.Server) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // TestClientPerClientDialUDP verifies that a per-client DialUDP is used and is |
| 36 | // preserved through the struct rebuild that DialWithLocalAddr performs. |
| 37 | func TestClientPerClientDialUDP(t *testing.T) { |
| 38 | sentinel := errors.New("per-client dialUDP called") |
| 39 | |
| 40 | // A per-client DialTCP feeds a scripted SOCKS5 negotiation/UDP-associate |
| 41 | // reply so that DialWithLocalAddr reaches the DialUDP call. |
| 42 | c := &Client{ |
| 43 | Server: "192.0.2.1:1080", |
| 44 | DialTCP: func(network, laddr, raddr string) (net.Conn, error) { |
| 45 | return newScriptedProxyConn(), nil |
| 46 | }, |
| 47 | DialUDP: func(network, laddr, raddr string) (net.Conn, error) { |
| 48 | return nil, sentinel |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | _, err := c.DialWithLocalAddr("udp", "", "192.0.2.2:53", nil) |
| 53 | if !errors.Is(err, sentinel) { |
| 54 | t.Fatalf("expected per-client DialUDP to be used, got err=%v", err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // newScriptedProxyConn returns a net.Conn pre-loaded with the proxy-side bytes |
| 59 | // of a no-auth negotiation reply followed by a successful UDP-associate reply |
| 60 | // pointing at 192.0.2.3:1234. Writes from the client are discarded. |
| 61 | func newScriptedProxyConn() net.Conn { |
| 62 | reply := []byte{ |
| 63 | // Negotiation reply: VER=5, METHOD=0 (no auth) |
| 64 | Ver, MethodNone, |
| 65 | // Request reply: VER=5, REP=0 (success), RSV=0, ATYP=1 (IPv4), |
| 66 | // BND.ADDR=192.0.2.3, BND.PORT=1234 |
| 67 | Ver, RepSuccess, 0x00, ATYPIPv4, 192, 0, 2, 3, 0x04, 0xd2, |
| 68 | } |
| 69 | return &scriptedConn{r: reply} |
| 70 | } |
| 71 | |
| 72 | type scriptedConn struct { |
| 73 | net.Conn |
| 74 | r []byte |
| 75 | off int |
| 76 | } |
| 77 | |
| 78 | func (c *scriptedConn) Read(b []byte) (int, error) { |
| 79 | if c.off >= len(c.r) { |
| 80 | return 0, errors.New("scriptedConn: no more data") |
| 81 | } |
| 82 | n := copy(b, c.r[c.off:]) |
| 83 | c.off += n |
| 84 | return n, nil |
| 85 | } |
| 86 | |
| 87 | func (c *scriptedConn) Write(b []byte) (int, error) { return len(b), nil } |
| 88 | func (c *scriptedConn) Close() error { return nil } |