-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (66 loc) · 1.92 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
package main
import (
"log/slog"
"os"
"strings"
"time"
"github.com/lmittmann/tint"
"gitub.com/khaliq/ddns/internal/config"
"gitub.com/khaliq/ddns/internal/dynu"
"gitub.com/khaliq/ddns/internal/ip"
"gitub.com/khaliq/ddns/internal/utils"
)
var (
version string
buildDate string
commitSha string
)
func main() {
conf := config.New()
logLevel := utils.SetLogLevel(conf.LogLevel)
opts := &tint.Options{
Level: logLevel,
AddSource: conf.Logs.EnableSource,
}
logger := slog.New(tint.NewHandler(os.Stdout, opts))
slog.SetDefault(logger)
utils.AppInfo(version, buildDate, commitSha, conf.LogLevel)
var lastIP string
for {
// Check the IP that the domain currently resolves to
domainIP, err := ip.GetDomainIP(conf.DomainName)
if err != nil {
logger.Error("Failed to resolve domain IP", "error", err)
time.Sleep(conf.CheckInterval * time.Second)
continue
}
// Get the current public IP address
currentIP, err := ip.GetPublicIP(logger, conf)
if err != nil {
logger.Error("Failed to get public IP", "error", err)
time.Sleep(conf.CheckInterval * time.Second)
continue
}
// If the IP has changed or is being checked for the first time, update DNS
if lastIP != currentIP || domainIP != currentIP {
req, err := dynu.NewDNSRequest(conf.UserName, conf.Password, conf.DomainName, conf.GroupName, currentIP, conf.EnableGroup, logger)
if err != nil {
logger.Error("Error initializing DNS request", "error", err)
time.Sleep(conf.CheckInterval * time.Second)
continue
}
response, err := dynu.UpdateDNSRecord(req, logger)
if err != nil {
logger.Error("Error updating DNS record", "error", err)
// Exit the program if authentication fails
if strings.Contains(err.Error(), "authentication failed") {
os.Exit(1)
}
} else {
logger.Info("DNS update status", "response", response)
lastIP = currentIP
}
}
time.Sleep(conf.CheckInterval * time.Second)
}
}