From 8474a5a1563643c52df176a31f4114d23efa61a6 Mon Sep 17 00:00:00 2001 From: jeessy2 <6205259+jeessy2@users.noreply.github.com> Date: Thu, 28 Mar 2024 03:13:55 -0700 Subject: [PATCH] fix: when local dns error occurs try every dns (#1063) --- main.go | 4 ++-- util/net_resolver.go | 8 +++----- util/wait_internet.go | 7 ++++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index c437c64aa..b32fc1777 100644 --- a/main.go +++ b/main.go @@ -136,8 +136,8 @@ func run() { }() } - // 初始化默认DNS - util.InitDefaultDNS(*customDNS, conf.Lang) + // 初始化备用DNS + util.InitBackupDNS(*customDNS, conf.Lang) // 等待网络连接 util.WaitInternet(dns.Addresses) diff --git a/util/net_resolver.go b/util/net_resolver.go index 2a62ce79c..42d7f87ff 100644 --- a/util/net_resolver.go +++ b/util/net_resolver.go @@ -8,20 +8,18 @@ import ( ) // BackupDNS will be used if DNS error occurs. -var BackupDNS = []string{} +var BackupDNS = []string{"1.1.1.1", "8.8.8.8", "9.9.9.9", "223.5.5.5"} -func InitDefaultDNS(customDNS, lang string) { +func InitBackupDNS(customDNS, lang string) { if customDNS != "" { BackupDNS = []string{customDNS} return } if lang == language.Chinese.String() { - BackupDNS = []string{"223.5.5.5", "114.114.114.114"} - return + BackupDNS = []string{"223.5.5.5", "114.114.114.114", "119.29.29.29"} } - BackupDNS = []string{"1.1.1.1", "8.8.8.8"} } // SetDNS sets the dialer.Resolver to use the given DNS server. diff --git a/util/wait_internet.go b/util/wait_internet.go index 897144acc..82f30a6c3 100644 --- a/util/wait_internet.go +++ b/util/wait_internet.go @@ -1,7 +1,6 @@ package util import ( - "math/rand" "strings" "time" ) @@ -14,6 +13,7 @@ import ( // - https://github.com/ddev/ddev/blob/v1.22.7/pkg/globalconfig/global_config.go#L776 func WaitInternet(addresses []string) { delay := time.Second * 5 + retryTimes := 0 for { for _, addr := range addresses { @@ -27,10 +27,11 @@ func WaitInternet(addresses []string) { Log("等待网络连接: %s", err) Log("%s 后重试...", delay) - if isDNSErr(err) && len(BackupDNS) > 0 { - dns := BackupDNS[rand.Intn(len(BackupDNS))] + if isDNSErr(err) || retryTimes > 0 { + dns := BackupDNS[retryTimes%len(BackupDNS)] Log("本机DNS异常! 将默认使用 %s, 可参考文档通过 -dns 自定义 DNS 服务器", dns) SetDNS(dns) + retryTimes = retryTimes + 1 } time.Sleep(delay)