feat: add DNS-over-TCP support to customize DNS servers (#1217)

* feat: add DNS-over-TCP & custom Port support to customize DNS servers

* feat: add DNS-over-TCP support to customize DNS servers

* fix: wrong dns string concatenation operator.
This commit is contained in:
Zerorigin
2024-08-09 09:55:50 +08:00
committed by GitHub
parent ce755764e8
commit 3257d0776b

View File

@ -3,6 +3,8 @@ package util
import ( import (
"context" "context"
"net" "net"
"net/url"
"strings"
"golang.org/x/text/language" "golang.org/x/text/language"
) )
@ -24,14 +26,29 @@ func InitBackupDNS(customDNS, lang string) {
// SetDNS sets the dialer.Resolver to use the given DNS server. // SetDNS sets the dialer.Resolver to use the given DNS server.
func SetDNS(dns string) { func SetDNS(dns string) {
// Error means that the given DNS doesn't have a port. Add it.
if _, _, err := net.SplitHostPort(dns); err != nil { if !strings.Contains(dns, "://") {
dns = net.JoinHostPort(dns, "53") dns = "udp://" + dns
}
svrParse, _ := url.Parse(dns)
var network string
switch strings.ToLower(svrParse.Scheme) {
case "tcp":
network = "tcp"
default:
network = "udp"
}
if svrParse.Port() == "" {
dns = net.JoinHostPort(svrParse.Host, "53")
} else {
dns = svrParse.Host
} }
dialer.Resolver = &net.Resolver{ dialer.Resolver = &net.Resolver{
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, _, address string) (net.Conn, error) {
return net.Dial(network, dns) return net.Dial(network, dns)
}, },
} }