github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/testing/scenarios/socks_test.go (about)

     1  package scenarios
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	xproxy "golang.org/x/net/proxy"
     8  	socks4 "h12.io/socks"
     9  
    10  	"v2ray.com/core"
    11  	"v2ray.com/core/app/proxyman"
    12  	"v2ray.com/core/app/router"
    13  	"v2ray.com/core/common"
    14  	"v2ray.com/core/common/net"
    15  	"v2ray.com/core/common/protocol"
    16  	"v2ray.com/core/common/serial"
    17  	"v2ray.com/core/proxy/blackhole"
    18  	"v2ray.com/core/proxy/dokodemo"
    19  	"v2ray.com/core/proxy/freedom"
    20  	"v2ray.com/core/proxy/socks"
    21  	"v2ray.com/core/testing/servers/tcp"
    22  	"v2ray.com/core/testing/servers/udp"
    23  )
    24  
    25  func TestSocksBridgeTCP(t *testing.T) {
    26  	tcpServer := tcp.Server{
    27  		MsgProcessor: xor,
    28  	}
    29  	dest, err := tcpServer.Start()
    30  	common.Must(err)
    31  	defer tcpServer.Close()
    32  
    33  	serverPort := tcp.PickPort()
    34  	serverConfig := &core.Config{
    35  		Inbound: []*core.InboundHandlerConfig{
    36  			{
    37  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    38  					PortRange: net.SinglePortRange(serverPort),
    39  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    40  				}),
    41  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
    42  					AuthType: socks.AuthType_PASSWORD,
    43  					Accounts: map[string]string{
    44  						"Test Account": "Test Password",
    45  					},
    46  					Address:    net.NewIPOrDomain(net.LocalHostIP),
    47  					UdpEnabled: false,
    48  				}),
    49  			},
    50  		},
    51  		Outbound: []*core.OutboundHandlerConfig{
    52  			{
    53  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    54  			},
    55  		},
    56  	}
    57  
    58  	clientPort := tcp.PickPort()
    59  	clientConfig := &core.Config{
    60  		Inbound: []*core.InboundHandlerConfig{
    61  			{
    62  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    63  					PortRange: net.SinglePortRange(clientPort),
    64  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    65  				}),
    66  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    67  					Address: net.NewIPOrDomain(dest.Address),
    68  					Port:    uint32(dest.Port),
    69  					NetworkList: &net.NetworkList{
    70  						Network: []net.Network{net.Network_TCP},
    71  					},
    72  				}),
    73  			},
    74  		},
    75  		Outbound: []*core.OutboundHandlerConfig{
    76  			{
    77  				ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
    78  					Server: []*protocol.ServerEndpoint{
    79  						{
    80  							Address: net.NewIPOrDomain(net.LocalHostIP),
    81  							Port:    uint32(serverPort),
    82  							User: []*protocol.User{
    83  								{
    84  									Account: serial.ToTypedMessage(&socks.Account{
    85  										Username: "Test Account",
    86  										Password: "Test Password",
    87  									}),
    88  								},
    89  							},
    90  						},
    91  					},
    92  				}),
    93  			},
    94  		},
    95  	}
    96  
    97  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
    98  	common.Must(err)
    99  	defer CloseAllServers(servers)
   100  
   101  	if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
   102  		t.Error(err)
   103  	}
   104  }
   105  
   106  func TestSocksBridageUDP(t *testing.T) {
   107  	udpServer := udp.Server{
   108  		MsgProcessor: xor,
   109  	}
   110  	dest, err := udpServer.Start()
   111  	common.Must(err)
   112  	defer udpServer.Close()
   113  
   114  	serverPort := tcp.PickPort()
   115  	serverConfig := &core.Config{
   116  		Inbound: []*core.InboundHandlerConfig{
   117  			{
   118  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   119  					PortRange: net.SinglePortRange(serverPort),
   120  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   121  				}),
   122  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
   123  					AuthType: socks.AuthType_PASSWORD,
   124  					Accounts: map[string]string{
   125  						"Test Account": "Test Password",
   126  					},
   127  					Address:    net.NewIPOrDomain(net.LocalHostIP),
   128  					UdpEnabled: true,
   129  				}),
   130  			},
   131  		},
   132  		Outbound: []*core.OutboundHandlerConfig{
   133  			{
   134  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   135  			},
   136  		},
   137  	}
   138  
   139  	clientPort := tcp.PickPort()
   140  	clientConfig := &core.Config{
   141  		Inbound: []*core.InboundHandlerConfig{
   142  			{
   143  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   144  					PortRange: net.SinglePortRange(clientPort),
   145  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   146  				}),
   147  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   148  					Address: net.NewIPOrDomain(dest.Address),
   149  					Port:    uint32(dest.Port),
   150  					NetworkList: &net.NetworkList{
   151  						Network: []net.Network{net.Network_TCP, net.Network_UDP},
   152  					},
   153  				}),
   154  			},
   155  		},
   156  		Outbound: []*core.OutboundHandlerConfig{
   157  			{
   158  				ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
   159  					Server: []*protocol.ServerEndpoint{
   160  						{
   161  							Address: net.NewIPOrDomain(net.LocalHostIP),
   162  							Port:    uint32(serverPort),
   163  							User: []*protocol.User{
   164  								{
   165  									Account: serial.ToTypedMessage(&socks.Account{
   166  										Username: "Test Account",
   167  										Password: "Test Password",
   168  									}),
   169  								},
   170  							},
   171  						},
   172  					},
   173  				}),
   174  			},
   175  		},
   176  	}
   177  
   178  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   179  	common.Must(err)
   180  	defer CloseAllServers(servers)
   181  
   182  	if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
   183  		t.Error(err)
   184  	}
   185  }
   186  
   187  func TestSocksBridageUDPWithRouting(t *testing.T) {
   188  	udpServer := udp.Server{
   189  		MsgProcessor: xor,
   190  	}
   191  	dest, err := udpServer.Start()
   192  	common.Must(err)
   193  	defer udpServer.Close()
   194  
   195  	serverPort := tcp.PickPort()
   196  	serverConfig := &core.Config{
   197  		App: []*serial.TypedMessage{
   198  			serial.ToTypedMessage(&router.Config{
   199  				Rule: []*router.RoutingRule{
   200  					{
   201  						TargetTag: &router.RoutingRule_Tag{
   202  							Tag: "out",
   203  						},
   204  						InboundTag: []string{"socks"},
   205  					},
   206  				},
   207  			}),
   208  		},
   209  		Inbound: []*core.InboundHandlerConfig{
   210  			{
   211  				Tag: "socks",
   212  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   213  					PortRange: net.SinglePortRange(serverPort),
   214  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   215  				}),
   216  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
   217  					AuthType:   socks.AuthType_NO_AUTH,
   218  					Address:    net.NewIPOrDomain(net.LocalHostIP),
   219  					UdpEnabled: true,
   220  				}),
   221  			},
   222  		},
   223  		Outbound: []*core.OutboundHandlerConfig{
   224  			{
   225  				ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
   226  			},
   227  			{
   228  				Tag:           "out",
   229  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   230  			},
   231  		},
   232  	}
   233  
   234  	clientPort := tcp.PickPort()
   235  	clientConfig := &core.Config{
   236  		Inbound: []*core.InboundHandlerConfig{
   237  			{
   238  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   239  					PortRange: net.SinglePortRange(clientPort),
   240  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   241  				}),
   242  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   243  					Address: net.NewIPOrDomain(dest.Address),
   244  					Port:    uint32(dest.Port),
   245  					NetworkList: &net.NetworkList{
   246  						Network: []net.Network{net.Network_TCP, net.Network_UDP},
   247  					},
   248  				}),
   249  			},
   250  		},
   251  		Outbound: []*core.OutboundHandlerConfig{
   252  			{
   253  				ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{
   254  					Server: []*protocol.ServerEndpoint{
   255  						{
   256  							Address: net.NewIPOrDomain(net.LocalHostIP),
   257  							Port:    uint32(serverPort),
   258  						},
   259  					},
   260  				}),
   261  			},
   262  		},
   263  	}
   264  
   265  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   266  	common.Must(err)
   267  	defer CloseAllServers(servers)
   268  
   269  	if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
   270  		t.Error(err)
   271  	}
   272  }
   273  
   274  func TestSocksConformanceMod(t *testing.T) {
   275  	tcpServer := tcp.Server{
   276  		MsgProcessor: xor,
   277  	}
   278  	dest, err := tcpServer.Start()
   279  	common.Must(err)
   280  	defer tcpServer.Close()
   281  
   282  	authPort := tcp.PickPort()
   283  	noAuthPort := tcp.PickPort()
   284  	serverConfig := &core.Config{
   285  		Inbound: []*core.InboundHandlerConfig{
   286  			{
   287  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   288  					PortRange: net.SinglePortRange(authPort),
   289  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   290  				}),
   291  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
   292  					AuthType: socks.AuthType_PASSWORD,
   293  					Accounts: map[string]string{
   294  						"Test Account": "Test Password",
   295  					},
   296  					Address:    net.NewIPOrDomain(net.LocalHostIP),
   297  					UdpEnabled: false,
   298  				}),
   299  			},
   300  			{
   301  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   302  					PortRange: net.SinglePortRange(noAuthPort),
   303  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   304  				}),
   305  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
   306  					AuthType: socks.AuthType_NO_AUTH,
   307  					Accounts: map[string]string{
   308  						"Test Account": "Test Password",
   309  					},
   310  					Address:    net.NewIPOrDomain(net.LocalHostIP),
   311  					UdpEnabled: false,
   312  				}),
   313  			},
   314  		},
   315  		Outbound: []*core.OutboundHandlerConfig{
   316  			{
   317  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   318  			},
   319  		},
   320  	}
   321  
   322  	servers, err := InitializeServerConfigs(serverConfig)
   323  	common.Must(err)
   324  	defer CloseAllServers(servers)
   325  
   326  	{
   327  		noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct)
   328  		common.Must(err)
   329  		conn, err := noAuthDialer.Dial("tcp", dest.NetAddr())
   330  		common.Must(err)
   331  		defer conn.Close()
   332  
   333  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   334  			t.Error(err)
   335  		}
   336  	}
   337  
   338  	{
   339  		authDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, authPort).NetAddr(), &xproxy.Auth{User: "Test Account", Password: "Test Password"}, xproxy.Direct)
   340  		common.Must(err)
   341  		conn, err := authDialer.Dial("tcp", dest.NetAddr())
   342  		common.Must(err)
   343  		defer conn.Close()
   344  
   345  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   346  			t.Error(err)
   347  		}
   348  	}
   349  
   350  	{
   351  		dialer := socks4.Dial("socks4://" + net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
   352  		conn, err := dialer("tcp", dest.NetAddr())
   353  		common.Must(err)
   354  		defer conn.Close()
   355  
   356  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   357  			t.Error(err)
   358  		}
   359  	}
   360  
   361  	{
   362  		dialer := socks4.Dial("socks4://" + net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
   363  		conn, err := dialer("tcp", net.TCPDestination(net.LocalHostIP, tcpServer.Port).NetAddr())
   364  		common.Must(err)
   365  		defer conn.Close()
   366  
   367  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   368  			t.Error(err)
   369  		}
   370  	}
   371  }