fix: when local dns error occurs try every dns (#1063)

This commit is contained in:
jeessy2
2024-03-28 03:13:55 -07:00
committed by GitHub
parent 89199cd18a
commit 8474a5a156
3 changed files with 9 additions and 10 deletions

View File

@ -136,8 +136,8 @@ func run() {
}()
}
// 初始化默认DNS
util.InitDefaultDNS(*customDNS, conf.Lang)
// 初始化备用DNS
util.InitBackupDNS(*customDNS, conf.Lang)
// 等待网络连接
util.WaitInternet(dns.Addresses)

View File

@ -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.

View File

@ -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)