github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/testing/scenarios/vless_test.go (about)

     1  package scenarios
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/hex"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/xmplusdev/xmcore/app/log"
    10  	"github.com/xmplusdev/xmcore/app/proxyman"
    11  	"github.com/xmplusdev/xmcore/common"
    12  	clog "github.com/xmplusdev/xmcore/common/log"
    13  	"github.com/xmplusdev/xmcore/common/net"
    14  	"github.com/xmplusdev/xmcore/common/protocol"
    15  	"github.com/xmplusdev/xmcore/common/protocol/tls/cert"
    16  	"github.com/xmplusdev/xmcore/common/serial"
    17  	"github.com/xmplusdev/xmcore/common/uuid"
    18  	core "github.com/xmplusdev/xmcore/core"
    19  	"github.com/xmplusdev/xmcore/proxy/dokodemo"
    20  	"github.com/xmplusdev/xmcore/proxy/freedom"
    21  	"github.com/xmplusdev/xmcore/proxy/vless"
    22  	"github.com/xmplusdev/xmcore/proxy/vless/inbound"
    23  	"github.com/xmplusdev/xmcore/proxy/vless/outbound"
    24  	"github.com/xmplusdev/xmcore/testing/servers/tcp"
    25  	"github.com/xmplusdev/xmcore/transport/internet"
    26  	"github.com/xmplusdev/xmcore/transport/internet/reality"
    27  	transtcp "github.com/xmplusdev/xmcore/transport/internet/tcp"
    28  	"github.com/xmplusdev/xmcore/transport/internet/tls"
    29  	"golang.org/x/sync/errgroup"
    30  )
    31  
    32  func TestVless(t *testing.T) {
    33  	tcpServer := tcp.Server{
    34  		MsgProcessor: xor,
    35  	}
    36  	dest, err := tcpServer.Start()
    37  	common.Must(err)
    38  	defer tcpServer.Close()
    39  
    40  	userID := protocol.NewID(uuid.New())
    41  	serverPort := tcp.PickPort()
    42  	serverConfig := &core.Config{
    43  		App: []*serial.TypedMessage{
    44  			serial.ToTypedMessage(&log.Config{
    45  				ErrorLogLevel: clog.Severity_Debug,
    46  				ErrorLogType:  log.LogType_Console,
    47  			}),
    48  		},
    49  		Inbound: []*core.InboundHandlerConfig{
    50  			{
    51  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    52  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
    53  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
    54  				}),
    55  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
    56  					Clients: []*protocol.User{
    57  						{
    58  							Account: serial.ToTypedMessage(&vless.Account{
    59  								Id: userID.String(),
    60  							}),
    61  						},
    62  					},
    63  				}),
    64  			},
    65  		},
    66  		Outbound: []*core.OutboundHandlerConfig{
    67  			{
    68  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
    69  			},
    70  		},
    71  	}
    72  
    73  	clientPort := tcp.PickPort()
    74  	clientConfig := &core.Config{
    75  		App: []*serial.TypedMessage{
    76  			serial.ToTypedMessage(&log.Config{
    77  				ErrorLogLevel: clog.Severity_Debug,
    78  				ErrorLogType:  log.LogType_Console,
    79  			}),
    80  		},
    81  		Inbound: []*core.InboundHandlerConfig{
    82  			{
    83  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    84  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
    85  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
    86  				}),
    87  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    88  					Address: net.NewIPOrDomain(dest.Address),
    89  					Port:    uint32(dest.Port),
    90  					NetworkList: &net.NetworkList{
    91  						Network: []net.Network{net.Network_TCP},
    92  					},
    93  				}),
    94  			},
    95  		},
    96  		Outbound: []*core.OutboundHandlerConfig{
    97  			{
    98  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
    99  					Vnext: []*protocol.ServerEndpoint{
   100  						{
   101  							Address: net.NewIPOrDomain(net.LocalHostIP),
   102  							Port:    uint32(serverPort),
   103  							User: []*protocol.User{
   104  								{
   105  									Account: serial.ToTypedMessage(&vless.Account{
   106  										Id: userID.String(),
   107  									}),
   108  								},
   109  							},
   110  						},
   111  					},
   112  				}),
   113  			},
   114  		},
   115  	}
   116  
   117  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   118  	common.Must(err)
   119  	defer CloseAllServers(servers)
   120  
   121  	var errg errgroup.Group
   122  	for i := 0; i < 10; i++ {
   123  		errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30))
   124  	}
   125  	if err := errg.Wait(); err != nil {
   126  		t.Error(err)
   127  	}
   128  }
   129  
   130  func TestVlessTls(t *testing.T) {
   131  	tcpServer := tcp.Server{
   132  		MsgProcessor: xor,
   133  	}
   134  	dest, err := tcpServer.Start()
   135  	common.Must(err)
   136  	defer tcpServer.Close()
   137  
   138  	userID := protocol.NewID(uuid.New())
   139  	serverPort := tcp.PickPort()
   140  	serverConfig := &core.Config{
   141  		App: []*serial.TypedMessage{
   142  			serial.ToTypedMessage(&log.Config{
   143  				ErrorLogLevel: clog.Severity_Debug,
   144  				ErrorLogType:  log.LogType_Console,
   145  			}),
   146  		},
   147  		Inbound: []*core.InboundHandlerConfig{
   148  			{
   149  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   150  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
   151  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   152  					StreamSettings: &internet.StreamConfig{
   153  						Protocol:     internet.TransportProtocol_TCP,
   154  						SecurityType: serial.GetMessageType(&tls.Config{}),
   155  						SecuritySettings: []*serial.TypedMessage{
   156  							serial.ToTypedMessage(&tls.Config{
   157  								Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil))},
   158  							}),
   159  						},
   160  					},
   161  				}),
   162  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   163  					Clients: []*protocol.User{
   164  						{
   165  							Account: serial.ToTypedMessage(&vless.Account{
   166  								Id: userID.String(),
   167  							}),
   168  						},
   169  					},
   170  				}),
   171  			},
   172  		},
   173  		Outbound: []*core.OutboundHandlerConfig{
   174  			{
   175  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   176  			},
   177  		},
   178  	}
   179  
   180  	clientPort := tcp.PickPort()
   181  	clientConfig := &core.Config{
   182  		App: []*serial.TypedMessage{
   183  			serial.ToTypedMessage(&log.Config{
   184  				ErrorLogLevel: clog.Severity_Debug,
   185  				ErrorLogType:  log.LogType_Console,
   186  			}),
   187  		},
   188  		Inbound: []*core.InboundHandlerConfig{
   189  			{
   190  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   191  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
   192  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   193  				}),
   194  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   195  					Address: net.NewIPOrDomain(dest.Address),
   196  					Port:    uint32(dest.Port),
   197  					NetworkList: &net.NetworkList{
   198  						Network: []net.Network{net.Network_TCP},
   199  					},
   200  				}),
   201  			},
   202  		},
   203  		Outbound: []*core.OutboundHandlerConfig{
   204  			{
   205  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   206  					Vnext: []*protocol.ServerEndpoint{
   207  						{
   208  							Address: net.NewIPOrDomain(net.LocalHostIP),
   209  							Port:    uint32(serverPort),
   210  							User: []*protocol.User{
   211  								{
   212  									Account: serial.ToTypedMessage(&vless.Account{
   213  										Id: userID.String(),
   214  									}),
   215  								},
   216  							},
   217  						},
   218  					},
   219  				}),
   220  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   221  					StreamSettings: &internet.StreamConfig{
   222  						Protocol: internet.TransportProtocol_TCP,
   223  						TransportSettings: []*internet.TransportConfig{
   224  							{
   225  								Protocol: internet.TransportProtocol_TCP,
   226  								Settings: serial.ToTypedMessage(&transtcp.Config{}),
   227  							},
   228  						},
   229  						SecurityType: serial.GetMessageType(&tls.Config{}),
   230  						SecuritySettings: []*serial.TypedMessage{
   231  							serial.ToTypedMessage(&tls.Config{
   232  								AllowInsecure: true,
   233  							}),
   234  						},
   235  					},
   236  				}),
   237  			},
   238  		},
   239  	}
   240  
   241  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   242  	common.Must(err)
   243  	defer CloseAllServers(servers)
   244  
   245  	var errg errgroup.Group
   246  	for i := 0; i < 10; i++ {
   247  		errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30))
   248  	}
   249  	if err := errg.Wait(); err != nil {
   250  		t.Error(err)
   251  	}
   252  }
   253  
   254  func TestVlessXtlsVision(t *testing.T) {
   255  	tcpServer := tcp.Server{
   256  		MsgProcessor: xor,
   257  	}
   258  	dest, err := tcpServer.Start()
   259  	common.Must(err)
   260  	defer tcpServer.Close()
   261  
   262  	userID := protocol.NewID(uuid.New())
   263  	serverPort := tcp.PickPort()
   264  	serverConfig := &core.Config{
   265  		App: []*serial.TypedMessage{
   266  			serial.ToTypedMessage(&log.Config{
   267  				ErrorLogLevel: clog.Severity_Debug,
   268  				ErrorLogType:  log.LogType_Console,
   269  			}),
   270  		},
   271  		Inbound: []*core.InboundHandlerConfig{
   272  			{
   273  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   274  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
   275  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   276  					StreamSettings: &internet.StreamConfig{
   277  						Protocol:     internet.TransportProtocol_TCP,
   278  						SecurityType: serial.GetMessageType(&tls.Config{}),
   279  						SecuritySettings: []*serial.TypedMessage{
   280  							serial.ToTypedMessage(&tls.Config{
   281  								Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil))},
   282  							}),
   283  						},
   284  					},
   285  				}),
   286  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   287  					Clients: []*protocol.User{
   288  						{
   289  							Account: serial.ToTypedMessage(&vless.Account{
   290  								Id:   userID.String(),
   291  								Flow: vless.XRV,
   292  							}),
   293  						},
   294  					},
   295  				}),
   296  			},
   297  		},
   298  		Outbound: []*core.OutboundHandlerConfig{
   299  			{
   300  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   301  			},
   302  		},
   303  	}
   304  
   305  	clientPort := tcp.PickPort()
   306  	clientConfig := &core.Config{
   307  		App: []*serial.TypedMessage{
   308  			serial.ToTypedMessage(&log.Config{
   309  				ErrorLogLevel: clog.Severity_Debug,
   310  				ErrorLogType:  log.LogType_Console,
   311  			}),
   312  		},
   313  		Inbound: []*core.InboundHandlerConfig{
   314  			{
   315  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   316  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
   317  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   318  				}),
   319  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   320  					Address: net.NewIPOrDomain(dest.Address),
   321  					Port:    uint32(dest.Port),
   322  					NetworkList: &net.NetworkList{
   323  						Network: []net.Network{net.Network_TCP},
   324  					},
   325  				}),
   326  			},
   327  		},
   328  		Outbound: []*core.OutboundHandlerConfig{
   329  			{
   330  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   331  					Vnext: []*protocol.ServerEndpoint{
   332  						{
   333  							Address: net.NewIPOrDomain(net.LocalHostIP),
   334  							Port:    uint32(serverPort),
   335  							User: []*protocol.User{
   336  								{
   337  									Account: serial.ToTypedMessage(&vless.Account{
   338  										Id:   userID.String(),
   339  										Flow: vless.XRV,
   340  									}),
   341  								},
   342  							},
   343  						},
   344  					},
   345  				}),
   346  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   347  					StreamSettings: &internet.StreamConfig{
   348  						Protocol: internet.TransportProtocol_TCP,
   349  						TransportSettings: []*internet.TransportConfig{
   350  							{
   351  								Protocol: internet.TransportProtocol_TCP,
   352  								Settings: serial.ToTypedMessage(&transtcp.Config{}),
   353  							},
   354  						},
   355  						SecurityType: serial.GetMessageType(&tls.Config{}),
   356  						SecuritySettings: []*serial.TypedMessage{
   357  							serial.ToTypedMessage(&tls.Config{
   358  								AllowInsecure: true,
   359  							}),
   360  						},
   361  					},
   362  				}),
   363  			},
   364  		},
   365  	}
   366  
   367  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   368  	common.Must(err)
   369  	defer CloseAllServers(servers)
   370  
   371  	var errg errgroup.Group
   372  	for i := 0; i < 10; i++ {
   373  		errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30))
   374  	}
   375  	if err := errg.Wait(); err != nil {
   376  		t.Error(err)
   377  	}
   378  }
   379  
   380  func TestVlessXtlsVisionReality(t *testing.T) {
   381  	tcpServer := tcp.Server{
   382  		MsgProcessor: xor,
   383  	}
   384  	dest, err := tcpServer.Start()
   385  	common.Must(err)
   386  	defer tcpServer.Close()
   387  
   388  	userID := protocol.NewID(uuid.New())
   389  	serverPort := tcp.PickPort()
   390  	privateKey, _ := base64.RawURLEncoding.DecodeString("aGSYystUbf59_9_6LKRxD27rmSW_-2_nyd9YG_Gwbks")
   391  	publicKey, _ := base64.RawURLEncoding.DecodeString("E59WjnvZcQMu7tR7_BgyhycuEdBS-CtKxfImRCdAvFM")
   392  	shortIds := make([][]byte, 1)
   393  	shortIds[0] = make([]byte, 8)
   394  	hex.Decode(shortIds[0], []byte("0123456789abcdef"))
   395  	serverConfig := &core.Config{
   396  		App: []*serial.TypedMessage{
   397  			serial.ToTypedMessage(&log.Config{
   398  				ErrorLogLevel: clog.Severity_Debug,
   399  				ErrorLogType:  log.LogType_Console,
   400  			}),
   401  		},
   402  		Inbound: []*core.InboundHandlerConfig{
   403  			{
   404  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   405  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
   406  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   407  					StreamSettings: &internet.StreamConfig{
   408  						Protocol:     internet.TransportProtocol_TCP,
   409  						SecurityType: serial.GetMessageType(&reality.Config{}),
   410  						SecuritySettings: []*serial.TypedMessage{
   411  							serial.ToTypedMessage(&reality.Config{
   412  								Show: true,
   413  								Dest: "www.google.com:443", // use google for now, may fail in some region
   414  								ServerNames: []string{"www.google.com"},
   415  								PrivateKey: privateKey,
   416  								ShortIds: shortIds,
   417  								Type: "tcp",
   418  							}),
   419  						},
   420  					},
   421  				}),
   422  				ProxySettings: serial.ToTypedMessage(&inbound.Config{
   423  					Clients: []*protocol.User{
   424  						{
   425  							Account: serial.ToTypedMessage(&vless.Account{
   426  								Id:   userID.String(),
   427  								Flow: vless.XRV,
   428  							}),
   429  						},
   430  					},
   431  				}),
   432  			},
   433  		},
   434  		Outbound: []*core.OutboundHandlerConfig{
   435  			{
   436  				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
   437  			},
   438  		},
   439  	}
   440  
   441  	clientPort := tcp.PickPort()
   442  	clientConfig := &core.Config{
   443  		App: []*serial.TypedMessage{
   444  			serial.ToTypedMessage(&log.Config{
   445  				ErrorLogLevel: clog.Severity_Debug,
   446  				ErrorLogType:  log.LogType_Console,
   447  			}),
   448  		},
   449  		Inbound: []*core.InboundHandlerConfig{
   450  			{
   451  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
   452  					PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
   453  					Listen:   net.NewIPOrDomain(net.LocalHostIP),
   454  				}),
   455  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
   456  					Address: net.NewIPOrDomain(dest.Address),
   457  					Port:    uint32(dest.Port),
   458  					NetworkList: &net.NetworkList{
   459  						Network: []net.Network{net.Network_TCP},
   460  					},
   461  				}),
   462  			},
   463  		},
   464  		Outbound: []*core.OutboundHandlerConfig{
   465  			{
   466  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
   467  					Vnext: []*protocol.ServerEndpoint{
   468  						{
   469  							Address: net.NewIPOrDomain(net.LocalHostIP),
   470  							Port:    uint32(serverPort),
   471  							User: []*protocol.User{
   472  								{
   473  									Account: serial.ToTypedMessage(&vless.Account{
   474  										Id:   userID.String(),
   475  										Flow: vless.XRV,
   476  									}),
   477  								},
   478  							},
   479  						},
   480  					},
   481  				}),
   482  				SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
   483  					StreamSettings: &internet.StreamConfig{
   484  						Protocol: internet.TransportProtocol_TCP,
   485  						TransportSettings: []*internet.TransportConfig{
   486  							{
   487  								Protocol: internet.TransportProtocol_TCP,
   488  								Settings: serial.ToTypedMessage(&transtcp.Config{}),
   489  							},
   490  						},
   491  						SecurityType: serial.GetMessageType(&reality.Config{}),
   492  						SecuritySettings: []*serial.TypedMessage{
   493  							serial.ToTypedMessage(&reality.Config{
   494  								Show: true,
   495  								Fingerprint: "chrome",
   496  								ServerName: "www.google.com",
   497  								PublicKey: publicKey,
   498  								ShortId: shortIds[0],
   499  								SpiderX: "/",
   500  							}),
   501  						},
   502  					},
   503  				}),
   504  			},
   505  		},
   506  	}
   507  
   508  	servers, err := InitializeServerConfigs(serverConfig, clientConfig)
   509  	common.Must(err)
   510  	defer CloseAllServers(servers)
   511  
   512  	var errg errgroup.Group
   513  	for i := 0; i < 1; i++ {
   514  		errg.Go(testTCPConn(clientPort, 1024*1024, time.Second*30))
   515  	}
   516  	if err := errg.Wait(); err != nil {
   517  		t.Error(err)
   518  	}
   519  }