🧹 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,7 +157,26 @@ 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:
|
||||||
|
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
|
||||||
|
|
||||||
|
if config is not None:
|
||||||
try:
|
try:
|
||||||
ipv4_enabled = config["a"]
|
ipv4_enabled = config["a"]
|
||||||
ipv6_enabled = config["aaaa"]
|
ipv6_enabled = config["aaaa"]
|
||||||
|
|||||||
Reference in New Issue
Block a user