Skip to content

Commit

Permalink
fix: when local dns error occurs try every dns (#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 authored Mar 28, 2024
1 parent 89199cd commit 8474a5a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func run() {
}()
}

// 初始化默认DNS
util.InitDefaultDNS(*customDNS, conf.Lang)
// 初始化备用DNS
util.InitBackupDNS(*customDNS, conf.Lang)

// 等待网络连接
util.WaitInternet(dns.Addresses)
Expand Down
8 changes: 3 additions & 5 deletions util/net_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions util/wait_internet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"math/rand"
"strings"
"time"
)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 8474a5a

Please sign in to comment.