Skip to content

Commit

Permalink
Merge pull request #57 from mietzen/IPv6-support-for-fritzbox
Browse files Browse the repository at this point in the history
Also get IPv6 address from fritzbox, closes #55
  • Loading branch information
mietzen authored May 11, 2024
2 parents cc9f003 + 69506f6 commit a5092bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 8 additions & 7 deletions porkbun_ddns/helpers.py
Original file line number Diff line number Diff line change
@@ -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:
----
Expand All @@ -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"')
Expand All @@ -36,7 +37,7 @@ def get_ips_from_fritzbox(fritzbox_ip):
data = '<?xml version="1.0" encoding="utf-8"?>' + \
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' + \
"<s:Body>" + \
"<u:" + schema + ' xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" />' + \
'<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" />' + \
"</s:Body>" + \
"</s:Envelope>"
req.data = data.encode("utf8")
Expand Down
5 changes: 4 additions & 1 deletion porkbun_ddns/porkbun_ddns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a5092bf

Please sign in to comment.