@clawchatsai/connector 0.1.31 → 0.1.32
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/dist/index.js +34 -1
- package/dist/webrtc-peer.js +16 -0
- package/package.json +1 -1
- package/server/controllers/filesystem.js +57 -1
- package/server/controllers/filesystem.test.js +62 -1
- package/server/desktop-tunnel.js +154 -0
- package/server/desktop-tunnel.test.js +133 -0
- package/server/index.js +3 -1
package/dist/index.js
CHANGED
|
@@ -62,6 +62,11 @@ class BrowserClientMap extends Map {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
const connectedClients = new BrowserClientMap();
|
|
65
|
+
/** The local gateway's WebSocket URL (the app's gateway client and Desktop tunnels). */
|
|
66
|
+
const GATEWAY_WS_URL = process.env.GATEWAY_WS_URL || 'ws://localhost:18789';
|
|
67
|
+
/** Open Desktop tunnels per browser connection (server/desktop-tunnel.js); a viewer uses one or two. */
|
|
68
|
+
const desktopTunnels = new Map();
|
|
69
|
+
const MAX_TUNNELS_PER_CONNECTION = 8;
|
|
65
70
|
/** Reassembly buffers for chunked RPC requests from browser (large uploads). */
|
|
66
71
|
const rpcReqChunkBuffers = new Map();
|
|
67
72
|
/** Reassembly buffers for chunked gateway-msg from browser (large payloads like image attachments). */
|
|
@@ -253,7 +258,7 @@ async function startClawChats(ctx, api) {
|
|
|
253
258
|
app = serverModule.createApp({
|
|
254
259
|
dataDir,
|
|
255
260
|
port: parseInt(process.env.PORT || '3001', 10),
|
|
256
|
-
gatewayUrl:
|
|
261
|
+
gatewayUrl: GATEWAY_WS_URL,
|
|
257
262
|
authToken: process.env.CLAWCHATS_AUTH_TOKEN || '', // P2P: DataChannel is the auth boundary
|
|
258
263
|
gatewayToken, // For WS auth to local OpenClaw gateway
|
|
259
264
|
llm: api.runtime?.llm, // host-run completions (chat-title fallback, /api/title)
|
|
@@ -329,6 +334,31 @@ async function startClawChats(ctx, api) {
|
|
|
329
334
|
setupDataChannelHandler(dc, connectionId, ctx);
|
|
330
335
|
signaling?.reportConnectionCount(webrtcPeer?.activeCount ?? 0);
|
|
331
336
|
});
|
|
337
|
+
// A Desktop view's tunnel to the gateway's desktop stream: only on an authenticated browser
|
|
338
|
+
// connection (webrtc-peer.ts already refuses gateway-sharing peers).
|
|
339
|
+
// @ts-expect-error — server/ is plain JS with no .d.ts
|
|
340
|
+
const { openDesktopTunnel } = await import('../server/desktop-tunnel.js');
|
|
341
|
+
webrtcPeer.on('tunnel-channel', (channel, connectionId) => {
|
|
342
|
+
const open = desktopTunnels.get(connectionId) ?? new Set();
|
|
343
|
+
if (!connectedClients.has(connectionId) || open.size >= MAX_TUNNELS_PER_CONNECTION) {
|
|
344
|
+
ctx.logger.warn(`Desktop tunnel refused for ${connectionId} (${connectedClients.has(connectionId) ? 'too many tunnels' : 'not signed in'})`);
|
|
345
|
+
try {
|
|
346
|
+
channel.close();
|
|
347
|
+
}
|
|
348
|
+
catch { /* gone */ }
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const tunnel = openDesktopTunnel(channel, { gatewayUrl: GATEWAY_WS_URL, log: ctx.logger });
|
|
352
|
+
if (!tunnel)
|
|
353
|
+
return;
|
|
354
|
+
open.add(tunnel);
|
|
355
|
+
desktopTunnels.set(connectionId, open);
|
|
356
|
+
channel.addEventListener('close', () => {
|
|
357
|
+
open.delete(tunnel);
|
|
358
|
+
if (!open.size && desktopTunnels.get(connectionId) === open)
|
|
359
|
+
desktopTunnels.delete(connectionId);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
332
362
|
// Gateway sharing: a DataChannel with another gateway. Never on the browser path.
|
|
333
363
|
webrtcPeer.on('peer-datachannel', (dc, info) => {
|
|
334
364
|
if (info.role === 'owner' && info.peer) {
|
|
@@ -362,6 +392,9 @@ async function startClawChats(ctx, api) {
|
|
|
362
392
|
webrtcPeer.on('datachannel-closed', (connectionId) => {
|
|
363
393
|
ctx.logger.info(`Browser disconnected: ${connectionId}`);
|
|
364
394
|
connectedClients.delete(connectionId);
|
|
395
|
+
for (const tunnel of desktopTunnels.get(connectionId) ?? [])
|
|
396
|
+
tunnel.close();
|
|
397
|
+
desktopTunnels.delete(connectionId);
|
|
365
398
|
signaling?.reportConnectionCount(webrtcPeer?.activeCount ?? 0);
|
|
366
399
|
});
|
|
367
400
|
// Wire signaling ICE events to peer manager
|
package/dist/webrtc-peer.js
CHANGED
|
@@ -13,6 +13,8 @@ import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate, } from 'node
|
|
|
13
13
|
// Public interfaces
|
|
14
14
|
// ---------------------------------------------------------------------------
|
|
15
15
|
const MAX_PENDING_ICE = 200;
|
|
16
|
+
/** Label prefix of a Desktop tunnel channel (matches server/desktop-tunnel.js). */
|
|
17
|
+
const TUNNEL_LABEL_PREFIX = 'tunnel:';
|
|
16
18
|
const MAX_PEER_CONNECTIONS_PER_SHARE = 3;
|
|
17
19
|
const PEER_OPEN_TIMEOUT_MS = 30_000;
|
|
18
20
|
/** sha-256 DTLS fingerprint from an SDP ("" when absent). */
|
|
@@ -132,6 +134,20 @@ export class WebRTCPeerManager extends EventEmitter {
|
|
|
132
134
|
// W3C-standard ondatachannel
|
|
133
135
|
pc.ondatachannel = (event) => {
|
|
134
136
|
const channel = event.channel;
|
|
137
|
+
// A Desktop view's tunnel (server/desktop-tunnel.js): an extra channel on a browser
|
|
138
|
+
// connection. It isn't the connection's main channel, so closing it must not close the
|
|
139
|
+
// peer connection. Gateway-sharing peers never get one.
|
|
140
|
+
if (String(channel.label || '').startsWith(TUNNEL_LABEL_PREFIX)) {
|
|
141
|
+
if (this.peerInfo.has(connectionId)) {
|
|
142
|
+
try {
|
|
143
|
+
channel.close();
|
|
144
|
+
}
|
|
145
|
+
catch { /* gone */ }
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.emit('tunnel-channel', channel, connectionId);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
135
151
|
this._handleDataChannel(channel, connectionId);
|
|
136
152
|
};
|
|
137
153
|
// Forward local ICE candidates to browser via signaling
|
package/package.json
CHANGED
|
@@ -252,12 +252,16 @@ export async function handleWorkspaceUpload(req, res, query) {
|
|
|
252
252
|
send(res, 200, { ok: true, uploaded });
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
+
function isBadName(name) {
|
|
256
|
+
return name === '.' || name === '..' || /[/\\\0]/.test(name) || name.trim() !== name;
|
|
257
|
+
}
|
|
258
|
+
|
|
255
259
|
// Creates one empty file or folder named `name` inside the directory `path`.
|
|
256
260
|
export function handleWorkspaceCreate(req, res, query) {
|
|
257
261
|
const { path: parentPath, name, type } = query;
|
|
258
262
|
if (!parentPath || !name) return sendError(res, 400, 'Missing path or name parameter');
|
|
259
263
|
if (type !== 'file' && type !== 'dir') return sendError(res, 400, 'type must be file or dir');
|
|
260
|
-
if (
|
|
264
|
+
if (isBadName(name)) return sendError(res, 400, 'Invalid name');
|
|
261
265
|
const parent = resolveInHome(parentPath);
|
|
262
266
|
if (!parent) return sendError(res, 403, 'Access denied');
|
|
263
267
|
if (!fs.existsSync(parent) || !fs.statSync(parent).isDirectory()) return sendError(res, 404, 'Target directory not found');
|
|
@@ -271,3 +275,55 @@ export function handleWorkspaceCreate(req, res, query) {
|
|
|
271
275
|
}
|
|
272
276
|
send(res, 200, { ok: true, path: target + (type === 'dir' ? '/' : ''), type });
|
|
273
277
|
}
|
|
278
|
+
|
|
279
|
+
// `name` inside `dir`, or with " (2)", " (3)"... before the extension when that is taken.
|
|
280
|
+
function freeName(dir, name) {
|
|
281
|
+
if (!exists(path.join(dir, name))) return name;
|
|
282
|
+
const ext = path.extname(name);
|
|
283
|
+
const stem = path.basename(name, ext);
|
|
284
|
+
for (let n = 2; ; n++) {
|
|
285
|
+
const candidate = `${stem} (${n})${ext}`;
|
|
286
|
+
if (!exists(path.join(dir, candidate))) return candidate;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Moves (op 'move') or copies (op 'copy') `path` into directory `dir` as `name` (default: its
|
|
291
|
+
// current name). An existing target is a 409 unless `unique=1`, which picks a free "name (2)".
|
|
292
|
+
// Folders copy recursively; a folder can't go inside itself.
|
|
293
|
+
function transfer(res, query, op) {
|
|
294
|
+
const { path: srcPath, dir: dirPath, unique } = query;
|
|
295
|
+
if (!srcPath || !dirPath) return sendError(res, 400, 'Missing path or dir parameter');
|
|
296
|
+
const src = resolveInHome(srcPath);
|
|
297
|
+
const dir = resolveInHome(dirPath);
|
|
298
|
+
if (!src || !dir || src === HOME) return sendError(res, 403, 'Access denied');
|
|
299
|
+
if (!exists(src)) return sendError(res, 404, 'Path not found');
|
|
300
|
+
if (!exists(dir) || !fs.statSync(dir).isDirectory()) return sendError(res, 404, 'Target directory not found');
|
|
301
|
+
let name = query.name || path.basename(src);
|
|
302
|
+
if (isBadName(name)) return sendError(res, 400, 'Invalid name');
|
|
303
|
+
const isDir = fs.statSync(src).isDirectory();
|
|
304
|
+
if (isDir && (dir === src || dir.startsWith(src + path.sep))) return sendError(res, 400, 'Can\'t put a folder inside itself');
|
|
305
|
+
let target = path.join(dir, name);
|
|
306
|
+
if (target === src && op === 'move') return send(res, 200, { ok: true, path: target + (isDir ? '/' : '') });
|
|
307
|
+
if (exists(target)) {
|
|
308
|
+
if (unique !== '1') return sendError(res, 409, `"${name}" already exists`);
|
|
309
|
+
name = freeName(dir, name);
|
|
310
|
+
target = path.join(dir, name);
|
|
311
|
+
}
|
|
312
|
+
try {
|
|
313
|
+
if (op === 'copy') fs.cpSync(src, target, { recursive: true, errorOnExist: true, force: false });
|
|
314
|
+
else {
|
|
315
|
+
try { fs.renameSync(src, target); }
|
|
316
|
+
catch (err) {
|
|
317
|
+
if (err.code !== 'EXDEV') throw err;
|
|
318
|
+
fs.cpSync(src, target, { recursive: true, errorOnExist: true, force: false });
|
|
319
|
+
fs.rmSync(src, { recursive: true, force: true });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} catch (err) {
|
|
323
|
+
return sendError(res, 500, `${op === 'copy' ? 'Copy' : 'Move'} failed: ${err.message}`);
|
|
324
|
+
}
|
|
325
|
+
send(res, 200, { ok: true, path: target + (isDir ? '/' : ''), type: isDir ? 'dir' : 'file' });
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function handleWorkspaceMove(req, res, query) { return transfer(res, query, 'move'); }
|
|
329
|
+
export function handleWorkspaceCopy(req, res, query) { return transfer(res, query, 'copy'); }
|
|
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
|
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
-
import { handleWorkspaceFileRead, handleWorkspaceCreate, resolveReadPath } from './filesystem.js';
|
|
6
|
+
import { handleWorkspaceFileRead, handleWorkspaceCreate, handleWorkspaceMove, handleWorkspaceCopy, resolveReadPath } from './filesystem.js';
|
|
7
7
|
|
|
8
8
|
function read(filePath, workspaceDir) {
|
|
9
9
|
const out = { status: 0, body: '' };
|
|
@@ -162,3 +162,64 @@ test('create: makes an empty file or folder inside home, rejects bad names and e
|
|
|
162
162
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
163
163
|
}
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
function call(handler, query) {
|
|
167
|
+
const out = { status: 0, body: null };
|
|
168
|
+
const res = {
|
|
169
|
+
writeHead(status) { out.status = status; },
|
|
170
|
+
setHeader() {},
|
|
171
|
+
end(body) { out.body = JSON.parse(String(body ?? 'null')); },
|
|
172
|
+
};
|
|
173
|
+
handler({}, res, query);
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
test('move: renames in place, moves into another folder, refuses clashes unless unique', () => {
|
|
178
|
+
const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-move-test-'));
|
|
179
|
+
try {
|
|
180
|
+
fs.writeFileSync(path.join(dir, 'a.md'), 'A');
|
|
181
|
+
fs.writeFileSync(path.join(dir, 'b.md'), 'B');
|
|
182
|
+
fs.mkdirSync(path.join(dir, 'sub'));
|
|
183
|
+
const renamed = call(handleWorkspaceMove, { path: path.join(dir, 'a.md'), dir, name: 'c.md' });
|
|
184
|
+
assert.equal(renamed.status, 200);
|
|
185
|
+
assert.equal(renamed.body.path, path.join(dir, 'c.md'));
|
|
186
|
+
assert.equal(fs.readFileSync(path.join(dir, 'c.md'), 'utf8'), 'A');
|
|
187
|
+
assert.ok(!fs.existsSync(path.join(dir, 'a.md')));
|
|
188
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir, name: 'b.md' }).status, 409);
|
|
189
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir, name: 'x/y' }).status, 400);
|
|
190
|
+
// Moving onto itself is a no-op, not a clash.
|
|
191
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir }).status, 200);
|
|
192
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir: path.join(dir, 'sub') }).status, 200);
|
|
193
|
+
assert.equal(fs.readFileSync(path.join(dir, 'sub', 'c.md'), 'utf8'), 'A');
|
|
194
|
+
fs.writeFileSync(path.join(dir, 'c.md'), 'A2');
|
|
195
|
+
const clash = call(handleWorkspaceMove, { path: path.join(dir, 'c.md'), dir: path.join(dir, 'sub'), unique: '1' });
|
|
196
|
+
assert.equal(clash.body.path, path.join(dir, 'sub', 'c (2).md'));
|
|
197
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'sub'), dir: path.join(dir, 'sub') }).status, 400);
|
|
198
|
+
assert.equal(call(handleWorkspaceMove, { path: dir, dir: path.join(dir, 'sub') }).status, 400);
|
|
199
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'missing'), dir }).status, 404);
|
|
200
|
+
assert.equal(call(handleWorkspaceMove, { path: path.join(dir, 'b.md'), dir: '/tmp' }).status, 403);
|
|
201
|
+
assert.equal(call(handleWorkspaceMove, { path: os.homedir(), dir }).status, 403);
|
|
202
|
+
} finally {
|
|
203
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('copy: copies files and folders, numbering clashes with unique', () => {
|
|
208
|
+
const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-copy-test-'));
|
|
209
|
+
try {
|
|
210
|
+
fs.mkdirSync(path.join(dir, 'src', 'deep'), { recursive: true });
|
|
211
|
+
fs.writeFileSync(path.join(dir, 'src', 'deep', 'f.txt'), 'F');
|
|
212
|
+
fs.writeFileSync(path.join(dir, 'a.md'), 'A');
|
|
213
|
+
const dup = call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir, name: 'a copy.md', unique: '1' });
|
|
214
|
+
assert.equal(dup.body.path, path.join(dir, 'a copy.md'));
|
|
215
|
+
assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir, name: 'a copy.md', unique: '1' }).body.path, path.join(dir, 'a copy (2).md'));
|
|
216
|
+
assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'a.md'), dir }).status, 409);
|
|
217
|
+
assert.equal(fs.readFileSync(path.join(dir, 'a.md'), 'utf8'), 'A');
|
|
218
|
+
const folder = call(handleWorkspaceCopy, { path: path.join(dir, 'src'), dir, unique: '1' });
|
|
219
|
+
assert.equal(folder.body.path, path.join(dir, 'src (2)') + '/');
|
|
220
|
+
assert.equal(fs.readFileSync(path.join(dir, 'src (2)', 'deep', 'f.txt'), 'utf8'), 'F');
|
|
221
|
+
assert.equal(call(handleWorkspaceCopy, { path: path.join(dir, 'src'), dir: path.join(dir, 'src', 'deep') }).status, 400);
|
|
222
|
+
} finally {
|
|
223
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
224
|
+
}
|
|
225
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Desktop tunnel: one extra DataChannel per Desktop view, piped to the gateway's desktop WebSocket.
|
|
2
|
+
//
|
|
3
|
+
// The Control UI's Desktop panel opens a raw binary WebSocket straight to the gateway
|
|
4
|
+
// (/desktop/observe?token=…, minted by the desktop.observe RPC). A ClawChats browser can't reach
|
|
5
|
+
// the gateway directly, so it opens a DataChannel labelled "tunnel:<that path>" on its existing
|
|
6
|
+
// WebRTC connection; this module connects it to the gateway on loopback and copies bytes both ways.
|
|
7
|
+
//
|
|
8
|
+
// Rules:
|
|
9
|
+
// - Only the desktop stream paths, with only a token parameter: the tunnel can't reach any other
|
|
10
|
+
// gateway URL. The gateway checks the one-time token itself; nothing here holds credentials.
|
|
11
|
+
// - Callers only pass channels from an authenticated browser connection (never a share peer).
|
|
12
|
+
// - Gateway → browser data goes in pieces of CHUNK bytes, so chat on the main channel interleaves.
|
|
13
|
+
// - Backpressure: when the channel has more than HIGH_WATER queued, stop reading the gateway
|
|
14
|
+
// socket until it drains below LOW_WATER. Memory stays bounded on a slow or relayed link.
|
|
15
|
+
// - Text messages on the channel are control frames: the connector sends one
|
|
16
|
+
// {"type":"close","code","reason"} before closing, so the viewer can show why (WebSocket close
|
|
17
|
+
// codes don't survive a DataChannel close).
|
|
18
|
+
|
|
19
|
+
import { WebSocket as WS } from 'ws';
|
|
20
|
+
|
|
21
|
+
export const TUNNEL_LABEL_PREFIX = 'tunnel:';
|
|
22
|
+
const ALLOWED_PATHS = new Set(['/desktop/observe', '/desktop/audio']);
|
|
23
|
+
export const CHUNK = 16 * 1024;
|
|
24
|
+
export const HIGH_WATER = 1024 * 1024;
|
|
25
|
+
export const LOW_WATER = 256 * 1024;
|
|
26
|
+
// Browser → gateway bytes buffered while the gateway socket is still connecting.
|
|
27
|
+
const MAX_PENDING_BYTES = 1024 * 1024;
|
|
28
|
+
const MAX_LABEL_LENGTH = 2048;
|
|
29
|
+
|
|
30
|
+
/** "tunnel:/desktop/observe?token=…" → "/desktop/observe?token=…", or null when not allowed. */
|
|
31
|
+
export function parseTunnelLabel(label) {
|
|
32
|
+
if (typeof label !== 'string' || !label.startsWith(TUNNEL_LABEL_PREFIX) || label.length > MAX_LABEL_LENGTH) return null;
|
|
33
|
+
const raw = label.slice(TUNNEL_LABEL_PREFIX.length);
|
|
34
|
+
if (!raw.startsWith('/') || raw.startsWith('//')) return null;
|
|
35
|
+
let url;
|
|
36
|
+
try { url = new URL(raw, 'http://127.0.0.1'); } catch { return null; }
|
|
37
|
+
if (url.origin !== 'http://127.0.0.1' || !ALLOWED_PATHS.has(url.pathname) || url.hash) return null;
|
|
38
|
+
const keys = [...url.searchParams.keys()];
|
|
39
|
+
const token = url.searchParams.get('token');
|
|
40
|
+
if (keys.length !== 1 || keys[0] !== 'token' || !token) return null;
|
|
41
|
+
return `${url.pathname}?token=${encodeURIComponent(token)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** ws://host:port (the gateway client's URL) + path → the gateway desktop socket URL. */
|
|
45
|
+
export function gatewaySocketUrl(gatewayUrl, path) {
|
|
46
|
+
const base = new URL(gatewayUrl);
|
|
47
|
+
return new URL(path, `${base.protocol}//${base.host}`).toString();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const toBuffer = data => Buffer.isBuffer(data) ? data
|
|
51
|
+
: data instanceof ArrayBuffer ? Buffer.from(data)
|
|
52
|
+
: ArrayBuffer.isView(data) ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
|
|
53
|
+
: null;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Pipe a browser DataChannel (W3C RTCDataChannel API: send, close, readyState, bufferedAmount,
|
|
57
|
+
* onmessage, onclose, onbufferedamountlow) to the gateway desktop socket its label names.
|
|
58
|
+
* Returns a handle with close(), or null when the label isn't an allowed tunnel (channel closed).
|
|
59
|
+
*/
|
|
60
|
+
export function openDesktopTunnel(dc, { gatewayUrl, WebSocketImpl = WS, log = console } = {}) {
|
|
61
|
+
const path = parseTunnelLabel(dc.label);
|
|
62
|
+
if (!path) {
|
|
63
|
+
try { dc.close(); } catch { /* gone */ }
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
try { dc.binaryType = 'arraybuffer'; } catch { /* read-only in some polyfills */ }
|
|
67
|
+
dc.bufferedAmountLowThreshold = LOW_WATER;
|
|
68
|
+
// A channel can arrive before it's open; the gateway speaks first (RFB), so connect once it is.
|
|
69
|
+
if (dc.readyState === 'connecting') {
|
|
70
|
+
const handle = { closed: false, inner: null, close() { this.closed = true; this.inner?.close(); try { dc.close(); } catch { /* gone */ } } };
|
|
71
|
+
dc.onopen = () => { if (!handle.closed) handle.inner = pipe(dc, path, { gatewayUrl, WebSocketImpl, log }); };
|
|
72
|
+
dc.onclose = () => { handle.closed = true; };
|
|
73
|
+
return handle;
|
|
74
|
+
}
|
|
75
|
+
return pipe(dc, path, { gatewayUrl, WebSocketImpl, log });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function pipe(dc, path, { gatewayUrl, WebSocketImpl, log }) {
|
|
79
|
+
let closed = false;
|
|
80
|
+
const sendControl = (code, reason) => {
|
|
81
|
+
try { if (dc.readyState === 'open') dc.send(JSON.stringify({ type: 'close', code, reason: String(reason || '').slice(0, 200) })); }
|
|
82
|
+
catch { /* channel already gone */ }
|
|
83
|
+
};
|
|
84
|
+
const ws = new WebSocketImpl(gatewaySocketUrl(gatewayUrl, path), { perMessageDeflate: false });
|
|
85
|
+
let pending = [];
|
|
86
|
+
let pendingBytes = 0;
|
|
87
|
+
let paused = false;
|
|
88
|
+
|
|
89
|
+
const finish = (code, reason, { fromChannel = false } = {}) => {
|
|
90
|
+
if (closed) return;
|
|
91
|
+
closed = true;
|
|
92
|
+
pending = [];
|
|
93
|
+
if (!fromChannel) {
|
|
94
|
+
sendControl(code, reason);
|
|
95
|
+
try { dc.close(); } catch { /* gone */ }
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
if (ws.readyState === WebSocketImpl.OPEN) ws.close(1000);
|
|
99
|
+
else if (ws.readyState === WebSocketImpl.CONNECTING) ws.terminate();
|
|
100
|
+
} catch { /* gone */ }
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const resume = () => {
|
|
104
|
+
if (!paused || closed) return;
|
|
105
|
+
paused = false;
|
|
106
|
+
try { ws.resume(); } catch { /* closed */ }
|
|
107
|
+
};
|
|
108
|
+
dc.onbufferedamountlow = resume;
|
|
109
|
+
|
|
110
|
+
ws.on('open', () => {
|
|
111
|
+
for (const buf of pending) ws.send(buf);
|
|
112
|
+
pending = [];
|
|
113
|
+
pendingBytes = 0;
|
|
114
|
+
});
|
|
115
|
+
ws.on('message', (data) => {
|
|
116
|
+
if (closed || dc.readyState !== 'open') return;
|
|
117
|
+
const buf = Array.isArray(data) ? Buffer.concat(data) : toBuffer(data) ?? Buffer.from(String(data));
|
|
118
|
+
try {
|
|
119
|
+
for (let i = 0; i < buf.length; i += CHUNK) dc.send(buf.subarray(i, Math.min(i + CHUNK, buf.length)));
|
|
120
|
+
} catch (e) {
|
|
121
|
+
finish(1011, `tunnel send failed: ${e.message}`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (!paused && dc.bufferedAmount > HIGH_WATER) {
|
|
125
|
+
paused = true;
|
|
126
|
+
ws.pause();
|
|
127
|
+
// The low-water event can fire before the pause took effect; check once more.
|
|
128
|
+
if (dc.bufferedAmount <= LOW_WATER) resume();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
ws.on('unexpected-response', (_req, res) => {
|
|
132
|
+
finish(1006, `the gateway refused the desktop stream (HTTP ${res.statusCode})`);
|
|
133
|
+
res.resume?.();
|
|
134
|
+
});
|
|
135
|
+
ws.on('close', (code, reason) => finish(code || 1006, reason?.toString?.() || ''));
|
|
136
|
+
ws.on('error', (e) => {
|
|
137
|
+
log.warn?.(`[desktop-tunnel] gateway socket error: ${e.message}`);
|
|
138
|
+
finish(1006, '');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
dc.onmessage = (event) => {
|
|
142
|
+
if (closed) return;
|
|
143
|
+
if (typeof event.data === 'string') return; // the browser sends no control frames yet
|
|
144
|
+
const buf = toBuffer(event.data);
|
|
145
|
+
if (!buf) return;
|
|
146
|
+
if (ws.readyState === WebSocketImpl.OPEN) { ws.send(buf); return; }
|
|
147
|
+
pendingBytes += buf.length;
|
|
148
|
+
if (pendingBytes > MAX_PENDING_BYTES) { finish(1009, 'tunnel buffer full'); return; }
|
|
149
|
+
pending.push(Buffer.from(buf)); // copy: the channel may reuse its buffer
|
|
150
|
+
};
|
|
151
|
+
dc.onclose = () => finish(1000, '', { fromChannel: true });
|
|
152
|
+
|
|
153
|
+
return { close: () => finish(1000, 'tunnel closed'), get closed() { return closed; } };
|
|
154
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { WebSocketServer } from 'ws';
|
|
4
|
+
import { parseTunnelLabel, gatewaySocketUrl, openDesktopTunnel, CHUNK, HIGH_WATER } from './desktop-tunnel.js';
|
|
5
|
+
|
|
6
|
+
test('parseTunnelLabel allows only the desktop stream paths with a token', () => {
|
|
7
|
+
assert.equal(parseTunnelLabel('tunnel:/desktop/observe?token=abc'), '/desktop/observe?token=abc');
|
|
8
|
+
assert.equal(parseTunnelLabel('tunnel:/desktop/audio?token=a%2Fb'), '/desktop/audio?token=a%2Fb');
|
|
9
|
+
for (const bad of [
|
|
10
|
+
'clawchats', 'tunnel:', 'tunnel:/desktop/observe', 'tunnel:/desktop/observe?token=',
|
|
11
|
+
'tunnel:/desktop/observe?token=a&x=1', 'tunnel:/ws?token=a', 'tunnel://evil.com/desktop/observe?token=a',
|
|
12
|
+
'tunnel:http://127.0.0.1/desktop/observe?token=a', 'tunnel:/desktop/observe/../../x?token=a',
|
|
13
|
+
'tunnel:/desktop/observe?token=a#x', `tunnel:/desktop/observe?token=${'a'.repeat(3000)}`, null,
|
|
14
|
+
]) assert.equal(parseTunnelLabel(bad), null, String(bad).slice(0, 60));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('gatewaySocketUrl keeps the gateway host and replaces the path', () => {
|
|
18
|
+
assert.equal(gatewaySocketUrl('ws://localhost:18789', '/desktop/observe?token=t'), 'ws://localhost:18789/desktop/observe?token=t');
|
|
19
|
+
assert.equal(gatewaySocketUrl('ws://127.0.0.1:1/some/path', '/desktop/audio?token=t'), 'ws://127.0.0.1:1/desktop/audio?token=t');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/** A W3C-style DataChannel double: records sends, exposes bufferedAmount, and can be closed. */
|
|
23
|
+
function fakeChannel(label, { readyState = 'open' } = {}) {
|
|
24
|
+
const dc = {
|
|
25
|
+
label, readyState, binaryType: 'blob', bufferedAmount: 0, bufferedAmountLowThreshold: 0,
|
|
26
|
+
sent: [], closed: false, onmessage: null, onclose: null, onopen: null, onbufferedamountlow: null,
|
|
27
|
+
send(data) { if (this.readyState !== 'open') throw new Error('not open'); this.sent.push(data); },
|
|
28
|
+
close() { if (this.closed) return; this.closed = true; this.readyState = 'closed'; this.onclose?.(); },
|
|
29
|
+
fromBrowser(bytes) { this.onmessage?.({ data: new Uint8Array(bytes).buffer }); },
|
|
30
|
+
binary() { return Buffer.concat(this.sent.filter(d => typeof d !== 'string').map(d => Buffer.from(d))); },
|
|
31
|
+
control() { return this.sent.filter(d => typeof d === 'string').map(d => JSON.parse(d)); },
|
|
32
|
+
};
|
|
33
|
+
return dc;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function gateway(onConnection, { verifyClient } = {}) {
|
|
37
|
+
const wss = new WebSocketServer({ port: 0, host: '127.0.0.1', ...(verifyClient ? { verifyClient } : {}) });
|
|
38
|
+
await new Promise(r => wss.on('listening', r));
|
|
39
|
+
wss.on('connection', onConnection);
|
|
40
|
+
return { wss, url: `ws://127.0.0.1:${wss.address().port}`, close: () => new Promise(r => { for (const c of wss.clients) c.terminate(); wss.close(r); }) };
|
|
41
|
+
}
|
|
42
|
+
const until = async (fn, ms = 2000) => {
|
|
43
|
+
const end = Date.now() + ms;
|
|
44
|
+
while (!fn()) { if (Date.now() > end) throw new Error('timed out'); await new Promise(r => setTimeout(r, 10)); }
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
test('pipes bytes both ways, in chunks toward the browser', async () => {
|
|
48
|
+
let gwSocket, gwPath, received = [];
|
|
49
|
+
const gw = await gateway((ws, req) => {
|
|
50
|
+
gwSocket = ws; gwPath = req.url;
|
|
51
|
+
ws.on('message', d => received.push(Buffer.from(d)));
|
|
52
|
+
ws.send(Buffer.alloc(CHUNK * 2 + 5, 7)); // the gateway speaks first (RFB version)
|
|
53
|
+
});
|
|
54
|
+
const dc = fakeChannel('tunnel:/desktop/observe?token=tok');
|
|
55
|
+
openDesktopTunnel(dc, { gatewayUrl: gw.url, log: { warn() {} } });
|
|
56
|
+
dc.fromBrowser([1, 2, 3]); // queued until the gateway socket opens
|
|
57
|
+
await until(() => dc.binary().length === CHUNK * 2 + 5);
|
|
58
|
+
assert.equal(gwPath, '/desktop/observe?token=tok');
|
|
59
|
+
assert.deepEqual(dc.sent.map(d => d.byteLength ?? d.length), [CHUNK, CHUNK, 5]);
|
|
60
|
+
await until(() => received.length === 1);
|
|
61
|
+
assert.deepEqual([...received[0]], [1, 2, 3]);
|
|
62
|
+
dc.fromBrowser([4]);
|
|
63
|
+
await until(() => received.length === 2);
|
|
64
|
+
gwSocket.close(4000, 'control-taken:Ada');
|
|
65
|
+
await until(() => dc.closed);
|
|
66
|
+
assert.deepEqual(dc.control(), [{ type: 'close', code: 4000, reason: 'control-taken:Ada' }]);
|
|
67
|
+
await gw.close();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('closing the channel closes the gateway socket', async () => {
|
|
71
|
+
let closedCode = null;
|
|
72
|
+
const gw = await gateway(ws => ws.on('close', code => { closedCode = code; }));
|
|
73
|
+
const dc = fakeChannel('tunnel:/desktop/observe?token=t');
|
|
74
|
+
openDesktopTunnel(dc, { gatewayUrl: gw.url });
|
|
75
|
+
await until(() => gw.wss.clients.size === 1);
|
|
76
|
+
dc.close();
|
|
77
|
+
await until(() => closedCode !== null);
|
|
78
|
+
assert.equal(closedCode, 1000);
|
|
79
|
+
await gw.close();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('a refused upgrade reaches the browser as a close reason', async () => {
|
|
83
|
+
const gw = await gateway(() => {}, { verifyClient: (_info, done) => done(false, 401) });
|
|
84
|
+
const dc = fakeChannel('tunnel:/desktop/observe?token=used');
|
|
85
|
+
openDesktopTunnel(dc, { gatewayUrl: gw.url, log: { warn() {} } });
|
|
86
|
+
await until(() => dc.closed);
|
|
87
|
+
assert.deepEqual(dc.control(), [{ type: 'close', code: 1006, reason: 'the gateway refused the desktop stream (HTTP 401)' }]);
|
|
88
|
+
await gw.close();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('a disallowed label closes the channel without connecting', async () => {
|
|
92
|
+
let connections = 0;
|
|
93
|
+
const gw = await gateway(() => { connections++; });
|
|
94
|
+
const dc = fakeChannel('tunnel:/ws?token=x');
|
|
95
|
+
assert.equal(openDesktopTunnel(dc, { gatewayUrl: gw.url }), null);
|
|
96
|
+
assert.equal(dc.closed, true);
|
|
97
|
+
await new Promise(r => setTimeout(r, 100));
|
|
98
|
+
assert.equal(connections, 0);
|
|
99
|
+
await gw.close();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('stops reading the gateway while the channel is backed up, resumes when it drains', async () => {
|
|
103
|
+
let gwSocket;
|
|
104
|
+
const gw = await gateway(ws => { gwSocket = ws; });
|
|
105
|
+
const dc = fakeChannel('tunnel:/desktop/observe?token=t');
|
|
106
|
+
openDesktopTunnel(dc, { gatewayUrl: gw.url });
|
|
107
|
+
await until(() => gwSocket);
|
|
108
|
+
dc.bufferedAmount = HIGH_WATER + 1; // the link is slow: lots still queued
|
|
109
|
+
gwSocket.send(Buffer.alloc(10, 1));
|
|
110
|
+
await until(() => dc.sent.length === 1);
|
|
111
|
+
gwSocket.send(Buffer.alloc(10, 2)); // arrives at the connector but isn't read while paused
|
|
112
|
+
await new Promise(r => setTimeout(r, 150));
|
|
113
|
+
assert.equal(dc.sent.length, 1);
|
|
114
|
+
dc.bufferedAmount = 0;
|
|
115
|
+
dc.onbufferedamountlow();
|
|
116
|
+
await until(() => dc.sent.length === 2);
|
|
117
|
+
dc.close();
|
|
118
|
+
await gw.close();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('a channel that is still connecting waits for open before dialling the gateway', async () => {
|
|
122
|
+
let connections = 0;
|
|
123
|
+
const gw = await gateway(ws => { connections++; ws.send(Buffer.from([9])); });
|
|
124
|
+
const dc = fakeChannel('tunnel:/desktop/observe?token=t', { readyState: 'connecting' });
|
|
125
|
+
openDesktopTunnel(dc, { gatewayUrl: gw.url });
|
|
126
|
+
await new Promise(r => setTimeout(r, 100));
|
|
127
|
+
assert.equal(connections, 0);
|
|
128
|
+
dc.readyState = 'open';
|
|
129
|
+
dc.onopen();
|
|
130
|
+
await until(() => dc.binary().length === 1);
|
|
131
|
+
dc.close();
|
|
132
|
+
await gw.close();
|
|
133
|
+
});
|
package/server/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import { Database } from './bootstrap/native.js';
|
|
|
8
8
|
import { GATEWAY_WS_URL, AUTH_TOKEN, discoverWorkspaceDir } from './config.js';
|
|
9
9
|
import { DebugLogger } from './debug.js';
|
|
10
10
|
import { GatewayClient } from './gateway.js';
|
|
11
|
-
import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload, handleWorkspaceCreate } from './controllers/filesystem.js';
|
|
11
|
+
import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload, handleWorkspaceCreate, handleWorkspaceMove, handleWorkspaceCopy } from './controllers/filesystem.js';
|
|
12
12
|
import { handleTranscribe } from './controllers/transcribe.js';
|
|
13
13
|
import { createGatewayMediaHandler } from './controllers/gateway-media.js';
|
|
14
14
|
import { handleStatic } from './controllers/static.js';
|
|
@@ -192,6 +192,8 @@ export function createApp(config = {}) {
|
|
|
192
192
|
if (method === 'DELETE' && urlPath === '/api/workspace/file') return handleWorkspaceFileDelete(req, res, query);
|
|
193
193
|
if (method === 'POST' && urlPath === '/api/workspace/upload') return await handleWorkspaceUpload(req, res, query);
|
|
194
194
|
if (method === 'POST' && urlPath === '/api/workspace/create') return handleWorkspaceCreate(req, res, query);
|
|
195
|
+
if (method === 'POST' && urlPath === '/api/workspace/move') return handleWorkspaceMove(req, res, query);
|
|
196
|
+
if (method === 'POST' && urlPath === '/api/workspace/copy') return handleWorkspaceCopy(req, res, query);
|
|
195
197
|
|
|
196
198
|
// Settings & misc
|
|
197
199
|
if (method === 'GET' && urlPath === '/api/settings') return handleGetSettings(req, res);
|