You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/testing/scenarios/dns_test.go

108 lines
2.5 KiB

8 years ago
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"
8 years ago
"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"
7 years ago
. "v2ray.com/ext/assert"
8 years ago
)
func TestResolveIP(t *testing.T) {
assert := With(t)
8 years ago
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert(err, IsNil)
8 years ago
defer tcpServer.Close()
serverPort := tcp.PickPort()
8 years ago
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&dns.Config{
Hosts: map[string]*net.IPOrDomain{
"google.com": net.NewIPOrDomain(dest.Address),
8 years ago
},
}),
serial.ToTypedMessage(&router.Config{
DomainStrategy: router.Config_IpIfNonMatch,
Rule: []*router.RoutingRule{
{
Cidr: []*router.CIDR{
{
Ip: []byte{127, 0, 0, 0},
Prefix: 8,
},
},
6 years ago
TargetTag: &router.RoutingRule_Tag{
Tag: "direct",
},
8 years ago
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
8 years ago
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
8 years ago
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
8 years ago
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
8 years ago
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DomainStrategy: freedom.Config_USE_IP,
}),
},
},
}
8 years ago
servers, err := InitializeServerConfigs(serverConfig)
assert(err, IsNil)
8 years ago
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
assert(err, IsNil)
8 years ago
conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
assert(err, IsNil)
8 years ago
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
8 years ago
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert(err, IsNil)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
8 years ago
}
8 years ago
CloseAllServers(servers)
8 years ago
}