mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
236 lines
5.8 KiB
Go
236 lines
5.8 KiB
Go
package scenarios
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"v2ray.com/core"
|
|
"v2ray.com/core/app/log"
|
|
"v2ray.com/core/app/proxyman"
|
|
"v2ray.com/core/common/net"
|
|
"v2ray.com/core/common/protocol"
|
|
"v2ray.com/core/common/serial"
|
|
"v2ray.com/core/common/uuid"
|
|
"v2ray.com/core/proxy/dokodemo"
|
|
"v2ray.com/core/proxy/freedom"
|
|
"v2ray.com/core/proxy/vmess"
|
|
"v2ray.com/core/proxy/vmess/inbound"
|
|
"v2ray.com/core/proxy/vmess/outbound"
|
|
"v2ray.com/core/testing/servers/tcp"
|
|
"v2ray.com/core/testing/servers/udp"
|
|
. "v2ray.com/ext/assert"
|
|
)
|
|
|
|
func TestDokodemoTCP(t *testing.T) {
|
|
assert := With(t)
|
|
|
|
tcpServer := tcp.Server{
|
|
MsgProcessor: xor,
|
|
}
|
|
dest, err := tcpServer.Start()
|
|
assert(err, IsNil)
|
|
defer tcpServer.Close()
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
serverPort := pickPort()
|
|
serverConfig := &core.Config{
|
|
App: []*serial.TypedMessage{
|
|
serial.ToTypedMessage(&log.Config{
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
ErrorLogType: log.LogType_Console,
|
|
}),
|
|
},
|
|
Inbound: []*proxyman.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: []*proxyman.OutboundHandlerConfig{
|
|
{
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
},
|
|
},
|
|
}
|
|
|
|
clientPort := uint32(pickPort())
|
|
clientPortRange := uint32(5)
|
|
clientConfig := &core.Config{
|
|
App: []*serial.TypedMessage{
|
|
serial.ToTypedMessage(&log.Config{
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
ErrorLogType: log.LogType_Console,
|
|
}),
|
|
},
|
|
Inbound: []*proxyman.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: []*proxyman.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(),
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
assert(err, IsNil)
|
|
|
|
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
IP: []byte{127, 0, 0, 1},
|
|
Port: int(port),
|
|
})
|
|
assert(err, IsNil)
|
|
|
|
payload := "dokodemo request."
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
assert(err, IsNil)
|
|
assert(nBytes, Equals, len(payload))
|
|
|
|
response := make([]byte, 1024)
|
|
nBytes, err = conn.Read(response)
|
|
assert(err, IsNil)
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
assert(conn.Close(), IsNil)
|
|
}
|
|
|
|
CloseAllServers(servers)
|
|
}
|
|
|
|
func TestDokodemoUDP(t *testing.T) {
|
|
assert := With(t)
|
|
|
|
udpServer := udp.Server{
|
|
MsgProcessor: xor,
|
|
}
|
|
dest, err := udpServer.Start()
|
|
assert(err, IsNil)
|
|
defer udpServer.Close()
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
serverPort := pickPort()
|
|
serverConfig := &core.Config{
|
|
Inbound: []*proxyman.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: []*proxyman.OutboundHandlerConfig{
|
|
{
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
},
|
|
},
|
|
}
|
|
|
|
clientPort := uint32(pickPort())
|
|
clientPortRange := uint32(5)
|
|
clientConfig := &core.Config{
|
|
Inbound: []*proxyman.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_UDP},
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
Outbound: []*proxyman.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(),
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
assert(err, IsNil)
|
|
|
|
for port := clientPort; port <= clientPort+clientPortRange; port++ {
|
|
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
IP: []byte{127, 0, 0, 1},
|
|
Port: int(port),
|
|
})
|
|
assert(err, IsNil)
|
|
|
|
payload := "dokodemo request."
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
assert(err, IsNil)
|
|
assert(nBytes, Equals, len(payload))
|
|
|
|
response := make([]byte, 1024)
|
|
nBytes, err = conn.Read(response)
|
|
assert(err, IsNil)
|
|
assert(response[:nBytes], Equals, xor([]byte(payload)))
|
|
assert(conn.Close(), IsNil)
|
|
}
|
|
|
|
CloseAllServers(servers)
|
|
}
|