-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (180 loc) · 4.4 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/cloudflare/cloudflare-go"
)
const (
EnvVarIp4InfoUrl = "IP4_INFO_URL"
Ip4InfoUrl = "https://v4.ident.me/"
EnvVarIp6InfoUrl = "IP6_INFO_URL"
Ip6InfoUrl = "https://v6.ident.me/"
EnvVarCfApiKey = "CF_API_KEY"
EnvVarCfEmail = "CF_API_EMAIL"
EnvVarCfZoneName = "CF_ZONE_NAME"
EnvVarRefresh = "REFRESH"
EnvVarEntry = "ENTRY"
)
func requiredEnvVariable(variable string) string {
if val, ok := os.LookupEnv(variable); ok {
return val
}
log.Fatalf("Required environment variable '%s' is not set!", variable)
return ""
}
func requiredEnvVariableTime(variable string) time.Duration {
value := requiredEnvVariable(variable)
t, err := strconv.Atoi(value)
if err != nil {
log.Fatalf("Environment variable '%s' is not a number", variable)
}
return time.Duration(t) * time.Second
}
func optionalEnvVariable(variable string, def string) string {
if val, ok := os.LookupEnv(variable); ok {
return val
}
return def
}
func main() {
apiKey := requiredEnvVariable(EnvVarCfApiKey)
email := requiredEnvVariable(EnvVarCfEmail)
zoneName := requiredEnvVariable(EnvVarCfZoneName)
refresh := requiredEnvVariableTime(EnvVarRefresh)
entry := requiredEnvVariable(EnvVarEntry)
ip4Url := optionalEnvVariable(EnvVarIp4InfoUrl, Ip4InfoUrl)
ip6Url := optionalEnvVariable(EnvVarIp6InfoUrl, Ip6InfoUrl)
api, err := cloudflare.New(apiKey, email)
if err != nil {
log.Fatalf("Failed to create cloudflare client: %v", err)
}
zoneId, err := api.ZoneIDByName(zoneName)
if err != nil {
log.Fatalf("Failed to find zone '%s': %v", zoneName, err)
}
log.Println("Starting flareupd...")
ticker := time.Tick(refresh)
cancel := make(chan os.Signal, 1)
signal.Notify(cancel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
ip4Updater := &FlareUpdater{
Api: api,
Fetcher: &IpFetcher{Url: ip4Url},
Type: "A",
ZoneId: zoneId,
Name: entry + "." + zoneName,
}
ip6Updater := &FlareUpdater{
Api: api,
Fetcher: &IpFetcher{Url: ip6Url},
Type: "AAAA",
ZoneId: zoneId,
Name: entry + "." + zoneName,
}
updaters := make([]*FlareUpdater, 0, 2)
if ip4Updater.Valid() {
updaters = append(updaters, ip4Updater)
} else {
log.Println("IPv4 updater is not valid: disabled")
}
if ip6Updater.Valid() {
updaters = append(updaters, ip6Updater)
} else {
log.Println("IPv6 updater is not valid: disabled")
}
for {
for _, u := range updaters {
if err := u.UpdateContent(); err != nil {
log.Printf("Failed to update record: %v", err)
}
}
select {
case <-ticker:
continue
case <-cancel:
log.Println("Stopping flareupd...")
os.Exit(0)
}
}
}
type FlareUpdater struct {
Api *cloudflare.API
Fetcher *IpFetcher
Type string
ZoneId string
Name string
}
func (f *FlareUpdater) Valid() bool {
ip, err := f.Fetcher.FetchIp()
if err != nil {
log.Printf("Failed to fetch an IP from '%s'", f.Fetcher.Url)
return false
}
parsed := net.ParseIP(ip)
if parsed == nil {
log.Printf("Failed to parse response from '%s' to an IP", f.Fetcher.Url)
log.Printf(" -- response: %s", ip)
return false
}
return true
}
func (f *FlareUpdater) UpdateContent() error {
ip, err := f.Fetcher.FetchIp()
if err != nil {
return err
}
existing, err := f.Api.DNSRecords(f.ZoneId, cloudflare.DNSRecord{Type: f.Type})
if err != nil {
return err
}
for _, e := range existing {
if e.Name == f.Name {
if e.Content == ip {
return nil
}
return f.update(e.ID, ip)
}
}
return f.create(ip)
}
func (f *FlareUpdater) update(id string, ip string) error {
record := cloudflare.DNSRecord{
ID: id,
Type: f.Type,
Name: f.Name,
Content: ip,
}
log.Printf("Updating record (%s) '%s' to IP '%s'", f.Type, f.Name, ip)
return f.Api.UpdateDNSRecord(f.ZoneId, id, record)
}
func (f *FlareUpdater) create(ip string) error {
record := cloudflare.DNSRecord{
Type: f.Type,
Name: f.Name,
Content: ip,
}
log.Printf("Updating record (%s) '%s' to IP '%s'", f.Type, f.Name, ip)
_, err := f.Api.CreateDNSRecord(f.ZoneId, record)
return err
}
type IpFetcher struct {
Url string
}
func (i *IpFetcher) FetchIp() (string, error) {
res, err := http.DefaultClient.Get(i.Url)
if err != nil {
return "", err
}
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return strings.TrimSpace(string(bs)), nil
}