2017-01-27 15:19:46 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
xproxy "golang.org/x/net/proxy"
|
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/app/dns"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/app/router"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-01-27 15:19:46 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/blackhole"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
"v2ray.com/core/proxy/socks"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
2017-10-26 15:44:22 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2017-01-27 15:19:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResolveIP(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-01-27 15:19:46 -05:00
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-27 15:19:46 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&dns.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Hosts: map[string]*net.IPOrDomain{
|
|
|
|
"google.com": net.NewIPOrDomain(dest.Address),
|
2017-01-27 15:19:46 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
serial.ToTypedMessage(&router.Config{
|
|
|
|
DomainStrategy: router.Config_IpIfNonMatch,
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
|
|
|
Cidr: []*router.CIDR{
|
|
|
|
{
|
|
|
|
Ip: []byte{127, 0, 0, 0},
|
|
|
|
Prefix: 8,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-27 15:19:46 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
|
|
|
AuthType: socks.AuthType_NO_AUTH,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-27 15:19:46 -05:00
|
|
|
UdpEnabled: false,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tag: "direct",
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{
|
|
|
|
DomainStrategy: freedom.Config_USE_IP,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-27 15:19:46 -05:00
|
|
|
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
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)
|
2017-01-27 15:19:46 -05:00
|
|
|
conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-01-27 15:19:46 -05:00
|
|
|
|
|
|
|
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))
|
2017-01-27 15:19:46 -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 15:19:46 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
CloseAllServers(servers)
|
2017-01-27 15:19:46 -05:00
|
|
|
}
|