Skip to content
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

Merged
merged 7 commits into from
Mar 26, 2024
Merged

Conversation

jeessy2
Copy link
Owner

@jeessy2 jeessy2 commented Mar 25, 2024

What does this PR do?

fix: #1056

Motivation

默认1.1.1.1可能会把自定义的dns覆盖,有些地区1.1.1.1可能不通

Additional Notes

@jeessy2
Copy link
Owner Author

jeessy2 commented Mar 25, 2024

@WaterLemons2k 有空看下

Copy link
Contributor

@WaterLemons2k WaterLemons2k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然和我最初的设想不同,但还是谢谢你。就这么做吧。

Comment on lines 34 to 43
// 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"}
}
Copy link
Contributor

@WaterLemons2k WaterLemons2k Mar 26, 2024

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
Comment on lines 139 to 140
// 等待网络连接
util.WaitInternet(dns.Addresses, util.GetDefaultDNS(conf.Lang, *customDNS))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉一个检查网络的函数还得提前把备用 DNS 传进去很奇怪;维护一个 DNS 列表,如果传了自定义 DNS 把自定义加进去,否则根据语言把默认 DNS 加进去。这样应该好些。

Suggested change
// 等待网络连接
util.WaitInternet(dns.Addresses, util.GetDefaultDNS(conf.Lang, *customDNS))
util.AddDNS(conf.Lang, *customDNS)
// 等待网络连接
util.WaitInternet(dns.Addresses)

Copy link
Contributor

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...)
}

Copy link
Owner Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也许可以随机选一个地址?

Suggested change
for _, addr := range addresses {
addr := addresses[rand.Intn(len(addresses))]

Comment on lines 30 to 31
if isLoopbackErr(err) && times >= 10 {
dns := fallbackDNS[times%len(fallbackDNS)]
Copy link
Contributor

@WaterLemons2k WaterLemons2k Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DNS 有问题时随机从列表中选一个 DNS?

Suggested change
if isLoopbackErr(err) && times >= 10 {
dns := fallbackDNS[times%len(fallbackDNS)]
if isDNSErr(err) && !fallback {
dns := DNSList[rand.Intn(len(DNSList))]

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback这个条件也可以不用了吧?

Copy link
Contributor

@WaterLemons2k WaterLemons2k Mar 26, 2024

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);结果本机不可用,然后就会不断回落。(虽然可能性很低)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

手动设置-dns ::1回落暂时不管,会间隔5秒提示

Comment on lines 42 to 44
// isLoopbackErr checks if the error is a loopback error.
func isLoopbackErr(e error) bool {
return strings.Contains(e.Error(), "[::1]:53")
Copy link
Contributor

@WaterLemons2k WaterLemons2k Mar 26, 2024

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 判断是否已回落了。

Suggested change
// 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")

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要是这里,以前判断过于太宽松了。

@jeessy2 jeessy2 merged commit 4623bb5 into master Mar 26, 2024
4 checks passed
@jeessy2 jeessy2 deleted the wait branch March 26, 2024 06:30
Comment on lines +10 to +13
// dnsList is a list of DNS servers.
var DNSList = []string{}

func InitDefaultDNS(customDNS, lang string) {
Copy link
Contributor

@WaterLemons2k WaterLemons2k Mar 26, 2024

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"}
	}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

openwrt 重启路由器,提示等待网络连接
2 participants