-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (76 loc) · 2.21 KB
/
main.go
File metadata and controls
89 lines (76 loc) · 2.21 KB
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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/digitalocean/godo"
)
func main() {
// get the token and the domains from the environment
token := os.Getenv("MYDOTOKEN")
domains, err := toPunnycode(os.Getenv("MYDODOMAINS"))
resolver := os.Getenv("MYRESOLVER")
if err != nil {
log.Fatalf("ERROR: invalid MYDOMAINS env variable: %v. Exiting.\n", err)
} else if token == "" {
log.Fatalln("ERROR: missing the MYDOTOKEN env variable. Exiting.")
} else if domains[0] == "" {
log.Fatalln("ERROR: missing the MYDODOMAINS env variable. Exiting.")
} else if resolver == "" {
resolver = "https://icanhazip.com"
log.Printf("INFO: missing the MYRESOLVER env variable, using '%v' as the resolver.\n", resolver)
}
// determine the public ip
ip, err := publicIP(resolver)
if err != nil {
log.Fatalf("ERROR: cannot determine the public IPv4 address: %v.\n", err)
}
// update the A records with the new public ip
client := godo.NewFromToken(token)
for _, d := range domains {
ctx := context.TODO()
// determine the domain
domain, err := getDomain(d)
if err != nil {
log.Fatalf("ERROR: cannot determine the domain: %v.\n", err)
}
// get an id for the subdomain
options := &godo.ListOptions{Page: 1, PerPage: 5000}
listRec, listResp, err := client.Domains.Records(ctx, domain, options)
if err != nil {
log.Fatalf("ERROR: cannot get the subdomain id: err: %v, resp: '%v'.\n", err, listResp)
}
id := 0
subdomain := ""
data := ""
for _, item := range listRec {
if fmt.Sprintf("%v.%v", item.Name, domain) == d {
id = item.ID
subdomain = item.Name
data = item.Data
}
}
// update the subdomain, if needed
decoded, err := fromPunnycode(d)
if err != nil {
decoded = d
}
if ip != data {
updateReq := &godo.DomainRecordEditRequest{
Type: "A",
Name: subdomain,
Data: ip,
TTL: 60,
}
_, resp, err := client.Domains.EditRecord(ctx, domain, id, updateReq)
if err != nil {
log.Fatalf("ERROR: cannot update the A record: err: %v, resp: '%v'.\n", err, resp)
} else {
log.Printf("INFO: updated the IP for the A record '%v'.\n", decoded)
}
} else {
log.Printf("INFO: the A record '%v' is up to date. Skipping.\n", decoded)
}
}
}