-
Notifications
You must be signed in to change notification settings - Fork 23
/
dynamicdns.go
460 lines (399 loc) · 12.3 KB
/
dynamicdns.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dynamicdns
import (
"encoding/json"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/libdns/libdns"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(App{})
}
// App is a Caddy app that keeps your DNS records updated with the public
// IP address of your instance. It updates A and AAAA records.
type App struct {
// The sources from which to get the server's public IP address.
// Multiple sources can be specified for redundancy.
// Default: simple_http
IPSourcesRaw []json.RawMessage `json:"ip_sources,omitempty" caddy:"namespace=dynamic_dns.ip_sources inline_key=source"`
// The configuration for the DNS provider with which the DNS
// records will be updated.
DNSProviderRaw json.RawMessage `json:"dns_provider,omitempty" caddy:"namespace=dns.providers inline_key=name"`
// The record names, keyed by DNS zone, for which to update the A/AAAA records.
// Record names are relative to the zone. The zone is usually your registered
// domain name. To refer to the zone itself, use the record name of "@".
//
// For example, assuming your zone is example.com, and you want to update A/AAAA
// records for "example.com" and "www.example.com" so that they resolve to this
// Caddy instance, configure like so: `"example.com": ["@", "www"]`
Domains map[string][]string `json:"domains,omitempty"`
// If enabled, no new DNS records will be created. Only existing records will be updated.
// This means that the A or AAAA records need to be created manually ahead of time.
UpdateOnly bool `json:"update_only,omitempty"`
// If enabled, the "http" app's config will be scanned to assemble the list
// of domains for which to enable dynamic DNS updates.
DynamicDomains bool `json:"dynamic_domains,omitempty"`
// The IP versions to enable. By default, both "ipv4" and "ipv6" will be enabled.
// To disable IPv6, specify {"ipv6": false}.
Versions IPVersions `json:"versions,omitempty"`
// How frequently to check the public IP address. Default: 30m
CheckInterval caddy.Duration `json:"check_interval,omitempty"`
// The TTL to set on DNS records.
TTL caddy.Duration `json:"ttl,omitempty"`
ipSources []IPSource
dnsProvider libdns.RecordSetter
ctx caddy.Context
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (App) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "dynamic_dns",
New: func() caddy.Module { return new(App) },
}
}
// Provision sets up the app module.
func (a *App) Provision(ctx caddy.Context) error {
a.ctx = ctx
a.logger = ctx.Logger(a)
// set up the DNS provider module
if len(a.DNSProviderRaw) == 0 {
return fmt.Errorf("a DNS provider is required")
}
val, err := ctx.LoadModule(a, "DNSProviderRaw")
if err != nil {
return fmt.Errorf("loading DNS provider module: %v", err)
}
a.dnsProvider = val.(libdns.RecordSetter)
// set up the IP source module or use a default
if a.IPSourcesRaw != nil {
vals, err := ctx.LoadModule(a, "IPSourcesRaw")
if err != nil {
return fmt.Errorf("loading IP source module: %v", err)
}
for _, val := range vals.([]interface{}) {
a.ipSources = append(a.ipSources, val.(IPSource))
}
}
if len(a.ipSources) == 0 {
var sh SimpleHTTP
if err = sh.Provision(ctx); err != nil {
return err
}
a.ipSources = []IPSource{sh}
}
// make sure a check interval is set
if a.CheckInterval == 0 {
a.CheckInterval = caddy.Duration(defaultCheckInterval)
}
if time.Duration(a.CheckInterval) < time.Second {
return fmt.Errorf("check interval must be at least 1 second")
}
return nil
}
// Start starts the app module.
func (a App) Start() error {
go a.checkerLoop()
return nil
}
// Stop stops the app module.
func (a App) Stop() error {
return nil
}
// checkerLoop checks the public IP address at every check
// interval. It stops when a.ctx is cancelled.
func (a App) checkerLoop() {
ticker := time.NewTicker(time.Duration(a.CheckInterval))
defer ticker.Stop()
a.checkIPAndUpdateDNS()
for {
select {
case <-ticker.C:
a.checkIPAndUpdateDNS()
case <-a.ctx.Done():
return
}
}
}
// checkIPAndUpdateDNS checks public IP addresses and, for any IP addresses
// that are different from before, it updates DNS records accordingly.
func (a App) checkIPAndUpdateDNS() {
a.logger.Debug("beginning IP address check")
lastIPsMu.Lock()
defer lastIPsMu.Unlock()
var err error
allDomains := a.allDomains()
// if we don't know current IPs, look them up from DNS
if lastIPs == nil {
lastIPs, err = a.lookupCurrentIPsFromDNS(allDomains)
if err != nil {
// not the end of the world, but might be an extra initial API hit with the DNS provider
a.logger.Error("unable to lookup current IPs from DNS records", zap.Error(err))
}
a.logger.Debug("looked up current IPs from DNS", zap.Any("lastIPs", lastIPs))
}
// look up current address(es) from first successful IP source
var currentIPs []net.IP
for _, ipSrc := range a.ipSources {
currentIPs, err = ipSrc.GetIPs(a.ctx, a.Versions)
if len(currentIPs) == 0 {
err = fmt.Errorf("no IP addresses returned")
}
if err == nil {
break
}
a.logger.Error("looking up IP address",
zap.String("ip_source", ipSrc.(caddy.Module).CaddyModule().ID.Name()),
zap.Error(err))
}
// make sure the source returns tidy info; duplicates are wasteful
currentIPs = removeDuplicateIPs(currentIPs)
// do a diff of current and previous IPs to make DNS records to update
updatedRecsByZone := make(map[string][]libdns.Record)
for _, ip := range currentIPs {
for zone, domains := range allDomains {
for _, domain := range domains {
oldIps, found := lastIPs[libdns.AbsoluteName(domain, zone)][recordType(ip)]
if !found && a.UpdateOnly {
a.logger.Debug("record doesn't exist; skipping update",
zap.String("zone", zone),
zap.String("name", domain),
zap.String("type", recordType(ip)),
)
continue
}
if found && ipListContains(oldIps, ip) {
// IP is not different and no new domains to manage; no update needed
continue
}
updatedRecsByZone[zone] = append(updatedRecsByZone[zone], libdns.Record{
Type: recordType(ip),
Name: domain,
Value: ip.String(),
TTL: time.Duration(a.TTL),
})
}
}
}
if len(updatedRecsByZone) == 0 {
a.logger.Debug("no IP address change; no update needed")
return
}
for zone, records := range updatedRecsByZone {
for _, rec := range records {
a.logger.Info("updating DNS record",
zap.String("zone", zone),
zap.String("type", rec.Type),
zap.String("name", rec.Name),
zap.String("value", rec.Value),
zap.Duration("ttl", rec.TTL),
)
}
_, err = a.dnsProvider.SetRecords(a.ctx, zone, records)
if err != nil {
a.logger.Error("failed setting DNS record(s) with new IP address(es)",
zap.String("zone", zone),
zap.Error(err),
)
}
for _, rec := range records {
name := libdns.AbsoluteName(rec.Name, zone)
if lastIPs == nil {
lastIPs = make(domainTypeIPs)
}
if lastIPs[name] == nil {
lastIPs[name] = make(map[string][]net.IP)
}
lastIPs[name][rec.Type] = []net.IP{net.ParseIP(rec.Value)}
}
}
currentIPStrings := make([]string, len(currentIPs))
for i, val := range currentIPs {
currentIPStrings[i] = val.String()
}
a.logger.Info("finished updating DNS",
zap.Strings("current_ips", currentIPStrings))
}
// lookupCurrentIPsFromDNS looks up the current IP addresses
// from DNS records.
func (a App) lookupCurrentIPsFromDNS(domains map[string][]string) (domainTypeIPs, error) {
types := []string{recordTypeA, recordTypeAAAA}
// avoid duplicates
currentIPs := make(domainTypeIPs)
if recordGetter, ok := a.dnsProvider.(libdns.RecordGetter); ok {
for zone, names := range domains {
recs, err := recordGetter.GetRecords(a.ctx, zone)
if err != nil {
return nil, err
}
recMap := make(map[string]map[string]net.IP)
for _, r := range recs {
if r.Type != recordTypeA && r.Type != recordTypeAAAA {
continue
}
a.logger.Debug("found DNS record", zap.String("type", r.Type), zap.String("name", r.Name), zap.String("zone", zone), zap.String("value", r.Value))
ip := net.ParseIP(r.Value)
if ip != nil {
name := libdns.AbsoluteName(r.Name, zone)
if _, ok := recMap[name]; !ok {
recMap[name] = make(map[string]net.IP)
}
recMap[name][r.Type] = ip
} else {
a.logger.Error("invalid IP address found in current DNS record", zap.String("value", r.Value), zap.String("type", r.Type))
}
}
for _, n := range names {
name := libdns.AbsoluteName(n, zone)
ips := make(map[string][]net.IP)
for _, t := range types {
if ip, ok := recMap[name][t]; ok {
ips[t] = []net.IP{ip}
} else {
a.logger.Info("domain not found in DNS", zap.String("domain", name))
ips[t] = []net.IP{nilIP}
}
}
currentIPs[name] = ips
}
}
}
return currentIPs, nil
}
func (a App) lookupManagedDomains() ([]string, error) {
cai, err := a.ctx.App("http")
if err != nil {
return nil, err
}
var hosts []string
ca := cai.(*caddyhttp.App)
for _, s := range ca.Servers {
for _, r := range s.Routes {
for _, ms := range r.MatcherSets {
for _, rm := range ms {
if hs, ok := rm.(caddyhttp.MatchHost); ok {
for _, h := range hs {
hosts = append(hosts, h)
}
}
}
}
}
}
return hosts, nil
}
func (a App) allDomains() map[string][]string {
if !a.DynamicDomains {
return a.Domains
}
// Read hosts from config.
m, err := a.lookupManagedDomains()
if err != nil {
return a.Domains
}
a.logger.Info("Loaded dynamic domains", zap.Strings("domains", m))
d := make(map[string][]string)
for zone, domains := range a.Domains {
d[zone] = domains
for _, h := range m {
name, ok := func() (string, bool) {
if h == zone {
return "@", true
}
suffix := "." + zone
if n := strings.TrimSuffix(h, suffix); n != h {
return n, true
}
return "", false
}()
if !ok {
// Not in this zone.
continue
}
a.logger.Info("Adding dynamic domain", zap.String("domain", name))
d[zone] = append(d[zone], name)
}
}
return d
}
// recordType returns the DNS record type associated with the version of ip.
func recordType(ip net.IP) string {
if ip.To4() == nil {
return recordTypeAAAA
}
return recordTypeA
}
// removeDuplicateIPs returns ips without duplicates.
func removeDuplicateIPs(ips []net.IP) []net.IP {
uniqueIPs := make(map[string]net.IP)
for _, ip := range ips {
uniqueIPs[ip.String()] = ip
}
clean := make([]net.IP, 0, len(uniqueIPs))
for _, ip := range uniqueIPs {
clean = append(clean, ip)
}
return clean
}
// ipListContains returns true if list contains ip; false otherwise.
func ipListContains(list []net.IP, ip net.IP) bool {
for _, ipInList := range list {
if ipInList.Equal(ip) {
return true
}
}
return false
}
// IPVersions is the IP versions to enable for dynamic DNS.
// Versions are enabled if true or nil, set to false to disable.
type IPVersions struct {
IPv4 *bool `json:"ipv4,omitempty"`
IPv6 *bool `json:"ipv6,omitempty"`
}
// V4Enabled returns true if IPv4 is enabled.
func (ip IPVersions) V4Enabled() bool {
return ip.IPv4 == nil || *ip.IPv4
}
// V6Enabled returns true if IPv6 is enabled.
func (ip IPVersions) V6Enabled() bool {
return ip.IPv6 == nil || *ip.IPv6
}
type domainTypeIPs map[string]map[string][]net.IP
// Remember what the last IPs are so that we
// don't try to update DNS records every
// time a new config is loaded; the IPs are
// unlikely to change very often.
var (
lastIPs domainTypeIPs
lastIPsMu sync.Mutex
// Special value indicate there is a new domain to manage.
nilIP net.IP
)
const (
recordTypeA = "A"
recordTypeAAAA = "AAAA"
)
const defaultCheckInterval = 30 * time.Minute
// Interface guards
var (
_ caddy.Provisioner = (*App)(nil)
_ caddy.App = (*App)(nil)
)