github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/testing/scenarios/shadowsocks_test.go (about)

     1  package scenarios
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"golang.org/x/sync/errgroup"
     8  	"google.golang.org/protobuf/types/known/anypb"
     9  
    10  	core "github.com/v2fly/v2ray-core/v5"
    11  	"github.com/v2fly/v2ray-core/v5/app/log"
    12  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    13  	"github.com/v2fly/v2ray-core/v5/common"
    14  	clog "github.com/v2fly/v2ray-core/v5/common/log"
    15  	"github.com/v2fly/v2ray-core/v5/common/net"
    16  	"github.com/v2fly/v2ray-core/v5/common/protocol"
    17  	"github.com/v2fly/v2ray-core/v5/common/serial"
    18  	"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
    19  	"github.com/v2fly/v2ray-core/v5/proxy/freedom"
    20  	"github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
    21  	"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
    22  	"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
    23  )
    24  
    25  func TestShadowsocksChaCha20Poly1305TCP(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  	account := serial.ToTypedMessage(&shadowsocks.Account{
    34  		Password:   "shadowsocks-password",
    35  		CipherType: shadowsocks.CipherType_CHACHA20_POLY1305,
    36  	})
    37  
    38  	serverPort := tcp.PickPort()
    39  	serverConfig := &core.Config{
    40  		Inbound: []*core.InboundHandlerConfig{
    41  			{
    42  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    43  					PortRange: net.SinglePortRange(serverPort),
    44  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    45  				}),
    46  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
    47  					User: &protocol.User{
    48  						Account: account,
    49  						Level:   1,
    50  					},
    51  					Network: []net.Network{net.Network_TCP},
    52  				}),
    53  			},
    54  		},
    55  		Outbound: []*core.OutboundHandlerConfig{
    56  			{
    57  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    58  			},
    59  		},
    60  	}
    61  
    62  	clientPort := tcp.PickPort()
    63  	clientConfig := &core.Config{
    64  		Inbound: []*core.InboundHandlerConfig{
    65  			{
    66  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    67  					PortRange: net.SinglePortRange(clientPort),
    68  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    69  				}),
    70  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    71  					Address:  net.NewIPOrDomain(dest.Address),
    72  					Port:     uint32(dest.Port),
    73  					Networks: []net.Network{net.Network_TCP},
    74  				}),
    75  			},
    76  		},
    77  		Outbound: []*core.OutboundHandlerConfig{
    78  			{
    79  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
    80  					Server: []*protocol.ServerEndpoint{
    81  						{
    82  							Address: net.NewIPOrDomain(net.LocalHostIP),
    83  							Port:    uint32(serverPort),
    84  							User: []*protocol.User{
    85  								{
    86  									Account: account,
    87  								},
    88  							},
    89  						},
    90  					},
    91  				}),
    92  			},
    93  		},
    94  	}
    95  
    96  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
    97  	common.Must(err)
    98  	defer CloseAllServers(servers)
    99  
   100  	var errGroup errgroup.Group
   101  	for i := 0; i < 10; i++ {
   102  		errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
   103  	}
   104  	if err := errGroup.Wait(); err != nil {
   105  		t.Error(err)
   106  	}
   107  }
   108  
   109  func TestShadowsocksAES256GCMTCP(t *testing.T) {
   110  	tcpServer := tcp.Server{
   111  		MsgProcessor: xor,
   112  	}
   113  	dest, err := tcpServer.Start()
   114  	common.Must(err)
   115  	defer tcpServer.Close()
   116  
   117  	account := serial.ToTypedMessage(&shadowsocks.Account{
   118  		Password:   "shadowsocks-password",
   119  		CipherType: shadowsocks.CipherType_AES_256_GCM,
   120  	})
   121  
   122  	serverPort := tcp.PickPort()
   123  	serverConfig := &core.Config{
   124  		App: []*anypb.Any{
   125  			serial.ToTypedMessage(&log.Config{
   126  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   127  			}),
   128  		},
   129  		Inbound: []*core.InboundHandlerConfig{
   130  			{
   131  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   132  					PortRange: net.SinglePortRange(serverPort),
   133  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   134  				}),
   135  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
   136  					User: &protocol.User{
   137  						Account: account,
   138  						Level:   1,
   139  					},
   140  					Network: []net.Network{net.Network_TCP},
   141  				}),
   142  			},
   143  		},
   144  		Outbound: []*core.OutboundHandlerConfig{
   145  			{
   146  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   147  			},
   148  		},
   149  	}
   150  
   151  	clientPort := tcp.PickPort()
   152  	clientConfig := &core.Config{
   153  		App: []*anypb.Any{
   154  			serial.ToTypedMessage(&log.Config{
   155  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   156  			}),
   157  		},
   158  		Inbound: []*core.InboundHandlerConfig{
   159  			{
   160  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   161  					PortRange: net.SinglePortRange(clientPort),
   162  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   163  				}),
   164  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   165  					Address:  net.NewIPOrDomain(dest.Address),
   166  					Port:     uint32(dest.Port),
   167  					Networks: []net.Network{net.Network_TCP},
   168  				}),
   169  			},
   170  		},
   171  		Outbound: []*core.OutboundHandlerConfig{
   172  			{
   173  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
   174  					Server: []*protocol.ServerEndpoint{
   175  						{
   176  							Address: net.NewIPOrDomain(net.LocalHostIP),
   177  							Port:    uint32(serverPort),
   178  							User: []*protocol.User{
   179  								{
   180  									Account: account,
   181  								},
   182  							},
   183  						},
   184  					},
   185  				}),
   186  			},
   187  		},
   188  	}
   189  
   190  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   191  	common.Must(err)
   192  	defer CloseAllServers(servers)
   193  
   194  	var errGroup errgroup.Group
   195  	for i := 0; i < 10; i++ {
   196  		errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
   197  	}
   198  
   199  	if err := errGroup.Wait(); err != nil {
   200  		t.Error(err)
   201  	}
   202  }
   203  
   204  func TestShadowsocksAES128GCMUDP(t *testing.T) {
   205  	udpServer := udp.Server{
   206  		MsgProcessor: xor,
   207  	}
   208  	dest, err := udpServer.Start()
   209  	common.Must(err)
   210  	defer udpServer.Close()
   211  
   212  	account := serial.ToTypedMessage(&shadowsocks.Account{
   213  		Password:   "shadowsocks-password",
   214  		CipherType: shadowsocks.CipherType_AES_128_GCM,
   215  	})
   216  
   217  	serverPort := udp.PickPort()
   218  	serverConfig := &core.Config{
   219  		App: []*anypb.Any{
   220  			serial.ToTypedMessage(&log.Config{
   221  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   222  			}),
   223  		},
   224  		Inbound: []*core.InboundHandlerConfig{
   225  			{
   226  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   227  					PortRange: net.SinglePortRange(serverPort),
   228  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   229  				}),
   230  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
   231  					User: &protocol.User{
   232  						Account: account,
   233  						Level:   1,
   234  					},
   235  					Network: []net.Network{net.Network_UDP},
   236  				}),
   237  			},
   238  		},
   239  		Outbound: []*core.OutboundHandlerConfig{
   240  			{
   241  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   242  			},
   243  		},
   244  	}
   245  
   246  	clientPort := udp.PickPort()
   247  	clientConfig := &core.Config{
   248  		App: []*anypb.Any{
   249  			serial.ToTypedMessage(&log.Config{
   250  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   251  			}),
   252  		},
   253  		Inbound: []*core.InboundHandlerConfig{
   254  			{
   255  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   256  					PortRange: net.SinglePortRange(clientPort),
   257  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   258  				}),
   259  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   260  					Address:  net.NewIPOrDomain(dest.Address),
   261  					Port:     uint32(dest.Port),
   262  					Networks: []net.Network{net.Network_UDP},
   263  				}),
   264  			},
   265  		},
   266  		Outbound: []*core.OutboundHandlerConfig{
   267  			{
   268  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
   269  					Server: []*protocol.ServerEndpoint{
   270  						{
   271  							Address: net.NewIPOrDomain(net.LocalHostIP),
   272  							Port:    uint32(serverPort),
   273  							User: []*protocol.User{
   274  								{
   275  									Account: account,
   276  								},
   277  							},
   278  						},
   279  					},
   280  				}),
   281  			},
   282  		},
   283  	}
   284  
   285  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   286  	common.Must(err)
   287  	defer CloseAllServers(servers)
   288  
   289  	var errGroup errgroup.Group
   290  	for i := 0; i < 10; i++ {
   291  		errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5))
   292  	}
   293  	if err := errGroup.Wait(); err != nil {
   294  		t.Error(err)
   295  	}
   296  }
   297  
   298  func TestShadowsocksAES128GCMUDPMux(t *testing.T) {
   299  	udpServer := udp.Server{
   300  		MsgProcessor: xor,
   301  	}
   302  	dest, err := udpServer.Start()
   303  	common.Must(err)
   304  	defer udpServer.Close()
   305  
   306  	account := serial.ToTypedMessage(&shadowsocks.Account{
   307  		Password:   "shadowsocks-password",
   308  		CipherType: shadowsocks.CipherType_AES_128_GCM,
   309  	})
   310  
   311  	serverPort := tcp.PickPort()
   312  	serverConfig := &core.Config{
   313  		App: []*anypb.Any{
   314  			serial.ToTypedMessage(&log.Config{
   315  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   316  			}),
   317  		},
   318  		Inbound: []*core.InboundHandlerConfig{
   319  			{
   320  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   321  					PortRange: net.SinglePortRange(serverPort),
   322  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   323  				}),
   324  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
   325  					User: &protocol.User{
   326  						Account: account,
   327  						Level:   1,
   328  					},
   329  					Network: []net.Network{net.Network_TCP},
   330  				}),
   331  			},
   332  		},
   333  		Outbound: []*core.OutboundHandlerConfig{
   334  			{
   335  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   336  			},
   337  		},
   338  	}
   339  
   340  	clientPort := udp.PickPort()
   341  	clientConfig := &core.Config{
   342  		App: []*anypb.Any{
   343  			serial.ToTypedMessage(&log.Config{
   344  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   345  			}),
   346  		},
   347  		Inbound: []*core.InboundHandlerConfig{
   348  			{
   349  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   350  					PortRange: net.SinglePortRange(clientPort),
   351  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   352  				}),
   353  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   354  					Address:  net.NewIPOrDomain(dest.Address),
   355  					Port:     uint32(dest.Port),
   356  					Networks: []net.Network{net.Network_UDP},
   357  				}),
   358  			},
   359  		},
   360  		Outbound: []*core.OutboundHandlerConfig{
   361  			{
   362  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   363  					MultiplexSettings: &proxyman.MultiplexingConfig{
   364  						Enabled:     true,
   365  						Concurrency: 8,
   366  					},
   367  				}),
   368  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
   369  					Server: []*protocol.ServerEndpoint{
   370  						{
   371  							Address: net.NewIPOrDomain(net.LocalHostIP),
   372  							Port:    uint32(serverPort),
   373  							User: []*protocol.User{
   374  								{
   375  									Account: account,
   376  								},
   377  							},
   378  						},
   379  					},
   380  				}),
   381  			},
   382  		},
   383  	}
   384  
   385  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   386  	common.Must(err)
   387  	defer CloseAllServers(servers)
   388  
   389  	var errGroup errgroup.Group
   390  	for i := 0; i < 10; i++ {
   391  		errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5))
   392  	}
   393  	if err := errGroup.Wait(); err != nil {
   394  		t.Error(err)
   395  	}
   396  }
   397  
   398  func TestShadowsocksNone(t *testing.T) {
   399  	tcpServer := tcp.Server{
   400  		MsgProcessor: xor,
   401  	}
   402  	dest, err := tcpServer.Start()
   403  	common.Must(err)
   404  
   405  	defer tcpServer.Close()
   406  
   407  	account := serial.ToTypedMessage(&shadowsocks.Account{
   408  		Password:   "shadowsocks-password",
   409  		CipherType: shadowsocks.CipherType_NONE,
   410  	})
   411  
   412  	serverPort := tcp.PickPort()
   413  	serverConfig := &core.Config{
   414  		Inbound: []*core.InboundHandlerConfig{
   415  			{
   416  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   417  					PortRange: net.SinglePortRange(serverPort),
   418  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   419  				}),
   420  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
   421  					User: &protocol.User{
   422  						Account: account,
   423  						Level:   1,
   424  					},
   425  					Network: []net.Network{net.Network_TCP},
   426  				}),
   427  			},
   428  		},
   429  		Outbound: []*core.OutboundHandlerConfig{
   430  			{
   431  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   432  			},
   433  		},
   434  	}
   435  
   436  	clientPort := tcp.PickPort()
   437  	clientConfig := &core.Config{
   438  		Inbound: []*core.InboundHandlerConfig{
   439  			{
   440  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   441  					PortRange: net.SinglePortRange(clientPort),
   442  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   443  				}),
   444  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   445  					Address:  net.NewIPOrDomain(dest.Address),
   446  					Port:     uint32(dest.Port),
   447  					Networks: []net.Network{net.Network_TCP},
   448  				}),
   449  			},
   450  		},
   451  		Outbound: []*core.OutboundHandlerConfig{
   452  			{
   453  				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
   454  					Server: []*protocol.ServerEndpoint{
   455  						{
   456  							Address: net.NewIPOrDomain(net.LocalHostIP),
   457  							Port:    uint32(serverPort),
   458  							User: []*protocol.User{
   459  								{
   460  									Account: account,
   461  								},
   462  							},
   463  						},
   464  					},
   465  				}),
   466  			},
   467  		},
   468  	}
   469  
   470  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   471  	common.Must(err)
   472  
   473  	defer CloseAllServers(servers)
   474  
   475  	var errGroup errgroup.Group
   476  	for i := 0; i < 10; i++ {
   477  		errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
   478  	}
   479  
   480  	if err := errGroup.Wait(); err != nil {
   481  		t.Fatal(err)
   482  	}
   483  }