1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 20:16:55 -05:00
v2fly/testing/scenarios/socks_test.go

444 lines
11 KiB
Go
Raw Normal View History

2017-01-07 19:06:35 -05:00
package scenarios
import (
"testing"
"v2ray.com/core/app/router"
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"
"v2ray.com/core/app/proxyman"
"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/blackhole"
2017-01-07 19:06:35 -05:00
"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()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-07 19:06:35 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
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",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-07 19:06:35 -05:00
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-07 19:06:35 -05:00
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-01-07 19:06:35 -05:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-07 19:06:35 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-07 19:06:35 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
2017-01-07 19:06:35 -05:00
Server: []*protocol.ServerEndpoint{
{
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()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-07 19:06:35 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
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",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-07 19:06:35 -05:00
UdpEnabled: true,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-07 19:06:35 -05:00
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-01-07 19:06:35 -05:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-07 19:06:35 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP, net.Network_UDP},
2017-01-07 19:06:35 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-07 19:06:35 -05:00
{
ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
2017-01-07 19:06:35 -05:00
Server: []*protocol.ServerEndpoint{
{
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),
})
assert(err, IsNil)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert(err, IsNil)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
CloseAllServers(servers)
}
func TestSocksBridageUDPWithRouting(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert(err, IsNil)
defer udpServer.Close()
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Tag: "out",
InboundTag: []string{"socks"},
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
Tag: "socks",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Address: net.NewIPOrDomain(net.LocalHostIP),
UdpEnabled: true,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
{
Tag: "out",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP, net.Network_UDP},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil)
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
2017-01-07 19:06:35 -05:00
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()
2018-03-01 05:53:39 -05:00
authPort := tcp.PickPort()
noAuthPort := tcp.PickPort()
2017-01-08 04:10:37 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-08 04:10:37 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(authPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
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",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-08 04:10:37 -05:00
UdpEnabled: false,
}),
},
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(noAuthPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
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",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-08 04:10:37 -05:00
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-08 04:10:37 -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
{
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
}
{
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
{
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
}
{
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
}