-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzen-cf-ddns-win.pyw
136 lines (115 loc) · 5.17 KB
/
zen-cf-ddns-win.pyw
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
#!/usr/bin/python -u
import CloudFlare
import json
import requests
import time
import logging
with open('zen-cf-ddns.conf', 'r') as f:
settings = json.load(f)
logging.basicConfig(filename=settings['log_file'], level=logging.DEBUG, format='%(asctime)s %(levelname)-s %(message)s')
def my_ip_address():
"""Cloudflare API code - example"""
# This list is adjustable - plus some v6 enabled services are needed
# 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:
logging.error('%s: failed' % url)
return
if ip_address == '':
logging.error('%s: failed' % url)
return
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, proxied):
"""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:
logging.error('/zones/dns_records %s - %d %s - api call failed' % (dns_name, e, e))
return
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
logging.info('IGNORED: %s %s ; wrong address family' % (dns_name, old_ip_address))
continue
if ip_address == old_ip_address:
logging.info(('UNCHANGED: %s %s' % (dns_name, ip_address)))
continue
# 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
}
try:
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=dns_record)
except CloudFlare.exceptions.CloudFlareAPIError as e:
logging.error('/zones.dns_records.put %s - %d %s - api call failed' % (dns_name, e, e))
return
logging.info(('UPDATED: %s %s -> %s' % (dns_name, old_ip_address, ip_address)))
return
def main():
while True:
logging.info('Verification begins')
ip_address, ip_address_type = my_ip_address()
logging.info('My IP address:' + ip_address)
try:
with open("zen-cf-ddns.cache", "r+", encoding='utf-8') as cache_file:
cache_js = json.loads(cache_file.read())
if ip_address == cache_js["ip_address"] and ip_address_type==cache_js["ip_address_type"]:
logging.info('IP unchanged')
time.sleep(settings['update_frequency'])
continue
else:
logging.info("IP changed, starting update: "+ip_address+" "+ip_address_type)
cache = {'ip_address': ip_address, 'ip_address_type': ip_address_type}
json.dump(cache, cache_file)
except Exception as exc:
with open("zen-cf-ddns.cache", "w+", encoding='utf-8') as cache_file:
logging.info("Cache empty, recreate: " + ip_address + " " + ip_address_type+"\nerror: "+exc.__str__())
cache = {'ip_address': ip_address, 'ip_address_type': ip_address_type}
json.dump(cache, cache_file, ensure_ascii=False, indent=4)
logging.info("Starting update")
for zone in settings['zones']:
cf = CloudFlare.CloudFlare(email=zone['email'], token=zone['api_key'])
try:
params = {'name': zone['name']}
cfzones = cf.zones.get(params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
logging.error('/zones %d %s - api call failed' % (e, e))
continue
except Exception as e:
logging.error('/zones.get - %s - api call failed' % e)
continue
if len(cfzones) == 0:
logging.error('/zones.get - %s - zone not found' % zone['name'])
continue
if len(cfzones) != 1:
logging.error('/zones.get - %s - api call returned %d items' % (zone['name'], len(cfzones)))
continue
cfzone = cfzones[0]
zone_name = cfzone['name']
zone_id = cfzone['id']
for a_record in zone['A_records']:
do_dns_update(cf, zone_name, zone_id, a_record['name'], ip_address, ip_address_type, a_record['proxied'])
time.sleep(settings['update_frequency'])
if __name__ == '__main__':
main()