Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also get IPv6 address from fritzbox, closes #55 #57

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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