🧹 Refactored code
This commit is contained in:
parent
04d87d3aa6
commit
9a8d7d57e1
@ -1,15 +1,5 @@
|
|||||||
import requests, json, sys, signal, os, time, threading
|
import requests, json, sys, signal, os, time, threading
|
||||||
|
|
||||||
PATH = os.getcwd() + "/"
|
|
||||||
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
|
|
||||||
shown_ipv4_warning = False
|
|
||||||
shown_ipv6_warning = False
|
|
||||||
ipv4_enabled = True
|
|
||||||
ipv6_enabled = True
|
|
||||||
|
|
||||||
if(version < 3.5):
|
|
||||||
raise Exception("🐍 This script requires Python 3.5+")
|
|
||||||
|
|
||||||
class GracefulExit:
|
class GracefulExit:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.kill_now = threading.Event()
|
self.kill_now = threading.Event()
|
||||||
@ -20,14 +10,6 @@ class GracefulExit:
|
|||||||
print("🛑 Stopping main thread...")
|
print("🛑 Stopping main thread...")
|
||||||
self.kill_now.set()
|
self.kill_now.set()
|
||||||
|
|
||||||
config = None
|
|
||||||
try:
|
|
||||||
with open(PATH + "config.json") as config_file:
|
|
||||||
config = json.loads(config_file.read())
|
|
||||||
except:
|
|
||||||
print("😡 Error reading config.json")
|
|
||||||
time.sleep(60) # wait 60 seconds to prevent excessive logging on docker auto restart
|
|
||||||
|
|
||||||
def deleteEntries(type):
|
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
|
||||||
@ -49,22 +31,26 @@ def deleteEntries(type):
|
|||||||
def getIPs():
|
def getIPs():
|
||||||
a = None
|
a = None
|
||||||
aaaa = None
|
aaaa = None
|
||||||
if ipv6_enabled:
|
global ipv4_enabled
|
||||||
|
global ipv6_enabled
|
||||||
|
if ipv4_enabled:
|
||||||
try:
|
try:
|
||||||
a = requests.get("https://1.1.1.1/cdn-cgi/trace").text.split("\n")
|
a = requests.get("https://1.1.1.1/cdn-cgi/trace").text.split("\n")
|
||||||
a.pop()
|
a.pop()
|
||||||
a = dict(s.split("=") for s in a)["ip"]
|
a = dict(s.split("=") for s in a)["ip"]
|
||||||
except Exception:
|
except Exception:
|
||||||
|
global shown_ipv4_warning
|
||||||
if not shown_ipv4_warning:
|
if not shown_ipv4_warning:
|
||||||
shown_ipv4_warning = True
|
shown_ipv4_warning = True
|
||||||
print("🧩 IPv4 not detected")
|
print("🧩 IPv4 not detected")
|
||||||
deleteEntries("A")
|
deleteEntries("A")
|
||||||
if ipv4_enabled:
|
if ipv6_enabled:
|
||||||
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")
|
||||||
aaaa.pop()
|
aaaa.pop()
|
||||||
aaaa = dict(s.split("=") for s in aaaa)["ip"]
|
aaaa = dict(s.split("=") for s in aaaa)["ip"]
|
||||||
except Exception:
|
except Exception:
|
||||||
|
global shown_ipv6_warning
|
||||||
if not shown_ipv6_warning:
|
if not shown_ipv6_warning:
|
||||||
shown_ipv6_warning = True
|
shown_ipv6_warning = True
|
||||||
print("🧩 IPv6 not detected")
|
print("🧩 IPv6 not detected")
|
||||||
@ -171,31 +157,50 @@ def updateIPs(ips):
|
|||||||
for ip in ips.values():
|
for ip in ips.values():
|
||||||
commitRecord(ip)
|
commitRecord(ip)
|
||||||
|
|
||||||
if __name__ == '__main__' and config is not None:
|
if __name__ == '__main__':
|
||||||
|
PATH = os.getcwd() + "/"
|
||||||
|
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
|
||||||
|
shown_ipv4_warning = False
|
||||||
|
shown_ipv6_warning = False
|
||||||
|
ipv4_enabled = True
|
||||||
|
ipv6_enabled = True
|
||||||
|
|
||||||
|
if(version < 3.5):
|
||||||
|
raise Exception("🐍 This script requires Python 3.5+")
|
||||||
|
|
||||||
|
config = None
|
||||||
try:
|
try:
|
||||||
ipv4_enabled = config["a"]
|
with open(PATH + "config.json") as config_file:
|
||||||
ipv6_enabled = config["aaaa"]
|
config = json.loads(config_file.read())
|
||||||
except:
|
except:
|
||||||
ipv4_enabled = True
|
print("😡 Error reading config.json")
|
||||||
ipv6_enabled = True
|
time.sleep(60) # wait 60 seconds to prevent excessive logging on docker auto restart
|
||||||
print("⚙️ Individually disable IPv4 or IPv6 with new config.json options. Read more about it here: https://github.com/timothymiller/cloudflare-ddns/blob/master/README.md")
|
|
||||||
if(len(sys.argv) > 1):
|
if config is not None:
|
||||||
if(sys.argv[1] == "--repeat"):
|
try:
|
||||||
delay = 60
|
ipv4_enabled = config["a"]
|
||||||
if ipv4_enabled and ipv6_enabled:
|
ipv6_enabled = config["aaaa"]
|
||||||
print("🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every minute")
|
except:
|
||||||
elif ipv4_enabled and not ipv6_enabled:
|
ipv4_enabled = True
|
||||||
print("🕰️ Updating IPv4 (A) records every minute")
|
ipv6_enabled = True
|
||||||
elif ipv6_enabled and not ipv4_enabled:
|
print("⚙️ Individually disable IPv4 or IPv6 with new config.json options. Read more about it here: https://github.com/timothymiller/cloudflare-ddns/blob/master/README.md")
|
||||||
print("🕰️ Updating IPv6 (AAAA) records every minute")
|
if(len(sys.argv) > 1):
|
||||||
next_time = time.time()
|
if(sys.argv[1] == "--repeat"):
|
||||||
killer = GracefulExit()
|
delay = 60
|
||||||
prev_ips = None
|
if ipv4_enabled and ipv6_enabled:
|
||||||
while True:
|
print("🕰️ Updating IPv4 (A) & IPv6 (AAAA) records every minute")
|
||||||
if killer.kill_now.wait(delay):
|
elif ipv4_enabled and not ipv6_enabled:
|
||||||
break
|
print("🕰️ Updating IPv4 (A) records every minute")
|
||||||
updateIPs(getIPs())
|
elif ipv6_enabled and not ipv4_enabled:
|
||||||
|
print("🕰️ Updating IPv6 (AAAA) records every minute")
|
||||||
|
next_time = time.time()
|
||||||
|
killer = GracefulExit()
|
||||||
|
prev_ips = None
|
||||||
|
while True:
|
||||||
|
if killer.kill_now.wait(delay):
|
||||||
|
break
|
||||||
|
updateIPs(getIPs())
|
||||||
|
else:
|
||||||
|
print("❓ Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
|
||||||
else:
|
else:
|
||||||
print("❓ Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
|
updateIPs(getIPs())
|
||||||
else:
|
|
||||||
updateIPs(getIPs())
|
|
||||||
Reference in New Issue
Block a user