diff --git a/adbops.py b/adbops.py index 1a3ebb3..2305290 100644 --- a/adbops.py +++ b/adbops.py @@ -243,12 +243,17 @@ async def status(dev: dict[str, Any]) -> dict[str, Any]: _addrs: dict[str, tuple[float, list[str]]] = {} +def is_tailnet(addr: str) -> bool: + """True for a Tailscale (CGNAT) IPv4 address.""" + m = re.fullmatch(r"(\d{1,3})\.(\d{1,3})(?:\.\d{1,3}){2}", addr) + return m is not None and int(m[1]) == 100 and 64 <= int(m[2]) <= 127 + + def lan_addresses(text: str) -> list[str]: """IPv4 addresses the device holds, less loopback and the tailnet range.""" found = [] for addr in INET_RE.findall(text): - a, b = (int(x) for x in addr.split(".")[:2]) - if a == 127 or (a == 100 and 64 <= b <= 127): + if addr.startswith("127.") or is_tailnet(addr): continue found.append(addr) return found @@ -257,7 +262,7 @@ def lan_addresses(text: str) -> list[str]: async def ip_alert(dev: dict[str, Any], serial: str) -> str | None: """Why devices.yml ip is stale: the device holds other LAN addresses, or only a fallback path answered.""" configured = str(dev.get("ip") or "") - if not configured: + if not configured or is_tailnet(configured): return None cached = _addrs.get(dev["name"]) if cached is None or time.monotonic() - cached[0] > ADDR_TTL_S: diff --git a/tests/test_enhancements.py b/tests/test_enhancements.py index 4135bb1..5ba80c6 100644 --- a/tests/test_enhancements.py +++ b/tests/test_enhancements.py @@ -104,6 +104,16 @@ def test_ip_alert_is_quiet_when_the_address_matches( assert run(adbops.ip_alert(SHIELD, "192.0.2.1:5555")) is None +def test_ip_alert_skips_a_device_configured_with_a_tailnet_address( + monkeypatch: pytest.MonkeyPatch, +) -> None: + calls = _read_returns(monkeypatch, IP_OUT) + tailnet = ".".join(map(str, (100, 67, 0, 1))) + dev = {**SHIELD, "name": "remote-shield", "ip": tailnet} + assert run(adbops.ip_alert(dev, f"{tailnet}:5555")) is None + assert calls == [] + + def test_ip_alert_names_a_fallback_path_when_the_device_reports_no_address( monkeypatch: pytest.MonkeyPatch, ) -> None: