-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Waiting for network connection when dns exception #1057
Conversation
@WaterLemons2k 有空看下 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
虽然和我最初的设想不同,但还是谢谢你。就这么做吧。
util/net_resolver.go
Outdated
// GetDefaultDNS returns the default DNS servers based on the given language and custom DNS. | ||
func GetDefaultDNS(lang string, customDNS string) []string { | ||
if customDNS != "" { | ||
return []string{customDNS} | ||
} | ||
if lang == language.Chinese.String() { | ||
return []string{"223.5.5.5", "114.114.114.114"} | ||
} | ||
return []string{"1.1.1.1", "8.8.8.8"} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以维护一个根据语言选择 DNS 的 map;从 map 中取值,取不到则回退到 English。
// dnsByLang is a map of language to default DNS servers.
var dnsByLang = map[string][]string{
language.Chinese.String(): {"114.114.114.114", "223.5.5.5"},
language.English.String(): {"1.1.1.1", "8.8.8.8"},
}
// getDNSByLang returns the default DNS servers for the given language.
//
// If the language is not supported, English will be used.
func getDNSByLang(lang string) []string {
if dns, ok := dnsByLang[lang]; ok {
return dns
}
return dnsByLang[language.English.String()]
}
main.go
Outdated
// 等待网络连接 | ||
util.WaitInternet(dns.Addresses, util.GetDefaultDNS(conf.Lang, *customDNS)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉一个检查网络的函数还得提前把备用 DNS 传进去很奇怪;维护一个 DNS 列表,如果传了自定义 DNS 把自定义加进去,否则根据语言把默认 DNS 加进去。这样应该好些。
// 等待网络连接 | |
util.WaitInternet(dns.Addresses, util.GetDefaultDNS(conf.Lang, *customDNS)) | |
util.AddDNS(conf.Lang, *customDNS) | |
// 等待网络连接 | |
util.WaitInternet(dns.Addresses) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
维护一个 DNS 列表,WaitInternet
需要备用 DNS 时就从 DNSList
里拿。
感觉整个 DNS 部分应该放到单独的文件中,已经和 net.Resolver
没多大关系了。
// DNSList is a list of DNS servers.
var DNSList = []string{}
func AddDNS(dns, lang string) {
result := []string{dns}
if dns == "" {
result = getDNSByLang(lang)
}
DNSList = append(DNSList, result...)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
根据思路,简化了
var times = 0 | ||
|
||
for { | ||
for _, addr := range addresses { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
也许可以随机选一个地址?
for _, addr := range addresses { | |
addr := addresses[rand.Intn(len(addresses))] |
util/wait_internet.go
Outdated
if isLoopbackErr(err) && times >= 10 { | ||
dns := fallbackDNS[times%len(fallbackDNS)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DNS 有问题时随机从列表中选一个 DNS?
if isLoopbackErr(err) && times >= 10 { | |
dns := fallbackDNS[times%len(fallbackDNS)] | |
if isDNSErr(err) && !fallback { | |
dns := DNSList[rand.Intn(len(DNSList))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fallback这个条件也可以不用了吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fallback这个条件也可以不用了吧?
只是为了确保回落只会发生一次。
主要是为了防止有人可能会设置自定义 DNS 为本机 DNS(-dns ::1
);结果本机不可用,然后就会不断回落。(虽然可能性很低)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
手动设置-dns ::1
回落暂时不管,会间隔5秒提示
util/wait_internet.go
Outdated
// isLoopbackErr checks if the error is a loopback error. | ||
func isLoopbackErr(e error) bool { | ||
return strings.Contains(e.Error(), "[::1]:53") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一块是我当初判断太宽松了;觉得只要 DNS 是本机就有问题,其实指不定是在 DNS 服务器上运行的呢。
因此,只有当 DNS 为本机但无法连接时才为 DNS 错误。
这样改完之后应该就没有误判了,也就可以改回用 bool 判断是否已回落了。
// isLoopbackErr checks if the error is a loopback error. | |
func isLoopbackErr(e error) bool { | |
return strings.Contains(e.Error(), "[::1]:53") | |
// isDNSErr checks if the error is caused by DNS. | |
func isDNSErr(e error) bool { | |
return strings.Contains(e.Error(), "[::1]:53: read: connection refused") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要是这里,以前判断过于太宽松了。
// dnsList is a list of DNS servers. | ||
var DNSList = []string{} | ||
|
||
func InitDefaultDNS(customDNS, lang string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我又想了一下,感觉 DNSList
这个命名不太好,有点随意了;看不太出来这个列表是拿来干什么用的。要不换个名字,BackupDNS
怎么样?
// BackupDNS will be used if DNS error occurs.
var BackupDNS = []string{}
// InitBackupDNS initializes BackupDNS based on the given custom DNS.
// If custom DNS is empty, BackupDNS will be initialized based on the language.
func InitBackupDNS(customDNS, lang string) {
switch {
case customDNS != "":
BackupDNS = []string{customDNS}
case lang == language.Chinese.String():
BackupDNS = []string{"223.5.5.5", "114.114.114.114"}
default:
BackupDNS = []string{"1.1.1.1", "8.8.8.8"}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以
What does this PR do?
fix: #1056
Motivation
默认1.1.1.1可能会把自定义的dns覆盖,有些地区1.1.1.1可能不通
Additional Notes