Revert merge pull request #39

This commit is contained in:
Timothy Miller 2021-03-05 21:53:18 -05:00
parent 378c600084
commit 6b25c64846

View File

@ -1,4 +1,4 @@
import argparse, requests, json, sys, signal, os, time import requests, json, sys, signal, os, time
PATH = os.getcwd() + "/" PATH = os.getcwd() + "/"
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1])) version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
@ -23,6 +23,9 @@ class GracefulExit:
print("🛑 Stopping main thread...") print("🛑 Stopping main thread...")
self.kill_now = True self.kill_now = True
with open(PATH + "config.json") as config_file:
config = json.loads(config_file.read())
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
@ -75,7 +78,7 @@ def getIPs():
}) })
return ips return ips
def commitRecord(ip, config): def commitRecord(ip):
for c in config["cloudflare"]: for c in config["cloudflare"]:
subdomains = c["subdomains"] subdomains = c["subdomains"]
response = cf_api("zones/" + c['zone_id'], "GET", c) response = cf_api("zones/" + c['zone_id'], "GET", c)
@ -148,25 +151,22 @@ def cf_api(endpoint, method, config, headers={}, data=False):
return response.json() return response.json()
def updateIPs(config): def updateIPs():
for ip in getIPs(): for ip in getIPs():
commitRecord(ip, config) commitRecord(ip)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() if(len(sys.argv) > 1):
parser.add_argument("--repeat", type=bool) if(sys.argv[1] == "--repeat"):
parser.add_argument("--config-path", dest="config", type=open, default=os.path.join(PATH, "config.json")) delay = 5*60
args = parser.parse_args() print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every 5 minutes")
next_time = time.time()
if args.repeat: killer = GracefulExit()
delay = 5*60 while not killer.kill_now:
print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every 5 minutes") time.sleep(max(0, next_time - time.time()))
next_time = time.time() updateIPs()
killer = GracefulExit() next_time += (time.time() - next_time) // delay * delay + delay
while not killer.kill_now: else:
time.sleep(max(0, next_time - time.time())) print("😡 Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
updateIPs(json.load(args.config))
next_time += (time.time() - next_time) // delay * delay + delay
else: else:
updateIPs(json.load(args.config)) updateIPs()