nameserver.go: refactor requiresRRUpdate and add basic tests

This commit is contained in:
Thomas Preisner 2021-10-16 22:06:51 +02:00
parent 204d5eacf6
commit 1cfe465cf9
4 changed files with 101 additions and 3 deletions

View file

@ -28,10 +28,19 @@ func getResolver(nameserver string) *net.Resolver {
}
}
func requiresRRUpdate(record string, addr net.IP, resolver *net.Resolver) bool {
// FIXME: mockdns.Resolver from foxccp/go-mockdns, which is used for testing,
// does not implement the complete net.Resolver interface. Thus, we need to
// define a more minimalistic resolver interface compatible with both
// net.Resolver and mockdns.Resolver to avoid compiler errors.
// Maybe there is a better way to circumvent this though.
type localResolver interface {
LookupHost(context.Context, string) ([]string, error)
}
func requiresRRUpdate(record string, addr net.IP, resolver localResolver) bool {
// TODO: the context could be replaced in order to allow cancelling the dns
// query if the user disconnects prematurely
addrs, err := resolver.LookupIP(context.Background(), "ip", record)
addrs, err := resolver.LookupHost(context.Background(), record)
if err != nil {
log.Printf("dns lookup failed: %s", err)
// enforce update, it's better than not trying at all
@ -40,7 +49,7 @@ func requiresRRUpdate(record string, addr net.IP, resolver *net.Resolver) bool {
// check if the current ip matches
for _, ip := range addrs {
if ip.Equal(addr) {
if addr.Equal(net.ParseIP(ip)) {
// the ip seems to be still up-to-date -> no update required
return false
}