🧹 Refactored code

This commit is contained in:
Timothy Miller 2021-03-17 02:33:51 -04:00
parent 04d87d3aa6
commit 9a8d7d57e1

View File

@ -1,15 +1,5 @@
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:
def __init__(self):
self.kill_now = threading.Event()
@ -20,14 +10,6 @@ class GracefulExit:
print("🛑 Stopping main thread...")
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):
# Helper function for deleting A or AAAA records
# in the case of no IPv4 or IPv6 connection, yet
@ -49,22 +31,26 @@ def deleteEntries(type):
def getIPs():
a = None
aaaa = None
if ipv6_enabled:
global ipv4_enabled
global ipv6_enabled
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:
global shown_ipv4_warning
if not shown_ipv4_warning:
shown_ipv4_warning = True
print("🧩 IPv4 not detected")
deleteEntries("A")
if ipv4_enabled:
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:
global shown_ipv6_warning
if not shown_ipv6_warning:
shown_ipv6_warning = True
print("🧩 IPv6 not detected")
@ -171,7 +157,26 @@ def updateIPs(ips):
for ip in ips.values():
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:
ipv4_enabled = config["a"]
ipv6_enabled = config["aaaa"]