@artenx/dshb 0.1.40 → 0.1.42
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/lib/auth/reset-cli.js +0 -0
- package/lib/{catalog-B3pDDoR4.js → catalog-BPkswHoz.js} +4 -2
- package/lib/catalog-BPkswHoz.js.map +1 -0
- package/lib/client.js +120 -536
- package/lib/client.js.map +1 -1
- package/lib/{connection-sIX25pkS.d.ts → connection-CDJM9aUw.d.ts} +10 -3
- package/lib/core/index.js +349 -10
- package/lib/core/index.js.map +1 -1
- package/lib/core/routes.d.ts +1 -1
- package/lib/core/sync/coordinator.d.ts +5 -5
- package/lib/core/sync/coordinator.js +3 -11
- package/lib/core/sync/coordinator.js.map +1 -1
- package/lib/core/sync/host-key.d.ts +1 -1
- package/lib/core/sync/internal-routes.d.ts +3 -3
- package/lib/core/sync/internal-routes.js +2 -2
- package/lib/core/sync/model.d.ts +1 -1
- package/lib/core/sync/model.js.map +1 -1
- package/lib/core/sync/node-sync.d.ts +1 -1
- package/lib/core/sync/offline-archive.d.ts +4 -4
- package/lib/core/sync/offline-archive.js +2 -2
- package/lib/core/sync/offline-routes.js +1 -1
- package/lib/core/sync/peer-manager.d.ts +2 -2
- package/lib/core/sync/routes.d.ts +2 -3
- package/lib/core/sync/routes.js +1 -17
- package/lib/core/sync/routes.js.map +1 -1
- package/lib/core/sync/session-journal.d.ts +1 -1
- package/lib/core/sync/session-journal.js +10 -0
- package/lib/core/sync/session-journal.js.map +1 -1
- package/lib/core/sync/session-routes.d.ts +1 -1
- package/lib/core/sync/session-routes.js +1 -25
- package/lib/core/sync/session-routes.js.map +1 -1
- package/lib/core/sync/session-sync.d.ts +1 -1
- package/lib/core/sync/session-sync.js +1 -1
- package/lib/core/sync/ssh-transport.d.ts +1 -1
- package/lib/core/sync/ssh-transport.js +1 -1
- package/lib/core/sync/stores.d.ts +1 -1
- package/lib/core/sync/stores.js +3 -19
- package/lib/core/sync/stores.js.map +1 -1
- package/lib/core/workspaces.d.ts +1 -1
- package/lib/exec-ssh/connection-pool.d.ts +1 -1
- package/lib/exec-ssh/connection.d.ts +2 -2
- package/lib/exec-ssh/connection.js +29 -12
- package/lib/exec-ssh/connection.js.map +1 -1
- package/lib/exec-ssh/executor.d.ts +10 -1
- package/lib/exec-ssh/executor.js +91 -42
- package/lib/exec-ssh/executor.js.map +1 -1
- package/lib/exec-ssh/index.d.ts +1 -1
- package/lib/{host-key-Dh2YTRX7.d.ts → host-key-CjO13Ds4.d.ts} +2 -2
- package/lib/{model-D1T078kt.d.ts → model-CX2TlH2E.d.ts} +2 -6
- package/lib/{node-sync-WdEv-ek7.d.ts → node-sync-qnJ7u0TU.d.ts} +3 -3
- package/lib/{session-journal-CTblFLJg.d.ts → session-journal-ftjZuIxo.d.ts} +4 -2
- package/lib/{session-sync-flNnd2JQ.d.ts → session-sync-AlV-M72t.d.ts} +7 -10
- package/lib/{session-sync-BZSp_o4n.js → session-sync-BV1CSYic.js} +14 -11
- package/lib/{session-sync-BZSp_o4n.js.map → session-sync-BV1CSYic.js.map} +1 -1
- package/lib/{ssh-transport-DQPMNwh1.d.ts → ssh-transport-a3qXoscG.d.ts} +5 -5
- package/lib/{stores-CUY_YFD-.d.ts → stores-BwQLabuJ.d.ts} +2 -2
- package/package.json +6 -6
- package/LICENSE +0 -21
- package/lib/catalog-B3pDDoR4.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Client } from "ssh2";
|
|
1
|
+
import { Client, ConnectConfig } from "ssh2";
|
|
2
2
|
//#region src/exec-ssh/connection.d.ts
|
|
3
3
|
interface SshAuthConfig {
|
|
4
4
|
kind: 'password' | 'key' | 'agent';
|
|
@@ -29,6 +29,7 @@ interface SshConnectionError extends Error {
|
|
|
29
29
|
}
|
|
30
30
|
declare function classifySshError(err: unknown): SshErrorCategory;
|
|
31
31
|
declare function toConnectionError(err: unknown): SshConnectionError;
|
|
32
|
+
declare function buildConnectConfig(target: SshTarget, secrets: SshSecrets): ConnectConfig;
|
|
32
33
|
declare class SshConnection {
|
|
33
34
|
private readonly target;
|
|
34
35
|
private readonly secrets;
|
|
@@ -37,10 +38,16 @@ declare class SshConnection {
|
|
|
37
38
|
private closed;
|
|
38
39
|
constructor(target: SshTarget, secrets: SshSecrets);
|
|
39
40
|
getClient(): Promise<Client>;
|
|
41
|
+
/**
|
|
42
|
+
* Discard the current client after an operation-level fault (for example a
|
|
43
|
+
* timed-out channel or SFTP request on a frozen connection) so the next
|
|
44
|
+
* getClient() performs a fresh handshake instead of reusing a dead socket.
|
|
45
|
+
*/
|
|
46
|
+
invalidate(): void;
|
|
40
47
|
private connectWithRetry;
|
|
41
48
|
private connectOnce;
|
|
42
49
|
close(): void;
|
|
43
50
|
}
|
|
44
51
|
//#endregion
|
|
45
|
-
export { SshJumpHop as a,
|
|
46
|
-
//# sourceMappingURL=connection-
|
|
52
|
+
export { SshJumpHop as a, buildConnectConfig as c, SshErrorCategory as i, classifySshError as l, SshConnection as n, SshSecrets as o, SshConnectionError as r, SshTarget as s, SshAuthConfig as t, toConnectionError as u };
|
|
53
|
+
//# sourceMappingURL=connection-CDJM9aUw.d.ts.map
|
package/lib/core/index.js
CHANGED
|
@@ -12,7 +12,8 @@ import { registerWorkspaceInstructionRoutes } from "./workspace-instruction-rout
|
|
|
12
12
|
import { WorkspaceBindingsStore } from "./workspace-bindings.js";
|
|
13
13
|
import { registerWorkspaceInstructionInjector } from "./workspace-instruction-context.js";
|
|
14
14
|
import { SyncPeerManager, SyncPeerNotFoundError } from "./sync/peer-manager.js";
|
|
15
|
-
import { i as SyncSessionService } from "../session-sync-
|
|
15
|
+
import { i as SyncSessionService } from "../session-sync-BV1CSYic.js";
|
|
16
|
+
import { SyncSessionJournalStore } from "./sync/session-journal.js";
|
|
16
17
|
import { registerSyncInternalRoutes } from "./sync/internal-routes.js";
|
|
17
18
|
import { SyncSshProbeError, SyncSshTransport } from "./sync/ssh-transport.js";
|
|
18
19
|
import { SyncCoordinator } from "./sync/coordinator.js";
|
|
@@ -225,6 +226,37 @@ const DEFAULT_REMOTE_WEB_PORT = 3080;
|
|
|
225
226
|
const MAX_RELAY_RESPONSE_BYTES = 67108864;
|
|
226
227
|
const TICKET_REFRESH_MARGIN_MS = 6e4;
|
|
227
228
|
const ENDPOINT_PATTERN = /^[a-z][A-Za-z0-9$]*\/[A-Za-z0-9$.]+$/;
|
|
229
|
+
const HOP_BY_HOP = /* @__PURE__ */ new Set([
|
|
230
|
+
"host",
|
|
231
|
+
"cookie",
|
|
232
|
+
"origin",
|
|
233
|
+
"connection",
|
|
234
|
+
"keep-alive",
|
|
235
|
+
"proxy-connection",
|
|
236
|
+
"transfer-encoding",
|
|
237
|
+
"te",
|
|
238
|
+
"trailer",
|
|
239
|
+
"upgrade"
|
|
240
|
+
]);
|
|
241
|
+
const PROXY_RESPONSE_DROP_HEADERS = /* @__PURE__ */ new Set([
|
|
242
|
+
...HOP_BY_HOP,
|
|
243
|
+
"set-cookie",
|
|
244
|
+
"content-security-policy",
|
|
245
|
+
"x-frame-options",
|
|
246
|
+
"cross-origin-opener-policy",
|
|
247
|
+
"cross-origin-embedder-policy"
|
|
248
|
+
]);
|
|
249
|
+
const PROXY_REQUEST_DROP_HEADERS = /* @__PURE__ */ new Set([
|
|
250
|
+
...HOP_BY_HOP,
|
|
251
|
+
"sec-fetch-site",
|
|
252
|
+
"sec-fetch-mode",
|
|
253
|
+
"sec-fetch-dest",
|
|
254
|
+
"sec-fetch-user",
|
|
255
|
+
"referer",
|
|
256
|
+
"if-none-match",
|
|
257
|
+
"if-modified-since"
|
|
258
|
+
]);
|
|
259
|
+
const UPGRADE_PATH_SUFFIX = "/app/api/remote.mux";
|
|
228
260
|
var RemoteRelayError = class extends Error {
|
|
229
261
|
code;
|
|
230
262
|
constructor(code, message) {
|
|
@@ -309,6 +341,211 @@ var RemoteRelayService = class {
|
|
|
309
341
|
cookie
|
|
310
342
|
});
|
|
311
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* 把一条浏览器请求流式转发到远端回环端口(远端会话页反向代理)。
|
|
346
|
+
* 请求与响应都不落盘:静态资源、API 调用与流式端点逐字节透传,
|
|
347
|
+
* 请求注入中继 cookie、Host 改写为远端回环权威名,浏览器元数据头剥离。
|
|
348
|
+
*/
|
|
349
|
+
async relayProxy(peerId, req, res, remotePath) {
|
|
350
|
+
const authority = await this.remoteAuthority(peerId);
|
|
351
|
+
const cookie = await this.relayTicket(peerId);
|
|
352
|
+
const channel = await this.openChannel(peerId);
|
|
353
|
+
const headers = {};
|
|
354
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
355
|
+
if (PROXY_REQUEST_DROP_HEADERS.has(name.toLowerCase())) continue;
|
|
356
|
+
headers[name] = value;
|
|
357
|
+
}
|
|
358
|
+
headers.host = authority;
|
|
359
|
+
headers.cookie = relayCookieHeader(cookie);
|
|
360
|
+
channel.socket.on?.("error", () => {});
|
|
361
|
+
const upstream = request({
|
|
362
|
+
host: "127.0.0.1",
|
|
363
|
+
port: channel.peerRemotePort,
|
|
364
|
+
method: req.method ?? "GET",
|
|
365
|
+
path: remotePath,
|
|
366
|
+
headers,
|
|
367
|
+
createConnection: () => channel.socket
|
|
368
|
+
});
|
|
369
|
+
let responded = false;
|
|
370
|
+
return await new Promise((resolve, reject) => {
|
|
371
|
+
const settle = (error) => {
|
|
372
|
+
channel.close();
|
|
373
|
+
if (error) reject(error);
|
|
374
|
+
else resolve();
|
|
375
|
+
};
|
|
376
|
+
upstream.on("error", (error) => {
|
|
377
|
+
if (!responded) {
|
|
378
|
+
responded = true;
|
|
379
|
+
if (!res.headersSent) {
|
|
380
|
+
res.writeHead(502, {
|
|
381
|
+
"content-type": "application/json",
|
|
382
|
+
"cache-control": "no-store"
|
|
383
|
+
});
|
|
384
|
+
res.end(JSON.stringify({
|
|
385
|
+
ok: false,
|
|
386
|
+
code: "relay-unreachable",
|
|
387
|
+
error: error.message
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
settle();
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
res.destroy();
|
|
394
|
+
settle();
|
|
395
|
+
});
|
|
396
|
+
upstream.on("close", () => {
|
|
397
|
+
if (!responded) {
|
|
398
|
+
responded = true;
|
|
399
|
+
if (!res.headersSent) {
|
|
400
|
+
res.writeHead(502, {
|
|
401
|
+
"content-type": "application/json",
|
|
402
|
+
"cache-control": "no-store"
|
|
403
|
+
});
|
|
404
|
+
res.end(JSON.stringify({
|
|
405
|
+
ok: false,
|
|
406
|
+
code: "relay-unreachable",
|
|
407
|
+
error: "remote closed before responding"
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
settle();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
req.on("error", () => {
|
|
414
|
+
try {
|
|
415
|
+
upstream.destroy();
|
|
416
|
+
} catch {}
|
|
417
|
+
settle();
|
|
418
|
+
});
|
|
419
|
+
upstream.on("response", (up) => {
|
|
420
|
+
responded = true;
|
|
421
|
+
const outHeaders = {};
|
|
422
|
+
for (const [name, value] of Object.entries(up.headers)) {
|
|
423
|
+
if (value === void 0 || PROXY_RESPONSE_DROP_HEADERS.has(name.toLowerCase())) continue;
|
|
424
|
+
outHeaders[name] = value;
|
|
425
|
+
}
|
|
426
|
+
outHeaders["x-dshb-relay"] = peerId;
|
|
427
|
+
res.writeHead(up.statusCode ?? 502, outHeaders);
|
|
428
|
+
up.on("error", () => {
|
|
429
|
+
res.destroy();
|
|
430
|
+
settle();
|
|
431
|
+
});
|
|
432
|
+
up.pipe(res);
|
|
433
|
+
res.on("close", () => settle());
|
|
434
|
+
});
|
|
435
|
+
if (req.readableEnded) upstream.end();
|
|
436
|
+
else req.pipe(upstream);
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* 把一条浏览器 WebSocket 升级透传到远端 `/api/remote.mux`:
|
|
441
|
+
* 构造原始升级请求(注入中继 cookie、Host 改写)写入 SSH 通道,
|
|
442
|
+
* 等远端 101 响应头原样回写后进入双向原始字节管道。
|
|
443
|
+
*/
|
|
444
|
+
async relayUpgrade(peerId, req, socket, head, remotePath) {
|
|
445
|
+
const rejectUpgrade = (code, message) => {
|
|
446
|
+
try {
|
|
447
|
+
socket.end(`HTTP/1.1 ${code} ${message}\r\nconnection: close\r\n\r\n`);
|
|
448
|
+
} catch {}
|
|
449
|
+
};
|
|
450
|
+
let authority;
|
|
451
|
+
let cookie;
|
|
452
|
+
try {
|
|
453
|
+
authority = await this.remoteAuthority(peerId);
|
|
454
|
+
cookie = await this.relayTicket(peerId);
|
|
455
|
+
} catch (error) {
|
|
456
|
+
rejectUpgrade(502, error instanceof Error ? error.message : "relay ticket unavailable");
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
let channel;
|
|
460
|
+
try {
|
|
461
|
+
channel = await this.openChannel(peerId);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
rejectUpgrade(502, error instanceof Error ? error.message : "relay channel unavailable");
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const key = req.headers["sec-websocket-key"];
|
|
467
|
+
const version = req.headers["sec-websocket-version"];
|
|
468
|
+
if (typeof key !== "string" || typeof version !== "string") {
|
|
469
|
+
rejectUpgrade(400, "Bad Request");
|
|
470
|
+
channel.close();
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const requestLines = [
|
|
474
|
+
`GET ${remotePath} HTTP/1.1`,
|
|
475
|
+
`Host: ${authority}`,
|
|
476
|
+
"Upgrade: websocket",
|
|
477
|
+
"Connection: Upgrade",
|
|
478
|
+
`Sec-WebSocket-Key: ${key}`,
|
|
479
|
+
`Sec-WebSocket-Version: ${version}`
|
|
480
|
+
];
|
|
481
|
+
const extensions = req.headers["sec-websocket-extensions"];
|
|
482
|
+
const protocol = req.headers["sec-websocket-protocol"];
|
|
483
|
+
if (typeof extensions === "string") requestLines.push(`Sec-WebSocket-Extensions: ${extensions}`);
|
|
484
|
+
if (typeof protocol === "string") requestLines.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
|
485
|
+
requestLines.push(`Cookie: ${relayCookieHeader(cookie)}`, "", "");
|
|
486
|
+
const upstream = channel.socket;
|
|
487
|
+
upstream.write(Buffer.from(requestLines.join("\r\n"), "utf8"));
|
|
488
|
+
if (head.length > 0) upstream.write(head);
|
|
489
|
+
let buffered = Buffer.alloc(0);
|
|
490
|
+
let piping = false;
|
|
491
|
+
const startPiping = () => {
|
|
492
|
+
if (piping) return;
|
|
493
|
+
piping = true;
|
|
494
|
+
upstream.on("data", (chunk) => {
|
|
495
|
+
if (!socket.writable || !socket.write(chunk)) upstream.pause();
|
|
496
|
+
});
|
|
497
|
+
socket.on("data", (chunk) => upstream.write(chunk));
|
|
498
|
+
const teardown = () => {
|
|
499
|
+
socket.destroy();
|
|
500
|
+
channel.close();
|
|
501
|
+
};
|
|
502
|
+
upstream.on("error", teardown);
|
|
503
|
+
socket.on("error", teardown);
|
|
504
|
+
upstream.on("close", teardown);
|
|
505
|
+
socket.on("close", teardown);
|
|
506
|
+
socket.on("drain", () => upstream.resume());
|
|
507
|
+
upstream.resume();
|
|
508
|
+
};
|
|
509
|
+
const onUpstreamData = (chunk) => {
|
|
510
|
+
if (piping) return;
|
|
511
|
+
buffered = Buffer.concat([buffered, chunk]);
|
|
512
|
+
const boundary = buffered.indexOf(`\r\n\r\n`);
|
|
513
|
+
if (boundary < 0) {
|
|
514
|
+
if (buffered.length > 65536) {
|
|
515
|
+
upstream.off("data", onUpstreamData);
|
|
516
|
+
rejectUpgrade(502, "relay upgrade response too large");
|
|
517
|
+
channel.close();
|
|
518
|
+
}
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
upstream.off("data", onUpstreamData);
|
|
522
|
+
const headerText = buffered.subarray(0, boundary).toString("latin1");
|
|
523
|
+
const status = Number(/^HTTP\/1\.[01] (\d{3})/.exec(headerText)?.[1] ?? 0);
|
|
524
|
+
const headRest = buffered.subarray(boundary + 4);
|
|
525
|
+
if (status !== 101) {
|
|
526
|
+
try {
|
|
527
|
+
socket.end(buffered.subarray(0, boundary + 4));
|
|
528
|
+
} catch {}
|
|
529
|
+
channel.close();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
try {
|
|
533
|
+
socket.write(buffered.subarray(0, boundary + 4));
|
|
534
|
+
if (headRest.length > 0) socket.write(headRest);
|
|
535
|
+
} catch {
|
|
536
|
+
channel.close();
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
startPiping();
|
|
540
|
+
};
|
|
541
|
+
upstream.on("data", onUpstreamData);
|
|
542
|
+
upstream.on("error", () => {
|
|
543
|
+
upstream.off("data", onUpstreamData);
|
|
544
|
+
rejectUpgrade(502, "relay upgrade failed");
|
|
545
|
+
});
|
|
546
|
+
socket.on("error", () => channel.close());
|
|
547
|
+
socket.on("close", () => channel.close());
|
|
548
|
+
}
|
|
312
549
|
async relayRequest(peerId, request$1) {
|
|
313
550
|
const channel = await this.openChannel(peerId);
|
|
314
551
|
try {
|
|
@@ -366,16 +603,40 @@ var RemoteRelayService = class {
|
|
|
366
603
|
this.tickets.delete(peerId);
|
|
367
604
|
}
|
|
368
605
|
};
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
606
|
+
function registerRemoteRelayRoutes(ctx, relay, deps = {}) {
|
|
607
|
+
const upgradeDisposers = /* @__PURE__ */ new Set();
|
|
608
|
+
const registeredUpgrades = /* @__PURE__ */ new Set();
|
|
609
|
+
ctx.effect(() => () => {
|
|
610
|
+
for (const dispose of upgradeDisposers) dispose();
|
|
611
|
+
upgradeDisposers.clear();
|
|
612
|
+
});
|
|
613
|
+
const ensureRelayUpgrade = (peerId) => {
|
|
614
|
+
if (registeredUpgrades.has(peerId)) return;
|
|
615
|
+
registeredUpgrades.add(peerId);
|
|
616
|
+
try {
|
|
617
|
+
const dispose = ctx.webServer.registerUpgrade({
|
|
618
|
+
path: `/api/dshb/sync/relay/${peerId}${UPGRADE_PATH_SUFFIX}`,
|
|
619
|
+
handler: (req, socket, head) => {
|
|
620
|
+
const url = new URL(req.url ?? "/", "http://x");
|
|
621
|
+
const remotePath = `${url.pathname.slice(`/api/dshb/sync/relay/${peerId}/app`.length)}${url.search}`;
|
|
622
|
+
relay.relayUpgrade(peerId, req, socket, head, remotePath).catch(() => {
|
|
623
|
+
try {
|
|
624
|
+
socket.destroy();
|
|
625
|
+
} catch {}
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
upgradeDisposers.add(dispose);
|
|
630
|
+
} catch {
|
|
631
|
+
registeredUpgrades.add(peerId);
|
|
632
|
+
}
|
|
633
|
+
};
|
|
374
634
|
ctx.effect(() => ctx.webServer.register({
|
|
375
635
|
kind: "prefix",
|
|
376
636
|
path: "/api/dshb/sync/relay",
|
|
377
637
|
handler: async (req, res) => {
|
|
378
|
-
const
|
|
638
|
+
const url = new URL(req.url ?? "/", "http://x");
|
|
639
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
379
640
|
const peerId = segments[4];
|
|
380
641
|
const action = segments[5];
|
|
381
642
|
const deny = (status, body) => {
|
|
@@ -417,12 +678,64 @@ function registerRemoteRelayRoutes(ctx, relay) {
|
|
|
417
678
|
}
|
|
418
679
|
return;
|
|
419
680
|
}
|
|
681
|
+
if (action === "app" && req.method === "GET") {
|
|
682
|
+
ensureRelayUpgrade(peerId);
|
|
683
|
+
const remotePath = `${url.pathname.slice(`/api/dshb/sync/relay/${peerId}/app`.length) || "/"}${url.search}`;
|
|
684
|
+
try {
|
|
685
|
+
await relay.relayProxy(peerId, req, res, remotePath);
|
|
686
|
+
} catch (error) {
|
|
687
|
+
if (!res.headersSent) relayFailure(res, deny, error);
|
|
688
|
+
else res.destroy();
|
|
689
|
+
}
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
420
692
|
deny(404, {
|
|
421
693
|
ok: false,
|
|
422
694
|
error: "not found"
|
|
423
695
|
});
|
|
424
696
|
}
|
|
425
697
|
}));
|
|
698
|
+
if (deps.resolveSessionOrigin) ctx.effect(() => ctx.webServer.register({
|
|
699
|
+
kind: "exact",
|
|
700
|
+
path: "/api/dshb/sync/session-origin",
|
|
701
|
+
handler: async (req, res) => {
|
|
702
|
+
const deny = (status, body) => {
|
|
703
|
+
res.writeHead(status, {
|
|
704
|
+
"content-type": "application/json",
|
|
705
|
+
"cache-control": "no-store"
|
|
706
|
+
});
|
|
707
|
+
res.end(JSON.stringify(body));
|
|
708
|
+
};
|
|
709
|
+
if (req.method !== "GET") {
|
|
710
|
+
deny(405, {
|
|
711
|
+
ok: false,
|
|
712
|
+
error: "method not allowed"
|
|
713
|
+
});
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const sessionId = new URL(req.url ?? "/", "http://x").searchParams.get("sessionId") ?? "";
|
|
717
|
+
if (!/^[\w:-]{8,200}$/.test(sessionId)) {
|
|
718
|
+
deny(400, {
|
|
719
|
+
ok: false,
|
|
720
|
+
error: "invalid sessionId"
|
|
721
|
+
});
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
const origin = deps.resolveSessionOrigin(sessionId);
|
|
725
|
+
res.writeHead(200, {
|
|
726
|
+
"content-type": "application/json",
|
|
727
|
+
"cache-control": "no-store"
|
|
728
|
+
});
|
|
729
|
+
res.end(JSON.stringify(origin ? {
|
|
730
|
+
ok: true,
|
|
731
|
+
peerId: origin.peerId,
|
|
732
|
+
remoteSessionId: origin.remoteSessionId
|
|
733
|
+
} : {
|
|
734
|
+
ok: true,
|
|
735
|
+
origin: null
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
}));
|
|
426
739
|
}
|
|
427
740
|
function relayFailure(res, deny, error) {
|
|
428
741
|
if (error instanceof RelayTicketError) {
|
|
@@ -511,7 +824,10 @@ function apply(ctx) {
|
|
|
511
824
|
sshWorlds?.remove?.(nodeId);
|
|
512
825
|
dockerWorlds?.remove?.(nodeId);
|
|
513
826
|
});
|
|
514
|
-
const syncSessions = new SyncSessionService(syncState, syncNodes, registry, bindings, () => workspaceRegistry, () => ctx.get("sessionPersistence"), () => ctx.get("attachments"), () => ctx.get("spillStore"), sharedSessionInstructionSnapshotStore(), void 0, void 0, process.env.DSH_HOME)
|
|
827
|
+
const syncSessions = new SyncSessionService(syncState, syncNodes, registry, bindings, () => workspaceRegistry, () => ctx.get("sessionPersistence"), () => ctx.get("attachments"), () => ctx.get("spillStore"), sharedSessionInstructionSnapshotStore(), void 0, void 0, process.env.DSH_HOME, void 0, (header) => {
|
|
828
|
+
const title = ctx.get("sessionProjectionCache")?.cachedSnapshot?.(header, ["title"])?.values?.title;
|
|
829
|
+
return typeof title === "string" && title.trim() ? title : void 0;
|
|
830
|
+
});
|
|
515
831
|
const syncOfflineArchives = new SyncOfflineArchiveService(syncNodes, syncSessions, void 0, void 0, syncState);
|
|
516
832
|
try {
|
|
517
833
|
syncOfflineArchives.cleanupStagingDirectories();
|
|
@@ -540,14 +856,37 @@ function apply(ctx) {
|
|
|
540
856
|
registerWorkspaceRoutes(ctx, registry, bindings, syncNodes);
|
|
541
857
|
registerRemoteDownloadRoutes(ctx, registry, bindings);
|
|
542
858
|
registerWorkspaceInstructionRoutes(ctx, sharedWorkspaceInstructionStore());
|
|
543
|
-
registerSyncPeerRoutes(ctx, syncPeerManager, syncSshTransport, syncCoordinator
|
|
859
|
+
registerSyncPeerRoutes(ctx, syncPeerManager, syncSshTransport, syncCoordinator);
|
|
544
860
|
registerSyncInternalRoutes(ctx, syncState, syncNodes, syncSessions);
|
|
545
861
|
registerSyncSessionRoutes(ctx, syncSessions);
|
|
546
862
|
registerSyncOfflineRoutes(ctx, syncOfflineArchives);
|
|
547
863
|
registerRelayTicketRoutes(ctx, ctx.credentials);
|
|
548
864
|
const remoteRelay = new RemoteRelayService(syncPeerManager, syncSshTransport);
|
|
549
865
|
ctx.provide("remoteRelayService", remoteRelay);
|
|
550
|
-
|
|
866
|
+
const sessionJournal = new SyncSessionJournalStore(process.env.DSH_HOME);
|
|
867
|
+
registerRemoteRelayRoutes(ctx, remoteRelay, { resolveSessionOrigin: (sessionId) => {
|
|
868
|
+
const state = syncState.read();
|
|
869
|
+
let recordKey;
|
|
870
|
+
let remoteSessionId;
|
|
871
|
+
for (const [key, meta] of Object.entries(state.records)) {
|
|
872
|
+
if (meta.kind !== "session" || meta.localEntityId !== sessionId) continue;
|
|
873
|
+
recordKey = key;
|
|
874
|
+
remoteSessionId = meta.originEntityId;
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
if (!recordKey || !remoteSessionId) {
|
|
878
|
+
const archive = Object.values(state.archives).find((a) => a.localSessionId === sessionId);
|
|
879
|
+
if (!archive) return void 0;
|
|
880
|
+
remoteSessionId = archive.originSessionId;
|
|
881
|
+
recordKey = archive.branchId;
|
|
882
|
+
}
|
|
883
|
+
const peerId = sessionJournal.peerIdForRecordKey(recordKey);
|
|
884
|
+
if (!peerId) return void 0;
|
|
885
|
+
return syncPeers.list().some((p) => p.id === peerId && p.enabled) ? {
|
|
886
|
+
peerId,
|
|
887
|
+
remoteSessionId
|
|
888
|
+
} : void 0;
|
|
889
|
+
} });
|
|
551
890
|
ctx.effect(() => {
|
|
552
891
|
const stopScheduler = syncScheduler.start();
|
|
553
892
|
const inboxSweeper = setInterval(() => {
|