From 3eb13868f269329715df32bc264b1b13ff92e46c Mon Sep 17 00:00:00 2001 From: CalmLong <37164399+CalmLong@users.noreply.github.com> Date: Sat, 2 Jan 2021 15:48:13 +0800 Subject: [PATCH] Feature: android binary runtime default dns set 8.8.8.8:53 (#572) ref https://github.com/golang/go/issues/8877 ref https://github.com/v2ray/v2ray-core/issues/1909 --- infra/conf/dns.go | 4 ++++ infra/conf/dns_bootstrap.go | 9 +++++++++ infra/conf/dns_bootstrap_android.go | 25 +++++++++++++++++++++++++ infra/conf/dns_bootstrap_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 infra/conf/dns_bootstrap.go create mode 100644 infra/conf/dns_bootstrap_android.go create mode 100644 infra/conf/dns_bootstrap_test.go diff --git a/infra/conf/dns.go b/infra/conf/dns.go index d45bc1f940a..506747cf717 100644 --- a/infra/conf/dns.go +++ b/infra/conf/dns.go @@ -158,6 +158,10 @@ func (c *DNSConfig) Build() (*dns.Config, error) { config.NameServer = append(config.NameServer, ns) } + if BootstrapDNS() { + newError("Bootstrap DNS: ", bootstrapDNS).AtWarning().WriteToLog() + } + if c.Hosts != nil && len(c.Hosts) > 0 { domains := make([]string, 0, len(c.Hosts)) for domain := range c.Hosts { diff --git a/infra/conf/dns_bootstrap.go b/infra/conf/dns_bootstrap.go new file mode 100644 index 00000000000..3f741705cb1 --- /dev/null +++ b/infra/conf/dns_bootstrap.go @@ -0,0 +1,9 @@ +// +build !android + +package conf + +const bootstrapDNS = "" + +func BootstrapDNS() bool { + return false +} diff --git a/infra/conf/dns_bootstrap_android.go b/infra/conf/dns_bootstrap_android.go new file mode 100644 index 00000000000..cbb8a922f01 --- /dev/null +++ b/infra/conf/dns_bootstrap_android.go @@ -0,0 +1,25 @@ +// +build android + +package conf + +import ( + "context" + "net" +) + +const bootstrapDNS = "8.8.8.8:53" + +func BootstrapDNS() bool { + var dialer net.Dialer + net.DefaultResolver = &net.Resolver{ + PreferGo: false, + Dial: func(context context.Context, _, _ string) (net.Conn, error) { + conn, err := dialer.DialContext(context, "udp", bootstrapDNS) + if err != nil { + return nil, err + } + return conn, nil + }, + } + return true +} diff --git a/infra/conf/dns_bootstrap_test.go b/infra/conf/dns_bootstrap_test.go new file mode 100644 index 00000000000..95e5ecb2c3c --- /dev/null +++ b/infra/conf/dns_bootstrap_test.go @@ -0,0 +1,28 @@ +package conf + +import ( + "context" + "net" + "testing" +) + +func TestBootstrapDNS(t *testing.T) { + const ( + defaultNS = "8.8.8.8:53" + domain = "github.com" + ) + var dialer net.Dialer + net.DefaultResolver = &net.Resolver{ + PreferGo: true, + Dial: func(context context.Context, network, address string) (net.Conn, error) { + conn, err := dialer.DialContext(context, "udp", defaultNS) + if err != nil { + return nil, err + } + return conn, nil + }, + } + if ips, err := net.LookupIP(domain); len(ips) == 0 { + t.Error("set BootstrapDNS failed: ", err) + } +}