dsh-remote-go 0.2.0 → 0.3.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.md +11 -3
- package/{client.js → dist/client.js} +7710 -8182
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/{src → dist/src}/constants.js +30 -56
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +37 -19
- package/src/admission.js +0 -43
- package/src/api.js +0 -536
- package/src/atomic-json.js +0 -225
- package/src/bootstrap.js +0 -113
- package/src/connect-log.js +0 -176
- package/src/connect-pipeline.js +0 -1900
- package/src/connect-progress.js +0 -155
- package/src/diagnostics.js +0 -160
- package/src/download-jobs.js +0 -234
- package/src/errors.js +0 -412
- package/src/host-lifecycle.js +0 -604
- package/src/host-registry.js +0 -217
- package/src/host-runtime.js +0 -127
- package/src/host-view.js +0 -75
- package/src/index.js +0 -206
- package/src/key-files.js +0 -54
- package/src/known-hosts.js +0 -133
- package/src/layout.js +0 -53
- package/src/local-download.js +0 -381
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -101
- package/src/prompts.js +0 -229
- package/src/redact.js +0 -68
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -137
- package/src/remote-proc.js +0 -343
- package/src/remote-proxy.js +0 -815
- package/src/remote-web.js +0 -751
- package/src/request-trust.js +0 -89
- package/src/secrets.js +0 -109
- package/src/service.js +0 -459
- package/src/settings.js +0 -163
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -731
- package/src/store.js +0 -193
- package/src/tools.js +0 -386
- package/src/tunnel.js +0 -132
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -43
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
package/dist/src/util.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small pure helpers shared by the host half, unit-tested with node:test.
|
|
3
|
+
*/
|
|
4
|
+
export function sleep(ms) {
|
|
5
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
|
+
}
|
|
7
|
+
export function randomDelay(min, max) {
|
|
8
|
+
return Math.round(min + Math.random() * Math.max(0, max - min));
|
|
9
|
+
}
|
|
10
|
+
const ID_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
11
|
+
export function randomId(prefix = 'h') {
|
|
12
|
+
let out = '';
|
|
13
|
+
for (let i = 0; i < 10; i += 1)
|
|
14
|
+
out += ID_ALPHABET[Math.floor(Math.random() * ID_ALPHABET.length)];
|
|
15
|
+
return `${prefix}_${out}`;
|
|
16
|
+
}
|
|
17
|
+
export function nowIso() {
|
|
18
|
+
return new Date().toISOString();
|
|
19
|
+
}
|
|
20
|
+
export function isPlainObject(value) {
|
|
21
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
const LOG_LEVELS = { error: 0, warn: 1, info: 2, debug: 3 };
|
|
24
|
+
/** Prototype-chain names that must never be written by a merge. */
|
|
25
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
26
|
+
/**
|
|
27
|
+
* Recursively merge `patch` into `base` (arrays and scalars replace). Only keys
|
|
28
|
+
* `base` declares are written, so untrusted input can change known values but
|
|
29
|
+
* never invent a property or poison the prototype.
|
|
30
|
+
*/
|
|
31
|
+
export function deepMerge(base, patch) {
|
|
32
|
+
if (!isPlainObject(base) || !isPlainObject(patch))
|
|
33
|
+
return (patch === undefined ? base : patch);
|
|
34
|
+
const out = { ...base };
|
|
35
|
+
for (const key of Object.keys(base)) {
|
|
36
|
+
if (UNSAFE_KEYS.has(key))
|
|
37
|
+
continue;
|
|
38
|
+
const value = patch[key];
|
|
39
|
+
if (value === undefined)
|
|
40
|
+
continue;
|
|
41
|
+
out[key] = isPlainObject(value) && isPlainObject(base[key]) ? deepMerge(base[key], value) : value;
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
export function clamp(value, min, max) {
|
|
46
|
+
return Math.min(max, Math.max(min, value));
|
|
47
|
+
}
|
|
48
|
+
export function expandHome(value, home) {
|
|
49
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
50
|
+
if (text === '')
|
|
51
|
+
return '';
|
|
52
|
+
if (text === '~')
|
|
53
|
+
return home;
|
|
54
|
+
if (text.startsWith('~/') || text.startsWith('~\\'))
|
|
55
|
+
return home.replace(/[\\/]+$/, '') + text.slice(1);
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
export function parseSshDestination(raw) {
|
|
59
|
+
const text = String(raw ?? '').trim();
|
|
60
|
+
if (text === '')
|
|
61
|
+
return { user: null, host: '', port: null };
|
|
62
|
+
let user = null;
|
|
63
|
+
let rest = text;
|
|
64
|
+
const at = rest.lastIndexOf('@');
|
|
65
|
+
if (at >= 0) {
|
|
66
|
+
user = rest.slice(0, at);
|
|
67
|
+
rest = rest.slice(at + 1);
|
|
68
|
+
}
|
|
69
|
+
let host = rest;
|
|
70
|
+
let port = null;
|
|
71
|
+
if (rest.startsWith('[')) {
|
|
72
|
+
const close = rest.indexOf(']');
|
|
73
|
+
if (close > 0) {
|
|
74
|
+
host = rest.slice(1, close);
|
|
75
|
+
const tail = rest.slice(close + 1);
|
|
76
|
+
if (/^:\d+$/.test(tail))
|
|
77
|
+
port = Number(tail.slice(1));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const colon = rest.lastIndexOf(':');
|
|
82
|
+
if (colon > 0 && rest.indexOf(':') === colon) {
|
|
83
|
+
const maybePort = rest.slice(colon + 1);
|
|
84
|
+
if (/^\d{1,5}$/.test(maybePort)) {
|
|
85
|
+
host = rest.slice(0, colon);
|
|
86
|
+
port = Number(maybePort);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { user: user || null, host, port };
|
|
91
|
+
}
|
|
92
|
+
export function isLoopbackHost(host) {
|
|
93
|
+
if (!host)
|
|
94
|
+
return false;
|
|
95
|
+
const normalized = host
|
|
96
|
+
.trim()
|
|
97
|
+
.toLowerCase()
|
|
98
|
+
.replace(/^\[|\]$/g, '');
|
|
99
|
+
if (normalized === 'localhost' || normalized === '::1')
|
|
100
|
+
return true;
|
|
101
|
+
if (normalized === '::ffff:127.0.0.1')
|
|
102
|
+
return true;
|
|
103
|
+
return /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(normalized);
|
|
104
|
+
}
|
|
105
|
+
export function shellQuote(value) {
|
|
106
|
+
return `'${String(value).replaceAll("'", `'\\''`)}'`;
|
|
107
|
+
}
|
|
108
|
+
export function headTail(text, max = 4000) {
|
|
109
|
+
const value = String(text ?? '');
|
|
110
|
+
if (value.length <= max)
|
|
111
|
+
return value;
|
|
112
|
+
const head = value.slice(0, Math.floor(max / 2));
|
|
113
|
+
const tail = value.slice(-Math.floor(max / 2));
|
|
114
|
+
return `${head}\n…\n${tail}`;
|
|
115
|
+
}
|
|
116
|
+
export function logVisible(level, threshold = 'info') {
|
|
117
|
+
const rank = LOG_LEVELS[level] ?? LOG_LEVELS.info;
|
|
118
|
+
const floor = LOG_LEVELS[threshold] ?? LOG_LEVELS.info;
|
|
119
|
+
return rank >= floor;
|
|
120
|
+
}
|
|
121
|
+
/** Message of an unknown thrown value, so catch blocks need not spell the ternary. */
|
|
122
|
+
export function messageOf(error) {
|
|
123
|
+
return String(error?.message ?? error);
|
|
124
|
+
}
|
|
125
|
+
/** A positive integer — the only shape `kill`, `/proc` and a TCP port accept. */
|
|
126
|
+
export function isPid(value) {
|
|
127
|
+
return Number.isInteger(value) && value > 0;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A promise that resolves at most once: through `settle`, or with `onTimeout()`
|
|
131
|
+
* when the deadline passes first. `onSettled` runs before the promise resolves so
|
|
132
|
+
* a caller can clear the pending slot it installed on both paths.
|
|
133
|
+
*/
|
|
134
|
+
export function createDecision(timeoutMs, onTimeout, onSettled) {
|
|
135
|
+
let settle;
|
|
136
|
+
const promise = new Promise((resolve) => {
|
|
137
|
+
let done = false;
|
|
138
|
+
const timer = setTimeout(() => settle(onTimeout?.()), timeoutMs);
|
|
139
|
+
settle = (value) => {
|
|
140
|
+
if (done)
|
|
141
|
+
return;
|
|
142
|
+
done = true;
|
|
143
|
+
clearTimeout(timer);
|
|
144
|
+
onSettled?.(value);
|
|
145
|
+
resolve(value);
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
return { promise, settle };
|
|
149
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version parsing/comparison and the exact-version parity rule, covering the shapes DSH and Node publish (`0.2.2-rc.1`, `24.22.2`, optional `v`).
|
|
3
|
+
*/
|
|
4
|
+
const VERSION_RE = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+([0-9A-Za-z.-]+))?$/;
|
|
5
|
+
export function parseVersion(input) {
|
|
6
|
+
if (typeof input !== 'string')
|
|
7
|
+
return null;
|
|
8
|
+
const match = VERSION_RE.exec(input.trim());
|
|
9
|
+
if (!match)
|
|
10
|
+
return null;
|
|
11
|
+
const [, major, minor, patch, prerelease, build] = match;
|
|
12
|
+
return {
|
|
13
|
+
raw: input.trim(),
|
|
14
|
+
major: Number(major),
|
|
15
|
+
minor: Number(minor),
|
|
16
|
+
patch: Number(patch),
|
|
17
|
+
prerelease: prerelease ? prerelease.split('.').map((part) => (/^\d+$/.test(part) ? Number(part) : part)) : [],
|
|
18
|
+
build: build ?? null,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function normalizeVersion(input) {
|
|
22
|
+
const parsed = parseVersion(input);
|
|
23
|
+
if (!parsed)
|
|
24
|
+
return String(input ?? '').trim();
|
|
25
|
+
const core = `${parsed.major}.${parsed.minor}.${parsed.patch}`;
|
|
26
|
+
return parsed.prerelease.length > 0 ? `${core}-${parsed.prerelease.join('.')}` : core;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Semver identifier comparison: numeric identifiers sort before alphanumeric ones. The
|
|
30
|
+
* pre-release list is `(string | number)[]` by spec, so the two `as number` assertions only
|
|
31
|
+
* restore the branch `typeof` already proved — nothing is coerced.
|
|
32
|
+
*/
|
|
33
|
+
function compareIdentifiers(a, b) {
|
|
34
|
+
const aNum = typeof a === 'number';
|
|
35
|
+
const bNum = typeof b === 'number';
|
|
36
|
+
if (aNum && bNum)
|
|
37
|
+
return a === b ? 0 : a < b ? -1 : 1;
|
|
38
|
+
if (aNum)
|
|
39
|
+
return -1; // numeric identifiers sort before alphanumeric ones
|
|
40
|
+
if (bNum)
|
|
41
|
+
return 1;
|
|
42
|
+
return a === b ? 0 : String(a) < String(b) ? -1 : 1;
|
|
43
|
+
}
|
|
44
|
+
export function compareVersions(a, b) {
|
|
45
|
+
const pa = parseVersion(a);
|
|
46
|
+
const pb = parseVersion(b);
|
|
47
|
+
if (!pa || !pb) {
|
|
48
|
+
const sa = String(a ?? '');
|
|
49
|
+
const sb = String(b ?? '');
|
|
50
|
+
return sa === sb ? 0 : sa < sb ? -1 : 1;
|
|
51
|
+
}
|
|
52
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
53
|
+
if (pa[key] !== pb[key])
|
|
54
|
+
return pa[key] < pb[key] ? -1 : 1;
|
|
55
|
+
}
|
|
56
|
+
if (pa.prerelease.length === 0 && pb.prerelease.length === 0)
|
|
57
|
+
return 0;
|
|
58
|
+
if (pa.prerelease.length === 0)
|
|
59
|
+
return 1;
|
|
60
|
+
if (pb.prerelease.length === 0)
|
|
61
|
+
return -1;
|
|
62
|
+
const length = Math.max(pa.prerelease.length, pb.prerelease.length);
|
|
63
|
+
for (let i = 0; i < length; i += 1) {
|
|
64
|
+
const left = pa.prerelease[i];
|
|
65
|
+
const right = pb.prerelease[i];
|
|
66
|
+
if (left === undefined)
|
|
67
|
+
return -1;
|
|
68
|
+
if (right === undefined)
|
|
69
|
+
return 1;
|
|
70
|
+
const result = compareIdentifiers(left, right);
|
|
71
|
+
if (result !== 0)
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
export function versionsEqual(a, b) {
|
|
77
|
+
return compareVersions(normalizeVersion(a), normalizeVersion(b)) === 0;
|
|
78
|
+
}
|
|
79
|
+
export function versionParity(localVersion, remoteVersion) {
|
|
80
|
+
if (!localVersion || !remoteVersion) {
|
|
81
|
+
return {
|
|
82
|
+
status: 'unknown',
|
|
83
|
+
local: localVersion ?? null,
|
|
84
|
+
remote: remoteVersion ?? null,
|
|
85
|
+
same: false,
|
|
86
|
+
message: {
|
|
87
|
+
zh: '无法确定版本一致性(缺少本机或远端版本)。',
|
|
88
|
+
en: 'Version parity is unknown (a local or remote version is missing).',
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const same = versionsEqual(localVersion, remoteVersion);
|
|
93
|
+
return {
|
|
94
|
+
status: same ? 'same' : 'different',
|
|
95
|
+
local: normalizeVersion(localVersion),
|
|
96
|
+
remote: normalizeVersion(remoteVersion),
|
|
97
|
+
same,
|
|
98
|
+
message: same
|
|
99
|
+
? { zh: '远端与本机 DSH 版本一致。', en: 'The remote DSH version matches this machine.' }
|
|
100
|
+
: {
|
|
101
|
+
zh: `远端 DSH(${normalizeVersion(remoteVersion)})与本机(${normalizeVersion(localVersion)})不一致。`,
|
|
102
|
+
en: `Remote DSH (${normalizeVersion(remoteVersion)}) differs from this machine (${normalizeVersion(localVersion)}).`,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export const MIN_NODE_VERSION = '24.9.0';
|
|
107
|
+
export function isNodeVersionSupported(version) {
|
|
108
|
+
if (!version)
|
|
109
|
+
return false;
|
|
110
|
+
// node -v prints "v24.22.2"; a bare "24" from a fallback should not pass.
|
|
111
|
+
if (!parseVersion(version))
|
|
112
|
+
return false;
|
|
113
|
+
return compareVersions(normalizeVersion(version), MIN_NODE_VERSION) >= 0;
|
|
114
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create the one bus a service instance owns.
|
|
3
|
+
* @param options - only the warn sink for a listener that throws.
|
|
4
|
+
* @returns the bus.
|
|
5
|
+
*/
|
|
6
|
+
export function createWatchdogBus(options = {}) {
|
|
7
|
+
const listeners = new Set();
|
|
8
|
+
return {
|
|
9
|
+
publish(event) {
|
|
10
|
+
// Snapshot: a listener that (un)subscribes while handling an event must not change the
|
|
11
|
+
// iteration of the event being published.
|
|
12
|
+
for (const listener of [...listeners]) {
|
|
13
|
+
try {
|
|
14
|
+
listener(event);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
// A listener failure must never break the probe that published the event.
|
|
18
|
+
options.warn?.('[dsh-remote-go] watchdog listener failed:', error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
subscribe(listener) {
|
|
23
|
+
listeners.add(listener);
|
|
24
|
+
return () => {
|
|
25
|
+
listeners.delete(listener);
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The liveness watchdog: the heartbeat probe, the episode it opens when the remote UI stops
|
|
3
|
+
* answering, and the two action requests it turns into bus events (design-arch §1.7).
|
|
4
|
+
*
|
|
5
|
+
* It used to live inside `host-lifecycle.ts` and reach its one-shot actions back through `ctx`
|
|
6
|
+
* (`requestReattach`/`repairTunnel`). Those reverse callbacks are gone: a confirmed or recovered
|
|
7
|
+
* episode is published on `ctx.bus`, and `connect/watchdog.ts` — the bus's only subscriber — runs
|
|
8
|
+
* the action. The dependency arrow therefore points one way only: this module never imports
|
|
9
|
+
* `connect/*`, so the modules that dispatch `arm(heartbeat)` do not create a cycle.
|
|
10
|
+
*
|
|
11
|
+
* The two timer *primitives* (`stopRemoteHeartbeat`/`cancelRemoteCheck`) stay in
|
|
12
|
+
* `host-lifecycle.ts`, the owner of the runtime's timer block; the policy that fills them lives
|
|
13
|
+
* here.
|
|
14
|
+
*/
|
|
15
|
+
import { REMOTE_EPISODE_COOLDOWN_MS, REMOTE_HEARTBEAT_DEFAULT_SEC, REMOTE_HEARTBEAT_MAX_SEC, REMOTE_HEARTBEAT_MIN_SEC, REMOTE_UNREACHABLE_FAILURES, } from './constants.js';
|
|
16
|
+
import { cancelRemoteCheck, stopRemoteHeartbeat } from './host-lifecycle.js';
|
|
17
|
+
import { isUiUp } from './remote-proc.js';
|
|
18
|
+
/** Milliseconds between liveness probes, from settings (default 30s, disabled allowed). */
|
|
19
|
+
export function remoteHeartbeatIntervalSec(ctx) {
|
|
20
|
+
const raw = ctx.settings()?.remoteHeartbeat;
|
|
21
|
+
const seconds = Number(raw?.intervalSec);
|
|
22
|
+
if (!Number.isFinite(seconds))
|
|
23
|
+
return REMOTE_HEARTBEAT_DEFAULT_SEC;
|
|
24
|
+
return Math.min(REMOTE_HEARTBEAT_MAX_SEC, Math.max(REMOTE_HEARTBEAT_MIN_SEC, Math.round(seconds)));
|
|
25
|
+
}
|
|
26
|
+
/** Start the light liveness probe for a host that just became connected. */
|
|
27
|
+
export function startRemoteHeartbeat(ctx, runtime) {
|
|
28
|
+
stopRemoteHeartbeat(runtime);
|
|
29
|
+
const enabled = ctx.settings()?.remoteHeartbeat?.enabled !== false;
|
|
30
|
+
if (!enabled) {
|
|
31
|
+
ctx.progress.log(runtime, 'debug', 'remote heartbeat: disabled by settings');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const intervalSec = remoteHeartbeatIntervalSec(ctx);
|
|
35
|
+
runtime.heartbeatTimer = setInterval(() => {
|
|
36
|
+
void remoteHeartbeatTick(ctx, runtime);
|
|
37
|
+
}, intervalSec * 1000);
|
|
38
|
+
runtime.heartbeatTimer.unref?.();
|
|
39
|
+
ctx.progress.log(runtime, 'debug', `remote heartbeat: probing every ${intervalSec}s`);
|
|
40
|
+
}
|
|
41
|
+
/** A host is watchable only while it is really up and no one-shot action owns the link. */
|
|
42
|
+
export function watchable(ctx, runtime) {
|
|
43
|
+
return (ctx.state.disposed !== true &&
|
|
44
|
+
runtime.desired === 'connected' &&
|
|
45
|
+
runtime.status === 'connected' &&
|
|
46
|
+
runtime.linkBusy === null &&
|
|
47
|
+
runtime.ssh?.connected === true &&
|
|
48
|
+
runtime.tunnel !== null);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Open a liveness episode at most once. Transient reports (a proxy/tunnel failure) are
|
|
52
|
+
* coalesced: after a confirmed-healthy episode the same kind of report is ignored for a
|
|
53
|
+
* cooldown window, so a flapping tunnel cannot emit suspected/recovered on every request.
|
|
54
|
+
* Returns whether a new episode was opened.
|
|
55
|
+
*/
|
|
56
|
+
function beginRemoteEpisode(ctx, runtime, source) {
|
|
57
|
+
if (runtime.remoteEpisode === true)
|
|
58
|
+
return false;
|
|
59
|
+
const transient = source === 'proxy' || source === 'tunnel';
|
|
60
|
+
if (transient && Date.now() < (Number(runtime.episodeCooldownUntil) || 0)) {
|
|
61
|
+
ctx.progress.log(runtime, 'debug', `remote-unreachable reported again (${source}); episode cooldown, ignoring`);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
runtime.remoteEpisode = true;
|
|
65
|
+
runtime.episodeSource = source;
|
|
66
|
+
ctx.progress.log(runtime, 'debug', `remote-unreachable suspected (${source}); confirming with probes`);
|
|
67
|
+
ctx.onRemoteUnreachable?.(runtime, { phase: 'suspected', source });
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Reset the failure streak and close the episode. The recovery *action* (probe the local
|
|
72
|
+
* listener, rebuild it when needed, announce the episode) belongs to the bus subscriber:
|
|
73
|
+
* `connect/watchdog.ts` runs it and publishes `recovered` once the link is usable again, which is
|
|
74
|
+
* the same point in the sequence the old inline code announced it at.
|
|
75
|
+
*/
|
|
76
|
+
function clearRemoteEpisode(ctx, runtime, source) {
|
|
77
|
+
const wasOpen = runtime.remoteEpisode === true;
|
|
78
|
+
const transient = runtime.episodeSource === 'proxy' || runtime.episodeSource === 'tunnel';
|
|
79
|
+
runtime.remoteEpisode = false;
|
|
80
|
+
runtime.remoteFailures = 0;
|
|
81
|
+
if (!wasOpen)
|
|
82
|
+
return;
|
|
83
|
+
runtime.episodeSource = null;
|
|
84
|
+
if (transient)
|
|
85
|
+
runtime.episodeCooldownUntil = Date.now() + REMOTE_EPISODE_COOLDOWN_MS;
|
|
86
|
+
ctx.bus.publish({ kind: 'recovered', hostId: runtime.id, source });
|
|
87
|
+
}
|
|
88
|
+
function scheduleRemoteCheck(ctx, runtime, source, delayMs) {
|
|
89
|
+
if (runtime.remoteCheckTimer)
|
|
90
|
+
return;
|
|
91
|
+
runtime.remoteCheckTimer = setTimeout(() => {
|
|
92
|
+
runtime.remoteCheckTimer = null;
|
|
93
|
+
void checkRemoteNow(ctx, runtime, source);
|
|
94
|
+
}, delayMs);
|
|
95
|
+
runtime.remoteCheckTimer.unref?.();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Probe the connected host once and feed the consecutive-failure counter. One
|
|
99
|
+
* failed request is not a restart: the counter has to reach
|
|
100
|
+
* {@link REMOTE_UNREACHABLE_FAILURES} before a re-attach is requested.
|
|
101
|
+
*/
|
|
102
|
+
async function checkRemoteNow(ctx, runtime, source) {
|
|
103
|
+
if (!watchable(ctx, runtime) || typeof ctx.deps.remoteStatus !== 'function')
|
|
104
|
+
return;
|
|
105
|
+
const ssh = runtime.ssh;
|
|
106
|
+
const port = runtime.remote.port;
|
|
107
|
+
let status = null;
|
|
108
|
+
try {
|
|
109
|
+
status = await ctx.deps.remoteStatus(ssh, port);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
// swallow-ok: an unreachable probe is a failed probe, handled by the same counter
|
|
113
|
+
status = null;
|
|
114
|
+
}
|
|
115
|
+
// The transport may have gone away while the probe was in flight; the close handler owns that.
|
|
116
|
+
if (!watchable(ctx, runtime) || runtime.ssh !== ssh)
|
|
117
|
+
return;
|
|
118
|
+
if (isUiUp(status)) {
|
|
119
|
+
clearRemoteEpisode(ctx, runtime, source);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
runtime.remoteFailures = (Number(runtime.remoteFailures) || 0) + 1;
|
|
123
|
+
ctx.progress.log(runtime, 'debug', `remote heartbeat: probe failed (HTTP ${status ?? 'none'}), ${runtime.remoteFailures}/${REMOTE_UNREACHABLE_FAILURES} (${source})`);
|
|
124
|
+
beginRemoteEpisode(ctx, runtime, source);
|
|
125
|
+
if (runtime.remoteFailures < REMOTE_UNREACHABLE_FAILURES) {
|
|
126
|
+
scheduleRemoteCheck(ctx, runtime, source, ctx.timers.remoteRecheckMs);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
ctx.progress.log(runtime, 'warn', `remote-unreachable: ${runtime.remoteFailures} consecutive probes failed (${source}); re-attaching`);
|
|
130
|
+
ctx.onRemoteUnreachable?.(runtime, {
|
|
131
|
+
phase: 'confirmed',
|
|
132
|
+
source,
|
|
133
|
+
failures: runtime.remoteFailures,
|
|
134
|
+
});
|
|
135
|
+
cancelRemoteCheck(runtime);
|
|
136
|
+
ctx.bus.publish({ kind: 'confirmed', hostId: runtime.id, reason: `probe-failed-${runtime.remoteFailures}`, source });
|
|
137
|
+
}
|
|
138
|
+
/** One scheduled heartbeat tick. */
|
|
139
|
+
export async function remoteHeartbeatTick(ctx, runtime) {
|
|
140
|
+
if (!watchable(ctx, runtime))
|
|
141
|
+
return;
|
|
142
|
+
if (runtime.remoteCheckTimer)
|
|
143
|
+
return;
|
|
144
|
+
await checkRemoteNow(ctx, runtime, 'heartbeat');
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A proxy or tunnel failure was observed: report the episode once and confirm it
|
|
148
|
+
* with probes instead of treating one failed request as a remote restart.
|
|
149
|
+
*/
|
|
150
|
+
export function noteRemoteUnreachable(ctx, runtime, source) {
|
|
151
|
+
if (!watchable(ctx, runtime))
|
|
152
|
+
return;
|
|
153
|
+
if (!beginRemoteEpisode(ctx, runtime, source))
|
|
154
|
+
return;
|
|
155
|
+
scheduleRemoteCheck(ctx, runtime, source, ctx.timers.remoteConfirmDelayMs);
|
|
156
|
+
}
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// The Cordis loader resolves a linked plugin by its directory entry (`<dir>/index.js`), not
|
|
2
|
+
// through package exports, so a `link:` install needs this file to exist. It is a one-line shim:
|
|
3
|
+
// the implementation is the build output in `dist/`, and `exports["."]` names the same file for
|
|
4
|
+
// consumers that resolve by exports.
|
|
5
|
+
export { apply, inject, name, optional } from './dist/index.js'
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "dsh-remote-go",
|
|
4
|
-
"description": "Use a remote DSH over SSH inside your local DSH: the sidebar host bar switches between Local and each host, the remote UI takes over the whole window and comes back without reloading; the connect flow shows staged progress and a live log, and you can restart or stop the remote DSH, download remote files and folders, and
|
|
4
|
+
"description": "Use a remote DSH over SSH inside your local DSH: the sidebar host bar switches between Local and each host, the remote UI takes over the whole window and comes back without reloading; the connect flow shows staged progress and a live log, and you can restart or stop the remote DSH, download remote files and folders, and give local AI sessions a host list, a diagnosis report and a guarded remote command (hidden until the session unlocks them). The remote runs on a plugin-managed Node with npx pinned to the local version, keeping its data in the remote ~/.dsh."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "dsh-remote-go",
|
|
4
|
-
"description": "在本地 DSH 里通过 SSH 使用远端 DSH:侧边栏主机条在「本地」与各主机间切换,远端界面整窗接管、切回不重载;连接过程有阶段进度和实时日志,可重启/关闭远端 DSH、下载远端文件与文件夹,并向本地 AI
|
|
4
|
+
"description": "在本地 DSH 里通过 SSH 使用远端 DSH:侧边栏主机条在「本地」与各主机间切换,远端界面整窗接管、切回不重载;连接过程有阶段进度和实时日志,可重启/关闭远端 DSH、下载远端文件与文件夹,并向本地 AI 会话提供主机清单、体检报告与受控远端命令(默认隐藏,按需解锁)。远端由插件自带的 Node + npx 按本机版本启动,数据留在远端 ~/.dsh。"
|
|
5
5
|
}
|
|
6
6
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-remote-go",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DSH 插件:在本地 DSH 里通过 SSH 使用远端 DSH
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "DSH 插件:在本地 DSH 里通过 SSH 使用远端 DSH(主机条切换、整窗接管、连接进度与日志、重启/关闭远端、远端文件下载、给本地 AI 会话的主机清单 / 体检报告 / 受控远端命令,三者默认隐藏按需解锁;插件自带 Node + npx,数据留在远端 ~/.dsh) | DSH plugin: use a remote DSH over SSH inside your local DSH (host switcher, full-window UI, connect progress and log, remote restart/stop, remote file downloads, and a host list / diagnosis report / guarded remote command for local AI sessions — hidden until the session unlocks them; plugin-managed Node + npx, data stays in the remote ~/.dsh)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"deepseek",
|
|
7
7
|
"harness",
|
|
@@ -28,19 +28,18 @@
|
|
|
28
28
|
"node": ">=24"
|
|
29
29
|
},
|
|
30
30
|
"sideEffects": [
|
|
31
|
-
"./client.js"
|
|
31
|
+
"./dist/client.js"
|
|
32
32
|
],
|
|
33
|
+
"main": "./index.js",
|
|
33
34
|
"exports": {
|
|
34
35
|
".": "./index.js",
|
|
35
|
-
"./client": "./client.js",
|
|
36
|
+
"./client": "./dist/client.js",
|
|
36
37
|
"./locale/*.json": "./locale/*.json",
|
|
37
38
|
"./package.json": "./package.json"
|
|
38
39
|
},
|
|
39
40
|
"files": [
|
|
40
41
|
"index.js",
|
|
41
|
-
"
|
|
42
|
-
"src",
|
|
43
|
-
"vendor/ssh2.bundle.cjs",
|
|
42
|
+
"dist",
|
|
44
43
|
"vendor/THIRD_PARTY_NOTICES.md",
|
|
45
44
|
"locale",
|
|
46
45
|
"cordis.patch.yml",
|
|
@@ -66,36 +65,55 @@
|
|
|
66
65
|
},
|
|
67
66
|
"dependencies": {},
|
|
68
67
|
"devDependencies": {
|
|
69
|
-
"
|
|
68
|
+
"@types/node": "^24.19.2",
|
|
69
|
+
"@types/react": "^19.3.0",
|
|
70
|
+
"esbuild": "0.28.2",
|
|
70
71
|
"js-yaml": "^5.5.0",
|
|
71
72
|
"prettier": "^3.10.1",
|
|
72
73
|
"puppeteer-core": "^25.17.0",
|
|
73
74
|
"react": "^19.3.1",
|
|
74
75
|
"react-dom": "^19.3.1",
|
|
75
|
-
"ssh2": "1.17.0"
|
|
76
|
+
"ssh2": "1.17.0",
|
|
77
|
+
"typescript": "^5.9.3"
|
|
76
78
|
},
|
|
77
79
|
"scripts": {
|
|
78
|
-
"
|
|
79
|
-
"test
|
|
80
|
-
"
|
|
81
|
-
"test:
|
|
82
|
-
"
|
|
83
|
-
"test:
|
|
84
|
-
"
|
|
80
|
+
"pretest": "pnpm build",
|
|
81
|
+
"test": "node --test \"test/unit/*.test.ts\"",
|
|
82
|
+
"pretest:unit": "pnpm build",
|
|
83
|
+
"test:unit": "node --test \"test/unit/*.test.ts\"",
|
|
84
|
+
"pretest:web": "pnpm build",
|
|
85
|
+
"test:web": "node --test \"test/integration/web.e2e.ts\"",
|
|
86
|
+
"pretest:desktop": "pnpm build",
|
|
87
|
+
"test:desktop": "node --test \"test/integration/desktop.e2e.ts\"",
|
|
88
|
+
"pretest:integration": "pnpm build",
|
|
89
|
+
"test:integration": "node --test --test-concurrency=1 \"test/integration/*.test.ts\" \"test/integration/*.e2e.ts\"",
|
|
90
|
+
"pretest:integration:local": "pnpm build",
|
|
91
|
+
"test:integration:local": "node --test --test-concurrency=1 test/integration/sim.test.ts test/integration/remote-restart.test.ts test/integration/stop-remote.test.ts",
|
|
92
|
+
"pretest:integration:external": "pnpm build",
|
|
93
|
+
"test:integration:external": "node --test --test-concurrency=1 test/integration/client-ui.test.ts test/integration/remote-proxy.test.ts test/integration/web.e2e.ts test/integration/browser-carrier.test.ts test/integration/guest-tools.e2e.ts test/integration/stop-remote.browser.test.ts",
|
|
94
|
+
"presim:remote": "pnpm build",
|
|
85
95
|
"sim:remote": "node e2e/sim/simulated-remote.mjs --self-test",
|
|
96
|
+
"pretest:coverage": "pnpm build",
|
|
86
97
|
"test:coverage": "node scripts/check-coverage.mjs",
|
|
87
98
|
"lint": "node scripts/check-repo.mjs",
|
|
88
99
|
"check:repo": "node scripts/check-repo.mjs",
|
|
100
|
+
"check:arch": "node scripts/check-architecture.mjs",
|
|
89
101
|
"check:syntax": "node scripts/check-syntax.mjs",
|
|
90
102
|
"check:workflows": "node scripts/check-workflows.mjs",
|
|
91
103
|
"check:pr-title": "node scripts/check-pr-title.mjs",
|
|
92
104
|
"format": "prettier --write .",
|
|
93
105
|
"format:check": "prettier --check .",
|
|
94
|
-
"gate": "node scripts/check-repo.mjs && prettier --check . && node scripts/check-syntax.mjs && node scripts/check-workflows.mjs && node scripts/pack-check.mjs && node scripts/check-coverage.mjs",
|
|
95
|
-
"verify": "node scripts/check-repo.mjs && prettier --check . && node scripts/check-syntax.mjs && node scripts/check-workflows.mjs && node scripts/pack-check.mjs && node scripts/check-coverage.mjs && node --test --test-concurrency=1 \"test/integration/*.test.
|
|
106
|
+
"gate": "pnpm build && node scripts/check-repo.mjs && node scripts/check-architecture.mjs && prettier --check . && node scripts/check-syntax.mjs && node scripts/check-workflows.mjs && node scripts/pack-check.mjs && node scripts/verify-artifacts.mjs && pnpm run verify:types && node scripts/check-coverage.mjs && pnpm pack --pack-destination artifacts && node scripts/pack-check.mjs --find artifacts",
|
|
107
|
+
"verify": "pnpm build && node scripts/check-repo.mjs && node scripts/check-architecture.mjs && prettier --check . && node scripts/check-syntax.mjs && node scripts/check-workflows.mjs && node scripts/pack-check.mjs && node scripts/verify-artifacts.mjs && pnpm run verify:types && node scripts/check-coverage.mjs && node --test --test-concurrency=1 \"test/integration/*.test.ts\" \"test/integration/*.e2e.ts\"",
|
|
108
|
+
"build": "node scripts/build.mjs && node scripts/build-client.mjs",
|
|
109
|
+
"clean": "node scripts/clean.mjs",
|
|
110
|
+
"build:client": "node scripts/build-client.mjs",
|
|
111
|
+
"typecheck:client": "node node_modules/typescript/bin/tsc -p client/tsconfig.json --noEmit",
|
|
112
|
+
"verify:artifacts": "node scripts/verify-artifacts.mjs",
|
|
113
|
+
"verify:types": "node scripts/build.mjs --noEmit && pnpm run typecheck:client && node scripts/check-types.mjs --stage P7 --strict",
|
|
96
114
|
"build:vendor": "node scripts/build-vendor.mjs",
|
|
97
115
|
"pack:check": "node scripts/pack-check.mjs",
|
|
98
116
|
"pack:check:tarball": "node scripts/pack-check.mjs --find artifacts",
|
|
99
|
-
"prepack": "node scripts/build-vendor.mjs && git diff --exit-code -- vendor/"
|
|
117
|
+
"prepack": "node scripts/build-vendor.mjs && git diff --exit-code -- vendor/ && pnpm build && node scripts/verify-artifacts.mjs"
|
|
100
118
|
}
|
|
101
119
|
}
|
package/src/admission.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DSH's own admission for the routes this plugin serves.
|
|
3
|
-
*
|
|
4
|
-
* The kernel decides whether a request may reach a Host feature at all: its `Host`/`Origin` fence
|
|
5
|
-
* plus browser authentication against a live Peer (`ctx.connection.requestRejection`). This module
|
|
6
|
-
* is the one place that turns that service into a per-request function, so the local API and the
|
|
7
|
-
* same-origin proxy share the gate and both failure modes stay testable.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Build the admission gate for one plugin activation.
|
|
12
|
-
*
|
|
13
|
-
* `connection` is optional and has to be read through `ctx.get`: Cordis throws when a plugin reads
|
|
14
|
-
* a service its fiber did not inject, and an older kernel may not provide the service at all. In
|
|
15
|
-
* both cases the gate returns `undefined`, which hands the request to the caller's own fence
|
|
16
|
-
* instead of going dark. A gate that throws while judging a request returns `500` — failing closed,
|
|
17
|
-
* because a check that cannot decide must never admit.
|
|
18
|
-
*
|
|
19
|
-
* @param {object} ctx - plugin context; only `ctx.get` is read, and it may be missing in tests.
|
|
20
|
-
* @param {{ warn?: (...args: any[]) => void }} [logger]
|
|
21
|
-
* @returns {(req: { headers: object }) => (number | undefined)} rejection status, or undefined to admit.
|
|
22
|
-
*/
|
|
23
|
-
export function createAdmission(ctx, logger = console) {
|
|
24
|
-
const warn = typeof logger.warn === 'function' ? logger.warn.bind(logger) : () => {}
|
|
25
|
-
|
|
26
|
-
return (req) => {
|
|
27
|
-
let connection = null
|
|
28
|
-
try {
|
|
29
|
-
connection = typeof ctx?.get === 'function' ? ctx.get('connection') : null
|
|
30
|
-
} catch (error) {
|
|
31
|
-
// A failed lookup says nothing about this request, so the caller's own fence decides it.
|
|
32
|
-
warn('[dsh-remote-go] DSH connection lookup failed; the local fence stays the only gate:', error)
|
|
33
|
-
return undefined
|
|
34
|
-
}
|
|
35
|
-
if (!connection || typeof connection.requestRejection !== 'function') return undefined
|
|
36
|
-
try {
|
|
37
|
-
return connection.requestRejection(req)
|
|
38
|
-
} catch (error) {
|
|
39
|
-
warn('[dsh-remote-go] DSH connection admission failed; rejecting the request:', error)
|
|
40
|
-
return 500
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|