mirror of
https://codeberg.org/privacy1st/netcup-dns
synced 2024-12-22 23:36:04 +01:00
refactor
This commit is contained in:
parent
c2b7f3284b
commit
fda4c0f170
@ -93,31 +93,37 @@ def external_ipv4(timeout=5) -> str:
|
|||||||
# so we try these endpoints last.
|
# so we try these endpoints last.
|
||||||
endpoints += ['https://ipinfo.io/ip']
|
endpoints += ['https://ipinfo.io/ip']
|
||||||
|
|
||||||
for endpoint in endpoints:
|
backup = None
|
||||||
backup = None
|
try:
|
||||||
try:
|
# Force the usage of IPv4.
|
||||||
# Force the usage of IPv4
|
# https://stackoverflow.com/a/72440253/6334421
|
||||||
# https://stackoverflow.com/a/72440253/6334421
|
#
|
||||||
#
|
# Alternatively, use urllib3: https://stackoverflow.com/a/46972341/6334421
|
||||||
# Alternatively, use urllib3: https://stackoverflow.com/a/46972341/6334421
|
backup = requests.packages.urllib3.util.connection.HAS_IPV6
|
||||||
backup = requests.packages.urllib3.util.connection.HAS_IPV6
|
requests.packages.urllib3.util.connection.HAS_IPV6 = False
|
||||||
requests.packages.urllib3.util.connection.HAS_IPV6 = False
|
|
||||||
|
|
||||||
|
for endpoint in endpoints:
|
||||||
# Timeout after 5 seconds.
|
# Timeout after 5 seconds.
|
||||||
ip = requests.get(endpoint, timeout=timeout).text.strip()
|
try:
|
||||||
|
ip = requests.get(endpoint, timeout=timeout).text.strip()
|
||||||
|
except requests.exceptions.RequestException:
|
||||||
|
continue
|
||||||
|
|
||||||
# Check if it is actually an IPv4 address.
|
# Check if it is actually an IPv4 address.
|
||||||
# Some services, such as e.g. v4.ident.me, sometimes return IPv6.
|
# Some services, such as e.g. v4.ident.me, sometimes return IPv6.
|
||||||
ipv4 = ipaddress.ip_address(ip)
|
try:
|
||||||
|
ipv4 = ipaddress.ip_address(ip)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
if not isinstance(ipv4, ipaddress.IPv4Address):
|
if not isinstance(ipv4, ipaddress.IPv4Address):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Return ip address as string.
|
# Return ip address as string.
|
||||||
return ipv4.exploded
|
return ipv4.exploded
|
||||||
except Exception as _e:
|
finally:
|
||||||
continue
|
if backup is not None:
|
||||||
finally:
|
requests.packages.urllib3.util.connection.HAS_IPV6 = backup
|
||||||
# Allow usage of IPv6 again.
|
|
||||||
if backup is not None:
|
|
||||||
requests.packages.urllib3.util.connection.HAS_IPV6 = backup
|
|
||||||
raise Exception('Could not determine public IPv4 address.')
|
raise Exception('Could not determine public IPv4 address.')
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user