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