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

+4 -1
View File
@@ -44,7 +44,8 @@ Options:
Environment:
PI2_PORT, PI2_HOST, PI2_STATIC_DIR, PI2_ORIGINS, PI2_RUNTIME_DIR,
PI2_DATA_DIR, PI2_STATE_DIR, PI2_LOG_LEVEL, PI2_WORKSPACE_ROOTS
PI2_DATA_DIR, PI2_STATE_DIR, PI2_LOG_LEVEL, PI2_WORKSPACE_ROOTS,
PI2_ACCESS_MODE (local|lan|tailscale), PI2_PUBLIC_URL
Runtime files ($PI2_RUNTIME_DIR):
d.sock plugin unix socket (nvim → daemon)
@@ -180,6 +181,8 @@ export async function main(argv: readonly string[], ioOptions: RunDaemonOptions
daemonId,
startedAt: Date.now(),
protocolVersion: PROTOCOL_VERSION,
access: config.access,
publicUrl: config.publicUrl,
},
});
// Rotate the local CLI token only when this process owns the lock:
+17 -2
View File
@@ -5,9 +5,13 @@
* Both files carry the same payload:
*
* ```jsonc
* {"pid":1234,"bootId":"…","port":7676,"daemonId":"d-…","startedAt":1733,"protocolVersion":1}
* {"pid":1234,"bootId":"…","port":7676,"daemonId":"d-…","startedAt":1733,"protocolVersion":1,
* "access":"local","publicUrl":null}
* ```
*
* `access`/`publicUrl` are optional so readers built before the access modes
* keep working.
*
* The lock is created with `wx` (atomic: only one daemon wins) and is
* considered **stale** when the pid is gone or the recorded boot id differs
* from the current one (pid reuse after a reboot). `--force` ignores a live
@@ -19,6 +23,7 @@ import { existsSync, readFileSync, renameSync, unlinkSync, writeFileSync } from
import { randomBytes } from "node:crypto";
import { dirname, join } from "node:path";
import { daemonLockPath, ensureDir, runtimeDir } from "../core/paths.js";
import { ACCESS_MODES, type AccessMode } from "../config.js";
export const LOCK_FILE = "daemon.lock";
export const DAEMON_JSON_FILE = "daemon.json";
@@ -30,6 +35,10 @@ export interface DaemonLock {
daemonId: string;
startedAt: number;
protocolVersion: number;
/** Optional: written by daemons that know about access modes. */
access?: AccessMode;
/** Optional: external base URL (or explicit `null` when unset). */
publicUrl?: string | null;
}
export type LockErrorCode = "E_LOCKED";
@@ -79,6 +88,10 @@ export function isPidAlive(pid: number): boolean {
}
}
function isAccessMode(value: unknown): value is AccessMode {
return typeof value === "string" && (ACCESS_MODES as readonly string[]).includes(value);
}
function isLock(value: unknown): value is DaemonLock {
if (typeof value !== "object" || value === null) return false;
const candidate = value as Partial<DaemonLock>;
@@ -91,7 +104,9 @@ function isLock(value: unknown): value is DaemonLock {
typeof candidate.daemonId === "string" &&
candidate.daemonId !== "" &&
typeof candidate.startedAt === "number" &&
typeof candidate.protocolVersion === "number"
typeof candidate.protocolVersion === "number" &&
(candidate.access === undefined || isAccessMode(candidate.access)) &&
(candidate.publicUrl === undefined || candidate.publicUrl === null || typeof candidate.publicUrl === "string")
);
}