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

+2
View File
@@ -28,6 +28,8 @@ export function testConfig(overrides: Partial<Config> = {}): Config {
limits: TEST_LIMITS,
tokenPath: "/tmp/pi2-daemon-cli-test.sock.token",
deviceStorePath: "/tmp/pi2-daemon-cli-test-devices.json",
access: "local",
publicUrl: null,
};
return { ...base, ...overrides, limits: { ...base.limits, ...(overrides.limits ?? {}) } };
}
+30
View File
@@ -183,6 +183,36 @@ describe("daemon.json", () => {
handle.release();
});
it("carries the optional access/publicUrl fields and stays readable without them", () => {
const handle = acquireLock({
lockPath,
payload: {
port: 7676,
daemonId: "d-access",
startedAt: 5,
protocolVersion: PROTOCOL_VERSION,
access: "tailscale",
publicUrl: "https://host.ts.net",
},
bootId: () => BOOT,
});
expect(readLockFile(lockPath)?.access).toBe("tailscale");
expect(readLockFile(lockPath)?.publicUrl).toBe("https://host.ts.net");
handle.release();
// A legacy payload (no new fields) still parses.
const legacy = { ...payload(), pid: process.pid, bootId: BOOT };
writeFileSync(lockPath, `${JSON.stringify(legacy)}\n`);
expect(readLockFile(lockPath)?.access).toBeUndefined();
expect(readLockFile(lockPath)?.publicUrl).toBeUndefined();
});
it("refuses a malformed access field", () => {
const legacy = { ...payload(), pid: process.pid, bootId: BOOT, access: "public" };
writeFileSync(lockPath, `${JSON.stringify(legacy)}\n`);
expect(readLockFile(lockPath)).toBeUndefined();
});
it("is removed by release() and missing files read as undefined", () => {
const handle = acquireLock({
lockPath,