github.com/v2fly/v2ray-core/v4@v4.45.2/testing/scenarios/dokodemo_test.go (about) 1 package scenarios 2 3 import ( 4 "testing" 5 "time" 6 7 "golang.org/x/sync/errgroup" 8 9 core "github.com/v2fly/v2ray-core/v4" 10 "github.com/v2fly/v2ray-core/v4/app/log" 11 "github.com/v2fly/v2ray-core/v4/app/proxyman" 12 "github.com/v2fly/v2ray-core/v4/common" 13 clog "github.com/v2fly/v2ray-core/v4/common/log" 14 "github.com/v2fly/v2ray-core/v4/common/net" 15 "github.com/v2fly/v2ray-core/v4/common/protocol" 16 "github.com/v2fly/v2ray-core/v4/common/serial" 17 "github.com/v2fly/v2ray-core/v4/common/uuid" 18 "github.com/v2fly/v2ray-core/v4/proxy/dokodemo" 19 "github.com/v2fly/v2ray-core/v4/proxy/freedom" 20 "github.com/v2fly/v2ray-core/v4/proxy/vmess" 21 "github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound" 22 "github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound" 23 "github.com/v2fly/v2ray-core/v4/testing/servers/tcp" 24 "github.com/v2fly/v2ray-core/v4/testing/servers/udp" 25 ) 26 27 func TestDokodemoTCP(t *testing.T) { 28 tcpServer := tcp.Server{ 29 MsgProcessor: xor, 30 } 31 dest, err := tcpServer.Start() 32 common.Must(err) 33 defer tcpServer.Close() 34 35 userID := protocol.NewID(uuid.New()) 36 serverPort := tcp.PickPort() 37 serverConfig := &core.Config{ 38 App: []*serial.TypedMessage{ 39 serial.ToTypedMessage(&log.Config{ 40 ErrorLogLevel: clog.Severity_Debug, 41 ErrorLogType: log.LogType_Console, 42 }), 43 }, 44 Inbound: []*core.InboundHandlerConfig{ 45 { 46 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 47 PortRange: net.SinglePortRange(serverPort), 48 Listen: net.NewIPOrDomain(net.LocalHostIP), 49 }), 50 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 51 User: []*protocol.User{ 52 { 53 Account: serial.ToTypedMessage(&vmess.Account{ 54 Id: userID.String(), 55 }), 56 }, 57 }, 58 }), 59 }, 60 }, 61 Outbound: []*core.OutboundHandlerConfig{ 62 { 63 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 64 }, 65 }, 66 } 67 68 clientPort := uint32(tcp.PickPort()) 69 clientPortRange := uint32(5) 70 clientConfig := &core.Config{ 71 App: []*serial.TypedMessage{ 72 serial.ToTypedMessage(&log.Config{ 73 ErrorLogLevel: clog.Severity_Debug, 74 ErrorLogType: log.LogType_Console, 75 }), 76 }, 77 Inbound: []*core.InboundHandlerConfig{ 78 { 79 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 80 PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange}, 81 Listen: net.NewIPOrDomain(net.LocalHostIP), 82 }), 83 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 84 Address: net.NewIPOrDomain(dest.Address), 85 Port: uint32(dest.Port), 86 NetworkList: &net.NetworkList{ 87 Network: []net.Network{net.Network_TCP}, 88 }, 89 }), 90 }, 91 }, 92 Outbound: []*core.OutboundHandlerConfig{ 93 { 94 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 95 Receiver: []*protocol.ServerEndpoint{ 96 { 97 Address: net.NewIPOrDomain(net.LocalHostIP), 98 Port: uint32(serverPort), 99 User: []*protocol.User{ 100 { 101 Account: serial.ToTypedMessage(&vmess.Account{ 102 Id: userID.String(), 103 }), 104 }, 105 }, 106 }, 107 }, 108 }), 109 }, 110 }, 111 } 112 113 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 114 common.Must(err) 115 defer CloseAllServers(servers) 116 117 for port := clientPort; port <= clientPort+clientPortRange; port++ { 118 if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil { 119 t.Error(err) 120 } 121 } 122 } 123 124 func TestDokodemoUDP(t *testing.T) { 125 udpServer := udp.Server{ 126 MsgProcessor: xor, 127 } 128 dest, err := udpServer.Start() 129 common.Must(err) 130 defer udpServer.Close() 131 132 userID := protocol.NewID(uuid.New()) 133 serverPort := tcp.PickPort() 134 serverConfig := &core.Config{ 135 Inbound: []*core.InboundHandlerConfig{ 136 { 137 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 138 PortRange: net.SinglePortRange(serverPort), 139 Listen: net.NewIPOrDomain(net.LocalHostIP), 140 }), 141 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 142 User: []*protocol.User{ 143 { 144 Account: serial.ToTypedMessage(&vmess.Account{ 145 Id: userID.String(), 146 }), 147 }, 148 }, 149 }), 150 }, 151 }, 152 Outbound: []*core.OutboundHandlerConfig{ 153 { 154 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 155 }, 156 }, 157 } 158 159 clientPort := uint32(udp.PickPort()) 160 clientPortRange := uint32(5) 161 clientConfig := &core.Config{ 162 Inbound: []*core.InboundHandlerConfig{ 163 { 164 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 165 PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange}, 166 Listen: net.NewIPOrDomain(net.LocalHostIP), 167 }), 168 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 169 Address: net.NewIPOrDomain(dest.Address), 170 Port: uint32(dest.Port), 171 NetworkList: &net.NetworkList{ 172 Network: []net.Network{net.Network_UDP}, 173 }, 174 }), 175 }, 176 }, 177 Outbound: []*core.OutboundHandlerConfig{ 178 { 179 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 180 Receiver: []*protocol.ServerEndpoint{ 181 { 182 Address: net.NewIPOrDomain(net.LocalHostIP), 183 Port: uint32(serverPort), 184 User: []*protocol.User{ 185 { 186 Account: serial.ToTypedMessage(&vmess.Account{ 187 Id: userID.String(), 188 }), 189 }, 190 }, 191 }, 192 }, 193 }), 194 }, 195 }, 196 } 197 198 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 199 common.Must(err) 200 defer CloseAllServers(servers) 201 202 var errg errgroup.Group 203 for port := clientPort; port <= clientPort+clientPortRange; port++ { 204 errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5)) 205 } 206 if err := errg.Wait(); err != nil { 207 t.Error(err) 208 } 209 }