name-dyndns/dyndns/daemon.go

143 lines
3.5 KiB
Go
Raw Permalink Normal View History

2015-05-04 20:20:11 -04:00
// Package dyndns provides a tool for running a
// dynamic dns updating service.
2015-05-02 22:17:13 -04:00
package dyndns
import (
"fmt"
"strings"
2015-05-02 22:17:13 -04:00
"sync"
"time"
"net"
2024-06-24 22:29:06 -04:00
"git.darkcloud.ca/kevin/name-dyndns/api"
"git.darkcloud.ca/kevin/name-dyndns/log"
2015-05-02 22:17:13 -04:00
)
var wg sync.WaitGroup
func contains(c api.Config, val string) bool {
for _, v := range c.Hostnames {
2015-05-06 11:23:27 -04:00
// We have a special case where an empty hostname
// is equivalent to the domain (i.e. val == domain).
if val == c.Domain && v == "" {
return true
} else if fmt.Sprintf("%s.%s", v, c.Domain) == val {
return true
}
}
return false
}
func updateDNSRecord(a api.API, domain, recordID string, newRecord api.DNSRecord) error {
2024-06-24 23:09:47 -04:00
addr := net.ParseIP(newRecord.Content)
if addr != nil {
log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name)
err := a.DeleteDNSRecord(domain, newRecord.RecordID)
if err != nil {
return err
}
2015-05-02 22:17:13 -04:00
log.Logger.Printf("Creating DNS record for %s: %s\n", newRecord.Name, newRecord)
// Remove the domain from the DNSRecord name.
// This is an unfortunate inconsistency from the API
// implementation (returns full name, but only requires host)
if newRecord.Name == domain {
newRecord.Name = ""
} else {
newRecord.Name = strings.TrimSuffix(newRecord.Name, fmt.Sprintf(".%s", domain))
}
return a.CreateDNSRecord(domain, newRecord)
} else {
log.Logger.Printf("Unable to parse IP: %s\n", newRecord)
2024-06-24 23:06:23 -04:00
return nil;
}
2015-05-02 22:17:13 -04:00
}
func runConfig(c api.Config, daemon bool) {
defer wg.Done()
a := api.NewAPIFromConfig(c)
for {
ip, err := GetExternalIP()
if err != nil {
2015-05-04 20:03:17 -04:00
log.Logger.Print("Failed to retreive IP: ")
2015-05-04 14:52:55 -04:00
if daemon {
log.Logger.Printf("Will retry in %d seconds...\n", c.Interval)
time.Sleep(time.Duration(c.Interval) * time.Second)
2015-05-04 14:52:55 -04:00
continue
} else {
2015-05-04 20:03:17 -04:00
log.Logger.Println("Giving up.")
2015-05-04 14:52:55 -04:00
break
}
}
// GetRecords retrieves a list of DNSRecords,
// 1 per hostname with the associated domain.
// If the content is not the current IP, then
// update it.
2015-05-04 20:20:11 -04:00
records, err := a.GetDNSRecords(c.Domain)
2015-05-04 14:52:55 -04:00
if err != nil {
log.Logger.Printf("Failed to retreive records for %s:\n\t%s\n", c.Domain, err)
2015-05-02 22:17:13 -04:00
if daemon {
log.Logger.Printf("Will retry in %d seconds...\n", c.Interval)
time.Sleep(time.Duration(c.Interval) * time.Second)
2015-05-02 22:17:13 -04:00
continue
} else {
2015-05-04 20:03:17 -04:00
log.Logger.Print("Giving up.")
2015-05-02 22:17:13 -04:00
break
}
}
2015-05-04 14:52:55 -04:00
for _, r := range records {
if !contains(c, r.Name) {
continue
}
// Only A records should be mapped to an IP.
// TODO: Support AAAA records.
if r.Type != "A" {
continue
}
log.Logger.Printf("Running update check for %s.", r.Name)
2015-05-04 14:52:55 -04:00
if r.Content != ip {
r.Content = ip
err = updateDNSRecord(a, c.Domain, r.RecordID, r)
2015-05-04 14:52:55 -04:00
if err != nil {
log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordID, r.Name, ip, err)
2015-05-04 20:03:17 -04:00
} else {
log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordID, r.Name, ip)
2015-05-04 14:52:55 -04:00
}
}
2015-05-02 22:17:13 -04:00
}
log.Logger.Println("Update complete.")
2015-05-02 22:17:13 -04:00
if !daemon {
2015-05-04 20:03:17 -04:00
log.Logger.Println("Non daemon mode, stopping.")
2015-05-02 22:17:13 -04:00
return
}
log.Logger.Printf("Will update again in %d seconds.\n", c.Interval)
2015-05-02 22:17:13 -04:00
time.Sleep(time.Duration(c.Interval) * time.Second)
}
}
// Run will process each configuration in configs.
// If daemon is true, then Run will run forever,
// processing each configuration at its specified
// interval.
//
// Each configuration represents a domain with
// multiple hostnames.
2015-05-02 22:17:13 -04:00
func Run(configs []api.Config, daemon bool) {
for _, config := range configs {
wg.Add(1)
go runConfig(config, daemon)
}
wg.Wait()
}