From d60ce5ff5318ed0012669e2c7555f7252d42d42d Mon Sep 17 00:00:00 2001 From: Nils Stein <31704359+mietzen@users.noreply.github.com> Date: Sat, 11 May 2024 13:53:26 +0200 Subject: [PATCH] Also get IPv6 address from fritzbox, closes #55 --- porkbun_ddns/helpers.py | 15 ++++++++------- porkbun_ddns/porkbun_ddns.py | 5 ++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/porkbun_ddns/helpers.py b/porkbun_ddns/helpers.py index ae658d9..d7b40a4 100644 --- a/porkbun_ddns/helpers.py +++ b/porkbun_ddns/helpers.py @@ -1,12 +1,9 @@ -import logging import urllib.request import xml.etree.ElementTree as ET -logger = logging.getLogger() - -def get_ips_from_fritzbox(fritzbox_ip): - """Retrieves the IP address of the Fritzbox router's external network interface. +def get_ips_from_fritzbox(fritzbox_ip, ip_version=4): + """Retrieves the IP addresses of the Fritzbox router's external network interface. Args: ---- @@ -23,11 +20,15 @@ def get_ips_from_fritzbox(fritzbox_ip): ValueError: If the provided `fritzbox_ip` is not a valid IP address. AttributeError: If the requested field is not found in the XML response. - """ + schema = "GetExternalIPAddress" field = "NewExternalIPAddress" + if ip_version == 6: + schema = "X_AVM_DE_GetExternalIPv6Address" + field = "NewExternalIPv6Address" + req = urllib.request.Request( "http://" + fritzbox_ip + ":49000/igdupnp/control/WANIPConn1") req.add_header("Content-Type", 'text/xml; charset="utf-8"') @@ -36,7 +37,7 @@ def get_ips_from_fritzbox(fritzbox_ip): data = '' + \ '' + \ "" + \ - "' + \ + '' + \ "" + \ "" req.data = data.encode("utf8") diff --git a/porkbun_ddns/porkbun_ddns.py b/porkbun_ddns/porkbun_ddns.py index a9635f5..a88b016 100644 --- a/porkbun_ddns/porkbun_ddns.py +++ b/porkbun_ddns/porkbun_ddns.py @@ -55,7 +55,10 @@ def get_public_ips(self) -> list: if self.fritzbox_ip: if self.ipv4: public_ips.append( - get_ips_from_fritzbox(self.fritzbox_ip)) + get_ips_from_fritzbox(self.fritzbox_ip, ip_version=4)) + if self.ipv6: + public_ips.append( + get_ips_from_fritzbox(self.fritzbox_ip, ip_version=6)) else: if self.ipv4: urls = ["https://v4.ident.me",