| 1 | package socks5scip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 5 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.16 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 TestClientPerClientDialTCPscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/TestClientPerClientDialTCP().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 12 | sentinel := errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("per-client dialTCP called") |
| 13 | var gotNetwork, gotRaddr string |
| 14 | |
| 15 | c := &Clientscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#{ |
| 16 | Serverscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#Server.: "192.0.2.1:1080", |
| 17 | DialTCPscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#DialTCP.: func(network, laddr, raddr string) (netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/golang/go/src go1.16 net/Conn#, error) { |
| 18 | gotNetwork, gotRaddr = network, raddr |
| 19 | return nil, sentinel |
| 20 | }, |
| 21 | } |
| 22 | |
| 23 | err := c.Negotiatescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#Negotiate().(nil) |
| 24 | if !errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Isscip-go gomod github.com/golang/go/src go1.16 errors/Is().(err, sentinel) { |
| 25 | t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected per-client DialTCP to be used, got err=%v", err) |
| 26 | } |
| 27 | if gotNetwork != "tcp" { |
| 28 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("network = %q, want %q", gotNetwork, "tcp") |
| 29 | } |
| 30 | if gotRaddr != c.Serverscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#Server. { |
| 31 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("raddr = %q, want %q", gotRaddr, c.Serverscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#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 TestClientPerClientDialUDPscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/TestClientPerClientDialUDP().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 38 | sentinel := errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 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 := &Clientscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#{ |
| 43 | Serverscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#Server.: "192.0.2.1:1080", |
| 44 | DialTCPscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#DialTCP.: func(network, laddr, raddr string) (netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/golang/go/src go1.16 net/Conn#, error) { |
| 45 | return newScriptedProxyConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/newScriptedProxyConn().(), nil |
| 46 | }, |
| 47 | DialUDPscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#DialUDP.: func(network, laddr, raddr string) (netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/golang/go/src go1.16 net/Conn#, error) { |
| 48 | return nil, sentinel |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | _, err := c.DialWithLocalAddrscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Client#DialWithLocalAddr().("udp", "", "192.0.2.2:53", nil) |
| 53 | if !errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Isscip-go gomod github.com/golang/go/src go1.16 errors/Is().(err, sentinel) { |
| 54 | t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#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 newScriptedProxyConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/newScriptedProxyConn().() netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/golang/go/src go1.16 net/Conn# { |
| 62 | reply := []byte{ |
| 63 | // Negotiation reply: VER=5, METHOD=0 (no auth) |
| 64 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver., MethodNonescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/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 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver., RepSuccessscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/RepSuccess., 0x00, ATYPIPv4scip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ATYPIPv4., 192, 0, 2, 3, 0x04, 0xd2, |
| 68 | } |
| 69 | return &scriptedConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#{rscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#r.: reply} |
| 70 | } |
| 71 | |
| 72 | type scriptedConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn# struct { |
| 73 | netscip-go gomod github.com/golang/go/src go1.16 net/.Connscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#Conn. |
| 74 | rscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#r. []byte |
| 75 | offscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#off. int |
| 76 | } |
| 77 | |
| 78 | func (c *scriptedConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#) Readscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#Read().(b []byte) (int, error) { |
| 79 | if c.offscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#off. >= len(c.rscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#r.) { |
| 80 | return 0, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("scriptedConn: no more data") |
| 81 | } |
| 82 | n := copy(b, c.rscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#r.[c.offscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#off.:]) |
| 83 | c.offscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#off. += n |
| 84 | return n, nil |
| 85 | } |
| 86 | |
| 87 | func (c *scriptedConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#) Writescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#Write().(b []byte) (int, error) { return len(b), nil } |
| 88 | func (c *scriptedConnscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#) Closescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/scriptedConn#Close().() error { return nil } |