Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
changeme | 8e19fdd081 | |
changeme | 4c62fa13ad | |
changeme | 2a3e1af50f | |
changeme | 08a02dde6d | |
changeme | 7cec842374 | |
changeme | 53ff70bbf3 | |
changeme | 4e0cced632 | |
changeme | 184d316501 | |
changeme | 0bee1a3c9e | |
mandlm | e21bb8d5fc | |
mandlm | 34cd9e62e7 |
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
|
@ -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")
|
||||||
|
|
|
@ -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"))
|
||||||
|
|
Loading…
Reference in New Issue