🦢 Improved error message handling
This commit is contained in:
parent
47ae1238e2
commit
6fe23a2aee
@ -6,7 +6,7 @@ shown_ipv4_warning = False
|
|||||||
shown_ipv6_warning = False
|
shown_ipv6_warning = False
|
||||||
|
|
||||||
if(version < 3.5):
|
if(version < 3.5):
|
||||||
raise Exception("This script requires Python 3.5+")
|
raise Exception("🐍 This script requires Python 3.5+")
|
||||||
|
|
||||||
class GracefulExit:
|
class GracefulExit:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -30,17 +30,16 @@ def deleteEntries(type):
|
|||||||
# Helper function for deleting A or AAAA records
|
# Helper function for deleting A or AAAA records
|
||||||
# in the case of no IPv4 or IPv6 connection, yet
|
# in the case of no IPv4 or IPv6 connection, yet
|
||||||
# existing A or AAAA records are found.
|
# existing A or AAAA records are found.
|
||||||
try:
|
for option in config["cloudflare"]:
|
||||||
for c in config["cloudflare"]:
|
|
||||||
answer = cf_api(
|
answer = cf_api(
|
||||||
"zones/" + c['zone_id'] + "/dns_records?per_page=100&type=" + type, "GET", c)
|
"zones/" + option['zone_id'] + "/dns_records?per_page=100&type=" + type,
|
||||||
for r in answer["result"]:
|
"GET", option)
|
||||||
identifier = str(r["id"])
|
for record in answer["result"]:
|
||||||
response = cf_api(
|
identifier = str(record["id"])
|
||||||
"zones/" + c['zone_id'] + "/dns_records/" + identifier, "DELETE", c)
|
cf_api(
|
||||||
|
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
|
||||||
|
"DELETE", option)
|
||||||
print("🗑️ Deleted stale record " + identifier)
|
print("🗑️ Deleted stale record " + identifier)
|
||||||
except Exception:
|
|
||||||
print("🤷 No " + type + " record(s) found")
|
|
||||||
|
|
||||||
def getIPs():
|
def getIPs():
|
||||||
global shown_ipv4_warning
|
global shown_ipv4_warning
|
||||||
@ -54,7 +53,7 @@ def getIPs():
|
|||||||
except Exception:
|
except Exception:
|
||||||
if not shown_ipv4_warning:
|
if not shown_ipv4_warning:
|
||||||
shown_ipv4_warning = True
|
shown_ipv4_warning = True
|
||||||
print("😨 Warning: IPv4 not detected")
|
print("🧩 IPv4 not detected")
|
||||||
deleteEntries("A")
|
deleteEntries("A")
|
||||||
try:
|
try:
|
||||||
aaaa = requests.get("https://[2606:4700:4700::1111]/cdn-cgi/trace").text.split("\n")
|
aaaa = requests.get("https://[2606:4700:4700::1111]/cdn-cgi/trace").text.split("\n")
|
||||||
@ -63,7 +62,7 @@ def getIPs():
|
|||||||
except Exception:
|
except Exception:
|
||||||
if not shown_ipv6_warning:
|
if not shown_ipv6_warning:
|
||||||
shown_ipv6_warning = True
|
shown_ipv6_warning = True
|
||||||
print("😨 Warning: IPv6 not detected")
|
print("🧩 IPv6 not detected")
|
||||||
deleteEntries("AAAA")
|
deleteEntries("AAAA")
|
||||||
ips = {}
|
ips = {}
|
||||||
if(a is not None):
|
if(a is not None):
|
||||||
@ -79,9 +78,9 @@ def getIPs():
|
|||||||
return ips
|
return ips
|
||||||
|
|
||||||
def commitRecord(ip):
|
def commitRecord(ip):
|
||||||
for c in config["cloudflare"]:
|
for option in config["cloudflare"]:
|
||||||
subdomains = c["subdomains"]
|
subdomains = option["subdomains"]
|
||||||
response = cf_api("zones/" + c['zone_id'], "GET", c)
|
response = cf_api("zones/" + option['zone_id'], "GET", option)
|
||||||
base_domain_name = response["result"]["name"]
|
base_domain_name = response["result"]["name"]
|
||||||
ttl = 300 # default Cloudflare TTL
|
ttl = 300 # default Cloudflare TTL
|
||||||
for subdomain in subdomains:
|
for subdomain in subdomains:
|
||||||
@ -90,17 +89,19 @@ def commitRecord(ip):
|
|||||||
"type": ip["type"],
|
"type": ip["type"],
|
||||||
"name": subdomain,
|
"name": subdomain,
|
||||||
"content": ip["ip"],
|
"content": ip["ip"],
|
||||||
"proxied": c["proxied"],
|
"proxied": option["proxied"],
|
||||||
"ttl": ttl
|
"ttl": ttl
|
||||||
}
|
}
|
||||||
dns_records = cf_api(
|
dns_records = cf_api(
|
||||||
"zones/" + c['zone_id'] + "/dns_records?per_page=100&type=" + ip["type"], "GET", c)
|
"zones/" + option['zone_id'] + "/dns_records?per_page=100&type=" + ip["type"],
|
||||||
|
"GET", option)
|
||||||
fqdn = base_domain_name
|
fqdn = base_domain_name
|
||||||
if subdomain:
|
if subdomain:
|
||||||
fqdn = subdomain + "." + base_domain_name
|
fqdn = subdomain + "." + base_domain_name
|
||||||
identifier = None
|
identifier = None
|
||||||
modified = False
|
modified = False
|
||||||
duplicate_ids = []
|
duplicate_ids = []
|
||||||
|
if dns_records is not None:
|
||||||
for r in dns_records["result"]:
|
for r in dns_records["result"]:
|
||||||
if (r["name"] == fqdn):
|
if (r["name"] == fqdn):
|
||||||
if identifier:
|
if identifier:
|
||||||
@ -117,16 +118,18 @@ def commitRecord(ip):
|
|||||||
if modified:
|
if modified:
|
||||||
print("📡 Updating record " + str(record))
|
print("📡 Updating record " + str(record))
|
||||||
response = cf_api(
|
response = cf_api(
|
||||||
"zones/" + c['zone_id'] + "/dns_records/" + identifier, "PUT", c, {}, record)
|
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
|
||||||
|
"PUT", option, {}, record)
|
||||||
else:
|
else:
|
||||||
print("➕ Adding new record " + str(record))
|
print("➕ Adding new record " + str(record))
|
||||||
response = cf_api(
|
response = cf_api(
|
||||||
"zones/" + c['zone_id'] + "/dns_records", "POST", c, {}, record)
|
"zones/" + option['zone_id'] + "/dns_records", "POST", option, {}, record)
|
||||||
for identifier in duplicate_ids:
|
for identifier in duplicate_ids:
|
||||||
identifier = str(identifier)
|
identifier = str(identifier)
|
||||||
print("🗑️ Deleting stale record " + identifier)
|
print("🗑️ Deleting stale record " + identifier)
|
||||||
response = cf_api(
|
response = cf_api(
|
||||||
"zones/" + c['zone_id'] + "/dns_records/" + identifier, "DELETE", c)
|
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
|
||||||
|
"DELETE", option)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def cf_api(endpoint, method, config, headers={}, data=False):
|
def cf_api(endpoint, method, config, headers={}, data=False):
|
||||||
@ -147,9 +150,14 @@ def cf_api(endpoint, method, config, headers={}, data=False):
|
|||||||
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers)
|
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers)
|
||||||
else:
|
else:
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers, json=data)
|
method, "https://api.cloudflare.com/client/v4/" + endpoint,
|
||||||
|
headers=headers, json=data)
|
||||||
|
|
||||||
|
if response.ok:
|
||||||
return response.json()
|
return response.json()
|
||||||
|
else:
|
||||||
|
print("📈 Rate limit exceeded")
|
||||||
|
return None
|
||||||
|
|
||||||
def updateIPs(ips):
|
def updateIPs(ips):
|
||||||
for ip in ips.values():
|
for ip in ips.values():
|
||||||
@ -159,7 +167,7 @@ if __name__ == '__main__' and config is not None:
|
|||||||
if(len(sys.argv) > 1):
|
if(len(sys.argv) > 1):
|
||||||
if(sys.argv[1] == "--repeat"):
|
if(sys.argv[1] == "--repeat"):
|
||||||
delay = 60
|
delay = 60
|
||||||
print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every minute")
|
print("🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every minute")
|
||||||
next_time = time.time()
|
next_time = time.time()
|
||||||
killer = GracefulExit()
|
killer = GracefulExit()
|
||||||
prev_ips = None
|
prev_ips = None
|
||||||
|
|||||||
Reference in New Issue
Block a user