From 07c381cec4690df90022e11c0574dfca78906c2f Mon Sep 17 00:00:00 2001 From: tituspijean Date: Sun, 26 Sep 2021 12:05:35 +0200 Subject: [PATCH] Use ipaddress lib to find private addresses --- bin/yunomdns | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bin/yunomdns b/bin/yunomdns index aa0697f5f..f50afd964 100755 --- a/bin/yunomdns +++ b/bin/yunomdns @@ -10,6 +10,7 @@ from time import sleep from typing import List, Dict import ifaddr +from ipaddress import ip_address from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser @@ -18,14 +19,10 @@ def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]: Returns interfaces with their associated local IPs """ - def islocal(ip: str) -> bool: - local_prefixes = ["192.168.", "10.", "172.16.", "fc00:"] - return any(ip.startswith(prefix) for prefix in local_prefixes) - interfaces = { adapter.name: { - "ipv4": [ip.ip for ip in adapter.ips if ip.is_IPv4 and islocal(ip.ip)], - "ipv6": [ip.ip[0] for ip in adapter.ips if ip.is_IPv6 and islocal(ip.ip[0])], + "ipv4": [ip.ip for ip in adapter.ips if ip.is_IPv4 and ip_address(ip.ip).is_private], + "ipv6": [ip.ip[0] for ip in adapter.ips if ip.is_IPv6 and ip_address(ip.ip[0]).is_private], } for adapter in ifaddr.get_adapters() if adapter.name != "lo"