-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
111 lines (91 loc) · 2.34 KB
/
update.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
package main
import (
"encoding/json"
"github.com/dr4ke616/go_cloudflare"
"log"
"os"
"time"
)
type Configuration struct {
Name string
Frequencey time.Duration
Cloudflare Cloudflare `json:"Cloudflare"`
}
type Cloudflare struct {
Email string
Token string
Domain string
RecordID string
Name string
RecordType string
}
func LoadConfiguration(config ...string) (c Configuration, err_ error) {
var err error
var file *os.File
var config_file = "config/application.json"
if len(config) > 0 {
config_file = config[0]
}
if file, err = os.Open(config_file); err != nil {
log.Println("Failed to open config file:", err)
return
}
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Configuration{}
if err = decoder.Decode(&configuration); err != nil {
log.Println("Failed to decode JSON config file:", err)
return
}
return configuration, nil
}
func Update(cloudflare *Cloudflare) {
log.Println("About to check status of IP")
ipInfo, err := trackCurrentIP()
if err != nil {
log.Fatal("Problem getting current IP Address:", err)
os.Exit(1)
}
log.Println("Your current IP info:")
log.Println("IP: ", ipInfo.IP)
log.Println("Latitude/longitude: ", ipInfo.Latlong)
log.Println("Country: ", ipInfo.Country)
log.Println("City: ", ipInfo.City)
client, err := go_cloudflare.NewClient(cloudflare.Email, cloudflare.Token)
if err != nil {
log.Fatal("Problem with clouflare client: ", err)
os.Exit(1)
}
record, err := client.RetrieveARecord(cloudflare.Domain, cloudflare.RecordID)
if err != nil {
log.Fatal("Problem with clouflare client: ", err)
os.Exit(1)
}
if record.Value == ipInfo.IP {
log.Println("Ip address match. No need to update")
return
}
err = client.UpdateRecord(cloudflare.Domain, cloudflare.RecordID, &go_cloudflare.UpdateRecord{
Content: ipInfo.IP,
Type: cloudflare.RecordType,
Name: cloudflare.Name,
})
if err != nil {
log.Fatal("Problem with clouflare client: ", err)
os.Exit(1)
}
log.Println("Ip address were different, so I updated it.")
}
func main() {
log.Println("Dynamic IP updater started")
config, err := LoadConfiguration()
if err != nil {
log.Fatal("Failed to load config file. Abort!!")
os.Exit(1)
}
log.Println("Starting", config.Name)
for {
Update(&config.Cloudflare)
time.Sleep(config.Frequencey * time.Second)
}
}