Fix bug where any non-empty hostname would not be updated
This commit is contained in:
parent
0400c2f406
commit
c1746de8eb
2 changed files with 45 additions and 3 deletions
15
api/api.go
15
api/api.go
|
@ -80,7 +80,20 @@ func (api API) performRequest(method, url string, body io.Reader) (response []by
|
||||||
// Create a DNS record for a given domain. The name
|
// Create a DNS record for a given domain. The name
|
||||||
// field in DNSRecord is in the format [hostname].[domainname]
|
// field in DNSRecord is in the format [hostname].[domainname]
|
||||||
func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
||||||
b, err := json.Marshal(record)
|
// We need to transform name -> hostname for JSON.
|
||||||
|
var body struct {
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Ttl string `json:"ttl"`
|
||||||
|
}
|
||||||
|
|
||||||
|
body.Hostname = record.Name
|
||||||
|
body.Type = record.Type
|
||||||
|
body.Content = record.Content
|
||||||
|
body.Ttl = record.Ttl
|
||||||
|
|
||||||
|
b, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,29 @@
|
||||||
package dyndns
|
package dyndns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/mfycheng/name-dyndns/api"
|
"github.com/mfycheng/name-dyndns/api"
|
||||||
"github.com/mfycheng/name-dyndns/log"
|
"github.com/mfycheng/name-dyndns/log"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error {
|
func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error {
|
||||||
log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name)
|
log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name)
|
||||||
err := a.DeleteDNSRecord(domain, newRecord.RecordId)
|
err := a.DeleteDNSRecord(domain, newRecord.RecordId)
|
||||||
|
@ -19,6 +34,16 @@ func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Logger.Printf("Creating DNS record for %s: %s\n", newRecord.Name, newRecord)
|
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)
|
return a.CreateDNSRecord(domain, newRecord)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +52,6 @@ func runConfig(c api.Config, daemon bool) {
|
||||||
|
|
||||||
a := api.NewAPIFromConfig(c)
|
a := api.NewAPIFromConfig(c)
|
||||||
for {
|
for {
|
||||||
log.Logger.Printf("Running update check for %s.", c.Domain)
|
|
||||||
ip, err := GetExternalIP()
|
ip, err := GetExternalIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Logger.Print("Failed to retreive IP: ")
|
log.Logger.Print("Failed to retreive IP: ")
|
||||||
|
@ -59,13 +83,18 @@ func runConfig(c api.Config, daemon bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range records {
|
for _, r := range records {
|
||||||
|
if !contains(c, r.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Logger.Printf("Running update check for %s.", r.Name)
|
||||||
if r.Content != ip {
|
if r.Content != ip {
|
||||||
r.Content = ip
|
r.Content = ip
|
||||||
err = updateDNSRecord(a, c.Domain, r.RecordId, r)
|
err = updateDNSRecord(a, c.Domain, r.RecordId, r)
|
||||||
if err != nil {
|
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)
|
log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordId, r.Name, ip, err)
|
||||||
} else {
|
} else {
|
||||||
log.Logger.Printf("Attempting to update record %s [%s] with IP: %s\n", r.RecordId, r.Name, ip)
|
log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordId, r.Name, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue