github.com/v2fly/v2ray-core/v4@v4.45.2/testing/scenarios/shadowsocks_test.go (about)

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