-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
288 lines (266 loc) · 6.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/glendc/go-external-ip"
"github.com/kryptoslogic/godaddy-domainclient"
"github.com/sirupsen/logrus"
)
var Version string
func main() {
handler()
}
var (
apiClient *godaddy.APIClient
)
func setup() {
basePath := os.Getenv("API_BASE")
if basePath == "" {
panic("API_BASE not set")
}
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
panic("API_KEY not set")
}
apiSecret := os.Getenv("API_SECRET")
if apiSecret == "" {
panic("API_SECRET not set")
}
var apiConfig = godaddy.NewConfiguration()
// Test
// apiConfig.BasePath = "https://api.ote-godaddy.com/"
// Prod
// apiConfig.BasePath = "https://api.godaddy.com/"
// set from env
apiConfig.BasePath = basePath
// Set auth
authString := fmt.Sprintf("sso-key %s:%s", apiKey, apiSecret)
// Set auth production
apiConfig.AddDefaultHeader("Authorization", authString)
apiClient = godaddy.NewAPIClient(apiConfig)
}
// Get preferred outbound ip of this machine
func GetOutboundIP() (net.IP, error) {
// Create the default consensus,
// using the default configuration and no logger.
consensus := externalip.DefaultConsensus(nil, nil)
// Get your IP,
// which is never <nil> when err is <nil>.
return consensus.ExternalIP()
}
func handler() (string, error) {
setup()
var whitelist []string
allowedNamesStr := os.Getenv("DOMAIN_NAMES_WHITELIST")
if allowedNamesStr == "" {
logrus.Infof("No whitelist provided, update will consider all A records for update.")
} else {
whitelist = strings.Split(allowedNamesStr, ",")
}
domain := os.Getenv("API_DOMAIN")
if domain == "" {
panic("API_DOMAIN not set")
}
ttlStr := os.Getenv("API_NEW_TTL")
var (
ttl int
e error
)
if ttlStr != "" {
ttl, e = strconv.Atoi(ttlStr)
if e != nil {
logrus.Errorf("Cannot interpret TTL input: %s", e)
ttl = 3600
} else {
logrus.Infof("Setting TTL to: %d", ttl)
}
}
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
os.Exit(1)
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
ipv4 := string(bodyBytes)
ipv6IP, _ := GetOutboundIP()
ipv6 := ""
if !strings.Contains(ipv6IP.String(), ".") {
ipv6 = ipv6IP.String() // no ipv6 is available
}
// io.Copy(os.Stdout, resp.Body)
// returns only ipv6 ?
// outbound, err := GetOutboundIP()
// if err != nil {
// panic(err)
//}
logrus.Infof("Current ipv4 IP: %s", ipv4)
currIP := ipv4
dnsRecords, _, e := apiClient.V1Api.RecordGet(context.Background(), domain, "A", "", nil)
if e != nil {
panic(e)
}
var updates []godaddy.DnsRecordCreateType
// check records
for _, r := range dnsRecords {
if len(whitelist) > 0 && !strArrContains(whitelist, r.Name) {
logrus.Warnf("%s is not in allowed names list, hence will not be updated.", r.Name)
// put back as is
updates = append(updates, godaddy.DnsRecordCreateType{
Data: r.Data,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: r.Ttl,
Weight: r.Weight,
})
continue
}
if r.Data != currIP || r.Ttl != int32(ttl) {
logrus.Warnf("Current IP has changed for %s. Updating %s -> %s", r.Name, r.Data, currIP)
// simply replace it
r.Data = currIP
if r.Ttl != int32(ttl) {
logrus.Infof("Updating TTL %d -> %d", r.Ttl, ttl)
}
updates = append(updates, godaddy.DnsRecordCreateType{
Data: currIP,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: int32(ttl),
Weight: r.Weight,
})
} else {
// put back as is
updates = append(updates, godaddy.DnsRecordCreateType{
Data: r.Data,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: r.Ttl,
Weight: r.Weight,
})
}
}
if len(updates) > 0 {
replace, e := apiClient.V1Api.RecordReplaceType(context.Background(), domain, "A", updates, nil)
if e != nil {
logrus.Errorf("Failed to replace old records. %s", e.(godaddy.GenericSwaggerError).Body())
panic(e)
}
if replace.StatusCode == 200 {
msg := fmt.Sprintf("Successfully updated %d dns records.", len(updates))
logrus.Infof(msg)
} else {
logrus.Errorf("Failed to update dns records. %+v", replace)
return "", errors.New("failed to update dns records")
}
} else {
logrus.Infof("No ipv4 update is required")
//spew.Dump(dnsRecords)
}
if ipv6 == "" {
return "no ipv6 update is need", nil // done
}
logrus.Infof("Current ipv6 IP: %s", ipv6)
currIP = ipv6
dnsRecords, _, e = apiClient.V1Api.RecordGet(context.Background(), domain, "AAAA", "", nil)
if e != nil {
panic(e)
}
var updates2 []godaddy.DnsRecordCreateType
// check records
for _, r := range dnsRecords {
if len(whitelist) > 0 && !strArrContains(whitelist, r.Name) {
logrus.Warnf("%s is not in allowed names list, hence will not be updated.", r.Name)
// put back as is
updates2 = append(updates2, godaddy.DnsRecordCreateType{
Data: r.Data,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: r.Ttl,
Weight: r.Weight,
})
continue
}
if r.Data != currIP || r.Ttl != int32(ttl) {
logrus.Warnf("Current IP has changed for %s. Updating %s -> %s", r.Name, r.Data, currIP)
// simply replace it
r.Data = currIP
if r.Ttl != int32(ttl) {
logrus.Infof("Updating TTL %d -> %d", r.Ttl, ttl)
}
updates2 = append(updates2, godaddy.DnsRecordCreateType{
Data: currIP,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: int32(ttl),
Weight: r.Weight,
})
} else {
// put back as is
updates = append(updates, godaddy.DnsRecordCreateType{
Data: r.Data,
Name: r.Name,
Port: r.Port,
Priority: r.Priority,
Protocol: r.Protocol,
Service: r.Service,
Ttl: r.Ttl,
Weight: r.Weight,
})
}
}
if len(updates2) > 0 {
replace, e := apiClient.V1Api.RecordReplaceType(context.Background(), domain, "AAAA", updates2, nil)
if e != nil {
logrus.Errorf("Failed to replace old records. %s", e.(godaddy.GenericSwaggerError).Body())
panic(e)
}
if replace.StatusCode == 200 {
msg := fmt.Sprintf("Successfully updated %d dns records.", len(updates2))
logrus.Infof(msg)
return msg, nil
} else {
logrus.Errorf("Failed to update dns records. %+v", replace)
return "", errors.New("failed to update ipv6 dns records")
}
} else {
logrus.Infof("No ipv6 update is required")
//spew.Dump(dnsRecords)
return "no update is required", nil
}
}
// Determines if a string is part of the array
func strArrContains(arr []string, s string) bool {
for _, str := range arr {
if str == s {
return true
}
}
return false
}