Skip to content

Commit

Permalink
feat: add DNS-over-TCP support to customize DNS servers (#1217)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Zerorigin authored Aug 9, 2024
1 parent ce75576 commit 3257d07
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions util/net_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package util
import (
"context"
"net"
"net/url"
"strings"

"golang.org/x/text/language"
)
Expand All @@ -24,14 +26,29 @@ func InitBackupDNS(customDNS, lang string) {

// SetDNS sets the dialer.Resolver to use the given DNS server.
func SetDNS(dns string) {
// Error means that the given DNS doesn't have a port. Add it.
if _, _, err := net.SplitHostPort(dns); err != nil {
dns = net.JoinHostPort(dns, "53")

if !strings.Contains(dns, "://") {
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{
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)
},
}
Expand Down

0 comments on commit 3257d07

Please sign in to comment.