-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (134 loc) · 3.88 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"flag"
"io"
"log"
"net"
"net/http"
"os"
google_oauth "golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
"google.golang.org/api/option"
)
type CloudDNSSpec struct {
svc *dns.Service
project *string
zone *string
default_ttl *int
dry_run *bool
}
func getMyIP() (string, error) {
res, err := http.Get("http://whatismyip.akamai.com")
if err != nil {
log.Fatal("HTTP Error getting our IP: ", err)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal("Error reading response body: ", err)
}
ip := net.ParseIP(string(resBody))
if ip == nil {
log.Fatalf("Non-IP returned from request: %s", ip)
}
return string(resBody), nil
}
func updateRecord(dns_spec *CloudDNSSpec, record_name string, old_ip, new_ip string) error {
log.Printf("Updating Cloud DNS: %s : %s -> %s", record_name, old_ip, new_ip)
change := &dns.Change{
Additions: []*dns.ResourceRecordSet{
{
Name: record_name,
Type: "A",
Rrdatas: []string{new_ip},
},
},
}
// Gcloud DNS shits the bed if you try to delete a record that's not there.
if old_ip != "" {
new_rr := dns.ResourceRecordSet{
Name: record_name,
Type: "A",
Rrdatas: []string{old_ip},
}
change.Deletions = append(change.Deletions, &new_rr)
}
call := dns_spec.svc.Changes.Create(*dns_spec.project, *dns_spec.zone, change)
if !*dns_spec.dry_run {
out, err := call.Do()
if err != nil {
log.Fatal("Error updating Cloud DNS: ", err)
}
log.Printf("Added [%d] and deleted [%d] records.", len(out.Additions), len(out.Deletions))
}
return nil
}
func main() {
var jsonKeyfile = flag.String("json-keyfile", "", "json credentials file for Cloud DNS")
var cloudProject = flag.String("cloud-project", "", "Google Cloud Project")
var cloudZone = flag.String("cloud-dns-zone", "", "Cloud DNS zone to operate on")
var cloudDnsRecordName = flag.String("cloud-dns-record-name", "", "Cloud DNS zone to operate on")
var defaultCloudTtl = flag.Int("cloud-dns-default-ttl", 300, "Default TTL for Cloud DNS records")
var dryRun = flag.Bool("dry-run", false, "Do not update Cloud DNS, print what would be done")
flag.Parse()
// These are required in all cases
if *cloudZone == "" {
log.Fatal("--cloud-dns-zone is required")
}
if *cloudDnsRecordName == "" {
log.Fatal("--cloud-dns-record-name is required")
}
ctx := context.Background()
creds := &google_oauth.Credentials{}
if *jsonKeyfile != "" {
jsonData, ioerror := os.ReadFile(*jsonKeyfile)
if ioerror != nil {
log.Fatal(*jsonKeyfile, ioerror)
}
creds, _ = google_oauth.CredentialsFromJSON(ctx, jsonData, "https://www.googleapis.com/auth/cloud-platform")
} else {
creds, _ = google_oauth.FindDefaultCredentials(ctx)
}
// Get project from json keyfile if present.
if creds.ProjectID != "" {
*cloudProject = creds.ProjectID
}
if *cloudProject == "" {
log.Fatal("--cloud-project is required")
}
dnsservice, err := dns.NewService(ctx, option.WithCredentials(creds))
if err != nil {
log.Fatal("Cloud DNS Error: ", err)
}
dns_spec := &CloudDNSSpec{
svc: dnsservice,
project: cloudProject,
zone: cloudZone,
default_ttl: defaultCloudTtl,
dry_run: dryRun,
}
my_ip, err := getMyIP()
if err != nil {
log.Fatalf("Error getting our IP: %s", err)
}
log.Printf("Detected IP: %s", my_ip)
current_dns, err := net.LookupIP(*cloudDnsRecordName)
current_ip := ""
if err != nil {
log.Print("Error in DNS resolution: ", err)
log.Print("Continuing...")
} else {
if len(current_dns) > 1 {
log.Fatalf("%s resolves to multiple IPs. Weird.", *cloudDnsRecordName)
}
current_ip = current_dns[0].To4().String()
if my_ip == current_ip {
log.Print("My IP matches DNS. Nothing to do.")
return
}
}
err = updateRecord(dns_spec, *cloudDnsRecordName, current_ip, my_ip)
if err != nil {
log.Fatalf("Error Updating GCloud: %s", err)
}
}