From 8231d2cdad87795034159e6642c2fca286fa425b Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 27 Jan 2017 21:19:46 +0100 Subject: [PATCH] dns test --- app/router/condition.go | 43 ++++++-------- testing/scenarios/dns_test.go | 104 ++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 25 deletions(-) create mode 100644 testing/scenarios/dns_test.go diff --git a/app/router/condition.go b/app/router/condition.go index 2276932b..e1123762 100644 --- a/app/router/condition.go +++ b/app/router/condition.go @@ -124,6 +124,13 @@ func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) } func (v *CIDRMatcher) Apply(ctx context.Context) bool { + ips := make([]net.IP, 4) + if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok { + for _, rip := range resolveIPs { + ips = append(ips, rip.IP()) + } + } + var dest v2net.Destination if v.onSource { dest = proxy.SourceFromContext(ctx) @@ -131,19 +138,8 @@ func (v *CIDRMatcher) Apply(ctx context.Context) bool { dest = proxy.DestinationFromContext(ctx) } - if !dest.IsValid() { - return false - } - - if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) { - return false - } - - ips := []net.IP{dest.Address.IP()} - if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok { - for _, rip := range resolveIPs { - ips = append(ips, rip.IP()) - } + if dest.IsValid() && dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) { + ips = append(ips, dest.Address.IP()) } for _, ip := range ips { @@ -167,25 +163,22 @@ func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher { } func (v *IPv4Matcher) Apply(ctx context.Context) bool { + ips := make([]net.IP, 4) + if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok { + for _, rip := range resolveIPs { + ips = append(ips, rip.IP()) + } + } + var dest v2net.Destination if v.onSource { dest = proxy.SourceFromContext(ctx) } else { dest = proxy.DestinationFromContext(ctx) } - if !dest.IsValid() { - return false - } - if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) { - return false - } - - ips := []net.IP{dest.Address.IP()} - if resolvedIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok { - for _, rip := range resolvedIPs { - ips = append(ips, rip.IP()) - } + if dest.IsValid() && dest.Address.Family().Either(v2net.AddressFamilyIPv4) { + ips = append(ips, dest.Address.IP()) } for _, ip := range ips { diff --git a/testing/scenarios/dns_test.go b/testing/scenarios/dns_test.go new file mode 100644 index 00000000..cdca98d9 --- /dev/null +++ b/testing/scenarios/dns_test.go @@ -0,0 +1,104 @@ +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" + v2net "v2ray.com/core/common/net" + "v2ray.com/core/common/serial" + "v2ray.com/core/proxy/blackhole" + "v2ray.com/core/proxy/freedom" + "v2ray.com/core/proxy/socks" + "v2ray.com/core/testing/assert" + "v2ray.com/core/testing/servers/tcp" +) + +func TestResolveIP(t *testing.T) { + assert := assert.On(t) + + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + assert.Error(err).IsNil() + defer tcpServer.Close() + + serverPort := pickPort() + serverConfig := &core.Config{ + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&dns.Config{ + Hosts: map[string]*v2net.IPOrDomain{ + "google.com": v2net.NewIPOrDomain(dest.Address), + }, + }), + 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: v2net.SinglePortRange(serverPort), + Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ + AuthType: socks.AuthType_NO_AUTH, + Accounts: map[string]string{ + "Test Account": "Test Password", + }, + Address: v2net.NewIPOrDomain(v2net.LocalHostIP), + UdpEnabled: false, + }), + }, + }, + Outbound: []*proxyman.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&blackhole.Config{}), + }, + { + Tag: "direct", + ProxySettings: serial.ToTypedMessage(&freedom.Config{ + DomainStrategy: freedom.Config_USE_IP, + }), + }, + }, + } + + assert.Error(InitializeServerConfig(serverConfig)).IsNil() + + { + noAuthDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct) + assert.Error(err).IsNil() + conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port)) + assert.Error(err).IsNil() + + payload := "test payload" + nBytes, err := conn.Write([]byte(payload)) + assert.Error(err).IsNil() + assert.Int(nBytes).Equals(len(payload)) + + response := make([]byte, 1024) + nBytes, err = conn.Read(response) + assert.Error(err).IsNil() + assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert.Error(conn.Close()).IsNil() + } + + CloseAllServers() +}