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