-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudflare-ddns.py
276 lines (250 loc) · 9.31 KB
/
cloudflare-ddns.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
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
import requests
import json
import sys
import signal
import os
import time
import threading
import re
import socket
import platform
class GracefulExit:
def __init__(self):
self.kill_now = threading.Event()
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
print("Stopping main thread...")
self.kill_now.set()
def deleteEntries(type):
# Helper function for deleting A or AAAA records
# in the case of no IPv4 or IPv6 connection, yet
# existing A or AAAA records are found.
for option in config["cloudflare"]:
answer = cf_api(
"zones/" + option['zone_id'] +
"/dns_records?per_page=100&type=" + type,
"GET", option)
if answer is None or answer["result"] is None:
time.sleep(5)
return
for record in answer["result"]:
identifier = str(record["id"])
cf_api(
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
"DELETE", option)
print("Deleted stale record " + identifier)
def checkValidIP(ip, type):
try:
if type == "ipv4":
socket.inet_pton(socket.AF_INET, ip)
elif type == "ipv6":
socket.inet_pton(socket.AF_INET6, ip)
else:
raise ValueError("IP type error, must be 'ipv4' or 'ipv6'")
return True
except socket.error:
return False
def getIPs(use_local_ip, ipv4_enabled, ipv6_enabled):
if use_local_ip:
return getLocalIPs(ipv4_enabled, ipv6_enabled)
else:
return getPublicIPs(ipv4_enabled, ipv6_enabled)
def getLocalIPs(ipv4_enabled, ipv6_enabled):
# get local IPs
if platform.system() == "Linux":
p = os.popen("hostname -I")
ip_list = str(p.read()).strip().split(' ')
elif platform.system() == "Windows":
assert socket.gethostname()
nets = socket.getaddrinfo(socket.gethostname(), None)
assert nets
ip_list = []
for net in nets:
ip_list.append(net[-1][0])
else:
raise ValueError("os type error, check result of platform.system()")
ips = {}
forbid_prefix = ["127.", "172.", "192.", "fe80:"]
for ip in ip_list:
if any(list(prefix in ip for prefix in forbid_prefix)):
continue
# TODO: roughly use the first mathing IP
if "ipv4" not in ips.keys() and ipv4_enabled and checkValidIP(ip, type="ipv4"):
ips["ipv4"] = {
"type": "A",
"ip": ip
}
if "ipv6" not in ips.keys() and ipv6_enabled and checkValidIP(ip, type="ipv6"):
ips["ipv6"] = {
"type": "AAAA",
"ip": ip
}
return ips
def getPublicIPs(ipv4_enabled, ipv6_enabled):
# get public IPs
a = None
aaaa = None
if ipv4_enabled:
try:
a = requests.get("https://1.1.1.1/cdn-cgi/trace").text.split("\n")
a.pop()
a = dict(s.split("=") for s in a)["ip"]
except Exception:
deleteEntries("A")
if ipv6_enabled:
try:
aaaa = requests.get(
"https://[2606:4700:4700::1111]/cdn-cgi/trace").text.split("\n")
aaaa.pop()
aaaa = dict(s.split("=") for s in aaaa)["ip"]
except Exception:
deleteEntries("AAAA")
ips = {}
if(a is not None):
ips["ipv4"] = {
"type": "A",
"ip": a
}
if(aaaa is not None):
ips["ipv6"] = {
"type": "AAAA",
"ip": aaaa
}
return ips
def commitRecord(ip):
for option in config["cloudflare"]:
subdomains = option["subdomains"]
response = cf_api("zones/" + option['zone_id'], "GET", option)
if response is None or response["result"]["name"] is None:
time.sleep(5)
return
base_domain_name = response["result"]["name"]
ttl = 0 # default Cloudflare TTL, 0 for auto
for subdomain_type in subdomains.keys():
if subdomain_type != ip["type"]:
continue
subdomain = subdomains[subdomain_type]
subdomain = subdomain.lower().strip()
record = {
"type": subdomain_type,
"name": subdomain,
"content": ip["ip"],
"proxied": option["proxied"],
"ttl": ttl
}
dns_records = cf_api(
"zones/" + option['zone_id'] +
"/dns_records?per_page=100&type=" + ip["type"],
"GET", option)
fqdn = base_domain_name
if subdomain:
fqdn = subdomain + "." + base_domain_name
identifier = None
modified = False
duplicate_ids = []
if dns_records is not None:
for r in dns_records["result"]:
if (r["name"] == fqdn):
if identifier:
if r["content"] == ip["ip"]:
duplicate_ids.append(identifier)
identifier = r["id"]
else:
duplicate_ids.append(r["id"])
else:
identifier = r["id"]
if r['content'] != record['content'] or r['proxied'] != record['proxied']:
modified = True
if identifier:
if modified:
print("Updating record " + str(record))
response = cf_api(
"zones/" + option['zone_id'] +
"/dns_records/" + identifier,
"PUT", option, {}, record)
else:
print("Record unchanged " + str(record))
else:
print("Adding new record " + str(record))
response = cf_api(
"zones/" + option['zone_id'] + "/dns_records", "POST", option, {}, record)
# for identifier in duplicate_ids:
# identifier = str(identifier)
# print("🗑️ Deleting stale record " + identifier)
# response = cf_api(
# "zones/" + option['zone_id'] + "/dns_records/" + identifier,
# "DELETE", option)
return True
def cf_api(endpoint, method, config, headers={}, data=False):
api_token = config['authentication']['api_token']
if api_token != '' and api_token != 'api_token_here':
headers = {
"Authorization": "Bearer " + api_token,
**headers
}
else:
headers = {
"X-Auth-Email": config['authentication']['api_key']['account_email'],
"X-Auth-Key": config['authentication']['api_key']['api_key'],
}
if(data == False):
response = requests.request(
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers)
else:
response = requests.request(
method, "https://api.cloudflare.com/client/v4/" + endpoint,
headers=headers, json=data)
if response.ok:
return response.json()
else:
print("Error sending '" + method +
"' request to '" + response.url + "':")
print(response.text)
return None
def updateIPs(ips):
assert len(ips) == 2, "Error getting IPs, len(IPs)=" + str(len(ips))
for ip in ips.values():
commitRecord(ip)
if __name__ == '__main__':
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# print("chdir to", sys.path[0])
os.chdir(sys.path[0])
# check python version
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
if(version < 3.5):
raise Exception("This script requires Python 3.5+")
# get config.json
PATH = os.getcwd() + "/"
try:
with open(PATH + "config.json") as config_file:
config = json.loads(config_file.read())
except:
raise Exception("Error reading config.json")
if config:
try:
ipv4_enabled = config["ipv4"]
ipv6_enabled = config["ipv6"]
use_local_ip = config["local_ip"]
except:
raise Exception("Please enable ipv4 or ipv6 at least one")
if(len(sys.argv) > 1):
if(sys.argv[1] == "--repeat" and len(sys.argv) > 2):
delay = int(sys.argv[2]) * 60
if ipv4_enabled and ipv6_enabled:
print("Updating IPv4 (A) & IPv6 (AAAA) records every 5 minutes")
elif ipv4_enabled and not ipv6_enabled:
print("Updating IPv4 (A) records every 5 minutes")
elif ipv6_enabled and not ipv4_enabled:
print("Updating IPv6 (AAAA) records every 5 minutes")
next_time = time.time()
killer = GracefulExit()
prev_ips = None
while True:
if killer.kill_now.wait(delay):
break
updateIPs(getIPs(use_local_ip, ipv4_enabled, ipv6_enabled))
else:
print("Error param! e.g. --repeat 5, for updating every 5 minutes")
else:
updateIPs(getIPs(use_local_ip, ipv4_enabled, ipv6_enabled))