github.com/xraypb/Xray-core@v1.8.1/testing/scenarios/transport_test.go (about) 1 package scenarios 2 3 import ( 4 "os" 5 "runtime" 6 "testing" 7 "time" 8 9 "github.com/xraypb/Xray-core/app/log" 10 "github.com/xraypb/Xray-core/app/proxyman" 11 "github.com/xraypb/Xray-core/common" 12 clog "github.com/xraypb/Xray-core/common/log" 13 "github.com/xraypb/Xray-core/common/net" 14 "github.com/xraypb/Xray-core/common/protocol" 15 "github.com/xraypb/Xray-core/common/serial" 16 "github.com/xraypb/Xray-core/common/uuid" 17 "github.com/xraypb/Xray-core/core" 18 "github.com/xraypb/Xray-core/proxy/dokodemo" 19 "github.com/xraypb/Xray-core/proxy/freedom" 20 "github.com/xraypb/Xray-core/proxy/vmess" 21 "github.com/xraypb/Xray-core/proxy/vmess/inbound" 22 "github.com/xraypb/Xray-core/proxy/vmess/outbound" 23 "github.com/xraypb/Xray-core/testing/servers/tcp" 24 "github.com/xraypb/Xray-core/testing/servers/udp" 25 "github.com/xraypb/Xray-core/transport/internet" 26 "github.com/xraypb/Xray-core/transport/internet/domainsocket" 27 "github.com/xraypb/Xray-core/transport/internet/headers/http" 28 "github.com/xraypb/Xray-core/transport/internet/headers/wechat" 29 "github.com/xraypb/Xray-core/transport/internet/quic" 30 tcptransport "github.com/xraypb/Xray-core/transport/internet/tcp" 31 "golang.org/x/sync/errgroup" 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 PortList: &net.PortList{Range: []*net.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 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 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 PortList: &net.PortList{Range: []*net.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 PortList: &net.PortList{Range: []*net.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 := udp.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 PortList: &net.PortList{Range: []*net.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 }), 294 }, 295 }, 296 }), 297 }, 298 }, 299 Outbound: []*core.OutboundHandlerConfig{ 300 { 301 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 302 }, 303 }, 304 } 305 306 clientPort := tcp.PickPort() 307 clientConfig := &core.Config{ 308 App: []*serial.TypedMessage{ 309 serial.ToTypedMessage(&log.Config{ 310 ErrorLogLevel: clog.Severity_Debug, 311 ErrorLogType: log.LogType_Console, 312 }), 313 }, 314 Inbound: []*core.InboundHandlerConfig{ 315 { 316 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 317 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 318 Listen: net.NewIPOrDomain(net.LocalHostIP), 319 }), 320 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 321 Address: net.NewIPOrDomain(dest.Address), 322 Port: uint32(dest.Port), 323 NetworkList: &net.NetworkList{ 324 Network: []net.Network{net.Network_TCP}, 325 }, 326 }), 327 }, 328 }, 329 Outbound: []*core.OutboundHandlerConfig{ 330 { 331 SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ 332 StreamSettings: &internet.StreamConfig{ 333 ProtocolName: "quic", 334 TransportSettings: []*internet.TransportConfig{ 335 { 336 ProtocolName: "quic", 337 Settings: serial.ToTypedMessage(&quic.Config{ 338 Header: serial.ToTypedMessage(&wechat.VideoConfig{}), 339 Security: &protocol.SecurityConfig{ 340 Type: protocol.SecurityType_NONE, 341 }, 342 }), 343 }, 344 }, 345 }, 346 }), 347 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 348 Receiver: []*protocol.ServerEndpoint{ 349 { 350 Address: net.NewIPOrDomain(net.LocalHostIP), 351 Port: uint32(serverPort), 352 User: []*protocol.User{ 353 { 354 Account: serial.ToTypedMessage(&vmess.Account{ 355 Id: userID.String(), 356 SecuritySettings: &protocol.SecurityConfig{ 357 Type: protocol.SecurityType_AES128_GCM, 358 }, 359 }), 360 }, 361 }, 362 }, 363 }, 364 }), 365 }, 366 }, 367 } 368 369 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 370 if err != nil { 371 t.Fatal("Failed to initialize all servers: ", err.Error()) 372 } 373 defer CloseAllServers(servers) 374 375 var errg errgroup.Group 376 for i := 0; i < 10; i++ { 377 errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40)) 378 } 379 380 if err := errg.Wait(); err != nil { 381 t.Error(err) 382 } 383 }