github.com/EagleQL/Xray-core@v1.4.3/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 11 "github.com/xtls/xray-core/app/log" 12 "github.com/xtls/xray-core/app/proxyman" 13 "github.com/xtls/xray-core/common" 14 clog "github.com/xtls/xray-core/common/log" 15 "github.com/xtls/xray-core/common/net" 16 "github.com/xtls/xray-core/common/protocol" 17 "github.com/xtls/xray-core/common/serial" 18 "github.com/xtls/xray-core/common/uuid" 19 "github.com/xtls/xray-core/core" 20 "github.com/xtls/xray-core/proxy/dokodemo" 21 "github.com/xtls/xray-core/proxy/freedom" 22 "github.com/xtls/xray-core/proxy/vmess" 23 "github.com/xtls/xray-core/proxy/vmess/inbound" 24 "github.com/xtls/xray-core/proxy/vmess/outbound" 25 "github.com/xtls/xray-core/testing/servers/tcp" 26 "github.com/xtls/xray-core/transport/internet" 27 "github.com/xtls/xray-core/transport/internet/domainsocket" 28 "github.com/xtls/xray-core/transport/internet/headers/http" 29 "github.com/xtls/xray-core/transport/internet/headers/wechat" 30 "github.com/xtls/xray-core/transport/internet/quic" 31 tcptransport "github.com/xtls/xray-core/transport/internet/tcp" 32 ) 33 34 func TestHTTPConnectionHeader(t *testing.T) { 35 tcpServer := tcp.Server{ 36 MsgProcessor: xor, 37 } 38 dest, err := tcpServer.Start() 39 common.Must(err) 40 defer tcpServer.Close() 41 42 userID := protocol.NewID(uuid.New()) 43 serverPort := tcp.PickPort() 44 serverConfig := &core.Config{ 45 Inbound: []*core.InboundHandlerConfig{ 46 { 47 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 48 PortRange: net.SinglePortRange(serverPort), 49 Listen: net.NewIPOrDomain(net.LocalHostIP), 50 StreamSettings: &internet.StreamConfig{ 51 TransportSettings: []*internet.TransportConfig{ 52 { 53 Protocol: internet.TransportProtocol_TCP, 54 Settings: serial.ToTypedMessage(&tcptransport.Config{ 55 HeaderSettings: serial.ToTypedMessage(&http.Config{}), 56 }), 57 }, 58 }, 59 }, 60 }), 61 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 62 User: []*protocol.User{ 63 { 64 Account: serial.ToTypedMessage(&vmess.Account{ 65 Id: userID.String(), 66 }), 67 }, 68 }, 69 }), 70 }, 71 }, 72 Outbound: []*core.OutboundHandlerConfig{ 73 { 74 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 75 }, 76 }, 77 } 78 79 clientPort := tcp.PickPort() 80 clientConfig := &core.Config{ 81 Inbound: []*core.InboundHandlerConfig{ 82 { 83 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 84 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 Receiver: []*protocol.ServerEndpoint{ 100 { 101 Address: net.NewIPOrDomain(net.LocalHostIP), 102 Port: uint32(serverPort), 103 User: []*protocol.User{ 104 { 105 Account: serial.ToTypedMessage(&vmess.Account{ 106 Id: userID.String(), 107 }), 108 }, 109 }, 110 }, 111 }, 112 }), 113 SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ 114 StreamSettings: &internet.StreamConfig{ 115 TransportSettings: []*internet.TransportConfig{ 116 { 117 Protocol: internet.TransportProtocol_TCP, 118 Settings: serial.ToTypedMessage(&tcptransport.Config{ 119 HeaderSettings: serial.ToTypedMessage(&http.Config{}), 120 }), 121 }, 122 }, 123 }, 124 }), 125 }, 126 }, 127 } 128 129 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 130 common.Must(err) 131 defer CloseAllServers(servers) 132 133 if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil { 134 t.Error(err) 135 } 136 } 137 138 func TestDomainSocket(t *testing.T) { 139 if runtime.GOOS == "windows" || runtime.GOOS == "android" { 140 t.Skip("Not supported on windows or android") 141 return 142 } 143 tcpServer := tcp.Server{ 144 MsgProcessor: xor, 145 } 146 dest, err := tcpServer.Start() 147 common.Must(err) 148 defer tcpServer.Close() 149 150 const dsPath = "/tmp/ds_scenario" 151 os.Remove(dsPath) 152 153 userID := protocol.NewID(uuid.New()) 154 serverPort := tcp.PickPort() 155 serverConfig := &core.Config{ 156 Inbound: []*core.InboundHandlerConfig{ 157 { 158 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 159 PortRange: net.SinglePortRange(serverPort), 160 Listen: net.NewIPOrDomain(net.LocalHostIP), 161 StreamSettings: &internet.StreamConfig{ 162 Protocol: internet.TransportProtocol_DomainSocket, 163 TransportSettings: []*internet.TransportConfig{ 164 { 165 Protocol: internet.TransportProtocol_DomainSocket, 166 Settings: serial.ToTypedMessage(&domainsocket.Config{ 167 Path: dsPath, 168 }), 169 }, 170 }, 171 }, 172 }), 173 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 174 User: []*protocol.User{ 175 { 176 Account: serial.ToTypedMessage(&vmess.Account{ 177 Id: userID.String(), 178 }), 179 }, 180 }, 181 }), 182 }, 183 }, 184 Outbound: []*core.OutboundHandlerConfig{ 185 { 186 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 187 }, 188 }, 189 } 190 191 clientPort := tcp.PickPort() 192 clientConfig := &core.Config{ 193 Inbound: []*core.InboundHandlerConfig{ 194 { 195 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 196 PortRange: net.SinglePortRange(clientPort), 197 Listen: net.NewIPOrDomain(net.LocalHostIP), 198 }), 199 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 200 Address: net.NewIPOrDomain(dest.Address), 201 Port: uint32(dest.Port), 202 NetworkList: &net.NetworkList{ 203 Network: []net.Network{net.Network_TCP}, 204 }, 205 }), 206 }, 207 }, 208 Outbound: []*core.OutboundHandlerConfig{ 209 { 210 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 211 Receiver: []*protocol.ServerEndpoint{ 212 { 213 Address: net.NewIPOrDomain(net.LocalHostIP), 214 Port: uint32(serverPort), 215 User: []*protocol.User{ 216 { 217 Account: serial.ToTypedMessage(&vmess.Account{ 218 Id: userID.String(), 219 }), 220 }, 221 }, 222 }, 223 }, 224 }), 225 SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ 226 StreamSettings: &internet.StreamConfig{ 227 Protocol: internet.TransportProtocol_DomainSocket, 228 TransportSettings: []*internet.TransportConfig{ 229 { 230 Protocol: internet.TransportProtocol_DomainSocket, 231 Settings: serial.ToTypedMessage(&domainsocket.Config{ 232 Path: dsPath, 233 }), 234 }, 235 }, 236 }, 237 }), 238 }, 239 }, 240 } 241 242 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 243 common.Must(err) 244 defer CloseAllServers(servers) 245 246 if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil { 247 t.Error(err) 248 } 249 } 250 251 func TestVMessQuic(t *testing.T) { 252 tcpServer := tcp.Server{ 253 MsgProcessor: xor, 254 } 255 dest, err := tcpServer.Start() 256 common.Must(err) 257 defer tcpServer.Close() 258 259 userID := protocol.NewID(uuid.New()) 260 serverPort := tcp.PickPort() 261 serverConfig := &core.Config{ 262 App: []*serial.TypedMessage{ 263 serial.ToTypedMessage(&log.Config{ 264 ErrorLogLevel: clog.Severity_Debug, 265 ErrorLogType: log.LogType_Console, 266 }), 267 }, 268 Inbound: []*core.InboundHandlerConfig{ 269 { 270 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 271 PortRange: net.SinglePortRange(serverPort), 272 Listen: net.NewIPOrDomain(net.LocalHostIP), 273 StreamSettings: &internet.StreamConfig{ 274 ProtocolName: "quic", 275 TransportSettings: []*internet.TransportConfig{ 276 { 277 ProtocolName: "quic", 278 Settings: serial.ToTypedMessage(&quic.Config{ 279 Header: serial.ToTypedMessage(&wechat.VideoConfig{}), 280 Security: &protocol.SecurityConfig{ 281 Type: protocol.SecurityType_NONE, 282 }, 283 }), 284 }, 285 }, 286 }, 287 }), 288 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 289 User: []*protocol.User{ 290 { 291 Account: serial.ToTypedMessage(&vmess.Account{ 292 Id: userID.String(), 293 AlterId: 64, 294 }), 295 }, 296 }, 297 }), 298 }, 299 }, 300 Outbound: []*core.OutboundHandlerConfig{ 301 { 302 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 303 }, 304 }, 305 } 306 307 clientPort := tcp.PickPort() 308 clientConfig := &core.Config{ 309 App: []*serial.TypedMessage{ 310 serial.ToTypedMessage(&log.Config{ 311 ErrorLogLevel: clog.Severity_Debug, 312 ErrorLogType: 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: 64, 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 }