llm-host/llmhost/newt.py
neil e814a2359d install-newt: optional internet access via a Pangolin tunnel, with OIDC sign-in on the dashboard
A plain install is unchanged: LAN-only, dashboard behind the API key. `llmhost install-newt`
is the upgrade path. It installs a pinned, sha256-checked newt as a third user service and
switches the dashboard to an OIDC authorization-code + PKCE sign-in limited to allowed groups
or users. The API still takes the key. Secrets live in 0600 files, never in config.json, a
service file or argv. `llmhost remove-newt` undoes it; setup/update keep it; `logs --newt`
shows the tunnel (and `logs -f` now follows the service it was asked for).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 21:44:09 -08:00

89 lines
3.5 KiB
Python

"""Optional internet access through a Pangolin tunnel (newt). Stdlib only.
`llmhost install-newt` is the upgrade path from a LAN install. It
- downloads a pinned newt release and checks it against the sha256 below,
- runs it as a third user service ("newt", via `llmhost run-newt`), which dials out to
the Pangolin server -- nothing on this machine is opened to the internet,
- and switches the dashboard to OIDC sign-in (web/oidc.py), because once it's public the
API key is too much power to hand out: it also stops and switches the model.
newt runs in userspace (no root, no kernel WireGuard). Its site secret is kept in
~/.config/llm-host/newt_secret (0600) and handed over in its environment at start, never
written into the service file.
"""
import hashlib
import os
import platform
import tempfile
import urllib.request
from . import paths
VERSION = "1.19.0"
# From the GitHub release's asset digests for VERSION.
SHA256 = {
"linux_amd64": "6d40e223fa49d8bcfa9b7094208c58ff616997979bff7c884d0af61fcc1c5376",
"linux_arm64": "2a282e3edda7be17f092ac58aff0ec6dac9ddd152a642e8e61e6d625f2abe696",
"darwin_arm64": "bdc93f811e017affb893eab8d924dfe72ea63e1bdeaf85decec1afb711288ee5",
"darwin_amd64": "d44a4fd5526174788622e4eebea55ef268df19c85fc67766d0f27e23eb8800fa",
}
URL = "https://github.com/fosrl/newt/releases/download/{version}/newt_{plat}"
def platform_tag() -> str:
os_name = {"Linux": "linux", "Darwin": "darwin"}.get(platform.system())
arch = {"x86_64": "amd64", "amd64": "amd64", "aarch64": "arm64", "arm64": "arm64"}.get(platform.machine().lower())
tag = f"{os_name}_{arch}"
if tag not in SHA256:
raise SystemExit(f"newt isn't packaged here for {platform.system()} {platform.machine()}")
return tag
def binary() -> "os.PathLike":
return paths.NEWT_DIR / "newt"
def installed_version():
try:
return (paths.NEWT_DIR / "VERSION").read_text().strip()
except OSError:
return None
def ensure_binary(say=print, fetch=None) -> bool:
"""Install the pinned newt if it isn't there already. Returns True if it changed."""
if installed_version() == VERSION and binary().exists():
return False
tag = platform_tag()
url = URL.format(version=VERSION, plat=tag)
say(f"Downloading newt {VERSION} ({tag})…")
data = (fetch or _fetch)(url)
got = hashlib.sha256(data).hexdigest()
if got != SHA256[tag]:
raise SystemExit(f"newt download didn't match its checksum (got {got}); not installing it")
paths.NEWT_DIR.mkdir(parents=True, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=paths.NEWT_DIR, prefix=".newt.")
with os.fdopen(fd, "wb") as f:
f.write(data)
os.chmod(tmp, 0o755)
os.replace(tmp, binary())
(paths.NEWT_DIR / "VERSION").write_text(VERSION + "\n")
return True
def _fetch(url) -> bytes:
with urllib.request.urlopen(url, timeout=300) as r:
return r.read()
def run():
"""The service's entry point: exec newt with its credentials in the environment."""
cfg = paths.load_config()
conf = cfg.get("newt") or {}
if not conf.get("endpoint") or not conf.get("id"):
raise SystemExit("newt isn't configured; run `llmhost install-newt`")
secret = paths.read_secret(paths.NEWT_SECRET_FILE, "newt site secret", "llmhost install-newt")
env = {**os.environ, "PANGOLIN_ENDPOINT": conf["endpoint"], "NEWT_ID": conf["id"],
"NEWT_SECRET": secret, "LOG_LEVEL": conf.get("log_level", "INFO")}
exe = str(binary())
os.execve(exe, [exe], env)