-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare-dns-updater.py
156 lines (120 loc) · 5.24 KB
/
cloudflare-dns-updater.py
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
#!/usr/bin/env python
import os
import sys
import requests
import time
token = os.getenv('TOKEN')
host = os.getenv('HOST')
host = host.split(',')
timeout = int(os.getenv('TIMEOUT'))
healthcheck_file = os.getenv('HEALTHFILE')
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare
def create_healthcheck_file():
f = open(healthcheck_file, "w")
f.write("OK")
f.close()
def my_ip_address():
# This list is adjustable, uncomment the one you want to use
# url = 'http://myip.dnsomatic.com'
# url = 'http://www.trackip.net/ip'
# url = 'http://myexternalip.com/raw'
url = 'https://api.ipify.org'
try:
ip_address = requests.get(url).text
except:
exit('%s: failed' % (url))
if ip_address == '':
exit('%s: failed' % (url))
if ':' in ip_address:
ip_address_type = 'AAAA'
else:
ip_address_type = 'A'
return ip_address, ip_address_type
def do_dns_update(cf, zone_name, zone_id, dns_name, ip_address, ip_address_type):
"""Cloudflare API code - example"""
try:
params = {'name':dns_name, 'match':'all', 'type':ip_address_type}
dns_records = cf.zones.dns_records.get(zone_id, params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e))
updated = False
# update the record - unless it's already correct
for dns_record in dns_records:
old_ip_address = dns_record['content']
old_ip_address_type = dns_record['type']
if ip_address_type not in ['A', 'AAAA']:
# we only deal with A / AAAA records
continue
if ip_address_type != old_ip_address_type:
# only update the correct address type (A or AAAA)
# we don't see this becuase of the search params above
print(time.ctime(time.time()),'[ warning ] IGNORED: %s %s ; wrong address family' % (dns_name, old_ip_address))
continue
if ip_address == old_ip_address:
print(time.ctime(time.time()),'[ info ] UNCHANGED: %s %s' % (dns_name, ip_address))
updated = True
continue
proxied_state = dns_record['proxied']
# Yes, we need to update this record - we know it's the same address type
dns_record_id = dns_record['id']
dns_record = {
'name':dns_name,
'type':ip_address_type,
'content':ip_address,
'proxied':proxied_state
}
try:
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=dns_record)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dns_records.put %s - %d %s - api call failed' % (dns_name, e, e))
print(time.ctime(time.time()),'[ info ] UPDATED: %s %s -> %s' % (dns_name, old_ip_address, ip_address))
updated = True
if updated:
return
# no exsiting dns record to update - so create dns record
dns_record = {
'name':dns_name,
'type':ip_address_type,
'content':ip_address
}
try:
dns_record = cf.zones.dns_records.post(zone_id, data=dns_record)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dns_records.post %s - %d %s - api call failed' % (dns_name, e, e))
print(time.ctime(time.time()),'[ info ] CREATED: %s %s' % (dns_name, ip_address))
def main():
while True:
print(time.ctime(time.time()),'[ info ] Renewing healthcheck file...')
create_healthcheck_file()
print(time.ctime(time.time()),'[ info ] Checking to see if the public IP has changed...')
for i in host:
try:
dns_name = i
except:
exit('usage: cloudflare-dns-updater.py fqdn-hostname')
print(time.ctime(time.time()),'[ info ] Checking ' + i + '...')
host_name, zone_name = '.'.join(dns_name.split('.')[:2]), '.'.join(dns_name.split('.')[-2:])
ip_address, ip_address_type = my_ip_address()
print(time.ctime(time.time()),'[ info ] MY IP: %s %s' % (dns_name, ip_address))
cf = CloudFlare.CloudFlare(token=token)
# grab the zone identifier
try:
params = {'name':zone_name}
zones = cf.zones.get(params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
print(time.ctime(time.time()),'[ error ] /zones %d %s - api call failed' % (e, e))
except Exception as e:
print(time.ctime(time.time()),'[ error ] /zones.get - %s - api call failed' % (e))
if len(zones) == 0:
print(time.ctime(time.time()),'[ error ] /zones.get - %s - zone not found' % (zone_name))
if len(zones) != 1:
print(time.ctime(time.time()),'[ error ] /zones.get - %s - api call returned %d items' % (zone_name, len(zones)))
zone = zones[0]
zone_name = zone['name']
zone_id = zone['id']
do_dns_update(cf, zone_name, zone_id, dns_name, ip_address, ip_address_type)
print(time.ctime(time.time()),'[ info ] Now sleeping for ' + str(timeout) + ' seconds...')
time.sleep(timeout)
if __name__ == '__main__':
main()