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