1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/testing/scenarios/dns_test.go

106 lines
2.5 KiB
Go
Raw Normal View History

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"
"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{
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{
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",
},
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
{
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
}