-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
wait_internet.go
50 lines (42 loc) · 1.03 KB
/
wait_internet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package util
import (
"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
retryTimes := 0
failed := false
for {
for _, addr := range addresses {
err := LookupHost(addr)
// Internet is connected.
if err == nil {
if failed {
Log("网络已连接")
}
return
}
failed = true
Log("等待网络连接: %s", err)
Log("%s 后重试...", delay)
if isDNSErr(err) || retryTimes > 0 {
dns := BackupDNS[retryTimes%len(BackupDNS)]
Log("本机DNS异常! 将默认使用 %s, 可参考文档通过 -dns 自定义 DNS 服务器", dns)
SetDNS(dns)
retryTimes = retryTimes + 1
}
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")
}