/
cloudflare.go
210 lines (176 loc) · 5.79 KB
/
cloudflare.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
package ddns
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"strings"
)
// CloudFlareClient implements the cloudflare dynamic dns client
/*
cloudflare docs:
https://api.cloudflare.com/#getting-started-resource-ids
https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
*/
type CloudFlareClient Client
type ZonesJsonResponse struct {
Zones []struct {
ID string `json:"id"`
} `json:"result"`
Success bool `json:"success"`
}
type ListDnsRecordsResponse struct {
Success bool `json:"success"`
DnsRecords []DnsRecord `json:"result"`
}
type DnsRecord struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
ZoneID string `json:"zone_id"`
ZoneName string `json:"zone_name"`
}
type DnsRecordUpdateResponse struct {
Success bool `json:"success"`
}
// UpdateIPAddresses performs the dynamic dns IP address update operation
func (client CloudFlareClient) UpdateIPAddresses(ipv4, ipv6 net.IP) error {
zonesResp, err := client.getZones()
if err != nil {
return err
}
dnsRecordsUpdated := 0
for _, zone := range zonesResp.Zones {
dnsRecordsResp, err := client.getDnsRecords(zone.ID)
if err != nil {
return err
}
for _, dnsRecord := range dnsRecordsResp.DnsRecords {
err := client.applyIpAddressUpdate(&dnsRecord, ipv4, ipv6)
if err != nil {
return err
}
dnsRecordsUpdated++
}
}
Client(client).LogIPAddressUpdate(fmt.Sprintf("%d DNS records were updated", dnsRecordsUpdated))
log.Printf("%d DNS records were updated at %s", dnsRecordsUpdated, client.ServiceConfig.ServiceType)
return nil
}
//getRequestHeaders returns headers commonly used across all Cloudflare requests
func (client CloudFlareClient) getRequestHeaders() *map[string]string {
headers := make(map[string]string)
headers["X-Auth-Email"] = client.ServiceConfig.EmailAddress
headers["X-Auth-Key"] = client.ServiceConfig.APIKey
headers["accept"] = "application/json"
headers["Content-Type"] = "application/json"
return &headers
}
//getZones returns a *ZonesJsonResponse representing all configured zones at Cloudflare
//for the specified client.ServiceConfig.EmailAddress / client.ServiceConfig.APIKey
func (client CloudFlareClient) getZones() (*ZonesJsonResponse, error) {
headers := client.getRequestHeaders()
statusCode, responseBytes, err := PerformHttpRequest(
http.MethodGet,
"https://api.cloudflare.com/client/v4/zones",
"",
"",
nil,
*headers)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("%s zones GET returned http status code %d and response \n%s",
client.ServiceConfig.ServiceType, statusCode, string(responseBytes))
}
var zonesJson ZonesJsonResponse
if err = json.Unmarshal(responseBytes, &zonesJson); err != nil {
return nil, err
}
if !zonesJson.Success {
return nil, fmt.Errorf("%s zones GET returned an unsuccessful response \n%s",
client.ServiceConfig.ServiceType, string(responseBytes))
}
log.Printf("%s zones GET returned %d zone(s) for processing",
client.ServiceConfig.ServiceType, len(zonesJson.Zones))
return &zonesJson, nil
}
//getDnsRecords returns a *ListDnsRecordsResponse representing all DNS records within the specified zoneId
func (client CloudFlareClient) getDnsRecords(zoneId string) (*ListDnsRecordsResponse, error) {
headers := client.getRequestHeaders()
statusCode, responseBytes, err := PerformHttpRequest(
http.MethodGet,
fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", zoneId),
"",
"",
nil,
*headers)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("%s dns_records GET returned http status code %d and response \n%s",
client.ServiceConfig.ServiceType, statusCode, string(responseBytes))
}
var dnsRecordsJson ListDnsRecordsResponse
if err = json.Unmarshal(responseBytes, &dnsRecordsJson); err != nil {
return nil, err
}
if !dnsRecordsJson.Success {
return nil, fmt.Errorf("%s dns_records GET returned an unsuccessful response \n%s",
client.ServiceConfig.ServiceType, string(responseBytes))
}
log.Printf("%s dns_records GET returned %d dns record(s) for processing",
client.ServiceConfig.ServiceType, len(dnsRecordsJson.DnsRecords))
return &dnsRecordsJson, nil
}
//applyIpAddressUpdate applies the DNS record update
func (client CloudFlareClient) applyIpAddressUpdate(dnsRecord *DnsRecord, ipv4, ipv6 net.IP) error {
var err error
if strings.EqualFold(client.ServiceConfig.TargetDomain, dnsRecord.Name) ||
strings.EqualFold(client.ServiceConfig.TargetDomain, dnsRecord.ZoneName) {
var ipToUse *net.IP
switch dnsRecord.Type {
case "A":
ipToUse = &ipv4
case "AAAA":
ipToUse = &ipv6
}
if ipToUse != nil {
headers := client.getRequestHeaders()
jsonBody := fmt.Sprintf(`{
"type":"%s",
"name":"%s",
"content":"%s",
"ttl":%d
}`, dnsRecord.Type, client.ServiceConfig.TargetDomain, *ipToUse, client.ServiceConfig.TTL)
statusCode, responseBytes, err := PerformHttpRequest(
http.MethodPut,
fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", dnsRecord.ZoneID, dnsRecord.ID),
"",
"",
bytes.NewBuffer([]byte(jsonBody)),
*headers)
if err != nil {
return err
}
if statusCode != http.StatusOK {
return fmt.Errorf("%s dns_records PUT returned http status code %d and response \n%s",
client.ServiceConfig.ServiceType, statusCode, string(responseBytes))
}
var dnsUpdateJson DnsRecordUpdateResponse
if err = json.Unmarshal(responseBytes, &dnsUpdateJson); err != nil {
return err
}
if !dnsUpdateJson.Success {
return fmt.Errorf("%s dns_records PUT returned an unsuccessful response \n%s",
client.ServiceConfig.ServiceType, string(responseBytes))
}
}
}
return err
}