@openchamber/web 1.14.1 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.test.js +8 -0
- package/bin/lib/DOCUMENTATION.md +3 -0
- package/bin/lib/cli-args.js +10 -0
- package/bin/lib/commands-connect-url.js +169 -22
- package/dist/assets/{DiagramView-Cb58svCq.js → DiagramView-he_RFVx5.js} +1 -1
- package/dist/assets/MarkdownRendererImpl-CdBIHSPZ.js +11 -0
- package/dist/assets/{MultiRunWindow-BPSxV_B7.js → MultiRunWindow-B_C-tvVq.js} +1 -1
- package/dist/assets/OnboardingScreen-BBnTt-9w.js +1 -0
- package/dist/assets/SessionSwitcherDropdown-CWWrCFzr.js +1 -0
- package/dist/assets/SettingsWindow-CjqLdpTE.js +1 -0
- package/dist/assets/{TerminalView-fTYjnQkh.js → TerminalView-DQcdegHp.js} +1 -1
- package/dist/assets/es-r0_jT91q.js +15 -0
- package/dist/assets/fr-Bo8rtAJ1.js +13 -0
- package/dist/assets/index-8J6waVO6.js +2 -0
- package/dist/assets/index-DEMqM4Lz.css +1 -0
- package/dist/assets/ja-BPWtkG-c.js +15 -0
- package/dist/assets/ko-Bv0k-0E3.js +15 -0
- package/dist/assets/main-B-GA5Tg5.js +237 -0
- package/dist/assets/main-Dfm-VKZ9.js +2 -0
- package/dist/assets/miniChat-CfmCFFfu.js +2 -0
- package/dist/assets/mobile-CXJqeDlm.js +2 -0
- package/dist/assets/pl-CiTP50bz.js +15 -0
- package/dist/assets/pt-BR-DadIjX5L.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-DS7Dy2ii.js +2 -0
- package/dist/assets/renderMobileApp-o7Fs9EBx.js +16 -0
- package/dist/assets/runtimeEndpointReset-MTaPPm4F.js +167 -0
- package/dist/assets/uk-2RpsWGoZ.js +15 -0
- package/dist/assets/{useAppFontEffects-BUUJYbAZ.js → useAppFontEffects-DkPuAMXD.js} +196 -133
- package/dist/assets/{vendor-.bun-C6iL44L9.js → vendor-.bun-BE757pvp.js} +1 -1
- package/dist/assets/zh-CN-BwKWaTuz.js +15 -0
- package/dist/assets/zh-TW--f_BqBpD.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +4 -4
- package/dist/mobile.html +4 -4
- package/package.json +2 -2
- package/server/index.js +236 -2
- package/server/lib/client-auth/pairing.js +308 -0
- package/server/lib/client-auth/pairing.test.js +142 -0
- package/server/lib/client-auth/remote-clients.js +65 -3
- package/server/lib/notifications/DOCUMENTATION.md +1 -1
- package/server/lib/notifications/apns-runtime.js +11 -16
- package/server/lib/notifications/runtime.js +135 -33
- package/server/lib/opencode/DOCUMENTATION.md +1 -0
- package/server/lib/opencode/agents.js +3 -0
- package/server/lib/opencode/bootstrap-runtime.js +15 -0
- package/server/lib/opencode/core-routes.js +321 -8
- package/server/lib/opencode/core-routes.test.js +329 -9
- package/server/lib/opencode/feature-routes-runtime.js +6 -0
- package/server/lib/opencode/network-runtime.js +14 -1
- package/server/lib/opencode/network-runtime.test.js +29 -9
- package/server/lib/opencode/proxy.js +47 -6
- package/server/lib/opencode/settings-helpers.js +24 -0
- package/server/lib/opencode/settings-helpers.test.js +25 -0
- package/server/lib/opencode/settings-normalization-runtime.js +21 -4
- package/server/lib/opencode/settings-normalization-runtime.test.js +21 -0
- package/server/lib/opencode/settings-runtime.js +25 -0
- package/server/lib/opencode/shutdown-runtime.js +2 -0
- package/server/lib/opencode/tunnel-auth.js +4 -9
- package/server/lib/package-manager.js +18 -5
- package/server/lib/permission-auto-accept/DOCUMENTATION.md +34 -0
- package/server/lib/permission-auto-accept/runtime.js +263 -0
- package/server/lib/permission-auto-accept/runtime.test.js +137 -0
- package/server/lib/projects/project-config.js +8 -0
- package/server/lib/quota/DOCUMENTATION.md +5 -2
- package/server/lib/quota/credentials/providers.js +43 -0
- package/server/lib/quota/credentials/store.js +48 -0
- package/server/lib/quota/credentials/store.test.js +26 -0
- package/server/lib/quota/opencode-go-credentials.js +11 -0
- package/server/lib/quota/opencode-go-credentials.test.js +32 -0
- package/server/lib/quota/providers/codex.js +7 -4
- package/server/lib/quota/providers/codex.test.js +48 -0
- package/server/lib/quota/providers/cursor.js +28 -39
- package/server/lib/quota/providers/index.js +7 -0
- package/server/lib/quota/providers/ollama-cloud.js +23 -42
- package/server/lib/quota/providers/ollama-cloud.test.js +12 -0
- package/server/lib/quota/providers/opencode-go.js +70 -0
- package/server/lib/quota/providers/opencode-go.test.js +17 -0
- package/server/lib/quota/routes.js +78 -7
- package/server/lib/quota/routes.test.js +40 -0
- package/server/lib/relay/DOCUMENTATION.md +98 -0
- package/server/lib/relay/cross-compat.test.js +135 -0
- package/server/lib/relay/e2ee.js +341 -0
- package/server/lib/relay/e2ee.test.js +124 -0
- package/server/lib/relay/host-client.js +389 -0
- package/server/lib/relay/host-client.test.js +280 -0
- package/server/lib/relay/host-lock.js +101 -0
- package/server/lib/relay/host-lock.test.js +129 -0
- package/server/lib/relay/identity.js +93 -0
- package/server/lib/relay/identity.test.js +78 -0
- package/server/lib/relay/service.js +334 -0
- package/server/lib/relay/signing-key.js +73 -0
- package/server/lib/relay/tunnel-codec.js +373 -0
- package/server/lib/relay/tunnel-codec.test.js +58 -0
- package/server/lib/relay/tunnel-host.js +462 -0
- package/server/lib/scheduled-tasks/runtime.js +104 -0
- package/server/lib/session-goal/DOCUMENTATION.md +176 -0
- package/server/lib/session-goal/objectives.js +61 -0
- package/server/lib/session-goal/routes.js +36 -0
- package/server/lib/session-goal/runtime.js +764 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +6 -0
- package/server/lib/ui-auth/ui-auth.js +10 -0
- package/server/opencode-proxy.test.js +38 -0
- package/dist/assets/MarkdownRendererImpl-D2yCDL5A.js +0 -7
- package/dist/assets/OnboardingScreen-Sl-6JgaP.js +0 -1
- package/dist/assets/SessionSwitcherDropdown-4FWg9xMC.js +0 -1
- package/dist/assets/SettingsWindow-CY8q4msv.js +0 -1
- package/dist/assets/es-bWcuDy_2.js +0 -15
- package/dist/assets/fr-DzRs3OzV.js +0 -13
- package/dist/assets/index-KnWk9fmM.css +0 -1
- package/dist/assets/index-ZJTzZ0_U.js +0 -2
- package/dist/assets/ja-BqUX_UD3.js +0 -15
- package/dist/assets/ko-BQo10SNx.js +0 -15
- package/dist/assets/main-BDoqLlQB.js +0 -236
- package/dist/assets/main-DUGyTrDt.js +0 -2
- package/dist/assets/miniChat-DNDiX8GA.js +0 -2
- package/dist/assets/mobile-DnghtOCb.js +0 -2
- package/dist/assets/pl-Bgx_ATJT.js +0 -15
- package/dist/assets/pt-BR-B1yXRvoX.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-BB4C1K7t.js +0 -2
- package/dist/assets/renderMobileApp-CZgaXKi5.js +0 -16
- package/dist/assets/runtimeEndpointReset-By39StyB.js +0 -167
- package/dist/assets/uk-C9k8SP8q.js +0 -15
- package/dist/assets/zh-CN-Bri-Vo5y.js +0 -15
- package/dist/assets/zh-TW-DAUGzmYG.js +0 -15
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Per-machine relay-host claim. Every OpenChamber instance on a machine shares
|
|
2
|
+
// the same data dir and therefore the same relay signing key / serverId, so if
|
|
3
|
+
// two processes run a relay host at once they fight over the single host slot
|
|
4
|
+
// at the relay worker (each new connection closes the previous one with
|
|
5
|
+
// "4001: Control replaced") and paired devices land on whichever instance won
|
|
6
|
+
// last — often a dev/worktree instance running different code.
|
|
7
|
+
//
|
|
8
|
+
// The claim file (`relay-host.lock` in the shared data dir) makes the contest
|
|
9
|
+
// deterministic instead of a network race:
|
|
10
|
+
// - an instance only starts its relay host when there is no LIVE claimant
|
|
11
|
+
// (a dead claimant's stale file is ignored);
|
|
12
|
+
// - explicit user intent (creating a pairing link) claims unconditionally —
|
|
13
|
+
// the instance the user is interacting with must be the one devices reach;
|
|
14
|
+
// - a running host that discovers another live process has claimed backs off
|
|
15
|
+
// instead of reconnecting, which is what ends the replace/reconnect fight.
|
|
16
|
+
//
|
|
17
|
+
// This is a cooperative claim, not an OS lock: correctness does not depend on
|
|
18
|
+
// atomicity (the relay worker still enforces a single host); the claim only
|
|
19
|
+
// decides which process KEEPS retrying and which stands down.
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{
|
|
23
|
+
* lockFilePath: string,
|
|
24
|
+
* fs?: typeof import('node:fs'),
|
|
25
|
+
* process?: NodeJS.Process,
|
|
26
|
+
* logger?: Pick<Console, 'warn'>,
|
|
27
|
+
* }} deps
|
|
28
|
+
*/
|
|
29
|
+
export const createRelayHostLock = ({ lockFilePath, fs, process: proc, logger = console }) => {
|
|
30
|
+
const fsImpl = fs;
|
|
31
|
+
const selfPid = proc.pid;
|
|
32
|
+
|
|
33
|
+
const isPidAlive = (pid) => {
|
|
34
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
35
|
+
try {
|
|
36
|
+
proc.kill(pid, 0);
|
|
37
|
+
return true;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
// EPERM means the process exists but belongs to another user — treat as
|
|
40
|
+
// alive; only ESRCH (no such process) means the claim is stale.
|
|
41
|
+
return error?.code === 'EPERM';
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const readClaim = () => {
|
|
46
|
+
try {
|
|
47
|
+
const raw = fsImpl.readFileSync(lockFilePath, 'utf8');
|
|
48
|
+
const parsed = JSON.parse(raw);
|
|
49
|
+
const pid = Number(parsed?.pid);
|
|
50
|
+
return Number.isInteger(pid) && pid > 0 ? { pid } : null;
|
|
51
|
+
} catch {
|
|
52
|
+
// Missing file or unparsable content: no valid claim.
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const writeClaim = () => {
|
|
58
|
+
try {
|
|
59
|
+
fsImpl.writeFileSync(lockFilePath, JSON.stringify({ pid: selfPid, claimedAt: new Date().toISOString() }));
|
|
60
|
+
return true;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// An unwritable data dir must not take the relay down with it — fall back
|
|
63
|
+
// to pre-lock behavior (start the host, let the relay worker arbitrate).
|
|
64
|
+
logger.warn(`[Relay] could not write host claim file: ${error?.message ?? error}`);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** The pid of the current live claimant, or null when the claim is free/stale. */
|
|
70
|
+
const liveClaimantPid = () => {
|
|
71
|
+
const claim = readClaim();
|
|
72
|
+
if (!claim) return null;
|
|
73
|
+
return isPidAlive(claim.pid) ? claim.pid : null;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Claim unless another LIVE process already holds it. Re-claiming our own is a no-op refresh. */
|
|
77
|
+
const tryClaim = () => {
|
|
78
|
+
const holder = liveClaimantPid();
|
|
79
|
+
if (holder !== null && holder !== selfPid) return false;
|
|
80
|
+
return writeClaim();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Unconditional claim — explicit user intent (pairing) overrides any holder. */
|
|
84
|
+
const forceClaim = () => writeClaim();
|
|
85
|
+
|
|
86
|
+
/** True while this process is the live claimant. */
|
|
87
|
+
const holdsClaim = () => liveClaimantPid() === selfPid;
|
|
88
|
+
|
|
89
|
+
/** Release only our own claim; never delete another process's. */
|
|
90
|
+
const release = () => {
|
|
91
|
+
const claim = readClaim();
|
|
92
|
+
if (!claim || claim.pid !== selfPid) return;
|
|
93
|
+
try {
|
|
94
|
+
fsImpl.unlinkSync(lockFilePath);
|
|
95
|
+
} catch {
|
|
96
|
+
// Already gone or unwritable — nothing to do.
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return { tryClaim, forceClaim, holdsClaim, liveClaimantPid, release };
|
|
101
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createRelayHostLock } from './host-lock.js';
|
|
4
|
+
|
|
5
|
+
// In-memory fs standing in for the shared data dir.
|
|
6
|
+
const makeFakeFs = () => {
|
|
7
|
+
const files = new Map();
|
|
8
|
+
return {
|
|
9
|
+
readFileSync: (p) => {
|
|
10
|
+
if (!files.has(p)) {
|
|
11
|
+
const error = new Error('ENOENT');
|
|
12
|
+
error.code = 'ENOENT';
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
return files.get(p);
|
|
16
|
+
},
|
|
17
|
+
writeFileSync: (p, data) => {
|
|
18
|
+
files.set(p, data);
|
|
19
|
+
},
|
|
20
|
+
unlinkSync: (p) => {
|
|
21
|
+
files.delete(p);
|
|
22
|
+
},
|
|
23
|
+
peek: (p) => files.get(p),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Fake process: `alive` is the set of pids that respond to kill(pid, 0).
|
|
28
|
+
const makeFakeProcess = (pid, alive = new Set([pid])) => ({
|
|
29
|
+
pid,
|
|
30
|
+
kill: (target) => {
|
|
31
|
+
if (!alive.has(target)) {
|
|
32
|
+
const error = new Error('ESRCH');
|
|
33
|
+
error.code = 'ESRCH';
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const LOCK = '/data/relay-host.lock';
|
|
41
|
+
const silentLogger = { warn: () => {} };
|
|
42
|
+
|
|
43
|
+
describe('relay host lock', () => {
|
|
44
|
+
it('claims a free lock and reports holding it', () => {
|
|
45
|
+
const fs = makeFakeFs();
|
|
46
|
+
const lock = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(100), logger: silentLogger });
|
|
47
|
+
|
|
48
|
+
expect(lock.tryClaim()).toBe(true);
|
|
49
|
+
expect(lock.holdsClaim()).toBe(true);
|
|
50
|
+
expect(JSON.parse(fs.peek(LOCK)).pid).toBe(100);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('refuses to claim while another LIVE process holds it', () => {
|
|
54
|
+
const fs = makeFakeFs();
|
|
55
|
+
const alive = new Set([100, 200]);
|
|
56
|
+
const first = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(100, alive), logger: silentLogger });
|
|
57
|
+
const second = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(200, alive), logger: silentLogger });
|
|
58
|
+
|
|
59
|
+
expect(first.tryClaim()).toBe(true);
|
|
60
|
+
expect(second.tryClaim()).toBe(false);
|
|
61
|
+
expect(second.holdsClaim()).toBe(false);
|
|
62
|
+
expect(second.liveClaimantPid()).toBe(100);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('treats a dead claimant as free (stale claim takeover)', () => {
|
|
66
|
+
const fs = makeFakeFs();
|
|
67
|
+
const alive = new Set([200]); // pid 100 is gone
|
|
68
|
+
fs.writeFileSync(LOCK, JSON.stringify({ pid: 100 }));
|
|
69
|
+
const lock = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(200, alive), logger: silentLogger });
|
|
70
|
+
|
|
71
|
+
expect(lock.liveClaimantPid()).toBe(null);
|
|
72
|
+
expect(lock.tryClaim()).toBe(true);
|
|
73
|
+
expect(lock.holdsClaim()).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('forceClaim overrides a live holder; the loser sees the takeover', () => {
|
|
77
|
+
const fs = makeFakeFs();
|
|
78
|
+
const alive = new Set([100, 200]);
|
|
79
|
+
const first = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(100, alive), logger: silentLogger });
|
|
80
|
+
const second = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(200, alive), logger: silentLogger });
|
|
81
|
+
|
|
82
|
+
expect(first.tryClaim()).toBe(true);
|
|
83
|
+
expect(second.forceClaim()).toBe(true);
|
|
84
|
+
expect(second.holdsClaim()).toBe(true);
|
|
85
|
+
expect(first.holdsClaim()).toBe(false);
|
|
86
|
+
expect(first.liveClaimantPid()).toBe(200);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('release removes only its own claim', () => {
|
|
90
|
+
const fs = makeFakeFs();
|
|
91
|
+
const alive = new Set([100, 200]);
|
|
92
|
+
const first = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(100, alive), logger: silentLogger });
|
|
93
|
+
const second = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(200, alive), logger: silentLogger });
|
|
94
|
+
|
|
95
|
+
expect(first.tryClaim()).toBe(true);
|
|
96
|
+
second.release(); // not the holder — must be a no-op
|
|
97
|
+
expect(first.holdsClaim()).toBe(true);
|
|
98
|
+
|
|
99
|
+
first.release();
|
|
100
|
+
expect(fs.peek(LOCK)).toBeUndefined();
|
|
101
|
+
expect(first.liveClaimantPid()).toBe(null);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('treats an unparsable claim file as free', () => {
|
|
105
|
+
const fs = makeFakeFs();
|
|
106
|
+
fs.writeFileSync(LOCK, 'not-json');
|
|
107
|
+
const lock = createRelayHostLock({ lockFilePath: LOCK, fs, process: makeFakeProcess(100), logger: silentLogger });
|
|
108
|
+
|
|
109
|
+
expect(lock.liveClaimantPid()).toBe(null);
|
|
110
|
+
expect(lock.tryClaim()).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('an EPERM kill probe still counts as a live holder', () => {
|
|
114
|
+
const fs = makeFakeFs();
|
|
115
|
+
fs.writeFileSync(LOCK, JSON.stringify({ pid: 100 }));
|
|
116
|
+
const proc = {
|
|
117
|
+
pid: 200,
|
|
118
|
+
kill: () => {
|
|
119
|
+
const error = new Error('EPERM');
|
|
120
|
+
error.code = 'EPERM';
|
|
121
|
+
throw error;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
const lock = createRelayHostLock({ lockFilePath: LOCK, fs, process: proc, logger: silentLogger });
|
|
125
|
+
|
|
126
|
+
expect(lock.liveClaimantPid()).toBe(100);
|
|
127
|
+
expect(lock.tryClaim()).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Host relay identity: the EXISTING ECDSA P-256 signing keypair (shared with
|
|
2
|
+
// the push relay via signing-key.js — same storage, same serverId) plus a NEW
|
|
3
|
+
// long-lived ECDH P-256 encryption keypair for the E2EE channel (WebCrypto
|
|
4
|
+
// keys are single-purpose, so signing and encryption keys must differ).
|
|
5
|
+
// The encryption keypair is persisted as `settings.relayEncryptionKey =
|
|
6
|
+
// { privateJwk, publicJwk }`, mirroring the relaySigningKey precedent.
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
canonicalPublicJwkString,
|
|
10
|
+
deriveServerId,
|
|
11
|
+
getOrCreateRelaySigningKeypair,
|
|
12
|
+
signRelayMessage,
|
|
13
|
+
} from './signing-key.js';
|
|
14
|
+
import { exportPublicKeyJwk, generateEcdhKeyPair, importEcdhPrivateKey } from './e2ee.js';
|
|
15
|
+
|
|
16
|
+
const isJwkPair = (value) => Boolean(value && typeof value === 'object' && value.privateJwk && value.publicJwk);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {{
|
|
20
|
+
* crypto: typeof import('node:crypto'),
|
|
21
|
+
* readSettingsFromDiskMigrated: () => Promise<object>,
|
|
22
|
+
* writeSettingsToDisk: (settings: object) => Promise<void>,
|
|
23
|
+
* readSettingsStrict?: () => Promise<object>,
|
|
24
|
+
* }} deps
|
|
25
|
+
*/
|
|
26
|
+
export const createRelayIdentityRuntime = (deps) => {
|
|
27
|
+
const { crypto, readSettingsFromDiskMigrated, writeSettingsToDisk, readSettingsStrict } = deps;
|
|
28
|
+
|
|
29
|
+
let cachedIdentity = null;
|
|
30
|
+
|
|
31
|
+
const getOrCreateEncryptionKeypair = async () => {
|
|
32
|
+
const settings = await readSettingsFromDiskMigrated();
|
|
33
|
+
const existing = settings?.relayEncryptionKey;
|
|
34
|
+
if (isJwkPair(existing)) {
|
|
35
|
+
return existing;
|
|
36
|
+
}
|
|
37
|
+
// Same regeneration gate as the signing key: never mint a replacement
|
|
38
|
+
// identity key off a swallowed read failure — a new encryption key breaks
|
|
39
|
+
// the E2EE trust anchor pinned by every paired device. Verify "missing" via
|
|
40
|
+
// the strict reader (throws on corrupt/unreadable) before generating.
|
|
41
|
+
let verifiedSettings = settings;
|
|
42
|
+
if (readSettingsStrict) {
|
|
43
|
+
verifiedSettings = await readSettingsStrict();
|
|
44
|
+
const verified = verifiedSettings?.relayEncryptionKey;
|
|
45
|
+
if (isJwkPair(verified)) {
|
|
46
|
+
return verified;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Loud on purpose: a new encryption key invalidates the E2EE trust anchor of
|
|
50
|
+
// every paired device. Expected exactly once, on first relay use.
|
|
51
|
+
console.warn('[relay-identity] Generating NEW relay encryption keypair (E2EE trust anchor changes; previously paired devices must re-pair)');
|
|
52
|
+
const keyPair = await generateEcdhKeyPair();
|
|
53
|
+
const privateJwk = await globalThis.crypto.subtle.exportKey('jwk', keyPair.privateKey);
|
|
54
|
+
const publicJwk = await exportPublicKeyJwk(keyPair.publicKey);
|
|
55
|
+
await writeSettingsToDisk({ ...settings, ...(verifiedSettings || {}), relayEncryptionKey: { privateJwk, publicJwk } });
|
|
56
|
+
return { privateJwk, publicJwk };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @returns {Promise<{
|
|
61
|
+
* serverId: string,
|
|
62
|
+
* hostEncPubJwk: JsonWebKey,
|
|
63
|
+
* hostEncPrivateKey: CryptoKey,
|
|
64
|
+
* signRelayAuth: (role: string, connectionId?: string | null) => { ts: number, sig: string, pk: string },
|
|
65
|
+
* }>}
|
|
66
|
+
*/
|
|
67
|
+
const getRelayIdentity = async () => {
|
|
68
|
+
if (cachedIdentity) return cachedIdentity;
|
|
69
|
+
const signing = await getOrCreateRelaySigningKeypair({ crypto, readSettingsFromDiskMigrated, writeSettingsToDisk, readSettingsStrict });
|
|
70
|
+
const serverId = deriveServerId({ crypto }, signing.publicJwk);
|
|
71
|
+
const encryption = await getOrCreateEncryptionKeypair();
|
|
72
|
+
const hostEncPrivateKey = await importEcdhPrivateKey(encryption.privateJwk);
|
|
73
|
+
const pk = Buffer.from(canonicalPublicJwkString(signing.publicJwk), 'utf8').toString('base64url');
|
|
74
|
+
|
|
75
|
+
// Relay-layer auth for host-control / host-data upgrades. Signature payload
|
|
76
|
+
// string is `${ts}.${serverId}.${role}.${connectionId ?? ""}` (spec Layer 1).
|
|
77
|
+
const signRelayAuth = (role, connectionId) => {
|
|
78
|
+
const ts = Date.now();
|
|
79
|
+
const sig = signRelayMessage({ crypto }, signing.privateKey, `${ts}.${serverId}.${role}.${connectionId ?? ''}`);
|
|
80
|
+
return { ts, sig, pk };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
cachedIdentity = {
|
|
84
|
+
serverId,
|
|
85
|
+
hostEncPubJwk: encryption.publicJwk,
|
|
86
|
+
hostEncPrivateKey,
|
|
87
|
+
signRelayAuth,
|
|
88
|
+
};
|
|
89
|
+
return cachedIdentity;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return { getRelayIdentity };
|
|
93
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
import { createRelayIdentityRuntime } from './identity.js';
|
|
5
|
+
import { canonicalPublicJwkString } from './signing-key.js';
|
|
6
|
+
|
|
7
|
+
// In-memory settings store standing in for the on-disk settings file.
|
|
8
|
+
const makeSettingsStore = (initial = {}) => {
|
|
9
|
+
let settings = { ...initial };
|
|
10
|
+
return {
|
|
11
|
+
readSettingsFromDiskMigrated: async () => ({ ...settings }),
|
|
12
|
+
writeSettingsToDisk: async (next) => {
|
|
13
|
+
settings = { ...next };
|
|
14
|
+
},
|
|
15
|
+
peek: () => settings,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe('relay identity', () => {
|
|
20
|
+
it('derives a stable serverId from the signing key and persists both keypairs', async () => {
|
|
21
|
+
const store = makeSettingsStore();
|
|
22
|
+
const runtime = createRelayIdentityRuntime({ crypto, ...store });
|
|
23
|
+
const identity = await runtime.getRelayIdentity();
|
|
24
|
+
|
|
25
|
+
const stored = store.peek();
|
|
26
|
+
expect(stored.relaySigningKey).toBeDefined();
|
|
27
|
+
expect(stored.relayEncryptionKey).toBeDefined();
|
|
28
|
+
|
|
29
|
+
const expectedServerId = crypto
|
|
30
|
+
.createHash('sha256')
|
|
31
|
+
.update(canonicalPublicJwkString(stored.relaySigningKey.publicJwk))
|
|
32
|
+
.digest('base64url');
|
|
33
|
+
expect(identity.serverId).toBe(expectedServerId);
|
|
34
|
+
expect(identity.hostEncPubJwk.crv).toBe('P-256');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('reuses an existing signing key (serverId stays stable across installs)', async () => {
|
|
38
|
+
const { privateKey, publicKey } = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' });
|
|
39
|
+
void privateKey;
|
|
40
|
+
const publicJwk = publicKey.export({ format: 'jwk' });
|
|
41
|
+
const store = makeSettingsStore({
|
|
42
|
+
relaySigningKey: {
|
|
43
|
+
privateJwk: crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' }).privateKey.export({ format: 'jwk' }),
|
|
44
|
+
publicJwk,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
// Match private to public so importing works.
|
|
48
|
+
const pair = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' });
|
|
49
|
+
store.peek().relaySigningKey.privateJwk = pair.privateKey.export({ format: 'jwk' });
|
|
50
|
+
store.peek().relaySigningKey.publicJwk = pair.publicKey.export({ format: 'jwk' });
|
|
51
|
+
|
|
52
|
+
const runtime = createRelayIdentityRuntime({ crypto, ...store });
|
|
53
|
+
const identity = await runtime.getRelayIdentity();
|
|
54
|
+
const expected = crypto
|
|
55
|
+
.createHash('sha256')
|
|
56
|
+
.update(canonicalPublicJwkString(pair.publicKey.export({ format: 'jwk' })))
|
|
57
|
+
.digest('base64url');
|
|
58
|
+
expect(identity.serverId).toBe(expected);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('produces a verifiable relay auth signature', async () => {
|
|
62
|
+
const store = makeSettingsStore();
|
|
63
|
+
const runtime = createRelayIdentityRuntime({ crypto, ...store });
|
|
64
|
+
const identity = await runtime.getRelayIdentity();
|
|
65
|
+
const { ts, sig, pk } = identity.signRelayAuth('host-control', null);
|
|
66
|
+
|
|
67
|
+
const canonical = Buffer.from(pk, 'base64url').toString('utf8');
|
|
68
|
+
const publicJwk = JSON.parse(canonical);
|
|
69
|
+
const key = crypto.createPublicKey({ key: publicJwk, format: 'jwk' });
|
|
70
|
+
const ok = crypto.verify(
|
|
71
|
+
'SHA256',
|
|
72
|
+
Buffer.from(`${ts}.${identity.serverId}.host-control.`),
|
|
73
|
+
{ key, dsaEncoding: 'ieee-p1363' },
|
|
74
|
+
Buffer.from(sig, 'base64url'),
|
|
75
|
+
);
|
|
76
|
+
expect(ok).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
});
|