Compare commits

..

11 Commits

Author SHA1 Message Date
changeme 8e19fdd081 Merge branch 'hotfix/logging' 2020-06-25 20:18:00 +02:00
changeme 4c62fa13ad Fixed logging and ip comparison 2020-06-25 20:17:24 +02:00
changeme 2a3e1af50f Merge branch 'hotfix/log-ip' 2020-06-25 20:09:52 +02:00
changeme 08a02dde6d Log ip 2020-06-25 20:09:41 +02:00
changeme 7cec842374 Merge branch 'hotfix/optional-initial-update' 2020-06-25 19:57:58 +02:00
changeme 53ff70bbf3 Used click's own environment variable handling 2020-06-25 19:57:36 +02:00
changeme 4e0cced632 Only update ip on startup if neccessary 2020-06-25 19:57:36 +02:00
changeme 184d316501 Added some very basic unit-tests 2020-06-24 20:29:31 +02:00
changeme 0bee1a3c9e Ignore __pycache__ 2020-06-24 20:29:20 +02:00
mandlm e21bb8d5fc Merge branch 'hotfix/typo-in-url' 2020-06-11 12:46:37 +02:00
mandlm 34cd9e62e7 Fixed typo in url 2020-06-11 12:46:30 +02:00
3 changed files with 30 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

@ -8,6 +8,7 @@ from os import environ
from netifaces import interfaces, ifaddresses, AF_INET6 from netifaces import interfaces, ifaddresses, AF_INET6
from ipaddress import ip_address from ipaddress import ip_address
from time import sleep from time import sleep
import socket
logging.basicConfig(level=logging.INFO, format="%(asctime)-15s - %(name)s %(levelname)-8s: %(message)s") logging.basicConfig(level=logging.INFO, format="%(asctime)-15s - %(name)s %(levelname)-8s: %(message)s")
log = logging.getLogger("ddns updater") log = logging.getLogger("ddns updater")
@ -33,7 +34,7 @@ def response_successful(response_text):
def ddns_update(host, key, ip): 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: try:
response = requests.get(url, auth=(host, key), timeout=3) response = requests.get(url, auth=(host, key), timeout=3)
@ -45,7 +46,7 @@ def ddns_update(host, key, ip):
response.raise_for_status() response.raise_for_status()
if not response_successful(response.text): if not response_successful(response.text):
raise RequestException("update failed") raise RequestException(f"update to {ip} failed")
except RequestException as error: except RequestException as error:
log.error(response.text.strip()) log.error(response.text.strip())
@ -53,7 +54,7 @@ def ddns_update(host, key, ip):
return False return False
log.debug(response.text.strip()) log.debug(response.text.strip())
log.info("update successful") log.info(f"update to {ip} successful")
return True return True
@ -62,17 +63,17 @@ def ddns_update(host, key, ip):
@click.option( @click.option(
"--host", "--host",
required=True, required=True,
default=lambda: environ.get("DDNS_HOST", None),
help="The dns name to update", help="The dns name to update",
) )
@click.option( @click.option(
"--key", "--key",
required=True, required=True,
default=lambda: environ.get("DDNS_KEY", None),
help="The ddns authorization key", help="The ddns authorization key",
) )
def loop_ddns_update(host, key): def loop_ddns_update(host, key):
last_ip = None last_ip = ip_address(socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0])
log.info(f"currently registered address: {last_ip}")
while True: while True:
current_ip = get_global_ipv6() current_ip = get_global_ipv6()
if current_ip != last_ip: if current_ip != last_ip:
@ -84,4 +85,5 @@ def loop_ddns_update(host, key):
if __name__ == "__main__": if __name__ == "__main__":
loop_ddns_update() log.info("starting...")
loop_ddns_update(auto_envvar_prefix="DDNS")

20
src/test_ddns_update.py Normal file
View File

@ -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"))