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 (
|
2015-05-06 11:08:30 -04:00
|
|
|
"fmt"
|
2015-05-02 22:17:13 -04:00
|
|
|
"github.com/mfycheng/name-dyndns/api"
|
2015-05-04 20:03:17 -04:00
|
|
|
"github.com/mfycheng/name-dyndns/log"
|
2015-05-06 11:08:30 -04:00
|
|
|
"strings"
|
2015-05-02 22:17:13 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-05-06 11:08:30 -04:00
|
|
|
func contains(c api.Config, val string) bool {
|
|
|
|
if val == c.Domain {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range c.Hostnames {
|
|
|
|
if fmt.Sprintf("%s.%s", v, c.Domain) == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:52:55 -04:00
|
|
|
func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error {
|
2015-05-05 11:07:59 -04:00
|
|
|
log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name)
|
2015-05-04 14:52:55 -04:00
|
|
|
err := a.DeleteDNSRecord(domain, newRecord.RecordId)
|
2015-05-02 22:17:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:07:59 -04:00
|
|
|
log.Logger.Printf("Creating DNS record for %s: %s\n", newRecord.Name, newRecord)
|
2015-05-06 11:08:30 -04:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:52:55 -04:00
|
|
|
return a.CreateDNSRecord(domain, newRecord)
|
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 {
|
2015-05-04 20:52:57 -04:00
|
|
|
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 {
|
2015-05-04 20:52:57 -04:00
|
|
|
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 {
|
2015-05-04 20:52:57 -04:00
|
|
|
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 {
|
2015-05-06 11:08:30 -04:00
|
|
|
if !contains(c, r.Name) {
|
|
|
|
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)
|
|
|
|
if err != nil {
|
2015-05-05 11:07:59 -04:00
|
|
|
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 {
|
2015-05-06 11:08:30 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-04 20:52:57 -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
|
|
|
|
}
|
2015-05-04 20:52:57 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each domain, check if the host record matches
|
|
|
|
// the current external IP. If it does not, it updates.
|
|
|
|
// If daemon is true, then Run will run forever, polling at
|
|
|
|
// an interval specified in each config.
|
|
|
|
func Run(configs []api.Config, daemon bool) {
|
|
|
|
for _, config := range configs {
|
|
|
|
wg.Add(1)
|
|
|
|
go runConfig(config, daemon)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|