github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/testing/scenarios/shadowsocks_test.go (about) 1 package scenarios 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/xtls/xray-core/app/log" 8 "github.com/xtls/xray-core/app/proxyman" 9 "github.com/xtls/xray-core/common" 10 clog "github.com/xtls/xray-core/common/log" 11 "github.com/xtls/xray-core/common/net" 12 "github.com/xtls/xray-core/common/protocol" 13 "github.com/xtls/xray-core/common/serial" 14 "github.com/xtls/xray-core/core" 15 "github.com/xtls/xray-core/proxy/dokodemo" 16 "github.com/xtls/xray-core/proxy/freedom" 17 "github.com/xtls/xray-core/proxy/shadowsocks" 18 "github.com/xtls/xray-core/testing/servers/tcp" 19 "github.com/xtls/xray-core/testing/servers/udp" 20 "golang.org/x/sync/errgroup" 21 ) 22 23 func TestShadowsocksChaCha20Poly1305TCP(t *testing.T) { 24 tcpServer := tcp.Server{ 25 MsgProcessor: xor, 26 } 27 dest, err := tcpServer.Start() 28 common.Must(err) 29 defer tcpServer.Close() 30 31 account := serial.ToTypedMessage(&shadowsocks.Account{ 32 Password: "shadowsocks-password", 33 CipherType: shadowsocks.CipherType_CHACHA20_POLY1305, 34 }) 35 36 serverPort := tcp.PickPort() 37 serverConfig := &core.Config{ 38 Inbound: []*core.InboundHandlerConfig{ 39 { 40 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 41 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 42 Listen: net.NewIPOrDomain(net.LocalHostIP), 43 }), 44 ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ 45 Users: []*protocol.User{{ 46 Account: account, 47 Level: 1, 48 }}, 49 Network: []net.Network{net.Network_TCP}, 50 }), 51 }, 52 }, 53 Outbound: []*core.OutboundHandlerConfig{ 54 { 55 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 56 }, 57 }, 58 } 59 60 clientPort := tcp.PickPort() 61 clientConfig := &core.Config{ 62 Inbound: []*core.InboundHandlerConfig{ 63 { 64 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 65 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 66 Listen: net.NewIPOrDomain(net.LocalHostIP), 67 }), 68 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 69 Address: net.NewIPOrDomain(dest.Address), 70 Port: uint32(dest.Port), 71 Networks: []net.Network{net.Network_TCP}, 72 }), 73 }, 74 }, 75 Outbound: []*core.OutboundHandlerConfig{ 76 { 77 ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ 78 Server: []*protocol.ServerEndpoint{ 79 { 80 Address: net.NewIPOrDomain(net.LocalHostIP), 81 Port: uint32(serverPort), 82 User: []*protocol.User{ 83 { 84 Account: account, 85 }, 86 }, 87 }, 88 }, 89 }), 90 }, 91 }, 92 } 93 94 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 95 common.Must(err) 96 defer CloseAllServers(servers) 97 98 var errGroup errgroup.Group 99 for i := 0; i < 10; i++ { 100 errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) 101 } 102 if err := errGroup.Wait(); err != nil { 103 t.Error(err) 104 } 105 } 106 107 func TestShadowsocksAES256GCMTCP(t *testing.T) { 108 tcpServer := tcp.Server{ 109 MsgProcessor: xor, 110 } 111 dest, err := tcpServer.Start() 112 common.Must(err) 113 defer tcpServer.Close() 114 115 account := serial.ToTypedMessage(&shadowsocks.Account{ 116 Password: "shadowsocks-password", 117 CipherType: shadowsocks.CipherType_AES_256_GCM, 118 }) 119 120 serverPort := tcp.PickPort() 121 serverConfig := &core.Config{ 122 App: []*serial.TypedMessage{ 123 serial.ToTypedMessage(&log.Config{ 124 ErrorLogLevel: clog.Severity_Debug, 125 ErrorLogType: log.LogType_Console, 126 }), 127 }, 128 Inbound: []*core.InboundHandlerConfig{ 129 { 130 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 131 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 132 Listen: net.NewIPOrDomain(net.LocalHostIP), 133 }), 134 ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ 135 Users: []*protocol.User{{ 136 Account: account, 137 Level: 1, 138 }}, 139 Network: []net.Network{net.Network_TCP}, 140 }), 141 }, 142 }, 143 Outbound: []*core.OutboundHandlerConfig{ 144 { 145 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 146 }, 147 }, 148 } 149 150 clientPort := tcp.PickPort() 151 clientConfig := &core.Config{ 152 App: []*serial.TypedMessage{ 153 serial.ToTypedMessage(&log.Config{ 154 ErrorLogLevel: clog.Severity_Debug, 155 ErrorLogType: log.LogType_Console, 156 }), 157 }, 158 Inbound: []*core.InboundHandlerConfig{ 159 { 160 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 161 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 162 Listen: net.NewIPOrDomain(net.LocalHostIP), 163 }), 164 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 165 Address: net.NewIPOrDomain(dest.Address), 166 Port: uint32(dest.Port), 167 Networks: []net.Network{net.Network_TCP}, 168 }), 169 }, 170 }, 171 Outbound: []*core.OutboundHandlerConfig{ 172 { 173 ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ 174 Server: []*protocol.ServerEndpoint{ 175 { 176 Address: net.NewIPOrDomain(net.LocalHostIP), 177 Port: uint32(serverPort), 178 User: []*protocol.User{ 179 { 180 Account: account, 181 }, 182 }, 183 }, 184 }, 185 }), 186 }, 187 }, 188 } 189 190 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 191 common.Must(err) 192 defer CloseAllServers(servers) 193 194 var errGroup errgroup.Group 195 for i := 0; i < 10; i++ { 196 errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) 197 } 198 199 if err := errGroup.Wait(); err != nil { 200 t.Error(err) 201 } 202 } 203 204 func TestShadowsocksAES128GCMUDP(t *testing.T) { 205 udpServer := udp.Server{ 206 MsgProcessor: xor, 207 } 208 dest, err := udpServer.Start() 209 common.Must(err) 210 defer udpServer.Close() 211 212 account := serial.ToTypedMessage(&shadowsocks.Account{ 213 Password: "shadowsocks-password", 214 CipherType: shadowsocks.CipherType_AES_128_GCM, 215 }) 216 217 serverPort := udp.PickPort() 218 serverConfig := &core.Config{ 219 App: []*serial.TypedMessage{ 220 serial.ToTypedMessage(&log.Config{ 221 ErrorLogLevel: clog.Severity_Debug, 222 ErrorLogType: log.LogType_Console, 223 }), 224 }, 225 Inbound: []*core.InboundHandlerConfig{ 226 { 227 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 228 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 229 Listen: net.NewIPOrDomain(net.LocalHostIP), 230 }), 231 ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ 232 Users: []*protocol.User{{ 233 Account: account, 234 Level: 1, 235 }}, 236 Network: []net.Network{net.Network_UDP}, 237 }), 238 }, 239 }, 240 Outbound: []*core.OutboundHandlerConfig{ 241 { 242 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 243 }, 244 }, 245 } 246 247 clientPort := udp.PickPort() 248 clientConfig := &core.Config{ 249 App: []*serial.TypedMessage{ 250 serial.ToTypedMessage(&log.Config{ 251 ErrorLogLevel: clog.Severity_Debug, 252 ErrorLogType: log.LogType_Console, 253 }), 254 }, 255 Inbound: []*core.InboundHandlerConfig{ 256 { 257 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 258 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 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 Networks: []net.Network{net.Network_UDP}, 265 }), 266 }, 267 }, 268 Outbound: []*core.OutboundHandlerConfig{ 269 { 270 ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ 271 Server: []*protocol.ServerEndpoint{ 272 { 273 Address: net.NewIPOrDomain(net.LocalHostIP), 274 Port: uint32(serverPort), 275 User: []*protocol.User{ 276 { 277 Account: account, 278 }, 279 }, 280 }, 281 }, 282 }), 283 }, 284 }, 285 } 286 287 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 288 common.Must(err) 289 defer CloseAllServers(servers) 290 291 var errGroup errgroup.Group 292 for i := 0; i < 2; i++ { 293 errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5)) 294 } 295 if err := errGroup.Wait(); err != nil { 296 t.Error(err) 297 } 298 } 299 300 func TestShadowsocksAES128GCMUDPMux(t *testing.T) { 301 udpServer := udp.Server{ 302 MsgProcessor: xor, 303 } 304 dest, err := udpServer.Start() 305 common.Must(err) 306 defer udpServer.Close() 307 308 account := serial.ToTypedMessage(&shadowsocks.Account{ 309 Password: "shadowsocks-password", 310 CipherType: shadowsocks.CipherType_AES_128_GCM, 311 }) 312 313 serverPort := tcp.PickPort() 314 serverConfig := &core.Config{ 315 App: []*serial.TypedMessage{ 316 serial.ToTypedMessage(&log.Config{ 317 ErrorLogLevel: clog.Severity_Debug, 318 ErrorLogType: log.LogType_Console, 319 }), 320 }, 321 Inbound: []*core.InboundHandlerConfig{ 322 { 323 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 324 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 325 Listen: net.NewIPOrDomain(net.LocalHostIP), 326 }), 327 ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ 328 Users: []*protocol.User{{ 329 Account: account, 330 Level: 1, 331 }}, 332 Network: []net.Network{net.Network_TCP}, 333 }), 334 }, 335 }, 336 Outbound: []*core.OutboundHandlerConfig{ 337 { 338 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 339 }, 340 }, 341 } 342 343 clientPort := udp.PickPort() 344 clientConfig := &core.Config{ 345 App: []*serial.TypedMessage{ 346 serial.ToTypedMessage(&log.Config{ 347 ErrorLogLevel: clog.Severity_Debug, 348 ErrorLogType: log.LogType_Console, 349 }), 350 }, 351 Inbound: []*core.InboundHandlerConfig{ 352 { 353 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 354 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 355 Listen: net.NewIPOrDomain(net.LocalHostIP), 356 }), 357 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 358 Address: net.NewIPOrDomain(dest.Address), 359 Port: uint32(dest.Port), 360 Networks: []net.Network{net.Network_UDP}, 361 }), 362 }, 363 }, 364 Outbound: []*core.OutboundHandlerConfig{ 365 { 366 SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ 367 MultiplexSettings: &proxyman.MultiplexingConfig{ 368 Enabled: true, 369 Concurrency: 8, 370 }, 371 }), 372 ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ 373 Server: []*protocol.ServerEndpoint{ 374 { 375 Address: net.NewIPOrDomain(net.LocalHostIP), 376 Port: uint32(serverPort), 377 User: []*protocol.User{ 378 { 379 Account: account, 380 }, 381 }, 382 }, 383 }, 384 }), 385 }, 386 }, 387 } 388 389 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 390 common.Must(err) 391 defer CloseAllServers(servers) 392 393 var errGroup errgroup.Group 394 for i := 0; i < 2; i++ { 395 errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5)) 396 } 397 if err := errGroup.Wait(); err != nil { 398 t.Error(err) 399 } 400 } 401 402 func TestShadowsocksNone(t *testing.T) { 403 tcpServer := tcp.Server{ 404 MsgProcessor: xor, 405 } 406 dest, err := tcpServer.Start() 407 common.Must(err) 408 409 defer tcpServer.Close() 410 411 account := serial.ToTypedMessage(&shadowsocks.Account{ 412 Password: "shadowsocks-password", 413 CipherType: shadowsocks.CipherType_NONE, 414 }) 415 416 serverPort := tcp.PickPort() 417 serverConfig := &core.Config{ 418 Inbound: []*core.InboundHandlerConfig{ 419 { 420 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 421 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 422 Listen: net.NewIPOrDomain(net.LocalHostIP), 423 }), 424 ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ 425 Users: []*protocol.User{{ 426 Account: account, 427 Level: 1, 428 }}, 429 Network: []net.Network{net.Network_TCP}, 430 }), 431 }, 432 }, 433 Outbound: []*core.OutboundHandlerConfig{ 434 { 435 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 436 }, 437 }, 438 } 439 440 clientPort := tcp.PickPort() 441 clientConfig := &core.Config{ 442 Inbound: []*core.InboundHandlerConfig{ 443 { 444 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 445 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 446 Listen: net.NewIPOrDomain(net.LocalHostIP), 447 }), 448 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 449 Address: net.NewIPOrDomain(dest.Address), 450 Port: uint32(dest.Port), 451 Networks: []net.Network{net.Network_TCP}, 452 }), 453 }, 454 }, 455 Outbound: []*core.OutboundHandlerConfig{ 456 { 457 ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{ 458 Server: []*protocol.ServerEndpoint{ 459 { 460 Address: net.NewIPOrDomain(net.LocalHostIP), 461 Port: uint32(serverPort), 462 User: []*protocol.User{ 463 { 464 Account: account, 465 }, 466 }, 467 }, 468 }, 469 }), 470 }, 471 }, 472 } 473 474 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 475 common.Must(err) 476 477 defer CloseAllServers(servers) 478 479 var errGroup errgroup.Group 480 for i := 0; i < 10; i++ { 481 errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) 482 } 483 484 if err := errGroup.Wait(); err != nil { 485 t.Fatal(err) 486 } 487 }