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

751 lines
18 KiB
Go
Raw Normal View History

2017-01-08 16:32:41 -05:00
package scenarios
import (
2017-05-17 18:39:30 -04:00
"io/ioutil"
2017-04-24 19:40:34 -04:00
"net/http"
"net/url"
2017-01-08 16:32:41 -05:00
"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"
2017-04-24 19:40:34 -04:00
"v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman"
2017-01-10 18:09:33 -05:00
"v2ray.com/core/app/router"
2017-12-19 16:29:56 -05:00
clog "v2ray.com/core/common/log"
"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"
2017-04-24 19:40:34 -04:00
v2http "v2ray.com/core/proxy/http"
"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/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-10-26 15:44:22 -04:00
. "v2ray.com/ext/assert"
2017-01-08 16:32:41 -05:00
)
func TestPassiveConnection(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-08 16:32:41 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
SendFirst: []byte("send first"),
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-08 16:32:41 -05:00
defer tcpServer.Close()
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-08 16:32:41 -05: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 16:32:41 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-08 16:32:41 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-08 16:32:41 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-08 16:32:41 -05:00
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-08 16:32:41 -05:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(serverPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-08 16:32:41 -05:00
{
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(string(response[:nBytes]), Equals, "send first")
2017-01-08 16:32:41 -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))
2017-01-08 16:32:41 -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)))
2017-01-08 16:32:41 -05:00
}
2017-10-24 10:15:35 -04:00
assert(conn.Close(), IsNil)
2017-01-08 16:32:41 -05:00
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-01-08 16:32:41 -05:00
}
2017-01-10 08:25:31 -05:00
func TestProxy(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-10 08:25:31 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -05:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.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: []*core.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: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.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: []*core.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: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05: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 08:25:31 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 08:25:31 -05:00
},
}),
},
},
Outbound: []*core.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: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 08:25:31 -05: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 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: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 06:59:57 -05:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-12 06:59:57 -05:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-12 06:59:57 -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))
2017-01-12 06:59:57 -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)
2017-01-12 06:59:57 -05:00
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-01-12 06:59:57 -05:00
}
func TestProxyOverKCP(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-12 06:59:57 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-12 06:59:57 -05:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
serverPort := pickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05: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 06:59:57 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.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: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.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: []*core.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: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05: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 06:59:57 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-12 06:59:57 -05:00
},
}),
},
},
Outbound: []*core.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: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 06:59:57 -05: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 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: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 08:25:31 -05:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -05:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -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))
2017-01-10 08:25:31 -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)
2017-01-10 08:25:31 -05:00
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-01-10 08:25:31 -05:00
}
func TestBlackhole(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-10 08:25:31 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -05:00
defer tcpServer.Close()
2017-01-10 18:09:33 -05:00
tcpServer2 := tcp.Server{
MsgProcessor: xor,
}
dest2, err := tcpServer2.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 18:09:33 -05:00
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: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05: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 08:25:31 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 08:25:31 -05:00
},
}),
},
2017-01-10 18:09:33 -05: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 18:09:33 -05:00
Port: uint32(dest2.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 18:09:33 -05:00
},
}),
},
2017-01-10 08:25:31 -05:00
},
Outbound: []*core.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: net.SinglePortRange(dest2.Port),
2017-01-10 18:09:33 -05:00
},
},
}),
},
2017-01-10 08:25:31 -05:00
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -05:00
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
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-10 08:25:31 -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))
2017-01-10 08:25:31 -05:00
}
{
response := make([]byte, 1024)
_, err := conn.Read(response)
2017-10-24 10:15:35 -04:00
assert(err, IsNotNil)
2017-01-10 08:25:31 -05:00
}
2017-10-24 10:15:35 -04:00
assert(conn.Close(), IsNil)
2017-01-10 08:25:31 -05:00
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-01-10 08:25:31 -05:00
}
func TestForward(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
defer tcpServer.Close()
serverPort := 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 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
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)
}
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
}
2017-01-27 08:10:36 -05:00
func TestUDPConnection(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-01-27 08:10:36 -05:00
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-27 08:10:36 -05:00
defer udpServer.Close()
clientPort := pickPort()
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-27 08:10:36 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-01-27 08:10:36 -05:00
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-27 08:10:36 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-01-27 08:10:36 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-27 08:10:36 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(clientConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-27 08:10:36 -05:00
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-01-27 08:10:36 -05:00
payload := "dokodemo request."
for i := 0; i < 5; i++ {
nBytes, err := conn.Write([]byte(payload))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2017-01-27 08:10:36 -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)))
2017-01-27 08:10:36 -05:00
}
2017-10-24 10:15:35 -04:00
assert(conn.Close(), IsNil)
2017-01-27 08:10:36 -05: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 10:15:35 -04:00
assert(err, IsNil)
2017-01-27 08:10:36 -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))
2017-01-27 08:10:36 -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)
2017-01-27 08:10:36 -05:00
}
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-01-27 08:10:36 -05:00
}
2017-04-24 19:40:34 -04:00
func TestDomainSniffing(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-24 19:40:34 -04:00
sniffingPort := pickPort()
httpPort := pickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-04-24 19:40:34 -04:00
{
Tag: "snif",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(sniffingPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
DomainOverride: []proxyman.KnownProtocols{
proxyman.KnownProtocols_TLS,
},
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
Port: 443,
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-04-24 19:40:34 -04:00
},
}),
},
{
Tag: "http",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(httpPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
}),
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-04-24 19:40:34 -04:00
{
Tag: "redir",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
Port: uint32(sniffingPort),
},
},
}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Tag: "direct",
InboundTag: []string{"snif"},
}, {
Tag: "redir",
InboundTag: []string{"http"},
},
},
}),
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-04-24 19:40:34 -04:00
ErrorLogType: log.LogType_Console,
}),
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-04-24 19:40:34 -04: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 10:15:35 -04:00
assert(err, IsNil)
assert(resp.StatusCode, Equals, 200)
2017-04-25 07:34:00 -04:00
2017-10-24 10:15:35 -04:00
assert(resp.Write(ioutil.Discard), IsNil)
2017-04-24 19:40:34 -04:00
}
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-04-24 19:40:34 -04:00
}