1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/testing/scenarios/socks_test.go

373 lines
9.7 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"
2021-10-28 06:34:19 -04:00
"google.golang.org/protobuf/types/known/anypb"
socks4 "h12.io/socks"
2018-10-12 17:57:56 -04:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/proxyman"
"github.com/v2fly/v2ray-core/v5/app/router"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/proxy/blackhole"
"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
"github.com/v2fly/v2ray-core/v5/proxy/freedom"
"github.com/v2fly/v2ray-core/v5/proxy/socks"
"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
2017-01-07 19:06:35 -05:00
)
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{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
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
func TestSocksConformanceMod(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
{
2020-06-27 08:36:10 -04:00
dialer := socks4.Dial("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
}
{
2020-06-27 08:36:10 -04:00
dialer := socks4.Dial("socks4://" + net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
conn, err := dialer("tcp", net.TCPDestination(net.LocalHostIP, 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
}