github.com/xmplusdev/xray-core@v1.8.10/testing/scenarios/shadowsocks_2022_test.go (about) 1 package scenarios 2 3 import ( 4 "crypto/rand" 5 "encoding/base64" 6 "testing" 7 "time" 8 9 "github.com/sagernet/sing-shadowsocks/shadowaead_2022" 10 "github.com/xmplusdev/xray-core/app/log" 11 "github.com/xmplusdev/xray-core/app/proxyman" 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/serial" 16 "github.com/xmplusdev/xray-core/core" 17 "github.com/xmplusdev/xray-core/proxy/dokodemo" 18 "github.com/xmplusdev/xray-core/proxy/freedom" 19 "github.com/xmplusdev/xray-core/proxy/shadowsocks_2022" 20 "github.com/xmplusdev/xray-core/testing/servers/tcp" 21 "github.com/xmplusdev/xray-core/testing/servers/udp" 22 "golang.org/x/sync/errgroup" 23 ) 24 25 func TestShadowsocks2022Tcp(t *testing.T) { 26 for _, method := range shadowaead_2022.List { 27 password := make([]byte, 32) 28 rand.Read(password) 29 t.Run(method, func(t *testing.T) { 30 testShadowsocks2022Tcp(t, method, base64.StdEncoding.EncodeToString(password)) 31 }) 32 } 33 } 34 35 func TestShadowsocks2022UdpAES128(t *testing.T) { 36 password := make([]byte, 32) 37 rand.Read(password) 38 testShadowsocks2022Udp(t, shadowaead_2022.List[0], base64.StdEncoding.EncodeToString(password)) 39 } 40 41 func TestShadowsocks2022UdpAES256(t *testing.T) { 42 password := make([]byte, 32) 43 rand.Read(password) 44 testShadowsocks2022Udp(t, shadowaead_2022.List[1], base64.StdEncoding.EncodeToString(password)) 45 } 46 47 func TestShadowsocks2022UdpChacha(t *testing.T) { 48 password := make([]byte, 32) 49 rand.Read(password) 50 testShadowsocks2022Udp(t, shadowaead_2022.List[2], base64.StdEncoding.EncodeToString(password)) 51 } 52 53 func testShadowsocks2022Tcp(t *testing.T, method string, password string) { 54 tcpServer := tcp.Server{ 55 MsgProcessor: xor, 56 } 57 dest, err := tcpServer.Start() 58 common.Must(err) 59 defer tcpServer.Close() 60 61 serverPort := tcp.PickPort() 62 serverConfig := &core.Config{ 63 App: []*serial.TypedMessage{ 64 serial.ToTypedMessage(&log.Config{ 65 ErrorLogLevel: clog.Severity_Debug, 66 ErrorLogType: log.LogType_Console, 67 }), 68 }, 69 Inbound: []*core.InboundHandlerConfig{ 70 { 71 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 72 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 73 Listen: net.NewIPOrDomain(net.LocalHostIP), 74 }), 75 ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ServerConfig{ 76 Method: method, 77 Key: password, 78 Network: []net.Network{net.Network_TCP}, 79 }), 80 }, 81 }, 82 Outbound: []*core.OutboundHandlerConfig{ 83 { 84 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 85 }, 86 }, 87 } 88 89 clientPort := tcp.PickPort() 90 clientConfig := &core.Config{ 91 App: []*serial.TypedMessage{ 92 serial.ToTypedMessage(&log.Config{ 93 ErrorLogLevel: clog.Severity_Debug, 94 ErrorLogType: log.LogType_Console, 95 }), 96 }, 97 Inbound: []*core.InboundHandlerConfig{ 98 { 99 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 100 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, 101 Listen: net.NewIPOrDomain(net.LocalHostIP), 102 }), 103 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 104 Address: net.NewIPOrDomain(dest.Address), 105 Port: uint32(dest.Port), 106 Networks: []net.Network{net.Network_TCP}, 107 }), 108 }, 109 }, 110 Outbound: []*core.OutboundHandlerConfig{ 111 { 112 ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ClientConfig{ 113 Address: net.NewIPOrDomain(net.LocalHostIP), 114 Port: uint32(serverPort), 115 Method: method, 116 Key: password, 117 }), 118 }, 119 }, 120 } 121 122 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 123 common.Must(err) 124 defer CloseAllServers(servers) 125 126 var errGroup errgroup.Group 127 for i := 0; i < 10; i++ { 128 errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) 129 } 130 131 if err := errGroup.Wait(); err != nil { 132 t.Error(err) 133 } 134 } 135 136 func testShadowsocks2022Udp(t *testing.T, method string, password string) { 137 udpServer := udp.Server{ 138 MsgProcessor: xor, 139 } 140 udpDest, err := udpServer.Start() 141 common.Must(err) 142 defer udpServer.Close() 143 144 serverPort := udp.PickPort() 145 serverConfig := &core.Config{ 146 App: []*serial.TypedMessage{ 147 serial.ToTypedMessage(&log.Config{ 148 ErrorLogLevel: clog.Severity_Debug, 149 ErrorLogType: log.LogType_Console, 150 }), 151 }, 152 Inbound: []*core.InboundHandlerConfig{ 153 { 154 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 155 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}}, 156 Listen: net.NewIPOrDomain(net.LocalHostIP), 157 }), 158 ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ServerConfig{ 159 Method: method, 160 Key: password, 161 Network: []net.Network{net.Network_UDP}, 162 }), 163 }, 164 }, 165 Outbound: []*core.OutboundHandlerConfig{ 166 { 167 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 168 }, 169 }, 170 } 171 172 udpClientPort := udp.PickPort() 173 clientConfig := &core.Config{ 174 App: []*serial.TypedMessage{ 175 serial.ToTypedMessage(&log.Config{ 176 ErrorLogLevel: clog.Severity_Debug, 177 ErrorLogType: log.LogType_Console, 178 }), 179 }, 180 Inbound: []*core.InboundHandlerConfig{ 181 { 182 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 183 PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(udpClientPort)}}, 184 Listen: net.NewIPOrDomain(net.LocalHostIP), 185 }), 186 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 187 Address: net.NewIPOrDomain(udpDest.Address), 188 Port: uint32(udpDest.Port), 189 Networks: []net.Network{net.Network_UDP}, 190 }), 191 }, 192 }, 193 Outbound: []*core.OutboundHandlerConfig{ 194 { 195 ProxySettings: serial.ToTypedMessage(&shadowsocks_2022.ClientConfig{ 196 Address: net.NewIPOrDomain(net.LocalHostIP), 197 Port: uint32(serverPort), 198 Method: method, 199 Key: password, 200 }), 201 }, 202 }, 203 } 204 205 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 206 common.Must(err) 207 defer CloseAllServers(servers) 208 209 var errGroup errgroup.Group 210 for i := 0; i < 2; i++ { 211 errGroup.Go(testUDPConn(udpClientPort, 1024, time.Second*5)) 212 } 213 214 if err := errGroup.Wait(); err != nil { 215 t.Error(err) 216 } 217 }