mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 15:33:46 +08:00
fix: when local dns error occurs try every dns (#1063)
This commit is contained in:
4
main.go
4
main.go
@ -136,8 +136,8 @@ func run() {
|
||||
}()
|
||||
}
|
||||
|
||||
// 初始化默认DNS
|
||||
util.InitDefaultDNS(*customDNS, conf.Lang)
|
||||
// 初始化备用DNS
|
||||
util.InitBackupDNS(*customDNS, conf.Lang)
|
||||
|
||||
// 等待网络连接
|
||||
util.WaitInternet(dns.Addresses)
|
||||
|
@ -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.
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user