From 19f8b27664956868b6f2f5cf10336f0f1c0d774e Mon Sep 17 00:00:00 2001 From: Maxime Le Conte des Floris Date: Sat, 26 Nov 2022 19:48:41 +0100 Subject: [PATCH] fix: correctly update the IPs if only one of the two is not up to date --- dyndns.go | 34 +++++++++++----------------------- tests/mocks/update-ipv4.yaml | 7 +++++++ tests/mocks/update-ipv6.yaml | 7 +++++++ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/dyndns.go b/dyndns.go index 05fce14..192bd3e 100644 --- a/dyndns.go +++ b/dyndns.go @@ -99,9 +99,9 @@ func (dyndns *DynDNS) execute(domain string, record string, ttl int, alwaysNotif return err } - ipsToUpdate := dyndns.matchIPs(resolvedIPs, dnsRecords) + needUpdate := dyndns.matchIPs(resolvedIPs, dnsRecords) - if len(ipsToUpdate) == 0 { + if !needUpdate { log.Println("IP address(es) match - no further action") if alwaysNotify { @@ -118,7 +118,7 @@ func (dyndns *DynDNS) execute(domain string, record string, ttl int, alwaysNotif return nil } - err = dyndns.gandiClient.put(domain, record, ipsToUpdate, ttl) + err = dyndns.gandiClient.put(domain, record, []*net.IP{resolvedIPs.V4, resolvedIPs.V6}, ttl) if err != nil { return err } @@ -155,37 +155,25 @@ func (dyndns *DynDNS) notifyDiscord(domain string, record string, ips []*net.IP) return errors.Wrap(err, "failed to post success message to Discord") } -func (dyndns *DynDNS) matchIPs(resolvedIPs *IPAddrs, dnsRecords []*domainRecord) []*net.IP { - toUpdate := make([]*net.IP, 0, 2) - - isIpV4UpToDate := false - isIpV6UpTodate := false - +func (dyndns *DynDNS) matchIPs(resolvedIPs *IPAddrs, dnsRecords []*domainRecord) bool { ipsFromDNS := make([]*net.IP, 0, 2) + var foundIPV4 bool + var foundIPV6 bool + for _, records := range dnsRecords { for _, rrsetValue := range records.RrsetValues { ipsFromDNS = append(ipsFromDNS, rrsetValue) if resolvedIPs.V4 != nil && rrsetValue.Equal(*resolvedIPs.V4) { - isIpV4UpToDate = true - } - - if resolvedIPs.V6 != nil && rrsetValue.Equal(*resolvedIPs.V6) { - isIpV6UpTodate = true + foundIPV4 = true + } else if resolvedIPs.V6 != nil && rrsetValue.Equal(*resolvedIPs.V6) { + foundIPV6 = true } } } log.Printf("IP(s) from DNS: %s", ipsFromDNS) - if resolvedIPs.V4 != nil && !isIpV4UpToDate { - toUpdate = append(toUpdate, resolvedIPs.V4) - } - - if resolvedIPs.V6 != nil && !isIpV6UpTodate { - toUpdate = append(toUpdate, resolvedIPs.V6) - } - - return toUpdate + return !foundIPV4 || !foundIPV6 } diff --git a/tests/mocks/update-ipv4.yaml b/tests/mocks/update-ipv4.yaml index cee5071..c093b3e 100644 --- a/tests/mocks/update-ipv4.yaml +++ b/tests/mocks/update-ipv4.yaml @@ -55,6 +55,13 @@ "rrset_values": [ "109.215.101.49" ] + }, + { + "rrset_ttl": 3600, + "rrset_type": "AAAA", + "rrset_values": [ + "0:cb19:96a:7c00:13b0:5ba3:16ae:6c82" + ] } ] } diff --git a/tests/mocks/update-ipv6.yaml b/tests/mocks/update-ipv6.yaml index 622e9c5..f8b9e89 100644 --- a/tests/mocks/update-ipv6.yaml +++ b/tests/mocks/update-ipv6.yaml @@ -49,6 +49,13 @@ value: > { "items": [ + { + "rrset_ttl": 3600, + "rrset_type": "A", + "rrset_values": [ + "109.215.101.49" + ] + }, { "rrset_ttl": 3600, "rrset_type": "AAAA",