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

     1  package scenarios
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"golang.org/x/sync/errgroup"
     9  	"v2ray.com/core"
    10  	"v2ray.com/core/app/log"
    11  	"v2ray.com/core/app/proxyman"
    12  	"v2ray.com/core/common"
    13  	clog "v2ray.com/core/common/log"
    14  	"v2ray.com/core/common/net"
    15  	"v2ray.com/core/common/protocol"
    16  	"v2ray.com/core/common/serial"
    17  	"v2ray.com/core/common/uuid"
    18  	"v2ray.com/core/proxy/dokodemo"
    19  	"v2ray.com/core/proxy/freedom"
    20  	"v2ray.com/core/proxy/vmess"
    21  	"v2ray.com/core/proxy/vmess/inbound"
    22  	"v2ray.com/core/proxy/vmess/outbound"
    23  	"v2ray.com/core/testing/servers/tcp"
    24  	"v2ray.com/core/testing/servers/udp"
    25  	"v2ray.com/core/transport/internet"
    26  	"v2ray.com/core/transport/internet/kcp"
    27  )
    28  
    29  func TestVMessDynamicPort(t *testing.T) {
    30  	tcpServer := tcp.Server{
    31  		MsgProcessor: xor,
    32  	}
    33  	dest, err := tcpServer.Start()
    34  	common.Must(err)
    35  	defer tcpServer.Close()
    36  
    37  	userID := protocol.NewID(uuid.New())
    38  	serverPort := tcp.PickPort()
    39  	serverConfig := &core.Config{
    40  		App: []*serial.TypedMessage{
    41  			serial.ToTypedMessage(&log.Config{
    42  				ErrorLogLevel: clog.Severity_Debug,
    43  				ErrorLogType:  log.LogType_Console,
    44  			}),
    45  		},
    46  		Inbound: []*core.InboundHandlerConfig{
    47  			{
    48  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    49  					PortRange: net.SinglePortRange(serverPort),
    50  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    51  				}),
    52  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
    53  					User: []*protocol.User{
    54  						{
    55  							Account: serial.ToTypedMessage(&vmess.Account{
    56  								Id: userID.String(),
    57  							}),
    58  						},
    59  					},
    60  					Detour: &inbound.DetourConfig{
    61  						To: "detour",
    62  					},
    63  				}),
    64  			},
    65  			{
    66  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    67  					PortRange: &net.PortRange{
    68  						From: uint32(serverPort + 1),
    69  						To:   uint32(serverPort + 100),
    70  					},
    71  					Listen: net.NewIPOrDomain(net.LocalHostIP),
    72  					AllocationStrategy: &proxyman.AllocationStrategy{
    73  						Type: proxyman.AllocationStrategy_Random,
    74  						Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
    75  							Value: 2,
    76  						},
    77  						Refresh: &proxyman.AllocationStrategy_AllocationStrategyRefresh{
    78  							Value: 5,
    79  						},
    80  					},
    81  				}),
    82  				ProxySettings: serial.ToTypedMessage(&inbound.Config{}),
    83  				Tag:           "detour",
    84  			},
    85  		},
    86  		Outbound: []*core.OutboundHandlerConfig{
    87  			{
    88  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    89  			},
    90  		},
    91  	}
    92  
    93  	clientPort := tcp.PickPort()
    94  	clientConfig := &core.Config{
    95  		App: []*serial.TypedMessage{
    96  			serial.ToTypedMessage(&log.Config{
    97  				ErrorLogLevel: clog.Severity_Debug,
    98  				ErrorLogType:  log.LogType_Console,
    99  			}),
   100  		},
   101  		Inbound: []*core.InboundHandlerConfig{
   102  			{
   103  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   104  					PortRange: net.SinglePortRange(clientPort),
   105  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   106  				}),
   107  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   108  					Address: net.NewIPOrDomain(dest.Address),
   109  					Port:    uint32(dest.Port),
   110  					NetworkList: &net.NetworkList{
   111  						Network: []net.Network{net.Network_TCP},
   112  					},
   113  				}),
   114  			},
   115  		},
   116  		Outbound: []*core.OutboundHandlerConfig{
   117  			{
   118  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   119  					Receiver: []*protocol.ServerEndpoint{
   120  						{
   121  							Address: net.NewIPOrDomain(net.LocalHostIP),
   122  							Port:    uint32(serverPort),
   123  							User: []*protocol.User{
   124  								{
   125  									Account: serial.ToTypedMessage(&vmess.Account{
   126  										Id: userID.String(),
   127  									}),
   128  								},
   129  							},
   130  						},
   131  					},
   132  				}),
   133  			},
   134  		},
   135  	}
   136  
   137  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   138  	common.Must(err)
   139  	defer CloseAllServers(servers)
   140  
   141  	for i := 0; i < 10; i++ {
   142  		if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
   143  			t.Error(err)
   144  		}
   145  	}
   146  }
   147  
   148  func TestVMessGCM(t *testing.T) {
   149  	tcpServer := tcp.Server{
   150  		MsgProcessor: xor,
   151  	}
   152  	dest, err := tcpServer.Start()
   153  	common.Must(err)
   154  	defer tcpServer.Close()
   155  
   156  	userID := protocol.NewID(uuid.New())
   157  	serverPort := tcp.PickPort()
   158  	serverConfig := &core.Config{
   159  		App: []*serial.TypedMessage{
   160  			serial.ToTypedMessage(&log.Config{
   161  				ErrorLogLevel: clog.Severity_Debug,
   162  				ErrorLogType:  log.LogType_Console,
   163  			}),
   164  		},
   165  		Inbound: []*core.InboundHandlerConfig{
   166  			{
   167  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   168  					PortRange: net.SinglePortRange(serverPort),
   169  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   170  				}),
   171  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   172  					User: []*protocol.User{
   173  						{
   174  							Account: serial.ToTypedMessage(&vmess.Account{
   175  								Id:      userID.String(),
   176  								AlterId: 64,
   177  							}),
   178  						},
   179  					},
   180  				}),
   181  			},
   182  		},
   183  		Outbound: []*core.OutboundHandlerConfig{
   184  			{
   185  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   186  			},
   187  		},
   188  	}
   189  
   190  	clientPort := tcp.PickPort()
   191  	clientConfig := &core.Config{
   192  		App: []*serial.TypedMessage{
   193  			serial.ToTypedMessage(&log.Config{
   194  				ErrorLogLevel: clog.Severity_Debug,
   195  				ErrorLogType:  log.LogType_Console,
   196  			}),
   197  		},
   198  		Inbound: []*core.InboundHandlerConfig{
   199  			{
   200  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   201  					PortRange: net.SinglePortRange(clientPort),
   202  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   203  				}),
   204  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   205  					Address: net.NewIPOrDomain(dest.Address),
   206  					Port:    uint32(dest.Port),
   207  					NetworkList: &net.NetworkList{
   208  						Network: []net.Network{net.Network_TCP},
   209  					},
   210  				}),
   211  			},
   212  		},
   213  		Outbound: []*core.OutboundHandlerConfig{
   214  			{
   215  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   216  					Receiver: []*protocol.ServerEndpoint{
   217  						{
   218  							Address: net.NewIPOrDomain(net.LocalHostIP),
   219  							Port:    uint32(serverPort),
   220  							User: []*protocol.User{
   221  								{
   222  									Account: serial.ToTypedMessage(&vmess.Account{
   223  										Id:      userID.String(),
   224  										AlterId: 64,
   225  										SecuritySettings: &protocol.SecurityConfig{
   226  											Type: protocol.SecurityType_AES128_GCM,
   227  										},
   228  									}),
   229  								},
   230  							},
   231  						},
   232  					},
   233  				}),
   234  			},
   235  		},
   236  	}
   237  
   238  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   239  	if err != nil {
   240  		t.Fatal("Failed to initialize all servers: ", err.Error())
   241  	}
   242  	defer CloseAllServers(servers)
   243  
   244  	var errg errgroup.Group
   245  	for i := 0; i < 10; i++ {
   246  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
   247  	}
   248  
   249  	if err := errg.Wait(); err != nil {
   250  		t.Error(err)
   251  	}
   252  }
   253  
   254  func TestVMessGCMReadv(t *testing.T) {
   255  	tcpServer := tcp.Server{
   256  		MsgProcessor: xor,
   257  	}
   258  	dest, err := tcpServer.Start()
   259  	common.Must(err)
   260  	defer tcpServer.Close()
   261  
   262  	userID := protocol.NewID(uuid.New())
   263  	serverPort := tcp.PickPort()
   264  	serverConfig := &core.Config{
   265  		App: []*serial.TypedMessage{
   266  			serial.ToTypedMessage(&log.Config{
   267  				ErrorLogLevel: clog.Severity_Debug,
   268  				ErrorLogType:  log.LogType_Console,
   269  			}),
   270  		},
   271  		Inbound: []*core.InboundHandlerConfig{
   272  			{
   273  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   274  					PortRange: net.SinglePortRange(serverPort),
   275  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   276  				}),
   277  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   278  					User: []*protocol.User{
   279  						{
   280  							Account: serial.ToTypedMessage(&vmess.Account{
   281  								Id:      userID.String(),
   282  								AlterId: 64,
   283  							}),
   284  						},
   285  					},
   286  				}),
   287  			},
   288  		},
   289  		Outbound: []*core.OutboundHandlerConfig{
   290  			{
   291  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   292  			},
   293  		},
   294  	}
   295  
   296  	clientPort := tcp.PickPort()
   297  	clientConfig := &core.Config{
   298  		App: []*serial.TypedMessage{
   299  			serial.ToTypedMessage(&log.Config{
   300  				ErrorLogLevel: clog.Severity_Debug,
   301  				ErrorLogType:  log.LogType_Console,
   302  			}),
   303  		},
   304  		Inbound: []*core.InboundHandlerConfig{
   305  			{
   306  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   307  					PortRange: net.SinglePortRange(clientPort),
   308  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   309  				}),
   310  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   311  					Address: net.NewIPOrDomain(dest.Address),
   312  					Port:    uint32(dest.Port),
   313  					NetworkList: &net.NetworkList{
   314  						Network: []net.Network{net.Network_TCP},
   315  					},
   316  				}),
   317  			},
   318  		},
   319  		Outbound: []*core.OutboundHandlerConfig{
   320  			{
   321  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   322  					Receiver: []*protocol.ServerEndpoint{
   323  						{
   324  							Address: net.NewIPOrDomain(net.LocalHostIP),
   325  							Port:    uint32(serverPort),
   326  							User: []*protocol.User{
   327  								{
   328  									Account: serial.ToTypedMessage(&vmess.Account{
   329  										Id:      userID.String(),
   330  										AlterId: 64,
   331  										SecuritySettings: &protocol.SecurityConfig{
   332  											Type: protocol.SecurityType_AES128_GCM,
   333  										},
   334  									}),
   335  								},
   336  							},
   337  						},
   338  					},
   339  				}),
   340  			},
   341  		},
   342  	}
   343  
   344  	const envName = "V2RAY_BUF_READV"
   345  	common.Must(os.Setenv(envName, "enable"))
   346  	defer os.Unsetenv(envName)
   347  
   348  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   349  	if err != nil {
   350  		t.Fatal("Failed to initialize all servers: ", err.Error())
   351  	}
   352  	defer CloseAllServers(servers)
   353  
   354  	var errg errgroup.Group
   355  	for i := 0; i < 10; i++ {
   356  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
   357  	}
   358  	if err := errg.Wait(); err != nil {
   359  		t.Error(err)
   360  	}
   361  }
   362  
   363  func TestVMessGCMUDP(t *testing.T) {
   364  	udpServer := udp.Server{
   365  		MsgProcessor: xor,
   366  	}
   367  	dest, err := udpServer.Start()
   368  	common.Must(err)
   369  	defer udpServer.Close()
   370  
   371  	userID := protocol.NewID(uuid.New())
   372  	serverPort := tcp.PickPort()
   373  	serverConfig := &core.Config{
   374  		App: []*serial.TypedMessage{
   375  			serial.ToTypedMessage(&log.Config{
   376  				ErrorLogLevel: clog.Severity_Debug,
   377  				ErrorLogType:  log.LogType_Console,
   378  			}),
   379  		},
   380  		Inbound: []*core.InboundHandlerConfig{
   381  			{
   382  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   383  					PortRange: net.SinglePortRange(serverPort),
   384  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   385  				}),
   386  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   387  					User: []*protocol.User{
   388  						{
   389  							Account: serial.ToTypedMessage(&vmess.Account{
   390  								Id:      userID.String(),
   391  								AlterId: 64,
   392  							}),
   393  						},
   394  					},
   395  				}),
   396  			},
   397  		},
   398  		Outbound: []*core.OutboundHandlerConfig{
   399  			{
   400  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   401  			},
   402  		},
   403  	}
   404  
   405  	clientPort := tcp.PickPort()
   406  	clientConfig := &core.Config{
   407  		App: []*serial.TypedMessage{
   408  			serial.ToTypedMessage(&log.Config{
   409  				ErrorLogLevel: clog.Severity_Debug,
   410  				ErrorLogType:  log.LogType_Console,
   411  			}),
   412  		},
   413  		Inbound: []*core.InboundHandlerConfig{
   414  			{
   415  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   416  					PortRange: net.SinglePortRange(clientPort),
   417  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   418  				}),
   419  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   420  					Address: net.NewIPOrDomain(dest.Address),
   421  					Port:    uint32(dest.Port),
   422  					NetworkList: &net.NetworkList{
   423  						Network: []net.Network{net.Network_UDP},
   424  					},
   425  				}),
   426  			},
   427  		},
   428  		Outbound: []*core.OutboundHandlerConfig{
   429  			{
   430  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   431  					Receiver: []*protocol.ServerEndpoint{
   432  						{
   433  							Address: net.NewIPOrDomain(net.LocalHostIP),
   434  							Port:    uint32(serverPort),
   435  							User: []*protocol.User{
   436  								{
   437  									Account: serial.ToTypedMessage(&vmess.Account{
   438  										Id:      userID.String(),
   439  										AlterId: 64,
   440  										SecuritySettings: &protocol.SecurityConfig{
   441  											Type: protocol.SecurityType_AES128_GCM,
   442  										},
   443  									}),
   444  								},
   445  							},
   446  						},
   447  					},
   448  				}),
   449  			},
   450  		},
   451  	}
   452  
   453  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   454  	common.Must(err)
   455  	defer CloseAllServers(servers)
   456  
   457  	var errg errgroup.Group
   458  	for i := 0; i < 10; i++ {
   459  		errg.Go(testUDPConn(clientPort, 1024, time.Second*5))
   460  	}
   461  	if err := errg.Wait(); err != nil {
   462  		t.Error(err)
   463  	}
   464  }
   465  
   466  func TestVMessChacha20(t *testing.T) {
   467  	tcpServer := tcp.Server{
   468  		MsgProcessor: xor,
   469  	}
   470  	dest, err := tcpServer.Start()
   471  	common.Must(err)
   472  	defer tcpServer.Close()
   473  
   474  	userID := protocol.NewID(uuid.New())
   475  	serverPort := tcp.PickPort()
   476  	serverConfig := &core.Config{
   477  		App: []*serial.TypedMessage{
   478  			serial.ToTypedMessage(&log.Config{
   479  				ErrorLogLevel: clog.Severity_Debug,
   480  				ErrorLogType:  log.LogType_Console,
   481  			}),
   482  		},
   483  		Inbound: []*core.InboundHandlerConfig{
   484  			{
   485  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   486  					PortRange: net.SinglePortRange(serverPort),
   487  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   488  				}),
   489  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   490  					User: []*protocol.User{
   491  						{
   492  							Account: serial.ToTypedMessage(&vmess.Account{
   493  								Id:      userID.String(),
   494  								AlterId: 64,
   495  							}),
   496  						},
   497  					},
   498  				}),
   499  			},
   500  		},
   501  		Outbound: []*core.OutboundHandlerConfig{
   502  			{
   503  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   504  			},
   505  		},
   506  	}
   507  
   508  	clientPort := tcp.PickPort()
   509  	clientConfig := &core.Config{
   510  		App: []*serial.TypedMessage{
   511  			serial.ToTypedMessage(&log.Config{
   512  				ErrorLogLevel: clog.Severity_Debug,
   513  				ErrorLogType:  log.LogType_Console,
   514  			}),
   515  		},
   516  		Inbound: []*core.InboundHandlerConfig{
   517  			{
   518  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   519  					PortRange: net.SinglePortRange(clientPort),
   520  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   521  				}),
   522  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   523  					Address: net.NewIPOrDomain(dest.Address),
   524  					Port:    uint32(dest.Port),
   525  					NetworkList: &net.NetworkList{
   526  						Network: []net.Network{net.Network_TCP},
   527  					},
   528  				}),
   529  			},
   530  		},
   531  		Outbound: []*core.OutboundHandlerConfig{
   532  			{
   533  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   534  					Receiver: []*protocol.ServerEndpoint{
   535  						{
   536  							Address: net.NewIPOrDomain(net.LocalHostIP),
   537  							Port:    uint32(serverPort),
   538  							User: []*protocol.User{
   539  								{
   540  									Account: serial.ToTypedMessage(&vmess.Account{
   541  										Id:      userID.String(),
   542  										AlterId: 64,
   543  										SecuritySettings: &protocol.SecurityConfig{
   544  											Type: protocol.SecurityType_CHACHA20_POLY1305,
   545  										},
   546  									}),
   547  								},
   548  							},
   549  						},
   550  					},
   551  				}),
   552  			},
   553  		},
   554  	}
   555  
   556  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   557  	common.Must(err)
   558  	defer CloseAllServers(servers)
   559  
   560  	var errg errgroup.Group
   561  	for i := 0; i < 10; i++ {
   562  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
   563  	}
   564  
   565  	if err := errg.Wait(); err != nil {
   566  		t.Error(err)
   567  	}
   568  }
   569  
   570  func TestVMessNone(t *testing.T) {
   571  	tcpServer := tcp.Server{
   572  		MsgProcessor: xor,
   573  	}
   574  	dest, err := tcpServer.Start()
   575  	common.Must(err)
   576  	defer tcpServer.Close()
   577  
   578  	userID := protocol.NewID(uuid.New())
   579  	serverPort := tcp.PickPort()
   580  	serverConfig := &core.Config{
   581  		App: []*serial.TypedMessage{
   582  			serial.ToTypedMessage(&log.Config{
   583  				ErrorLogLevel: clog.Severity_Debug,
   584  				ErrorLogType:  log.LogType_Console,
   585  			}),
   586  		},
   587  		Inbound: []*core.InboundHandlerConfig{
   588  			{
   589  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   590  					PortRange: net.SinglePortRange(serverPort),
   591  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   592  				}),
   593  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   594  					User: []*protocol.User{
   595  						{
   596  							Account: serial.ToTypedMessage(&vmess.Account{
   597  								Id:      userID.String(),
   598  								AlterId: 64,
   599  							}),
   600  						},
   601  					},
   602  				}),
   603  			},
   604  		},
   605  		Outbound: []*core.OutboundHandlerConfig{
   606  			{
   607  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   608  			},
   609  		},
   610  	}
   611  
   612  	clientPort := tcp.PickPort()
   613  	clientConfig := &core.Config{
   614  		App: []*serial.TypedMessage{
   615  			serial.ToTypedMessage(&log.Config{
   616  				ErrorLogLevel: clog.Severity_Debug,
   617  				ErrorLogType:  log.LogType_Console,
   618  			}),
   619  		},
   620  		Inbound: []*core.InboundHandlerConfig{
   621  			{
   622  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   623  					PortRange: net.SinglePortRange(clientPort),
   624  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   625  				}),
   626  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   627  					Address: net.NewIPOrDomain(dest.Address),
   628  					Port:    uint32(dest.Port),
   629  					NetworkList: &net.NetworkList{
   630  						Network: []net.Network{net.Network_TCP},
   631  					},
   632  				}),
   633  			},
   634  		},
   635  		Outbound: []*core.OutboundHandlerConfig{
   636  			{
   637  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   638  					Receiver: []*protocol.ServerEndpoint{
   639  						{
   640  							Address: net.NewIPOrDomain(net.LocalHostIP),
   641  							Port:    uint32(serverPort),
   642  							User: []*protocol.User{
   643  								{
   644  									Account: serial.ToTypedMessage(&vmess.Account{
   645  										Id:      userID.String(),
   646  										AlterId: 64,
   647  										SecuritySettings: &protocol.SecurityConfig{
   648  											Type: protocol.SecurityType_NONE,
   649  										},
   650  									}),
   651  								},
   652  							},
   653  						},
   654  					},
   655  				}),
   656  			},
   657  		},
   658  	}
   659  
   660  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   661  	common.Must(err)
   662  	defer CloseAllServers(servers)
   663  
   664  	var errg errgroup.Group
   665  	for i := 0; i < 10; i++ {
   666  		errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30))
   667  	}
   668  	if err := errg.Wait(); err != nil {
   669  		t.Error(err)
   670  	}
   671  }
   672  
   673  func TestVMessKCP(t *testing.T) {
   674  	tcpServer := tcp.Server{
   675  		MsgProcessor: xor,
   676  	}
   677  	dest, err := tcpServer.Start()
   678  	common.Must(err)
   679  	defer tcpServer.Close()
   680  
   681  	userID := protocol.NewID(uuid.New())
   682  	serverPort := udp.PickPort()
   683  	serverConfig := &core.Config{
   684  		App: []*serial.TypedMessage{
   685  			serial.ToTypedMessage(&log.Config{
   686  				ErrorLogLevel: clog.Severity_Debug,
   687  				ErrorLogType:  log.LogType_Console,
   688  			}),
   689  		},
   690  		Inbound: []*core.InboundHandlerConfig{
   691  			{
   692  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   693  					PortRange: net.SinglePortRange(serverPort),
   694  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   695  					StreamSettings: &internet.StreamConfig{
   696  						Protocol: internet.TransportProtocol_MKCP,
   697  					},
   698  				}),
   699  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   700  					User: []*protocol.User{
   701  						{
   702  							Account: serial.ToTypedMessage(&vmess.Account{
   703  								Id:      userID.String(),
   704  								AlterId: 64,
   705  							}),
   706  						},
   707  					},
   708  				}),
   709  			},
   710  		},
   711  		Outbound: []*core.OutboundHandlerConfig{
   712  			{
   713  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   714  			},
   715  		},
   716  	}
   717  
   718  	clientPort := tcp.PickPort()
   719  	clientConfig := &core.Config{
   720  		App: []*serial.TypedMessage{
   721  			serial.ToTypedMessage(&log.Config{
   722  				ErrorLogLevel: clog.Severity_Debug,
   723  				ErrorLogType:  log.LogType_Console,
   724  			}),
   725  		},
   726  		Inbound: []*core.InboundHandlerConfig{
   727  			{
   728  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   729  					PortRange: net.SinglePortRange(clientPort),
   730  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   731  				}),
   732  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   733  					Address: net.NewIPOrDomain(dest.Address),
   734  					Port:    uint32(dest.Port),
   735  					NetworkList: &net.NetworkList{
   736  						Network: []net.Network{net.Network_TCP},
   737  					},
   738  				}),
   739  			},
   740  		},
   741  		Outbound: []*core.OutboundHandlerConfig{
   742  			{
   743  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   744  					Receiver: []*protocol.ServerEndpoint{
   745  						{
   746  							Address: net.NewIPOrDomain(net.LocalHostIP),
   747  							Port:    uint32(serverPort),
   748  							User: []*protocol.User{
   749  								{
   750  									Account: serial.ToTypedMessage(&vmess.Account{
   751  										Id:      userID.String(),
   752  										AlterId: 64,
   753  										SecuritySettings: &protocol.SecurityConfig{
   754  											Type: protocol.SecurityType_AES128_GCM,
   755  										},
   756  									}),
   757  								},
   758  							},
   759  						},
   760  					},
   761  				}),
   762  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   763  					StreamSettings: &internet.StreamConfig{
   764  						Protocol: internet.TransportProtocol_MKCP,
   765  					},
   766  				}),
   767  			},
   768  		},
   769  	}
   770  
   771  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   772  	common.Must(err)
   773  	defer CloseAllServers(servers)
   774  
   775  	var errg errgroup.Group
   776  	for i := 0; i < 10; i++ {
   777  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Minute*2))
   778  	}
   779  	if err := errg.Wait(); err != nil {
   780  		t.Error(err)
   781  	}
   782  }
   783  
   784  func TestVMessKCPLarge(t *testing.T) {
   785  	tcpServer := tcp.Server{
   786  		MsgProcessor: xor,
   787  	}
   788  	dest, err := tcpServer.Start()
   789  	common.Must(err)
   790  	defer tcpServer.Close()
   791  
   792  	userID := protocol.NewID(uuid.New())
   793  	serverPort := udp.PickPort()
   794  	serverConfig := &core.Config{
   795  		App: []*serial.TypedMessage{
   796  			serial.ToTypedMessage(&log.Config{
   797  				ErrorLogLevel: clog.Severity_Debug,
   798  				ErrorLogType:  log.LogType_Console,
   799  			}),
   800  		},
   801  		Inbound: []*core.InboundHandlerConfig{
   802  			{
   803  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   804  					PortRange: net.SinglePortRange(serverPort),
   805  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   806  					StreamSettings: &internet.StreamConfig{
   807  						Protocol: internet.TransportProtocol_MKCP,
   808  						TransportSettings: []*internet.TransportConfig{
   809  							{
   810  								Protocol: internet.TransportProtocol_MKCP,
   811  								Settings: serial.ToTypedMessage(&kcp.Config{
   812  									ReadBuffer: &kcp.ReadBuffer{
   813  										Size: 512 * 1024,
   814  									},
   815  									WriteBuffer: &kcp.WriteBuffer{
   816  										Size: 512 * 1024,
   817  									},
   818  									UplinkCapacity: &kcp.UplinkCapacity{
   819  										Value: 20,
   820  									},
   821  									DownlinkCapacity: &kcp.DownlinkCapacity{
   822  										Value: 20,
   823  									},
   824  								}),
   825  							},
   826  						},
   827  					},
   828  				}),
   829  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   830  					User: []*protocol.User{
   831  						{
   832  							Account: serial.ToTypedMessage(&vmess.Account{
   833  								Id:      userID.String(),
   834  								AlterId: 64,
   835  							}),
   836  						},
   837  					},
   838  				}),
   839  			},
   840  		},
   841  		Outbound: []*core.OutboundHandlerConfig{
   842  			{
   843  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   844  			},
   845  		},
   846  	}
   847  
   848  	clientPort := tcp.PickPort()
   849  	clientConfig := &core.Config{
   850  		App: []*serial.TypedMessage{
   851  			serial.ToTypedMessage(&log.Config{
   852  				ErrorLogLevel: clog.Severity_Debug,
   853  				ErrorLogType:  log.LogType_Console,
   854  			}),
   855  		},
   856  		Inbound: []*core.InboundHandlerConfig{
   857  			{
   858  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   859  					PortRange: net.SinglePortRange(clientPort),
   860  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   861  				}),
   862  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   863  					Address: net.NewIPOrDomain(dest.Address),
   864  					Port:    uint32(dest.Port),
   865  					NetworkList: &net.NetworkList{
   866  						Network: []net.Network{net.Network_TCP},
   867  					},
   868  				}),
   869  			},
   870  		},
   871  		Outbound: []*core.OutboundHandlerConfig{
   872  			{
   873  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   874  					Receiver: []*protocol.ServerEndpoint{
   875  						{
   876  							Address: net.NewIPOrDomain(net.LocalHostIP),
   877  							Port:    uint32(serverPort),
   878  							User: []*protocol.User{
   879  								{
   880  									Account: serial.ToTypedMessage(&vmess.Account{
   881  										Id:      userID.String(),
   882  										AlterId: 64,
   883  										SecuritySettings: &protocol.SecurityConfig{
   884  											Type: protocol.SecurityType_AES128_GCM,
   885  										},
   886  									}),
   887  								},
   888  							},
   889  						},
   890  					},
   891  				}),
   892  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   893  					StreamSettings: &internet.StreamConfig{
   894  						Protocol: internet.TransportProtocol_MKCP,
   895  						TransportSettings: []*internet.TransportConfig{
   896  							{
   897  								Protocol: internet.TransportProtocol_MKCP,
   898  								Settings: serial.ToTypedMessage(&kcp.Config{
   899  									ReadBuffer: &kcp.ReadBuffer{
   900  										Size: 512 * 1024,
   901  									},
   902  									WriteBuffer: &kcp.WriteBuffer{
   903  										Size: 512 * 1024,
   904  									},
   905  									UplinkCapacity: &kcp.UplinkCapacity{
   906  										Value: 20,
   907  									},
   908  									DownlinkCapacity: &kcp.DownlinkCapacity{
   909  										Value: 20,
   910  									},
   911  								}),
   912  							},
   913  						},
   914  					},
   915  				}),
   916  			},
   917  		},
   918  	}
   919  
   920  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   921  	common.Must(err)
   922  
   923  	var errg errgroup.Group
   924  	for i := 0; i < 2; i++ {
   925  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Minute*5))
   926  	}
   927  	if err := errg.Wait(); err != nil {
   928  		t.Error(err)
   929  	}
   930  
   931  	defer func() {
   932  		<-time.After(5 * time.Second)
   933  		CloseAllServers(servers)
   934  	}()
   935  }
   936  
   937  func TestVMessGCMMux(t *testing.T) {
   938  	tcpServer := tcp.Server{
   939  		MsgProcessor: xor,
   940  	}
   941  	dest, err := tcpServer.Start()
   942  	common.Must(err)
   943  	defer tcpServer.Close()
   944  
   945  	userID := protocol.NewID(uuid.New())
   946  	serverPort := tcp.PickPort()
   947  	serverConfig := &core.Config{
   948  		App: []*serial.TypedMessage{
   949  			serial.ToTypedMessage(&log.Config{
   950  				ErrorLogLevel: clog.Severity_Debug,
   951  				ErrorLogType:  log.LogType_Console,
   952  			}),
   953  		},
   954  		Inbound: []*core.InboundHandlerConfig{
   955  			{
   956  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   957  					PortRange: net.SinglePortRange(serverPort),
   958  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   959  				}),
   960  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   961  					User: []*protocol.User{
   962  						{
   963  							Account: serial.ToTypedMessage(&vmess.Account{
   964  								Id:      userID.String(),
   965  								AlterId: 64,
   966  							}),
   967  						},
   968  					},
   969  				}),
   970  			},
   971  		},
   972  		Outbound: []*core.OutboundHandlerConfig{
   973  			{
   974  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   975  			},
   976  		},
   977  	}
   978  
   979  	clientPort := tcp.PickPort()
   980  	clientConfig := &core.Config{
   981  		App: []*serial.TypedMessage{
   982  			serial.ToTypedMessage(&log.Config{
   983  				ErrorLogLevel: clog.Severity_Debug,
   984  				ErrorLogType:  log.LogType_Console,
   985  			}),
   986  		},
   987  		Inbound: []*core.InboundHandlerConfig{
   988  			{
   989  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   990  					PortRange: net.SinglePortRange(clientPort),
   991  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   992  				}),
   993  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   994  					Address: net.NewIPOrDomain(dest.Address),
   995  					Port:    uint32(dest.Port),
   996  					NetworkList: &net.NetworkList{
   997  						Network: []net.Network{net.Network_TCP},
   998  					},
   999  				}),
  1000  			},
  1001  		},
  1002  		Outbound: []*core.OutboundHandlerConfig{
  1003  			{
  1004  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  1005  					MultiplexSettings: &proxyman.MultiplexingConfig{
  1006  						Enabled:     true,
  1007  						Concurrency: 4,
  1008  					},
  1009  				}),
  1010  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
  1011  					Receiver: []*protocol.ServerEndpoint{
  1012  						{
  1013  							Address: net.NewIPOrDomain(net.LocalHostIP),
  1014  							Port:    uint32(serverPort),
  1015  							User: []*protocol.User{
  1016  								{
  1017  									Account: serial.ToTypedMessage(&vmess.Account{
  1018  										Id:      userID.String(),
  1019  										AlterId: 64,
  1020  										SecuritySettings: &protocol.SecurityConfig{
  1021  											Type: protocol.SecurityType_AES128_GCM,
  1022  										},
  1023  									}),
  1024  								},
  1025  							},
  1026  						},
  1027  					},
  1028  				}),
  1029  			},
  1030  		},
  1031  	}
  1032  
  1033  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  1034  	common.Must(err)
  1035  	defer CloseAllServers(servers)
  1036  
  1037  	for range "abcd" {
  1038  		var errg errgroup.Group
  1039  		for i := 0; i < 16; i++ {
  1040  			errg.Go(testTCPConn(clientPort, 10240, time.Second*20))
  1041  		}
  1042  		if err := errg.Wait(); err != nil {
  1043  			t.Fatal(err)
  1044  		}
  1045  		time.Sleep(time.Second)
  1046  	}
  1047  }
  1048  
  1049  func TestVMessGCMMuxUDP(t *testing.T) {
  1050  	tcpServer := tcp.Server{
  1051  		MsgProcessor: xor,
  1052  	}
  1053  	dest, err := tcpServer.Start()
  1054  	common.Must(err)
  1055  	defer tcpServer.Close()
  1056  
  1057  	udpServer := udp.Server{
  1058  		MsgProcessor: xor,
  1059  	}
  1060  	udpDest, err := udpServer.Start()
  1061  	common.Must(err)
  1062  	defer udpServer.Close()
  1063  
  1064  	userID := protocol.NewID(uuid.New())
  1065  	serverPort := tcp.PickPort()
  1066  	serverConfig := &core.Config{
  1067  		App: []*serial.TypedMessage{
  1068  			serial.ToTypedMessage(&log.Config{
  1069  				ErrorLogLevel: clog.Severity_Debug,
  1070  				ErrorLogType:  log.LogType_Console,
  1071  			}),
  1072  		},
  1073  		Inbound: []*core.InboundHandlerConfig{
  1074  			{
  1075  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  1076  					PortRange: net.SinglePortRange(serverPort),
  1077  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
  1078  				}),
  1079  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
  1080  					User: []*protocol.User{
  1081  						{
  1082  							Account: serial.ToTypedMessage(&vmess.Account{
  1083  								Id:      userID.String(),
  1084  								AlterId: 64,
  1085  							}),
  1086  						},
  1087  					},
  1088  				}),
  1089  			},
  1090  		},
  1091  		Outbound: []*core.OutboundHandlerConfig{
  1092  			{
  1093  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  1094  			},
  1095  		},
  1096  	}
  1097  
  1098  	clientPort := tcp.PickPort()
  1099  	clientUDPPort := udp.PickPort()
  1100  	clientConfig := &core.Config{
  1101  		App: []*serial.TypedMessage{
  1102  			serial.ToTypedMessage(&log.Config{
  1103  				ErrorLogLevel: clog.Severity_Debug,
  1104  				ErrorLogType:  log.LogType_Console,
  1105  			}),
  1106  		},
  1107  		Inbound: []*core.InboundHandlerConfig{
  1108  			{
  1109  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  1110  					PortRange: net.SinglePortRange(clientPort),
  1111  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
  1112  				}),
  1113  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  1114  					Address: net.NewIPOrDomain(dest.Address),
  1115  					Port:    uint32(dest.Port),
  1116  					NetworkList: &net.NetworkList{
  1117  						Network: []net.Network{net.Network_TCP},
  1118  					},
  1119  				}),
  1120  			},
  1121  			{
  1122  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  1123  					PortRange: net.SinglePortRange(clientUDPPort),
  1124  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
  1125  				}),
  1126  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  1127  					Address: net.NewIPOrDomain(udpDest.Address),
  1128  					Port:    uint32(udpDest.Port),
  1129  					NetworkList: &net.NetworkList{
  1130  						Network: []net.Network{net.Network_UDP},
  1131  					},
  1132  				}),
  1133  			},
  1134  		},
  1135  		Outbound: []*core.OutboundHandlerConfig{
  1136  			{
  1137  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  1138  					MultiplexSettings: &proxyman.MultiplexingConfig{
  1139  						Enabled:     true,
  1140  						Concurrency: 4,
  1141  					},
  1142  				}),
  1143  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
  1144  					Receiver: []*protocol.ServerEndpoint{
  1145  						{
  1146  							Address: net.NewIPOrDomain(net.LocalHostIP),
  1147  							Port:    uint32(serverPort),
  1148  							User: []*protocol.User{
  1149  								{
  1150  									Account: serial.ToTypedMessage(&vmess.Account{
  1151  										Id:      userID.String(),
  1152  										AlterId: 64,
  1153  										SecuritySettings: &protocol.SecurityConfig{
  1154  											Type: protocol.SecurityType_AES128_GCM,
  1155  										},
  1156  									}),
  1157  								},
  1158  							},
  1159  						},
  1160  					},
  1161  				}),
  1162  			},
  1163  		},
  1164  	}
  1165  
  1166  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  1167  	common.Must(err)
  1168  
  1169  	for range "abcd" {
  1170  		var errg errgroup.Group
  1171  		for i := 0; i < 16; i++ {
  1172  			errg.Go(testTCPConn(clientPort, 10240, time.Second*20))
  1173  			errg.Go(testUDPConn(clientUDPPort, 1024, time.Second*10))
  1174  		}
  1175  		if err := errg.Wait(); err != nil {
  1176  			t.Error(err)
  1177  		}
  1178  		time.Sleep(time.Second)
  1179  	}
  1180  
  1181  	defer func() {
  1182  		<-time.After(5 * time.Second)
  1183  		CloseAllServers(servers)
  1184  	}()
  1185  }