1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/testing/scenarios/socks_test.go

372 lines
9.5 KiB
Go
Raw Normal View History

2017-01-07 19:06:35 -05:00
package scenarios
import (
"testing"
2019-01-11 11:17:59 -05:00
"time"
2017-01-07 19:06:35 -05:00
2017-01-08 04:10:37 -05:00
xproxy "golang.org/x/net/proxy"
socks4 "h12.io/socks"
2018-10-12 17:57:56 -04:00
2017-01-07 19:06:35 -05:00
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
2018-10-12 17:57:56 -04:00
"v2ray.com/core/app/router"
2019-01-11 11:17:59 -05:00
"v2ray.com/core/common"
"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"
)
func TestSocksBridgeTCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-01-11 11:17:59 -05:00
common.Must(err)
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)
2019-01-11 11:17:59 -05:00
common.Must(err)
defer CloseAllServers(servers)
2017-01-07 19:06:35 -05:00
2019-01-11 11:17:59 -05:00
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
t.Error(err)
}
2017-01-07 19:06:35 -05:00
}
func TestSocksBridageUDP(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-07 19:06:35 -05:00
2019-02-03 13:46:53 -05:00
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
func TestSocksBridageUDPWithRouting(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
defer udpServer.Close()
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2019-02-03 13:46:53 -05:00
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
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-01-08 04:10:37 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-08 04:10:37 -05:00
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-08 04:10:37 -05:00
conn, err := noAuthDialer.Dial("tcp", dest.NetAddr())
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2017-01-08 04:10:37 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-08 04:10:37 -05:00
conn, err := authDialer.Dial("tcp", dest.NetAddr())
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2017-01-08 04:10:37 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
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())
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2017-01-08 04:23:55 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
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())
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2017-01-08 04:23:55 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
2017-01-08 04:23:55 -05:00
}
2017-01-08 04:10:37 -05:00
}