Skip to content

Commit

Permalink
Use all configured ipv4 dns services
Browse files Browse the repository at this point in the history
It brings all ipv4 dns and make dns resolve request one by one until it receives first answer

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Nov 15, 2024
1 parent 72b102d commit e9dc64b
Show file tree
Hide file tree
Showing 3 changed files with 42 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
19 changes: 13 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,23 @@
package dns

import (
"net"

"github.com/miekg/dns"
)

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 := net.ParseIP(server)
// add only ipv4 dns addresses
if dnsIP != nil && dnsIP.To4() != nil {
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 = make([]string, 5)
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 e9dc64b

Please sign in to comment.