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

     1  package scenarios
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"testing"
     7  	"time"
     8  
     9  	"golang.org/x/sync/errgroup"
    10  
    11  	core "github.com/v2fly/v2ray-core/v4"
    12  	"github.com/v2fly/v2ray-core/v4/app/log"
    13  	"github.com/v2fly/v2ray-core/v4/app/proxyman"
    14  	"github.com/v2fly/v2ray-core/v4/common"
    15  	clog "github.com/v2fly/v2ray-core/v4/common/log"
    16  	"github.com/v2fly/v2ray-core/v4/common/net"
    17  	"github.com/v2fly/v2ray-core/v4/common/protocol"
    18  	"github.com/v2fly/v2ray-core/v4/common/serial"
    19  	"github.com/v2fly/v2ray-core/v4/common/uuid"
    20  	"github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
    21  	"github.com/v2fly/v2ray-core/v4/proxy/freedom"
    22  	"github.com/v2fly/v2ray-core/v4/proxy/vmess"
    23  	"github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
    24  	"github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
    25  	"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
    26  	"github.com/v2fly/v2ray-core/v4/testing/servers/udp"
    27  	"github.com/v2fly/v2ray-core/v4/transport/internet"
    28  	"github.com/v2fly/v2ray-core/v4/transport/internet/domainsocket"
    29  	"github.com/v2fly/v2ray-core/v4/transport/internet/headers/http"
    30  	"github.com/v2fly/v2ray-core/v4/transport/internet/headers/wechat"
    31  	"github.com/v2fly/v2ray-core/v4/transport/internet/quic"
    32  	tcptransport "github.com/v2fly/v2ray-core/v4/transport/internet/tcp"
    33  )
    34  
    35  func TestHTTPConnectionHeader(t *testing.T) {
    36  	tcpServer := tcp.Server{
    37  		MsgProcessor: xor,
    38  	}
    39  	dest, err := tcpServer.Start()
    40  	common.Must(err)
    41  	defer tcpServer.Close()
    42  
    43  	userID := protocol.NewID(uuid.New())
    44  	serverPort := tcp.PickPort()
    45  	serverConfig := &core.Config{
    46  		Inbound: []*core.InboundHandlerConfig{
    47  			{
    48  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    49  					PortRange: net.SinglePortRange(serverPort),
    50  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    51  					StreamSettings: &internet.StreamConfig{
    52  						TransportSettings: []*internet.TransportConfig{
    53  							{
    54  								Protocol: internet.TransportProtocol_TCP,
    55  								Settings: serial.ToTypedMessage(&tcptransport.Config{
    56  									HeaderSettings: serial.ToTypedMessage(&http.Config{}),
    57  								}),
    58  							},
    59  						},
    60  					},
    61  				}),
    62  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
    63  					User: []*protocol.User{
    64  						{
    65  							Account: serial.ToTypedMessage(&vmess.Account{
    66  								Id: userID.String(),
    67  							}),
    68  						},
    69  					},
    70  				}),
    71  			},
    72  		},
    73  		Outbound: []*core.OutboundHandlerConfig{
    74  			{
    75  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    76  			},
    77  		},
    78  	}
    79  
    80  	clientPort := tcp.PickPort()
    81  	clientConfig := &core.Config{
    82  		Inbound: []*core.InboundHandlerConfig{
    83  			{
    84  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    85  					PortRange: net.SinglePortRange(clientPort),
    86  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    87  				}),
    88  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    89  					Address: net.NewIPOrDomain(dest.Address),
    90  					Port:    uint32(dest.Port),
    91  					NetworkList: &net.NetworkList{
    92  						Network: []net.Network{net.Network_TCP},
    93  					},
    94  				}),
    95  			},
    96  		},
    97  		Outbound: []*core.OutboundHandlerConfig{
    98  			{
    99  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   100  					Receiver: []*protocol.ServerEndpoint{
   101  						{
   102  							Address: net.NewIPOrDomain(net.LocalHostIP),
   103  							Port:    uint32(serverPort),
   104  							User: []*protocol.User{
   105  								{
   106  									Account: serial.ToTypedMessage(&vmess.Account{
   107  										Id: userID.String(),
   108  									}),
   109  								},
   110  							},
   111  						},
   112  					},
   113  				}),
   114  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   115  					StreamSettings: &internet.StreamConfig{
   116  						TransportSettings: []*internet.TransportConfig{
   117  							{
   118  								Protocol: internet.TransportProtocol_TCP,
   119  								Settings: serial.ToTypedMessage(&tcptransport.Config{
   120  									HeaderSettings: serial.ToTypedMessage(&http.Config{}),
   121  								}),
   122  							},
   123  						},
   124  					},
   125  				}),
   126  			},
   127  		},
   128  	}
   129  
   130  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   131  	common.Must(err)
   132  	defer CloseAllServers(servers)
   133  
   134  	if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
   135  		t.Error(err)
   136  	}
   137  }
   138  
   139  func TestDomainSocket(t *testing.T) {
   140  	if runtime.GOOS == "windows" || runtime.GOOS == "android" {
   141  		t.Skip("Not supported on windows and android")
   142  		return
   143  	}
   144  	tcpServer := tcp.Server{
   145  		MsgProcessor: xor,
   146  	}
   147  	dest, err := tcpServer.Start()
   148  	common.Must(err)
   149  	defer tcpServer.Close()
   150  
   151  	const dsPath = "/tmp/ds_scenario"
   152  	os.Remove(dsPath)
   153  
   154  	userID := protocol.NewID(uuid.New())
   155  	serverPort := tcp.PickPort()
   156  	serverConfig := &core.Config{
   157  		Inbound: []*core.InboundHandlerConfig{
   158  			{
   159  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   160  					PortRange: net.SinglePortRange(serverPort),
   161  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   162  					StreamSettings: &internet.StreamConfig{
   163  						Protocol: internet.TransportProtocol_DomainSocket,
   164  						TransportSettings: []*internet.TransportConfig{
   165  							{
   166  								Protocol: internet.TransportProtocol_DomainSocket,
   167  								Settings: serial.ToTypedMessage(&domainsocket.Config{
   168  									Path: dsPath,
   169  								}),
   170  							},
   171  						},
   172  					},
   173  				}),
   174  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   175  					User: []*protocol.User{
   176  						{
   177  							Account: serial.ToTypedMessage(&vmess.Account{
   178  								Id: userID.String(),
   179  							}),
   180  						},
   181  					},
   182  				}),
   183  			},
   184  		},
   185  		Outbound: []*core.OutboundHandlerConfig{
   186  			{
   187  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   188  			},
   189  		},
   190  	}
   191  
   192  	clientPort := tcp.PickPort()
   193  	clientConfig := &core.Config{
   194  		Inbound: []*core.InboundHandlerConfig{
   195  			{
   196  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   197  					PortRange: net.SinglePortRange(clientPort),
   198  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   199  				}),
   200  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   201  					Address: net.NewIPOrDomain(dest.Address),
   202  					Port:    uint32(dest.Port),
   203  					NetworkList: &net.NetworkList{
   204  						Network: []net.Network{net.Network_TCP},
   205  					},
   206  				}),
   207  			},
   208  		},
   209  		Outbound: []*core.OutboundHandlerConfig{
   210  			{
   211  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   212  					Receiver: []*protocol.ServerEndpoint{
   213  						{
   214  							Address: net.NewIPOrDomain(net.LocalHostIP),
   215  							Port:    uint32(serverPort),
   216  							User: []*protocol.User{
   217  								{
   218  									Account: serial.ToTypedMessage(&vmess.Account{
   219  										Id: userID.String(),
   220  									}),
   221  								},
   222  							},
   223  						},
   224  					},
   225  				}),
   226  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   227  					StreamSettings: &internet.StreamConfig{
   228  						Protocol: internet.TransportProtocol_DomainSocket,
   229  						TransportSettings: []*internet.TransportConfig{
   230  							{
   231  								Protocol: internet.TransportProtocol_DomainSocket,
   232  								Settings: serial.ToTypedMessage(&domainsocket.Config{
   233  									Path: dsPath,
   234  								}),
   235  							},
   236  						},
   237  					},
   238  				}),
   239  			},
   240  		},
   241  	}
   242  
   243  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   244  	common.Must(err)
   245  	defer CloseAllServers(servers)
   246  
   247  	if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
   248  		t.Error(err)
   249  	}
   250  }
   251  
   252  func TestVMessQuic(t *testing.T) {
   253  	tcpServer := tcp.Server{
   254  		MsgProcessor: xor,
   255  	}
   256  	dest, err := tcpServer.Start()
   257  	common.Must(err)
   258  	defer tcpServer.Close()
   259  
   260  	userID := protocol.NewID(uuid.New())
   261  	serverPort := udp.PickPort()
   262  	serverConfig := &core.Config{
   263  		App: []*serial.TypedMessage{
   264  			serial.ToTypedMessage(&log.Config{
   265  				ErrorLogLevel: clog.Severity_Debug,
   266  				ErrorLogType:  log.LogType_Console,
   267  			}),
   268  		},
   269  		Inbound: []*core.InboundHandlerConfig{
   270  			{
   271  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   272  					PortRange: net.SinglePortRange(serverPort),
   273  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   274  					StreamSettings: &internet.StreamConfig{
   275  						ProtocolName: "quic",
   276  						TransportSettings: []*internet.TransportConfig{
   277  							{
   278  								ProtocolName: "quic",
   279  								Settings: serial.ToTypedMessage(&quic.Config{
   280  									Header: serial.ToTypedMessage(&wechat.VideoConfig{}),
   281  									Security: &protocol.SecurityConfig{
   282  										Type: protocol.SecurityType_NONE,
   283  									},
   284  								}),
   285  							},
   286  						},
   287  					},
   288  				}),
   289  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   290  					User: []*protocol.User{
   291  						{
   292  							Account: serial.ToTypedMessage(&vmess.Account{
   293  								Id:      userID.String(),
   294  								AlterId: 0,
   295  							}),
   296  						},
   297  					},
   298  				}),
   299  			},
   300  		},
   301  		Outbound: []*core.OutboundHandlerConfig{
   302  			{
   303  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   304  			},
   305  		},
   306  	}
   307  
   308  	clientPort := tcp.PickPort()
   309  	clientConfig := &core.Config{
   310  		App: []*serial.TypedMessage{
   311  			serial.ToTypedMessage(&log.Config{
   312  				ErrorLogLevel: clog.Severity_Debug,
   313  				ErrorLogType:  log.LogType_Console,
   314  			}),
   315  		},
   316  		Inbound: []*core.InboundHandlerConfig{
   317  			{
   318  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   319  					PortRange: net.SinglePortRange(clientPort),
   320  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
   321  				}),
   322  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   323  					Address: net.NewIPOrDomain(dest.Address),
   324  					Port:    uint32(dest.Port),
   325  					NetworkList: &net.NetworkList{
   326  						Network: []net.Network{net.Network_TCP},
   327  					},
   328  				}),
   329  			},
   330  		},
   331  		Outbound: []*core.OutboundHandlerConfig{
   332  			{
   333  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   334  					StreamSettings: &internet.StreamConfig{
   335  						ProtocolName: "quic",
   336  						TransportSettings: []*internet.TransportConfig{
   337  							{
   338  								ProtocolName: "quic",
   339  								Settings: serial.ToTypedMessage(&quic.Config{
   340  									Header: serial.ToTypedMessage(&wechat.VideoConfig{}),
   341  									Security: &protocol.SecurityConfig{
   342  										Type: protocol.SecurityType_NONE,
   343  									},
   344  								}),
   345  							},
   346  						},
   347  					},
   348  				}),
   349  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   350  					Receiver: []*protocol.ServerEndpoint{
   351  						{
   352  							Address: net.NewIPOrDomain(net.LocalHostIP),
   353  							Port:    uint32(serverPort),
   354  							User: []*protocol.User{
   355  								{
   356  									Account: serial.ToTypedMessage(&vmess.Account{
   357  										Id:      userID.String(),
   358  										AlterId: 0,
   359  										SecuritySettings: &protocol.SecurityConfig{
   360  											Type: protocol.SecurityType_AES128_GCM,
   361  										},
   362  									}),
   363  								},
   364  							},
   365  						},
   366  					},
   367  				}),
   368  			},
   369  		},
   370  	}
   371  
   372  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   373  	if err != nil {
   374  		t.Fatal("Failed to initialize all servers: ", err.Error())
   375  	}
   376  	defer CloseAllServers(servers)
   377  
   378  	var errg errgroup.Group
   379  	for i := 0; i < 10; i++ {
   380  		errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
   381  	}
   382  
   383  	if err := errg.Wait(); err != nil {
   384  		t.Error(err)
   385  	}
   386  }