v2ray-core/testing/scenarios/dokodemo_test.go

237 lines
5.8 KiB
Go
Raw Normal View History

2015-12-04 15:49:10 +00:00
package scenarios
import (
"testing"
"v2ray.com/core"
2017-05-23 22:54:30 +00:00
"v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman"
2017-12-19 22:54:21 +00:00
clog "v2ray.com/core/common/log"
"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"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/testing/servers/tcp"
2017-01-05 10:41:51 +00:00
"v2ray.com/core/testing/servers/udp"
2017-10-26 19:44:22 +00:00
. "v2ray.com/ext/assert"
2015-12-04 15:49:10 +00:00
)
func TestDokodemoTCP(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2015-12-04 15:49:10 +00:00
tcpServer := tcp.Server{
MsgProcessor: xor,
2015-12-04 15:49:10 +00:00
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2015-12-15 15:00:47 +00:00
defer tcpServer.Close()
2015-12-04 15:49:10 +00:00
userID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 21:29:56 +00: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 10:53:39 +00:00
clientPort := uint32(tcp.PickPort())
clientPortRange := uint32(5)
clientConfig := &core.Config{
2017-05-23 22:54:30 +00:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 21:29:56 +00:00
ErrorLogLevel: clog.Severity_Debug,
2017-05-23 22:54:30 +00: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 15:49:10 +00:00
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
for port := clientPort; port <= clientPort+clientPortRange; port++ {
2015-12-04 15:49:10 +00:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-05 10:41:51 +00:00
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2017-01-05 10:41:51 +00:00
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
2017-01-05 10:41:51 +00:00
}
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-05 10:41:51 +00:00
}
func TestDokodemoUDP(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-05 10:41:51 +00:00
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-05 10:41:51 +00:00
defer udpServer.Close()
userID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
2017-01-05 10:41:51 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-05 10:41:51 +00:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-05 10:41:51 +00:00
},
},
}
2018-03-01 10:53:39 +00:00
clientPort := uint32(tcp.PickPort())
2017-01-05 10:41:51 +00:00
clientPortRange := uint32(5)
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-05 10:41:51 +00: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 10:41:51 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-01-05 10:41:51 +00:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-05 10:41:51 +00:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-05 10:41:51 +00:00
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-05 10:41:51 +00:00
for port := clientPort; port <= clientPort+clientPortRange; port++ {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2015-12-04 15:49:10 +00:00
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2015-12-04 15:49:10 +00:00
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
2015-12-04 15:49:10 +00:00
}
2016-01-03 23:33:25 +00:00
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2015-12-04 15:49:45 +00:00
}