Skip to content
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
11 changes: 8 additions & 3 deletions adbops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down