From c27050ad9051dfa89b45668617a13faea747df14 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 21 Feb 2019 00:03:31 +0100 Subject: [PATCH] fix usability for both IPv4 and v6 in static dns mapping --- app/dns/hosts.go | 5 +++++ app/dns/hosts_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/dns/hosts.go b/app/dns/hosts.go index 267f40278..b4d18ff1e 100644 --- a/app/dns/hosts.go +++ b/app/dns/hosts.go @@ -80,6 +80,11 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning() } + // Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping. + if len(ips) == 1 && ips[0] == net.LocalHostIP { + ips = append(ips, net.LocalHostIPv6) + } + sh.ips[id] = ips } diff --git a/app/dns/hosts_test.go b/app/dns/hosts_test.go index e6a819186..2bb65cb5c 100644 --- a/app/dns/hosts_test.go +++ b/app/dns/hosts_test.go @@ -7,6 +7,7 @@ import ( . "v2ray.com/core/app/dns" "v2ray.com/core/common" + "v2ray.com/core/common/net" ) func TestStaticHosts(t *testing.T) { @@ -25,6 +26,13 @@ func TestStaticHosts(t *testing.T) { {2, 2, 2, 2}, }, }, + { + Type: DomainMatchingType_Subdomain, + Domain: "baidu.com", + Ip: [][]byte{ + {127, 0, 0, 1}, + }, + }, } hosts, err := NewStaticHosts(pb, nil) @@ -55,4 +63,17 @@ func TestStaticHosts(t *testing.T) { t.Error(diff) } } + + { + ips := hosts.LookupIP("baidu.com", IPOption{ + IPv4Enable: false, + IPv6Enable: true, + }) + if len(ips) != 1 { + t.Error("expect 1 IP, but got ", len(ips)) + } + if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" { + t.Error(diff) + } + } }