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