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

     1  package scenarios
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  	"testing"
     9  	"time"
    10  
    11  	xproxy "golang.org/x/net/proxy"
    12  	"google.golang.org/protobuf/types/known/anypb"
    13  
    14  	core "github.com/v2fly/v2ray-core/v5"
    15  	"github.com/v2fly/v2ray-core/v5/app/dispatcher"
    16  	"github.com/v2fly/v2ray-core/v5/app/log"
    17  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    18  	_ "github.com/v2fly/v2ray-core/v5/app/proxyman/inbound"
    19  	_ "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound"
    20  	"github.com/v2fly/v2ray-core/v5/app/router"
    21  	"github.com/v2fly/v2ray-core/v5/common"
    22  	clog "github.com/v2fly/v2ray-core/v5/common/log"
    23  	"github.com/v2fly/v2ray-core/v5/common/net"
    24  	"github.com/v2fly/v2ray-core/v5/common/protocol"
    25  	"github.com/v2fly/v2ray-core/v5/common/serial"
    26  	"github.com/v2fly/v2ray-core/v5/common/uuid"
    27  	"github.com/v2fly/v2ray-core/v5/proxy/blackhole"
    28  	"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
    29  	"github.com/v2fly/v2ray-core/v5/proxy/freedom"
    30  	v2http "github.com/v2fly/v2ray-core/v5/proxy/http"
    31  	"github.com/v2fly/v2ray-core/v5/proxy/socks"
    32  	"github.com/v2fly/v2ray-core/v5/proxy/vmess"
    33  	"github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound"
    34  	"github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
    35  	"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
    36  	"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
    37  	"github.com/v2fly/v2ray-core/v5/transport/internet"
    38  )
    39  
    40  func TestPassiveConnection(t *testing.T) {
    41  	tcpServer := tcp.Server{
    42  		MsgProcessor: xor,
    43  		SendFirst:    []byte("send first"),
    44  	}
    45  	dest, err := tcpServer.Start()
    46  	common.Must(err)
    47  	defer tcpServer.Close()
    48  
    49  	serverPort := tcp.PickPort()
    50  	serverConfig := &core.Config{
    51  		Inbound: []*core.InboundHandlerConfig{
    52  			{
    53  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    54  					PortRange: net.SinglePortRange(serverPort),
    55  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    56  				}),
    57  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    58  					Address: net.NewIPOrDomain(dest.Address),
    59  					Port:    uint32(dest.Port),
    60  					NetworkList: &net.NetworkList{
    61  						Network: []net.Network{net.Network_TCP},
    62  					},
    63  				}),
    64  			},
    65  		},
    66  		Outbound: []*core.OutboundHandlerConfig{
    67  			{
    68  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    69  			},
    70  		},
    71  	}
    72  
    73  	servers, err := InitializeServerConfigs(serverConfig)
    74  	common.Must(err)
    75  	defer CloseAllServers(servers)
    76  
    77  	conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
    78  		IP:   []byte{127, 0, 0, 1},
    79  		Port: int(serverPort),
    80  	})
    81  	common.Must(err)
    82  
    83  	{
    84  		response := make([]byte, 1024)
    85  		nBytes, err := conn.Read(response)
    86  		common.Must(err)
    87  		if string(response[:nBytes]) != "send first" {
    88  			t.Error("unexpected first response: ", string(response[:nBytes]))
    89  		}
    90  	}
    91  
    92  	if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
    93  		t.Error(err)
    94  	}
    95  }
    96  
    97  func TestProxy(t *testing.T) {
    98  	tcpServer := tcp.Server{
    99  		MsgProcessor: xor,
   100  	}
   101  	dest, err := tcpServer.Start()
   102  	common.Must(err)
   103  	defer tcpServer.Close()
   104  
   105  	serverUserID := protocol.NewID(uuid.New())
   106  	serverPort := tcp.PickPort()
   107  	serverConfig := &core.Config{
   108  		Inbound: []*core.InboundHandlerConfig{
   109  			{
   110  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   111  					PortRange: net.SinglePortRange(serverPort),
   112  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   113  				}),
   114  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   115  					User: []*protocol.User{
   116  						{
   117  							Account: serial.ToTypedMessage(&vmess.Account{
   118  								Id: serverUserID.String(),
   119  							}),
   120  						},
   121  					},
   122  				}),
   123  			},
   124  		},
   125  		Outbound: []*core.OutboundHandlerConfig{
   126  			{
   127  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   128  			},
   129  		},
   130  	}
   131  
   132  	proxyUserID := protocol.NewID(uuid.New())
   133  	proxyPort := tcp.PickPort()
   134  	proxyConfig := &core.Config{
   135  		Inbound: []*core.InboundHandlerConfig{
   136  			{
   137  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   138  					PortRange: net.SinglePortRange(proxyPort),
   139  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   140  				}),
   141  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   142  					User: []*protocol.User{
   143  						{
   144  							Account: serial.ToTypedMessage(&vmess.Account{
   145  								Id: proxyUserID.String(),
   146  							}),
   147  						},
   148  					},
   149  				}),
   150  			},
   151  		},
   152  		Outbound: []*core.OutboundHandlerConfig{
   153  			{
   154  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   155  			},
   156  		},
   157  	}
   158  
   159  	clientPort := tcp.PickPort()
   160  	clientConfig := &core.Config{
   161  		Inbound: []*core.InboundHandlerConfig{
   162  			{
   163  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   164  					PortRange: net.SinglePortRange(clientPort),
   165  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   166  				}),
   167  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   168  					Address: net.NewIPOrDomain(dest.Address),
   169  					Port:    uint32(dest.Port),
   170  					NetworkList: &net.NetworkList{
   171  						Network: []net.Network{net.Network_TCP},
   172  					},
   173  				}),
   174  			},
   175  		},
   176  		Outbound: []*core.OutboundHandlerConfig{
   177  			{
   178  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   179  					Receiver: []*protocol.ServerEndpoint{
   180  						{
   181  							Address: net.NewIPOrDomain(net.LocalHostIP),
   182  							Port:    uint32(serverPort),
   183  							User: []*protocol.User{
   184  								{
   185  									Account: serial.ToTypedMessage(&vmess.Account{
   186  										Id: serverUserID.String(),
   187  									}),
   188  								},
   189  							},
   190  						},
   191  					},
   192  				}),
   193  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   194  					ProxySettings: &internet.ProxyConfig{
   195  						Tag: "proxy",
   196  					},
   197  				}),
   198  			},
   199  			{
   200  				Tag: "proxy",
   201  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   202  					Receiver: []*protocol.ServerEndpoint{
   203  						{
   204  							Address: net.NewIPOrDomain(net.LocalHostIP),
   205  							Port:    uint32(proxyPort),
   206  							User: []*protocol.User{
   207  								{
   208  									Account: serial.ToTypedMessage(&vmess.Account{
   209  										Id: proxyUserID.String(),
   210  									}),
   211  								},
   212  							},
   213  						},
   214  					},
   215  				}),
   216  			},
   217  		},
   218  	}
   219  
   220  	servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
   221  	common.Must(err)
   222  	defer CloseAllServers(servers)
   223  
   224  	if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
   225  		t.Error(err)
   226  	}
   227  }
   228  
   229  func TestProxyOverKCP(t *testing.T) {
   230  	tcpServer := tcp.Server{
   231  		MsgProcessor: xor,
   232  	}
   233  	dest, err := tcpServer.Start()
   234  	common.Must(err)
   235  	defer tcpServer.Close()
   236  
   237  	serverUserID := protocol.NewID(uuid.New())
   238  	serverPort := tcp.PickPort()
   239  	serverConfig := &core.Config{
   240  		Inbound: []*core.InboundHandlerConfig{
   241  			{
   242  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   243  					PortRange: net.SinglePortRange(serverPort),
   244  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   245  					StreamSettings: &internet.StreamConfig{
   246  						Protocol: internet.TransportProtocol_MKCP,
   247  					},
   248  				}),
   249  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   250  					User: []*protocol.User{
   251  						{
   252  							Account: serial.ToTypedMessage(&vmess.Account{
   253  								Id: serverUserID.String(),
   254  							}),
   255  						},
   256  					},
   257  				}),
   258  			},
   259  		},
   260  		Outbound: []*core.OutboundHandlerConfig{
   261  			{
   262  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   263  			},
   264  		},
   265  	}
   266  
   267  	proxyUserID := protocol.NewID(uuid.New())
   268  	proxyPort := tcp.PickPort()
   269  	proxyConfig := &core.Config{
   270  		Inbound: []*core.InboundHandlerConfig{
   271  			{
   272  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   273  					PortRange: net.SinglePortRange(proxyPort),
   274  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   275  				}),
   276  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   277  					User: []*protocol.User{
   278  						{
   279  							Account: serial.ToTypedMessage(&vmess.Account{
   280  								Id: proxyUserID.String(),
   281  							}),
   282  						},
   283  					},
   284  				}),
   285  			},
   286  		},
   287  		Outbound: []*core.OutboundHandlerConfig{
   288  			{
   289  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   290  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   291  					StreamSettings: &internet.StreamConfig{
   292  						Protocol: internet.TransportProtocol_MKCP,
   293  					},
   294  				}),
   295  			},
   296  		},
   297  	}
   298  
   299  	clientPort := tcp.PickPort()
   300  	clientConfig := &core.Config{
   301  		Inbound: []*core.InboundHandlerConfig{
   302  			{
   303  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   304  					PortRange: net.SinglePortRange(clientPort),
   305  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   306  				}),
   307  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   308  					Address: net.NewIPOrDomain(dest.Address),
   309  					Port:    uint32(dest.Port),
   310  					NetworkList: &net.NetworkList{
   311  						Network: []net.Network{net.Network_TCP},
   312  					},
   313  				}),
   314  			},
   315  		},
   316  		Outbound: []*core.OutboundHandlerConfig{
   317  			{
   318  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   319  					Receiver: []*protocol.ServerEndpoint{
   320  						{
   321  							Address: net.NewIPOrDomain(net.LocalHostIP),
   322  							Port:    uint32(serverPort),
   323  							User: []*protocol.User{
   324  								{
   325  									Account: serial.ToTypedMessage(&vmess.Account{
   326  										Id: serverUserID.String(),
   327  									}),
   328  								},
   329  							},
   330  						},
   331  					},
   332  				}),
   333  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   334  					ProxySettings: &internet.ProxyConfig{
   335  						Tag: "proxy",
   336  					},
   337  					StreamSettings: &internet.StreamConfig{
   338  						Protocol: internet.TransportProtocol_MKCP,
   339  					},
   340  				}),
   341  			},
   342  			{
   343  				Tag: "proxy",
   344  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   345  					Receiver: []*protocol.ServerEndpoint{
   346  						{
   347  							Address: net.NewIPOrDomain(net.LocalHostIP),
   348  							Port:    uint32(proxyPort),
   349  							User: []*protocol.User{
   350  								{
   351  									Account: serial.ToTypedMessage(&vmess.Account{
   352  										Id: proxyUserID.String(),
   353  									}),
   354  								},
   355  							},
   356  						},
   357  					},
   358  				}),
   359  			},
   360  		},
   361  	}
   362  
   363  	servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
   364  	common.Must(err)
   365  	defer CloseAllServers(servers)
   366  
   367  	if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
   368  		t.Error(err)
   369  	}
   370  }
   371  
   372  func TestBlackhole(t *testing.T) {
   373  	tcpServer := tcp.Server{
   374  		MsgProcessor: xor,
   375  	}
   376  	dest, err := tcpServer.Start()
   377  	common.Must(err)
   378  	defer tcpServer.Close()
   379  
   380  	tcpServer2 := tcp.Server{
   381  		MsgProcessor: xor,
   382  	}
   383  	dest2, err := tcpServer2.Start()
   384  	common.Must(err)
   385  	defer tcpServer2.Close()
   386  
   387  	serverPort := tcp.PickPort()
   388  	serverPort2 := tcp.PickPort()
   389  	serverConfig := &core.Config{
   390  		Inbound: []*core.InboundHandlerConfig{
   391  			{
   392  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   393  					PortRange: net.SinglePortRange(serverPort),
   394  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   395  				}),
   396  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   397  					Address: net.NewIPOrDomain(dest.Address),
   398  					Port:    uint32(dest.Port),
   399  					NetworkList: &net.NetworkList{
   400  						Network: []net.Network{net.Network_TCP},
   401  					},
   402  				}),
   403  			},
   404  			{
   405  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   406  					PortRange: net.SinglePortRange(serverPort2),
   407  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   408  				}),
   409  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   410  					Address: net.NewIPOrDomain(dest2.Address),
   411  					Port:    uint32(dest2.Port),
   412  					NetworkList: &net.NetworkList{
   413  						Network: []net.Network{net.Network_TCP},
   414  					},
   415  				}),
   416  			},
   417  		},
   418  		Outbound: []*core.OutboundHandlerConfig{
   419  			{
   420  				Tag:           "direct",
   421  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   422  			},
   423  			{
   424  				Tag:           "blocked",
   425  				ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
   426  			},
   427  		},
   428  		App: []*anypb.Any{
   429  			serial.ToTypedMessage(&router.Config{
   430  				Rule: []*router.RoutingRule{
   431  					{
   432  						TargetTag: &router.RoutingRule_Tag{
   433  							Tag: "blocked",
   434  						},
   435  						PortRange: net.SinglePortRange(dest2.Port),
   436  					},
   437  				},
   438  			}),
   439  		},
   440  	}
   441  
   442  	servers, err := InitializeServerConfigs(serverConfig)
   443  	common.Must(err)
   444  	defer CloseAllServers(servers)
   445  
   446  	if err := testTCPConn(serverPort2, 1024, time.Second*5)(); err == nil {
   447  		t.Error("nil error")
   448  	}
   449  }
   450  
   451  func TestForward(t *testing.T) {
   452  	tcpServer := tcp.Server{
   453  		MsgProcessor: xor,
   454  	}
   455  	dest, err := tcpServer.Start()
   456  	common.Must(err)
   457  	defer tcpServer.Close()
   458  
   459  	serverPort := tcp.PickPort()
   460  	serverConfig := &core.Config{
   461  		Inbound: []*core.InboundHandlerConfig{
   462  			{
   463  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   464  					PortRange: net.SinglePortRange(serverPort),
   465  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   466  				}),
   467  				ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
   468  					AuthType: socks.AuthType_NO_AUTH,
   469  					Accounts: map[string]string{
   470  						"Test Account": "Test Password",
   471  					},
   472  					Address:    net.NewIPOrDomain(net.LocalHostIP),
   473  					UdpEnabled: false,
   474  				}),
   475  			},
   476  		},
   477  		Outbound: []*core.OutboundHandlerConfig{
   478  			{
   479  				ProxySettings: serial.ToTypedMessage(&freedom.Config{
   480  					DestinationOverride: &freedom.DestinationOverride{
   481  						Server: &protocol.ServerEndpoint{
   482  							Address: net.NewIPOrDomain(net.LocalHostIP),
   483  							Port:    uint32(dest.Port),
   484  						},
   485  					},
   486  				}),
   487  			},
   488  		},
   489  	}
   490  
   491  	servers, err := InitializeServerConfigs(serverConfig)
   492  	common.Must(err)
   493  	defer CloseAllServers(servers)
   494  
   495  	{
   496  		noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
   497  		common.Must(err)
   498  		conn, err := noAuthDialer.Dial("tcp", "google.com:80")
   499  		common.Must(err)
   500  		defer conn.Close()
   501  
   502  		if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   503  			t.Error(err)
   504  		}
   505  	}
   506  }
   507  
   508  func TestUDPConnection(t *testing.T) {
   509  	udpServer := udp.Server{
   510  		MsgProcessor: xor,
   511  	}
   512  	dest, err := udpServer.Start()
   513  	common.Must(err)
   514  	defer udpServer.Close()
   515  
   516  	clientPort := udp.PickPort()
   517  	clientConfig := &core.Config{
   518  		Inbound: []*core.InboundHandlerConfig{
   519  			{
   520  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   521  					PortRange: net.SinglePortRange(clientPort),
   522  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   523  				}),
   524  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   525  					Address: net.NewIPOrDomain(dest.Address),
   526  					Port:    uint32(dest.Port),
   527  					NetworkList: &net.NetworkList{
   528  						Network: []net.Network{net.Network_UDP},
   529  					},
   530  				}),
   531  			},
   532  		},
   533  		Outbound: []*core.OutboundHandlerConfig{
   534  			{
   535  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   536  			},
   537  		},
   538  	}
   539  
   540  	servers, err := InitializeServerConfigs(clientConfig)
   541  	common.Must(err)
   542  	defer CloseAllServers(servers)
   543  
   544  	if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
   545  		t.Error(err)
   546  	}
   547  
   548  	time.Sleep(20 * time.Second)
   549  
   550  	if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
   551  		t.Error(err)
   552  	}
   553  }
   554  
   555  func TestDomainSniffing(t *testing.T) {
   556  	sniffingPort := tcp.PickPort()
   557  	httpPort := tcp.PickPort()
   558  	serverConfig := &core.Config{
   559  		Inbound: []*core.InboundHandlerConfig{
   560  			{
   561  				Tag: "snif",
   562  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   563  					PortRange: net.SinglePortRange(sniffingPort),
   564  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   565  					DomainOverride: []proxyman.KnownProtocols{
   566  						proxyman.KnownProtocols_TLS,
   567  					},
   568  				}),
   569  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   570  					Address: net.NewIPOrDomain(net.LocalHostIP),
   571  					Port:    443,
   572  					NetworkList: &net.NetworkList{
   573  						Network: []net.Network{net.Network_TCP},
   574  					},
   575  				}),
   576  			},
   577  			{
   578  				Tag: "http",
   579  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   580  					PortRange: net.SinglePortRange(httpPort),
   581  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   582  				}),
   583  				ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
   584  			},
   585  		},
   586  		Outbound: []*core.OutboundHandlerConfig{
   587  			{
   588  				Tag: "redir",
   589  				ProxySettings: serial.ToTypedMessage(&freedom.Config{
   590  					DestinationOverride: &freedom.DestinationOverride{
   591  						Server: &protocol.ServerEndpoint{
   592  							Address: net.NewIPOrDomain(net.LocalHostIP),
   593  							Port:    uint32(sniffingPort),
   594  						},
   595  					},
   596  				}),
   597  			},
   598  			{
   599  				Tag:           "direct",
   600  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   601  			},
   602  		},
   603  		App: []*anypb.Any{
   604  			serial.ToTypedMessage(&router.Config{
   605  				Rule: []*router.RoutingRule{
   606  					{
   607  						TargetTag: &router.RoutingRule_Tag{
   608  							Tag: "direct",
   609  						},
   610  						InboundTag: []string{"snif"},
   611  					}, {
   612  						TargetTag: &router.RoutingRule_Tag{
   613  							Tag: "redir",
   614  						},
   615  						InboundTag: []string{"http"},
   616  					},
   617  				},
   618  			}),
   619  			serial.ToTypedMessage(&log.Config{
   620  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   621  			}),
   622  		},
   623  	}
   624  
   625  	servers, err := InitializeServerConfigs(serverConfig)
   626  	common.Must(err)
   627  	defer CloseAllServers(servers)
   628  
   629  	{
   630  		transport := &http.Transport{
   631  			Proxy: func(req *http.Request) (*url.URL, error) {
   632  				return url.Parse("http://127.0.0.1:" + httpPort.String())
   633  			},
   634  		}
   635  
   636  		client := &http.Client{
   637  			Transport: transport,
   638  		}
   639  
   640  		resp, err := client.Get("https://www.github.com/")
   641  		common.Must(err)
   642  		defer resp.Body.Close()
   643  		if resp.StatusCode != 200 {
   644  			t.Error("unexpected status code: ", resp.StatusCode)
   645  		}
   646  		common.Must(resp.Write(io.Discard))
   647  	}
   648  }
   649  
   650  func TestDialV2Ray(t *testing.T) {
   651  	tcpServer := tcp.Server{
   652  		MsgProcessor: xor,
   653  	}
   654  	dest, err := tcpServer.Start()
   655  	common.Must(err)
   656  	defer tcpServer.Close()
   657  
   658  	userID := protocol.NewID(uuid.New())
   659  	serverPort := tcp.PickPort()
   660  	serverConfig := &core.Config{
   661  		App: []*anypb.Any{
   662  			serial.ToTypedMessage(&log.Config{
   663  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
   664  			}),
   665  		},
   666  		Inbound: []*core.InboundHandlerConfig{
   667  			{
   668  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   669  					PortRange: net.SinglePortRange(serverPort),
   670  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   671  				}),
   672  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   673  					User: []*protocol.User{
   674  						{
   675  							Account: serial.ToTypedMessage(&vmess.Account{
   676  								Id:      userID.String(),
   677  								AlterId: 0,
   678  							}),
   679  						},
   680  					},
   681  				}),
   682  			},
   683  		},
   684  		Outbound: []*core.OutboundHandlerConfig{
   685  			{
   686  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   687  			},
   688  		},
   689  	}
   690  
   691  	clientConfig := &core.Config{
   692  		App: []*anypb.Any{
   693  			serial.ToTypedMessage(&dispatcher.Config{}),
   694  			serial.ToTypedMessage(&proxyman.InboundConfig{}),
   695  			serial.ToTypedMessage(&proxyman.OutboundConfig{}),
   696  		},
   697  		Inbound: []*core.InboundHandlerConfig{},
   698  		Outbound: []*core.OutboundHandlerConfig{
   699  			{
   700  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   701  					Receiver: []*protocol.ServerEndpoint{
   702  						{
   703  							Address: net.NewIPOrDomain(net.LocalHostIP),
   704  							Port:    uint32(serverPort),
   705  							User: []*protocol.User{
   706  								{
   707  									Account: serial.ToTypedMessage(&vmess.Account{
   708  										Id:      userID.String(),
   709  										AlterId: 0,
   710  										SecuritySettings: &protocol.SecurityConfig{
   711  											Type: protocol.SecurityType_AES128_GCM,
   712  										},
   713  									}),
   714  								},
   715  							},
   716  						},
   717  					},
   718  				}),
   719  			},
   720  		},
   721  	}
   722  
   723  	servers, err := InitializeServerConfigs(serverConfig)
   724  	common.Must(err)
   725  	defer CloseAllServers(servers)
   726  
   727  	client, err := core.New(clientConfig)
   728  	common.Must(err)
   729  
   730  	conn, err := core.Dial(context.Background(), client, dest)
   731  	common.Must(err)
   732  	defer conn.Close()
   733  
   734  	if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
   735  		t.Error(err)
   736  	}
   737  }