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

feat: custom DNS server #682

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var noWebService = flag.Bool("noweb", false, "不启动 web 服务")
// 跳过验证证书
var skipVerify = flag.Bool("skipVerify", false, "跳过验证证书, 适合不能升级的老系统")

// 自定义 DNS 服务器
var customDNSServer = flag.String("dns", "", "自定义 DNS 服务器(例如 1.1.1.1)")

//go:embed static
var staticEmbededFiles embed.FS

Expand All @@ -47,9 +50,6 @@ var faviconEmbededFile embed.FS
// version
var version = "DEV"

// buildTime
var buildTime = ""

func main() {
flag.Parse()
if _, err := net.ResolveTCPAddr("tcp", *listen); err != nil {
Expand All @@ -63,6 +63,9 @@ func main() {
if *skipVerify {
os.Setenv(util.SkipVerifyENV, "true")
}
if *customDNSServer != "" {
os.Setenv(util.DNSServerEnv, *customDNSServer+":53")
}
switch *serviceType {
case "install":
installService()
Expand Down
23 changes: 23 additions & 0 deletions util/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"context"
"net"
"os"
)

const DNSServerEnv = "DDNS_GO_DNS_SERVER"

// customDNSResolver 当 DNSServerEnv 值不为空时,使用 Go 内置 DNS 解析器来解析其 DNS 服务器。
func customDNSResolver() *net.Resolver {
s := os.Getenv(DNSServerEnv)
if s != "" {
return &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", s)
},
}
}
return &net.Resolver{}
}
16 changes: 16 additions & 0 deletions util/dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import (
"context"
"os"
"testing"
)

// TestCustomDNSResolver 测试能否通过 DNSServerEnv 值的 DNS 服务器解析域名的 IP。
func TestCustomDNSResolver(t *testing.T) {
os.Setenv(DNSServerEnv, "1.1.1.1:53")
_, err := customDNSResolver().LookupIP(context.Background(), "ip", "cloudflare.com")
if err != nil {
t.Errorf("Failed to lookup IP, err: %v", err)
}
}
2 changes: 2 additions & 0 deletions util/http_client_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var defaultTransport = &http.Transport{

// CreateHTTPClient Create Default HTTP Client
func CreateHTTPClient() *http.Client {
dialer.Resolver = customDNSResolver()
// SkipVerfiry
defaultTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: os.Getenv(SkipVerifyENV) == "true"}
return &http.Client{
Expand Down Expand Up @@ -73,6 +74,7 @@ var noProxyTcp6Transport = &http.Transport{

// CreateNoProxyHTTPClient Create NoProxy HTTP Client
func CreateNoProxyHTTPClient(network string) *http.Client {
dialer.Resolver = customDNSResolver()
if network == "tcp6" {
// SkipVerfiry
noProxyTcp6Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: os.Getenv(SkipVerifyENV) == "true"}
Expand Down