diff --git a/.env.example b/.env.example index a0baa0d..f4d33fe 100644 --- a/.env.example +++ b/.env.example @@ -55,3 +55,5 @@ RETENTION_DAYS=14 # The hub address shown in probe install commands (default: the address in your browser). HUB_PUBLIC_URL= +# Where probe install commands fetch install-probe.sh (default: from this hub, /install-probe.sh). +PROBE_INSTALL_URL= diff --git a/CLAUDE.md b/CLAUDE.md index 51fad8d..15932ef 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -16,7 +16,10 @@ A home network monitor with two roles, built from one repo: While the hub is unreachable, results queue in memory and upload later. With no `HUB_URL` it is serve-only, e.g. on a VPS. -Remote: `https://git.neilbantoc.dev/services/network-monitor.git` (Gitea). The installers are fetched from `https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/`. That raw URL pattern is an assumption about this Gitea instance; if curl gets a 404, fix it in `install.sh`, `scripts/build-installers.sh`, `app/hub/settings.py` (`DEFAULT_PROBE_INSTALL_URL`) and the README. +Remote: `https://git.neilbantoc.dev/services/network-monitor.git` (Gitea). **The repo is private.** Anonymous requests to its web, API and raw URLs return 404. Machines need a read-only token (`GIT_TOKEN`, saved in their `.env`) to clone and update. + +- **Hub install:** fetch the installer with `curl -H "Authorization: token $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh`. That raw path is assumed from Gitea's URL scheme and hasn't been confirmed with a token yet. If it 404s, `git clone` the repo and run `install.sh` from the clone. +- **Probe installs:** fetch `install-probe.sh` from the hub (`http://:8000/install-probe.sh`, open even with `UI_PASSWORD`). The Add probe dialog builds that command. ## Deployment modes diff --git a/README.md b/README.md index 3e26c14..e833342 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,19 @@ Either way, the service starts on boot and restarts if it crashes. ### 1. The hub +The repo is private, so fetching the installer needs a read-only Gitea token (Settings → Applications → Access tokens, scope `read:repository`). The token is read without echoing it or saving it in history, and the installer reuses it to clone: + ```bash -curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | bash +read -rsp 'Gitea token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: token $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | bash ``` -Inside an LXC you are root, so that's it. On another Linux machine in native mode, use `| sudo env INSTALL_MODE=native bash`. +Inside an LXC you are root, so that's it. On another Linux machine in native mode, use `| sudo -E env INSTALL_MODE=native bash`. If the raw URL gives a 404 even with the token, clone and run the installer instead: + +```bash +git clone https://git.neilbantoc.dev/services/network-monitor.git /tmp/nm && bash /tmp/nm/install.sh +``` + +The token is saved in `/opt/network-monitor/.env` (`GIT_TOKEN`) so `update` can fetch. Only the update step sees it, not the running app. Then connect AdGuard Home in `/opt/network-monitor/.env`: @@ -77,19 +85,18 @@ Open `http://:8000`. The hub listens on the LAN (`HOST_BIND=0.0.0.0`) be ### 2. Probes -On the hub, open **Probes → Add probe** and give the probe a name. The hub shows a command with that probe's token. Open the hub at its LAN address, not `localhost`, so the command has a `HUB_URL` the probe can reach. Run it on the probe machine: +On the hub, open **Probes → Add probe** and give the probe a name. The hub shows a command with that probe's token. The hub serves the installer itself, so fetching it needs no Gitea access. Open the hub at its LAN address, not `localhost`, so the command has a `HUB_URL` the probe can reach. Run it on the probe machine: ```bash -# Linux host (Docker), or an LXC as root (native, detected automatically) -curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=http://192.168.1.10:8000 PROBE_TOKEN=... bash +# Linux host (Docker), an LXC as root (native, detected automatically), or macOS as your user +curl -fsSL http://192.168.1.10:8000/install-probe.sh | HUB_URL=http://192.168.1.10:8000 PROBE_TOKEN=... bash # Linux, native, not root -curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | sudo env HUB_URL=http://192.168.1.10:8000 PROBE_TOKEN=... bash - -# macOS: as your own user (it asks for sudo when it needs it) -curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=http://192.168.1.10:8000 PROBE_TOKEN=... bash +curl -fsSL http://192.168.1.10:8000/install-probe.sh | sudo env HUB_URL=http://192.168.1.10:8000 PROBE_TOKEN=... bash ``` +Because the repo is private, the installer asks for a read-only Gitea token on the terminal before it clones. To skip the prompt, put `GIT_TOKEN=...` before `bash`. + The probe's test server listens on port 8765 (`PROBE_PORT`) on all interfaces. **Ping on Linux.** Linux needs unprivileged ping allowed (`net.ipv4.ping_group_range`). The installer sets it and persists it in `/etc/sysctl.d/60-network-probe-ping.conf`. That needs root. Without it, the probe skips ping and keeps its HTTP, DNS and throughput checks. @@ -108,7 +115,7 @@ pct create 210 local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst \ --net0 name=eth0,bridge=vmbr0,ip=dhcp --unprivileged 1 --features nesting=1 --onboot 1 pct start 210 && pct enter 210 apt update && apt install -y curl # if the template lacks curl -curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=http://:8000 PROBE_TOKEN=... bash +curl -fsSL http://:8000/install-probe.sh | HUB_URL=http://:8000 PROBE_TOKEN=... bash ``` - **`nesting=1`:** Debian 12's systemd needs it in an unprivileged container. New containers get it by default on recent Proxmox. @@ -130,11 +137,10 @@ On the VPS, run `install-probe.sh` with no `HUB_URL` or `PROBE_TOKEN`, and open ### Private repo -If the repo is private, both installers ask for a read-only token. To avoid echoing it or saving it in shell history: - -```bash -read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: Bearer $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash -``` +- **Fetching:** the hub installer comes from Gitea with a token (above), and probe installers come from the hub. +- **Cloning:** both installers clone with `GIT_TOKEN` and ask for it on the terminal if they can't read the repo. +- **Storage:** the token goes into that machine's `.env` for later updates. +- **Revoking:** use a read-only token per machine, so one can be revoked without touching the others. ## Deploy changes diff --git a/app/hub/server.py b/app/hub/server.py index 6140e34..05e9133 100644 --- a/app/hub/server.py +++ b/app/hub/server.py @@ -10,7 +10,7 @@ import secrets from pathlib import Path from fastapi import APIRouter, FastAPI, HTTPException, Request -from fastapi.responses import FileResponse, JSONResponse, Response +from fastapi.responses import FileResponse, JSONResponse, PlainTextResponse, Response from fastapi.staticfiles import StaticFiles from app.hub import adguard, db, dns, health, probes, settings @@ -63,7 +63,7 @@ async def _adguard_error(_req, exc): return JSONResponse({"detail": str(exc), "code": "adguard_error"}, status_code=502) -OPEN_PATHS = ("/healthz", "/api/probe/report") +OPEN_PATHS = ("/healthz", "/api/probe/report", "/install-probe.sh", "/install.sh") @app.middleware("http") @@ -106,6 +106,18 @@ api.include_router(dns.router) app.include_router(api, prefix="/api") +ROOT = Path(__file__).resolve().parent.parent.parent + + +@app.get("/install-probe.sh", include_in_schema=False) +@app.get("/install.sh", include_in_schema=False) +def installer(request: Request) -> PlainTextResponse: + """The installers, served from the hub's own checkout so a private repo needs no raw-file + access: curl -fsSL http://:8000/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash""" + name = request.url.path.lstrip("/") + return PlainTextResponse((ROOT / name).read_text(), media_type="text/x-shellscript", headers={"Cache-Control": "no-cache"}) + + @app.get("/healthz", include_in_schema=False) def healthz() -> dict: return {"status": "ok", "version": VERSION, "role": "hub"} diff --git a/app/hub/settings.py b/app/hub/settings.py index 2d95658..87ed746 100644 --- a/app/hub/settings.py +++ b/app/hub/settings.py @@ -4,7 +4,9 @@ import os from dataclasses import dataclass from urllib.parse import urlparse -DEFAULT_PROBE_INSTALL_URL = "https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh" +# Empty: the probe install command fetches install-probe.sh from the hub itself (/install-probe.sh), +# which works for a private repo. Set PROBE_INSTALL_URL to use another location. +DEFAULT_PROBE_INSTALL_URL = "" def _list(name: str, default: str) -> list[str]: diff --git a/app/static/js/views/probes.js b/app/static/js/views/probes.js index f7b1e40..e6fdef5 100644 --- a/app/static/js/views/probes.js +++ b/app/static/js/views/probes.js @@ -13,7 +13,8 @@ export function mount(root, ctx) { function installCommand(info, token) { const hub = info.hub_url || location.origin; - return `curl -fsSL ${info.install_url} | HUB_URL=${hub} PROBE_TOKEN=${token} bash`; + const url = info.install_url || `${hub}/install-probe.sh`; + return `curl -fsSL ${url} | HUB_URL=${hub} PROBE_TOKEN=${token} bash`; } function commandBlock(cmd) { @@ -87,7 +88,7 @@ function list(root, ctx) { function showInstall(p) { const cmd = installCommand(info, p.token); const dlg = modal(`Install “${p.name}”`, el('div', { style: 'display:grid;gap:12px' }, - el('p', { style: 'margin:0' }, 'Run this on the machine that will be the probe. It starts on boot and keeps running. On Linux hosts it uses Docker (installed if needed). On macOS and inside Proxmox LXC containers it installs natively as a system service; there, Linux needs root: put sudo env before HUB_URL.'), + el('p', { style: 'margin:0' }, 'Run this on the machine that will be the probe. It starts on boot and keeps running. On Linux hosts it uses Docker (installed if needed). On macOS and inside Proxmox LXC containers it installs natively as a system service; on Linux that needs root, so outside an LXC put sudo env before HUB_URL. If the git repo is private, the installer asks for a read-only token.'), commandBlock(cmd), el('p.small.muted', { style: 'margin:0' }, `The probe reports to ${info.hub_url || location.origin}. If that address isn't reachable from the probe, change HUB_URL in the command. The command is also on the probe's page.`), ), { wide: true, footer: [el('a.btn', { href: `/probes/${p.id}`, onclick: () => dlg.close() }, 'Open probe'), el('button.btn.primary', { type: 'button', onclick: () => dlg.close() }, 'Done')] }); diff --git a/install-probe.sh b/install-probe.sh index 1fcc1d2..ddbaeb4 100755 --- a/install-probe.sh +++ b/install-probe.sh @@ -13,7 +13,7 @@ # and open PROBE_PORT (default 8765/tcp) in its firewall. # # Private repo over https: add GIT_TOKEN, read without echoing it: -# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: Bearer $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash +# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: token $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash # # Two ways to run (INSTALL_MODE): # docker The probe in a container with host networking. Default on Linux hosts. diff --git a/install.sh b/install.sh index adec0f8..b72950e 100755 --- a/install.sh +++ b/install.sh @@ -8,7 +8,7 @@ # curl -fsSL https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | sudo bash # # Private repo over https (the token is asked for, not typed into history): -# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: Bearer $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | bash +# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: token $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | bash # # Two ways to run (INSTALL_MODE): # docker The app in a container (compose). Default on Linux hosts. diff --git a/scripts/build-installers.sh b/scripts/build-installers.sh index 4594044..e70670e 100755 --- a/scripts/build-installers.sh +++ b/scripts/build-installers.sh @@ -24,7 +24,7 @@ out=install-probe.sh # and open PROBE_PORT (default 8765/tcp) in its firewall. # # Private repo over https: add GIT_TOKEN, read without echoing it: -# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: Bearer \$GIT_TOKEN" $RAW/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash +# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: token \$GIT_TOKEN" $RAW/install-probe.sh | HUB_URL=... PROBE_TOKEN=... bash # # Two ways to run (INSTALL_MODE): # docker The probe in a container with host networking. Default on Linux hosts. diff --git a/tests/test_app.py b/tests/test_app.py index 02b3a56..319787c 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -29,3 +29,13 @@ def test_password(hub, monkeypatch): assert hub.get("/api/info", auth=("admin", "s3cret")).status_code == 200 assert hub.get("/healthz").status_code == 200 assert hub.post("/api/probe/report", json={}).status_code in (401, 422) # token auth, not the password + + +def test_hub_serves_installers_without_password(hub, monkeypatch): + monkeypatch.setenv("UI_PASSWORD", "s3cret") + r = hub.get("/install-probe.sh") + assert r.status_code == 200 and r.text.startswith("#!/usr/bin/env bash") and "\nNETMON_ROLE=probe\n" in r.text + assert "\nNETMON_ROLE=hub\n" in hub.get("/install.sh").text + assert hub.get("/api/probes").status_code == 401 # the rest stays protected + monkeypatch.delenv("UI_PASSWORD") + assert hub.get("/api/probes").json()["install_url"] == "" # UI builds /install-probe.sh