From c5ff55a193a7b7dd32de06ed1dc8c886c2af0b31 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Fri, 19 Feb 2021 10:16:58 -0800 Subject: [PATCH 1/2] find dns from any number of providers --- src/models/constants.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/models/constants.go b/src/models/constants.go index ea3d6dc..5f57c26 100644 --- a/src/models/constants.go +++ b/src/models/constants.go @@ -19,13 +19,13 @@ var ( func init() { var err error - DEFAULT_RELAY, err = lookupIP(DEFAULT_RELAY) + DEFAULT_RELAY, err = lookupIPs(DEFAULT_RELAY) if err == nil { DEFAULT_RELAY += ":" + DEFAULT_PORT } else { DEFAULT_RELAY = "" } - DEFAULT_RELAY6, err = lookupIP(DEFAULT_RELAY6) + DEFAULT_RELAY6, err = lookupIPs(DEFAULT_RELAY6) if err == nil { DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT } else { @@ -33,14 +33,32 @@ func init() { } } -func lookupIP(address string) (ipaddress string, err error) { +func lookupIPs(address string) (ipaddress string, err error) { + var publicDns = []string{"1.1.1.1", "8.8.8.8", "8.8.4.4", "1.0.0.1", "8.26.56.26", "208.67.222.222", "208.67.220.220"} + result := make(chan string, len(publicDns)) + for _, dns := range publicDns { + go func(dns string) { + s, _ := lookupIP(address, dns) + result <- s + }(dns) + } + for i := 0; i < len(publicDns); i++ { + ipaddress = <-result + if ipaddress != "" { + return + } + } + return +} + +func lookupIP(address, dns string) (ipaddress string, err error) { r := &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ Timeout: time.Millisecond * time.Duration(10000), } - return d.DialContext(ctx, "udp", "1.1.1.1:53") + return d.DialContext(ctx, "udp", dns+":53") }, } ip, err := r.LookupHost(context.Background(), address) From cd2802b8b524cea7ca357512632f0a1013f6f543 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Fri, 19 Feb 2021 10:18:13 -0800 Subject: [PATCH 2/2] shorter dns fail time --- src/models/constants.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/constants.go b/src/models/constants.go index 5f57c26..329f69e 100644 --- a/src/models/constants.go +++ b/src/models/constants.go @@ -56,7 +56,7 @@ func lookupIP(address, dns string) (ipaddress string, err error) { PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ - Timeout: time.Millisecond * time.Duration(10000), + Timeout: time.Millisecond * time.Duration(1000), } return d.DialContext(ctx, "udp", dns+":53") },