dsh-remote-go 0.2.0 → 0.3.1
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} +7144 -8367
- package/dist/index.js +4 -0
- package/dist/src/admission.js +21 -0
- package/dist/src/api.js +380 -0
- package/dist/src/atomic-json.js +201 -0
- package/dist/src/bootstrap.js +96 -0
- package/dist/src/connect/attach.js +21 -0
- package/dist/src/connect/bootstrap.js +8 -0
- package/dist/src/connect/failures.js +29 -0
- package/dist/src/connect/guards.js +36 -0
- package/dist/src/connect/launch.js +70 -0
- package/dist/src/connect/links.js +54 -0
- package/dist/src/connect/ownership.js +139 -0
- package/dist/src/connect/pipeline.js +376 -0
- package/dist/src/connect/reattach.js +191 -0
- package/dist/src/connect/registry.js +10 -0
- package/dist/src/connect/repair.js +97 -0
- package/dist/src/connect/restart.js +255 -0
- package/dist/src/connect/stop.js +164 -0
- package/dist/src/connect/transport.js +101 -0
- package/dist/src/connect/verify.js +30 -0
- package/dist/src/connect/watchdog.js +44 -0
- package/dist/src/connect-log.js +137 -0
- package/dist/src/connect-progress.js +110 -0
- package/dist/src/constants.js +29 -0
- package/dist/src/context.js +96 -0
- package/dist/src/diagnostics.js +133 -0
- package/dist/src/download-jobs.js +221 -0
- package/dist/src/errors.js +228 -0
- package/dist/src/events.js +326 -0
- package/dist/src/host-lifecycle.js +368 -0
- package/dist/src/host-registry.js +181 -0
- package/dist/src/host-runtime.js +77 -0
- package/dist/src/host-view.js +52 -0
- package/dist/src/index.js +196 -0
- package/dist/src/kernel-adapter.js +388 -0
- package/dist/src/key-files.js +39 -0
- package/dist/src/known-hosts.js +104 -0
- package/dist/src/layout.js +26 -0
- package/dist/src/local-download.js +311 -0
- package/dist/src/local-ssh.js +23 -0
- package/dist/src/local-version.js +87 -0
- package/dist/src/node/download.js +157 -0
- package/dist/src/node-runtime.js +327 -0
- package/dist/src/probe.js +95 -0
- package/dist/src/prompts.js +198 -0
- package/dist/src/proxy/cookies.js +102 -0
- package/dist/src/proxy/headers.js +167 -0
- package/dist/src/proxy/http.js +134 -0
- package/dist/src/proxy/index.js +121 -0
- package/dist/src/proxy/limits.js +6 -0
- package/dist/src/proxy/routes.js +32 -0
- package/dist/src/proxy/upgrade.js +160 -0
- package/dist/src/redact.js +85 -0
- package/dist/src/remote-files.js +60 -0
- package/dist/src/remote-path.js +82 -0
- package/dist/src/remote-proc.js +271 -0
- package/dist/src/remote-web/registry.js +107 -0
- package/dist/src/remote-web/resolve.js +229 -0
- package/dist/src/remote-web/start.js +190 -0
- package/dist/src/remote-web/stop.js +74 -0
- package/dist/src/request-trust.js +70 -0
- package/dist/src/runtime/dispatch.js +200 -0
- package/dist/src/runtime/effects.js +81 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +70 -0
- package/dist/src/runtime/rules.js +480 -0
- package/dist/src/secrets.js +119 -0
- package/dist/src/service.js +300 -0
- package/dist/src/settings.js +143 -0
- package/dist/src/ssh-config.js +257 -0
- package/dist/src/ssh-failures.js +71 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +258 -0
- package/dist/src/ssh.js +395 -0
- package/dist/src/store.js +174 -0
- package/dist/src/tool-shared.js +146 -0
- package/dist/src/tools-exec-policy.js +121 -0
- package/dist/src/tools-exec.js +307 -0
- package/dist/src/tools-report.js +357 -0
- package/dist/src/tools.js +337 -0
- package/dist/src/tunnel.js +111 -0
- package/dist/src/util.js +112 -0
- package/dist/src/version.js +108 -0
- package/dist/src/watchdog-bus.js +21 -0
- package/dist/src/watchdog.js +116 -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/constants.js +0 -93
- 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
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** The host registry: configured hosts, their runtime objects and the client-facing snapshot; reads are pure, writes
|
|
2
|
+
* re-emit the snapshot after `store`. */
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { errors } from './errors.js';
|
|
6
|
+
import { createHostRuntime } from './host-runtime.js';
|
|
7
|
+
import { hostView } from './host-view.js';
|
|
8
|
+
import { listKeyFiles as listKeyFilesInHome, OUTSIDE_HOME } from './key-files.js';
|
|
9
|
+
import { parseKnownHosts, summarizeKnownHosts } from './known-hosts.js';
|
|
10
|
+
import { stopRemoteHeartbeat } from './host-lifecycle.js';
|
|
11
|
+
import { dispatch, scopeFactsFor } from './runtime/dispatch.js';
|
|
12
|
+
import { createRuntimeEffects } from './runtime/effects.js';
|
|
13
|
+
import { discoverSshAliases as discoverAliasesFromConfig, parseSshConfig } from './ssh-config.js';
|
|
14
|
+
import { startRemoteHeartbeat } from './watchdog.js';
|
|
15
|
+
function ensureRuntime(ctx, host) {
|
|
16
|
+
const existing = ctx.state.runtimes.get(host.id);
|
|
17
|
+
if (existing) {
|
|
18
|
+
existing.entry = host;
|
|
19
|
+
return existing;
|
|
20
|
+
}
|
|
21
|
+
// The store issues the id and the map is keyed by it; an entry without one can only come from a
|
|
22
|
+
// caller that never saved it, never from `listHosts`.
|
|
23
|
+
const runtime = createHostRuntime(host);
|
|
24
|
+
ctx.state.runtimes.set(host.id, runtime);
|
|
25
|
+
return runtime;
|
|
26
|
+
}
|
|
27
|
+
export function runtimeOf(ctx, id) {
|
|
28
|
+
const runtime = ctx.state.runtimes.get(id);
|
|
29
|
+
if (!runtime)
|
|
30
|
+
throw errors.invalidInput('id', { zh: `未知主机 ${id}`, en: `Unknown host ${id}` });
|
|
31
|
+
return runtime;
|
|
32
|
+
}
|
|
33
|
+
export async function initRegistry(ctx) {
|
|
34
|
+
if (ctx.state.initialized)
|
|
35
|
+
return;
|
|
36
|
+
await ctx.store.load();
|
|
37
|
+
for (const host of ctx.store.listHosts())
|
|
38
|
+
ensureRuntime(ctx, host);
|
|
39
|
+
// `passwordSaved` is a fact about the credential store, not this process: without this read the
|
|
40
|
+
// Client would ask again after every DSH restart even though the password is saved.
|
|
41
|
+
if (ctx.secrets) {
|
|
42
|
+
await Promise.all([...ctx.state.runtimes.values()].map(async (runtime) => {
|
|
43
|
+
if (runtime.entry.auth?.type !== 'password') {
|
|
44
|
+
runtime.passwordSaved = false;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
runtime.passwordSaved = await ctx.secrets.has(runtime.id);
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
ctx.state.initialized = true;
|
|
51
|
+
}
|
|
52
|
+
function hostViewFor(ctx, runtime, options = {}) {
|
|
53
|
+
return hostView(runtime, {
|
|
54
|
+
targetVersion: ctx.localVersion(),
|
|
55
|
+
passwordStorageAvailable: ctx.secrets ? ctx.secrets.available() === true : false,
|
|
56
|
+
storeCorrupt: ctx.store?.hostsCorrupt === true,
|
|
57
|
+
}, options);
|
|
58
|
+
}
|
|
59
|
+
export function snapshot(ctx, options = {}) {
|
|
60
|
+
const localVersion = ctx.localVersion();
|
|
61
|
+
const hosts = [...ctx.state.runtimes.values()].map((runtime) => hostViewFor(ctx, runtime, options));
|
|
62
|
+
return {
|
|
63
|
+
hostVersion: localVersion,
|
|
64
|
+
targetVersion: localVersion,
|
|
65
|
+
hosts,
|
|
66
|
+
settings: ctx.store.getSettings(),
|
|
67
|
+
activeHostId: ctx.state.activeHostId,
|
|
68
|
+
firstRun: hosts.length === 0,
|
|
69
|
+
passwordStorageAvailable: ctx.secrets ? ctx.secrets.available() === true : false,
|
|
70
|
+
storeCorrupt: ctx.store?.hostsCorrupt === true,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export async function saveHost(ctx, rawHost) {
|
|
74
|
+
await ctx.init();
|
|
75
|
+
const previousAuth = rawHost && typeof rawHost === 'object' && typeof rawHost.id === 'string'
|
|
76
|
+
? (ctx.store.getHost(rawHost.id)?.auth?.type ?? null)
|
|
77
|
+
: null;
|
|
78
|
+
let entry;
|
|
79
|
+
try {
|
|
80
|
+
entry = await ctx.store.upsertHost(rawHost, { settings: ctx.store.getSettings() });
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
84
|
+
if (message === 'address is required') {
|
|
85
|
+
throw errors.invalidInput('address', {
|
|
86
|
+
zh: '请填写服务器地址或 ~/.ssh/config 别名。',
|
|
87
|
+
en: 'Enter a server address or an ~/.ssh/config alias.',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (message === 'address is invalid') {
|
|
91
|
+
throw errors.invalidInput('address', { zh: '服务器地址格式不正确。', en: 'The server address is not valid.' });
|
|
92
|
+
}
|
|
93
|
+
throw errors.invalidInput('host', { zh: `主机配置不合法:${message}`, en: `Invalid host entry: ${message}` });
|
|
94
|
+
}
|
|
95
|
+
const runtime = ensureRuntime(ctx, entry);
|
|
96
|
+
if (previousAuth === 'password' && entry.auth?.type !== 'password') {
|
|
97
|
+
await ctx.secrets?.remove(entry.id);
|
|
98
|
+
runtime.passwordSaved = false;
|
|
99
|
+
runtime.password = null;
|
|
100
|
+
}
|
|
101
|
+
ctx.emitState();
|
|
102
|
+
return hostViewFor(ctx, runtime);
|
|
103
|
+
}
|
|
104
|
+
/** Delete one host. The teardown needs the watchdog's five slices and the deletion needs `Store`, so the gate registers
|
|
105
|
+
* this function in `godCtxParams` rather than pretending it is narrow. */
|
|
106
|
+
export async function removeHost(ctx, id) {
|
|
107
|
+
await ctx.init();
|
|
108
|
+
const runtime = ctx.state.runtimes.get(id);
|
|
109
|
+
if (runtime) {
|
|
110
|
+
// A deleted host must not be resurrected by a timer, and a connect pipeline still in flight must
|
|
111
|
+
// see that the user no longer wants it: it may never publish a connected state afterwards.
|
|
112
|
+
if (ctx.state.activeHostId === id)
|
|
113
|
+
ctx.state.activeHostId = null;
|
|
114
|
+
// `host.removed` owns the desired flag, the token bump, the timer disarms, the prompt settlement
|
|
115
|
+
// and the teardown; the frame is published below with the host already gone.
|
|
116
|
+
const event = { type: 'host.removed', keepAlive: runtime.entry.keepAlive };
|
|
117
|
+
await dispatch(createRuntimeEffects(ctx), runtime, event, scopeFactsFor(ctx, runtime, event), {
|
|
118
|
+
caller: 'silent',
|
|
119
|
+
emit: false,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
ctx.state.runtimes.delete(id);
|
|
123
|
+
await ctx.store.removeHost(id);
|
|
124
|
+
await ctx.secrets?.remove(id);
|
|
125
|
+
if (ctx.state.activeHostId === id)
|
|
126
|
+
ctx.state.activeHostId = null;
|
|
127
|
+
ctx.emitState();
|
|
128
|
+
return { ok: true };
|
|
129
|
+
}
|
|
130
|
+
export function setActiveHost(ctx, id) {
|
|
131
|
+
if (id !== null && !ctx.state.runtimes.has(id))
|
|
132
|
+
throw errors.invalidInput('id');
|
|
133
|
+
ctx.state.activeHostId = id;
|
|
134
|
+
ctx.emitState();
|
|
135
|
+
}
|
|
136
|
+
export async function saveSettings(ctx, patch) {
|
|
137
|
+
await ctx.init();
|
|
138
|
+
const settings = await ctx.store.saveSettings(patch);
|
|
139
|
+
for (const runtime of ctx.state.runtimes.values()) {
|
|
140
|
+
if (runtime.status !== 'connected')
|
|
141
|
+
continue;
|
|
142
|
+
if (settings.remoteHeartbeat.enabled)
|
|
143
|
+
startRemoteHeartbeat(ctx, runtime);
|
|
144
|
+
else
|
|
145
|
+
stopRemoteHeartbeat(runtime);
|
|
146
|
+
}
|
|
147
|
+
ctx.emitState();
|
|
148
|
+
return settings;
|
|
149
|
+
}
|
|
150
|
+
export async function listKeyFiles(ctx, dir) {
|
|
151
|
+
// The picker is rooted at the OS user home, not DSH_HOME: private keys live in `~/.ssh` of the
|
|
152
|
+
// account, while the service's `home` is the DSH state root.
|
|
153
|
+
const userHome = homedir();
|
|
154
|
+
let listing;
|
|
155
|
+
try {
|
|
156
|
+
listing = await listKeyFilesInHome({ home: userHome, dir: typeof dir === 'string' ? dir : '' });
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
// A path outside the home directory (or an unreadable one) is the caller's mistake: answer 400
|
|
160
|
+
// instead of turning a bare `Error` into a 500, and never echo the path back in `technical`.
|
|
161
|
+
if (error?.code === OUTSIDE_HOME || ['ENOENT', 'EACCES', 'EPERM', 'ENOTDIR'].includes(error?.code)) {
|
|
162
|
+
throw errors.invalidInput('path', {
|
|
163
|
+
zh: '这个目录不在用户主目录内,或无法读取。',
|
|
164
|
+
en: 'This folder is outside the home directory, or cannot be read.',
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
const settings = ctx.store.getSettings();
|
|
170
|
+
return { ...listing, defaultKeyPath: settings.defaultKeyPath, defaultDir: path.join(userHome, '.ssh') };
|
|
171
|
+
}
|
|
172
|
+
export async function discoverSshAliases(ctx) {
|
|
173
|
+
const material = await ctx.deps.loadSshMaterial({ home: ctx.userHome() });
|
|
174
|
+
const parsed = parseSshConfig(material.configText);
|
|
175
|
+
const aliases = discoverAliasesFromConfig(parsed, { home: ctx.userHome() });
|
|
176
|
+
const knownHosts = parseKnownHosts(material.knownHostsText);
|
|
177
|
+
return {
|
|
178
|
+
aliases: aliases.map(({ alias, host, user, port }) => ({ alias, host, user, port, source: 'config' })),
|
|
179
|
+
knownHosts: summarizeKnownHosts(knownHosts),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export function emptyRemote() {
|
|
2
|
+
return {
|
|
3
|
+
version: null,
|
|
4
|
+
uiReady: false,
|
|
5
|
+
localPort: null,
|
|
6
|
+
port: null,
|
|
7
|
+
pid: null,
|
|
8
|
+
dshHome: null,
|
|
9
|
+
profile: null,
|
|
10
|
+
bootstrapLevel: null,
|
|
11
|
+
nodeVersion: null,
|
|
12
|
+
nodeSource: null,
|
|
13
|
+
startedAt: null,
|
|
14
|
+
viewRev: 0,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function createHostRuntime(host) {
|
|
18
|
+
return {
|
|
19
|
+
id: host.id,
|
|
20
|
+
entry: host,
|
|
21
|
+
status: 'unconfigured',
|
|
22
|
+
phase: null,
|
|
23
|
+
progress: null,
|
|
24
|
+
error: null,
|
|
25
|
+
warning: null,
|
|
26
|
+
warningSource: null,
|
|
27
|
+
fingerprint: host.pinnedFingerprint
|
|
28
|
+
? { sha256: host.pinnedFingerprint, keyType: 'pinned', status: 'pinned', pinned: host.pinnedFingerprint }
|
|
29
|
+
: null,
|
|
30
|
+
jumpFingerprint: null,
|
|
31
|
+
remote: emptyRemote(),
|
|
32
|
+
ssh: null,
|
|
33
|
+
jumpSsh: null,
|
|
34
|
+
tunnel: null,
|
|
35
|
+
token: null,
|
|
36
|
+
password: null,
|
|
37
|
+
savePassword: false,
|
|
38
|
+
forgetPassword: false,
|
|
39
|
+
passwordSaved: false,
|
|
40
|
+
facts: null,
|
|
41
|
+
remoteHome: null,
|
|
42
|
+
remotePid: null,
|
|
43
|
+
remoteProfile: null,
|
|
44
|
+
exchange: null,
|
|
45
|
+
pendingHostKey: null,
|
|
46
|
+
pendingConflict: null,
|
|
47
|
+
conflict: null,
|
|
48
|
+
desired: 'idle',
|
|
49
|
+
connectCancelled: false,
|
|
50
|
+
transportLost: false,
|
|
51
|
+
reconnectAttempts: 0,
|
|
52
|
+
reconnectTimer: null,
|
|
53
|
+
idleTimer: null,
|
|
54
|
+
connectPromise: null,
|
|
55
|
+
linkBusy: null,
|
|
56
|
+
linkToken: 0,
|
|
57
|
+
restartPromise: null,
|
|
58
|
+
reattachPromise: null,
|
|
59
|
+
launched: new Map(),
|
|
60
|
+
bound: null,
|
|
61
|
+
tunnelRepair: null,
|
|
62
|
+
tunnelRepairFailures: 0,
|
|
63
|
+
nextTunnelRepairAt: 0,
|
|
64
|
+
heartbeatTimer: null,
|
|
65
|
+
remoteCheckTimer: null,
|
|
66
|
+
remoteFailures: 0,
|
|
67
|
+
remoteEpisode: false,
|
|
68
|
+
episodeSource: null,
|
|
69
|
+
episodeCooldownUntil: 0,
|
|
70
|
+
alongsidePids: [],
|
|
71
|
+
restarts: [],
|
|
72
|
+
closing: false,
|
|
73
|
+
logs: [],
|
|
74
|
+
logSeq: 0,
|
|
75
|
+
connectCount: 0,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** The client-facing view of one host (docs/api.md HostView). */
|
|
2
|
+
import { versionParity } from './version.js';
|
|
3
|
+
import { connectLogTail, connectLogVisible } from './connect-log.js';
|
|
4
|
+
export function hostView(runtime, ctx, options = {}) {
|
|
5
|
+
const entry = runtime.entry;
|
|
6
|
+
return {
|
|
7
|
+
id: runtime.id,
|
|
8
|
+
name: entry.name,
|
|
9
|
+
address: entry.address,
|
|
10
|
+
// Presentation only: the editor must never parse this form back (a bare IPv6 such as
|
|
11
|
+
// `fe80::1` used to yield port 1 and silently replace the default port with 22).
|
|
12
|
+
addressMasked: maskAddress(entry),
|
|
13
|
+
username: entry.username ?? null,
|
|
14
|
+
port: entry.port ?? 22,
|
|
15
|
+
authType: entry.auth?.type ?? 'key',
|
|
16
|
+
authKeyPath: entry.auth?.type === 'key' && typeof entry.auth.keyPath === 'string' ? entry.auth.keyPath : null,
|
|
17
|
+
passwordSaved: entry.auth?.type === 'password' ? runtime.passwordSaved === true : false,
|
|
18
|
+
passwordStorageAvailable: ctx.passwordStorageAvailable,
|
|
19
|
+
jumpHost: entry.jumpHost ?? null,
|
|
20
|
+
status: runtime.status,
|
|
21
|
+
phase: runtime.phase,
|
|
22
|
+
progress: runtime.progress,
|
|
23
|
+
error: runtime.error,
|
|
24
|
+
warning: runtime.warning,
|
|
25
|
+
conflict: runtime.conflict,
|
|
26
|
+
fingerprint: runtime.fingerprint,
|
|
27
|
+
logs: options.logs === false ? [] : connectLogVisible(runtime.status) ? connectLogTail(runtime) : [],
|
|
28
|
+
logAttempt: typeof runtime.connectCount === 'number' ? runtime.connectCount : 0,
|
|
29
|
+
logSeq: typeof runtime.logSeq === 'number' ? runtime.logSeq : 0,
|
|
30
|
+
remote: { ...runtime.remote, version: runtime.remote.version ?? null },
|
|
31
|
+
keepAlive: entry.keepAlive,
|
|
32
|
+
idleMinutes: entry.idleMinutes,
|
|
33
|
+
autoReconnect: entry.autoReconnect,
|
|
34
|
+
hostKeyPolicy: entry.hostKeyPolicy,
|
|
35
|
+
lastOpenedAt: entry.lastOpenedAt,
|
|
36
|
+
createdAt: entry.createdAt,
|
|
37
|
+
updatedAt: entry.updatedAt,
|
|
38
|
+
targetVersion: ctx.targetVersion,
|
|
39
|
+
versionParity: versionParity(ctx.targetVersion, runtime.remote.version ?? null),
|
|
40
|
+
storeCorrupt: ctx.storeCorrupt === true,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** The display form of the endpoint (`user@host`, `user@host:2222`, bracketed IPv6); the port is omitted only for SSH's
|
|
44
|
+
* default 22, which is why the Client reads `port`/`username` instead of parsing this string. */
|
|
45
|
+
export function maskAddress(entry) {
|
|
46
|
+
const port = entry.port && entry.port !== 22 ? `:${entry.port}` : '';
|
|
47
|
+
const user = entry.username ? `${entry.username}@` : '';
|
|
48
|
+
const raw = entry.address || '?';
|
|
49
|
+
// An unbracketed address containing a colon is IPv6 in URL/SSH notation.
|
|
50
|
+
const host = raw.includes(':') && !(raw.startsWith('[') && raw.endsWith(']')) ? `[${raw}]` : raw;
|
|
51
|
+
return `${user}${host}${port}`;
|
|
52
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/** dsh-remote-go — Host half: registers the local HTTP API and the browser half's same-origin remote proxy, and owns
|
|
2
|
+
* the SSH / bootstrap / tunnel lifecycle; it never copies remote data. */
|
|
3
|
+
import { API_PREFIX, PLUGIN_ID, REMOTE_VIEW_PREFIX } from './constants.js';
|
|
4
|
+
import { statSync } from 'node:fs';
|
|
5
|
+
import { createAdmission } from './admission.js';
|
|
6
|
+
import { createApi } from './api.js';
|
|
7
|
+
import { KERNEL_PROBE_VERSION, createKernelAdapter } from './kernel-adapter.js';
|
|
8
|
+
import { detectLocalEnginesNode, detectLocalVersion } from './local-version.js';
|
|
9
|
+
import { createRemoteProxy } from './proxy/index.js';
|
|
10
|
+
import { RemoteGoService } from './service.js';
|
|
11
|
+
import { createSecretStore } from './secrets.js';
|
|
12
|
+
import { HostStore, resolveDshHome, resolveStateDir } from './store.js';
|
|
13
|
+
import { registerHostTools } from './tools.js';
|
|
14
|
+
import { messageOf } from './util.js';
|
|
15
|
+
export const name = PLUGIN_ID;
|
|
16
|
+
export const inject = ['webServer'];
|
|
17
|
+
export const optional = ['connection', 'credentials'];
|
|
18
|
+
export function apply(ctx, config = {}) {
|
|
19
|
+
const logger = createLogger();
|
|
20
|
+
// The kernel's own metadata (loader fiber, client-module registry) is read through one adapter, so
|
|
21
|
+
// `/diagnostics/self` and every future kernel probe share the same three-state answers.
|
|
22
|
+
const kernel = createKernelAdapter(ctx);
|
|
23
|
+
const home = config.home ? String(config.home) : resolveDshHome(process.env);
|
|
24
|
+
const stateDir = resolveStateDir(home);
|
|
25
|
+
const store = new HostStore({ dir: stateDir, logger });
|
|
26
|
+
const secrets = createSecretStore({ ctx, logger });
|
|
27
|
+
const service = new RemoteGoService({
|
|
28
|
+
store,
|
|
29
|
+
home,
|
|
30
|
+
logger,
|
|
31
|
+
secrets,
|
|
32
|
+
localVersion: config.localVersion ? String(config.localVersion) : null,
|
|
33
|
+
hostKeyConfirmTimeoutMs: config.hostKeyConfirmTimeoutMs,
|
|
34
|
+
});
|
|
35
|
+
const getOrigin = () => {
|
|
36
|
+
try {
|
|
37
|
+
return ctx.webServer?.getOrigin?.() ?? null;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
// DSH's own admission for every route this plugin serves; `connection` is optional, so the gate
|
|
44
|
+
// hands the request to the plugin's own fence when the service is absent (see admission.ts).
|
|
45
|
+
const admit = createAdmission(ctx, logger);
|
|
46
|
+
const api = createApi({
|
|
47
|
+
service,
|
|
48
|
+
getOrigin,
|
|
49
|
+
admit,
|
|
50
|
+
logger,
|
|
51
|
+
diagnostics: () => describeSelf(kernel, {
|
|
52
|
+
home,
|
|
53
|
+
stateDir,
|
|
54
|
+
localVersion: service.localVersion,
|
|
55
|
+
engineNodeRange: service.engineNodeRange,
|
|
56
|
+
storeCorrupt: store.hostsCorrupt,
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
// The browser half's remote window lives on the local origin, so the same-origin proxy is the only
|
|
60
|
+
// thing that needs to know how to reach a tunnel.
|
|
61
|
+
const proxy = createRemoteProxy({
|
|
62
|
+
webServer: ctx.webServer,
|
|
63
|
+
getOrigin,
|
|
64
|
+
admit,
|
|
65
|
+
resolveHost: (id) => service.webViewTarget(id),
|
|
66
|
+
onUpstreamError: (id) => service.noteUpstreamFailure(id),
|
|
67
|
+
logger,
|
|
68
|
+
});
|
|
69
|
+
ctx.effect(() => {
|
|
70
|
+
const disposeRoute = ctx.webServer.register({
|
|
71
|
+
kind: 'prefix',
|
|
72
|
+
path: API_PREFIX,
|
|
73
|
+
handler: (req, res) => {
|
|
74
|
+
void api.handler(req, res);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const disposeView = ctx.webServer.register({
|
|
78
|
+
kind: 'prefix',
|
|
79
|
+
path: proxy.routePath,
|
|
80
|
+
handler: (req, res) => proxy.handle(req, res),
|
|
81
|
+
});
|
|
82
|
+
const disposeTools = registerHostTools(ctx, {
|
|
83
|
+
service,
|
|
84
|
+
logger,
|
|
85
|
+
exec: { allowWrite: config.exec?.allowWrite === true },
|
|
86
|
+
});
|
|
87
|
+
const syncViewRoutes = () => proxy.sync(service.hostIds());
|
|
88
|
+
service.on('state', syncViewRoutes);
|
|
89
|
+
logger.info(`API mounted at ${API_PREFIX} (state dir ${stateDir})`);
|
|
90
|
+
logger.info(`remote view mounted at ${REMOTE_VIEW_PREFIX}<host> (browser carrier)`);
|
|
91
|
+
void (async () => {
|
|
92
|
+
try {
|
|
93
|
+
await service.init();
|
|
94
|
+
syncViewRoutes();
|
|
95
|
+
const detected = await detectLocalVersion({ ctx, logger });
|
|
96
|
+
if (detected.version)
|
|
97
|
+
service.setLocalVersion(detected.version, detected.source);
|
|
98
|
+
// The managed Node version is the lower bound of DSH's own `engines.node`, read here to stay
|
|
99
|
+
// in step with the running DSH build.
|
|
100
|
+
const engine = await detectLocalEnginesNode({ ctx });
|
|
101
|
+
if (engine.range)
|
|
102
|
+
service.setLocalEnginesNode(engine.range);
|
|
103
|
+
if (!detected.version)
|
|
104
|
+
logger.warn('could not detect the local DSH version; the remote npx runtime cannot be pinned to this machine, so connections will fail until the version is readable');
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
logger.error('initialization failed:', error);
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
110
|
+
return () => {
|
|
111
|
+
service.removeListener('state', syncViewRoutes);
|
|
112
|
+
// A failing disposer must not skip the rest of the unload: the proxy, the API and the SSH
|
|
113
|
+
// transports below still have to be released.
|
|
114
|
+
try {
|
|
115
|
+
disposeTools();
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
logger.error('unload: could not dispose the model tools:', error);
|
|
119
|
+
}
|
|
120
|
+
proxy.dispose();
|
|
121
|
+
api.dispose();
|
|
122
|
+
void service.shutdown();
|
|
123
|
+
disposeRoute();
|
|
124
|
+
disposeView();
|
|
125
|
+
logger.info('unloaded');
|
|
126
|
+
};
|
|
127
|
+
}, 'dsh-remote-go: http api + service');
|
|
128
|
+
return service;
|
|
129
|
+
}
|
|
130
|
+
/** The `/diagnostics/self` body: every kernel read goes through `kernel-adapter.ts`, whose three states map the same
|
|
131
|
+
* way everywhere (`ok` fills the field, `absent`/`changed` leave it `null` and `probes` says which). */
|
|
132
|
+
function describeSelf(kernel, info) {
|
|
133
|
+
const out = {
|
|
134
|
+
home: info.home,
|
|
135
|
+
stateDir: info.stateDir,
|
|
136
|
+
hostVersion: info.localVersion,
|
|
137
|
+
engineNodeRange: info.engineNodeRange ?? null,
|
|
138
|
+
storeCorrupt: info.storeCorrupt === true,
|
|
139
|
+
entry: null,
|
|
140
|
+
client: null,
|
|
141
|
+
kernel: { version: KERNEL_PROBE_VERSION, probes: kernel.probeReport() },
|
|
142
|
+
};
|
|
143
|
+
const entry = kernel.fiberEntry();
|
|
144
|
+
if (entry.state === 'ok' && entry.value !== null)
|
|
145
|
+
out.entry = { id: entry.value.id, name: entry.value.name };
|
|
146
|
+
const graph = kernel.clientModuleGraph();
|
|
147
|
+
const row = kernel.clientModuleRow(PLUGIN_ID);
|
|
148
|
+
const clientPathProbe = kernel.clientPath(PLUGIN_ID);
|
|
149
|
+
const clientPath = clientPathProbe.state === 'ok' ? clientPathProbe.value : null;
|
|
150
|
+
let bundle = null;
|
|
151
|
+
if (clientPath) {
|
|
152
|
+
try {
|
|
153
|
+
const stat = statSync(clientPath);
|
|
154
|
+
bundle = { path: clientPath, size: stat.size, mtimeMs: stat.mtimeMs };
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
bundle = { path: clientPath, error: messageOf(error) };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
out.client = {
|
|
161
|
+
graph: graph.state === 'ok' && graph.value !== null
|
|
162
|
+
? { rev: graph.value.rev, release: graph.value.release, entries: graph.value.entries }
|
|
163
|
+
: null,
|
|
164
|
+
row: row.state === 'ok' && row.value !== null
|
|
165
|
+
? {
|
|
166
|
+
id: row.value.id,
|
|
167
|
+
rev: row.value.rev,
|
|
168
|
+
url: row.value.url,
|
|
169
|
+
inject: row.value.inject,
|
|
170
|
+
immediately: row.value.immediately,
|
|
171
|
+
}
|
|
172
|
+
: null,
|
|
173
|
+
clientPath,
|
|
174
|
+
bundle,
|
|
175
|
+
};
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
function createLogger() {
|
|
179
|
+
const tag = '[dsh-remote-go]';
|
|
180
|
+
// The sink is resolved per call, not captured: an embedding shell may replace `console.*` after
|
|
181
|
+
// `apply()`, and a captured function would keep writing to the old one.
|
|
182
|
+
const write = (level, args) => {
|
|
183
|
+
try {
|
|
184
|
+
console[level]?.call(console, tag, ...args);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// swallow-ok: logging must never throw into the host
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
info: (...args) => write('info', args),
|
|
192
|
+
warn: (...args) => write('warn', args),
|
|
193
|
+
error: (...args) => write('error', args),
|
|
194
|
+
debug: (...args) => write('debug', args),
|
|
195
|
+
};
|
|
196
|
+
}
|