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