livedns-go/main.go
2025-01-01 20:51:16 +01:00

161 lines
3.3 KiB
Go

package main
import (
"context"
"log"
"os"
"gitnet.fr/deblan/livedns-go/config"
"gitnet.fr/deblan/livedns-go/dns"
gdi "gitnet.fr/deblan/livedns-go/dns/gandi"
"gitnet.fr/deblan/livedns-go/dns/ip"
"gitnet.fr/deblan/livedns-go/dns/powerdns"
)
func main() {
if len(os.Args) != 2 {
log.Fatalf("Configuration required")
}
err := config.LoadConfiguration(os.Args[1])
if err != nil {
log.Fatalf("Error while loading the configuration: %s", err)
}
conf := config.Get()
ctx := context.Background()
pdns := powerdns.GetPowerDnsClient()
ip4 := ip.GetIp4()
ip6 := ip.GetIp6()
if ip4 == "" || ip6 == "" {
log.Fatalf("No ip retrieved: IP4=%s IP6=%s", ip4, ip6)
}
// ip4 = "90.100.102.191"
// ip6 = "2a01:cb10:9365:1400:feaa:14ff:fec1:c4d4"
log.Printf("IP4=%s", ip4)
log.Printf("IP6=%s", ip6)
gandi := gdi.GetGandiClient()
log.Print("---- Update of GANDI ----")
for _, item := range conf.Gandi.GlueRecords.Ip4 {
gandi.UpdateGlueRecord(item.Domain, item.Name, ip4)
}
for _, item := range conf.Gandi.Records.Ip4 {
gandi.UpdateRecord(item.Domain, item.Name, "A", ip4, item.Ttl)
}
for _, item := range conf.Gandi.Records.Ip6 {
gandi.UpdateRecord(item.Domain, item.Name, "AAAA", ip6, item.Ttl)
}
zones, _ := pdns.Zones.List(ctx)
log.Print("---- Update of PowerDNS ----")
for _, item := range zones {
zone, _ := pdns.Zones.Get(ctx, *item.Name)
rrsets := zone.RRsets
for _, rrset := range rrsets {
if len(rrset.Comments) != 1 {
continue
}
for _, comment := range rrset.Comments {
value := *comment.Content
if value == "livedns-a" {
for _, record := range rrset.Records {
if *record.Content != ip4 {
log.Printf(
"[%s] Record=%s State=KO CurrentValue=%s NextValue=%s",
*zone.Name,
*rrset.Name,
*record.Content,
ip4,
)
pdns.Records.Change(
ctx,
*zone.Name,
*rrset.Name,
*rrset.Type,
*rrset.TTL,
[]string{ip4},
)
} else {
log.Printf(
"[%s] Record=%s State=OK",
*zone.Name,
*rrset.Name,
)
}
}
} else if value == "livedns-aaaa" {
for _, record := range rrset.Records {
if *record.Content != ip6 {
log.Printf(
"[%s] Record=%s State=KO CurrentValue=%s NextValue=%s",
*zone.Name,
*rrset.Name,
*record.Content,
ip6,
)
pdns.Records.Change(
ctx,
*zone.Name,
*rrset.Name,
*rrset.Type,
*rrset.TTL,
[]string{ip6},
)
} else {
log.Printf(
"[%s] Record=%s State=OK",
*zone.Name,
*rrset.Name,
)
}
}
} else if value == "livedns-spf" {
for _, record := range rrset.Records {
spf := dns.UpdateSpf(*record.Content, ip4, ip6)
if *record.Content != spf {
log.Printf(
"[%s] Record=%s State=KO CurrentValue=%s NextValue=%s",
*zone.Name,
*rrset.Name,
*record.Content,
spf,
)
pdns.Records.Change(
ctx,
*zone.Name,
*rrset.Name,
*rrset.Type,
*rrset.TTL,
[]string{spf},
)
} else {
log.Printf(
"[%s] Record=%s State=OK",
*zone.Name,
*rrset.Name,
)
}
}
}
}
}
}
}