github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/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 "google.golang.org/protobuf/types/known/anypb" 10 11 core "github.com/v2fly/v2ray-core/v5" 12 "github.com/v2fly/v2ray-core/v5/app/log" 13 "github.com/v2fly/v2ray-core/v5/app/policy" 14 "github.com/v2fly/v2ray-core/v5/app/proxyman" 15 "github.com/v2fly/v2ray-core/v5/common" 16 clog "github.com/v2fly/v2ray-core/v5/common/log" 17 "github.com/v2fly/v2ray-core/v5/common/net" 18 "github.com/v2fly/v2ray-core/v5/common/protocol" 19 "github.com/v2fly/v2ray-core/v5/common/serial" 20 "github.com/v2fly/v2ray-core/v5/common/uuid" 21 "github.com/v2fly/v2ray-core/v5/proxy/dokodemo" 22 "github.com/v2fly/v2ray-core/v5/proxy/freedom" 23 "github.com/v2fly/v2ray-core/v5/proxy/vmess" 24 "github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound" 25 "github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound" 26 "github.com/v2fly/v2ray-core/v5/testing/servers/tcp" 27 ) 28 29 func startQuickClosingTCPServer() (net.Listener, error) { 30 listener, err := net.Listen("tcp", "127.0.0.1:0") 31 if err != nil { 32 return nil, err 33 } 34 go func() { 35 for { 36 conn, err := listener.Accept() 37 if err != nil { 38 break 39 } 40 b := make([]byte, 1024) 41 conn.Read(b) 42 conn.Close() 43 } 44 }() 45 return listener, nil 46 } 47 48 func TestVMessClosing(t *testing.T) { 49 tcpServer, err := startQuickClosingTCPServer() 50 common.Must(err) 51 defer tcpServer.Close() 52 53 dest := net.DestinationFromAddr(tcpServer.Addr()) 54 55 userID := protocol.NewID(uuid.New()) 56 serverPort := tcp.PickPort() 57 serverConfig := &core.Config{ 58 App: []*anypb.Any{ 59 serial.ToTypedMessage(&policy.Config{ 60 Level: map[uint32]*policy.Policy{ 61 0: { 62 Timeout: &policy.Policy_Timeout{ 63 UplinkOnly: &policy.Second{Value: 0}, 64 DownlinkOnly: &policy.Second{Value: 0}, 65 }, 66 }, 67 }, 68 }), 69 }, 70 Inbound: []*core.InboundHandlerConfig{ 71 { 72 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 73 PortRange: net.SinglePortRange(serverPort), 74 Listen: net.NewIPOrDomain(net.LocalHostIP), 75 }), 76 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 77 User: []*protocol.User{ 78 { 79 Account: serial.ToTypedMessage(&vmess.Account{ 80 Id: userID.String(), 81 AlterId: 0, 82 }), 83 }, 84 }, 85 }), 86 }, 87 }, 88 Outbound: []*core.OutboundHandlerConfig{ 89 { 90 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 91 }, 92 }, 93 } 94 95 clientPort := tcp.PickPort() 96 clientConfig := &core.Config{ 97 App: []*anypb.Any{ 98 serial.ToTypedMessage(&policy.Config{ 99 Level: map[uint32]*policy.Policy{ 100 0: { 101 Timeout: &policy.Policy_Timeout{ 102 UplinkOnly: &policy.Second{Value: 0}, 103 DownlinkOnly: &policy.Second{Value: 0}, 104 }, 105 }, 106 }, 107 }), 108 }, 109 Inbound: []*core.InboundHandlerConfig{ 110 { 111 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 112 PortRange: net.SinglePortRange(clientPort), 113 Listen: net.NewIPOrDomain(net.LocalHostIP), 114 }), 115 ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ 116 Address: net.NewIPOrDomain(dest.Address), 117 Port: uint32(dest.Port), 118 NetworkList: &net.NetworkList{ 119 Network: []net.Network{net.Network_TCP}, 120 }, 121 }), 122 }, 123 }, 124 Outbound: []*core.OutboundHandlerConfig{ 125 { 126 ProxySettings: serial.ToTypedMessage(&outbound.Config{ 127 Receiver: []*protocol.ServerEndpoint{ 128 { 129 Address: net.NewIPOrDomain(net.LocalHostIP), 130 Port: uint32(serverPort), 131 User: []*protocol.User{ 132 { 133 Account: serial.ToTypedMessage(&vmess.Account{ 134 Id: userID.String(), 135 AlterId: 0, 136 SecuritySettings: &protocol.SecurityConfig{ 137 Type: protocol.SecurityType_AES128_GCM, 138 }, 139 }), 140 }, 141 }, 142 }, 143 }, 144 }), 145 }, 146 }, 147 } 148 149 servers, err := InitializeServerConfigs(serverConfig, clientConfig) 150 common.Must(err) 151 defer CloseAllServers(servers) 152 153 if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF { 154 t.Error(err) 155 } 156 } 157 158 func TestZeroBuffer(t *testing.T) { 159 tcpServer := tcp.Server{ 160 MsgProcessor: xor, 161 } 162 dest, err := tcpServer.Start() 163 common.Must(err) 164 defer tcpServer.Close() 165 166 userID := protocol.NewID(uuid.New()) 167 serverPort := tcp.PickPort() 168 serverConfig := &core.Config{ 169 App: []*anypb.Any{ 170 serial.ToTypedMessage(&policy.Config{ 171 Level: map[uint32]*policy.Policy{ 172 0: { 173 Timeout: &policy.Policy_Timeout{ 174 UplinkOnly: &policy.Second{Value: 0}, 175 DownlinkOnly: &policy.Second{Value: 0}, 176 }, 177 Buffer: &policy.Policy_Buffer{ 178 Connection: 0, 179 }, 180 }, 181 }, 182 }), 183 }, 184 Inbound: []*core.InboundHandlerConfig{ 185 { 186 ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ 187 PortRange: net.SinglePortRange(serverPort), 188 Listen: net.NewIPOrDomain(net.LocalHostIP), 189 }), 190 ProxySettings: serial.ToTypedMessage(&inbound.Config{ 191 User: []*protocol.User{ 192 { 193 Account: serial.ToTypedMessage(&vmess.Account{ 194 Id: userID.String(), 195 AlterId: 0, 196 }), 197 }, 198 }, 199 }), 200 }, 201 }, 202 Outbound: []*core.OutboundHandlerConfig{ 203 { 204 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 205 }, 206 }, 207 } 208 209 clientPort := tcp.PickPort() 210 clientConfig := &core.Config{ 211 App: []*anypb.Any{ 212 serial.ToTypedMessage(&log.Config{ 213 Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: 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: 0, 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 }