v2ray-core/testing/scenarios/feature_test.go

860 lines
21 KiB
Go
Raw Normal View History

2017-01-08 21:32:41 +00:00
package scenarios
import (
2018-02-20 23:18:53 +00:00
"context"
2017-05-17 22:39:30 +00:00
"io/ioutil"
2017-04-24 23:40:34 +00:00
"net/http"
"net/url"
2017-01-08 21:32:41 +00:00
"testing"
2017-01-27 13:10:36 +00:00
"time"
2017-01-08 21:32:41 +00:00
xproxy "golang.org/x/net/proxy"
2017-01-08 21:32:41 +00:00
"v2ray.com/core"
2018-02-20 23:18:53 +00:00
"v2ray.com/core/app/dispatcher"
2017-04-24 23:40:34 +00:00
"v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman"
2018-02-20 23:18:53 +00:00
_ "v2ray.com/core/app/proxyman/inbound"
_ "v2ray.com/core/app/proxyman/outbound"
2017-01-10 23:09:33 +00:00
"v2ray.com/core/app/router"
2017-12-19 21:29:56 +00:00
clog "v2ray.com/core/common/log"
"v2ray.com/core/common/net"
2017-01-10 13:25:31 +00:00
"v2ray.com/core/common/protocol"
2017-01-08 21:32:41 +00:00
"v2ray.com/core/common/serial"
2017-01-10 13:25:31 +00:00
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/blackhole"
2017-01-08 21:32:41 +00:00
"v2ray.com/core/proxy/dokodemo"
"v2ray.com/core/proxy/freedom"
2017-04-24 23:40:34 +00:00
v2http "v2ray.com/core/proxy/http"
"v2ray.com/core/proxy/socks"
2017-01-10 13:25:31 +00:00
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/proxy/vmess/outbound"
2017-01-08 21:32:41 +00:00
"v2ray.com/core/testing/servers/tcp"
2017-01-27 13:10:36 +00:00
"v2ray.com/core/testing/servers/udp"
2017-01-10 13:25:31 +00:00
"v2ray.com/core/transport/internet"
2017-10-26 19:44:22 +00:00
. "v2ray.com/ext/assert"
2017-01-08 21:32:41 +00:00
)
func TestPassiveConnection(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-08 21:32:41 +00:00
tcpServer := tcp.Server{
MsgProcessor: xor,
SendFirst: []byte("send first"),
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-08 21:32:41 +00:00
defer tcpServer.Close()
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
2017-01-08 21:32:41 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-08 21:32:41 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-08 21:32:41 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-08 21:32:41 +00:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-08 21:32:41 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-08 21:32:41 +00:00
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-08 21:32:41 +00:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(serverPort),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-08 21:32:41 +00:00
{
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(string(response[:nBytes]), Equals, "send first")
2017-01-08 21:32:41 +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-08 21:32:41 +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)))
2017-01-08 21:32:41 +00:00
}
2017-10-24 14:15:35 +00:00
assert(conn.Close(), IsNil)
2017-01-08 21:32:41 +00:00
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-08 21:32:41 +00:00
}
2017-01-10 13:25:31 +00:00
func TestProxy(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-10 13:25:31 +00:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +00:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
2017-01-10 13:25:31 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 13:25:31 +00:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 13:25:31 +00:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
proxyPort := tcp.PickPort()
2017-01-10 13:25:31 +00:00
proxyConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 13:25:31 +00:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 13:25:31 +00:00
},
},
}
2018-03-01 10:53:39 +00:00
clientPort := tcp.PickPort()
2017-01-10 13:25:31 +00:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-10 13:25:31 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 13:25:31 +00:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 13:25:31 +00:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 13:25:31 +00:00
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 13:25:31 +00:00
},
2017-01-12 11:59:57 +00:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 11:59:57 +00:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 11:59:57 +00:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-12 11:59:57 +00:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-12 11:59:57 +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-12 11:59:57 +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-12 11:59:57 +00:00
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-12 11:59:57 +00:00
}
func TestProxyOverKCP(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-12 11:59:57 +00:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-12 11:59:57 +00:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
2017-01-12 11:59:57 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 11:59:57 +00:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-12 11:59:57 +00:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
2018-03-01 10:53:39 +00:00
proxyPort := tcp.PickPort()
2017-01-12 11:59:57 +00:00
proxyConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 11:59:57 +00:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
2017-01-12 11:59:57 +00:00
},
},
}
2018-03-01 10:53:39 +00:00
clientPort := tcp.PickPort()
2017-01-12 11:59:57 +00:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-12 11:59:57 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-12 11:59:57 +00:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 11:59:57 +00:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 11:59:57 +00:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 11:59:57 +00:00
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 11:59:57 +00:00
},
2017-01-10 13:25:31 +00:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 13:25:31 +00:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 13:25:31 +00:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +00:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +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-10 13:25:31 +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-10 13:25:31 +00:00
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-10 13:25:31 +00:00
}
func TestBlackhole(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-10 13:25:31 +00:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +00:00
defer tcpServer.Close()
2017-01-10 23:09:33 +00:00
tcpServer2 := tcp.Server{
MsgProcessor: xor,
}
dest2, err := tcpServer2.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 23:09:33 +00:00
defer tcpServer2.Close()
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
serverPort2 := tcp.PickPort()
2017-01-10 13:25:31 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-10 13:25:31 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 13:25:31 +00:00
},
}),
},
2017-01-10 23:09:33 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort2),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest2.Address),
2017-01-10 23:09:33 +00:00
Port: uint32(dest2.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 23:09:33 +00:00
},
}),
},
2017-01-10 13:25:31 +00:00
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 13:25:31 +00:00
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 23:09:33 +00:00
},
{
Tag: "blocked",
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
2017-01-10 13:25:31 +00:00
},
},
2017-01-10 23:09:33 +00:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2018-11-08 00:01:53 +00:00
TargetTag: &router.RoutingRule_Tag{
Tag: "blocked",
},
PortRange: net.SinglePortRange(dest2.Port),
2017-01-10 23:09:33 +00:00
},
},
}),
},
2017-01-10 13:25:31 +00:00
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +00:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
2017-01-10 23:09:33 +00:00
Port: int(serverPort2),
2017-01-10 13:25:31 +00:00
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-10 13:25:31 +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-10 13:25:31 +00:00
}
{
response := make([]byte, 1024)
_, err := conn.Read(response)
2017-10-24 14:15:35 +00:00
assert(err, IsNotNil)
2017-01-10 13:25:31 +00:00
}
2017-10-24 14:15:35 +00:00
assert(conn.Close(), IsNil)
2017-01-10 13:25:31 +00:00
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-10 13:25:31 +00:00
}
func TestForward(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
defer tcpServer.Close()
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(dest.Port),
},
},
}),
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
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-05-17 22:39:30 +00:00
CloseAllServers(servers)
}
2017-01-27 13:10:36 +00:00
func TestUDPConnection(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-01-27 13:10:36 +00:00
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-27 13:10:36 +00:00
defer udpServer.Close()
2018-03-01 10:53:39 +00:00
clientPort := tcp.PickPort()
2017-01-27 13:10:36 +00:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-27 13:10:36 +00:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-01-27 13:10:36 +00:00
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-27 13:10:36 +00:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-01-27 13:10:36 +00:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-27 13:10:36 +00:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(clientConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-27 13:10:36 +00:00
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-27 13:10:36 +00:00
payload := "dokodemo request."
for i := 0; i < 5; i++ {
nBytes, err := conn.Write([]byte(payload))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2017-01-27 13:10:36 +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)))
2017-01-27 13:10:36 +00:00
}
2017-10-24 14:15:35 +00:00
assert(conn.Close(), IsNil)
2017-01-27 13:10:36 +00:00
}
time.Sleep(20 * time.Second)
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-27 13:10:36 +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-27 13:10:36 +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-27 13:10:36 +00:00
}
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-01-27 13:10:36 +00:00
}
2017-04-24 23:40:34 +00:00
func TestDomainSniffing(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-04-24 23:40:34 +00:00
2018-03-01 10:53:39 +00:00
sniffingPort := tcp.PickPort()
httpPort := tcp.PickPort()
2017-04-24 23:40:34 +00:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-04-24 23:40:34 +00:00
{
Tag: "snif",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(sniffingPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 23:40:34 +00:00
DomainOverride: []proxyman.KnownProtocols{
proxyman.KnownProtocols_TLS,
},
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 23:40:34 +00:00
Port: 443,
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-04-24 23:40:34 +00:00
},
}),
},
{
Tag: "http",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(httpPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 23:40:34 +00:00
}),
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-04-24 23:40:34 +00:00
{
Tag: "redir",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 23:40:34 +00:00
Port: uint32(sniffingPort),
},
},
}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2018-11-08 00:01:53 +00:00
TargetTag: &router.RoutingRule_Tag{
Tag: "direct",
},
2017-04-24 23:40:34 +00:00
InboundTag: []string{"snif"},
}, {
2018-11-08 00:01:53 +00:00
TargetTag: &router.RoutingRule_Tag{
Tag: "redir",
},
2017-04-24 23:40:34 +00:00
InboundTag: []string{"http"},
},
},
}),
serial.ToTypedMessage(&log.Config{
2017-12-19 21:29:56 +00:00
ErrorLogLevel: clog.Severity_Debug,
2017-04-24 23:40:34 +00:00
ErrorLogType: log.LogType_Console,
}),
},
}
2017-05-17 22:39:30 +00:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-04-24 23:40:34 +00:00
{
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:" + httpPort.String())
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("https://www.github.com/")
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(resp.StatusCode, Equals, 200)
2017-04-25 11:34:00 +00:00
2017-10-24 14:15:35 +00:00
assert(resp.Write(ioutil.Discard), IsNil)
2017-04-24 23:40:34 +00:00
}
2017-05-17 22:39:30 +00:00
CloseAllServers(servers)
2017-04-24 23:40:34 +00:00
}
2018-02-20 23:18:53 +00:00
func TestDialV2Ray(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())
2018-03-01 10:53:39 +00:00
serverPort := tcp.PickPort()
2018-02-20 23:18:53 +00:00
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
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(),
AlterId: 64,
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&dispatcher.Config{}),
serial.ToTypedMessage(&proxyman.InboundConfig{}),
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
},
Inbound: []*core.InboundHandlerConfig{},
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(),
AlterId: 64,
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
}),
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig)
assert(err, IsNil)
client, err := core.New(clientConfig)
assert(err, IsNil)
conn, err := core.Dial(context.Background(), client, dest)
assert(err, IsNil)
payload := "commander 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)
}