Skip to content

Commit

Permalink
Merge pull request #426 from evidolob/use-all-dns
Browse files Browse the repository at this point in the history
Use all configured ipv4 dns services
  • Loading branch information
openshift-merge-bot[bot] authored Nov 15, 2024
2 parents 72b102d + d69e2ac commit 2be6b3f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
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

}

0 comments on commit 2be6b3f

Please sign in to comment.