From 34cd9e62e7c80b07ba883e3edb53d5b5bdd7a464 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 11 Jun 2020 12:46:30 +0200 Subject: [PATCH 1/7] Fixed typo in url --- src/ddns_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ddns_update.py b/src/ddns_update.py index 86b8ea6..8c6c370 100755 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -33,7 +33,7 @@ def response_successful(response_text): def ddns_update(host, key, ip): - url = f"https://dyndns.strato_.com/nic/update?hostname={host}&myip={ip}" + url = f"https://dyndns.strato.com/nic/update?hostname={host}&myip={ip}" try: response = requests.get(url, auth=(host, key), timeout=3) From 0bee1a3c9e6512013d8ff5a8e002ea20540554d1 Mon Sep 17 00:00:00 2001 From: changeme Date: Wed, 24 Jun 2020 20:29:20 +0200 Subject: [PATCH 2/7] Ignore __pycache__ --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ From 184d316501e919c491e95144c37b69963b63b59e Mon Sep 17 00:00:00 2001 From: changeme Date: Wed, 24 Jun 2020 20:29:31 +0200 Subject: [PATCH 3/7] Added some very basic unit-tests --- src/test_ddns_update.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test_ddns_update.py diff --git a/src/test_ddns_update.py b/src/test_ddns_update.py new file mode 100644 index 0000000..a091beb --- /dev/null +++ b/src/test_ddns_update.py @@ -0,0 +1,20 @@ +import unittest +from .ddns_update import response_successful + +class TestDdnsUpdate(unittest.TestCase): + + def test_response_successful(self): + self.assertTrue(response_successful("good")) + self.assertTrue(response_successful("good 1.2.3.4")) + self.assertTrue(response_successful("good abcd:abcd:abcd:abcd:abcd:abcd:abcd:abcd")) + self.assertTrue(response_successful("nochg")) + self.assertTrue(response_successful("nochg 1.2.3.4")) + self.assertTrue(response_successful("nochg abcd:abcd:abcd:abcd:abcd:abcd:abcd:abcd")) + self.assertFalse(response_successful("notfqdn")) + self.assertFalse(response_successful("nohost")) + self.assertFalse(response_successful("numhost")) + self.assertFalse(response_successful("abuse")) + self.assertFalse(response_successful("badagent")) + self.assertFalse(response_successful("dnserr")) + self.assertFalse(response_successful("911")) + From 4e0cced632f1044a697ef9017e05539eda2e54a8 Mon Sep 17 00:00:00 2001 From: changeme Date: Thu, 25 Jun 2020 19:53:02 +0200 Subject: [PATCH 4/7] Only update ip on startup if neccessary --- src/ddns_update.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ddns_update.py b/src/ddns_update.py index 8c6c370..099ad2c 100755 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -8,6 +8,7 @@ from os import environ from netifaces import interfaces, ifaddresses, AF_INET6 from ipaddress import ip_address from time import sleep +import socket logging.basicConfig(level=logging.INFO, format="%(asctime)-15s - %(name)s %(levelname)-8s: %(message)s") log = logging.getLogger("ddns updater") @@ -72,7 +73,9 @@ def ddns_update(host, key, ip): help="The ddns authorization key", ) def loop_ddns_update(host, key): - last_ip = None + last_ip = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0] + log.debug(f"current registered address: {last_ip}") + while True: current_ip = get_global_ipv6() if current_ip != last_ip: @@ -84,4 +87,5 @@ def loop_ddns_update(host, key): if __name__ == "__main__": + log.info("starting...") loop_ddns_update() From 53ff70bbf31d23214872730f4bba3e313be0b2d5 Mon Sep 17 00:00:00 2001 From: changeme Date: Thu, 25 Jun 2020 19:55:23 +0200 Subject: [PATCH 5/7] Used click's own environment variable handling --- src/ddns_update.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ddns_update.py b/src/ddns_update.py index 099ad2c..9de2843 100755 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -63,13 +63,11 @@ def ddns_update(host, key, ip): @click.option( "--host", required=True, - default=lambda: environ.get("DDNS_HOST", None), help="The dns name to update", ) @click.option( "--key", required=True, - default=lambda: environ.get("DDNS_KEY", None), help="The ddns authorization key", ) def loop_ddns_update(host, key): @@ -88,4 +86,4 @@ def loop_ddns_update(host, key): if __name__ == "__main__": log.info("starting...") - loop_ddns_update() + loop_ddns_update(auto_envvar_prefix="DDNS") From 08a02dde6db6b138b25952e7145e2f514c4d63c0 Mon Sep 17 00:00:00 2001 From: changeme Date: Thu, 25 Jun 2020 20:09:41 +0200 Subject: [PATCH 6/7] Log ip --- src/ddns_update.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ddns_update.py b/src/ddns_update.py index 9de2843..50a51b5 100755 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -46,7 +46,7 @@ def ddns_update(host, key, ip): response.raise_for_status() if not response_successful(response.text): - raise RequestException("update failed") + raise RequestException(f"update to {ip} failed") except RequestException as error: log.error(response.text.strip()) @@ -54,7 +54,7 @@ def ddns_update(host, key, ip): return False log.debug(response.text.strip()) - log.info("update successful") + log.info("update to {ip} successful") return True @@ -72,7 +72,7 @@ def ddns_update(host, key, ip): ) def loop_ddns_update(host, key): last_ip = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0] - log.debug(f"current registered address: {last_ip}") + log.info(f"currently registered address: {last_ip}") while True: current_ip = get_global_ipv6() From 4c62fa13ad4bf6c3e4791c53da3db337585ab537 Mon Sep 17 00:00:00 2001 From: changeme Date: Thu, 25 Jun 2020 20:17:24 +0200 Subject: [PATCH 7/7] Fixed logging and ip comparison --- src/ddns_update.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ddns_update.py b/src/ddns_update.py index 50a51b5..befd4ae 100755 --- a/src/ddns_update.py +++ b/src/ddns_update.py @@ -54,7 +54,7 @@ def ddns_update(host, key, ip): return False log.debug(response.text.strip()) - log.info("update to {ip} successful") + log.info(f"update to {ip} successful") return True @@ -71,7 +71,7 @@ def ddns_update(host, key, ip): help="The ddns authorization key", ) def loop_ddns_update(host, key): - last_ip = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0] + last_ip = ip_address(socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]) log.info(f"currently registered address: {last_ip}") while True: