Merge pull request #39 from bjackman/set-config-path

🏁 Add a flag to modify config.json location
This commit is contained in:
Timothy Miller 2021-03-03 22:16:30 -05:00 committed by GitHub
commit 378c600084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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