feat(daemon)!: phone-first binding and a two-step pairing flow
Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.
Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
kind classification, ordering (public → lan → other → tailscale → loopback,
physical NICs first, link-local skipped), bind-aware `networkBases()`,
`primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
`access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
public URL is configured).
Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
chosen URL, the expiry, the nonce, the QR matrix and every usable base.
`base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
(5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
`--base`; `--json` and `--public-url` are gone (the Neovim client calls the
HTTP mint directly).
Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
field in device/hello payloads are removed: nothing enforced them, and a
paired device now holds the same rights as the local browser. A permission
model, if wanted, gets its own ADR.
Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.
Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
This commit is contained in:
1 parent
3789394c85
commit
a57c874095
34 files changed
+2157
-488
No files matched your search
+28
-80
@@ -4,13 +4,18 @@
|
||||
* Defaults follow `docs/protocol.md`; deployment-specific values come from the
|
||||
* environment so `pi2 daemon` can be started without a config file.
|
||||
*
|
||||
* `PI2_ACCESS_MODE` picks the bind address and the derived CORS origins:
|
||||
* `local` is loopback-only, `lan` adds the detected LAN address and
|
||||
* `tailscale` adds the origin of `PI2_PUBLIC_URL`. `PI2_ORIGINS` is *additive*
|
||||
* to the derived list; it never replaces it.
|
||||
* The daemon binds `0.0.0.0` by default: the console is meant to be opened from
|
||||
* a phone as well as from the desktop, and access is gated by the pairing code
|
||||
* and the device token — never by the bind address. `PI2_HOST` narrows the bind
|
||||
* (e.g. `127.0.0.1` for a loopback-only daemon), `PI2_PUBLIC_URL` adds the
|
||||
* external base used behind `tailscale serve`, and `PI2_ORIGINS` is *additive*.
|
||||
*
|
||||
* The CORS allowlist is derived by `core/network` from the addresses the daemon
|
||||
* is actually reachable on, so it always matches the pairing link.
|
||||
*/
|
||||
import { networkInterfaces } from "node:os";
|
||||
import type { NetworkInterfaceInfo } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isLoopbackHost, isWildcardHost, localOrigins } from "./core/network.js";
|
||||
import { daemonTokenPath, dataDir } from "./core/paths.js";
|
||||
|
||||
export const DEFAULT_PORT = 7676;
|
||||
@@ -18,8 +23,8 @@ export const DEFAULT_MAX_BODY_BYTES = 8 * 1024 * 1024;
|
||||
export const DEFAULT_MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
||||
export const DEFAULT_MAX_CONNECTIONS = 64;
|
||||
|
||||
export type AccessMode = "local" | "lan" | "tailscale";
|
||||
export const ACCESS_MODES: readonly AccessMode[] = ["local", "lan", "tailscale"];
|
||||
/** Wildcard bind: reachable from the desktop, the LAN and a tailnet. */
|
||||
export const DEFAULT_HOST = "0.0.0.0";
|
||||
|
||||
export interface Limits {
|
||||
maxBodyBytes: number;
|
||||
@@ -37,7 +42,6 @@ export interface Config {
|
||||
limits: Limits;
|
||||
tokenPath: string;
|
||||
deviceStorePath: string;
|
||||
access: AccessMode;
|
||||
/** External base URL (no trailing slash), e.g. `https://host.tail1234.ts.net`. */
|
||||
publicUrl: string | null;
|
||||
}
|
||||
@@ -45,8 +49,8 @@ export interface Config {
|
||||
export type ConfigEnv = Record<string, string | undefined>;
|
||||
|
||||
export interface ConfigOptions {
|
||||
/** LAN address probe for `PI2_ACCESS_MODE=lan`; overridable in tests. */
|
||||
detectLanIp?: () => string | null;
|
||||
/** Network interfaces used to derive the CORS origins; overridable in tests. */
|
||||
interfaces?: NodeJS.Dict<NetworkInterfaceInfo[]>;
|
||||
}
|
||||
|
||||
function defaultStaticDir(): string {
|
||||
@@ -63,14 +67,6 @@ function parsePort(raw: string | undefined): number {
|
||||
return port;
|
||||
}
|
||||
|
||||
/** Unknown modes fail fast; the daemon prints the message as a one-line `pi2: ` error. */
|
||||
export function parseAccessMode(raw: string | undefined): AccessMode {
|
||||
const value = raw?.trim() ?? "";
|
||||
if (value === "") return "local";
|
||||
if ((ACCESS_MODES as readonly string[]).includes(value)) return value as AccessMode;
|
||||
throw new Error("PI2_ACCESS_MODE must be local, lan or tailscale");
|
||||
}
|
||||
|
||||
/** Trailing slashes and empty strings are not meaningful origins. */
|
||||
function normalizeOrigin(raw: string): string | null {
|
||||
const value = raw.trim().replace(/\/+$/, "");
|
||||
@@ -82,15 +78,6 @@ function normalizePublicUrl(raw: string | undefined): string | null {
|
||||
return normalizeOrigin(raw);
|
||||
}
|
||||
|
||||
/** `https://host/path?q=1` → `https://host`; unparsable values fall back verbatim. */
|
||||
function originOf(url: string): string | null {
|
||||
try {
|
||||
return new URL(url).origin;
|
||||
} catch {
|
||||
return normalizeOrigin(url);
|
||||
}
|
||||
}
|
||||
|
||||
function parseOrigins(raw: string | undefined): string[] {
|
||||
if (raw === undefined) return [];
|
||||
return raw
|
||||
@@ -103,61 +90,16 @@ function dedupe(origins: readonly string[]): string[] {
|
||||
return [...new Set(origins)];
|
||||
}
|
||||
|
||||
function defaultHostFor(access: AccessMode): string {
|
||||
return access === "lan" ? "0.0.0.0" : "127.0.0.1";
|
||||
}
|
||||
|
||||
function loopbackOrigins(port: number): string[] {
|
||||
return [`http://127.0.0.1:${port}`, `http://localhost:${port}`, `http://[::1]:${port}`];
|
||||
}
|
||||
|
||||
/**
|
||||
* First non-internal IPv4 address, best effort: `lan` mode only *adds* an
|
||||
* origin, and an unknown address just leaves the loopback list in place.
|
||||
*/
|
||||
export function detectLanIp(): string | null {
|
||||
try {
|
||||
for (const addresses of Object.values(networkInterfaces())) {
|
||||
for (const address of addresses ?? []) {
|
||||
if (address.family !== "IPv4" || address.internal) continue;
|
||||
if (address.address.startsWith("169.254.")) continue;
|
||||
return address.address;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// best effort only
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function derivedOrigins(
|
||||
access: AccessMode,
|
||||
port: number,
|
||||
publicUrl: string | null,
|
||||
detect: () => string | null,
|
||||
): string[] {
|
||||
const origins = loopbackOrigins(port);
|
||||
if (access === "lan") {
|
||||
let lanIp: string | null = null;
|
||||
try {
|
||||
lanIp = detect();
|
||||
} catch {
|
||||
lanIp = null;
|
||||
}
|
||||
if (lanIp !== null && lanIp.trim() !== "") origins.push(`http://${lanIp.trim()}:${port}`);
|
||||
} else if (access === "tailscale" && publicUrl !== null) {
|
||||
const origin = originOf(publicUrl);
|
||||
if (origin !== null) origins.push(origin);
|
||||
}
|
||||
return origins;
|
||||
}
|
||||
|
||||
export function loadConfig(env: ConfigEnv = process.env, options: ConfigOptions = {}): Config {
|
||||
const port = parsePort(env.PI2_PORT);
|
||||
const access = parseAccessMode(env.PI2_ACCESS_MODE);
|
||||
const publicUrl = normalizePublicUrl(env.PI2_PUBLIC_URL);
|
||||
const host = env.PI2_HOST?.trim() || defaultHostFor(access);
|
||||
const derived = derivedOrigins(access, port, publicUrl, options.detectLanIp ?? detectLanIp);
|
||||
const host = env.PI2_HOST?.trim() || DEFAULT_HOST;
|
||||
const derived = localOrigins({
|
||||
port,
|
||||
bindHost: host,
|
||||
publicUrl,
|
||||
...(options.interfaces === undefined ? {} : { interfaces: options.interfaces }),
|
||||
});
|
||||
|
||||
return {
|
||||
host,
|
||||
@@ -171,7 +113,13 @@ export function loadConfig(env: ConfigEnv = process.env, options: ConfigOptions
|
||||
},
|
||||
tokenPath: daemonTokenPath(),
|
||||
deviceStorePath: `${dataDir()}/devices.json`,
|
||||
access,
|
||||
publicUrl,
|
||||
};
|
||||
}
|
||||
|
||||
/** Human-readable bind summary, shared by `pi2 status` and `pi2 doctor`. */
|
||||
export function describeHost(host: string): string {
|
||||
if (isWildcardHost(host)) return "all interfaces";
|
||||
if (isLoopbackHost(host)) return "loopback only";
|
||||
return host;
|
||||
}
|
||||
Reference in new issue
Block a user