clideck-remote 0.1.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/LICENSE-REMOTE +99 -0
- package/README.md +1 -0
- package/bin/clideck-remote.js +52 -0
- package/package.json +30 -0
- package/remote.js +302 -0
package/LICENSE-REMOTE
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
Parameters
|
|
4
|
+
|
|
5
|
+
Licensor: Or Kuntzman
|
|
6
|
+
|
|
7
|
+
Licensed Work: The CliDeck Remote components in this repository, currently:
|
|
8
|
+
- remote.js
|
|
9
|
+
- DESIGN-remote.md
|
|
10
|
+
- relay/
|
|
11
|
+
|
|
12
|
+
The Licensed Work is Copyright (c) 2026 Or Kuntzman and contributors.
|
|
13
|
+
|
|
14
|
+
Additional Use Grant: You may make Production Use of the Licensed Work only as
|
|
15
|
+
part of the official CliDeck desktop application, and only to access or use the
|
|
16
|
+
remote or mobile service operated by or for the Licensor, for your own personal
|
|
17
|
+
use or internal business use. You may not make Production Use of the Licensed
|
|
18
|
+
Work to offer, operate, host, white-label, resell, or enable a hosted or
|
|
19
|
+
managed service, relay service, mobile control service, or similar product that
|
|
20
|
+
competes with CliDeck Remote or materially substitutes for it.
|
|
21
|
+
|
|
22
|
+
Change Date: 2030-03-12
|
|
23
|
+
|
|
24
|
+
Change License: GPL version 3.0 or later
|
|
25
|
+
|
|
26
|
+
For information about alternative licensing arrangements, contact:
|
|
27
|
+
licensing@clideck.dev
|
|
28
|
+
|
|
29
|
+
Notice
|
|
30
|
+
|
|
31
|
+
The Business Source License (this document, or the "License") is not an Open
|
|
32
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
33
|
+
under an Open Source License, as stated in this License.
|
|
34
|
+
|
|
35
|
+
License text copyright (c) 2024 MariaDB plc, All Rights Reserved. "Business
|
|
36
|
+
Source License" is a trademark of MariaDB plc.
|
|
37
|
+
|
|
38
|
+
Terms
|
|
39
|
+
|
|
40
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
41
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
42
|
+
Licensor may make an Additional Use Grant, above, permitting limited production
|
|
43
|
+
use.
|
|
44
|
+
|
|
45
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
46
|
+
available distribution of a specific version of the Licensed Work under this
|
|
47
|
+
License, whichever comes first, the Licensor hereby grants you rights under the
|
|
48
|
+
terms of the Change License, and the rights granted in the paragraph above
|
|
49
|
+
terminate.
|
|
50
|
+
|
|
51
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
52
|
+
currently in effect as described in this License, you must purchase a
|
|
53
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
54
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
55
|
+
|
|
56
|
+
All copies of the original and modified Licensed Work, and derivative works of
|
|
57
|
+
the Licensed Work, are subject to this License. This License applies separately
|
|
58
|
+
for each version of the Licensed Work and the Change Date may vary for each
|
|
59
|
+
version of the Licensed Work released by Licensor.
|
|
60
|
+
|
|
61
|
+
You must conspicuously display this License on each original or modified copy
|
|
62
|
+
of the Licensed Work. If you receive the Licensed Work in original or modified
|
|
63
|
+
form from a third party, the terms and conditions set forth in this License
|
|
64
|
+
apply to your use of that work.
|
|
65
|
+
|
|
66
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
67
|
+
terminate your rights under this License for the current and all other versions
|
|
68
|
+
of the Licensed Work.
|
|
69
|
+
|
|
70
|
+
This License does not grant you any right in any trademark or logo of Licensor
|
|
71
|
+
or its affiliates (provided that you may use a trademark or logo of Licensor as
|
|
72
|
+
expressly required by this License).
|
|
73
|
+
|
|
74
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN
|
|
75
|
+
"AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS
|
|
76
|
+
OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY,
|
|
77
|
+
FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
|
|
78
|
+
|
|
79
|
+
MariaDB hereby grants you permission to use this License's text to license your
|
|
80
|
+
works, and to refer to it using the trademark "Business Source License", as
|
|
81
|
+
long as you comply with the Covenants of Licensor below.
|
|
82
|
+
|
|
83
|
+
Covenants of Licensor
|
|
84
|
+
|
|
85
|
+
In consideration of the right to use this License's text and the "Business
|
|
86
|
+
Source License" name and trademark, Licensor covenants to MariaDB, and to all
|
|
87
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
88
|
+
|
|
89
|
+
To specify as the Change License the GPL Version 2.0 or any later version, or
|
|
90
|
+
a license that is compatible with GPL Version 2.0 or a later version, where
|
|
91
|
+
"compatible" means that software provided under the Change License can be
|
|
92
|
+
included in a program with software provided under GPL Version 2.0 or a later
|
|
93
|
+
version. Licensor may specify additional Change Licenses without limitation.
|
|
94
|
+
|
|
95
|
+
To either: (a) specify an additional grant of rights to use that does not
|
|
96
|
+
impose any additional restriction on the right granted in this License, as the
|
|
97
|
+
Additional Use Grant; or (b) insert the text "None" to specify a Change Date.
|
|
98
|
+
|
|
99
|
+
Not to modify this License in any other way.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# clideck-remote
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// clideck-remote CLI — interface contract for CliDeck OSS app
|
|
4
|
+
// Commands: status, pair, unpair
|
|
5
|
+
|
|
6
|
+
const command = process.argv[2];
|
|
7
|
+
const json = process.argv.includes('--json');
|
|
8
|
+
|
|
9
|
+
function out(obj) {
|
|
10
|
+
if (json) process.stdout.write(JSON.stringify(obj) + '\n');
|
|
11
|
+
else {
|
|
12
|
+
for (const [k, v] of Object.entries(obj)) console.log(`${k}: ${v}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
switch (command) {
|
|
17
|
+
case 'status':
|
|
18
|
+
// TODO: check daemon state, relay connection, plan entitlement
|
|
19
|
+
out({ paired: false, connected: false, url: null, qr: null, plan: 'free' });
|
|
20
|
+
break;
|
|
21
|
+
|
|
22
|
+
case 'pair':
|
|
23
|
+
// TODO: start relay connection, generate keypair, return QR
|
|
24
|
+
out({ url: null, qr: null, roomId: null, plan: 'free' });
|
|
25
|
+
break;
|
|
26
|
+
|
|
27
|
+
case 'unpair':
|
|
28
|
+
// TODO: tear down relay connection
|
|
29
|
+
out({ ok: true });
|
|
30
|
+
break;
|
|
31
|
+
|
|
32
|
+
case '--version':
|
|
33
|
+
case '-v':
|
|
34
|
+
console.log(require('../package.json').version);
|
|
35
|
+
break;
|
|
36
|
+
|
|
37
|
+
case '--help':
|
|
38
|
+
case '-h':
|
|
39
|
+
case undefined:
|
|
40
|
+
console.log(`clideck-remote v${require('../package.json').version}
|
|
41
|
+
|
|
42
|
+
Usage:
|
|
43
|
+
clideck-remote status [--json] Check connection and plan state
|
|
44
|
+
clideck-remote pair [--json] Start pairing (relay + QR)
|
|
45
|
+
clideck-remote unpair [--json] Disconnect from relay
|
|
46
|
+
clideck-remote --version Show version`);
|
|
47
|
+
break;
|
|
48
|
+
|
|
49
|
+
default:
|
|
50
|
+
console.error(`Unknown command: ${command}`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clideck-remote",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Mobile remote access for CliDeck — control your AI agents from your phone",
|
|
5
|
+
"main": "remote.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"clideck-remote": "bin/clideck-remote.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"clideck",
|
|
11
|
+
"mobile",
|
|
12
|
+
"remote",
|
|
13
|
+
"cli",
|
|
14
|
+
"ai",
|
|
15
|
+
"agent"
|
|
16
|
+
],
|
|
17
|
+
"author": "Or Kuntzman",
|
|
18
|
+
"license": "SEE LICENSE IN LICENSE-REMOTE",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/rustykuntz/clideck-remote.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://clideck.dev/",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"ws": "^8.19.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/remote.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// Licensed under the Business Source License 1.1.
|
|
2
|
+
// See LICENSE-REMOTE at the repository root.
|
|
3
|
+
//
|
|
4
|
+
// CliDeck Remote — desktop-side relay connector with E2E encryption.
|
|
5
|
+
// Connects outbound to the Cloudflare relay, encrypts all session content.
|
|
6
|
+
|
|
7
|
+
const { webcrypto } = require('crypto');
|
|
8
|
+
const subtle = webcrypto.subtle;
|
|
9
|
+
const WebSocket = require('ws');
|
|
10
|
+
const transcript = require('./transcript');
|
|
11
|
+
const activity = require('./activity');
|
|
12
|
+
|
|
13
|
+
const RELAY_URL = process.env.CLIDECK_RELAY_URL || 'wss://remote.clideck.dev';
|
|
14
|
+
const ENC = new TextEncoder();
|
|
15
|
+
const DEC = new TextDecoder();
|
|
16
|
+
|
|
17
|
+
let ctx = null; // { getSessions, getResumable, getConfig, input, resume, close, broadcast }
|
|
18
|
+
|
|
19
|
+
// Pairing state
|
|
20
|
+
let ws = null;
|
|
21
|
+
let keyPair = null;
|
|
22
|
+
let aesKey = null;
|
|
23
|
+
let roomId = null;
|
|
24
|
+
let subscribedSession = null;
|
|
25
|
+
let paired = false;
|
|
26
|
+
let pairingUrl = null;
|
|
27
|
+
let lastMobileInput = null;
|
|
28
|
+
|
|
29
|
+
// --- Base64url ---
|
|
30
|
+
|
|
31
|
+
function toB64(buf) {
|
|
32
|
+
return Buffer.from(buf).toString('base64url');
|
|
33
|
+
}
|
|
34
|
+
function fromB64(s) {
|
|
35
|
+
return Buffer.from(s, 'base64url');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// --- Crypto (ECDH P-256 + HKDF + AES-256-GCM) ---
|
|
39
|
+
|
|
40
|
+
async function generateKeyPair() {
|
|
41
|
+
return subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveBits']);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function exportPub(kp) {
|
|
45
|
+
return toB64(await subtle.exportKey('raw', kp.publicKey));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function deriveAesKey(privateKey, peerPubB64, room) {
|
|
49
|
+
const peerPub = await subtle.importKey(
|
|
50
|
+
'raw', fromB64(peerPubB64), { name: 'ECDH', namedCurve: 'P-256' }, false, []
|
|
51
|
+
);
|
|
52
|
+
const bits = await subtle.deriveBits({ name: 'ECDH', public: peerPub }, privateKey, 256);
|
|
53
|
+
const hkdf = await subtle.importKey('raw', bits, 'HKDF', false, ['deriveKey']);
|
|
54
|
+
return subtle.deriveKey(
|
|
55
|
+
{ name: 'HKDF', hash: 'SHA-256', salt: ENC.encode(room), info: ENC.encode('clideck-e2ee-v1') },
|
|
56
|
+
hkdf, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function encrypt(key, obj) {
|
|
61
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(12));
|
|
62
|
+
const ct = await subtle.encrypt({ name: 'AES-GCM', iv }, key, ENC.encode(JSON.stringify(obj)));
|
|
63
|
+
return JSON.stringify({ type: 'e', iv: toB64(iv), ct: toB64(ct) });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function decrypt(key, msg) {
|
|
67
|
+
const plain = await subtle.decrypt({ name: 'AES-GCM', iv: fromB64(msg.iv) }, key, fromB64(msg.ct));
|
|
68
|
+
return JSON.parse(DEC.decode(plain));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// --- Send encrypted message to mobile ---
|
|
72
|
+
|
|
73
|
+
async function sendToMobile(obj) {
|
|
74
|
+
if (!ws || ws.readyState !== 1 || !aesKey) return;
|
|
75
|
+
try { ws.send(await encrypt(aesKey, obj)); }
|
|
76
|
+
catch (e) { console.error('[remote] encrypt/send failed:', e.message); }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function agentType(id) {
|
|
80
|
+
return ctx.getSessions()?.get(id)?.presetId || null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// --- Build full state snapshot for mobile ---
|
|
84
|
+
|
|
85
|
+
function buildState() {
|
|
86
|
+
const sessions = ctx.getSessions();
|
|
87
|
+
const cfg = ctx.getConfig();
|
|
88
|
+
const active = [...sessions].map(([id, s]) => ({
|
|
89
|
+
id, name: s.name, commandId: s.commandId, projectId: s.projectId || null,
|
|
90
|
+
working: s._remoteWorking != null ? !!s._remoteWorking : activity.isActive(id),
|
|
91
|
+
preview: (s.lastPreview || '').slice(0, 120),
|
|
92
|
+
lastActivityAt: s.lastActivityAt || null,
|
|
93
|
+
}));
|
|
94
|
+
const resumable = (ctx.getResumable() || []).map(s => ({
|
|
95
|
+
id: s.id, name: s.name, commandId: s.commandId, projectId: s.projectId || null,
|
|
96
|
+
preview: (s.lastPreview || '').slice(0, 120),
|
|
97
|
+
lastActivityAt: s.lastActivityAt || null,
|
|
98
|
+
}));
|
|
99
|
+
const projects = (cfg?.projects || []).map(p => ({
|
|
100
|
+
id: p.id, name: p.name, color: p.color,
|
|
101
|
+
}));
|
|
102
|
+
return { active, resumable, projects };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function sendState() {
|
|
106
|
+
const s = buildState();
|
|
107
|
+
sendToMobile({ type: 'state', active: s.active, resumable: s.resumable, projects: s.projects });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// --- Handle messages from mobile ---
|
|
111
|
+
|
|
112
|
+
async function handleMobileMessage(raw) {
|
|
113
|
+
let msg;
|
|
114
|
+
try { msg = JSON.parse(raw); } catch { return; }
|
|
115
|
+
|
|
116
|
+
// Key exchange — mobile sends its public key
|
|
117
|
+
if (msg.type === 'pub' && msg.key && keyPair) {
|
|
118
|
+
try {
|
|
119
|
+
aesKey = await deriveAesKey(keyPair.privateKey, msg.key, roomId);
|
|
120
|
+
paired = true;
|
|
121
|
+
console.log('[remote] paired — sending state');
|
|
122
|
+
sendState();
|
|
123
|
+
ctx.broadcast({ type: 'remote.status', paired: true, connected: true, url: pairingUrl });
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.error('[remote] key derivation failed:', e.message);
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Relay tells us the mobile peer disconnected
|
|
131
|
+
if (msg.type === 'peer-disconnected' && msg.role === 'mobile') {
|
|
132
|
+
console.log('[remote] mobile disconnected');
|
|
133
|
+
paired = false;
|
|
134
|
+
aesKey = null;
|
|
135
|
+
subscribedSession = null;
|
|
136
|
+
ctx.broadcast({ type: 'remote.status', paired: false, connected: true, url: pairingUrl });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Relay tells us mobile reconnected — they'll re-send pub to re-derive keys
|
|
141
|
+
if (msg.type === 'peer-connected' && msg.role === 'mobile') {
|
|
142
|
+
console.log('[remote] mobile reconnected — awaiting key exchange');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Encrypted envelope
|
|
147
|
+
if (msg.type === 'e' && aesKey) {
|
|
148
|
+
let plain;
|
|
149
|
+
try { plain = await decrypt(aesKey, msg); } catch { return; }
|
|
150
|
+
|
|
151
|
+
switch (plain.type) {
|
|
152
|
+
case 'input':
|
|
153
|
+
if (plain.id && plain.text) {
|
|
154
|
+
if (plain.text !== '\r') lastMobileInput = plain.text.trim();
|
|
155
|
+
ctx.input({ id: plain.id, data: plain.text });
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
|
|
159
|
+
case 'subscribe':
|
|
160
|
+
if (plain.id) {
|
|
161
|
+
subscribedSession = plain.id;
|
|
162
|
+
const turns = transcript.getScreenTurns(plain.id, agentType(plain.id)) || transcript.getLastTurns(plain.id, 4);
|
|
163
|
+
if (turns.length) sendToMobile({ type: 'history', id: plain.id, turns });
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
|
|
167
|
+
case 'resume':
|
|
168
|
+
if (plain.id) {
|
|
169
|
+
const remoteWs = { send(data) {
|
|
170
|
+
try { const parsed = JSON.parse(data); if (parsed.type === 'error') sendToMobile({ type: 'error', text: parsed.message }); }
|
|
171
|
+
catch {}
|
|
172
|
+
}};
|
|
173
|
+
ctx.resume({ id: plain.id }, remoteWs, ctx.getConfig());
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
|
|
177
|
+
case 'close':
|
|
178
|
+
if (plain.id) ctx.close({ id: plain.id });
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// --- Broadcast listener — forward events to mobile ---
|
|
186
|
+
|
|
187
|
+
function onBroadcast(msg) {
|
|
188
|
+
if (!paired) return;
|
|
189
|
+
|
|
190
|
+
switch (msg.type) {
|
|
191
|
+
case 'created':
|
|
192
|
+
case 'closed':
|
|
193
|
+
case 'sessions.resumable':
|
|
194
|
+
sendState();
|
|
195
|
+
if (msg.type === 'closed' && subscribedSession === msg.id) subscribedSession = null;
|
|
196
|
+
break;
|
|
197
|
+
|
|
198
|
+
case 'renamed':
|
|
199
|
+
case 'session.setProject':
|
|
200
|
+
case 'session.preview':
|
|
201
|
+
case 'config':
|
|
202
|
+
sendState();
|
|
203
|
+
break;
|
|
204
|
+
|
|
205
|
+
case 'session.status': {
|
|
206
|
+
const s = ctx.getSessions()?.get(msg.id);
|
|
207
|
+
if (s) s._remoteWorking = msg.working;
|
|
208
|
+
sendToMobile({ type: 'status', id: msg.id, working: msg.working });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
case 'screen.updated':
|
|
213
|
+
if (msg.id === subscribedSession) {
|
|
214
|
+
const turns = transcript.getScreenTurns(msg.id, agentType(msg.id)) || transcript.getLastTurns(msg.id, 2);
|
|
215
|
+
if (turns?.length) {
|
|
216
|
+
const last = turns[turns.length - 1];
|
|
217
|
+
if (last.role === 'agent') sendToMobile({ type: 'turn', id: msg.id, role: 'agent', text: last.text });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
break;
|
|
221
|
+
|
|
222
|
+
case 'transcript.append':
|
|
223
|
+
// Forward desktop-originated user turns to mobile
|
|
224
|
+
if (msg.role === 'user' && msg.id === subscribedSession) {
|
|
225
|
+
if (lastMobileInput && msg.text === lastMobileInput) {
|
|
226
|
+
lastMobileInput = null; // dedupe — mobile already showed it
|
|
227
|
+
} else {
|
|
228
|
+
sendToMobile({ type: 'turn', id: msg.id, role: 'user', text: msg.text });
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// --- WebSocket connection to relay ---
|
|
236
|
+
|
|
237
|
+
function connectRelay() {
|
|
238
|
+
if (!roomId) return;
|
|
239
|
+
const url = `${RELAY_URL}/ws?room=${roomId}&role=desktop`;
|
|
240
|
+
ws = new WebSocket(url);
|
|
241
|
+
|
|
242
|
+
ws.on('open', () => {
|
|
243
|
+
console.log('[remote] connected to relay');
|
|
244
|
+
ctx.broadcast({ type: 'remote.status', paired: false, connected: true, url: pairingUrl });
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
ws.on('message', (data) => handleMobileMessage(data.toString()));
|
|
248
|
+
|
|
249
|
+
ws.on('close', () => {
|
|
250
|
+
console.log('[remote] relay disconnected');
|
|
251
|
+
paired = false;
|
|
252
|
+
aesKey = null;
|
|
253
|
+
subscribedSession = null;
|
|
254
|
+
ctx.broadcast({ type: 'remote.status', paired: false, connected: false, url: pairingUrl });
|
|
255
|
+
if (roomId) setTimeout(connectRelay, 3000);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
ws.on('error', (e) => {
|
|
259
|
+
console.error('[remote] relay error:', e.message);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// --- Public API ---
|
|
264
|
+
|
|
265
|
+
async function startPairing() {
|
|
266
|
+
if (ws) stopPairing();
|
|
267
|
+
|
|
268
|
+
roomId = webcrypto.randomUUID();
|
|
269
|
+
keyPair = await generateKeyPair();
|
|
270
|
+
const pubB64 = await exportPub(keyPair);
|
|
271
|
+
paired = false;
|
|
272
|
+
aesKey = null;
|
|
273
|
+
subscribedSession = null;
|
|
274
|
+
|
|
275
|
+
connectRelay();
|
|
276
|
+
|
|
277
|
+
const mobileUrl = RELAY_URL.replace('wss://', 'https://').replace('ws://', 'http://');
|
|
278
|
+
pairingUrl = `${mobileUrl}/#r=${roomId}&k=${pubB64}`;
|
|
279
|
+
return { roomId, url: pairingUrl };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function stopPairing() {
|
|
283
|
+
roomId = null;
|
|
284
|
+
keyPair = null;
|
|
285
|
+
aesKey = null;
|
|
286
|
+
paired = false;
|
|
287
|
+
pairingUrl = null;
|
|
288
|
+
subscribedSession = null;
|
|
289
|
+
if (ws) { try { ws.close(); } catch {} ws = null; }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function isPaired() { return paired; }
|
|
293
|
+
function isConnected() { return ws?.readyState === 1; }
|
|
294
|
+
function getPairingUrl() { return pairingUrl; }
|
|
295
|
+
|
|
296
|
+
function init(options) {
|
|
297
|
+
ctx = options;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function shutdown() { stopPairing(); }
|
|
301
|
+
|
|
302
|
+
module.exports = { init, startPairing, stopPairing, isPaired, isConnected, getPairingUrl, onBroadcast, shutdown };
|