update serial of zone

This commit is contained in:
Joachim Lusiardi 2025-02-27 07:29:37 +01:00
parent 3f772923c4
commit f994926c6f

32
main.go
View File

@ -4,7 +4,7 @@ package main
* https://github.com/joeig/go-powerdns
* https://github.com/xlzd/gotp
* https://github.com/dspinhirne/netaddr-go => https://pkg.go.dev/github.com/dspinhirne/netaddr-go
* https://github.com/go-yaml/yaml/tree/v2.4.0 =>
* https://github.com/go-yaml/yaml/tree/v2.4.0 =>
*/
import (
"context"
@ -40,6 +40,14 @@ func (c *conf) getConf() *conf {
}
func main() {
f, err := os.OpenFile("/tmp/testlogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
var c conf
c.getConf()
@ -57,7 +65,8 @@ func main() {
if userIP == nil {
log.Fatalf("Err %v ", err)
}
fmt.Fprintf(w, "IP: %q \n", ip)
//fmt.Fprintf(w, "IP: %q \n", ip)
//fmt.Fprintf(w, "IP: %q \n", ip)
log.Println(fmt.Sprintf("TOTP : %q", gotp.NewDefaultTOTP(c.TotpToken).Now()))
log.Println(fmt.Sprintf("Token: %q", token))
@ -80,16 +89,33 @@ func main() {
new_ipv6 := netaddr.NewIPv6(new_prefix, hostId)
log.Println(fmt.Sprintf("Host: %q -> %q needs update: %t", ipv6, new_ipv6, ipv6.Long() != new_ipv6.Long()))
if ipv6.Long() != new_ipv6.Long() {
fmt.Fprintf(w, "Update Host %q to %q\n", *r.Name, ip)
updated = true
err = pdns.Records.Change(ctx, c.Zone, record_to_update+"."+c.Zone, powerdns.RRTypeAAAA, 30, []string{new_ipv6.Long()})
if err != nil {
log.Fatalf("Update err %v ", err)
log.Fatalf("Update err %v ", err)
}
} else {
fmt.Fprintf(w, "No update for host %q\n", *r.Name)
}
}
}
}
if updated {
// increase serial
zone, err := pdns.Zones.Get(ctx, c.Zone)
if err != nil {
log.Fatalf("%v", err)
}
serial := *zone.Serial
zoneChangeSet := &powerdns.Zone{
Serial: powerdns.Uint32(serial + 1),
}
if err := pdns.Zones.Change(ctx, c.Zone, zoneChangeSet); err != nil {
log.Fatalf("%v", err)
}
pdns.Zones.Notify(ctx, c.Zone)
log.Println("Updated...")
}