-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
97 lines (81 loc) · 2.19 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
package main
import (
"cloudflare-ip-updater/services"
"flag"
"fmt"
"strings"
"sync"
"time"
)
var (
zoneIdentifier string
filter string
authKey string
previousIP string
ipLock sync.Mutex
)
func main() {
// Define flags with custom error messages
flag.StringVar(&zoneIdentifier, "zone_identifier", "", "Cloudflare Zone Identifier (required)")
flag.StringVar(&filter, "filter", "", "Cloudflare A record filter (required)")
flag.StringVar(&authKey, "auth_key", "", "Cloudflare Authentication Key (required)")
// Parse the command line arguments
flag.Parse()
// Check if required flags are provided
if err := checkRequiredFlags(); err != nil {
fmt.Println("Error:", err)
flag.PrintDefaults()
return
}
cloudflareService := services.NewCloudflareService(zoneIdentifier, authKey, filter)
// Set up a Goroutine to run every 2 minutes
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
for ; true; <-ticker.C {
go func() {
fmt.Println("Stating IP Update Process")
// Get the current IP address
currentIP, err := services.GetIPAddress()
if err != nil {
fmt.Println("Error getting IP address:", err)
return
}
// Compare the current IP with the previous IP
ipLock.Lock()
if currentIP != previousIP {
fmt.Printf("IP has changed. Updating Cloudflare DNS records, ip: %s\n", currentIP)
records, err := cloudflareService.GetDnsRecords()
if err != nil {
fmt.Println("Error Getting A Records:", err)
return
}
err = cloudflareService.UpdateDnsRecord(records, currentIP)
if err != nil {
fmt.Println("Error Updating A Records:", err)
return
}
// Update the previous IP
previousIP = currentIP
}
ipLock.Unlock()
}()
}
// Run indefinitely
select {}
}
func checkRequiredFlags() error {
var missingFlags []string
if zoneIdentifier == "" {
missingFlags = append(missingFlags, "-zone_identifier")
}
if filter == "" {
missingFlags = append(missingFlags, "-filter")
}
if authKey == "" {
missingFlags = append(missingFlags, "-auth_key")
}
if len(missingFlags) > 0 {
return fmt.Errorf("missing or empty required flags: %s", strings.Join(missingFlags, ", "))
}
return nil
}