Skip to content

Commit

Permalink
refactor: Waiting for network connection when dns exception (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 authored Mar 26, 2024
1 parent 2699a61 commit 4623bb5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 65 deletions.
5 changes: 1 addition & 4 deletions dns/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/jeessy2/ddns-go/v6/config"
"github.com/jeessy2/ddns-go/v6/dns/internet"
"github.com/jeessy2/ddns-go/v6/util"
)

Expand All @@ -16,7 +15,7 @@ type DNS interface {
}

var (
addresses = []string{
Addresses = []string{
alidnsEndpoint,
baiduEndpoint,
zonesAPI,
Expand All @@ -35,8 +34,6 @@ var (

// RunTimer 定时运行
func RunTimer(delay time.Duration) {
internet.Wait(addresses)

for {
RunOnce()
time.Sleep(delay)
Expand Down
56 changes: 0 additions & 56 deletions dns/internet/wait.go

This file was deleted.

16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var noWebService = flag.Bool("noweb", false, "No web service")
var skipVerify = flag.Bool("skipVerify", false, "Skip certificate verification")

// 自定义 DNS 服务器
var customDNSServer = flag.String("dns", "", "Custom DNS server, example: 8.8.8.8")
var customDNS = flag.String("dns", "", "Custom DNS server, example: 8.8.8.8")

//go:embed static
var staticEmbeddedFiles embed.FS
Expand Down Expand Up @@ -83,8 +83,8 @@ func main() {
if *skipVerify {
util.SetInsecureSkipVerify()
}
if *customDNSServer != "" {
util.SetDNS(*customDNSServer)
if *customDNS != "" {
util.SetDNS(*customDNS)
}
os.Setenv(util.IPCacheTimesENV, strconv.Itoa(*ipCacheTimes))
switch *serviceType {
Expand Down Expand Up @@ -136,6 +136,12 @@ func run() {
}()
}

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

// 等待网络连接
util.WaitInternet(dns.Addresses)

// 定时运行
dns.RunTimer(time.Duration(*every) * time.Second)
}
Expand Down Expand Up @@ -221,8 +227,8 @@ func getService() service.Service {
svcConfig.Arguments = append(svcConfig.Arguments, "-skipVerify")
}

if *customDNSServer != "" {
svcConfig.Arguments = append(svcConfig.Arguments, "-dns", *customDNSServer)
if *customDNS != "" {
svcConfig.Arguments = append(svcConfig.Arguments, "-dns", *customDNS)
}

prg := &program{}
Expand Down
19 changes: 19 additions & 0 deletions util/net_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,27 @@ package util
import (
"context"
"net"

"golang.org/x/text/language"
)

// dnsList is a list of DNS servers.
var DNSList = []string{}

func InitDefaultDNS(customDNS, lang string) {
if customDNS != "" {
DNSList = []string{customDNS}
return
}

if lang == language.Chinese.String() {
DNSList = []string{"223.5.5.5", "114.114.114.114"}
return
}

DNSList = []string{"1.1.1.1", "8.8.8.8"}
}

// SetDNS sets the dialer.Resolver to use the given DNS server.
func SetDNS(dns string) {
// Error means that the given DNS doesn't have a port. Add it.
Expand Down
44 changes: 44 additions & 0 deletions util/wait_internet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package util

import (
"math/rand"
"strings"
"time"
)

// Wait blocks until the Internet is connected.
//
// See also:
//
// - https://stackoverflow.com/a/50058255
// - https://github.com/ddev/ddev/blob/v1.22.7/pkg/globalconfig/global_config.go#L776
func WaitInternet(addresses []string) {
delay := time.Second * 5

for {
for _, addr := range addresses {

err := LookupHost(addr)
// Internet is connected.
if err == nil {
return
}

Log("等待网络连接: %s", err)
Log("%s 后重试...", delay)

if isDNSErr(err) && len(DNSList) > 0 {
dns := DNSList[rand.Intn(len(DNSList))]
Log("本机DNS异常! 将默认使用 %s, 可参考文档通过 -dns 自定义 DNS 服务器", dns)
SetDNS(dns)
}

time.Sleep(delay)
}
}
}

// isDNSErr checks if the error is caused by DNS.
func isDNSErr(e error) bool {
return strings.Contains(e.Error(), "[::1]:53: read: connection refused")
}

0 comments on commit 4623bb5

Please sign in to comment.