Fixed purgeUnknownRecords behavior
This commit is contained in:
parent
a9d25c743a
commit
464d2792b1
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -5,6 +5,7 @@
|
|||||||
"**/.hg": true,
|
"**/.hg": true,
|
||||||
"**/CVS": true,
|
"**/CVS": true,
|
||||||
"**/.DS_Store": true,
|
"**/.DS_Store": true,
|
||||||
|
"**/Thumbs.db": true,
|
||||||
".github": true,
|
".github": true,
|
||||||
".gitignore": true,
|
".gitignore": true,
|
||||||
".vscode": true,
|
".vscode": true,
|
||||||
@ -15,5 +16,6 @@
|
|||||||
},
|
},
|
||||||
"explorerExclude.backup": null,
|
"explorerExclude.backup": null,
|
||||||
"python.linting.pylintEnabled": true,
|
"python.linting.pylintEnabled": true,
|
||||||
"python.linting.enabled": true
|
"python.linting.enabled": true,
|
||||||
|
"python.formatting.provider": "autopep8"
|
||||||
}
|
}
|
||||||
@ -18,6 +18,7 @@ import requests
|
|||||||
|
|
||||||
CONFIG_PATH = os.environ.get('CONFIG_PATH', os.getcwd() + "/")
|
CONFIG_PATH = os.environ.get('CONFIG_PATH', os.getcwd() + "/")
|
||||||
|
|
||||||
|
|
||||||
class GracefulExit:
|
class GracefulExit:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.kill_now = threading.Event()
|
self.kill_now = threading.Event()
|
||||||
@ -28,13 +29,15 @@ class GracefulExit:
|
|||||||
print("🛑 Stopping main thread...")
|
print("🛑 Stopping main thread...")
|
||||||
self.kill_now.set()
|
self.kill_now.set()
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
# existing A or AAAA records are found.
|
# existing A or AAAA records are found.
|
||||||
for option in config["cloudflare"]:
|
for option in config["cloudflare"]:
|
||||||
answer = cf_api(
|
answer = cf_api(
|
||||||
"zones/" + option['zone_id'] + "/dns_records?per_page=100&type=" + type,
|
"zones/" + option['zone_id'] +
|
||||||
|
"/dns_records?per_page=100&type=" + type,
|
||||||
"GET", option)
|
"GET", option)
|
||||||
if answer is None or answer["result"] is None:
|
if answer is None or answer["result"] is None:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
@ -46,6 +49,7 @@ def deleteEntries(type):
|
|||||||
"DELETE", option)
|
"DELETE", option)
|
||||||
print("🗑️ Deleted stale record " + identifier)
|
print("🗑️ Deleted stale record " + identifier)
|
||||||
|
|
||||||
|
|
||||||
def getIPs():
|
def getIPs():
|
||||||
a = None
|
a = None
|
||||||
aaaa = None
|
aaaa = None
|
||||||
@ -66,7 +70,8 @@ def getIPs():
|
|||||||
deleteEntries("A")
|
deleteEntries("A")
|
||||||
if ipv6_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:
|
||||||
@ -89,6 +94,7 @@ def getIPs():
|
|||||||
}
|
}
|
||||||
return ips
|
return ips
|
||||||
|
|
||||||
|
|
||||||
def commitRecord(ip):
|
def commitRecord(ip):
|
||||||
for option in config["cloudflare"]:
|
for option in config["cloudflare"]:
|
||||||
subdomains = option["subdomains"]
|
subdomains = option["subdomains"]
|
||||||
@ -108,7 +114,8 @@ def commitRecord(ip):
|
|||||||
"ttl": ttl
|
"ttl": ttl
|
||||||
}
|
}
|
||||||
dns_records = cf_api(
|
dns_records = cf_api(
|
||||||
"zones/" + option['zone_id'] + "/dns_records?per_page=100&type=" + ip["type"],
|
"zones/" + option['zone_id'] +
|
||||||
|
"/dns_records?per_page=100&type=" + ip["type"],
|
||||||
"GET", option)
|
"GET", option)
|
||||||
fqdn = base_domain_name
|
fqdn = base_domain_name
|
||||||
if subdomain:
|
if subdomain:
|
||||||
@ -133,7 +140,8 @@ def commitRecord(ip):
|
|||||||
if modified:
|
if modified:
|
||||||
print("📡 Updating record " + str(record))
|
print("📡 Updating record " + str(record))
|
||||||
response = cf_api(
|
response = cf_api(
|
||||||
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
|
"zones/" + option['zone_id'] +
|
||||||
|
"/dns_records/" + identifier,
|
||||||
"PUT", option, {}, record)
|
"PUT", option, {}, record)
|
||||||
else:
|
else:
|
||||||
print("➕ Adding new record " + str(record))
|
print("➕ Adding new record " + str(record))
|
||||||
@ -144,16 +152,17 @@ def commitRecord(ip):
|
|||||||
identifier = str(identifier)
|
identifier = str(identifier)
|
||||||
print("🗑️ Deleting stale record " + identifier)
|
print("🗑️ Deleting stale record " + identifier)
|
||||||
response = cf_api(
|
response = cf_api(
|
||||||
"zones/" + option['zone_id'] + "/dns_records/" + identifier,
|
"zones/" + option['zone_id'] +
|
||||||
|
"/dns_records/" + identifier,
|
||||||
"DELETE", option)
|
"DELETE", option)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def cf_api(endpoint, method, config, headers={}, data=False):
|
def cf_api(endpoint, method, config, headers={}, data=False):
|
||||||
api_token = config['authentication']['api_token']
|
api_token = config['authentication']['api_token']
|
||||||
if api_token != '' and api_token != 'api_token_here':
|
if api_token != '' and api_token != 'api_token_here':
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer " + api_token,
|
"Authorization": "Bearer " + api_token, **headers
|
||||||
**headers
|
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
headers = {
|
headers = {
|
||||||
@ -172,14 +181,17 @@ def cf_api(endpoint, method, config, headers={}, data=False):
|
|||||||
if response.ok:
|
if response.ok:
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
print("📈 Error sending '" + method + "' request to '" + response.url + "':")
|
print("📈 Error sending '" + method +
|
||||||
|
"' request to '" + response.url + "':")
|
||||||
print(response.text)
|
print(response.text)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def updateIPs(ips):
|
def updateIPs(ips):
|
||||||
for ip in ips.values():
|
for ip in ips.values():
|
||||||
commitRecord(ip)
|
commitRecord(ip)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
shown_ipv4_warning = False
|
shown_ipv4_warning = False
|
||||||
shown_ipv6_warning = False
|
shown_ipv6_warning = False
|
||||||
@ -196,7 +208,8 @@ if __name__ == '__main__':
|
|||||||
config = json.loads(config_file.read())
|
config = json.loads(config_file.read())
|
||||||
except:
|
except:
|
||||||
print("😡 Error reading config.json")
|
print("😡 Error reading config.json")
|
||||||
time.sleep(60) # wait 60 seconds to prevent excessive logging on docker auto restart
|
# wait 60 seconds to prevent excessive logging on docker auto restart
|
||||||
|
time.sleep(60)
|
||||||
|
|
||||||
if config is not None:
|
if config is not None:
|
||||||
try:
|
try:
|
||||||
@ -228,6 +241,7 @@ if __name__ == '__main__':
|
|||||||
if killer.kill_now.wait(delay):
|
if killer.kill_now.wait(delay):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("❓ Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
|
print("❓ Unrecognized parameter '" +
|
||||||
|
sys.argv[1] + "'. Stopping now.")
|
||||||
else:
|
else:
|
||||||
updateIPs(getIPs())
|
updateIPs(getIPs())
|
||||||
|
|||||||
Reference in New Issue
Block a user