-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
177 lines (146 loc) · 4.06 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var logger *log.Logger
func init() {
logger = log.New(io.Writer(os.Stdout), "go-cddns:", log.Lshortfile)
}
func main() {
// Parse command line flags
opts := parseCommandLineFlags()
// Read config file
file, err := ioutil.ReadFile(opts.Filename)
if err != nil {
logger.Fatalf("Cannot open config file %s", opts.Filename)
}
var cloudflareConfig config
err = json.Unmarshal(file, &cloudflareConfig)
if err != nil {
logger.Fatalln(err)
}
cloudflareConfig.CloudflareURL = "https://api.cloudflare.com/client/v4/zones/"
err = validateConfig(&cloudflareConfig)
if err != nil {
logger.Fatalln(err)
}
minimalTLSConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
tlsTransport := &http.Transport{
TLSClientConfig: minimalTLSConfig,
}
client := &http.Client{
Transport: tlsTransport,
}
cfm := &cloudflareManager{
cloudflareConfig,
client,
"",
}
// Register interrupt handler
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go handleInterrupt(sigs, cfm)
updateCloudflareRecord(cfm)
for range time.NewTicker(time.Duration(cloudflareConfig.UpdateInterval) * time.Minute).C {
updateCloudflareRecord(cfm)
}
logger.Println("done")
}
func handleInterrupt(signalChannel chan os.Signal, cfm *cloudflareManager) {
<-signalChannel
logger.Println("Shutting down, performing cleanup operations")
if cfm.Remove {
logger.Println("Removing DNS records")
records, err := cfm.GetDNSRecords()
if err != nil {
logger.Fatalln(err)
}
for _, name := range cfm.RecordNames {
if record, ok := records[name]; ok {
logger.Printf("Deleting %v", record.Name)
cfm.DeleteDNSRecord(record)
}
}
}
logger.Println("Exiting")
os.Exit(0)
}
func updateCloudflareRecord(cfm *cloudflareManager) {
// Get an updated record
updatedIPAddress, err := getCurrentIPAddress(cfm.Client)
if err != nil {
logger.Println(err)
return
}
if updatedIPAddress != cfm.CurrentIPAddress {
logger.Printf("IP Address changed from %v, to %v\n", cfm.CurrentIPAddress, updatedIPAddress)
cfm.CurrentIPAddress = updatedIPAddress
// Get the cloudflare DNS records
records, err := cfm.GetDNSRecords()
if err != nil {
logger.Println(err)
return
}
// Range over the given RecordNames and update them, if possible
for _, name := range cfm.RecordNames {
if record, ok := records[name]; ok {
if record.Content != cfm.CurrentIPAddress {
logger.Printf("Record %v has ip address %v, updating to %v\n", name, record.Content, cfm.CurrentIPAddress)
cfm.updateDNSRecord(record, cfm.CurrentIPAddress)
} else {
logger.Printf("Record %v has correct IP Address, skipping\n", name)
}
} else {
// Record doesn't exist, create it
logger.Printf("Record %v doesn't exist, creating it with IP Address: %v\n", name, cfm.CurrentIPAddress)
cfm.CreateDNSRecord(name, A, cfm.CurrentIPAddress)
}
}
}
}
func getCurrentIPAddress(httpClient *http.Client) (string, error) {
resp, err := httpClient.Get("http://bot.whatismyipaddress.com")
if err != nil {
return "", err
}
defer resp.Body.Close()
ipAddress, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(ipAddress), nil
}
func validateConfig(cloudflareConfig *config) error {
// Validate credentials
if cloudflareConfig.Key == "" {
return errors.New("Empty API Key, exiting")
}
if cloudflareConfig.Email == "" {
return errors.New("Empty Email Address, exiting")
}
if cloudflareConfig.DomainName == "" {
return errors.New("Empty Domain Name, exiting")
}
if len(cloudflareConfig.RecordNames) == 0 {
return errors.New("No records to update, exiting")
}
if cloudflareConfig.UpdateInterval < 5 {
return errors.New("Update interval cannot be less than 5 minutes")
}
logger.Printf("Updating records for: %v", cloudflareConfig.RecordNames)
// Check for correct config values
logger.Printf("Setting update interval to %v minutes\n", cloudflareConfig.UpdateInterval)
return nil
}