-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
416 lines (345 loc) · 10.2 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
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
package main
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
gdns "google.golang.org/api/dns/v1"
)
var (
fetchDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "smart_dns_updater",
Name: "fetch_method_duration_seconds",
Help: "Duration of fetch operations by method",
Buckets: prometheus.DefBuckets,
},
[]string{"fetch_method"},
)
isIPOutOfSyncDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "smart_dns_updater",
Name: "is_out_of_sync_duration_seconds",
Help: "Duration of the isIPOutOfSync function",
Buckets: prometheus.DefBuckets,
},
)
fetchErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "smart_dns_updater",
Name: "fetch_method_errors_total",
Help: "Total number of fetch errors by method",
},
[]string{"fetch_method"},
)
updateErrors = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "smart_dns_updater",
Name: "dns_updates_errors_total",
Help: "Total number of dns update errors",
},
)
updatesApplied = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "smart_dns_updater",
Name: "dns_updates_applied_total",
Help: "Total number of dns updates applied",
},
)
outOfSync = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: "smart_dns_updater",
Name: "out_of_sync",
Help: "Is DNS out of sync",
},
)
versionGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "smart_dns_updater",
Name: "build_info",
Help: "smart-dns-updater build information",
}, []string{"version", "git_commit"})
// Build information
version string = "dev"
gitSha string = "no-commit"
logLevel slog.Level = slog.LevelInfo
)
func init() {
// Register the Prometheus metrics
prometheus.MustRegister(fetchDuration)
prometheus.MustRegister(isIPOutOfSyncDuration)
prometheus.MustRegister(fetchErrors)
prometheus.MustRegister(outOfSync)
prometheus.MustRegister(updateErrors)
prometheus.MustRegister(updatesApplied)
prometheus.MustRegister(versionGauge)
versionGauge.With(prometheus.Labels{"version": version, "git_commit": gitSha}).Set(1)
// Initialize the error counters to 0
fetchErrors.WithLabelValues("dns").Add(0)
fetchErrors.WithLabelValues("http").Add(0)
updateErrors.Add(0)
// Initialize update counter to 0
updatesApplied.Add(0)
}
type Config struct {
ProjectID string `toml:"project_id"`
Zone string `toml:"zone"`
TickTime int `toml:"tick_time"`
Timeout int `toml:"timeout"`
PreferredResponse string `toml:"preferred_response"`
RecordName string `toml:"record_name"`
}
type DNSClient interface {
ExchangeContext(
ctx context.Context,
msg *dns.Msg,
address string,
) (*dns.Msg, time.Duration, error)
}
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
func fetchIPViaDNS(ctx context.Context, client DNSClient, question string) (string, error) {
timer := prometheus.NewTimer(fetchDuration.WithLabelValues("dns"))
defer timer.ObserveDuration()
msg := new(dns.Msg)
msg.SetQuestion(question, dns.TypeA)
msg.RecursionDesired = true
r, _, err := client.ExchangeContext(ctx, msg, "resolver1.opendns.com:53")
if err != nil {
fetchErrors.WithLabelValues("dns").Inc()
return "", fmt.Errorf("failed to exchange with DNS server: %w", err)
}
if len(r.Answer) == 0 {
fetchErrors.WithLabelValues("dns").Inc()
return "", fmt.Errorf("no A record found in DNS query")
}
return r.Answer[0].(*dns.A).A.String(), nil
}
func fetchIPViaHTTP(ctx context.Context, client HTTPClient) (string, error) {
timer := prometheus.NewTimer(fetchDuration.WithLabelValues("http"))
defer timer.ObserveDuration()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://ifconfig.me/ip", nil)
resp, err := client.Do(req)
if err != nil {
fetchErrors.WithLabelValues("http").Inc()
return "", fmt.Errorf("failed to get IP via HTTP: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fetchErrors.WithLabelValues("http").Inc()
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fetchErrors.WithLabelValues("http").Inc()
return "", fmt.Errorf("failed to read response body: %w", err)
}
return strings.TrimSpace(string(body)), nil
}
func loadConfig(configPath string) (*Config, error) {
config := &Config{}
configFile, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("could not open config file: %w", err)
}
defer configFile.Close()
decoder := toml.NewDecoder(configFile)
if _, err := decoder.Decode(config); err != nil {
return nil, fmt.Errorf("could not decode config file: %w", err)
}
return config, nil
}
func newLogger(logLevel *slog.Level) *slog.Logger {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: logLevel,
}))
slog.SetDefault(logger)
return logger
}
func reconcileDns(httpClient HTTPClient, dnsClient DNSClient, config *Config, logger *slog.Logger) {
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(config.Timeout)*time.Second,
)
// Synchronize the fetch calls
var wg sync.WaitGroup
var dnsIp, prodIp, httpIp string
var dnsErr, prodErr, httpErr error
wg.Add(3)
// Fetch IP via DNS for my production domain
go func() {
defer wg.Done()
prodIp, prodErr = fetchIPViaDNS(ctx, dnsClient, "imeyer.io.")
if dnsErr != nil {
logger.Error("error fetching IP from DNS", "error", prodErr)
}
}()
// Fetch IP via DNS
go func() {
defer wg.Done()
dnsIp, dnsErr = fetchIPViaDNS(ctx, dnsClient, "myip.opendns.com.")
if dnsErr != nil {
logger.Error("error fetching IP from DNS", "error", dnsErr)
}
}()
// Fetch IP via HTTP
go func() {
defer wg.Done()
httpIp, httpErr = fetchIPViaHTTP(ctx, httpClient)
if httpErr != nil {
logger.Error("error fetching IP from HTTP", "error", httpErr)
}
}()
// Wait for all goroutines to finish
wg.Wait()
// After all goroutines have returned, compare the results if no errors
if dnsErr == nil && httpErr == nil && prodErr == nil {
if ip, outofsync := isIPOutOfSync(dnsIp, httpIp, prodIp, config.PreferredResponse, logger); outofsync {
logger.Info("ips out of sync", "prod_ip", prodIp, "fetched_ip", ip)
if err := updateDNS(config, ip, logger); err != nil {
logger.Error(err.Error())
}
} else {
logger.Info("all IPs match", "dns_ip", dnsIp, "http_ip", httpIp, "prod_ip", prodIp)
}
} else {
logger.Error("errors in responses", "dns_err", dnsErr, "http_err", httpErr, "prod_err", prodErr)
}
// Cancel the context
cancel()
}
func updateDNS(config *Config, ip string, logger *slog.Logger) error {
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(config.Timeout)*time.Second,
)
defer cancel()
if ip == "" {
return fmt.Errorf("ip is empty")
}
client, err := gdns.NewService(ctx)
if err != nil {
return fmt.Errorf("failed to create DNS client: %v", err)
}
rrs := []*gdns.ResourceRecordSet{
{
Name: config.RecordName,
Rrdatas: []string{ip},
Type: "A",
Ttl: 3600,
},
}
resp, err := client.ResourceRecordSets.List(config.ProjectID, config.Zone).Name(config.RecordName).Type("A").Do()
if err != nil {
return fmt.Errorf("failed to list DNS records: %v", err)
}
changes := &gdns.Change{
Additions: rrs,
Deletions: resp.Rrsets,
}
_, err = client.Changes.Create(config.ProjectID, config.Zone, changes).Context(ctx).Do()
if err != nil {
updateErrors.Inc()
return fmt.Errorf("failed to update DNS: %v", err)
}
logger.Info("dns changes applied", "record", config.RecordName, "ip", ip)
updatesApplied.Inc()
return nil
}
func isIPOutOfSync(dns, http, prod, preferred string, logger *slog.Logger) (string, bool) {
timer := prometheus.NewTimer(isIPOutOfSyncDuration)
defer timer.ObserveDuration()
if dns != http {
logger.Warn("DNS and HTTP IPs do not match", "dns", dns, "http", http)
}
var finalIp string
switch preferred {
case "dns":
finalIp = dns
case "http":
finalIp = http
default:
finalIp = ""
}
if prod != finalIp {
outOfSync.Set(1)
return finalIp, true
}
outOfSync.Set(0)
return "", false
}
func main() {
var configPath string
var debug bool
flag.StringVar(&configPath, "config", "smart-dns-updater.toml", "path to the config file")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
flag.Parse()
if debug {
logLevel = slog.LevelDebug
}
logger := newLogger(&logLevel)
logger.Info("starting smart-dns-updater", "version", version, "git_sha", gitSha)
config, err := loadConfig(configPath)
if err != nil {
logger.Error("failed to load config", "error", err)
os.Exit(1)
}
logger.Debug("", "config", fmt.Sprintf("%#v", config))
server := &http.Server{
Addr: ":39387",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
// Expose Prometheus metrics
http.Handle("/metrics", promhttp.Handler())
go func() {
if err := server.ListenAndServe(); err != nil {
logger.Error("error spawning http server for /metrics")
}
}()
ticker := time.NewTicker(time.Duration(config.TickTime) * time.Second)
defer ticker.Stop()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
dnsClient := new(dns.Client)
httpClient := &http.Client{}
logger.Info(
"starting main thread",
"preferred_response",
config.PreferredResponse,
"fetch_timeout",
config.Timeout,
)
// reconcile once then wait for the Ticker duration
reconcileDns(httpClient, dnsClient, config, logger)
for {
select {
case sig := <-sigChan:
// Exit with the UNIX standard of 128+signal number
if sigNum, ok := sig.(syscall.Signal); ok {
s := 128 + int(sigNum)
logger.Info("Received signal, exiting gracefully", "signal", sig.String())
os.Exit(s)
} else {
// Exit with 1 implies being killed by NOT a signal
os.Exit(1)
}
case <-ticker.C:
reconcileDns(httpClient, dnsClient, config, logger)
}
}
}