From 11ceb88f1bcf61a419de99426afec385e9dcf896 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sun, 11 Feb 2024 19:02:57 +0100 Subject: [PATCH] Make sure that what we are comparing is acutal IP structure --- main.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 23f0943..f97342c 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os +import re import requests from bulk_dns_update import updateAllZones, setAPIKey, getDDNSAddresszoneId from notify import notify @@ -12,11 +13,18 @@ recordedIP = None DDNS_ZONE = os.getenv('DDNS_ZONE') +def validIP(ipString): + ipRegex = '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' + return re.search(ipRegex, ipString) + def publicAddress(): global currentIP logger.info('Getting public IP from ifconfg.me...') r = requests.get('https://ifconfig.me') + if r.status_code != 200 or not validIP(r.text): + return + currentIP = r.text logger.info('Public IP: {}'.format(currentIP)) @@ -28,7 +36,7 @@ def cloudflareDDNS(): recordedIP = ddnsRecord['content'] logger.info('Found ddns recorded IP: {}'.format(recordedIP)) - if currentIP != recordedIP: + if currentIP != recordedIP and validIP(recordedIP): logger.info('Public IP has changed, updating all A records.') return True else: