-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathund.go
68 lines (61 loc) · 1.8 KB
/
und.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
)
var (
fVersion = flag.Bool("v", false, "print version information and exit")
fKey = flag.String("k", "12345", " To be replaced by your unique API key. Visit the API Manager page within your account for details.")
fDomain = flag.String("d", "namesilo.com", "The domain associated with the DNS resource record to modify")
fHost = flag.String("h", "www", "The hostname to use (there is no need to include the \".DOMAIN\")")
fInterval = flag.Duration("i", 300*time.Second, "The seconds of updating interval")
)
var version = "None"
func main() {
flag.Parse()
if *fVersion {
fmt.Println(version)
os.Exit(0)
}
updateDNSLoop()
}
func updateDNSLoop() {
tick := time.NewTicker(*fInterval)
defer tick.Stop()
for {
select {
case <-tick.C:
err := doUpdateDNS(*fDomain, *fHost, *fKey)
if err != nil {
log.Printf("[%v] update DNS record failed, domain:%s, host:%s, error:%v", time.Now().Format(time.RFC3339), *fDomain, *fHost, err)
}
}
}
}
func doUpdateDNS(domain, host, key string) error {
listResp, err := dnsList(domain, key)
if err != nil {
return err
}
// find the one need to be updated
fullHost := host + "." + domain
for _, item := range listResp.ListReply.DNSRecords {
if item.Host == fullHost {
if item.Value != listResp.Request.IP {
// update record
log.Printf("[%v] host: %s, update old IP: %s to new IP: %s", time.Now().Format(time.RFC3339), item.Host, item.Value, listResp.Request.IP)
err = dnsUpdate(key, domain, item.RecordID, host, listResp.Request.IP)
if err != nil {
return err
}
} else {
log.Printf("[%v] host: %s, old IP: %s, new IP: %s are same, nothing to do", time.Now().Format(time.RFC3339), item.Host, item.Value, listResp.Request.IP)
}
return nil
}
}
return nil
}