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

Use all configured ipv4 dns services #426

Merged
merged 1 commit into from
Nov 15, 2024
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
42 changes: 23 additions & 19 deletions pkg/services/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
)

type dnsHandler struct {
zones []types.Zone
zonesLock sync.RWMutex
udpClient *dns.Client
tcpClient *dns.Client
hostsFile *HostsFile
nameserver string
zones []types.Zone
zonesLock sync.RWMutex
udpClient *dns.Client
tcpClient *dns.Client
hostsFile *HostsFile
nameservers []string
}

func newDNSHandler(zones []types.Zone) (*dnsHandler, error) {

nameserver, port, err := getDNSHostAndPort()
nameservers, err := getDNSHostAndPort()
if err != nil {
return nil, err
}
Expand All @@ -37,11 +37,11 @@ func newDNSHandler(zones []types.Zone) (*dnsHandler, error) {
}

return &dnsHandler{
zones: zones,
tcpClient: &dns.Client{Net: "tcp"},
udpClient: &dns.Client{Net: "udp"},
nameserver: net.JoinHostPort(nameserver, port),
hostsFile: hostsFile,
zones: zones,
tcpClient: &dns.Client{Net: "tcp"},
udpClient: &dns.Client{Net: "udp"},
nameservers: nameservers,
hostsFile: hostsFile,
}, nil

}
Expand Down Expand Up @@ -145,15 +145,19 @@ func (h *dnsHandler) addAnswers(dnsClient *dns.Client, r *dns.Msg) *dns.Msg {
return m
}
}

r, _, err := dnsClient.Exchange(r, h.nameserver)
if err != nil {
log.Errorf("Error during DNS Exchange: %s", err)
m.Rcode = dns.RcodeNameError
return m
for _, nameserver := range h.nameservers {
msg := r.Copy()
r, _, err := dnsClient.Exchange(msg, nameserver)
// return first good answer
if err == nil {
return r
}
log.Debugf("Error during DNS Exchange: %s", err)
}

return r
// return the error if none of configured nameservers has right answer
m.Rcode = dns.RcodeNameError
return m
}

type Server struct {
Expand Down
25 changes: 19 additions & 6 deletions pkg/services/dns/dns_config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
package dns

import (
"net"
"net/netip"

"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

func getDNSHostAndPort() (string, string, error) {
func getDNSHostAndPort() ([]string, error) {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return "", "", err
return []string{}, err
}
// TODO: use all configured nameservers, instead just first one
nameserver := conf.Servers[0]

return nameserver, conf.Port, nil
var hosts = make([]string, len(conf.Servers))
for _, server := range conf.Servers {
dnsIP, err := netip.ParseAddr(server)
if err != nil {
log.Errorf("Failed to parse DNS IP address: %s", server)
continue
}
// add only ipv4 dns addresses
if dnsIP.Is4() {
hosts = append(hosts, net.JoinHostPort(server, conf.Port))
}
}
return hosts, nil
}
13 changes: 6 additions & 7 deletions pkg/services/dns/dns_config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
package dns

import (
"net/netip"
"net"
"strconv"

qdmDns "github.com/qdm12/dns/v2/pkg/nameserver"
)

func getDNSHostAndPort() (string, string, error) {
func getDNSHostAndPort() ([]string, error) {
nameservers := qdmDns.GetDNSServers()

var nameserver netip.AddrPort
var dnsServers []string
for _, n := range nameservers {
// return first non ipv6 nameserver
// return only ipv4 nameservers
if n.Addr().Is4() {
nameserver = n
break
dnsServers = append(dnsServers, net.JoinHostPort(n.Addr().String(), strconv.Itoa(int(n.Port()))))
}
}

return nameserver.Addr().String(), strconv.Itoa(int(nameserver.Port())), nil
return dnsServers, nil

}
Loading