-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (96 loc) · 2.33 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
package main
import (
"context"
"flag"
"os"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
)
var (
Version = "dev"
Commit = "none"
Date = "unknown"
BuiltBy = "dirty hands"
)
func main() {
var configFile string
var logLevel string
var runOnce bool
var version bool
flag.StringVar(&configFile, "config", "./config.yaml", "Path to config file")
flag.StringVar(&logLevel, "loglevel", "info", "Log level")
flag.BoolVar(&runOnce, "run-once", false, "disables the periodic update")
flag.BoolVar(&version, "version", false, "prints version and exits")
flag.Parse()
err := configureLogs(logLevel)
if err != nil {
log.Fatal(err)
}
log.Infof("Version: %s, Commit: %s, Date: %s, BuiltBy: %s", Version, Commit, Date, BuiltBy)
if version {
return
}
c, err := getConfig(configFile)
if err != nil {
log.Fatal(err)
}
if c.Metrics.Enabled {
go func() {
err := startMetricsServer(c.Metrics.Port)
if err != nil {
log.Fatal(err)
}
}()
}
initMetrics(c.Records)
updateRecords(c)
if !runOnce {
tick := time.Tick(time.Duration(c.UpdateIntervalMinutes) * time.Minute)
for range tick {
updateRecords(c)
}
}
}
func configureLogs(logLevel string) error {
parsedLogLevel, err := log.ParseLevel(logLevel)
if err != nil {
return err
}
log.SetLevel(parsedLogLevel)
if !term.IsTerminal(int(os.Stdout.Fd())) {
log.SetFormatter(&log.JSONFormatter{})
}
return err
}
func updateRecords(c configuration) {
clients, err := getPorkbunClients(c.PorkbunCredentials)
if err != nil {
log.Fatal(err)
}
log.Debug("Updating records")
ipv4address, ipv6address := getIpAddresses(c, wtfismyipClient{})
ctx := context.Background()
for _, record := range c.Records {
client, exists := clients[record.Credentials]
if !exists {
credentialsErrorsTotal.Inc()
log.Errorf("Credentials not found: %s", record.Credentials)
continue
}
err := updateRecord(ctx, record, client, ipv4address, ipv6address)
if err != nil {
updateErrorsTotal.WithLabelValues(record.Host, record.Domain).Inc()
log.WithFields(log.Fields{
"host": record.Host,
"domain": record.Domain,
"IPv4": record.IpV4,
"IPv6": record.IpV6,
"credentials": record.Credentials,
"IPv4Address": ipv4address,
"IPv6Address": ipv6address,
}).Error(err)
}
}
log.Debug("Records updated")
}