From 2129c6e1e9a61dd37df8ae5013588b57a39a2ae0 Mon Sep 17 00:00:00 2001 From: rurirei <72071920+rurirei@users.noreply.github.com> Date: Wed, 5 May 2021 05:22:01 +0800 Subject: [PATCH] feat: update bootstrap_dns_android (#966) * update with non_specified network * remove wrongs * add alternative bootstrapDialer * do test * Update dns_bootstrap_android_test.go * example of alternative dialer * public method * move const * Update dns_bootstrap_android_test.go * no duplicated * Rename infra/conf/dns_bootstrap_android.go to transport/internet/system_dns_android.go * Update system_dns_android.go * Update and rename infra/conf/dns_bootstrap_android_test.go to transport/internet/system_dns_android_test.go * no imports * Update system_dns_android.go * Update system_dns_android_test.go * create systemDNS * Create system_dns_android.go * Update system_dialer.go * call Resolver on systemDialer * create system_dns_test.go * resolver.LookupIP params * param fix * noneed TestSystemDNSResolver * revert: no specified resolver * Delete system_dns.go * android only * android only test * typo --- infra/conf/dns_bootstrap_android.go | 24 ---------------- infra/conf/dns_bootstrap_test.go | 28 ------------------- transport/internet/system_dns_android.go | 26 +++++++++++++++++ transport/internet/system_dns_android_test.go | 15 ++++++++++ 4 files changed, 41 insertions(+), 52 deletions(-) delete mode 100644 infra/conf/dns_bootstrap_android.go delete mode 100644 infra/conf/dns_bootstrap_test.go create mode 100644 transport/internet/system_dns_android.go create mode 100644 transport/internet/system_dns_android_test.go diff --git a/infra/conf/dns_bootstrap_android.go b/infra/conf/dns_bootstrap_android.go deleted file mode 100644 index 0d56bbe89..000000000 --- a/infra/conf/dns_bootstrap_android.go +++ /dev/null @@ -1,24 +0,0 @@ -// +build android - -package conf - -import ( - "context" - "net" -) - -func init() { - const bootstrapDNS = "8.8.8.8:53" - var dialer net.Dialer - net.DefaultResolver = &net.Resolver{ - PreferGo: true, - 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 - }, - } - newError("Bootstrap DNS: ", bootstrapDNS).AtWarning().WriteToLog() -} diff --git a/infra/conf/dns_bootstrap_test.go b/infra/conf/dns_bootstrap_test.go deleted file mode 100644 index 95e5ecb2c..000000000 --- a/infra/conf/dns_bootstrap_test.go +++ /dev/null @@ -1,28 +0,0 @@ -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) - } -} diff --git a/transport/internet/system_dns_android.go b/transport/internet/system_dns_android.go new file mode 100644 index 000000000..5591d811c --- /dev/null +++ b/transport/internet/system_dns_android.go @@ -0,0 +1,26 @@ +// +build android + +package internet + +import ( + "context" + "net" +) + +const SystemDNS = "8.8.8.8:53" + +type DNSResolverFunc func() *net.Resolver + +var NewDNSResolver DNSResolverFunc = func() *net.Resolver { + return &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { + var dialer net.Dialer + return dialer.DialContext(ctx, network, SystemDNS) + }, + } +} + +func init() { + net.DefaultResolver = NewDNSResolver() +} diff --git a/transport/internet/system_dns_android_test.go b/transport/internet/system_dns_android_test.go new file mode 100644 index 000000000..d019c0391 --- /dev/null +++ b/transport/internet/system_dns_android_test.go @@ -0,0 +1,15 @@ +// +build android + +package internet + +import ( + "context" + "testing" +) + +func TestDNSResolver(t *testing.T) { + resolver := NewDNSResolver() + if ips, err := resolver.LookupIP(context.Background(), "ip", "www.google.com"); err != nil { + t.Errorf("failed to lookupIP, %v, %v", ips, err) + } +}