-
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
perf: check internet with DNS lookup #1034
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Package internet implements utilities for checking the Internet connection. | ||
package internet | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/jeessy2/ddns-go/v6/util" | ||
) | ||
|
||
const ( | ||
// fallbackDNS used when a fallback occurs. | ||
fallbackDNS = "1.1.1.1" | ||
|
||
// delay is the delay time for each DNS lookup. | ||
delay = time.Second * 5 | ||
) | ||
|
||
// 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 Wait(addresses []string) { | ||
// fallbase in case loopback DNS is unavailable and only once. | ||
fallback := false | ||
|
||
for { | ||
for _, addr := range addresses { | ||
err := util.LookupHost(addr) | ||
// Internet is connected. | ||
if err == nil { | ||
return | ||
} | ||
|
||
if !fallback && isLoopback(err) { | ||
util.Log("本机DNS异常! 将默认使用 %s, 可参考文档通过 -dns 自定义 DNS 服务器", fallbackDNS) | ||
util.SetDNS(fallbackDNS) | ||
|
||
fallback = true | ||
continue | ||
} | ||
|
||
util.Log("等待网络连接: %s", err) | ||
|
||
util.Log("%s 后重试...", delay) | ||
time.Sleep(delay) | ||
} | ||
} | ||
} | ||
|
||
// isLoopback checks if the error is a loopback error. | ||
func isLoopback(e error) bool { | ||
return strings.Contains(e.Error(), "[::1]:53") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
import "testing" | ||
|
||
const ( | ||
testDNS = "1.1.1.1" | ||
testURL = "https://cloudflare.com" | ||
) | ||
|
||
// TestNewDialerResolver 测试传递 DNS 服务器地址时能否设置 dialer.Resolver。 | ||
func TestNewDialerResolver(t *testing.T) { | ||
// 测试前重置以确保正常设置 | ||
dialer.Resolver = nil | ||
func TestSetDNS(t *testing.T) { | ||
SetDNS(testDNS) | ||
|
||
NewDialerResolver("1.1.1.1:53") | ||
if dialer.Resolver == nil { | ||
t.Error("Failed to set dialer.Resolver") | ||
} | ||
|
||
// 测试后重置以确保与测试前的值一致 | ||
dialer.Resolver = nil | ||
} | ||
|
||
// TestNewNetResolver 测试能否通过 newNetResolver 返回的 net.Resolver 解析域名的 IP。 | ||
func TestNewNetResolver(t *testing.T) { | ||
_, err := newNetResolver("1.1.1.1:53").LookupIP(context.Background(), "ip", "cloudflare.com") | ||
if err != nil { | ||
t.Errorf("Failed to lookup IP, err: %v", err) | ||
} | ||
func TestLookupHost(t *testing.T) { | ||
t.Run("Valid URL", func(t *testing.T) { | ||
if err := LookupHost(testURL); err != nil { | ||
t.Errorf("Expected nil error, got %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Invalid URL", func(t *testing.T) { | ||
if err := LookupHost("invalidurl"); err == nil { | ||
t.Error("Expected error, got nil") | ||
} | ||
}) | ||
|
||
t.Run("After SetDNS", func(t *testing.T) { | ||
SetDNS(testDNS) | ||
|
||
if err := LookupHost(testURL); err != nil { | ||
t.Errorf("Expected nil error, got %v", err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package util | ||
|
||
import "testing" | ||
|
||
func TestWriteString(t *testing.T) { | ||
tests := []struct { | ||
input []string | ||
expected string | ||
}{ | ||
{[]string{"hello", "world"}, "helloworld"}, | ||
{[]string{"", "test"}, "test"}, | ||
{[]string{"hello", " ", "world"}, "hello world"}, | ||
{[]string{""}, ""}, | ||
} | ||
|
||
for _, tt := range tests { | ||
result := WriteString(tt.input...) | ||
if result != tt.expected { | ||
t.Errorf("Expected %s, but got %s", tt.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func TestToHostname(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"With https scheme", "https://www.example.com", "www.example.com"}, | ||
{"With path", "www.example.com/path", "www.example.com"}, | ||
{"With https scheme and path", "https://www.example.com/path", "www.example.com"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := toHostname(tt.input) | ||
if result != tt.expected { | ||
t.Errorf("Expected %s, but got %s", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
是不是这里的,默认1.1.1.1可能会把自定义的dns覆盖,有些地区1.1.1.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 比较好。
另外,感觉是不是应该更严格一点,失败多次再回落?
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.
可以考虑失败多次再回落,回落可以多个,国内主流的,如
223.5.5.5
114.114.114.114
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,根据浏览器语言来回落不同DNS?
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 并不是一个网络工具该做的。
现在,为了让 DNS 在没有
/etc/resolv.conf
文件的环境中正常工作,引入了太多不必要的复杂性(DNS 不可用时自动回落、维护国内/国外 DNS 列表等);还破坏了其他用户的体验,所以是时候停止了。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.
内置也还行吧。系统虽然复杂点,减少用户操作复杂度。不过可以简化下系统复杂度,达到更好效果