dsh-remote-vs 0.25.0 → 0.27.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/README.en.md +12 -15
- package/README.md +11 -12
- package/THIRD-PARTY-NOTICES.md +193 -0
- package/lib/client.js +7313 -79961
- package/lib/index.js +1529 -964
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/CodeEditor.d.ts +22 -0
- package/lib/types/client/OutlinePanel.d.ts +22 -0
- package/lib/types/client/ProcessesSection.d.ts +19 -0
- package/lib/types/client/RemoteExplorer.d.ts +53 -0
- package/lib/types/client/api.d.ts +42 -3
- package/lib/types/client/blame-model.d.ts +77 -0
- package/lib/types/client/changes/review-state.d.ts +19 -0
- package/lib/types/client/connection.d.ts +16 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/outline-model.d.ts +59 -0
- package/lib/types/client/request-gate.d.ts +110 -0
- package/lib/types/client/scm/DiffView.d.ts +45 -7
- package/lib/types/client/scm/actions.d.ts +43 -4
- package/lib/types/client/store.d.ts +71 -0
- package/lib/types/client/vsc/FindWidget.d.ts +6 -0
- package/lib/types/client/vsc/blame.d.ts +19 -0
- package/lib/types/client/vsc/change-marks.d.ts +16 -6
- package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
- package/lib/types/client/vsc/kit.d.ts +11 -2
- package/lib/types/client/vsc/preview-document.d.ts +46 -0
- package/lib/types/client/vsc/preview-model.d.ts +31 -0
- package/lib/types/client/vsc/preview-services.d.ts +24 -0
- package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
- package/lib/types/client/vsc/terminal-theme.d.ts +28 -2
- package/lib/types/forward.d.ts +12 -0
- package/lib/types/fs-ops.d.ts +42 -0
- package/lib/types/git-ops.d.ts +18 -0
- package/lib/types/http.d.ts +2 -0
- package/lib/types/keygen.d.ts +9 -0
- package/lib/types/monitor.d.ts +8 -0
- package/lib/types/notices.d.ts +16 -1
- package/lib/types/p2/helper-rpc.d.ts +45 -0
- package/lib/types/processes.d.ts +43 -0
- package/lib/types/protocol.d.ts +34 -0
- package/lib/types/routes.d.ts +2 -0
- package/lib/types/store.d.ts +7 -0
- package/lib/types/terminal.d.ts +8 -0
- package/lib/types/view-settings.d.ts +5 -0
- package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
- package/package.json +3 -1
- package/src/client/CodeEditor.tsx +194 -9
- package/src/client/DirectoryFlow.tsx +16 -0
- package/src/client/DoctorPanel.tsx +70 -7
- package/src/client/EditorPane.tsx +574 -42
- package/src/client/HostsView.tsx +12 -3
- package/src/client/MonitorView.tsx +16 -5
- package/src/client/OutlinePanel.tsx +287 -0
- package/src/client/ProcessesSection.tsx +292 -0
- package/src/client/RemoteDirectoryPicker.tsx +12 -0
- package/src/client/RemoteExplorer.tsx +929 -40
- package/src/client/RemoteRoot.tsx +147 -22
- package/src/client/SearchView.tsx +8 -0
- package/src/client/TerminalSnippets.tsx +83 -8
- package/src/client/api.ts +114 -13
- package/src/client/blame-model.ts +242 -0
- package/src/client/changes/ChangesDock.tsx +188 -6
- package/src/client/changes/changes.css +10 -3
- package/src/client/changes/review-state.ts +34 -0
- package/src/client/connection.ts +29 -0
- package/src/client/downloads.ts +49 -4
- package/src/client/i18n.ts +276 -8
- package/src/client/index.tsx +4 -0
- package/src/client/outline-model.ts +775 -0
- package/src/client/request-gate.ts +221 -0
- package/src/client/scm/CommitPane.tsx +20 -2
- package/src/client/scm/DiffView.tsx +343 -117
- package/src/client/scm/HistoryPanel.tsx +61 -3
- package/src/client/scm/RepoPicker.tsx +1 -1
- package/src/client/scm/ScmPanel.tsx +391 -87
- package/src/client/scm/actions.ts +150 -14
- package/src/client/store.tsx +426 -14
- package/src/client/ui.tsx +4 -0
- package/src/client/vsc/FindWidget.tsx +24 -9
- package/src/client/vsc/blame.ts +147 -0
- package/src/client/vsc/breadcrumb.tsx +91 -3
- package/src/client/vsc/change-marks.ts +93 -44
- package/src/client/vsc/editor-keymap.ts +59 -1
- package/src/client/vsc/file-icons.tsx +17 -3
- package/src/client/vsc/kit.tsx +29 -20
- package/src/client/vsc/preview-document.tsx +323 -0
- package/src/client/vsc/preview-model.ts +216 -0
- package/src/client/vsc/preview-services.ts +118 -0
- package/src/client/vsc/save-shortcut.ts +47 -1
- package/src/client/vsc/terminal-theme.ts +44 -18
- package/src/client/vsc/vsc.css +6509 -5458
- package/src/forward.ts +47 -9
- package/src/fs-ops.ts +124 -4
- package/src/git-ops.ts +54 -3
- package/src/http.ts +35 -4
- package/src/keygen.ts +55 -4
- package/src/monitor.ts +13 -0
- package/src/notices.ts +23 -1
- package/src/p2/helper-rpc.ts +113 -7
- package/src/p2/worlds.ts +14 -1
- package/src/processes.ts +177 -0
- package/src/protocol.ts +37 -0
- package/src/routes.ts +330 -43
- package/src/sftp.ts +51 -14
- package/src/store.ts +10 -0
- package/src/terminal.ts +73 -26
- package/src/view-settings.ts +50 -10
- package/lib/types/client/Dialog.d.ts +0 -14
- package/src/client/Dialog.tsx +0 -76
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as dshHome, D as viewSettingsPath, E as storePath, S as aliasRoot, T as keysDir, _ as MAX_TIMEOUT_MS, a as takeHostNotices, b as resolveTimeouts, c as pushPipeline, d as WriteConflictError, f as attachmentHeader, g as DeadlineError, h as DEFAULT_TIMEOUTS, i as drainHostNotices, l as seedHelperCache, m as DEFAULT_READ_TIMEOUT_MS, n as TombstonedError, o as helperAvailability, p as DEFAULT_GIT_TIMEOUT_MS, s as probe, t as RemoteWorlds, u as RemoteFiles, v as MIN_TIMEOUT_MS, w as ensurePrivateDir, x as require_lib, y as clampExecTimeout } from "./worlds-RG8K5eom.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
3
|
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, statSync, writeSync } from "node:fs";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
@@ -26,7 +26,7 @@ function emptyDoc() {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
/** Same file identity (both absent counts as same). */
|
|
29
|
-
function sameStamp(left, right) {
|
|
29
|
+
function sameStamp$1(left, right) {
|
|
30
30
|
if (left === null || right === null) return left === right;
|
|
31
31
|
return left.mtimeMs === right.mtimeMs && left.size === right.size;
|
|
32
32
|
}
|
|
@@ -66,6 +66,15 @@ var RemoteStore = class {
|
|
|
66
66
|
this.reload();
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
+
* Absolute path of the JSON document.
|
|
70
|
+
*
|
|
71
|
+
* Diagnostics only: a corrupt file must name itself in the error a surface
|
|
72
|
+
* shows, otherwise the user only sees "the request failed".
|
|
73
|
+
*/
|
|
74
|
+
get filePath() {
|
|
75
|
+
return this.file;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
69
78
|
* Reload from disk (picks up external edits).
|
|
70
79
|
*
|
|
71
80
|
* Re-reading on every access is deliberate: the root fs router asks this store
|
|
@@ -80,7 +89,7 @@ var RemoteStore = class {
|
|
|
80
89
|
*/
|
|
81
90
|
reload() {
|
|
82
91
|
const stamp = this.stampOf();
|
|
83
|
-
if (stamp !== void 0 && this.stamp !== void 0 && sameStamp(stamp, this.stamp)) return;
|
|
92
|
+
if (stamp !== void 0 && this.stamp !== void 0 && sameStamp$1(stamp, this.stamp)) return;
|
|
84
93
|
this.doc = this.loadDoc();
|
|
85
94
|
this.stamp = stamp;
|
|
86
95
|
}
|
|
@@ -168,7 +177,7 @@ var RemoteStore = class {
|
|
|
168
177
|
const before = JSON.stringify(this.doc);
|
|
169
178
|
const result = apply(this.doc);
|
|
170
179
|
if (JSON.stringify(this.doc) === before) return result;
|
|
171
|
-
if (!sameStamp(base, this.stampOf() ?? null)) continue;
|
|
180
|
+
if (!sameStamp$1(base, this.stampOf() ?? null)) continue;
|
|
172
181
|
this.save();
|
|
173
182
|
return result;
|
|
174
183
|
}
|
|
@@ -408,7 +417,9 @@ const REMOTE_API = {
|
|
|
408
417
|
/** Remote listening TCP sockets for the port panel (one bounded probe). */
|
|
409
418
|
listenPorts: "/api/dsh-remote/listen-ports",
|
|
410
419
|
/** Live system metrics for the monitor panel (one bounded probe per host). */
|
|
411
|
-
metrics: "/api/dsh-remote/metrics"
|
|
420
|
+
metrics: "/api/dsh-remote/metrics",
|
|
421
|
+
/** One bounded process table for the monitor page, plus TERM/KILL actions. */
|
|
422
|
+
processes: "/api/dsh-remote/processes"
|
|
412
423
|
};
|
|
413
424
|
/** Suggested/attachment file name for one multi-selection archive. */
|
|
414
425
|
function selectionDownloadName(count) {
|
|
@@ -458,6 +469,21 @@ function clampViewWidthRatio(value) {
|
|
|
458
469
|
function defaults() {
|
|
459
470
|
return { ...REMOTE_VIEW_SETTINGS_DEFAULTS };
|
|
460
471
|
}
|
|
472
|
+
/** Same file identity (both absent counts as same). */
|
|
473
|
+
function sameStamp(left, right) {
|
|
474
|
+
if (left === null || right === null) return left === right;
|
|
475
|
+
return left.mtimeMs === right.mtimeMs && left.size === right.size;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Replays one read-modify-write before giving up.
|
|
479
|
+
*
|
|
480
|
+
* Desktop and `dsh web` share `$DSH_HOME/dsh-remote-view.json` by design, and
|
|
481
|
+
* every patch rewrites the WHOLE document: a plain read-merge-write let the last
|
|
482
|
+
* window win and silently dropped the other one's preference. A stamp re-check
|
|
483
|
+
* right before the write turns that into a replay on fresh content; a third
|
|
484
|
+
* failed attempt fails loud instead of losing a setting.
|
|
485
|
+
*/
|
|
486
|
+
const PATCH_ATTEMPTS = 3;
|
|
461
487
|
/** A finite width share, clamped into the allowed band; anything else is a miss. */
|
|
462
488
|
function readWidthRatio(value) {
|
|
463
489
|
if (typeof value !== "number" || !Number.isFinite(value)) return void 0;
|
|
@@ -502,13 +528,33 @@ var RemoteViewSettingsStore = class {
|
|
|
502
528
|
* @returns the settings as stored.
|
|
503
529
|
*/
|
|
504
530
|
patch(patch) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
531
|
+
for (let attempt = 0; attempt < PATCH_ATTEMPTS; attempt += 1) {
|
|
532
|
+
const before = this.stampOf();
|
|
533
|
+
const current = this.get();
|
|
534
|
+
const next = {
|
|
535
|
+
autoOpenRemoteWorkspace: typeof patch.autoOpenRemoteWorkspace === "boolean" ? patch.autoOpenRemoteWorkspace : current.autoOpenRemoteWorkspace,
|
|
536
|
+
remoteWorkspaceWidthRatio: readWidthRatio(patch.remoteWorkspaceWidthRatio) ?? current.remoteWorkspaceWidthRatio
|
|
537
|
+
};
|
|
538
|
+
if (!sameStamp(before, this.stampOf())) continue;
|
|
539
|
+
this.save(next);
|
|
540
|
+
return next;
|
|
541
|
+
}
|
|
542
|
+
throw new Error(`${this.file}: another process keeps editing the view settings; retry the operation`);
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* File identity right now; `null` when the file is absent (or cannot be
|
|
546
|
+
* statted — `get()` fails loud on that case anyway).
|
|
547
|
+
*/
|
|
548
|
+
stampOf() {
|
|
549
|
+
try {
|
|
550
|
+
const stat = statSync(this.file, { throwIfNoEntry: false });
|
|
551
|
+
return stat === void 0 ? null : {
|
|
552
|
+
mtimeMs: stat.mtimeMs,
|
|
553
|
+
size: stat.size
|
|
554
|
+
};
|
|
555
|
+
} catch {
|
|
556
|
+
return null;
|
|
557
|
+
}
|
|
512
558
|
}
|
|
513
559
|
/** Write one document atomically: a crash never leaves a half-written file. */
|
|
514
560
|
save(settings) {
|
|
@@ -2007,6 +2053,18 @@ var ForwardManager = class {
|
|
|
2007
2053
|
* first listener orphaned — unreachable from the panel and never closed.
|
|
2008
2054
|
*/
|
|
2009
2055
|
starting = /* @__PURE__ */ new Map();
|
|
2056
|
+
/**
|
|
2057
|
+
* Rules stopped while their listener was still BINDING.
|
|
2058
|
+
*
|
|
2059
|
+
* A pending bind is not in `forwards` yet, so `stop()`, `stopAlias()`,
|
|
2060
|
+
* `stopAll()` and `dispose()` used to see nothing to close: the listener the
|
|
2061
|
+
* OS was about to hand out stayed bound with no panel row left to stop it
|
|
2062
|
+
* (click a forward, immediately disable the plugin or delete the host, and
|
|
2063
|
+
* `127.0.0.1:<port>` stayed taken until the Host restarted). The id sits here
|
|
2064
|
+
* until the bind settles; a settling bind that finds its id records itself as
|
|
2065
|
+
* stopped and closes the fresh listener instead of entering `forwards`.
|
|
2066
|
+
*/
|
|
2067
|
+
stoppedWhileBinding = /* @__PURE__ */ new Set();
|
|
2010
2068
|
/** Set on unload so a bind that settles afterwards closes itself. */
|
|
2011
2069
|
disposed = false;
|
|
2012
2070
|
constructor(deps) {
|
|
@@ -2032,15 +2090,16 @@ var ForwardManager = class {
|
|
|
2032
2090
|
const maxPerAlias = this.deps.maxPerAlias ?? 16;
|
|
2033
2091
|
if (perAlias >= maxPerAlias) throw new Error(`host ${request.alias} already has ${maxPerAlias} local forwards; stop one first`);
|
|
2034
2092
|
const pending = this.bindForward(id, request, requestedLocalPort).then((forward) => {
|
|
2035
|
-
if (this.disposed) {
|
|
2093
|
+
if (this.disposed || this.stoppedWhileBinding.has(id)) {
|
|
2036
2094
|
this.closeForward(forward);
|
|
2037
|
-
throw new Error("the port-forward manager was unloaded while the rule was binding");
|
|
2095
|
+
throw new Error(this.disposed ? "the port-forward manager was unloaded while the rule was binding" : "the rule was stopped while its local listener was binding");
|
|
2038
2096
|
}
|
|
2039
2097
|
this.forwards.set(id, forward);
|
|
2040
2098
|
this.log("info", `forward ${id} listening on 127.0.0.1:${forward.localPort}`);
|
|
2041
2099
|
return this.stateOf(forward);
|
|
2042
2100
|
}).finally(() => {
|
|
2043
2101
|
this.starting.delete(id);
|
|
2102
|
+
this.stoppedWhileBinding.delete(id);
|
|
2044
2103
|
});
|
|
2045
2104
|
this.starting.set(id, pending);
|
|
2046
2105
|
return pending;
|
|
@@ -2048,10 +2107,15 @@ var ForwardManager = class {
|
|
|
2048
2107
|
/** Stop one rule. Returns false when the id is unknown (already stopped). */
|
|
2049
2108
|
stop(id) {
|
|
2050
2109
|
const forward = this.forwards.get(id);
|
|
2051
|
-
if (forward
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2110
|
+
if (forward !== void 0) {
|
|
2111
|
+
this.forwards.delete(id);
|
|
2112
|
+
this.closeForward(forward);
|
|
2113
|
+
this.log("info", `forward ${id} stopped`);
|
|
2114
|
+
return true;
|
|
2115
|
+
}
|
|
2116
|
+
if (!this.starting.has(id)) return false;
|
|
2117
|
+
this.stoppedWhileBinding.add(id);
|
|
2118
|
+
this.log("info", `forward ${id} stopped while binding; its listener will close on settle`);
|
|
2055
2119
|
return true;
|
|
2056
2120
|
}
|
|
2057
2121
|
/**
|
|
@@ -2069,6 +2133,11 @@ var ForwardManager = class {
|
|
|
2069
2133
|
this.closeForward(forward);
|
|
2070
2134
|
stopped += 1;
|
|
2071
2135
|
}
|
|
2136
|
+
for (const id of [...this.starting.keys()]) {
|
|
2137
|
+
if (this.forwards.has(id) || !id.startsWith(`${alias}|`)) continue;
|
|
2138
|
+
this.stoppedWhileBinding.add(id);
|
|
2139
|
+
stopped += 1;
|
|
2140
|
+
}
|
|
2072
2141
|
if (stopped > 0) this.log("info", `stopped ${stopped} forward(s) for deleted host ${alias}`);
|
|
2073
2142
|
return stopped;
|
|
2074
2143
|
}
|
|
@@ -2082,6 +2151,11 @@ var ForwardManager = class {
|
|
|
2082
2151
|
stopAll() {
|
|
2083
2152
|
let stopped = 0;
|
|
2084
2153
|
for (const id of [...this.forwards.keys()]) if (this.stop(id)) stopped += 1;
|
|
2154
|
+
for (const id of [...this.starting.keys()]) {
|
|
2155
|
+
if (this.forwards.has(id)) continue;
|
|
2156
|
+
this.stoppedWhileBinding.add(id);
|
|
2157
|
+
stopped += 1;
|
|
2158
|
+
}
|
|
2085
2159
|
return stopped;
|
|
2086
2160
|
}
|
|
2087
2161
|
/** Every rule, optionally narrowed to one host alias. */
|
|
@@ -2091,7 +2165,7 @@ var ForwardManager = class {
|
|
|
2091
2165
|
/** Close every listener and every proxied connection (plugin unload). */
|
|
2092
2166
|
dispose() {
|
|
2093
2167
|
this.disposed = true;
|
|
2094
|
-
|
|
2168
|
+
this.stopAll();
|
|
2095
2169
|
}
|
|
2096
2170
|
/** Bind the loopback listener. Split out so the error path is one place. */
|
|
2097
2171
|
async bindForward(id, request, requestedLocalPort) {
|
|
@@ -2314,9 +2388,31 @@ async function readJsonBodyResult(req, opts = {}) {
|
|
|
2314
2388
|
function isJsonObject(value) {
|
|
2315
2389
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2316
2390
|
}
|
|
2391
|
+
/**
|
|
2392
|
+
* The host alias one response is about (consumed by {@link writeJson}).
|
|
2393
|
+
*
|
|
2394
|
+
* A response may only carry news about the host it concerns, otherwise one
|
|
2395
|
+
* window shows another window's silent helper push and the queue loses it for
|
|
2396
|
+
* the window that caused it. `null` is the explicit "about no single host"
|
|
2397
|
+
* answer (a hosts/workspaces listing): it leaves the queue alone. An untouched
|
|
2398
|
+
* response keeps the historical drain-all answer — only this plugin's route
|
|
2399
|
+
* table marks its responses, and every route states what it is about.
|
|
2400
|
+
*/
|
|
2401
|
+
const noticeAliasOf = /* @__PURE__ */ new WeakMap();
|
|
2402
|
+
/** Declare which host the response being written concerns (`null` = no host). */
|
|
2403
|
+
function scopeHostNotices(res, alias) {
|
|
2404
|
+
noticeAliasOf.set(res, alias === "" ? null : alias);
|
|
2405
|
+
}
|
|
2406
|
+
/** The notices this response may carry and consume. */
|
|
2407
|
+
function noticesFor(res, attachable, status) {
|
|
2408
|
+
if (!attachable || status >= 400) return [];
|
|
2409
|
+
if (!noticeAliasOf.has(res)) return drainHostNotices();
|
|
2410
|
+
const alias = noticeAliasOf.get(res);
|
|
2411
|
+
return typeof alias === "string" && alias !== "" ? takeHostNotices(alias) : [];
|
|
2412
|
+
}
|
|
2317
2413
|
/** Write one JSON response. */
|
|
2318
2414
|
function writeJson(res, status, body, headers = {}) {
|
|
2319
|
-
const notices = typeof body === "object" && body !== null && !Array.isArray(body)
|
|
2415
|
+
const notices = noticesFor(res, typeof body === "object" && body !== null && !Array.isArray(body), status);
|
|
2320
2416
|
const payload = notices.length > 0 ? {
|
|
2321
2417
|
...body,
|
|
2322
2418
|
notices
|
|
@@ -2477,6 +2573,40 @@ function selectionArchiveCommand(paths) {
|
|
|
2477
2573
|
* store entry flips to key auth with the managed keyPath.
|
|
2478
2574
|
*/
|
|
2479
2575
|
const execFileAsync = promisify(execFile);
|
|
2576
|
+
/**
|
|
2577
|
+
* `type base64` — the only two fields of a `.pub` file that are key material.
|
|
2578
|
+
*
|
|
2579
|
+
* ssh-keygen appends a free-text comment, and that comment is NOT part of the
|
|
2580
|
+
* key. The old code only checked the line's prefix and then spliced the WHOLE
|
|
2581
|
+
* line into a single-quoted remote shell command; a garbled or tampered `.pub`
|
|
2582
|
+
* therefore reached `sh -c` as code. The strict grammar below admits nothing
|
|
2583
|
+
* but the two safe fields, and the comment is regenerated from the alias.
|
|
2584
|
+
*/
|
|
2585
|
+
const PUBLIC_KEY_FIELDS_RE = /^(ssh-ed25519) ([A-Za-z0-9+/]+={0,2})$/;
|
|
2586
|
+
/** Ed25519 wire blob: 4-byte length + "ssh-ed25519", 4-byte length + 32 bytes. */
|
|
2587
|
+
function isEd25519Blob(blob) {
|
|
2588
|
+
const decoded = Buffer.from(blob, "base64");
|
|
2589
|
+
if (decoded.length !== 51) return false;
|
|
2590
|
+
if (decoded.readUInt32BE(0) !== 11) return false;
|
|
2591
|
+
if (decoded.subarray(4, 15).toString("latin1") !== "ssh-ed25519") return false;
|
|
2592
|
+
return decoded.readUInt32BE(15) === 32;
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Normalize one `.pub` line to `ssh-ed25519 <base64> dsh-remote-vs/<alias>`.
|
|
2596
|
+
*
|
|
2597
|
+
* Throws when the line is not a well-formed ed25519 public key, so the caller
|
|
2598
|
+
* sees a clear "this file is not a key" error instead of a remote login that
|
|
2599
|
+
* fails later. The returned text is the only form that may enter the shell
|
|
2600
|
+
* command: its comment is ours, and both remaining fields are shell-inert.
|
|
2601
|
+
*/
|
|
2602
|
+
function formatPublicKey(raw, alias) {
|
|
2603
|
+
const fields = raw.trim().split(/\s+/);
|
|
2604
|
+
const type = fields[0];
|
|
2605
|
+
const blob = fields[1];
|
|
2606
|
+
if (fields.length < 2 || type === void 0 || blob === void 0 || !PUBLIC_KEY_FIELDS_RE.test(`${type} ${blob}`)) throw new Error("expected an \"ssh-ed25519 <base64>\" public key line");
|
|
2607
|
+
if (!isEd25519Blob(blob)) throw new Error("the base64 blob is not a valid ed25519 public key");
|
|
2608
|
+
return `${type} ${blob} dsh-remote-vs/${alias.replace(/[^A-Za-z0-9._-]/g, "_")}`;
|
|
2609
|
+
}
|
|
2480
2610
|
/** Ensure the managed keypair exists; return the private key path. */
|
|
2481
2611
|
async function ensureKeypair(alias, dir = keysDir()) {
|
|
2482
2612
|
ensurePrivateDir(dir);
|
|
@@ -2501,8 +2631,12 @@ async function ensureKeypair(alias, dir = keysDir()) {
|
|
|
2501
2631
|
chmodSync(privatePath, 384);
|
|
2502
2632
|
} catch {}
|
|
2503
2633
|
}
|
|
2504
|
-
|
|
2505
|
-
|
|
2634
|
+
let publicKey;
|
|
2635
|
+
try {
|
|
2636
|
+
publicKey = formatPublicKey(readFileSync(publicPath, "utf8"), alias);
|
|
2637
|
+
} catch (error) {
|
|
2638
|
+
throw new Error(`unexpected public key format in ${publicPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2639
|
+
}
|
|
2506
2640
|
return {
|
|
2507
2641
|
privatePath,
|
|
2508
2642
|
publicKey
|
|
@@ -2524,7 +2658,7 @@ async function bootstrap(pool, store, alias) {
|
|
|
2524
2658
|
const script = [
|
|
2525
2659
|
"mkdir -p ~/.ssh && chmod 700 ~/.ssh",
|
|
2526
2660
|
`touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys`,
|
|
2527
|
-
`grep -
|
|
2661
|
+
`grep -qF '${publicKey.split(" ").slice(0, 2).join(" ")}' ~/.ssh/authorized_keys 2>/dev/null || echo '${publicKey}' >> ~/.ssh/authorized_keys`
|
|
2528
2662
|
].join(" && ");
|
|
2529
2663
|
const result = await pool.exec(alias, script, { timeoutMs: 3e4 });
|
|
2530
2664
|
if (!result.success) throw new Error(`key install failed [exit code: ${result.exitCode}]: ${result.stderr || result.stdout || result.error || "unknown"}`);
|
|
@@ -2829,6 +2963,14 @@ const OUT_COALESCE_CHARS = 64 * 1024;
|
|
|
2829
2963
|
*/
|
|
2830
2964
|
const OUT_ECHO_IDLE_MS = 24;
|
|
2831
2965
|
/**
|
|
2966
|
+
* How much recent output a reattaching socket receives (see `Session.replay`).
|
|
2967
|
+
*
|
|
2968
|
+
* 128 Ki UTF-16 units is a few screens of logs plus the prompt line; the buffer
|
|
2969
|
+
* may grow to twice this between trims, so one terminal costs at most ~512 KiB
|
|
2970
|
+
* of the host's memory in replay state.
|
|
2971
|
+
*/
|
|
2972
|
+
const REPLAY_MAX_CHARS = 128 * 1024;
|
|
2973
|
+
/**
|
|
2832
2974
|
* The largest client frame accepted, in bytes.
|
|
2833
2975
|
*
|
|
2834
2976
|
* The only frame with a payload is `in` — keystrokes, and pastes, which the
|
|
@@ -2888,6 +3030,7 @@ var TerminalSessionRegistry = class {
|
|
|
2888
3030
|
paused: false,
|
|
2889
3031
|
outBuffer: "",
|
|
2890
3032
|
outTimer: void 0,
|
|
3033
|
+
replay: "",
|
|
2891
3034
|
lastFlushAt: 0,
|
|
2892
3035
|
outstanding: /* @__PURE__ */ new Map(),
|
|
2893
3036
|
reap: void 0,
|
|
@@ -2941,6 +3084,10 @@ var TerminalSessionRegistry = class {
|
|
|
2941
3084
|
const session = this.sessions.get(id);
|
|
2942
3085
|
if (session === void 0 || !session.alive) return false;
|
|
2943
3086
|
this.hookSocket(session, ws);
|
|
3087
|
+
if (session.replay !== "") this.sendOutTo(session, ws, JSON.stringify({
|
|
3088
|
+
t: "out",
|
|
3089
|
+
d: session.replay
|
|
3090
|
+
}));
|
|
2944
3091
|
this.flushOut(session);
|
|
2945
3092
|
this.send(ws, {
|
|
2946
3093
|
t: "ready",
|
|
@@ -3078,6 +3225,8 @@ var TerminalSessionRegistry = class {
|
|
|
3078
3225
|
if (session.outBuffer === "") return;
|
|
3079
3226
|
const text = session.outBuffer;
|
|
3080
3227
|
session.outBuffer = "";
|
|
3228
|
+
session.replay += text;
|
|
3229
|
+
if (session.replay.length > REPLAY_MAX_CHARS * 2) session.replay = session.replay.slice(-131072);
|
|
3081
3230
|
session.lastFlushAt = Date.now();
|
|
3082
3231
|
this.broadcast(session, {
|
|
3083
3232
|
t: "out",
|
|
@@ -3099,29 +3248,37 @@ var TerminalSessionRegistry = class {
|
|
|
3099
3248
|
}
|
|
3100
3249
|
broadcast(session, frame) {
|
|
3101
3250
|
const text = JSON.stringify(frame);
|
|
3251
|
+
for (const socket of session.sockets) this.sendOutTo(session, socket, text);
|
|
3252
|
+
if (session.pending > OUT_HIGH_WATER) this.setPaused(session, true);
|
|
3253
|
+
}
|
|
3254
|
+
/**
|
|
3255
|
+
* Send one already-stringified `out` frame to ONE socket with accounting.
|
|
3256
|
+
*
|
|
3257
|
+
* Shared by `broadcast` and by the replay on `attach`; the byte count must be
|
|
3258
|
+
* counted once per socket so the session's brake reflects every socket's
|
|
3259
|
+
* backlog (see `Session.pending`).
|
|
3260
|
+
*/
|
|
3261
|
+
sendOutTo(session, socket, text) {
|
|
3102
3262
|
const size = Buffer.byteLength(text, "utf8");
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
done();
|
|
3122
|
-
}
|
|
3263
|
+
let settled = false;
|
|
3264
|
+
const done = () => {
|
|
3265
|
+
if (settled) return;
|
|
3266
|
+
settled = true;
|
|
3267
|
+
const left = session.outstanding.get(socket);
|
|
3268
|
+
if (left === void 0) return;
|
|
3269
|
+
const remaining = Math.max(0, left - size);
|
|
3270
|
+
if (remaining === 0) session.outstanding.delete(socket);
|
|
3271
|
+
else session.outstanding.set(socket, remaining);
|
|
3272
|
+
session.pending = Math.max(0, session.pending - size);
|
|
3273
|
+
if (session.pending <= OUT_LOW_WATER) this.setPaused(session, false);
|
|
3274
|
+
};
|
|
3275
|
+
session.pending += size;
|
|
3276
|
+
session.outstanding.set(socket, (session.outstanding.get(socket) ?? 0) + size);
|
|
3277
|
+
try {
|
|
3278
|
+
socket.send(text, done);
|
|
3279
|
+
} catch {
|
|
3280
|
+
done();
|
|
3123
3281
|
}
|
|
3124
|
-
if (session.pending > OUT_HIGH_WATER) this.setPaused(session, true);
|
|
3125
3282
|
}
|
|
3126
3283
|
};
|
|
3127
3284
|
//#endregion
|
|
@@ -3587,6 +3744,16 @@ var HostMonitor = class {
|
|
|
3587
3744
|
this.inflight.delete(alias);
|
|
3588
3745
|
this.generations.set(alias, (this.generations.get(alias) ?? 0) + 1);
|
|
3589
3746
|
}
|
|
3747
|
+
/**
|
|
3748
|
+
* Snapshots already in the cache, without dialing anything.
|
|
3749
|
+
*
|
|
3750
|
+
* A GET must not have a dial side effect: listing what the Host knows is the
|
|
3751
|
+
* read-only shape, and only an explicit per-host request (or the opt-in batch
|
|
3752
|
+
* flag) may wake a sleeping host.
|
|
3753
|
+
*/
|
|
3754
|
+
cached() {
|
|
3755
|
+
return [...this.cache.values()].map((entry) => entry.snapshot).sort((left, right) => left.alias.localeCompare(right.alias));
|
|
3756
|
+
}
|
|
3590
3757
|
async probe(alias) {
|
|
3591
3758
|
const generation = this.generations.get(alias) ?? 0;
|
|
3592
3759
|
const startedAt = this.now();
|
|
@@ -3644,17 +3811,175 @@ var HostMonitor = class {
|
|
|
3644
3811
|
}
|
|
3645
3812
|
};
|
|
3646
3813
|
//#endregion
|
|
3814
|
+
//#region src/processes.ts
|
|
3815
|
+
/** Marker proving which probe branch actually ran. */
|
|
3816
|
+
const PROCESSES_MARK = "@@RVS-PROCS";
|
|
3817
|
+
/** Budget for the whole probe; a stuck process table must not pin a channel. */
|
|
3818
|
+
const PROCESSES_TIMEOUT_MS = 15e3;
|
|
3819
|
+
/**
|
|
3820
|
+
* One bounded probe.
|
|
3821
|
+
*
|
|
3822
|
+
* GNU first (`--sort` + `etimes`), then the portable `ps aux` table for BSD /
|
|
3823
|
+
* old systems (`ppid` and elapsed time are unknown there). The marker
|
|
3824
|
+
* line is printed BEFORE the table so an interrupted probe is distinguishable
|
|
3825
|
+
* from a genuinely empty one; the final marker lets the parser reject a
|
|
3826
|
+
* truncated run.
|
|
3827
|
+
*/
|
|
3828
|
+
function processesCommand(max = 500) {
|
|
3829
|
+
const cap = Math.max(1, Math.min(500, Math.floor(max)));
|
|
3830
|
+
const gnu = `ps -eo pid=,ppid=,user:20=,pcpu=,pmem=,etimes=,args= --sort=-pcpu 2>/dev/null`;
|
|
3831
|
+
return [
|
|
3832
|
+
"export LC_ALL=C",
|
|
3833
|
+
`if ${gnu} >/dev/null 2>&1; then`,
|
|
3834
|
+
` echo "${PROCESSES_MARK} gnu"`,
|
|
3835
|
+
` ${gnu} | head -n ${cap + 1}`,
|
|
3836
|
+
"elif ps aux >/dev/null 2>&1; then",
|
|
3837
|
+
` echo "${PROCESSES_MARK} bsd"`,
|
|
3838
|
+
` ps aux | tail -n +2 | sort -k3 -rn | head -n ${cap + 1}`,
|
|
3839
|
+
"else",
|
|
3840
|
+
` echo "${PROCESSES_MARK} none"`,
|
|
3841
|
+
"fi",
|
|
3842
|
+
`echo "${PROCESSES_MARK} end"`
|
|
3843
|
+
].join("\n");
|
|
3844
|
+
}
|
|
3845
|
+
function isKillSignal(value) {
|
|
3846
|
+
return value === "TERM" || value === "KILL";
|
|
3847
|
+
}
|
|
3848
|
+
/** Assert a PID a process snapshot may contain (1 is the remote init). */
|
|
3849
|
+
function assertProcessPid(value) {
|
|
3850
|
+
const pid = typeof value === "number" ? value : Number(value);
|
|
3851
|
+
if (!Number.isInteger(pid) || pid <= 1 || pid > 2147483647) throw new Error("invalid process id");
|
|
3852
|
+
return pid;
|
|
3853
|
+
}
|
|
3854
|
+
/**
|
|
3855
|
+
* Build the one kill command. The PID has already passed the snapshot check;
|
|
3856
|
+
* single-quoting it keeps the command shape uniform with every other builder.
|
|
3857
|
+
*/
|
|
3858
|
+
function killCommand(pid, signal) {
|
|
3859
|
+
return `kill -s ${signal} ${shQuote(String(assertProcessPid(pid)))}`;
|
|
3860
|
+
}
|
|
3861
|
+
function numberOr(value) {
|
|
3862
|
+
if (!/^[0-9]+(?:\.[0-9]+)?$/.test(value)) return null;
|
|
3863
|
+
const number = Number(value);
|
|
3864
|
+
return Number.isFinite(number) ? number : null;
|
|
3865
|
+
}
|
|
3866
|
+
/** Bound one argv line before it is serialized into the JSON response. */
|
|
3867
|
+
function trimCommand(command) {
|
|
3868
|
+
return command.length <= 300 ? command : `${command.slice(0, 299)}…`;
|
|
3869
|
+
}
|
|
3870
|
+
/** GNU row: pid ppid user pcpu pmem etimes args. */
|
|
3871
|
+
function parseGnuRow(line) {
|
|
3872
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+([0-9.]+)\s+([0-9.]+)\s+(\d+)\s+(.*)$/.exec(line);
|
|
3873
|
+
if (match === null) return void 0;
|
|
3874
|
+
const pid = match[1];
|
|
3875
|
+
const ppid = match[2];
|
|
3876
|
+
const user = match[3];
|
|
3877
|
+
const cpu = match[4];
|
|
3878
|
+
const mem = match[5];
|
|
3879
|
+
const elapsed = match[6];
|
|
3880
|
+
const command = match[7];
|
|
3881
|
+
return {
|
|
3882
|
+
pid: Number(pid),
|
|
3883
|
+
ppid: Number(ppid),
|
|
3884
|
+
user,
|
|
3885
|
+
cpuPercent: numberOr(cpu) ?? 0,
|
|
3886
|
+
memPercent: numberOr(mem) ?? 0,
|
|
3887
|
+
elapsedSeconds: numberOr(elapsed) ?? null,
|
|
3888
|
+
command: trimCommand(command.trim())
|
|
3889
|
+
};
|
|
3890
|
+
}
|
|
3891
|
+
/** BSD `ps aux` row: user pid %cpu %mem vsz rss tt stat started time command. */
|
|
3892
|
+
function parseBsdRow(line) {
|
|
3893
|
+
const match = /^\s*(\S+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+(.*)$/.exec(line);
|
|
3894
|
+
if (match === null) return void 0;
|
|
3895
|
+
const user = match[1];
|
|
3896
|
+
const pid = match[2];
|
|
3897
|
+
const cpu = match[3];
|
|
3898
|
+
const mem = match[4];
|
|
3899
|
+
const command = match[6];
|
|
3900
|
+
return {
|
|
3901
|
+
pid: Number(pid),
|
|
3902
|
+
ppid: 0,
|
|
3903
|
+
user,
|
|
3904
|
+
cpuPercent: numberOr(cpu) ?? 0,
|
|
3905
|
+
memPercent: numberOr(mem) ?? 0,
|
|
3906
|
+
elapsedSeconds: null,
|
|
3907
|
+
command: trimCommand(command.trim())
|
|
3908
|
+
};
|
|
3909
|
+
}
|
|
3910
|
+
/**
|
|
3911
|
+
* Parse one probe. `exitCode === null` (channel died) or a missing end marker
|
|
3912
|
+
* means `unknown`, never an empty process table.
|
|
3913
|
+
*/
|
|
3914
|
+
function parseProcesses(stdout, exitCode) {
|
|
3915
|
+
const lines = stdout.split("\n").map((line) => line.endsWith("\r") ? line.slice(0, -1) : line);
|
|
3916
|
+
const markerAt = lines.findIndex((line) => line.trim() === `@@RVS-PROCS gnu` || line.trim() === `@@RVS-PROCS bsd` || line.trim() === `@@RVS-PROCS none`);
|
|
3917
|
+
if (exitCode === null || markerAt < 0) return {
|
|
3918
|
+
tool: "unknown",
|
|
3919
|
+
processes: [],
|
|
3920
|
+
truncated: false,
|
|
3921
|
+
error: "the remote process probe ended without a complete answer"
|
|
3922
|
+
};
|
|
3923
|
+
const marker = lines[markerAt]?.trim() ?? "";
|
|
3924
|
+
let endAt = -1;
|
|
3925
|
+
for (let index = lines.length - 1; index > markerAt; index -= 1) if (lines[index].trim() === `@@RVS-PROCS end`) {
|
|
3926
|
+
endAt = index;
|
|
3927
|
+
break;
|
|
3928
|
+
}
|
|
3929
|
+
if (endAt < 0) return {
|
|
3930
|
+
tool: "unknown",
|
|
3931
|
+
processes: [],
|
|
3932
|
+
truncated: false,
|
|
3933
|
+
error: "the remote process probe was interrupted"
|
|
3934
|
+
};
|
|
3935
|
+
if (marker.endsWith("none")) return {
|
|
3936
|
+
tool: "none",
|
|
3937
|
+
processes: [],
|
|
3938
|
+
truncated: false
|
|
3939
|
+
};
|
|
3940
|
+
const gnu = marker.endsWith("gnu");
|
|
3941
|
+
const parsedProcesses = [];
|
|
3942
|
+
for (const line of lines.slice(markerAt + 1, endAt)) {
|
|
3943
|
+
if (line.trim() === "") continue;
|
|
3944
|
+
const row = gnu ? parseGnuRow(line) : parseBsdRow(line);
|
|
3945
|
+
if (row !== void 0 && !row.command.includes("@@RVS-PROCS")) parsedProcesses.push(row);
|
|
3946
|
+
}
|
|
3947
|
+
if (parsedProcesses.length === 0) return {
|
|
3948
|
+
tool: "unknown",
|
|
3949
|
+
processes: [],
|
|
3950
|
+
truncated: false,
|
|
3951
|
+
error: "the remote ps output could not be parsed"
|
|
3952
|
+
};
|
|
3953
|
+
const truncated = parsedProcesses.length > 500;
|
|
3954
|
+
return {
|
|
3955
|
+
tool: gnu ? "gnu" : "bsd",
|
|
3956
|
+
processes: parsedProcesses.slice(0, 500),
|
|
3957
|
+
truncated
|
|
3958
|
+
};
|
|
3959
|
+
}
|
|
3960
|
+
//#endregion
|
|
3647
3961
|
//#region src/routes.ts
|
|
3648
|
-
/** One noServer WebSocket server for terminal upgrades (RFC 6455). */
|
|
3649
|
-
const terminalWss = new WebSocketServer({
|
|
3650
|
-
noServer: true,
|
|
3651
|
-
maxPayload: TERMINAL_MAX_FRAME_BYTES
|
|
3652
|
-
});
|
|
3653
3962
|
function queryParam(url, name) {
|
|
3654
3963
|
const value = url.searchParams.get(name);
|
|
3655
3964
|
return value === null ? void 0 : value;
|
|
3656
3965
|
}
|
|
3657
3966
|
/**
|
|
3967
|
+
* Host alias a request URL names (`?alias=`), or `null` when it names none.
|
|
3968
|
+
*
|
|
3969
|
+
* Only the routes know what a request is about; the response writer needs that
|
|
3970
|
+
* in order to attach only THIS host's notices (see `scopeHostNotices`).
|
|
3971
|
+
*/
|
|
3972
|
+
function urlAliasOf(req) {
|
|
3973
|
+
const alias = new URL(req.url ?? "/", "http://localhost").searchParams.get("alias");
|
|
3974
|
+
return alias === null || alias === "" ? null : alias;
|
|
3975
|
+
}
|
|
3976
|
+
/** Host alias a parsed JSON body names, or `null` when it names none. */
|
|
3977
|
+
function bodyAliasOf(value) {
|
|
3978
|
+
if (typeof value !== "object" || value === null) return null;
|
|
3979
|
+
const alias = value.alias;
|
|
3980
|
+
return typeof alias === "string" && alias !== "" ? alias : null;
|
|
3981
|
+
}
|
|
3982
|
+
/**
|
|
3658
3983
|
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
3659
3984
|
*
|
|
3660
3985
|
* `/files` used to throw plain Errors for missing fields and map EVERY failure
|
|
@@ -3704,6 +4029,10 @@ async function readBodyOrError(req, res, opts = {}) {
|
|
|
3704
4029
|
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3705
4030
|
return { ok: false };
|
|
3706
4031
|
}
|
|
4032
|
+
if (result.kind === "ok") {
|
|
4033
|
+
const alias = bodyAliasOf(result.value);
|
|
4034
|
+
if (alias !== null) scopeHostNotices(res, alias);
|
|
4035
|
+
}
|
|
3707
4036
|
return {
|
|
3708
4037
|
ok: true,
|
|
3709
4038
|
body: result.kind === "ok" ? result.value : null
|
|
@@ -3821,9 +4150,106 @@ function makeRoutes(deps) {
|
|
|
3821
4150
|
const files = new RemoteFiles(pool);
|
|
3822
4151
|
const terminalSessions = new TerminalSessionRegistry(pool);
|
|
3823
4152
|
const monitor = new HostMonitor(pool);
|
|
4153
|
+
/**
|
|
4154
|
+
* Last process snapshot per alias. The kill route validates against this
|
|
4155
|
+
* table, so a browser cannot construct a `kill` for an arbitrary PID; a stale
|
|
4156
|
+
* entry is refreshed before validation rather than trusted indefinitely.
|
|
4157
|
+
*/
|
|
4158
|
+
const processSnapshots = /* @__PURE__ */ new Map();
|
|
4159
|
+
/** One probe per alias at a time; a second reader joins the in-flight one. */
|
|
4160
|
+
const processInflight = /* @__PURE__ */ new Map();
|
|
4161
|
+
const forgetProcesses = (alias) => {
|
|
4162
|
+
processSnapshots.delete(alias);
|
|
4163
|
+
processInflight.delete(alias);
|
|
4164
|
+
};
|
|
4165
|
+
const readProcesses = async (alias, maxAgeMs) => {
|
|
4166
|
+
const cached = processSnapshots.get(alias);
|
|
4167
|
+
if (maxAgeMs > 0 && cached !== void 0 && Date.now() - cached.at < maxAgeMs) return cached.result;
|
|
4168
|
+
const joined = processInflight.get(alias);
|
|
4169
|
+
if (joined !== void 0) return joined;
|
|
4170
|
+
const task = (async () => {
|
|
4171
|
+
const result = await pool.exec(alias, processesCommand(), { timeoutMs: PROCESSES_TIMEOUT_MS });
|
|
4172
|
+
let parsed;
|
|
4173
|
+
if (result.error !== void 0) parsed = {
|
|
4174
|
+
tool: "unknown",
|
|
4175
|
+
processes: [],
|
|
4176
|
+
truncated: false,
|
|
4177
|
+
error: result.error
|
|
4178
|
+
};
|
|
4179
|
+
else if (result.timedOut) parsed = {
|
|
4180
|
+
tool: "unknown",
|
|
4181
|
+
processes: [],
|
|
4182
|
+
truncated: false,
|
|
4183
|
+
error: `the process probe did not finish within ${PROCESSES_TIMEOUT_MS / 1e3}s`
|
|
4184
|
+
};
|
|
4185
|
+
else if (result.exitCode === null) parsed = {
|
|
4186
|
+
tool: "unknown",
|
|
4187
|
+
processes: [],
|
|
4188
|
+
truncated: false,
|
|
4189
|
+
error: "the process probe ended without an exit status"
|
|
4190
|
+
};
|
|
4191
|
+
else parsed = parseProcesses(result.stdout, result.exitCode);
|
|
4192
|
+
if (parsed.tool === "gnu" || parsed.tool === "bsd") processSnapshots.set(alias, {
|
|
4193
|
+
at: Date.now(),
|
|
4194
|
+
result: parsed
|
|
4195
|
+
});
|
|
4196
|
+
else processSnapshots.delete(alias);
|
|
4197
|
+
return parsed;
|
|
4198
|
+
})();
|
|
4199
|
+
processInflight.set(alias, task);
|
|
4200
|
+
task.then(() => {
|
|
4201
|
+
if (processInflight.get(alias) === task) processInflight.delete(alias);
|
|
4202
|
+
}, () => {
|
|
4203
|
+
if (processInflight.get(alias) === task) processInflight.delete(alias);
|
|
4204
|
+
});
|
|
4205
|
+
return task;
|
|
4206
|
+
};
|
|
3824
4207
|
const forwards = deps.forwards;
|
|
3825
4208
|
const viewSettings = deps.viewSettings ?? new RemoteViewSettingsStore();
|
|
3826
4209
|
/**
|
|
4210
|
+
* One noServer WebSocket server for terminal upgrades (RFC 6455).
|
|
4211
|
+
*
|
|
4212
|
+
* `maxPayload` is the authoritative frame bound: without it ws buffers up to
|
|
4213
|
+
* its 100 MiB default before the handler ever sees the message, in the
|
|
4214
|
+
* process that also runs the agent. Terminal frames are small JSON control
|
|
4215
|
+
* messages.
|
|
4216
|
+
*
|
|
4217
|
+
* It belongs to the route set, not to the module: as a module-level singleton
|
|
4218
|
+
* a plugin unload (and every later reload with a fresh module instance) left
|
|
4219
|
+
* the old server object alive forever. It must NOT close per route rebuild —
|
|
4220
|
+
* live PTYs survive a settings edit, and ws answers an upgrade on a closed
|
|
4221
|
+
* server with 503 — so teardown is bound to the fiber (the unload boundary);
|
|
4222
|
+
* a later load builds a fresh instance.
|
|
4223
|
+
*/
|
|
4224
|
+
const terminalWss = new WebSocketServer({
|
|
4225
|
+
noServer: true,
|
|
4226
|
+
maxPayload: TERMINAL_MAX_FRAME_BYTES
|
|
4227
|
+
});
|
|
4228
|
+
/** Idempotent teardown of the terminal upgrade server (plugin unload). */
|
|
4229
|
+
const close = () => {
|
|
4230
|
+
try {
|
|
4231
|
+
terminalWss.close();
|
|
4232
|
+
} catch {}
|
|
4233
|
+
};
|
|
4234
|
+
if (typeof ctx.effect === "function") ctx.effect(() => close, "dsh-remote-vs: terminal websocket server");
|
|
4235
|
+
/**
|
|
4236
|
+
* A corrupt store is a service-side failure with a path the user can repair.
|
|
4237
|
+
*
|
|
4238
|
+
* `store.list()`/`listWorkspaces()` re-read the JSON file, and a bad file
|
|
4239
|
+
* threw straight out of the route: the host webserver answered a bare 400
|
|
4240
|
+
* with no body, so the GUI could only report "request failed" and the path
|
|
4241
|
+
* that needs fixing never reached the person who could fix it.
|
|
4242
|
+
*/
|
|
4243
|
+
const writeStoreError = (res, error) => {
|
|
4244
|
+
writeJson(res, 503, {
|
|
4245
|
+
code: "store-corrupt",
|
|
4246
|
+
error: error instanceof Error ? error.message : String(error),
|
|
4247
|
+
path: store.filePath
|
|
4248
|
+
});
|
|
4249
|
+
};
|
|
4250
|
+
/** Did the store's own JSON parse/validate fail? (`store.filePath` names it.) */
|
|
4251
|
+
const isStoreError = (error) => error instanceof Error && error.message.includes(store.filePath);
|
|
4252
|
+
/**
|
|
3827
4253
|
* Stream one remote `tar -czf -` command into a `.tar.gz` download response.
|
|
3828
4254
|
*
|
|
3829
4255
|
* Shared by the directory and multi-selection routes (they differ only in the
|
|
@@ -3922,60 +4348,26 @@ function makeRoutes(deps) {
|
|
|
3922
4348
|
skipped
|
|
3923
4349
|
};
|
|
3924
4350
|
};
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
4351
|
+
const routes = [
|
|
4352
|
+
{
|
|
4353
|
+
kind: "exact",
|
|
4354
|
+
path: REMOTE_API.hosts,
|
|
4355
|
+
handler: async (req, res) => {
|
|
4356
|
+
if (!isLoopbackRequest(req)) {
|
|
4357
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4358
|
+
return;
|
|
4359
|
+
}
|
|
4360
|
+
const method = req.method ?? "GET";
|
|
4361
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4362
|
+
if (method === "GET") {
|
|
4363
|
+
try {
|
|
3938
4364
|
writeJson(res, 200, { hosts: store.list().map((h) => store.summarize(h)) });
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
if (method === "POST") {
|
|
3942
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3943
|
-
if (!read.ok) return;
|
|
3944
|
-
const body = read.body;
|
|
3945
|
-
if (body === null) {
|
|
3946
|
-
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3947
|
-
return;
|
|
3948
|
-
}
|
|
3949
|
-
try {
|
|
3950
|
-
const entry = store.create(body);
|
|
3951
|
-
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3952
|
-
} catch (error) {
|
|
3953
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3954
|
-
}
|
|
3955
|
-
return;
|
|
3956
|
-
}
|
|
3957
|
-
if (method !== "PATCH" && method !== "DELETE") {
|
|
3958
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
3959
|
-
return;
|
|
3960
|
-
}
|
|
3961
|
-
const alias = queryParam(url, "alias");
|
|
3962
|
-
if (alias === void 0 || alias === "") {
|
|
3963
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
3964
|
-
return;
|
|
3965
|
-
}
|
|
3966
|
-
if (method === "DELETE") {
|
|
3967
|
-
forwards?.stopAlias(alias);
|
|
3968
|
-
pool.forget(alias);
|
|
3969
|
-
monitor.forget(alias);
|
|
3970
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3971
|
-
if (!store.remove(alias)) {
|
|
3972
|
-
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
3973
|
-
return;
|
|
3974
|
-
}
|
|
3975
|
-
worlds.syncWorkspaces().catch(() => void 0);
|
|
3976
|
-
writeJson(res, 200, { ok: true });
|
|
3977
|
-
return;
|
|
4365
|
+
} catch (error) {
|
|
4366
|
+
writeStoreError(res, error);
|
|
3978
4367
|
}
|
|
4368
|
+
return;
|
|
4369
|
+
}
|
|
4370
|
+
if (method === "POST") {
|
|
3979
4371
|
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3980
4372
|
if (!read.ok) return;
|
|
3981
4373
|
const body = read.body;
|
|
@@ -3984,498 +4376,579 @@ function makeRoutes(deps) {
|
|
|
3984
4376
|
return;
|
|
3985
4377
|
}
|
|
3986
4378
|
try {
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3990
|
-
const entry = store.update(alias, body);
|
|
3991
|
-
writeJson(res, 200, { host: store.summarize(entry) });
|
|
4379
|
+
const entry = store.create(body);
|
|
4380
|
+
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3992
4381
|
} catch (error) {
|
|
3993
|
-
writeJson(res,
|
|
4382
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3994
4383
|
}
|
|
4384
|
+
return;
|
|
3995
4385
|
}
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
path: REMOTE_API.status,
|
|
4000
|
-
handler: (req, res) => {
|
|
4001
|
-
if (!isLoopbackRequest(req)) {
|
|
4002
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4003
|
-
return;
|
|
4004
|
-
}
|
|
4005
|
-
if (req.method !== "GET") {
|
|
4006
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4007
|
-
return;
|
|
4008
|
-
}
|
|
4009
|
-
writeJson(res, 200, { connections: pool.status() });
|
|
4386
|
+
if (method !== "PATCH" && method !== "DELETE") {
|
|
4387
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4388
|
+
return;
|
|
4010
4389
|
}
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
handler: async (req, res) => {
|
|
4016
|
-
if (!isLoopbackRequest(req)) {
|
|
4017
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4018
|
-
return;
|
|
4019
|
-
}
|
|
4020
|
-
if (req.method !== "POST") {
|
|
4021
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4022
|
-
return;
|
|
4023
|
-
}
|
|
4024
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4025
|
-
if (!read.ok) return;
|
|
4026
|
-
const body = read.body;
|
|
4027
|
-
if (body === null || typeof body.alias !== "string") {
|
|
4028
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
4029
|
-
return;
|
|
4030
|
-
}
|
|
4031
|
-
writeJson(res, 200, await pool.test(body.alias));
|
|
4390
|
+
const alias = queryParam(url, "alias");
|
|
4391
|
+
if (alias === void 0 || alias === "") {
|
|
4392
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4393
|
+
return;
|
|
4032
4394
|
}
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
}
|
|
4042
|
-
if (req.method !== "POST") {
|
|
4043
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4044
|
-
return;
|
|
4045
|
-
}
|
|
4046
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4047
|
-
if (!read.ok) return;
|
|
4048
|
-
const body = read.body;
|
|
4049
|
-
if (body === null || typeof body.alias !== "string") {
|
|
4050
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
4395
|
+
if (method === "DELETE") {
|
|
4396
|
+
forwards?.stopAlias(alias);
|
|
4397
|
+
pool.forget(alias);
|
|
4398
|
+
monitor.forget(alias);
|
|
4399
|
+
forgetProcesses(alias);
|
|
4400
|
+
worlds.dropHost(alias).catch(() => void 0);
|
|
4401
|
+
if (!store.remove(alias)) {
|
|
4402
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
4051
4403
|
return;
|
|
4052
4404
|
}
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
4057
|
-
writeJson(res, message.startsWith("unknown host alias") ? 404 : 502, { error: message });
|
|
4058
|
-
}
|
|
4405
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
4406
|
+
writeJson(res, 200, { ok: true });
|
|
4407
|
+
return;
|
|
4059
4408
|
}
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4067
|
-
return;
|
|
4068
|
-
}
|
|
4069
|
-
if (req.method !== "POST") {
|
|
4070
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4071
|
-
return;
|
|
4072
|
-
}
|
|
4073
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4074
|
-
if (!read.ok) return;
|
|
4075
|
-
const body = read.body;
|
|
4076
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4077
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4078
|
-
return;
|
|
4079
|
-
}
|
|
4080
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4081
|
-
writeJson(res, 200, await pool.exec(body.alias, body.command, timeoutMs === void 0 ? {} : { timeoutMs }));
|
|
4409
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4410
|
+
if (!read.ok) return;
|
|
4411
|
+
const body = read.body;
|
|
4412
|
+
if (body === null) {
|
|
4413
|
+
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4414
|
+
return;
|
|
4082
4415
|
}
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
}
|
|
4092
|
-
if (req.method !== "POST") {
|
|
4093
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4094
|
-
return;
|
|
4095
|
-
}
|
|
4096
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4097
|
-
if (!read.ok) return;
|
|
4098
|
-
const body = read.body;
|
|
4099
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4100
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4101
|
-
return;
|
|
4102
|
-
}
|
|
4103
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4104
|
-
res.writeHead(200, {
|
|
4105
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
4106
|
-
"Cache-Control": "no-cache, no-transform",
|
|
4107
|
-
Connection: "keep-alive",
|
|
4108
|
-
"X-Accel-Buffering": "no"
|
|
4109
|
-
});
|
|
4110
|
-
res.flushHeaders();
|
|
4111
|
-
/** No more events to the client (finished, or the socket broke). */
|
|
4112
|
-
let outputClosed = false;
|
|
4113
|
-
/** `res.end` already called. */
|
|
4114
|
-
let ended = false;
|
|
4115
|
-
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4116
|
-
let cancelRequested = false;
|
|
4117
|
-
/** The cancel handle was invoked (only once). */
|
|
4118
|
-
let cancelSent = false;
|
|
4119
|
-
let cancel;
|
|
4120
|
-
/** Server-side forward cap: the client shows a scrolling tail anyway. */
|
|
4121
|
-
let sent = 0;
|
|
4122
|
-
const FORWARD_CAP = 4 * 1024 * 1024;
|
|
4123
|
-
/**
|
|
4124
|
-
* Stop the remote process group exactly once.
|
|
4125
|
-
*
|
|
4126
|
-
* The disconnect may happen while `pool.exec` is still acquiring the
|
|
4127
|
-
* channel (slot queue, setsid probe, connect) — the handle does not exist
|
|
4128
|
-
* yet then, so the request is latched and fired the moment `onCancel`
|
|
4129
|
-
* arrives; otherwise a push would finish unseen and uncancellable.
|
|
4130
|
-
*/
|
|
4131
|
-
const abortRemote = () => {
|
|
4132
|
-
cancelRequested = true;
|
|
4133
|
-
if (cancel === void 0 || cancelSent) return;
|
|
4134
|
-
cancelSent = true;
|
|
4135
|
-
cancel();
|
|
4136
|
-
};
|
|
4137
|
-
const send = (event) => {
|
|
4138
|
-
if (outputClosed) return;
|
|
4139
|
-
try {
|
|
4140
|
-
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
4141
|
-
} catch {
|
|
4142
|
-
outputClosed = true;
|
|
4143
|
-
abortRemote();
|
|
4144
|
-
}
|
|
4145
|
-
};
|
|
4146
|
-
const heartbeat = setInterval(() => {
|
|
4147
|
-
if (outputClosed) return;
|
|
4148
|
-
try {
|
|
4149
|
-
res.write(": ping\n\n");
|
|
4150
|
-
} catch {
|
|
4151
|
-
outputClosed = true;
|
|
4152
|
-
abortRemote();
|
|
4153
|
-
}
|
|
4154
|
-
}, 15e3);
|
|
4155
|
-
heartbeat.unref?.();
|
|
4156
|
-
const stop = () => {
|
|
4157
|
-
if (ended) return;
|
|
4158
|
-
ended = true;
|
|
4159
|
-
outputClosed = true;
|
|
4160
|
-
clearInterval(heartbeat);
|
|
4161
|
-
try {
|
|
4162
|
-
res.end();
|
|
4163
|
-
} catch {}
|
|
4164
|
-
};
|
|
4165
|
-
res.on("close", () => {
|
|
4166
|
-
abortRemote();
|
|
4167
|
-
stop();
|
|
4168
|
-
});
|
|
4169
|
-
try {
|
|
4170
|
-
const result = await pool.exec(body.alias, body.command, {
|
|
4171
|
-
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4172
|
-
onData: (stream, text) => {
|
|
4173
|
-
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
4174
|
-
sent += Buffer.byteLength(text, "utf8");
|
|
4175
|
-
send({
|
|
4176
|
-
t: stream,
|
|
4177
|
-
d: text
|
|
4178
|
-
});
|
|
4179
|
-
},
|
|
4180
|
-
onCancel: (handle) => {
|
|
4181
|
-
cancel = handle;
|
|
4182
|
-
if (cancelRequested && !cancelSent) {
|
|
4183
|
-
cancelSent = true;
|
|
4184
|
-
handle();
|
|
4185
|
-
}
|
|
4186
|
-
}
|
|
4187
|
-
});
|
|
4188
|
-
if (outputClosed) return;
|
|
4189
|
-
send({
|
|
4190
|
-
t: "exit",
|
|
4191
|
-
success: result.success,
|
|
4192
|
-
exitCode: result.exitCode,
|
|
4193
|
-
timedOut: result.timedOut,
|
|
4194
|
-
durationMs: result.durationMs,
|
|
4195
|
-
...result.error === void 0 ? {} : { error: result.error }
|
|
4196
|
-
});
|
|
4197
|
-
} catch (error) {
|
|
4198
|
-
send({
|
|
4199
|
-
t: "exit",
|
|
4200
|
-
success: false,
|
|
4201
|
-
exitCode: null,
|
|
4202
|
-
timedOut: false,
|
|
4203
|
-
durationMs: 0,
|
|
4204
|
-
error: error instanceof Error ? error.message : String(error)
|
|
4205
|
-
});
|
|
4206
|
-
} finally {
|
|
4207
|
-
stop();
|
|
4208
|
-
}
|
|
4416
|
+
try {
|
|
4417
|
+
pool.forget(alias);
|
|
4418
|
+
monitor.forget(alias);
|
|
4419
|
+
forgetProcesses(alias);
|
|
4420
|
+
worlds.dropHost(alias).catch(() => void 0);
|
|
4421
|
+
const entry = store.update(alias, body);
|
|
4422
|
+
writeJson(res, 200, { host: store.summarize(entry) });
|
|
4423
|
+
} catch (error) {
|
|
4424
|
+
writeJson(res, (error instanceof Error ? error.message : "").startsWith("unknown host alias") ? 404 : 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4209
4425
|
}
|
|
4210
|
-
}
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
}
|
|
4237
|
-
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4272
|
-
|
|
4273
|
-
|
|
4274
|
-
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4426
|
+
}
|
|
4427
|
+
},
|
|
4428
|
+
{
|
|
4429
|
+
kind: "exact",
|
|
4430
|
+
path: REMOTE_API.status,
|
|
4431
|
+
handler: (req, res) => {
|
|
4432
|
+
if (!isLoopbackRequest(req)) {
|
|
4433
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4434
|
+
return;
|
|
4435
|
+
}
|
|
4436
|
+
if (req.method !== "GET") {
|
|
4437
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4438
|
+
return;
|
|
4439
|
+
}
|
|
4440
|
+
writeJson(res, 200, { connections: pool.status() });
|
|
4441
|
+
}
|
|
4442
|
+
},
|
|
4443
|
+
{
|
|
4444
|
+
kind: "exact",
|
|
4445
|
+
path: REMOTE_API.test,
|
|
4446
|
+
handler: async (req, res) => {
|
|
4447
|
+
if (!isLoopbackRequest(req)) {
|
|
4448
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4449
|
+
return;
|
|
4450
|
+
}
|
|
4451
|
+
if (req.method !== "POST") {
|
|
4452
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4453
|
+
return;
|
|
4454
|
+
}
|
|
4455
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4456
|
+
if (!read.ok) return;
|
|
4457
|
+
const body = read.body;
|
|
4458
|
+
if (body === null || typeof body.alias !== "string") {
|
|
4459
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4460
|
+
return;
|
|
4461
|
+
}
|
|
4462
|
+
writeJson(res, 200, await pool.test(body.alias));
|
|
4463
|
+
}
|
|
4464
|
+
},
|
|
4465
|
+
{
|
|
4466
|
+
kind: "exact",
|
|
4467
|
+
path: REMOTE_API.keygen,
|
|
4468
|
+
handler: async (req, res) => {
|
|
4469
|
+
if (!isLoopbackRequest(req)) {
|
|
4470
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4471
|
+
return;
|
|
4472
|
+
}
|
|
4473
|
+
if (req.method !== "POST") {
|
|
4474
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4475
|
+
return;
|
|
4476
|
+
}
|
|
4477
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4478
|
+
if (!read.ok) return;
|
|
4479
|
+
const body = read.body;
|
|
4480
|
+
if (body === null || typeof body.alias !== "string") {
|
|
4481
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4482
|
+
return;
|
|
4483
|
+
}
|
|
4484
|
+
try {
|
|
4485
|
+
writeJson(res, 200, await bootstrap(pool, store, body.alias));
|
|
4486
|
+
} catch (error) {
|
|
4487
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4488
|
+
writeJson(res, message.startsWith("unknown host alias") ? 404 : 502, { error: message });
|
|
4489
|
+
}
|
|
4490
|
+
}
|
|
4491
|
+
},
|
|
4492
|
+
{
|
|
4493
|
+
kind: "exact",
|
|
4494
|
+
path: REMOTE_API.exec,
|
|
4495
|
+
handler: async (req, res) => {
|
|
4496
|
+
if (!isLoopbackRequest(req)) {
|
|
4497
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4498
|
+
return;
|
|
4499
|
+
}
|
|
4500
|
+
if (req.method !== "POST") {
|
|
4501
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4502
|
+
return;
|
|
4503
|
+
}
|
|
4504
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4505
|
+
if (!read.ok) return;
|
|
4506
|
+
const body = read.body;
|
|
4507
|
+
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4508
|
+
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4509
|
+
return;
|
|
4510
|
+
}
|
|
4511
|
+
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4512
|
+
/** The client closed the request: its response is never read. */
|
|
4513
|
+
let clientGone = false;
|
|
4514
|
+
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4515
|
+
let cancelRequested = false;
|
|
4516
|
+
/** The cancel handle was invoked (only once). */
|
|
4517
|
+
let cancelSent = false;
|
|
4518
|
+
let cancel;
|
|
4519
|
+
/**
|
|
4520
|
+
* Stop the remote process group exactly once.
|
|
4521
|
+
*
|
|
4522
|
+
* Same latch as the streaming route: the disconnect may land while
|
|
4523
|
+
* `pool.exec` is still acquiring the channel (slot queue, setsid probe,
|
|
4524
|
+
* connect), where no handle exists yet — the request is remembered and
|
|
4525
|
+
* fired the moment `onCancel` supplies one. Without this the command kept
|
|
4526
|
+
* running to its timeout after a page refresh, holding a channel slot and
|
|
4527
|
+
* remote CPU for nobody.
|
|
4528
|
+
*/
|
|
4529
|
+
const abortRemote = () => {
|
|
4530
|
+
cancelRequested = true;
|
|
4531
|
+
if (cancel === void 0 || cancelSent) return;
|
|
4532
|
+
cancelSent = true;
|
|
4533
|
+
cancel();
|
|
4534
|
+
};
|
|
4535
|
+
res.on("close", () => {
|
|
4536
|
+
clientGone = true;
|
|
4537
|
+
abortRemote();
|
|
4538
|
+
});
|
|
4539
|
+
try {
|
|
4540
|
+
const result = await pool.exec(body.alias, body.command, {
|
|
4541
|
+
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4542
|
+
onCancel: (handle) => {
|
|
4543
|
+
cancel = handle;
|
|
4544
|
+
if (cancelRequested && !cancelSent) {
|
|
4545
|
+
cancelSent = true;
|
|
4546
|
+
handle();
|
|
4280
4547
|
}
|
|
4281
|
-
case "mkdir":
|
|
4282
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4283
|
-
await files.mkdir(alias, body.path);
|
|
4284
|
-
writeJson(res, 200, { ok: true });
|
|
4285
|
-
return;
|
|
4286
|
-
case "rm":
|
|
4287
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4288
|
-
await files.remove(alias, body.path, body.recursive === true);
|
|
4289
|
-
writeJson(res, 200, { ok: true });
|
|
4290
|
-
return;
|
|
4291
|
-
case "rename":
|
|
4292
|
-
if (typeof body.from !== "string" || typeof body.to !== "string") throw new BadRequestError("from and to are required");
|
|
4293
|
-
await files.rename(alias, body.from, body.to);
|
|
4294
|
-
writeJson(res, 200, { ok: true });
|
|
4295
|
-
return;
|
|
4296
|
-
default: writeJson(res, 400, { error: `unknown op: ${body.op}` });
|
|
4297
4548
|
}
|
|
4298
|
-
}
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
4303
|
-
* client needs the current version to offer overwrite / reload / see
|
|
4304
|
-
* the difference. Collapsing this into the generic remote-error path
|
|
4305
|
-
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
4306
|
-
*/
|
|
4307
|
-
if (error instanceof WriteConflictError) {
|
|
4308
|
-
writeJson(res, 409, {
|
|
4309
|
-
error: error.message,
|
|
4310
|
-
code: "conflict",
|
|
4311
|
-
conflict: error.conflict
|
|
4312
|
-
});
|
|
4313
|
-
return;
|
|
4314
|
-
}
|
|
4315
|
-
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4316
|
-
}
|
|
4549
|
+
});
|
|
4550
|
+
if (!clientGone) writeJson(res, 200, result);
|
|
4551
|
+
} catch (error) {
|
|
4552
|
+
if (!clientGone) writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4317
4553
|
}
|
|
4318
|
-
}
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4554
|
+
}
|
|
4555
|
+
},
|
|
4556
|
+
{
|
|
4557
|
+
kind: "exact",
|
|
4558
|
+
path: REMOTE_API.execStream,
|
|
4559
|
+
handler: async (req, res) => {
|
|
4560
|
+
if (!isLoopbackRequest(req)) {
|
|
4561
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4562
|
+
return;
|
|
4563
|
+
}
|
|
4564
|
+
if (req.method !== "POST") {
|
|
4565
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4566
|
+
return;
|
|
4567
|
+
}
|
|
4568
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4569
|
+
if (!read.ok) return;
|
|
4570
|
+
const body = read.body;
|
|
4571
|
+
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4572
|
+
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4573
|
+
return;
|
|
4574
|
+
}
|
|
4575
|
+
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4576
|
+
res.writeHead(200, {
|
|
4577
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
4578
|
+
"Cache-Control": "no-cache, no-transform",
|
|
4579
|
+
Connection: "keep-alive",
|
|
4580
|
+
"X-Accel-Buffering": "no"
|
|
4581
|
+
});
|
|
4582
|
+
res.flushHeaders();
|
|
4583
|
+
/** No more events to the client (finished, or the socket broke). */
|
|
4584
|
+
let outputClosed = false;
|
|
4585
|
+
/** `res.end` already called. */
|
|
4586
|
+
let ended = false;
|
|
4587
|
+
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4588
|
+
let cancelRequested = false;
|
|
4589
|
+
/** The cancel handle was invoked (only once). */
|
|
4590
|
+
let cancelSent = false;
|
|
4591
|
+
let cancel;
|
|
4592
|
+
/** Server-side forward cap: the client shows a scrolling tail anyway. */
|
|
4593
|
+
let sent = 0;
|
|
4594
|
+
const FORWARD_CAP = 4 * 1024 * 1024;
|
|
4595
|
+
/**
|
|
4596
|
+
* Stop the remote process group exactly once.
|
|
4597
|
+
*
|
|
4598
|
+
* The disconnect may happen while `pool.exec` is still acquiring the
|
|
4599
|
+
* channel (slot queue, setsid probe, connect) — the handle does not exist
|
|
4600
|
+
* yet then, so the request is latched and fired the moment `onCancel`
|
|
4601
|
+
* arrives; otherwise a push would finish unseen and uncancellable.
|
|
4602
|
+
*/
|
|
4603
|
+
const abortRemote = () => {
|
|
4604
|
+
cancelRequested = true;
|
|
4605
|
+
if (cancel === void 0 || cancelSent) return;
|
|
4606
|
+
cancelSent = true;
|
|
4607
|
+
cancel();
|
|
4608
|
+
};
|
|
4609
|
+
const send = (event) => {
|
|
4610
|
+
if (outputClosed) return;
|
|
4611
|
+
try {
|
|
4612
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
4613
|
+
} catch {
|
|
4614
|
+
outputClosed = true;
|
|
4615
|
+
abortRemote();
|
|
4338
4616
|
}
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4617
|
+
};
|
|
4618
|
+
const heartbeat = setInterval(() => {
|
|
4619
|
+
if (outputClosed) return;
|
|
4620
|
+
try {
|
|
4621
|
+
res.write(": ping\n\n");
|
|
4622
|
+
} catch {
|
|
4623
|
+
outputClosed = true;
|
|
4624
|
+
abortRemote();
|
|
4342
4625
|
}
|
|
4626
|
+
}, 15e3);
|
|
4627
|
+
heartbeat.unref?.();
|
|
4628
|
+
const stop = () => {
|
|
4629
|
+
if (ended) return;
|
|
4630
|
+
ended = true;
|
|
4631
|
+
outputClosed = true;
|
|
4632
|
+
clearInterval(heartbeat);
|
|
4343
4633
|
try {
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4634
|
+
res.end();
|
|
4635
|
+
} catch {}
|
|
4636
|
+
};
|
|
4637
|
+
res.on("close", () => {
|
|
4638
|
+
abortRemote();
|
|
4639
|
+
stop();
|
|
4640
|
+
});
|
|
4641
|
+
try {
|
|
4642
|
+
const result = await pool.exec(body.alias, body.command, {
|
|
4643
|
+
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4644
|
+
onData: (stream, text) => {
|
|
4645
|
+
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
4646
|
+
sent += Buffer.byteLength(text, "utf8");
|
|
4647
|
+
send({
|
|
4648
|
+
t: stream,
|
|
4649
|
+
d: text
|
|
4650
|
+
});
|
|
4651
|
+
},
|
|
4652
|
+
onCancel: (handle) => {
|
|
4653
|
+
cancel = handle;
|
|
4654
|
+
if (cancelRequested && !cancelSent) {
|
|
4655
|
+
cancelSent = true;
|
|
4656
|
+
handle();
|
|
4351
4657
|
}
|
|
4352
4658
|
}
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4659
|
+
});
|
|
4660
|
+
if (outputClosed) return;
|
|
4661
|
+
send({
|
|
4662
|
+
t: "exit",
|
|
4663
|
+
success: result.success,
|
|
4664
|
+
exitCode: result.exitCode,
|
|
4665
|
+
timedOut: result.timedOut,
|
|
4666
|
+
durationMs: result.durationMs,
|
|
4667
|
+
...result.error === void 0 ? {} : { error: result.error }
|
|
4668
|
+
});
|
|
4669
|
+
} catch (error) {
|
|
4670
|
+
send({
|
|
4671
|
+
t: "exit",
|
|
4672
|
+
success: false,
|
|
4673
|
+
exitCode: null,
|
|
4674
|
+
timedOut: false,
|
|
4675
|
+
durationMs: 0,
|
|
4676
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4677
|
+
});
|
|
4678
|
+
} finally {
|
|
4679
|
+
stop();
|
|
4360
4680
|
}
|
|
4361
|
-
}
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
if (req.method !== "GET") {
|
|
4371
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4372
|
-
return;
|
|
4373
|
-
}
|
|
4374
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4375
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4376
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4377
|
-
if (alias === "" || path === "") {
|
|
4378
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4379
|
-
return;
|
|
4380
|
-
}
|
|
4381
|
-
if (!path.startsWith("/")) {
|
|
4382
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4383
|
-
return;
|
|
4384
|
-
}
|
|
4385
|
-
try {
|
|
4386
|
-
await files.download(alias, path, res);
|
|
4387
|
-
} catch (error) {
|
|
4388
|
-
if (!res.headersSent && !res.destroyed) writeRouteError(res, error);
|
|
4389
|
-
else res.destroy(error instanceof Error ? error : void 0);
|
|
4390
|
-
}
|
|
4681
|
+
}
|
|
4682
|
+
},
|
|
4683
|
+
{
|
|
4684
|
+
kind: "exact",
|
|
4685
|
+
path: REMOTE_API.files,
|
|
4686
|
+
handler: async (req, res) => {
|
|
4687
|
+
if (!isLoopbackRequest(req)) {
|
|
4688
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4689
|
+
return;
|
|
4391
4690
|
}
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4691
|
+
if (req.method !== "POST") {
|
|
4692
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4693
|
+
return;
|
|
4694
|
+
}
|
|
4695
|
+
if (bodyTooLarge(req, FILES_BODY_LIMIT)) {
|
|
4696
|
+
writePayloadTooLarge(req, res, FILES_BODY_LIMIT);
|
|
4697
|
+
return;
|
|
4698
|
+
}
|
|
4699
|
+
const read = await readBodyOrError(req, res, {
|
|
4700
|
+
objectOnly: true,
|
|
4701
|
+
maxBytes: FILES_BODY_LIMIT
|
|
4702
|
+
});
|
|
4703
|
+
if (!read.ok) return;
|
|
4704
|
+
const body = read.body;
|
|
4705
|
+
if (body === null || typeof body.op !== "string" || typeof body.alias !== "string") {
|
|
4706
|
+
writeJson(res, 400, { error: "op and alias are required" });
|
|
4707
|
+
return;
|
|
4708
|
+
}
|
|
4709
|
+
const alias = body.alias;
|
|
4710
|
+
try {
|
|
4711
|
+
switch (body.op) {
|
|
4712
|
+
case "list":
|
|
4713
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4714
|
+
writeJson(res, 200, { entries: await files.list(alias, body.path) });
|
|
4715
|
+
return;
|
|
4716
|
+
case "stat":
|
|
4717
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4718
|
+
writeJson(res, 200, { entry: await files.stat(alias, body.path) });
|
|
4719
|
+
return;
|
|
4720
|
+
case "read":
|
|
4721
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4722
|
+
writeJson(res, 200, await files.read(alias, body.path));
|
|
4723
|
+
return;
|
|
4724
|
+
case "write": {
|
|
4725
|
+
if (typeof body.path !== "string" || typeof body.content !== "string") throw new BadRequestError("path and content are required");
|
|
4726
|
+
const encoding = body.encoding === "base64" ? "base64" : "utf8";
|
|
4727
|
+
/**
|
|
4728
|
+
* Optional optimistic lock. `expectMtime` is the version the client
|
|
4729
|
+
* loaded; when present the Host refuses a file that changed since.
|
|
4730
|
+
* Requests without it (helpers, deploy, the agent's write tool)
|
|
4731
|
+
* keep the old unconditional semantics — the guard protects a
|
|
4732
|
+
* human's buffer, it must not make programmatic writes fail.
|
|
4733
|
+
*/
|
|
4734
|
+
const expectMtime = body.expectMtime;
|
|
4735
|
+
const expectSize = body.expectSize;
|
|
4736
|
+
/**
|
|
4737
|
+
* A malformed baseline is a CALLER mistake, not "no baseline".
|
|
4738
|
+
*
|
|
4739
|
+
* Silently dropping it would turn `{expectMtime: "123"}` (a client
|
|
4740
|
+
* bug, a serializer change) into an UNGUARDED write — the exact
|
|
4741
|
+
* overwrite the caller was asking to be protected from, reported as
|
|
4742
|
+
* a success.
|
|
4743
|
+
*/
|
|
4744
|
+
if (expectMtime !== void 0 && (typeof expectMtime !== "number" || !Number.isFinite(expectMtime))) throw new BadRequestError("expectMtime must be a finite number");
|
|
4745
|
+
if (expectSize !== void 0 && (typeof expectSize !== "number" || !Number.isFinite(expectSize))) throw new BadRequestError("expectSize must be a finite number");
|
|
4746
|
+
const guard = typeof expectMtime === "number" ? {
|
|
4747
|
+
expectMtime,
|
|
4748
|
+
...typeof expectSize === "number" ? { expectSize } : {}
|
|
4749
|
+
} : void 0;
|
|
4750
|
+
writeJson(res, 200, await files.write(alias, body.path, body.content, encoding, guard));
|
|
4751
|
+
return;
|
|
4752
|
+
}
|
|
4753
|
+
case "mkdir":
|
|
4754
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4755
|
+
await files.mkdir(alias, body.path);
|
|
4756
|
+
writeJson(res, 200, { ok: true });
|
|
4757
|
+
return;
|
|
4758
|
+
case "rm":
|
|
4759
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4760
|
+
await files.remove(alias, body.path, body.recursive === true);
|
|
4761
|
+
writeJson(res, 200, { ok: true });
|
|
4762
|
+
return;
|
|
4763
|
+
case "rename":
|
|
4764
|
+
if (typeof body.from !== "string" || typeof body.to !== "string") throw new BadRequestError("from and to are required");
|
|
4765
|
+
await files.rename(alias, body.from, body.to);
|
|
4766
|
+
writeJson(res, 200, { ok: true });
|
|
4767
|
+
return;
|
|
4768
|
+
default: writeJson(res, 400, { error: `unknown op: ${body.op}` });
|
|
4411
4769
|
}
|
|
4412
|
-
|
|
4413
|
-
|
|
4770
|
+
} catch (error) {
|
|
4771
|
+
/**
|
|
4772
|
+
* A save the remote side refused because the file moved on.
|
|
4773
|
+
*
|
|
4774
|
+
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
4775
|
+
* client needs the current version to offer overwrite / reload / see
|
|
4776
|
+
* the difference. Collapsing this into the generic remote-error path
|
|
4777
|
+
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
4778
|
+
*/
|
|
4779
|
+
if (error instanceof WriteConflictError) {
|
|
4780
|
+
writeJson(res, 409, {
|
|
4781
|
+
error: error.message,
|
|
4782
|
+
code: "conflict",
|
|
4783
|
+
conflict: error.conflict
|
|
4784
|
+
});
|
|
4414
4785
|
return;
|
|
4415
4786
|
}
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4787
|
+
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4788
|
+
}
|
|
4789
|
+
}
|
|
4790
|
+
},
|
|
4791
|
+
{
|
|
4792
|
+
kind: "exact",
|
|
4793
|
+
path: REMOTE_API.upload,
|
|
4794
|
+
handler: async (req, res) => {
|
|
4795
|
+
if (!isLoopbackRequest(req)) {
|
|
4796
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4797
|
+
return;
|
|
4798
|
+
}
|
|
4799
|
+
if (req.method !== "POST") {
|
|
4800
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4801
|
+
return;
|
|
4802
|
+
}
|
|
4803
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4804
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4805
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4806
|
+
const overwrite = url.searchParams.get("overwrite") === "1";
|
|
4807
|
+
if (alias === "" || path === "") {
|
|
4808
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4809
|
+
return;
|
|
4810
|
+
}
|
|
4811
|
+
if (!path.startsWith("/")) {
|
|
4812
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4813
|
+
return;
|
|
4814
|
+
}
|
|
4815
|
+
try {
|
|
4816
|
+
if (!overwrite) {
|
|
4817
|
+
if (await files.stat(alias, path).then(() => true, () => false)) {
|
|
4818
|
+
writeJson(res, 409, {
|
|
4819
|
+
error: `${path} already exists`,
|
|
4820
|
+
exists: true
|
|
4821
|
+
});
|
|
4420
4822
|
return;
|
|
4421
4823
|
}
|
|
4422
|
-
command = directoryArchiveCommand(path);
|
|
4423
|
-
} catch (error) {
|
|
4424
|
-
writeRouteError(res, error);
|
|
4425
|
-
return;
|
|
4426
4824
|
}
|
|
4427
|
-
|
|
4825
|
+
writeJson(res, 200, {
|
|
4826
|
+
ok: true,
|
|
4827
|
+
size: (await files.upload(alias, path, req, void 0, overwrite)).size
|
|
4828
|
+
});
|
|
4829
|
+
} catch (error) {
|
|
4830
|
+
writeRouteError(res, error);
|
|
4428
4831
|
}
|
|
4429
|
-
}
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
if (req.method !== "GET") {
|
|
4439
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4440
|
-
return;
|
|
4441
|
-
}
|
|
4442
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4443
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4444
|
-
const paths = url.searchParams.getAll("path");
|
|
4445
|
-
if (alias === "" || paths.length === 0) {
|
|
4446
|
-
writeJson(res, 400, { error: "alias and at least one path query parameter are required" });
|
|
4447
|
-
return;
|
|
4448
|
-
}
|
|
4449
|
-
if (paths.length > 100) {
|
|
4450
|
-
writeJson(res, 400, { error: `too many entries: ${paths.length} (max 100)` });
|
|
4451
|
-
return;
|
|
4452
|
-
}
|
|
4453
|
-
for (const path of paths) if (!path.startsWith("/")) {
|
|
4454
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4455
|
-
return;
|
|
4456
|
-
}
|
|
4457
|
-
let command;
|
|
4458
|
-
try {
|
|
4459
|
-
for (const path of paths) await files.stat(alias, path);
|
|
4460
|
-
command = selectionArchiveCommand(paths);
|
|
4461
|
-
} catch (error) {
|
|
4462
|
-
writeRouteError(res, error);
|
|
4463
|
-
return;
|
|
4464
|
-
}
|
|
4465
|
-
await streamArchive(alias, command, res, selectionDownloadName(paths.length));
|
|
4832
|
+
}
|
|
4833
|
+
},
|
|
4834
|
+
{
|
|
4835
|
+
kind: "exact",
|
|
4836
|
+
path: REMOTE_API.download,
|
|
4837
|
+
handler: async (req, res) => {
|
|
4838
|
+
if (!isLoopbackRequest(req)) {
|
|
4839
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4840
|
+
return;
|
|
4466
4841
|
}
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4842
|
+
if (req.method !== "GET") {
|
|
4843
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4844
|
+
return;
|
|
4845
|
+
}
|
|
4846
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4847
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4848
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4849
|
+
if (alias === "" || path === "") {
|
|
4850
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4851
|
+
return;
|
|
4852
|
+
}
|
|
4853
|
+
if (!path.startsWith("/")) {
|
|
4854
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4855
|
+
return;
|
|
4856
|
+
}
|
|
4857
|
+
try {
|
|
4858
|
+
await files.download(alias, path, res);
|
|
4859
|
+
} catch (error) {
|
|
4860
|
+
if (!res.headersSent && !res.destroyed) writeRouteError(res, error);
|
|
4861
|
+
else res.destroy(error instanceof Error ? error : void 0);
|
|
4862
|
+
}
|
|
4863
|
+
}
|
|
4864
|
+
},
|
|
4865
|
+
{
|
|
4866
|
+
kind: "exact",
|
|
4867
|
+
path: REMOTE_API.downloadDir,
|
|
4868
|
+
handler: async (req, res) => {
|
|
4869
|
+
if (!isLoopbackRequest(req)) {
|
|
4870
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4871
|
+
return;
|
|
4872
|
+
}
|
|
4873
|
+
if (req.method !== "GET") {
|
|
4874
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4875
|
+
return;
|
|
4876
|
+
}
|
|
4877
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4878
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4879
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4880
|
+
if (alias === "" || path === "") {
|
|
4881
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4882
|
+
return;
|
|
4883
|
+
}
|
|
4884
|
+
if (!path.startsWith("/")) {
|
|
4885
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4886
|
+
return;
|
|
4887
|
+
}
|
|
4888
|
+
let command;
|
|
4889
|
+
try {
|
|
4890
|
+
if (!(await files.stat(alias, path)).isDir) {
|
|
4891
|
+
writeJson(res, 400, { error: `not a directory: ${path}` });
|
|
4474
4892
|
return;
|
|
4475
4893
|
}
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4894
|
+
command = directoryArchiveCommand(path);
|
|
4895
|
+
} catch (error) {
|
|
4896
|
+
writeRouteError(res, error);
|
|
4897
|
+
return;
|
|
4898
|
+
}
|
|
4899
|
+
await streamArchive(alias, command, res, directoryArchiveName(path));
|
|
4900
|
+
}
|
|
4901
|
+
},
|
|
4902
|
+
{
|
|
4903
|
+
kind: "exact",
|
|
4904
|
+
path: REMOTE_API.downloadMany,
|
|
4905
|
+
handler: async (req, res) => {
|
|
4906
|
+
if (!isLoopbackRequest(req)) {
|
|
4907
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4908
|
+
return;
|
|
4909
|
+
}
|
|
4910
|
+
if (req.method !== "GET") {
|
|
4911
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4912
|
+
return;
|
|
4913
|
+
}
|
|
4914
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4915
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4916
|
+
const paths = url.searchParams.getAll("path");
|
|
4917
|
+
if (alias === "" || paths.length === 0) {
|
|
4918
|
+
writeJson(res, 400, { error: "alias and at least one path query parameter are required" });
|
|
4919
|
+
return;
|
|
4920
|
+
}
|
|
4921
|
+
if (paths.length > 100) {
|
|
4922
|
+
writeJson(res, 400, { error: `too many entries: ${paths.length} (max 100)` });
|
|
4923
|
+
return;
|
|
4924
|
+
}
|
|
4925
|
+
for (const path of paths) if (!path.startsWith("/")) {
|
|
4926
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4927
|
+
return;
|
|
4928
|
+
}
|
|
4929
|
+
let command;
|
|
4930
|
+
try {
|
|
4931
|
+
for (const path of paths) await files.stat(alias, path);
|
|
4932
|
+
command = selectionArchiveCommand(paths);
|
|
4933
|
+
} catch (error) {
|
|
4934
|
+
writeRouteError(res, error);
|
|
4935
|
+
return;
|
|
4936
|
+
}
|
|
4937
|
+
await streamArchive(alias, command, res, selectionDownloadName(paths.length));
|
|
4938
|
+
}
|
|
4939
|
+
},
|
|
4940
|
+
{
|
|
4941
|
+
kind: "exact",
|
|
4942
|
+
path: REMOTE_API.workspaces,
|
|
4943
|
+
handler: async (req, res) => {
|
|
4944
|
+
if (!isLoopbackRequest(req)) {
|
|
4945
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4946
|
+
return;
|
|
4947
|
+
}
|
|
4948
|
+
const method = req.method ?? "GET";
|
|
4949
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4950
|
+
if (method === "GET") {
|
|
4951
|
+
try {
|
|
4479
4952
|
const bindings = store.listWorkspaces();
|
|
4480
4953
|
const aliasPaths = {};
|
|
4481
4954
|
for (const b of bindings) aliasPaths[b.id] = worlds.aliasPathFor(b.id);
|
|
@@ -4488,477 +4961,569 @@ function makeRoutes(deps) {
|
|
|
4488
4961
|
version: pluginVersion(),
|
|
4489
4962
|
timeouts: timeouts()
|
|
4490
4963
|
});
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
if (method === "POST") {
|
|
4494
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4495
|
-
if (!read.ok) return;
|
|
4496
|
-
const body = read.body;
|
|
4497
|
-
if (body === null) {
|
|
4498
|
-
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4499
|
-
return;
|
|
4500
|
-
}
|
|
4501
|
-
try {
|
|
4502
|
-
const binding = store.createWorkspace(body);
|
|
4503
|
-
const aliasPath = worlds.aliasPathFor(binding.id);
|
|
4504
|
-
try {
|
|
4505
|
-
mkdirSync(aliasPath, { recursive: true });
|
|
4506
|
-
} catch (error) {
|
|
4507
|
-
throw new Error(`cannot materialize alias dir ${aliasPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
4508
|
-
}
|
|
4509
|
-
await worlds.syncWorkspaces().catch(() => void 0);
|
|
4510
|
-
writeJson(res, 201, {
|
|
4511
|
-
workspace: binding,
|
|
4512
|
-
aliasPath
|
|
4513
|
-
});
|
|
4514
|
-
} catch (error) {
|
|
4515
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4516
|
-
}
|
|
4517
|
-
return;
|
|
4518
|
-
}
|
|
4519
|
-
if (method !== "DELETE") {
|
|
4520
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4521
|
-
return;
|
|
4522
|
-
}
|
|
4523
|
-
const id = queryParam(url, "id");
|
|
4524
|
-
if (id === void 0 || id === "") {
|
|
4525
|
-
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4526
|
-
return;
|
|
4527
|
-
}
|
|
4528
|
-
if (!store.removeWorkspace(id)) {
|
|
4529
|
-
writeJson(res, 404, { error: `unknown workspace id: ${id}` });
|
|
4530
|
-
return;
|
|
4531
|
-
}
|
|
4532
|
-
worlds.syncWorkspaces().catch(() => void 0);
|
|
4533
|
-
writeJson(res, 200, { ok: true });
|
|
4534
|
-
}
|
|
4535
|
-
},
|
|
4536
|
-
{
|
|
4537
|
-
kind: "exact",
|
|
4538
|
-
path: REMOTE_API.sessionCwd,
|
|
4539
|
-
handler: async (req, res) => {
|
|
4540
|
-
if (!isLoopbackRequest(req)) {
|
|
4541
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4542
|
-
return;
|
|
4543
|
-
}
|
|
4544
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4545
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4546
|
-
return;
|
|
4547
|
-
}
|
|
4548
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4549
|
-
if (!read.ok) return;
|
|
4550
|
-
const body = read.body;
|
|
4551
|
-
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4552
|
-
if (sessionId === "") {
|
|
4553
|
-
writeJson(res, 400, { error: "sessionId must be a non-empty string" });
|
|
4554
|
-
return;
|
|
4964
|
+
} catch (error) {
|
|
4965
|
+
writeStoreError(res, error);
|
|
4555
4966
|
}
|
|
4556
|
-
|
|
4967
|
+
return;
|
|
4557
4968
|
}
|
|
4558
|
-
|
|
4559
|
-
{
|
|
4560
|
-
kind: "exact",
|
|
4561
|
-
path: REMOTE_API.changes,
|
|
4562
|
-
handler: async (req, res) => {
|
|
4563
|
-
if (!isLoopbackRequest(req)) {
|
|
4564
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4565
|
-
return;
|
|
4566
|
-
}
|
|
4567
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4568
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4569
|
-
return;
|
|
4570
|
-
}
|
|
4969
|
+
if (method === "POST") {
|
|
4571
4970
|
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4572
4971
|
if (!read.ok) return;
|
|
4573
4972
|
const body = read.body;
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
if (sessionId === "" || op !== "list" && op !== "diff") {
|
|
4577
|
-
writeJson(res, 400, { error: "op (list|diff) and a non-empty sessionId are required" });
|
|
4578
|
-
return;
|
|
4579
|
-
}
|
|
4580
|
-
const ledger = deps.changes;
|
|
4581
|
-
if (ledger === void 0) {
|
|
4582
|
-
writeJson(res, 200, {
|
|
4583
|
-
summary: null,
|
|
4584
|
-
diff: null
|
|
4585
|
-
});
|
|
4586
|
-
return;
|
|
4587
|
-
}
|
|
4588
|
-
if (typeof body?.repoSubdir === "string" || body?.repoSubdir === null) ledger.useRepoSubdir(sessionId, typeof body.repoSubdir === "string" ? body.repoSubdir : void 0);
|
|
4589
|
-
if (op === "list") {
|
|
4590
|
-
if (ledger.summary(sessionId) === null) {
|
|
4591
|
-
const cwd = liveSessionCwd(ctx, sessionId);
|
|
4592
|
-
if (cwd !== void 0) ledger.beginTurn({
|
|
4593
|
-
sessionId,
|
|
4594
|
-
cwd
|
|
4595
|
-
});
|
|
4596
|
-
}
|
|
4597
|
-
if (body?.force === true) ledger.rescan(sessionId);
|
|
4598
|
-
else ledger.touch(sessionId);
|
|
4599
|
-
writeJson(res, 200, { summary: ledger.summary(sessionId) });
|
|
4600
|
-
return;
|
|
4601
|
-
}
|
|
4602
|
-
const path = typeof body?.path === "string" ? body.path : "";
|
|
4603
|
-
if (path === "") {
|
|
4604
|
-
writeJson(res, 400, { error: "path is required for op=diff" });
|
|
4973
|
+
if (body === null) {
|
|
4974
|
+
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4605
4975
|
return;
|
|
4606
4976
|
}
|
|
4607
4977
|
try {
|
|
4608
|
-
|
|
4978
|
+
const binding = store.createWorkspace(body);
|
|
4979
|
+
const aliasPath = worlds.aliasPathFor(binding.id);
|
|
4980
|
+
try {
|
|
4981
|
+
mkdirSync(aliasPath, { recursive: true });
|
|
4982
|
+
} catch (error) {
|
|
4983
|
+
throw new Error(`cannot materialize alias dir ${aliasPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
4984
|
+
}
|
|
4985
|
+
await worlds.syncWorkspaces().catch(() => void 0);
|
|
4986
|
+
writeJson(res, 201, {
|
|
4987
|
+
workspace: binding,
|
|
4988
|
+
aliasPath
|
|
4989
|
+
});
|
|
4609
4990
|
} catch (error) {
|
|
4610
|
-
|
|
4991
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4992
|
+
}
|
|
4993
|
+
return;
|
|
4994
|
+
}
|
|
4995
|
+
if (method !== "DELETE") {
|
|
4996
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4997
|
+
return;
|
|
4998
|
+
}
|
|
4999
|
+
const id = queryParam(url, "id");
|
|
5000
|
+
if (id === void 0 || id === "") {
|
|
5001
|
+
writeJson(res, 400, { error: "id query parameter is required" });
|
|
5002
|
+
return;
|
|
5003
|
+
}
|
|
5004
|
+
if (!store.removeWorkspace(id)) {
|
|
5005
|
+
writeJson(res, 404, { error: `unknown workspace id: ${id}` });
|
|
5006
|
+
return;
|
|
5007
|
+
}
|
|
5008
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
5009
|
+
writeJson(res, 200, { ok: true });
|
|
5010
|
+
}
|
|
5011
|
+
},
|
|
5012
|
+
{
|
|
5013
|
+
kind: "exact",
|
|
5014
|
+
path: REMOTE_API.sessionCwd,
|
|
5015
|
+
handler: async (req, res) => {
|
|
5016
|
+
if (!isLoopbackRequest(req)) {
|
|
5017
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5018
|
+
return;
|
|
5019
|
+
}
|
|
5020
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
5021
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
5022
|
+
return;
|
|
5023
|
+
}
|
|
5024
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5025
|
+
if (!read.ok) return;
|
|
5026
|
+
const body = read.body;
|
|
5027
|
+
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
5028
|
+
if (sessionId === "") {
|
|
5029
|
+
writeJson(res, 400, { error: "sessionId must be a non-empty string" });
|
|
5030
|
+
return;
|
|
5031
|
+
}
|
|
5032
|
+
writeJson(res, 200, { cwd: liveSessionCwd(ctx, sessionId) });
|
|
5033
|
+
}
|
|
5034
|
+
},
|
|
5035
|
+
{
|
|
5036
|
+
kind: "exact",
|
|
5037
|
+
path: REMOTE_API.changes,
|
|
5038
|
+
handler: async (req, res) => {
|
|
5039
|
+
if (!isLoopbackRequest(req)) {
|
|
5040
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5041
|
+
return;
|
|
5042
|
+
}
|
|
5043
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
5044
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
5045
|
+
return;
|
|
5046
|
+
}
|
|
5047
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5048
|
+
if (!read.ok) return;
|
|
5049
|
+
const body = read.body;
|
|
5050
|
+
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
5051
|
+
const op = typeof body?.op === "string" ? body.op : "";
|
|
5052
|
+
if (sessionId === "" || op !== "list" && op !== "diff") {
|
|
5053
|
+
writeJson(res, 400, { error: "op (list|diff) and a non-empty sessionId are required" });
|
|
5054
|
+
return;
|
|
5055
|
+
}
|
|
5056
|
+
const ledger = deps.changes;
|
|
5057
|
+
if (ledger === void 0) {
|
|
5058
|
+
writeJson(res, 200, {
|
|
5059
|
+
summary: null,
|
|
5060
|
+
diff: null
|
|
5061
|
+
});
|
|
5062
|
+
return;
|
|
5063
|
+
}
|
|
5064
|
+
if (typeof body?.repoSubdir === "string" || body?.repoSubdir === null) ledger.useRepoSubdir(sessionId, typeof body.repoSubdir === "string" ? body.repoSubdir : void 0);
|
|
5065
|
+
if (op === "list") {
|
|
5066
|
+
if (ledger.summary(sessionId) === null) {
|
|
5067
|
+
const cwd = liveSessionCwd(ctx, sessionId);
|
|
5068
|
+
if (cwd !== void 0) ledger.beginTurn({
|
|
5069
|
+
sessionId,
|
|
5070
|
+
cwd
|
|
5071
|
+
});
|
|
4611
5072
|
}
|
|
5073
|
+
if (body?.force === true) ledger.rescan(sessionId);
|
|
5074
|
+
else ledger.touch(sessionId);
|
|
5075
|
+
writeJson(res, 200, { summary: ledger.summary(sessionId) });
|
|
5076
|
+
return;
|
|
5077
|
+
}
|
|
5078
|
+
const path = typeof body?.path === "string" ? body.path : "";
|
|
5079
|
+
if (path === "") {
|
|
5080
|
+
writeJson(res, 400, { error: "path is required for op=diff" });
|
|
5081
|
+
return;
|
|
5082
|
+
}
|
|
5083
|
+
try {
|
|
5084
|
+
writeJson(res, 200, { diff: await ledger.diff(sessionId, path) });
|
|
5085
|
+
} catch (error) {
|
|
5086
|
+
writeRouteError(res, error);
|
|
5087
|
+
}
|
|
5088
|
+
}
|
|
5089
|
+
},
|
|
5090
|
+
{
|
|
5091
|
+
kind: "exact",
|
|
5092
|
+
path: REMOTE_API.routing,
|
|
5093
|
+
handler: async (req, res) => {
|
|
5094
|
+
if (!isLoopbackRequest(req)) {
|
|
5095
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5096
|
+
return;
|
|
5097
|
+
}
|
|
5098
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
5099
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
5100
|
+
return;
|
|
4612
5101
|
}
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
if (
|
|
4623
|
-
writeJson(res,
|
|
4624
|
-
return;
|
|
4625
|
-
}
|
|
4626
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4627
|
-
if (!read.ok) return;
|
|
4628
|
-
const mode = read.body?.mode;
|
|
4629
|
-
if (mode !== "off" && mode !== "shadow" && mode !== "enforce") {
|
|
4630
|
-
writeJson(res, 400, { error: `mode must be one of off/shadow/enforce (got ${JSON.stringify(mode) ?? "missing"})` });
|
|
5102
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5103
|
+
if (!read.ok) return;
|
|
5104
|
+
const mode = read.body?.mode;
|
|
5105
|
+
if (mode !== "off" && mode !== "shadow" && mode !== "enforce") {
|
|
5106
|
+
writeJson(res, 400, { error: `mode must be one of off/shadow/enforce (got ${JSON.stringify(mode) ?? "missing"})` });
|
|
5107
|
+
return;
|
|
5108
|
+
}
|
|
5109
|
+
try {
|
|
5110
|
+
const settings = ctx.get("settings");
|
|
5111
|
+
if (typeof settings?.update !== "function") {
|
|
5112
|
+
writeJson(res, 503, { error: "settings service unavailable (cannot persist routing mode)" });
|
|
4631
5113
|
return;
|
|
4632
5114
|
}
|
|
5115
|
+
const ns = ownSettingsNs(ctx) ?? "remote-vs";
|
|
5116
|
+
await settings.update(ns, { routingMode: mode });
|
|
5117
|
+
writeJson(res, 200, { mode: worlds.mode });
|
|
5118
|
+
} catch (error) {
|
|
5119
|
+
writeRouteError(res, error);
|
|
5120
|
+
}
|
|
5121
|
+
}
|
|
5122
|
+
},
|
|
5123
|
+
{
|
|
5124
|
+
kind: "exact",
|
|
5125
|
+
path: REMOTE_API.viewSettings,
|
|
5126
|
+
handler: async (req, res) => {
|
|
5127
|
+
if (!isLoopbackRequest(req)) {
|
|
5128
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5129
|
+
return;
|
|
5130
|
+
}
|
|
5131
|
+
const method = req.method ?? "GET";
|
|
5132
|
+
if (method === "GET") {
|
|
4633
5133
|
try {
|
|
4634
|
-
|
|
4635
|
-
if (typeof settings?.update !== "function") {
|
|
4636
|
-
writeJson(res, 503, { error: "settings service unavailable (cannot persist routing mode)" });
|
|
4637
|
-
return;
|
|
4638
|
-
}
|
|
4639
|
-
const ns = ownSettingsNs(ctx) ?? "remote-vs";
|
|
4640
|
-
await settings.update(ns, { routingMode: mode });
|
|
4641
|
-
writeJson(res, 200, { mode: worlds.mode });
|
|
5134
|
+
writeJson(res, 200, { settings: viewSettings.get() });
|
|
4642
5135
|
} catch (error) {
|
|
4643
5136
|
writeRouteError(res, error);
|
|
4644
5137
|
}
|
|
5138
|
+
return;
|
|
4645
5139
|
}
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
5140
|
+
if (method !== "POST") {
|
|
5141
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
5142
|
+
return;
|
|
5143
|
+
}
|
|
5144
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5145
|
+
if (!read.ok) return;
|
|
5146
|
+
try {
|
|
5147
|
+
writeJson(res, 200, { settings: viewSettings.patch(read.body ?? {}) });
|
|
5148
|
+
} catch (error) {
|
|
5149
|
+
writeRouteError(res, error);
|
|
5150
|
+
}
|
|
5151
|
+
}
|
|
5152
|
+
},
|
|
5153
|
+
{
|
|
5154
|
+
kind: "exact",
|
|
5155
|
+
path: REMOTE_API.deploy,
|
|
5156
|
+
handler: async (req, res) => {
|
|
5157
|
+
if (!isLoopbackRequest(req)) {
|
|
5158
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5159
|
+
return;
|
|
5160
|
+
}
|
|
5161
|
+
const method = req.method ?? "GET";
|
|
5162
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
5163
|
+
if (method === "GET") {
|
|
5164
|
+
const alias = queryParam(url, "alias");
|
|
5165
|
+
const view = queryParam(url, "view") ?? "status";
|
|
5166
|
+
if (alias === void 0 || alias === "") {
|
|
5167
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4653
5168
|
return;
|
|
4654
5169
|
}
|
|
4655
|
-
|
|
4656
|
-
if (method === "GET") {
|
|
5170
|
+
if (view === "probe") {
|
|
4657
5171
|
try {
|
|
4658
|
-
writeJson(res, 200,
|
|
5172
|
+
writeJson(res, 200, await probe(pool, alias));
|
|
4659
5173
|
} catch (error) {
|
|
4660
5174
|
writeRouteError(res, error);
|
|
4661
5175
|
}
|
|
4662
5176
|
return;
|
|
4663
5177
|
}
|
|
4664
|
-
if (
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
writeRouteError(res, error);
|
|
4674
|
-
}
|
|
4675
|
-
}
|
|
4676
|
-
},
|
|
4677
|
-
{
|
|
4678
|
-
kind: "exact",
|
|
4679
|
-
path: REMOTE_API.deploy,
|
|
4680
|
-
handler: async (req, res) => {
|
|
4681
|
-
if (!isLoopbackRequest(req)) {
|
|
4682
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4683
|
-
return;
|
|
4684
|
-
}
|
|
4685
|
-
const method = req.method ?? "GET";
|
|
4686
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4687
|
-
if (method === "GET") {
|
|
4688
|
-
const alias = queryParam(url, "alias");
|
|
4689
|
-
const view = queryParam(url, "view") ?? "status";
|
|
4690
|
-
if (alias === void 0 || alias === "") {
|
|
4691
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4692
|
-
return;
|
|
4693
|
-
}
|
|
4694
|
-
if (view === "probe") {
|
|
4695
|
-
try {
|
|
4696
|
-
writeJson(res, 200, await probe(pool, alias));
|
|
4697
|
-
} catch (error) {
|
|
4698
|
-
writeRouteError(res, error);
|
|
4699
|
-
}
|
|
4700
|
-
return;
|
|
4701
|
-
}
|
|
4702
|
-
if (view === "status") {
|
|
4703
|
-
try {
|
|
4704
|
-
writeJson(res, 200, {
|
|
4705
|
-
deploy: store.require(alias).deploy ?? { status: "not-deployed" },
|
|
4706
|
-
helpers: helperAvailability()
|
|
4707
|
-
});
|
|
4708
|
-
} catch (error) {
|
|
4709
|
-
writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) });
|
|
4710
|
-
}
|
|
4711
|
-
return;
|
|
5178
|
+
if (view === "status") {
|
|
5179
|
+
try {
|
|
5180
|
+
writeJson(res, 200, {
|
|
5181
|
+
deploy: store.require(alias).deploy ?? { status: "not-deployed" },
|
|
5182
|
+
helpers: helperAvailability()
|
|
5183
|
+
});
|
|
5184
|
+
} catch (error) {
|
|
5185
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
5186
|
+
else writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) });
|
|
4712
5187
|
}
|
|
4713
|
-
writeJson(res, 400, { error: `unknown view: ${view}` });
|
|
4714
|
-
return;
|
|
4715
|
-
}
|
|
4716
|
-
if (method !== "POST") {
|
|
4717
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4718
5188
|
return;
|
|
4719
5189
|
}
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
const body = read.body;
|
|
4723
|
-
if (body === null || typeof body.alias !== "string") {
|
|
4724
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
4725
|
-
return;
|
|
4726
|
-
}
|
|
4727
|
-
try {
|
|
4728
|
-
writeJson(res, 200, await pushPipeline(pool, store, body.alias, typeof body.version === "string" ? { version: body.version } : {}));
|
|
4729
|
-
} catch (error) {
|
|
4730
|
-
writeRouteError(res, error);
|
|
4731
|
-
}
|
|
5190
|
+
writeJson(res, 400, { error: `unknown view: ${view}` });
|
|
5191
|
+
return;
|
|
4732
5192
|
}
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
|
|
4736
|
-
path: REMOTE_API.importSshConfig,
|
|
4737
|
-
handler: async (req, res) => {
|
|
4738
|
-
if (!isLoopbackRequest(req)) {
|
|
4739
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4740
|
-
return;
|
|
4741
|
-
}
|
|
4742
|
-
if (req.method !== "POST") {
|
|
4743
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4744
|
-
return;
|
|
4745
|
-
}
|
|
4746
|
-
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
4747
|
-
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT);
|
|
4748
|
-
return;
|
|
4749
|
-
}
|
|
4750
|
-
const read = await readBodyOrError(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT });
|
|
4751
|
-
if (!read.ok) return;
|
|
4752
|
-
const body = read.body;
|
|
4753
|
-
try {
|
|
4754
|
-
const text = body !== null && typeof body.text === "string" ? body.text : readDefaultSshConfig();
|
|
4755
|
-
writeJson(res, 200, applyImport(parseSshConfig(text)));
|
|
4756
|
-
} catch (error) {
|
|
4757
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4758
|
-
}
|
|
5193
|
+
if (method !== "POST") {
|
|
5194
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
5195
|
+
return;
|
|
4759
5196
|
}
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
}
|
|
4769
|
-
|
|
4770
|
-
|
|
5197
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5198
|
+
if (!read.ok) return;
|
|
5199
|
+
const body = read.body;
|
|
5200
|
+
if (body === null || typeof body.alias !== "string") {
|
|
5201
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
5202
|
+
return;
|
|
5203
|
+
}
|
|
5204
|
+
try {
|
|
5205
|
+
writeJson(res, 200, await pushPipeline(pool, store, body.alias, typeof body.version === "string" ? { version: body.version } : {}));
|
|
5206
|
+
} catch (error) {
|
|
5207
|
+
writeRouteError(res, error);
|
|
5208
|
+
}
|
|
5209
|
+
}
|
|
5210
|
+
},
|
|
5211
|
+
{
|
|
5212
|
+
kind: "exact",
|
|
5213
|
+
path: REMOTE_API.importSshConfig,
|
|
5214
|
+
handler: async (req, res) => {
|
|
5215
|
+
if (!isLoopbackRequest(req)) {
|
|
5216
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5217
|
+
return;
|
|
5218
|
+
}
|
|
5219
|
+
if (req.method !== "POST") {
|
|
5220
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5221
|
+
return;
|
|
5222
|
+
}
|
|
5223
|
+
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
5224
|
+
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT);
|
|
5225
|
+
return;
|
|
5226
|
+
}
|
|
5227
|
+
const read = await readBodyOrError(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT });
|
|
5228
|
+
if (!read.ok) return;
|
|
5229
|
+
const body = read.body;
|
|
5230
|
+
try {
|
|
5231
|
+
const text = body !== null && typeof body.text === "string" ? body.text : readDefaultSshConfig();
|
|
5232
|
+
writeJson(res, 200, applyImport(parseSshConfig(text)));
|
|
5233
|
+
} catch (error) {
|
|
5234
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
5235
|
+
}
|
|
5236
|
+
}
|
|
5237
|
+
},
|
|
5238
|
+
{
|
|
5239
|
+
kind: "exact",
|
|
5240
|
+
path: REMOTE_API.importDshSsh,
|
|
5241
|
+
handler: async (req, res) => {
|
|
5242
|
+
if (!isLoopbackRequest(req)) {
|
|
5243
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5244
|
+
return;
|
|
5245
|
+
}
|
|
5246
|
+
if (req.method !== "POST") {
|
|
5247
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5248
|
+
return;
|
|
5249
|
+
}
|
|
5250
|
+
try {
|
|
5251
|
+
writeJson(res, 200, applyImport(migrateDshSsh()));
|
|
5252
|
+
} catch (error) {
|
|
5253
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
5254
|
+
}
|
|
5255
|
+
}
|
|
5256
|
+
},
|
|
5257
|
+
{
|
|
5258
|
+
kind: "exact",
|
|
5259
|
+
path: REMOTE_API.terminalClose,
|
|
5260
|
+
handler: async (req, res) => {
|
|
5261
|
+
if (!isLoopbackRequest(req)) {
|
|
5262
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5263
|
+
return;
|
|
5264
|
+
}
|
|
5265
|
+
if (req.method !== "POST") {
|
|
5266
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5267
|
+
return;
|
|
5268
|
+
}
|
|
5269
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5270
|
+
if (!read.ok) return;
|
|
5271
|
+
const body = read.body;
|
|
5272
|
+
if (body === null || typeof body.id !== "string" || body.id === "") {
|
|
5273
|
+
writeJson(res, 400, { error: "id is required" });
|
|
5274
|
+
return;
|
|
5275
|
+
}
|
|
5276
|
+
writeJson(res, 200, { ok: terminalSessions.close(body.id) });
|
|
5277
|
+
}
|
|
5278
|
+
},
|
|
5279
|
+
{
|
|
5280
|
+
kind: "exact",
|
|
5281
|
+
path: REMOTE_API.forwards,
|
|
5282
|
+
handler: async (req, res) => {
|
|
5283
|
+
if (!isLoopbackRequest(req)) {
|
|
5284
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5285
|
+
return;
|
|
5286
|
+
}
|
|
5287
|
+
if (forwards === void 0) {
|
|
5288
|
+
writeJson(res, 503, { error: "port forwarding is not available in this composition" });
|
|
5289
|
+
return;
|
|
5290
|
+
}
|
|
5291
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
5292
|
+
if (req.method === "GET") {
|
|
5293
|
+
const alias = queryParam(url, "alias");
|
|
5294
|
+
writeJson(res, 200, { forwards: forwards.list(alias === void 0 || alias === "" ? void 0 : alias) });
|
|
5295
|
+
return;
|
|
5296
|
+
}
|
|
5297
|
+
if (req.method === "DELETE") {
|
|
5298
|
+
const id = queryParam(url, "id");
|
|
5299
|
+
if (id === void 0 || id === "") {
|
|
5300
|
+
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4771
5301
|
return;
|
|
4772
5302
|
}
|
|
4773
|
-
|
|
4774
|
-
|
|
4775
|
-
} catch (error) {
|
|
4776
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4777
|
-
}
|
|
5303
|
+
writeJson(res, 200, { ok: forwards.stop(id) });
|
|
5304
|
+
return;
|
|
4778
5305
|
}
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
5306
|
+
if (req.method !== "POST") {
|
|
5307
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5308
|
+
return;
|
|
5309
|
+
}
|
|
5310
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5311
|
+
if (!read.ok) return;
|
|
5312
|
+
const validated = validateForwardRequest(read.body);
|
|
5313
|
+
if (!validated.ok) {
|
|
5314
|
+
writeJson(res, 400, { error: validated.error });
|
|
5315
|
+
return;
|
|
5316
|
+
}
|
|
5317
|
+
try {
|
|
5318
|
+
writeJson(res, 200, { forward: await forwards.start(validated.value) });
|
|
5319
|
+
} catch (error) {
|
|
5320
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
5321
|
+
writeJson(res, /EADDRINUSE|EACCES|already in use|already has \d+ local forwards|permission denied|not permitted/i.test(message) ? 409 : 502, { error: message });
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5324
|
+
},
|
|
5325
|
+
{
|
|
5326
|
+
kind: "exact",
|
|
5327
|
+
path: REMOTE_API.listenPorts,
|
|
5328
|
+
handler: async (req, res) => {
|
|
5329
|
+
if (!isLoopbackRequest(req)) {
|
|
5330
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5331
|
+
return;
|
|
5332
|
+
}
|
|
5333
|
+
if (req.method !== "GET") {
|
|
5334
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5335
|
+
return;
|
|
5336
|
+
}
|
|
5337
|
+
const alias = queryParam(new URL(req.url ?? "/", "http://localhost"), "alias");
|
|
5338
|
+
if (alias === void 0 || alias === "") {
|
|
5339
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
5340
|
+
return;
|
|
5341
|
+
}
|
|
5342
|
+
try {
|
|
5343
|
+
const result = await pool.exec(alias, listenPortsCommand(), { timeoutMs: LISTEN_PORTS_TIMEOUT_MS });
|
|
5344
|
+
if (result.error !== void 0) {
|
|
5345
|
+
writeJson(res, 502, { error: result.error });
|
|
4786
5346
|
return;
|
|
4787
5347
|
}
|
|
4788
|
-
if (
|
|
4789
|
-
writeJson(res,
|
|
5348
|
+
if (result.timedOut) {
|
|
5349
|
+
writeJson(res, 200, {
|
|
5350
|
+
tool: "unknown",
|
|
5351
|
+
ports: [],
|
|
5352
|
+
error: `the remote probe did not finish within ${LISTEN_PORTS_TIMEOUT_MS / 1e3}s`
|
|
5353
|
+
});
|
|
4790
5354
|
return;
|
|
4791
5355
|
}
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
5356
|
+
if (result.exitCode === null) {
|
|
5357
|
+
writeJson(res, 200, {
|
|
5358
|
+
tool: "unknown",
|
|
5359
|
+
ports: [],
|
|
5360
|
+
error: "the remote probe ended without an exit status (the SSH channel closed)"
|
|
5361
|
+
});
|
|
4797
5362
|
return;
|
|
4798
5363
|
}
|
|
4799
|
-
writeJson(res, 200,
|
|
5364
|
+
writeJson(res, 200, parseListenPorts(result.stdout, result.exitCode));
|
|
5365
|
+
} catch (error) {
|
|
5366
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
5367
|
+
else writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4800
5368
|
}
|
|
4801
|
-
}
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
}
|
|
4814
|
-
|
|
4815
|
-
|
|
5369
|
+
}
|
|
5370
|
+
},
|
|
5371
|
+
{
|
|
5372
|
+
kind: "exact",
|
|
5373
|
+
path: REMOTE_API.processes,
|
|
5374
|
+
handler: async (req, res) => {
|
|
5375
|
+
if (!isLoopbackRequest(req)) {
|
|
5376
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5377
|
+
return;
|
|
5378
|
+
}
|
|
5379
|
+
const method = req.method ?? "GET";
|
|
5380
|
+
if (method !== "GET" && method !== "POST") {
|
|
5381
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
5382
|
+
return;
|
|
5383
|
+
}
|
|
5384
|
+
try {
|
|
5385
|
+
if (method === "GET") {
|
|
5386
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4816
5387
|
const alias = queryParam(url, "alias");
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
}
|
|
4820
|
-
if (req.method === "DELETE") {
|
|
4821
|
-
const id = queryParam(url, "id");
|
|
4822
|
-
if (id === void 0 || id === "") {
|
|
4823
|
-
writeJson(res, 400, { error: "id query parameter is required" });
|
|
5388
|
+
if (alias === void 0 || alias === "") {
|
|
5389
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4824
5390
|
return;
|
|
4825
5391
|
}
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
writeJson(res,
|
|
5392
|
+
if (store.get(alias) === void 0) {
|
|
5393
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
5394
|
+
return;
|
|
5395
|
+
}
|
|
5396
|
+
writeJson(res, 200, await readProcesses(alias, queryParam(url, "refresh") === "1" ? 0 : 3e3));
|
|
4831
5397
|
return;
|
|
4832
5398
|
}
|
|
4833
5399
|
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4834
5400
|
if (!read.ok) return;
|
|
4835
|
-
const
|
|
4836
|
-
|
|
4837
|
-
|
|
5401
|
+
const body = read.body ?? {};
|
|
5402
|
+
const alias = typeof body.alias === "string" ? body.alias : "";
|
|
5403
|
+
if (alias === "") {
|
|
5404
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4838
5405
|
return;
|
|
4839
5406
|
}
|
|
5407
|
+
let pid;
|
|
4840
5408
|
try {
|
|
4841
|
-
|
|
5409
|
+
pid = assertProcessPid(body.pid);
|
|
4842
5410
|
} catch (error) {
|
|
4843
|
-
|
|
4844
|
-
writeJson(res, /EADDRINUSE|EACCES|already in use|already has \d+ local forwards|permission denied|not permitted/i.test(message) ? 409 : 502, { error: message });
|
|
4845
|
-
}
|
|
4846
|
-
}
|
|
4847
|
-
},
|
|
4848
|
-
{
|
|
4849
|
-
kind: "exact",
|
|
4850
|
-
path: REMOTE_API.listenPorts,
|
|
4851
|
-
handler: async (req, res) => {
|
|
4852
|
-
if (!isLoopbackRequest(req)) {
|
|
4853
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4854
|
-
return;
|
|
4855
|
-
}
|
|
4856
|
-
if (req.method !== "GET") {
|
|
4857
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4858
|
-
return;
|
|
4859
|
-
}
|
|
4860
|
-
const alias = queryParam(new URL(req.url ?? "/", "http://localhost"), "alias");
|
|
4861
|
-
if (alias === void 0 || alias === "") {
|
|
4862
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
5411
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4863
5412
|
return;
|
|
4864
5413
|
}
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
if (result.error !== void 0) {
|
|
4868
|
-
writeJson(res, 502, { error: result.error });
|
|
4869
|
-
return;
|
|
4870
|
-
}
|
|
4871
|
-
if (result.timedOut) {
|
|
4872
|
-
writeJson(res, 200, {
|
|
4873
|
-
tool: "unknown",
|
|
4874
|
-
ports: [],
|
|
4875
|
-
error: `the remote probe did not finish within ${LISTEN_PORTS_TIMEOUT_MS / 1e3}s`
|
|
4876
|
-
});
|
|
4877
|
-
return;
|
|
4878
|
-
}
|
|
4879
|
-
if (result.exitCode === null) {
|
|
4880
|
-
writeJson(res, 200, {
|
|
4881
|
-
tool: "unknown",
|
|
4882
|
-
ports: [],
|
|
4883
|
-
error: "the remote probe ended without an exit status (the SSH channel closed)"
|
|
4884
|
-
});
|
|
4885
|
-
return;
|
|
4886
|
-
}
|
|
4887
|
-
writeJson(res, 200, parseListenPorts(result.stdout, result.exitCode));
|
|
4888
|
-
} catch (error) {
|
|
4889
|
-
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4890
|
-
}
|
|
4891
|
-
}
|
|
4892
|
-
},
|
|
4893
|
-
{
|
|
4894
|
-
kind: "exact",
|
|
4895
|
-
path: REMOTE_API.metrics,
|
|
4896
|
-
handler: async (req, res) => {
|
|
4897
|
-
if (!isLoopbackRequest(req)) {
|
|
4898
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5414
|
+
if (!isKillSignal(body.signal)) {
|
|
5415
|
+
writeJson(res, 400, { error: "signal must be TERM or KILL" });
|
|
4899
5416
|
return;
|
|
4900
5417
|
}
|
|
4901
|
-
if (
|
|
4902
|
-
writeJson(res,
|
|
5418
|
+
if ((await readProcesses(alias, 0)).processes.find((process) => process.pid === pid) === void 0) {
|
|
5419
|
+
writeJson(res, 400, { error: `process ${pid} is not in the current remote snapshot` });
|
|
4903
5420
|
return;
|
|
4904
5421
|
}
|
|
4905
|
-
const
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
5422
|
+
const result = await pool.exec(alias, killCommand(pid, body.signal), { timeoutMs: 1e4 });
|
|
5423
|
+
if (result.error !== void 0 || result.exitCode !== 0) {
|
|
5424
|
+
const message = result.error ?? result.stderr.trim() ?? "";
|
|
5425
|
+
writeJson(res, 502, { error: message === "" ? `kill failed [exit code: ${result.exitCode ?? "?"}]` : message });
|
|
4910
5426
|
return;
|
|
4911
5427
|
}
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
|
|
4916
|
-
|
|
4917
|
-
}
|
|
5428
|
+
processSnapshots.delete(alias);
|
|
5429
|
+
writeJson(res, 200, {
|
|
5430
|
+
ok: true,
|
|
5431
|
+
pid,
|
|
5432
|
+
signal: body.signal
|
|
5433
|
+
});
|
|
5434
|
+
} catch (error) {
|
|
5435
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
5436
|
+
else writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4918
5437
|
}
|
|
4919
5438
|
}
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
5439
|
+
},
|
|
5440
|
+
{
|
|
5441
|
+
kind: "exact",
|
|
5442
|
+
path: REMOTE_API.metrics,
|
|
5443
|
+
handler: async (req, res) => {
|
|
4924
5444
|
if (!isLoopbackRequest(req)) {
|
|
4925
|
-
|
|
4926
|
-
|
|
5445
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5446
|
+
return;
|
|
5447
|
+
}
|
|
5448
|
+
if (req.method !== "GET") {
|
|
5449
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4927
5450
|
return;
|
|
4928
5451
|
}
|
|
4929
5452
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4930
5453
|
const alias = queryParam(url, "alias");
|
|
4931
|
-
const
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
5454
|
+
const refresh = queryParam(url, "refresh") === "1";
|
|
5455
|
+
/** Explicit opt-in to sampling every configured host (tooling/debug). */
|
|
5456
|
+
const all = queryParam(url, "all") === "1";
|
|
5457
|
+
try {
|
|
5458
|
+
let hosts;
|
|
5459
|
+
if (alias !== void 0 && alias !== "") {
|
|
5460
|
+
if (store.get(alias) === void 0) {
|
|
5461
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
5462
|
+
return;
|
|
5463
|
+
}
|
|
5464
|
+
hosts = [await monitor.sample(alias, refresh)];
|
|
5465
|
+
} else if (all) {
|
|
5466
|
+
const aliases = store.list().map((host) => host.alias);
|
|
5467
|
+
hosts = await Promise.all(aliases.map((host) => monitor.sample(host, refresh)));
|
|
5468
|
+
} else hosts = monitor.cached();
|
|
5469
|
+
writeJson(res, 200, { hosts });
|
|
5470
|
+
} catch (error) {
|
|
5471
|
+
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
5472
|
+
}
|
|
5473
|
+
}
|
|
5474
|
+
}
|
|
5475
|
+
];
|
|
5476
|
+
const upgrade = {
|
|
5477
|
+
path: REMOTE_API.terminal,
|
|
5478
|
+
handler: (req, socket, head) => {
|
|
5479
|
+
if (!isLoopbackRequest(req)) {
|
|
5480
|
+
socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
|
|
5481
|
+
socket.destroy();
|
|
5482
|
+
return;
|
|
5483
|
+
}
|
|
5484
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
5485
|
+
const alias = queryParam(url, "alias");
|
|
5486
|
+
const sessionId = queryParam(url, "session");
|
|
5487
|
+
if (alias === void 0 && sessionId === void 0) {
|
|
5488
|
+
socket.write("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|
|
5489
|
+
socket.destroy();
|
|
5490
|
+
return;
|
|
5491
|
+
}
|
|
5492
|
+
const cols = Number.parseInt(queryParam(url, "cols") ?? "80", 10);
|
|
5493
|
+
const rows = Number.parseInt(queryParam(url, "rows") ?? "24", 10);
|
|
5494
|
+
const size = {
|
|
5495
|
+
cols: Number.isFinite(cols) && cols > 0 ? Math.min(cols, 500) : 80,
|
|
5496
|
+
rows: Number.isFinite(rows) && rows > 0 ? Math.min(rows, 200) : 24
|
|
5497
|
+
};
|
|
5498
|
+
terminalWss.handleUpgrade(req, socket, head, (ws) => {
|
|
5499
|
+
if (sessionId !== void 0) {
|
|
5500
|
+
if (terminalSessions.attach(ws, sessionId, size.cols, size.rows)) return;
|
|
5501
|
+
try {
|
|
5502
|
+
ws.send(JSON.stringify({
|
|
5503
|
+
t: "error",
|
|
5504
|
+
message: "terminal session expired"
|
|
5505
|
+
}));
|
|
5506
|
+
} catch {}
|
|
5507
|
+
try {
|
|
5508
|
+
ws.close(1e3);
|
|
5509
|
+
} catch {}
|
|
4935
5510
|
return;
|
|
4936
5511
|
}
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
|
|
4943
|
-
|
|
4944
|
-
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
ws.send(JSON.stringify({
|
|
4948
|
-
t: "error",
|
|
4949
|
-
message: "terminal session expired"
|
|
4950
|
-
}));
|
|
4951
|
-
} catch {}
|
|
4952
|
-
try {
|
|
4953
|
-
ws.close(1e3);
|
|
4954
|
-
} catch {}
|
|
4955
|
-
return;
|
|
4956
|
-
}
|
|
4957
|
-
terminalSessions.open(ws, alias, size.cols, size.rows).then(() => void 0, () => void 0);
|
|
4958
|
-
});
|
|
5512
|
+
terminalSessions.open(ws, alias, size.cols, size.rows).then(() => void 0, () => void 0);
|
|
5513
|
+
});
|
|
5514
|
+
}
|
|
5515
|
+
};
|
|
5516
|
+
return {
|
|
5517
|
+
routes: routes.map((route) => ({
|
|
5518
|
+
...route,
|
|
5519
|
+
handler: (req, res) => {
|
|
5520
|
+
scopeHostNotices(res, urlAliasOf(req));
|
|
5521
|
+
return route.handler(req, res);
|
|
4959
5522
|
}
|
|
4960
|
-
},
|
|
4961
|
-
|
|
5523
|
+
})),
|
|
5524
|
+
upgrade,
|
|
5525
|
+
terminalSessions,
|
|
5526
|
+
close
|
|
4962
5527
|
};
|
|
4963
5528
|
}
|
|
4964
5529
|
//#endregion
|