mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 15:33:46 +08:00
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:
@ -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)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user