github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/testing/scenarios/policy_test.go (about) 1 package scenarios 2 3 import ( 4 "io" 5 "testing" 6 "time" 7 8 "golang.org/x/sync/errgroup" 9 10 "v2ray.com/core" 11 "v2ray.com/core/app/log" 12 "v2ray.com/core/app/policy" 13 "v2ray.com/core/app/proxyman" 14 "v2ray.com/core/common" 15 clog "v2ray.com/core/common/log" 16 "v2ray.com/core/common/net" 17 "v2ray.com/core/common/protocol" 18 "v2ray.com/core/common/serial" 19 "v2ray.com/core/common/uuid" 20 "v2ray.com/core/proxy/dokodemo" 21 "v2ray.com/core/proxy/freedom" 22 "v2ray.com/core/proxy/vmess" 23 "v2ray.com/core/proxy/vmess/inbound" 24 "v2ray.com/core/proxy/vmess/outbound" 25 "v2ray.com/core/testing/servers/tcp" 26 ) 27 28 func startQuickClosingTCPServer() (net.Listener, error) { 29 listener, err := net.Listen("tcp", "127.0.0.1:0") 30 if err != nil { 31 return nil, err 32 } 33 go func() { 34 for { 35 conn, err := listener.Accept() 36 if err != nil { 37 break 38 } 39 b := make([]byte, 1024) 40 conn.Read(b) 41 conn.Close() 42 } 43 }() 44 return listener, nil 45 } 46 47 func TestVMessClosing(t *testing.T) { 48 tcpServer, err := startQuickClosingTCPServer() 49 common.Must(err) 50 defer tcpServer.Close() 51 52 dest := net.DestinationFromAddr(tcpServer.Addr()) 53 54 userID := protocol.NewID(uuid.New()) 55 serverPort := tcp.PickPort() 56 serverConfig := &core.Config{ 57 App: []*serial.TypedMessage{ 58 serial.ToTypedMessage(&policy.Config{ 59 Level: map[uint32]*policy.Policy{ 60 0: { 61 Timeout: &policy.Policy_Timeout{ 62 UplinkOnly: &policy.Second{Value: 0}, 63 DownlinkOnly: &policy.Second{Value: 0}, 64 }, 65 }, 66 }, 67 }), 68 }, 69 Inbound: []*core.InboundHandlerConfig{ 70 { 71 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 72 PortRange: net.SinglePortRange(serverPort), 73 Listen: net.NewIPOrDomain(net.LocalHostIP), 74 }), 75 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 76 User: []*protocol.User{ 77 { 78 Account: serial.ToTypedMessage(&vmess.Account{ 79 Id: userID.String(), 80 AlterId: 64, 81 }), 82 }, 83 }, 84 }), 85 }, 86 }, 87 Outbound: []*core.OutboundHandlerConfig{ 88 { 89 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 90 }, 91 }, 92 } 93 94 clientPort := tcp.PickPort() 95 clientConfig := &core.Config{ 96 App: []*serial.TypedMessage{ 97 serial.ToTypedMessage(&policy.Config{ 98 Level: map[uint32]*policy.Policy{ 99 0: { 100 Timeout: &policy.Policy_Timeout{ 101 UplinkOnly: &policy.Second{Value: 0}, 102 DownlinkOnly: &policy.Second{Value: 0}, 103 }, 104 }, 105 }, 106 }), 107 }, 108 Inbound: []*core.InboundHandlerConfig{ 109 { 110 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 111 PortRange: net.SinglePortRange(clientPort), 112 Listen: net.NewIPOrDomain(net.LocalHostIP), 113 }), 114 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 115 Address: net.NewIPOrDomain(dest.Address), 116 Port: uint32(dest.Port), 117 NetworkList: &net.NetworkList{ 118 Network: []net.Network{net.Network_TCP}, 119 }, 120 }), 121 }, 122 }, 123 Outbound: []*core.OutboundHandlerConfig{ 124 { 125 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 126 Receiver: []*protocol.ServerEndpoint{ 127 { 128 Address: net.NewIPOrDomain(net.LocalHostIP), 129 Port: uint32(serverPort), 130 User: []*protocol.User{ 131 { 132 Account: serial.ToTypedMessage(&vmess.Account{ 133 Id: userID.String(), 134 AlterId: 64, 135 SecuritySettings: &protocol.SecurityConfig{ 136 Type: protocol.SecurityType_AES128_GCM, 137 }, 138 }), 139 }, 140 }, 141 }, 142 }, 143 }), 144 }, 145 }, 146 } 147 148 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 149 common.Must(err) 150 defer CloseAllServers(servers) 151 152 if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF { 153 t.Error(err) 154 } 155 } 156 157 func TestZeroBuffer(t *testing.T) { 158 tcpServer := tcp.Server{ 159 MsgProcessor: xor, 160 } 161 dest, err := tcpServer.Start() 162 common.Must(err) 163 defer tcpServer.Close() 164 165 userID := protocol.NewID(uuid.New()) 166 serverPort := tcp.PickPort() 167 serverConfig := &core.Config{ 168 App: []*serial.TypedMessage{ 169 serial.ToTypedMessage(&policy.Config{ 170 Level: map[uint32]*policy.Policy{ 171 0: { 172 Timeout: &policy.Policy_Timeout{ 173 UplinkOnly: &policy.Second{Value: 0}, 174 DownlinkOnly: &policy.Second{Value: 0}, 175 }, 176 Buffer: &policy.Policy_Buffer{ 177 Connection: 0, 178 }, 179 }, 180 }, 181 }), 182 }, 183 Inbound: []*core.InboundHandlerConfig{ 184 { 185 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 186 PortRange: net.SinglePortRange(serverPort), 187 Listen: net.NewIPOrDomain(net.LocalHostIP), 188 }), 189 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 190 User: []*protocol.User{ 191 { 192 Account: serial.ToTypedMessage(&vmess.Account{ 193 Id: userID.String(), 194 AlterId: 64, 195 }), 196 }, 197 }, 198 }), 199 }, 200 }, 201 Outbound: []*core.OutboundHandlerConfig{ 202 { 203 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 204 }, 205 }, 206 } 207 208 clientPort := tcp.PickPort() 209 clientConfig := &core.Config{ 210 App: []*serial.TypedMessage{ 211 serial.ToTypedMessage(&log.Config{ 212 ErrorLogLevel: clog.Severity_Debug, 213 ErrorLogType: log.LogType_Console, 214 }), 215 }, 216 Inbound: []*core.InboundHandlerConfig{ 217 { 218 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 219 PortRange: net.SinglePortRange(clientPort), 220 Listen: net.NewIPOrDomain(net.LocalHostIP), 221 }), 222 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 223 Address: net.NewIPOrDomain(dest.Address), 224 Port: uint32(dest.Port), 225 NetworkList: &net.NetworkList{ 226 Network: []net.Network{net.Network_TCP}, 227 }, 228 }), 229 }, 230 }, 231 Outbound: []*core.OutboundHandlerConfig{ 232 { 233 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 234 Receiver: []*protocol.ServerEndpoint{ 235 { 236 Address: net.NewIPOrDomain(net.LocalHostIP), 237 Port: uint32(serverPort), 238 User: []*protocol.User{ 239 { 240 Account: serial.ToTypedMessage(&vmess.Account{ 241 Id: userID.String(), 242 AlterId: 64, 243 SecuritySettings: &protocol.SecurityConfig{ 244 Type: protocol.SecurityType_AES128_GCM, 245 }, 246 }), 247 }, 248 }, 249 }, 250 }, 251 }), 252 }, 253 }, 254 } 255 256 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 257 common.Must(err) 258 defer CloseAllServers(servers) 259 260 var errg errgroup.Group 261 for i := 0; i < 10; i++ { 262 errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20)) 263 } 264 if err := errg.Wait(); err != nil { 265 t.Error(err) 266 } 267 }