github.com/eagleql/xray-core@v1.4.4/testing/scenarios/reverse_test.go (about)

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