Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI): - Network health: live probe/target matrix, status strips, incidents - Probes: add (with install command), rename, remove, latency and throughput charts - Clients merged from AdGuard, DHCP, query log and probe ARP tables; names, categories, suspicious flag, notes, per-client DNS requests - Query log, domain detail (clients, related subdomains), blocked domains via the AdGuard Home /control API Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS, internet URLs and an optional VPS; buffers results while the hub is down. Gateway and ARP from /proc/net on Linux, route/arp on macOS. Install: install.sh (hub) and install-probe.sh (generated from it), each in docker mode (compose; default on Linux hosts) or native mode (venv + systemd or launchd; default on macOS and in LXC). Both modes deploy with an update command that checks the new commit and rolls back on failure. CLAUDE.md carries shared context for Claude Code sessions on each machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
54a765510a
63 changed files with 6464 additions and 0 deletions
132
tests/test_probe.py
Normal file
132
tests/test_probe.py
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
"""The probe role: its endpoints, its checks and its report loop."""
|
||||
|
||||
import asyncio
|
||||
import socket
|
||||
import struct
|
||||
import threading
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.probe import checks
|
||||
|
||||
|
||||
def test_probe_endpoints(monkeypatch):
|
||||
monkeypatch.setenv("ROLE", "probe")
|
||||
from app.probe.server import app
|
||||
with TestClient(app) as c:
|
||||
assert c.get("/healthz").json()["role"] == "probe"
|
||||
assert c.get("/probe/ping").json()["ok"]
|
||||
blob = c.get("/probe/blob?bytes=200000")
|
||||
assert len(blob.content) == 200000
|
||||
assert c.get("/").json()["mode"] == "serve only"
|
||||
assert c.get("/probe/blob?bytes=999999999").status_code == 422
|
||||
|
||||
|
||||
def fake_dns_server(rcode=0, answers=1):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.bind(("127.0.0.1", 0))
|
||||
|
||||
def serve():
|
||||
data, addr = s.recvfrom(512)
|
||||
qid = struct.unpack("!H", data[:2])[0]
|
||||
s.sendto(struct.pack("!HHHHHH", qid, 0x8180 | rcode, 1, answers, 0, 0) + data[12:], addr)
|
||||
s.close()
|
||||
threading.Thread(target=serve, daemon=True).start()
|
||||
return s.getsockname()[1]
|
||||
|
||||
|
||||
def test_dns_check():
|
||||
for rcode, answers, ok, err in [(0, 1, True, None), (3, 0, False, "NXDOMAIN"), (0, 0, False, "empty answer")]:
|
||||
port = fake_dns_server(rcode, answers)
|
||||
r = asyncio.run(checks.dns("127.0.0.1", "example.com", "dns:x", "x", "lan", port=port))
|
||||
assert r["ok"] is ok and r["error"] == err and r["method"] == "dns"
|
||||
|
||||
|
||||
def test_dns_timeout():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.bind(("127.0.0.1", 0)) # bound but silent
|
||||
r = asyncio.run(checks.dns("127.0.0.1", "example.com", "dns:x", "x", "lan", timeout=0.3, port=s.getsockname()[1]))
|
||||
s.close()
|
||||
assert not r["ok"] and r["error"] == "timeout"
|
||||
|
||||
|
||||
def test_proc_net_parsing(tmp_path, monkeypatch):
|
||||
(tmp_path / "route").write_text(
|
||||
"Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n"
|
||||
"eth0\t00000000\t0101A8C0\t0003\t0\t0\t100\t00000000\t0\t0\t0\n"
|
||||
"eth0\t0001A8C0\t00000000\t0001\t0\t0\t100\t00FFFFFF\t0\t0\t0\n")
|
||||
(tmp_path / "arp").write_text(
|
||||
"IP address HW type Flags HW address Mask Device\n"
|
||||
"192.168.1.1 0x1 0x2 AA:bb:cc:dd:ee:ff * eth0\n"
|
||||
"192.168.1.77 0x1 0x0 00:00:00:00:00:00 * eth0\n"
|
||||
"172.18.0.2 0x1 0x2 02:42:ac:12:00:02 * br-1a2b3c\n"
|
||||
"172.17.0.3 0x1 0x2 02:42:ac:11:00:03 * docker0\n")
|
||||
monkeypatch.setattr(checks, "PROC_NET", str(tmp_path))
|
||||
assert checks.default_gateway() == "192.168.1.1"
|
||||
assert checks.neighbors() == [{"ip": "192.168.1.1", "mac": "aa:bb:cc:dd:ee:ff"}]
|
||||
|
||||
|
||||
def test_probe_reports_to_hub(hub, monkeypatch):
|
||||
"""One real cycle: the probe loop measures (with the network stubbed) and reports to the hub."""
|
||||
import httpx
|
||||
|
||||
from app.probe import server as probe
|
||||
|
||||
pid = hub.post("/api/probes", json={"name": "Kitchen"}).json()
|
||||
monkeypatch.setenv("HUB_URL", "http://hub.test")
|
||||
monkeypatch.setenv("PROBE_TOKEN", pid["token"])
|
||||
|
||||
async def no_icmp(*a, **k):
|
||||
return None
|
||||
|
||||
async def fake_dns(server, name, target, label, kind, **kw):
|
||||
return checks.m(target, label, kind, "dns", True, latency=12.0)
|
||||
monkeypatch.setattr(checks, "icmp", no_icmp)
|
||||
monkeypatch.setattr(checks, "dns", fake_dns)
|
||||
monkeypatch.setattr(checks, "default_gateway", lambda: None)
|
||||
|
||||
def via_hub(request: httpx.Request):
|
||||
r = hub.request(request.method, request.url.path, content=request.content, headers=dict(request.headers))
|
||||
return httpx.Response(r.status_code, content=r.content, headers={"content-type": "application/json"})
|
||||
real_client = httpx.AsyncClient
|
||||
monkeypatch.setattr(probe.httpx, "AsyncClient", lambda **kw: real_client(transport=httpx.MockTransport(via_hub), **kw))
|
||||
|
||||
async def one_cycle():
|
||||
sleeps = []
|
||||
|
||||
async def fake_sleep(s):
|
||||
sleeps.append(s)
|
||||
if len(sleeps) > 2:
|
||||
raise asyncio.CancelledError
|
||||
monkeypatch.setattr(probe.asyncio, "sleep", fake_sleep)
|
||||
try:
|
||||
await probe.probe_loop()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
asyncio.run(one_cycle())
|
||||
assert probe.state["registered"] is True and probe.state["config"]["name"] == "Kitchen"
|
||||
mx = hub.get("/api/health/matrix").json()
|
||||
assert any(c["target"] == "dns:1.1.1.1" and c["status"] == "ok" for c in mx["cells"])
|
||||
|
||||
|
||||
def test_macos_parsers():
|
||||
route = """ route to: default
|
||||
destination: default
|
||||
mask: default
|
||||
gateway: 192.168.68.1
|
||||
interface: en0
|
||||
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING,GLOBAL>
|
||||
"""
|
||||
assert checks.parse_route_get(route) == "192.168.68.1"
|
||||
assert checks.parse_route_get("route: writing to routing socket: not in table\n") is None
|
||||
arp = """? (192.168.68.1) at 0:ef:15:97:fc:1e on en0 ifscope [ethernet]
|
||||
? (192.168.68.20) at e0:51:d8:1c:5f:96 on en0 ifscope [ethernet]
|
||||
? (192.168.68.77) at (incomplete) on en0 ifscope [ethernet]
|
||||
? (192.168.68.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
|
||||
? (192.168.64.2) at 2a:1:3:4:5:6 on bridge100 ifscope [bridge]
|
||||
? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
|
||||
"""
|
||||
assert checks.parse_arp_an(arp) == [
|
||||
{"ip": "192.168.68.1", "mac": "00:ef:15:97:fc:1e"},
|
||||
{"ip": "192.168.68.20", "mac": "e0:51:d8:1c:5f:96"},
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue