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

     1  package scenarios
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"golang.org/x/sync/errgroup"
     8  	"google.golang.org/protobuf/types/known/anypb"
     9  
    10  	core "github.com/v2fly/v2ray-core/v5"
    11  	"github.com/v2fly/v2ray-core/v5/app/log"
    12  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    13  	"github.com/v2fly/v2ray-core/v5/common"
    14  	clog "github.com/v2fly/v2ray-core/v5/common/log"
    15  	"github.com/v2fly/v2ray-core/v5/common/net"
    16  	"github.com/v2fly/v2ray-core/v5/common/protocol"
    17  	"github.com/v2fly/v2ray-core/v5/common/serial"
    18  	"github.com/v2fly/v2ray-core/v5/common/uuid"
    19  	"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
    20  	"github.com/v2fly/v2ray-core/v5/proxy/freedom"
    21  	"github.com/v2fly/v2ray-core/v5/proxy/vmess"
    22  	"github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound"
    23  	"github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
    24  	"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
    25  	"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
    26  )
    27  
    28  func TestDokodemoTCP(t *testing.T) {
    29  	tcpServer := tcp.Server{
    30  		MsgProcessor: xor,
    31  	}
    32  	dest, err := tcpServer.Start()
    33  	common.Must(err)
    34  	defer tcpServer.Close()
    35  
    36  	userID := protocol.NewID(uuid.New())
    37  	serverPort := tcp.PickPort()
    38  	serverConfig := &core.Config{
    39  		App: []*anypb.Any{
    40  			serial.ToTypedMessage(&log.Config{
    41  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
    42  			}),
    43  		},
    44  		Inbound: []*core.InboundHandlerConfig{
    45  			{
    46  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    47  					PortRange: net.SinglePortRange(serverPort),
    48  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    49  				}),
    50  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
    51  					User: []*protocol.User{
    52  						{
    53  							Account: serial.ToTypedMessage(&vmess.Account{
    54  								Id: userID.String(),
    55  							}),
    56  						},
    57  					},
    58  				}),
    59  			},
    60  		},
    61  		Outbound: []*core.OutboundHandlerConfig{
    62  			{
    63  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    64  			},
    65  		},
    66  	}
    67  
    68  	clientPort := uint32(tcp.PickPort())
    69  	clientPortRange := uint32(5)
    70  	clientConfig := &core.Config{
    71  		App: []*anypb.Any{
    72  			serial.ToTypedMessage(&log.Config{
    73  				Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
    74  			}),
    75  		},
    76  		Inbound: []*core.InboundHandlerConfig{
    77  			{
    78  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    79  					PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
    80  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    81  				}),
    82  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    83  					Address: net.NewIPOrDomain(dest.Address),
    84  					Port:    uint32(dest.Port),
    85  					NetworkList: &net.NetworkList{
    86  						Network: []net.Network{net.Network_TCP},
    87  					},
    88  				}),
    89  			},
    90  		},
    91  		Outbound: []*core.OutboundHandlerConfig{
    92  			{
    93  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
    94  					Receiver: []*protocol.ServerEndpoint{
    95  						{
    96  							Address: net.NewIPOrDomain(net.LocalHostIP),
    97  							Port:    uint32(serverPort),
    98  							User: []*protocol.User{
    99  								{
   100  									Account: serial.ToTypedMessage(&vmess.Account{
   101  										Id: userID.String(),
   102  									}),
   103  								},
   104  							},
   105  						},
   106  					},
   107  				}),
   108  			},
   109  		},
   110  	}
   111  
   112  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   113  	common.Must(err)
   114  	defer CloseAllServers(servers)
   115  
   116  	for port := clientPort; port <= clientPort+clientPortRange; port++ {
   117  		if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
   118  			t.Error(err)
   119  		}
   120  	}
   121  }
   122  
   123  func TestDokodemoUDP(t *testing.T) {
   124  	udpServer := udp.Server{
   125  		MsgProcessor: xor,
   126  	}
   127  	dest, err := udpServer.Start()
   128  	common.Must(err)
   129  	defer udpServer.Close()
   130  
   131  	userID := protocol.NewID(uuid.New())
   132  	serverPort := tcp.PickPort()
   133  	serverConfig := &core.Config{
   134  		Inbound: []*core.InboundHandlerConfig{
   135  			{
   136  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   137  					PortRange: net.SinglePortRange(serverPort),
   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: userID.String(),
   145  							}),
   146  						},
   147  					},
   148  				}),
   149  			},
   150  		},
   151  		Outbound: []*core.OutboundHandlerConfig{
   152  			{
   153  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   154  			},
   155  		},
   156  	}
   157  
   158  	clientPort := uint32(udp.PickPort())
   159  	clientPortRange := uint32(5)
   160  	clientConfig := &core.Config{
   161  		Inbound: []*core.InboundHandlerConfig{
   162  			{
   163  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   164  					PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
   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_UDP},
   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: userID.String(),
   187  									}),
   188  								},
   189  							},
   190  						},
   191  					},
   192  				}),
   193  			},
   194  		},
   195  	}
   196  
   197  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   198  	common.Must(err)
   199  	defer CloseAllServers(servers)
   200  
   201  	var errg errgroup.Group
   202  	for port := clientPort; port <= clientPort+clientPortRange; port++ {
   203  		errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5))
   204  	}
   205  	if err := errg.Wait(); err != nil {
   206  		t.Error(err)
   207  	}
   208  }