1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-13 13:46:45 -05:00
v2fly/testing/scenarios/feature_test.go

651 lines
16 KiB
Go
Raw Normal View History

2017-01-08 16:32:41 -05:00
package scenarios
import (
"net"
"testing"
2017-01-27 08:10:36 -05:00
"time"
2017-01-08 16:32:41 -05:00
xproxy "golang.org/x/net/proxy"
2017-01-08 16:32:41 -05:00
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
2017-01-10 18:09:33 -05:00
"v2ray.com/core/app/router"
2017-01-08 16:32:41 -05:00
v2net "v2ray.com/core/common/net"
2017-01-10 08:25:31 -05:00
"v2ray.com/core/common/protocol"
2017-01-08 16:32:41 -05:00
"v2ray.com/core/common/serial"
2017-01-10 08:25:31 -05:00
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/blackhole"
2017-01-08 16:32:41 -05:00
"v2ray.com/core/proxy/dokodemo"
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/proxy/socks"
2017-01-10 08:25:31 -05:00
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/proxy/vmess/outbound"
2017-01-08 16:32:41 -05:00
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
2017-01-27 08:10:36 -05:00
"v2ray.com/core/testing/servers/udp"
2017-01-10 08:25:31 -05:00
"v2ray.com/core/transport/internet"
2017-01-08 16:32:41 -05:00
)
func TestPassiveConnection(t *testing.T) {
assert := assert.On(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
SendFirst: []byte("send first"),
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-08 16:32:41 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-08 16:32:41 -05:00
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-08 16:32:41 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-08 16:32:41 -05:00
},
},
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(serverPort),
})
assert.Error(err).IsNil()
{
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
assert.Error(err).IsNil()
assert.String(string(response[:nBytes])).Equals("send first")
}
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()
}
2017-01-10 08:25:31 -05:00
func TestProxy(t *testing.T) {
assert := assert.On(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 08:25:31 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 08:25:31 -05:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
proxyPort := pickPort()
proxyConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(proxyPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 08:25:31 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 08:25:31 -05:00
},
},
}
clientPort := pickPort()
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(clientPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-10 08:25:31 -05:00
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 08:25:31 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
},
},
}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
ProxySettings: &internet.ProxyConfig{
Tag: "proxy",
},
}),
2017-01-10 08:25:31 -05:00
},
2017-01-12 06:59:57 -05:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 06:59:57 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
assert.Error(InitializeServerConfig(proxyConfig)).IsNil()
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
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 TestProxyOverKCP(t *testing.T) {
assert := assert.On(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 06:59:57 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-12 06:59:57 -05:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
proxyPort := pickPort()
proxyConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(proxyPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 06:59:57 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
2017-01-12 06:59:57 -05:00
},
},
}
clientPort := pickPort()
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(clientPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-12 06:59:57 -05:00
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 06:59:57 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
},
},
}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
ProxySettings: &internet.ProxyConfig{
Tag: "proxy",
},
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
2017-01-12 06:59:57 -05:00
},
2017-01-10 08:25:31 -05:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 08:25:31 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
assert.Error(InitializeServerConfig(proxyConfig)).IsNil()
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
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 TestBlackhole(t *testing.T) {
assert := assert.On(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
2017-01-10 18:09:33 -05:00
tcpServer2 := tcp.Server{
MsgProcessor: xor,
}
dest2, err := tcpServer2.Start()
assert.Error(err).IsNil()
defer tcpServer2.Close()
2017-01-10 08:25:31 -05:00
serverPort := pickPort()
2017-01-10 18:09:33 -05:00
serverPort2 := pickPort()
2017-01-10 08:25:31 -05:00
serverConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-10 08:25:31 -05:00
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
2017-01-10 18:09:33 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(serverPort2),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2017-01-10 18:09:33 -05:00
Address: v2net.NewIPOrDomain(dest2.Address),
Port: uint32(dest2.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
},
}),
},
2017-01-10 08:25:31 -05:00
},
Outbound: []*proxyman.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 18:09:33 -05:00
},
{
Tag: "blocked",
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
2017-01-10 08:25:31 -05:00
},
},
2017-01-10 18:09:33 -05:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Tag: "blocked",
PortRange: v2net.SinglePortRange(dest2.Port),
},
},
}),
},
2017-01-10 08:25:31 -05:00
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
2017-01-10 18:09:33 -05:00
Port: int(serverPort2),
2017-01-10 08:25:31 -05: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)
_, err := conn.Read(response)
assert.Error(err).IsNotNil()
}
assert.Error(conn.Close()).IsNil()
CloseAllServers()
}
func TestForward(t *testing.T) {
assert := assert.On(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert.Error(err).IsNil()
defer tcpServer.Close()
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(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
UdpEnabled: false,
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(dest.Port),
},
},
}),
},
},
}
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
{
noAuthDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
assert.Error(err).IsNil()
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
assert.Error(err).IsNil()
payload := "test payload"
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()
}
2017-01-27 08:10:36 -05:00
func TestUDPConnection(t *testing.T) {
assert := assert.On(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert.Error(err).IsNil()
defer udpServer.Close()
clientPort := pickPort()
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(clientPort),
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_UDP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert.Error(err).IsNil()
payload := "dokodemo request."
for i := 0; i < 5; i++ {
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()
}
time.Sleep(20 * time.Second)
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
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()
}