-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (77 loc) · 2.12 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
cf "github.com/nallerooth/cloudflare-dns-updater/cloudflare"
"github.com/nallerooth/cloudflare-dns-updater/config"
"github.com/nallerooth/cloudflare-dns-updater/iplookup"
)
type options struct {
configPath string
verbose bool
watch bool
}
func getLaunchFlags() options {
o := options{}
flag.StringVar(&o.configPath, "c", "./config.json", "Path to config file")
flag.BoolVar(&o.watch, "w", false, "Keep watching for external IP change")
flag.BoolVar(&o.verbose, "v", false, "Verbose mode")
flag.Parse()
return o
}
func compareIPAddrs(ip string, dns *cf.DNSRecordDetails) bool {
return strings.Compare(ip, dns.Content) == 0
}
func run(conf *config.Config) error {
ip, err := iplookup.GetExternalIPv4Address(conf)
if err != nil {
return fmt.Errorf("Unable to get external IP address: %s", err)
}
if conf.VerboseMode {
log.Println("External IP Address", ip)
}
dns, err := cf.GetDNSEntry(conf)
if err != nil {
return fmt.Errorf("Unable to fetch DNS entry from Cloudflare: %s", err)
}
if compareIPAddrs(ip, dns) {
if conf.VerboseMode {
log.Println("Cloudflare DNS record matches external IP address")
}
} else {
log.Printf("Cloudflare IP (%s) does not match external IP -> Updating Cloudflare DNS record to %s", dns.Content, ip)
dns.Content = ip
success, err := cf.UpdateDNSEntry(dns, conf)
if err != nil {
return fmt.Errorf("Unable to update Cloudflare DNS record: %s", err)
}
if success {
log.Println("Cloudflare DNS record successfully updated")
} else {
log.Println("The DNS record failed to update, but no error was given by Cloudflare")
}
}
return nil
}
func main() {
flags := getLaunchFlags()
conf, err := config.LoadFromFile(flags.configPath)
if err != nil {
log.Fatalln("Unable to load config file")
}
// Allow override of verbose mode set in config file
conf.VerboseMode = conf.VerboseMode || flags.verbose
for {
if err = run(conf); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
}
if !flags.watch {
break
}
time.Sleep(time.Second * time.Duration(conf.SleepSeconds))
}
}