-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dns/ddclient: Add support for altering IPv6 addresses in ddclient plu…
…gin (#4497) * Add support for altering IPv6 addresses in ddclient plugin * Refactoring of checkip * dns/ddclient - minor cleanups for #4491 * simplify network / host concat a bit * add try...except for the curl fetch in case the other end doesn't return a valid address * extend form help text for "Dynamic ipv6 host" a bit --------- Co-authored-by: SaarLAN-Pissbeutel <[email protected]> Co-authored-by: Marc Philippi <[email protected]>
- Loading branch information
1 parent
2b17488
commit 8606b35
Showing
5 changed files
with
42 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Copyright (c) 2022-2023 Ad Schellevis <[email protected]> | ||
Copyright (c) 2022-2025 Ad Schellevis <[email protected]> | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
|
@@ -67,11 +67,29 @@ def extract_address(host, txt): | |
return "" | ||
|
||
|
||
def checkip(service, proto='https', timeout='10', interface=None): | ||
def transform_ip(ip, ipv6host=None): | ||
""" Changes ipv6 addresses if interface identifier is given | ||
:param ip: ip address | ||
:param ipv6host: 64 bit interface identifier | ||
:return ipaddress.IPv4Address|ipaddress.IPv6Address | ||
:raises ValueError: If the input can not be converted to an IPaddress | ||
""" | ||
if ipv6host and ip.find(':') > 0: | ||
# extract 64 bit long prefix and add ipv6host [64]bits | ||
return ipaddress.ip_address( | ||
ipaddress.ip_network("%s/64" % ip, strict=False).network_address.exploded[0:19] + | ||
ipaddress.ip_address(ipv6host).exploded[19:] | ||
) | ||
else: | ||
return ipaddress.ip_address(ip) | ||
|
||
|
||
def checkip(service, proto='https', timeout='10', interface=None, dynipv6host=None): | ||
""" find ip address using external services defined in checkip_service_list | ||
:param proto: protocol | ||
:param timeout: timeout in seconds | ||
:param interface: bind to interface | ||
:param dynipv6host: optional partial ipv6 address | ||
:return: str | ||
""" | ||
if service.startswith('web_'): | ||
|
@@ -84,8 +102,13 @@ def checkip(service, proto='https', timeout='10', interface=None): | |
params.append(interface) | ||
url = checkip_service_list[service] % proto | ||
params.append(url) | ||
return extract_address(urlparse(url).hostname, | ||
extracted_address = extract_address(urlparse(url).hostname, | ||
subprocess.run(params, capture_output=True, text=True).stdout) | ||
try: | ||
return str(transform_ip(extracted_address, dynipv6host)) | ||
except ValueError: | ||
# invalid address | ||
return "" | ||
elif service in ['if', 'if6'] and interface is not None: | ||
# return first non private IPv[4|6] interface address | ||
ifcfg = subprocess.run(['/sbin/ifconfig', interface], capture_output=True, text=True).stdout | ||
|
@@ -94,7 +117,7 @@ def checkip(service, proto='https', timeout='10', interface=None): | |
parts = line.split() | ||
if (parts[0] == 'inet' and service == 'if') or (parts[0] == 'inet6' and service == 'if6'): | ||
try: | ||
address = ipaddress.ip_address(parts[1]) | ||
address = transform_ip(parts[1], dynipv6host) | ||
if address.is_global: | ||
return str(address) | ||
except ValueError: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters