Skip to content

Commit

Permalink
fix: correctly update the IPs if only one of the two is not up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
mlcdf committed Nov 26, 2022
1 parent febc7e9 commit 19f8b27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
34 changes: 11 additions & 23 deletions dyndns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions tests/mocks/update-ipv4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions tests/mocks/update-ipv6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
value: >
{
"items": [
{
"rrset_ttl": 3600,
"rrset_type": "A",
"rrset_values": [
"109.215.101.49"
]
},
{
"rrset_ttl": 3600,
"rrset_type": "AAAA",
Expand Down

0 comments on commit 19f8b27

Please sign in to comment.