You've already forked ddns-updater
Initial Notifiers implementation
This commit is contained in:
32
main.go
32
main.go
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/cloudflare/cloudflare-go"
|
||||
"github.com/mkelcik/cloudflare-ddns-update/internal"
|
||||
"github.com/mkelcik/cloudflare-ddns-update/notifications"
|
||||
"github.com/mkelcik/cloudflare-ddns-update/public_resolvers"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,21 @@ type PublicIpResolver interface {
|
||||
ResolvePublicIp(ctx context.Context) (net.IP, error)
|
||||
}
|
||||
|
||||
func getNotifiers(tags []string) notifications.Notifiers {
|
||||
out := notifications.Notifiers{}
|
||||
for _, t := range tags {
|
||||
if initFn, ok := notifications.Available[t]; ok {
|
||||
notifier, err := initFn()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
out = append(out, notifier)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func getResolver(resolverName string) (PublicIpResolver, string) {
|
||||
switch resolverName {
|
||||
// HERE add another resolver if needed
|
||||
@@ -51,6 +67,8 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
notifiers := getNotifiers(config.Notifiers)
|
||||
|
||||
// public ip resolver
|
||||
publicIpResolver, resolverTag := getResolver(config.PublicIpResolverTag)
|
||||
|
||||
@@ -85,9 +103,19 @@ func main() {
|
||||
|
||||
if _, err := api.UpdateDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), update); err != nil {
|
||||
log.Printf("error updating dns record: %s", err)
|
||||
} else {
|
||||
log.Printf("Updated to `%s`", currentPublicIP)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := notifiers.NotifyWithLog(ctx, notifications.Notification{
|
||||
OldIp: net.ParseIP(dnsRecord.Content),
|
||||
NewIp: currentPublicIP,
|
||||
CheckedAt: time.Now(),
|
||||
ResolverTag: resolverTag,
|
||||
Domain: dnsRecord.Name,
|
||||
}); err != nil {
|
||||
log.Printf("errors in notifications: %s", err)
|
||||
}
|
||||
log.Printf("Updated to `%s`", currentPublicIP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user