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