1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/testing/scenarios/dokodemo_test.go

210 lines
5.5 KiB
Go
Raw Normal View History

2015-12-04 10:49:10 -05:00
package scenarios
import (
"testing"
2019-01-11 11:17:59 -05:00
"time"
2015-12-04 10:49:10 -05:00
2019-02-03 13:46:53 -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/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"
"github.com/v2fly/v2ray-core/v4/testing/servers/udp"
2015-12-04 10:49:10 -05:00
)
func TestDokodemoTCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
2015-12-04 10:49:10 -05:00
}
dest, err := tcpServer.Start()
2019-01-11 11:17:59 -05:00
common.Must(err)
2015-12-15 10:00:47 -05:00
defer tcpServer.Close()
2015-12-04 10:49:10 -05:00
userID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
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(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := uint32(tcp.PickPort())
clientPortRange := uint32(5)
clientConfig := &core.Config{
2017-05-23 18:54:30 -04:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-05-23 18:54:30 -04:00
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
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(),
}),
},
},
},
},
}),
},
},
}
2015-12-04 10:49:10 -05:00
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-01-11 11:17:59 -05:00
common.Must(err)
defer CloseAllServers(servers)
for port := clientPort; port <= clientPort+clientPortRange; port++ {
2019-01-11 11:17:59 -05:00
if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
t.Error(err)
}
2017-01-05 05:41:51 -05:00
}
}
func TestDokodemoUDP(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-05 05:41:51 -05:00
defer udpServer.Close()
userID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-05 05:41:51 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-05 05:41:51 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-05 05:41:51 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-05 05:41:51 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-05 05:41:51 -05:00
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := uint32(tcp.PickPort())
2017-01-05 05:41:51 -05:00
clientPortRange := uint32(5)
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-05 05:41:51 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-05 05:41:51 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-01-05 05:41:51 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-05 05:41:51 -05:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-05 05:41:51 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-05 05:41:51 -05:00
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-05 05:41:51 -05:00
2019-02-03 13:46:53 -05:00
var errg errgroup.Group
2017-01-05 05:41:51 -05:00
for port := clientPort; port <= clientPort+clientPortRange; port++ {
2019-02-03 13:46:53 -05:00
errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5))
}
if err := errg.Wait(); err != nil {
t.Error(err)
2015-12-04 10:49:10 -05:00
}
2015-12-04 10:49:45 -05:00
}