Files
he-dydns/internal/updater/updater.go
T

64 lines
1.6 KiB
Go

// Package updater runs one dynamic DNS update pass across all configured entries.
package updater
import (
"fmt"
"log"
"net"
"git.aehoo.net/alphard/he-dydns/internal/config"
"git.aehoo.net/alphard/he-dydns/internal/hedns"
"git.aehoo.net/alphard/he-dydns/internal/network"
)
// Run discovers the current address(es) and updates every configured entry.
// It returns an error only when address discovery itself fails outright;
// per-entry update failures are logged and aggregated but do not stop other
// entries from being attempted.
func Run(cfg *config.Config) error {
addrs, err := network.Discover(cfg.Interface)
if err != nil {
return fmt.Errorf("discovering address: %w", err)
}
if addrs.Empty() {
log.Printf("no address found (interface=%q); skipping update", cfg.Interface)
return nil
}
if addrs.IPv4 != nil {
log.Printf("discovered IPv4 address %s", addrs.IPv4)
}
if addrs.IPv6 != nil {
log.Printf("discovered IPv6 address %s", addrs.IPv6)
}
var failures int
for _, entry := range cfg.Entries {
var ip net.IP
switch entry.RecordType {
case config.RecordTypeAAAA:
ip = addrs.IPv6
default:
ip = addrs.IPv4
}
if ip == nil {
log.Printf("no %s address available for %s; skipping", entry.RecordType, entry.Hostname)
continue
}
if err := hedns.Update(entry.Hostname, entry.Token, ip); err != nil {
log.Printf("error: %v", err)
failures++
} else {
log.Printf("updated %s (%s) -> %s", entry.Hostname, entry.RecordType, ip)
}
}
if failures > 0 {
return fmt.Errorf("%d update(s) failed", failures)
}
return nil
}