feat(daemon): add access modes and the local-browser login handshake

`PI2_ACCESS_MODE` (local|lan|tailscale) picks the bind address and the derived
CORS origins, `PI2_PUBLIC_URL` adds the external origin, and `PI2_ORIGINS` is now
additive instead of replacing the derived list. `daemon.json` and
`/api/v1/info` carry the mode and the public URL.

`POST /api/v1/console/session` is the `:PiWeb` handshake: loopback + the local
CLI token only, it mints or reuses the single "local browser" device and answers
`http://127.0.0.1:<port>/#t=<token>`. The token is stored plaintext next to
`devices.json` (0600) so repeated logins return the same credentials instead of
logging the already-open tab out; revoking the device drops it.
This commit is contained in:
yuez committed 2026-10-23 08:34:39 +08:00
1 parent 9eb857465c
commit 01db49fb38
11 files changed
+690 -26

No files matched your search

+113 -11
View File
@@ -3,7 +3,13 @@
*
* 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.
*/
import { networkInterfaces } from "node:os";
import { fileURLToPath } from "node:url";
import { daemonTokenPath, dataDir } from "./core/paths.js";
@@ -12,6 +18,9 @@ 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"];
export interface Limits {
maxBodyBytes: number;
maxFrameBytes: number;
@@ -28,10 +37,18 @@ 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;
}
export type ConfigEnv = Record<string, string | undefined>;
export interface ConfigOptions {
/** LAN address probe for `PI2_ACCESS_MODE=lan`; overridable in tests. */
detectLanIp?: () => string | null;
}
function defaultStaticDir(): string {
return fileURLToPath(new URL("../web/dist", import.meta.url));
}
@@ -46,24 +63,107 @@ function parsePort(raw: string | undefined): number {
return port;
}
function parseOrigins(raw: string | undefined, port: number): string[] {
if (raw !== undefined) {
return raw
.split(",")
.map((origin) => origin.trim())
.filter((origin) => origin.length > 0);
}
return [`http://127.0.0.1:${port}`, `http://localhost:${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");
}
export function loadConfig(env: ConfigEnv = process.env): Config {
/** Trailing slashes and empty strings are not meaningful origins. */
function normalizeOrigin(raw: string): string | null {
const value = raw.trim().replace(/\/+$/, "");
return value === "" ? null : value;
}
function normalizePublicUrl(raw: string | undefined): string | null {
if (raw === undefined) return 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
.split(",")
.map((origin) => normalizeOrigin(origin))
.filter((origin): origin is string => origin !== null);
}
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 host = env.PI2_HOST?.trim() || "127.0.0.1";
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);
return {
host,
port,
staticDir: env.PI2_STATIC_DIR?.trim() || defaultStaticDir(),
corsOrigins: parseOrigins(env.PI2_ORIGINS, port),
corsOrigins: dedupe([...derived, ...parseOrigins(env.PI2_ORIGINS)]),
limits: {
maxBodyBytes: DEFAULT_MAX_BODY_BYTES,
maxFrameBytes: DEFAULT_MAX_FRAME_BYTES,
@@ -71,5 +171,7 @@ export function loadConfig(env: ConfigEnv = process.env): Config {
},
tokenPath: daemonTokenPath(),
deviceStorePath: `${dataDir()}/devices.json`,
access,
publicUrl,
};
}