Skip to content

Commit

Permalink
Merge pull request #21 from cromefire/retry
Browse files Browse the repository at this point in the history
More resilient polling
  • Loading branch information
cromefire authored Oct 22, 2023
2 parents 2a688c4 + e978d98 commit 570adc6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
40 changes: 20 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func startPollServer(out chan<- *net.IP, localIp *net.IP) {
if err != nil {
slog.Warn("Failed to poll WAN IPv4 from router", logging.ErrorAttr(err))
} else {
out <- &ipv4
if !lastV4.Equal(ipv4) {
slog.Info("New WAN IPv4 found", slog.Any("ipv4", ipv4))
out <- &ipv4
lastV4 = ipv4
}
}
Expand All @@ -221,29 +221,29 @@ func startPollServer(out chan<- *net.IP, localIp *net.IP) {
if err != nil {
slog.Warn("Failed to poll IPv6 Prefix from router", logging.ErrorAttr(err))
} else {
if !lastV6.Equal(prefix.IP) {

constructedIp := make(net.IP, net.IPv6len)
copy(constructedIp, prefix.IP)

maskLen, _ := prefix.Mask.Size()

for i := 0; i < net.IPv6len; i++ {
b := constructedIp[i]
lb := (*localIp)[i]
var mask byte = 0b00000000
for j := 0; j < 8; j++ {
if (i*8 + j) >= maskLen {
mask += 0b00000001 << (7 - j)
}
constructedIp := make(net.IP, net.IPv6len)
copy(constructedIp, prefix.IP)

maskLen, _ := prefix.Mask.Size()

for i := 0; i < net.IPv6len; i++ {
b := constructedIp[i]
lb := (*localIp)[i]
var mask byte = 0b00000000
for j := 0; j < 8; j++ {
if (i*8 + j) >= maskLen {
mask += 0b00000001 << (7 - j)
}
b += lb & mask
constructedIp[i] = b
}
b += lb & mask
constructedIp[i] = b
}

slog.Info("New IPv6 Prefix found", slog.Any("prefix", prefix), slog.Any("ipv6", constructedIp))

slog.Info("New IPv6 Prefix found", slog.Any("prefix", prefix), slog.Any("ipv6", constructedIp))
out <- &constructedIp

out <- &constructedIp
if !lastV6.Equal(prefix.IP) {
lastV6 = prefix.IP
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/cloudflare/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Updater struct {
log *slog.Logger

In chan *net.IP

lastIpv4 *net.IP
lastIpv6 *net.IP
}

func NewUpdater(log *slog.Logger) *Updater {
Expand Down Expand Up @@ -136,6 +139,15 @@ func (u *Updater) spawnWorker() {
for {
select {
case ip := <-u.In:
if ip.To4() == nil {
if u.lastIpv6 != nil && u.lastIpv6.Equal(*ip) {
continue
}
} else {
if u.lastIpv4 != nil && u.lastIpv4.Equal(*ip) {
continue
}
}
u.log.Info("Received update request", slog.Any("ip", ip))

for _, action := range u.actions {
Expand Down Expand Up @@ -201,6 +213,10 @@ func (u *Updater) spawnWorker() {
for _, record := range records {
alog.Info("Updating DNS record", slog.Any("record-id", record.ID))

if record.Content == ip.String() {
continue
}

// Ensure we submit all required fields even if they did not change,otherwise
// cloudflare-go might revert them to default values.
_, err := u.api.UpdateDNSRecord(ctx, rc, cf.UpdateDNSRecordParams{
Expand All @@ -219,6 +235,11 @@ func (u *Updater) spawnWorker() {
cancel()
}

if ip.To4() == nil {
u.lastIpv6 = ip
} else {
u.lastIpv4 = ip
}
}
}
}

0 comments on commit 570adc6

Please sign in to comment.