1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/testing/scenarios/policy_test.go

269 lines
6.6 KiB
Go
Raw Normal View History

2018-02-20 07:53:07 -05:00
package scenarios
import (
2021-06-19 09:36:54 -04:00
"google.golang.org/protobuf/types/known/anypb"
2018-02-20 07:53:07 -05:00
"io"
"testing"
"time"
2019-01-11 11:17:59 -05:00
"golang.org/x/sync/errgroup"
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/log"
"github.com/v2fly/v2ray-core/v4/app/policy"
"github.com/v2fly/v2ray-core/v4/app/proxyman"
"github.com/v2fly/v2ray-core/v4/common"
clog "github.com/v2fly/v2ray-core/v4/common/log"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/common/uuid"
"github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
"github.com/v2fly/v2ray-core/v4/proxy/vmess"
"github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
"github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
2018-02-20 07:53:07 -05:00
)
func startQuickClosingTCPServer() (net.Listener, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
break
}
b := make([]byte, 1024)
conn.Read(b)
conn.Close()
}
}()
return listener, nil
}
func TestVMessClosing(t *testing.T) {
tcpServer, err := startQuickClosingTCPServer()
2019-01-11 11:17:59 -05:00
common.Must(err)
2018-02-20 07:53:07 -05:00
defer tcpServer.Close()
dest := net.DestinationFromAddr(tcpServer.Addr())
userID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2018-02-20 07:53:07 -05:00
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-02-20 07:53:07 -05:00
serial.ToTypedMessage(&policy.Config{
Level: map[uint32]*policy.Policy{
2018-02-22 04:34:14 -05:00
0: {
2018-02-20 07:53:07 -05:00
Timeout: &policy.Policy_Timeout{
UplinkOnly: &policy.Second{Value: 0},
DownlinkOnly: &policy.Second{Value: 0},
},
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
AlterId: 64,
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2018-02-20 07:53:07 -05:00
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-02-20 07:53:07 -05:00
serial.ToTypedMessage(&policy.Config{
Level: map[uint32]*policy.Policy{
2018-02-22 04:34:14 -05:00
0: {
2018-02-20 07:53:07 -05:00
Timeout: &policy.Policy_Timeout{
UplinkOnly: &policy.Second{Value: 0},
DownlinkOnly: &policy.Second{Value: 0},
},
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
AlterId: 64,
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
}),
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-01-11 11:17:59 -05:00
common.Must(err)
2018-02-20 07:53:07 -05:00
defer CloseAllServers(servers)
2019-01-11 11:17:59 -05:00
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF {
t.Error(err)
}
2018-02-20 07:53:07 -05:00
}
2018-05-25 06:08:28 -04:00
func TestZeroBuffer(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-01-11 11:17:59 -05:00
common.Must(err)
2018-05-25 06:08:28 -04:00
defer tcpServer.Close()
userID := protocol.NewID(uuid.New())
serverPort := tcp.PickPort()
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-05-25 06:08:28 -04:00
serial.ToTypedMessage(&policy.Config{
Level: map[uint32]*policy.Policy{
0: {
Timeout: &policy.Policy_Timeout{
UplinkOnly: &policy.Second{Value: 0},
DownlinkOnly: &policy.Second{Value: 0},
},
Buffer: &policy.Policy_Buffer{
2018-05-25 07:12:00 -04:00
Connection: 0,
2018-05-25 06:08:28 -04:00
},
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
AlterId: 64,
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-05-25 06:08:28 -04:00
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
AlterId: 64,
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
}),
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-01-11 11:17:59 -05:00
common.Must(err)
2018-07-28 09:43:53 -04:00
defer CloseAllServers(servers)
2018-05-25 06:08:28 -04:00
2019-01-11 11:17:59 -05:00
var errg errgroup.Group
2018-05-25 06:08:28 -04:00
for i := 0; i < 10; i++ {
2019-01-11 11:17:59 -05:00
errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
}
if err := errg.Wait(); err != nil {
t.Error(err)
2018-05-25 06:08:28 -04:00
}
}