From 5b89c6aada960a051b1a4da9d22df67a53a1c359 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 8 Jan 2017 10:23:55 +0100 Subject: [PATCH] socks4 tests --- proxy/socks/protocol.go | 4 ++-- testing/scenarios/socks_test.go | 37 ++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 9100e0314..7bfeaabca 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -73,12 +73,12 @@ func (s *ServerSession) Handshake(reader io.Reader, writer io.Writer) (*protocol request.Address = address request.Port = port request.Version = socks4Version - if err := writeSocks4Response(writer, socks4RequestGranted, address, port); err != nil { + if err := writeSocks4Response(writer, socks4RequestGranted, v2net.AnyIP, v2net.Port(0)); err != nil { return nil, err } return request, nil default: - writeSocks4Response(writer, socks4RequestRejected, address, port) + writeSocks4Response(writer, socks4RequestRejected, v2net.AnyIP, v2net.Port(0)) return nil, errors.New("Socks|Server: Unsupported command: ", buffer.Byte(1)) } } diff --git a/testing/scenarios/socks_test.go b/testing/scenarios/socks_test.go index 965932ab1..0f1b42451 100644 --- a/testing/scenarios/socks_test.go +++ b/testing/scenarios/socks_test.go @@ -5,6 +5,7 @@ import ( "testing" xproxy "golang.org/x/net/proxy" + socks4 "h12.me/socks" "v2ray.com/core" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -203,7 +204,7 @@ func TestSocksBridageUDP(t *testing.T) { CloseAllServers() } -func TestSocks5conformance(t *testing.T) { +func TestSocksConformance(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ @@ -287,5 +288,39 @@ func TestSocks5conformance(t *testing.T) { assert.Error(conn.Close()).IsNil() } + { + dialer := socks4.DialSocksProxy(socks4.SOCKS4, v2net.TCPDestination(v2net.LocalHostIP, noAuthPort).NetAddr()) + conn, err := dialer("tcp", dest.NetAddr()) + assert.Error(err).IsNil() + + payload := "test payload" + nBytes, err := conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + response := make([]byte, 1024) + nBytes, err = conn.Read(response) + assert.Error(err).IsNil() + assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert.Error(conn.Close()).IsNil() + } + + { + dialer := socks4.DialSocksProxy(socks4.SOCKS4A, v2net.TCPDestination(v2net.LocalHostIP, noAuthPort).NetAddr()) + conn, err := dialer("tcp", v2net.TCPDestination(v2net.LocalHostDomain, tcpServer.Port).NetAddr()) + assert.Error(err).IsNil() + + payload := "test payload" + nBytes, err := conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + response := make([]byte, 1024) + nBytes, err = conn.Read(response) + assert.Error(err).IsNil() + assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert.Error(conn.Close()).IsNil() + } + CloseAllServers() }