2017-01-07 19:06:35 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-01-08 04:10:37 -05:00
|
|
|
xproxy "golang.org/x/net/proxy"
|
2017-01-08 04:23:55 -05:00
|
|
|
socks4 "h12.me/socks"
|
2017-01-07 19:06:35 -05:00
|
|
|
"v2ray.com/core"
|
2017-01-26 14:46:44 -05:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-01-07 19:06:35 -05:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/dokodemo"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
"v2ray.com/core/proxy/socks"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
|
|
|
"v2ray.com/core/testing/servers/udp"
|
2017-10-26 15:44:22 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2017-01-07 19:06:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSocksBridgeTCP(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
AuthType: socks.AuthType_PASSWORD,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-07 19:06:35 -05:00
|
|
|
UdpEnabled: false,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-07 19:06:35 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-07 19:06:35 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-07 19:06:35 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
Server: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-07 19:06:35 -05:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&socks.Account{
|
|
|
|
Username: "Test Account",
|
|
|
|
Password: "Test Password",
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(clientPort),
|
|
|
|
})
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
payload := "test payload"
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
CloseAllServers(servers)
|
2017-01-07 19:06:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSocksBridageUDP(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
udpServer := udp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := udpServer.Start()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
defer udpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
AuthType: socks.AuthType_PASSWORD,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-07 19:06:35 -05:00
|
|
|
UdpEnabled: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-07 19:06:35 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-07 19:06:35 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP, net.Network_UDP},
|
2017-01-07 19:06:35 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
|
2017-01-07 19:06:35 -05:00
|
|
|
Server: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-07 19:06:35 -05:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&socks.Account{
|
|
|
|
Username: "Test Account",
|
|
|
|
Password: "Test Password",
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(clientPort),
|
|
|
|
})
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
payload := "dokodemo request."
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-07 19:06:35 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-07 19:06:35 -05:00
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
CloseAllServers(servers)
|
2017-01-07 19:06:35 -05:00
|
|
|
}
|
2017-01-08 04:10:37 -05:00
|
|
|
|
2017-01-08 04:23:55 -05:00
|
|
|
func TestSocksConformance(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
authPort := pickPort()
|
|
|
|
noAuthPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-08 04:10:37 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(authPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
2017-01-08 04:10:37 -05:00
|
|
|
AuthType: socks.AuthType_PASSWORD,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-08 04:10:37 -05:00
|
|
|
UdpEnabled: false,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(noAuthPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
2017-01-08 04:10:37 -05:00
|
|
|
AuthType: socks.AuthType_NO_AUTH,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-08 04:10:37 -05:00
|
|
|
UdpEnabled: false,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-08 04:10:37 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-08 04:10:37 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
conn, err := noAuthDialer.Dial("tcp", dest.NetAddr())
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
payload := "test payload"
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
authDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, authPort).NetAddr(), &xproxy.Auth{User: "Test Account", Password: "Test Password"}, xproxy.Direct)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
conn, err := authDialer.Dial("tcp", dest.NetAddr())
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
payload := "test payload"
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-08 04:10:37 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-08 04:10:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-08 04:23:55 -05:00
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
dialer := socks4.DialSocksProxy(socks4.SOCKS4, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
|
2017-01-08 04:23:55 -05:00
|
|
|
conn, err := dialer("tcp", dest.NetAddr())
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:23:55 -05:00
|
|
|
|
|
|
|
payload := "test payload"
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-08 04:23:55 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-08 04:23:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
dialer := socks4.DialSocksProxy(socks4.SOCKS4A, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
|
|
|
|
conn, err := dialer("tcp", net.TCPDestination(net.LocalHostDomain, tcpServer.Port).NetAddr())
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-08 04:23:55 -05:00
|
|
|
|
|
|
|
payload := "test payload"
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(nBytes, Equals, len(payload))
|
2017-01-08 04:23:55 -05:00
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(response)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
|
|
assert(conn.Close(), IsNil)
|
2017-01-08 04:23:55 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
CloseAllServers(servers)
|
2017-01-08 04:10:37 -05:00
|
|
|
}
|