v2ray-core/testing/scenarios/dokodemo_test.go

224 lines
5.7 KiB
Go
Raw Normal View History

2015-12-04 15:49:10 +00:00
package scenarios
import (
"net"
"testing"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
2016-08-20 18:55:45 +00:00
v2net "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/assert"
"v2ray.com/core/testing/servers/tcp"
2017-01-05 10:41:51 +00:00
"v2ray.com/core/testing/servers/udp"
2015-12-04 15:49:10 +00:00
)
func TestDokodemoTCP(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(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()
2015-12-04 15:49:10 +00:00
assert.Error(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())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.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{}),
},
},
}
2015-12-04 15:49:10 +00:00
clientPort := uint32(pickPort())
clientPortRange := uint32(5)
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
},
},
}),
},
},
}
2015-12-04 15:49:10 +00:00
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
assert.Error(InitializeServerConfig(clientConfig)).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-01-05 10:41:51 +00:00
assert.Error(err).IsNil()
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
assert.Error(conn.Close()).IsNil()
}
CloseAllServers()
}
func TestDokodemoUDP(t *testing.T) {
assert := assert.On(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert.Error(err).IsNil()
defer udpServer.Close()
userID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.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: []*proxyman.OutboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-05 10:41:51 +00:00
},
},
}
clientPort := uint32(pickPort())
clientPortRange := uint32(5)
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-05 10:41:51 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-05 10:41:51 +00:00
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_UDP},
},
}),
},
},
Outbound: []*proxyman.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: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
}),
},
},
},
},
}),
},
},
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
assert.Error(InitializeServerConfig(clientConfig)).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.Error(err).IsNil()
2015-12-04 15:49:10 +00:00
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
assert.Error(conn.Close()).IsNil()
2015-12-04 15:49:10 +00:00
}
2016-01-03 23:33:25 +00:00
CloseAllServers()
2015-12-04 15:49:45 +00:00
}