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,97 @@
|
|
|
1
|
+
/** The listener rebuild for a host whose remote UI answers the SSH probe but whose proxied requests fail: the tunnel, not the remote, is what broke. Single-flight per host and owned by a link-work token; the replacement is created before the old listener closes (no window without a listener) and its new random local port bumps `viewRev` so the carrier refetches. It reads five slices and never the persistence layer, so it takes `WatchdogCtx` rather than `ConnectCtx`. */
|
|
2
|
+
import { REMOTE_REPAIR_BACKOFF_MAX_MS, REMOTE_REPAIR_BACKOFF_MS } from '../constants.js';
|
|
3
|
+
import { errors } from '../errors.js';
|
|
4
|
+
import { beginLinkWork, endLinkWork, isLinkWorkCurrent } from '../host-lifecycle.js';
|
|
5
|
+
import { isPid, messageOf } from '../util.js';
|
|
6
|
+
import { noteRemoteUnreachable } from '../watchdog.js';
|
|
7
|
+
export async function repairTunnel(ctx, runtime) {
|
|
8
|
+
if (ctx.state.disposed === true ||
|
|
9
|
+
runtime.desired !== 'connected' ||
|
|
10
|
+
runtime.status !== 'connected' ||
|
|
11
|
+
runtime.linkBusy !== null ||
|
|
12
|
+
runtime.tunnel === null) {
|
|
13
|
+
return { ok: false, reason: 'not-watchable' };
|
|
14
|
+
}
|
|
15
|
+
if (runtime.ssh?.connected !== true)
|
|
16
|
+
return { ok: false, reason: 'no-transport' };
|
|
17
|
+
if (runtime.tunnelRepair)
|
|
18
|
+
return runtime.tunnelRepair;
|
|
19
|
+
if (Date.now() < (Number(runtime.nextTunnelRepairAt) || 0))
|
|
20
|
+
return { ok: false, reason: 'backoff' };
|
|
21
|
+
const port = runtime.remote.port;
|
|
22
|
+
const previous = runtime.tunnel;
|
|
23
|
+
const token = beginLinkWork(runtime, 'repair');
|
|
24
|
+
const stillCurrent = () => isLinkWorkCurrent(runtime, token) &&
|
|
25
|
+
ctx.state.disposed !== true &&
|
|
26
|
+
runtime.desired === 'connected' &&
|
|
27
|
+
runtime.status === 'connected' &&
|
|
28
|
+
runtime.ssh?.connected === true;
|
|
29
|
+
let created = null;
|
|
30
|
+
const settled = (async () => {
|
|
31
|
+
try {
|
|
32
|
+
if (runtime.ssh?.connected !== true)
|
|
33
|
+
throw errors.internal(new Error('the SSH transport is gone'));
|
|
34
|
+
if (!isPid(port) || port > 65535)
|
|
35
|
+
throw errors.internal(new Error(`invalid remote port for the tunnel: ${port}`));
|
|
36
|
+
const next = await ctx.deps.tunnelFactory({
|
|
37
|
+
ssh: runtime.ssh,
|
|
38
|
+
remotePort: port,
|
|
39
|
+
onError: () => noteRemoteUnreachable(ctx, runtime, 'tunnel'),
|
|
40
|
+
});
|
|
41
|
+
created = next;
|
|
42
|
+
const nextPort = Number(next?.localPort);
|
|
43
|
+
if (!Number.isInteger(nextPort) || !isPid(nextPort) || nextPort > 65535) {
|
|
44
|
+
throw errors.internal(new Error(`the rebuilt tunnel has no usable local port (${next?.localPort})`));
|
|
45
|
+
}
|
|
46
|
+
if (!stillCurrent())
|
|
47
|
+
return { ok: false, reason: 'cancelled' };
|
|
48
|
+
runtime.tunnel = next;
|
|
49
|
+
runtime.remote.localPort = nextPort;
|
|
50
|
+
try {
|
|
51
|
+
await previous.close();
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// swallow-ok: the old listener could already be gone; the new one is what matters
|
|
55
|
+
}
|
|
56
|
+
if (!stillCurrent())
|
|
57
|
+
return { ok: false, reason: 'cancelled' };
|
|
58
|
+
runtime.remote.viewRev = (Number(runtime.remote.viewRev) || 0) + 1;
|
|
59
|
+
ctx.progress.log(runtime, 'info', `tunnel rebuilt after a failed request (remote port ${port}, local port ${previous.localPort} -> ${nextPort})`);
|
|
60
|
+
runtime.tunnelRepairFailures = 0;
|
|
61
|
+
runtime.nextTunnelRepairAt = 0;
|
|
62
|
+
ctx.emitState();
|
|
63
|
+
return { ok: true };
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
// swallow-ok: a failed rebuild is a typed, retryable reason (with backoff) for the caller; the
|
|
67
|
+
// old listener is still serving, so this is not a connection failure.
|
|
68
|
+
runtime.tunnelRepairFailures = (Number(runtime.tunnelRepairFailures) || 0) + 1;
|
|
69
|
+
const backoff = Math.min(REMOTE_REPAIR_BACKOFF_MAX_MS, REMOTE_REPAIR_BACKOFF_MS * 2 ** (runtime.tunnelRepairFailures - 1));
|
|
70
|
+
runtime.nextTunnelRepairAt = Date.now() + backoff;
|
|
71
|
+
ctx.progress.log(runtime, 'warn', 'tunnel rebuild failed:', messageOf(error));
|
|
72
|
+
return { ok: false, reason: 'rebuild-failed' };
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
// A listener this rebuild created must never outlive it: close it on a cancelled token (or if it
|
|
76
|
+
// was never published), and clear the view only when it owned the slot.
|
|
77
|
+
if (created !== null && (!isLinkWorkCurrent(runtime, token) || runtime.tunnel !== created)) {
|
|
78
|
+
if (runtime.tunnel === created) {
|
|
79
|
+
runtime.tunnel = null;
|
|
80
|
+
runtime.remote.localPort = null;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await created.close();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// swallow-ok: the unpublished listener is discarded; its close cannot fail the repair
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
endLinkWork(runtime, token);
|
|
90
|
+
}
|
|
91
|
+
})().finally(() => {
|
|
92
|
+
if (runtime.tunnelRepair === settled)
|
|
93
|
+
runtime.tunnelRepair = null;
|
|
94
|
+
});
|
|
95
|
+
runtime.tunnelRepair = settled;
|
|
96
|
+
return settled;
|
|
97
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/** The local restart of the managed remote web on the same SSH transport: stop exactly the instance this connection launched, `[start]` again on the same plan, rebuild the tunnel for the new random port, verify and resume. */
|
|
2
|
+
import { REMOTE_START_TIMEOUT_MS } from '../constants.js';
|
|
3
|
+
import { errors, toErrorView } from '../errors.js';
|
|
4
|
+
import { closeTunnel, endLinkWork, isLinkWorkCurrent } from '../host-lifecycle.js';
|
|
5
|
+
import { layoutForRuntime } from '../layout.js';
|
|
6
|
+
import { buildRunRegistryRecord, registryNamesInstance } from '../remote-web/registry.js';
|
|
7
|
+
import { commandMatchesProfile } from '../remote-web/resolve.js';
|
|
8
|
+
import { dispatch, scopeFactsFor } from '../runtime/dispatch.js';
|
|
9
|
+
import { createRuntimeEffects } from '../runtime/effects.js';
|
|
10
|
+
import { isPid, messageOf, nowIso } from '../util.js';
|
|
11
|
+
import { markConnected, recordRestart, resumeConnected } from './attach.js';
|
|
12
|
+
import { planForRuntime } from './bootstrap.js';
|
|
13
|
+
import { SSH_CLOSED_DETAIL } from './failures.js';
|
|
14
|
+
import { LinkCancelled, linkActionState } from './guards.js';
|
|
15
|
+
import { PATCHED_CARRIER_NOTE, resolveForeignConflict } from './launch.js';
|
|
16
|
+
import { OWNERSHIP_OPTIONS, proveOwnership, rememberLaunched } from './ownership.js';
|
|
17
|
+
import { readRegistryQuietly } from './registry.js';
|
|
18
|
+
import { openTunnelFor } from './transport.js';
|
|
19
|
+
import { verifyRemoteUi } from './verify.js';
|
|
20
|
+
/** Restart the managed remote web on the same SSH transport; `[ssh]`/`[probe]`/`[bootstrap]` are not re-run, so no password or host-key prompt can appear here. Also available from the `remote-unreachable` error state, which keeps the SSH transport on purpose. */
|
|
21
|
+
export async function restartHost(ctx, runtime) {
|
|
22
|
+
await ctx.init();
|
|
23
|
+
if (runtime.connectPromise || runtime.linkBusy !== null)
|
|
24
|
+
return { accepted: false, reason: 'busy' };
|
|
25
|
+
// A live link, or the `remote-unreachable` error state that deliberately keeps the SSH transport.
|
|
26
|
+
if (linkActionState(runtime) === null)
|
|
27
|
+
throw errors.restartUnavailable();
|
|
28
|
+
// One transition owns `activity`/`epoch`, the `connecting` card and the action start (`run`), and adopts the token.
|
|
29
|
+
const token = (Number(runtime.linkToken) || 0) + 1;
|
|
30
|
+
const event = { type: 'restart.requested', token, pid: runtime.remote.pid, port: runtime.remote.port };
|
|
31
|
+
await dispatch(createRuntimeEffects(ctx), runtime, event, scopeFactsFor(ctx, runtime, event), {
|
|
32
|
+
caller: 'script',
|
|
33
|
+
run: (activity, started) => {
|
|
34
|
+
if (activity !== 'restart')
|
|
35
|
+
return;
|
|
36
|
+
const work = runRestart(ctx, runtime, started);
|
|
37
|
+
runtime.restartPromise = work;
|
|
38
|
+
void work.finally(() => {
|
|
39
|
+
endLinkWork(runtime, started);
|
|
40
|
+
if (runtime.restartPromise === work)
|
|
41
|
+
runtime.restartPromise = null;
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return { accepted: true };
|
|
46
|
+
}
|
|
47
|
+
/** The action body: proof of ownership, the conflict prompt, stop/start, the registry rewrite, the tunnel and the verify step all read the same six slices as its entry point. */
|
|
48
|
+
async function runRestart(ctx, runtime, token) {
|
|
49
|
+
const log = (level, ...args) => ctx.progress.log(runtime, level, ...args);
|
|
50
|
+
const phase = (name, code, params) => ctx.progress.phase(runtime, name, code, params);
|
|
51
|
+
const layout = layoutForRuntime(runtime);
|
|
52
|
+
const profile = runtime.remote.profile ?? ctx.settings().bootstrap.profile;
|
|
53
|
+
const previousPort = runtime.remote.port;
|
|
54
|
+
const hadTunnel = runtime.tunnel !== null;
|
|
55
|
+
let killable = false;
|
|
56
|
+
let stoppedOwn = false;
|
|
57
|
+
let started = false;
|
|
58
|
+
let tunnelCreated = false;
|
|
59
|
+
try {
|
|
60
|
+
log('info', `restart requested (pid=${runtime.remote.pid ?? 'unknown'}, remote port=${previousPort ?? 'unknown'})`);
|
|
61
|
+
// Stopping needs two independent proofs: the registry names this connection's pid, and this connection launched it
|
|
62
|
+
// with the same /proc id. A pid a re-attach found, or one kept alongside, is hosted, never killed.
|
|
63
|
+
const registry = await readRegistryQuietly(ctx, runtime, layout);
|
|
64
|
+
const managedPid = Number(runtime.remote.pid ?? 0);
|
|
65
|
+
if (registryNamesInstance(registry, layout, profile, managedPid)) {
|
|
66
|
+
const verdict = await proveOwnership(ctx, runtime, {
|
|
67
|
+
...OWNERSHIP_OPTIONS.restart,
|
|
68
|
+
pid: managedPid,
|
|
69
|
+
layout,
|
|
70
|
+
profile,
|
|
71
|
+
});
|
|
72
|
+
killable = verdict.ok === true;
|
|
73
|
+
if (verdict.ok !== true) {
|
|
74
|
+
const why = `restart: not stopping pid ${managedPid} (${verdict.reason}); only a process this connection launched is stopped`;
|
|
75
|
+
log('warn', why);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const which = isPid(managedPid) ? managedPid : 'unknown';
|
|
80
|
+
log('warn', `restart: the run registry does not name this connection's pid (${which}); leaving the running instance alone`);
|
|
81
|
+
}
|
|
82
|
+
// A live instance this connection cannot stop and did not launch is hosted, not raced: it goes through the conflict prompt.
|
|
83
|
+
let knownProcess = null;
|
|
84
|
+
const launchedHere = runtime.launched instanceof Map && runtime.launched.has(managedPid);
|
|
85
|
+
if (!killable && !launchedHere && isPid(managedPid) && runtime.ssh?.connected === true) {
|
|
86
|
+
const identity = await ctx.deps.processIdentity(runtime.ssh, managedPid);
|
|
87
|
+
if (identity.ok === true) {
|
|
88
|
+
const sameHome = identity.dshHome === '' || identity.dshHome === layout.dshHome;
|
|
89
|
+
if (sameHome && commandMatchesProfile(identity.command, profile))
|
|
90
|
+
knownProcess = { pid: managedPid, command: identity.command };
|
|
91
|
+
// Alive but not provable: asking is safer than starting a second instance.
|
|
92
|
+
}
|
|
93
|
+
else if (identity.reason === 'unreadable') {
|
|
94
|
+
knownProcess = { pid: managedPid, command: `dsh --profile ${profile}` };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
98
|
+
return;
|
|
99
|
+
// Ask about foreign instances BEFORE anything is stopped: cancelling the prompt must not destroy
|
|
100
|
+
// the instance the user is currently connected to.
|
|
101
|
+
const decision = await resolveForeignConflict(ctx, runtime, layout, profile, {
|
|
102
|
+
ignoredPids: Array.isArray(runtime.alongsidePids) ? runtime.alongsidePids : [],
|
|
103
|
+
knownProcess,
|
|
104
|
+
});
|
|
105
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
106
|
+
return;
|
|
107
|
+
if (decision === 'stop')
|
|
108
|
+
stoppedOwn = true;
|
|
109
|
+
if (killable) {
|
|
110
|
+
// A stop can take seconds; the link may have been dropped or replaced in the meantime.
|
|
111
|
+
const stopped = await ctx.deps.stopWeb(runtime.ssh, layout, { pid: managedPid });
|
|
112
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
113
|
+
return;
|
|
114
|
+
stoppedOwn = stopped?.ok === true;
|
|
115
|
+
if (stoppedOwn) {
|
|
116
|
+
log('info', `restart: stopped the managed remote web pid=${managedPid}`);
|
|
117
|
+
if (runtime.launched instanceof Map)
|
|
118
|
+
runtime.launched.delete(managedPid);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
// Never start a second instance next to one whose stop was not confirmed: the old tunnel still works, so the
|
|
122
|
+
// host stays usable. The warning is action-scoped; `connect.succeeded` clears it once a fresh attach replaces.
|
|
123
|
+
runtime.warning = {
|
|
124
|
+
zh: '远端受管 DSH 没有确认停止,本次没有启动新实例;请稍后重试,或先断开再重连。',
|
|
125
|
+
en: 'The managed remote DSH did not confirm its stop, so no new instance was started; retry shortly, or disconnect and reconnect.',
|
|
126
|
+
};
|
|
127
|
+
runtime.warningSource = 'restart-stop';
|
|
128
|
+
log('warn', `restart: the managed remote web did not confirm its stop; not launching a second instance (${String(stopped?.output ?? '').slice(0, 300) || 'no output'})`);
|
|
129
|
+
if (await resumeConnected(ctx, runtime, true))
|
|
130
|
+
return;
|
|
131
|
+
// The link changed under us, or there was no tunnel to keep (restart from the error state):
|
|
132
|
+
// never publish a fake "connected" state; explain instead of staying stuck.
|
|
133
|
+
if (isLinkWorkCurrent(runtime, token) && runtime.status === 'connecting') {
|
|
134
|
+
const failure = {
|
|
135
|
+
type: 'restart.failed',
|
|
136
|
+
phase: 'start',
|
|
137
|
+
view: toErrorView(errors.restartUnavailable({
|
|
138
|
+
zh: '远端受管 DSH 没有确认停止,本次没有启动新实例;请稍后重试。',
|
|
139
|
+
en: 'The managed remote DSH did not confirm its stop, so no new instance was started; retry shortly.',
|
|
140
|
+
})),
|
|
141
|
+
};
|
|
142
|
+
await dispatch(createRuntimeEffects(ctx), runtime, failure, scopeFactsFor(ctx, runtime, failure), {
|
|
143
|
+
caller: 'script',
|
|
144
|
+
keepLink: true,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
151
|
+
return;
|
|
152
|
+
const { plan, targetVersion, facts } = planForRuntime(ctx, runtime, profile);
|
|
153
|
+
phase('start', 'restarting-remote', { level: plan.level });
|
|
154
|
+
log('info', `restart: launching @deepseek-ai/dsh@${plan.start.targetVersion} with profile ${profile}`);
|
|
155
|
+
const webInfo = await ctx.deps.startWeb(runtime.ssh, {
|
|
156
|
+
plan,
|
|
157
|
+
layout,
|
|
158
|
+
profile,
|
|
159
|
+
timeoutMs: REMOTE_START_TIMEOUT_MS,
|
|
160
|
+
onLine: (line) => log('debug', `remote: ${line}`),
|
|
161
|
+
});
|
|
162
|
+
started = true;
|
|
163
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
164
|
+
return;
|
|
165
|
+
await rememberLaunched(ctx, runtime, webInfo.pid, layout, profile);
|
|
166
|
+
if (webInfo.browserCarrier === 'patched')
|
|
167
|
+
log('info', PATCHED_CARRIER_NOTE);
|
|
168
|
+
log('info', `restart: remote DSH started pid ${webInfo.pid ?? '?'}, port ${webInfo.port}`);
|
|
169
|
+
const record = buildRunRegistryRecord({
|
|
170
|
+
pid: webInfo.pid,
|
|
171
|
+
port: webInfo.port,
|
|
172
|
+
version: targetVersion,
|
|
173
|
+
profile,
|
|
174
|
+
dshHome: layout.dshHome,
|
|
175
|
+
startedAt: webInfo.startedAt,
|
|
176
|
+
});
|
|
177
|
+
const written = await ctx.deps.writeRegistry(runtime.ssh, layout, record);
|
|
178
|
+
if (written.ok === true)
|
|
179
|
+
log('info', `restart: run registry refreshed (pid ${record.pid}, port ${record.port})`);
|
|
180
|
+
else
|
|
181
|
+
log('warn', 'restart: the run registry could not be refreshed:', written.output);
|
|
182
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
183
|
+
return;
|
|
184
|
+
// The remote picked a random port again, so the old tunnel points at a dead port.
|
|
185
|
+
phase('tunnel', 'tunneling');
|
|
186
|
+
await closeTunnel(runtime);
|
|
187
|
+
runtime.token = webInfo.token;
|
|
188
|
+
runtime.remote = {
|
|
189
|
+
...runtime.remote,
|
|
190
|
+
version: targetVersion,
|
|
191
|
+
uiReady: false,
|
|
192
|
+
localPort: null,
|
|
193
|
+
port: webInfo.port,
|
|
194
|
+
pid: webInfo.pid ?? null,
|
|
195
|
+
dshHome: layout.dshHome,
|
|
196
|
+
profile,
|
|
197
|
+
bootstrapLevel: plan.level,
|
|
198
|
+
nodeVersion: facts.nodeVersion,
|
|
199
|
+
nodeSource: 'managed',
|
|
200
|
+
startedAt: webInfo.startedAt ?? nowIso(),
|
|
201
|
+
};
|
|
202
|
+
const tunnel = await openTunnelFor(ctx, runtime, webInfo.port);
|
|
203
|
+
tunnelCreated = true;
|
|
204
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
205
|
+
return;
|
|
206
|
+
phase('verifying', 'verifying');
|
|
207
|
+
await verifyRemoteUi(ctx, runtime, webInfo.port, webInfo.token, tunnel.localPort);
|
|
208
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
209
|
+
return;
|
|
210
|
+
if (ctx.settings().saveTokens)
|
|
211
|
+
await ctx.store.saveToken(runtime.id, webInfo.token);
|
|
212
|
+
if (!isLinkWorkCurrent(runtime, token))
|
|
213
|
+
return;
|
|
214
|
+
recordRestart(runtime, {
|
|
215
|
+
kind: 'local-restart',
|
|
216
|
+
fromPort: previousPort,
|
|
217
|
+
toPort: webInfo.port,
|
|
218
|
+
pid: webInfo.pid ?? null,
|
|
219
|
+
});
|
|
220
|
+
await markConnected(ctx, runtime);
|
|
221
|
+
log('info', `restart: connected again on 127.0.0.1:${runtime.tunnel?.localPort ?? tunnel.localPort} (remote port ${previousPort} -> ${webInfo.port})`);
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
if (!isLinkWorkCurrent(runtime, token)) {
|
|
225
|
+
log('debug', 'restart aborted: the link was replaced by a disconnect or reconnect');
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
// Nothing this connection owned was stopped, so the previous attachment is still valid:
|
|
229
|
+
// cancelling a conflict prompt (or a failed relaunch) keeps the host usable.
|
|
230
|
+
if (!stoppedOwn && !started && hadTunnel && runtime.ssh?.connected === true && runtime.tunnel !== null) {
|
|
231
|
+
log('warn', `restart cancelled: ${messageOf(error)}; keeping the current remote instance`);
|
|
232
|
+
await resumeConnected(ctx, runtime);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const view = error instanceof LinkCancelled
|
|
236
|
+
? toErrorView(errors.network(SSH_CLOSED_DETAIL, { technical: 'reason=ssh-closed during restart' }))
|
|
237
|
+
: toErrorView(error);
|
|
238
|
+
const failure = { type: 'restart.failed', view, phase: runtime.phase };
|
|
239
|
+
await dispatch(createRuntimeEffects(ctx), runtime, failure, scopeFactsFor(ctx, runtime, failure), {
|
|
240
|
+
caller: 'script',
|
|
241
|
+
emit: false,
|
|
242
|
+
// Only a remote this restart launched is stopped; a restart that failed before the launch leaves
|
|
243
|
+
// the instance it never touched alone.
|
|
244
|
+
teardown: { stopRemote: started && runtime.entry.keepAlive === 'stop' },
|
|
245
|
+
});
|
|
246
|
+
ctx.emitState();
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
// A listener this action created must never outlive the action: a disconnect during the rebuild
|
|
250
|
+
// would leave it open with a disposed SSH channel.
|
|
251
|
+
if (tunnelCreated && !isLinkWorkCurrent(runtime, token))
|
|
252
|
+
await closeTunnel(runtime);
|
|
253
|
+
endLinkWork(runtime, token);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { errors, toErrorView } from '../errors.js';
|
|
2
|
+
import { endLinkWork, isLinkWorkCurrent } from '../host-lifecycle.js';
|
|
3
|
+
import { layoutForRuntime } from '../layout.js';
|
|
4
|
+
import { dispatch, scopeFactsFor } from '../runtime/dispatch.js';
|
|
5
|
+
import { createRuntimeEffects } from '../runtime/effects.js';
|
|
6
|
+
import { isPid } from '../util.js';
|
|
7
|
+
import { linkActionState } from './guards.js';
|
|
8
|
+
import { OWNERSHIP_OPTIONS, proveOwnership } from './ownership.js';
|
|
9
|
+
/** Why the explicit stop refused, as the bilingual `ErrorView.detail` the Client renders. */
|
|
10
|
+
function describeStopRefusal(reason, pid) {
|
|
11
|
+
const target = isPid(pid) ? `pid ${pid}` : 'pid unknown';
|
|
12
|
+
switch (reason) {
|
|
13
|
+
case 'kept-alongside':
|
|
14
|
+
return {
|
|
15
|
+
zh: `远端进程 ${target} 是你在冲突提示里选择「保留」的实例,不会被停止。`,
|
|
16
|
+
en: `The remote process ${target} is the one you chose to keep alongside; it will not be stopped.`,
|
|
17
|
+
};
|
|
18
|
+
case 'not-launched-by-this-connection':
|
|
19
|
+
case 'not-the-bound-instance':
|
|
20
|
+
return {
|
|
21
|
+
zh: '远端进程不是本连接启动或接管的实例,无法证明归属,因此没有停止它;可以点「重连」。',
|
|
22
|
+
en: 'The remote process is not the instance this connection launched or took over, so its ownership cannot be proven and it was not stopped; use Reconnect.',
|
|
23
|
+
};
|
|
24
|
+
case 'registry-mismatch':
|
|
25
|
+
return {
|
|
26
|
+
zh: '远端 run/web.json 没有命名本连接的进程(或读不到),无法证明归属,因此没有停止它。',
|
|
27
|
+
en: 'The remote run/web.json does not name this connection\u2019s process (or could not be read), so ownership cannot be proven and nothing was stopped.',
|
|
28
|
+
};
|
|
29
|
+
case 'pid-reused':
|
|
30
|
+
return {
|
|
31
|
+
zh: '这个 pid 已被另一个进程复用,不是本连接启动的 DSH;没有停止它。',
|
|
32
|
+
en: 'This pid was reused by another process, not the DSH this connection launched; nothing was stopped.',
|
|
33
|
+
};
|
|
34
|
+
case 'dsh-home-mismatch':
|
|
35
|
+
case 'profile-mismatch':
|
|
36
|
+
return {
|
|
37
|
+
zh: '远端进程的 DSH home / profile 与本连接不一致,没有停止它。',
|
|
38
|
+
en: 'The remote process runs a different DSH home / profile than this connection; nothing was stopped.',
|
|
39
|
+
};
|
|
40
|
+
case 'pid-unknown':
|
|
41
|
+
return {
|
|
42
|
+
zh: '本连接没有记录远端 DSH 的 pid,无法定位要停止的进程。',
|
|
43
|
+
en: 'This connection has no recorded remote DSH pid, so there is no process to stop.',
|
|
44
|
+
};
|
|
45
|
+
default:
|
|
46
|
+
return {
|
|
47
|
+
zh: `无法读取远端进程 ${target} 的身份(${reason}),没有停止它。`,
|
|
48
|
+
en: `The identity of the remote process ${target} could not be proven (${reason}), so nothing was stopped.`,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Stop the remote DSH this connection manages, then release the link so the card offers Reconnect again; unlike disconnect this never kills a pid without an ownership proof. */
|
|
53
|
+
export async function stopRemoteHost(ctx, runtime) {
|
|
54
|
+
await ctx.init();
|
|
55
|
+
if (runtime.connectPromise || runtime.linkBusy !== null) {
|
|
56
|
+
throw errors.stopRemoteUnavailable({
|
|
57
|
+
zh: '这台主机正在执行另一个连接动作,请等它结束后再关闭远端 DSH。',
|
|
58
|
+
en: 'This host is busy with another link action; wait for it to finish before stopping the remote DSH.',
|
|
59
|
+
}, { technical: 'reason=link-busy' });
|
|
60
|
+
}
|
|
61
|
+
if (linkActionState(runtime) === null)
|
|
62
|
+
throw errors.stopRemoteUnavailable();
|
|
63
|
+
// The `previous` snapshot is what `stop.refused` restores; it is captured before the action writes anything.
|
|
64
|
+
const previous = {
|
|
65
|
+
status: runtime.status,
|
|
66
|
+
phase: runtime.phase,
|
|
67
|
+
progress: runtime.progress,
|
|
68
|
+
error: runtime.error,
|
|
69
|
+
uiReady: runtime.remote.uiReady === true,
|
|
70
|
+
localPort: runtime.remote.localPort ?? null,
|
|
71
|
+
};
|
|
72
|
+
const token = (Number(runtime.linkToken) || 0) + 1;
|
|
73
|
+
// One transition owns `activity`/`epoch`, the `connecting` card and the action start (`run`); the token
|
|
74
|
+
// is the old `beginLinkWork` one, so `isLinkWorkCurrent` keeps the exact late-write semantics.
|
|
75
|
+
const event = { type: 'stop.requested', token, pid: runtime.remote.pid, port: runtime.remote.port };
|
|
76
|
+
let started = null;
|
|
77
|
+
try {
|
|
78
|
+
await dispatch(createRuntimeEffects(ctx), runtime, event, scopeFactsFor(ctx, runtime, event), {
|
|
79
|
+
caller: 'script',
|
|
80
|
+
run: (activity, actionToken) => {
|
|
81
|
+
if (activity !== 'stop')
|
|
82
|
+
return;
|
|
83
|
+
started = runStopRemote(ctx, runtime, actionToken);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
return await started;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
const view = toErrorView(error);
|
|
90
|
+
const phase = typeof runtime.phase === 'string' ? runtime.phase : previous.phase;
|
|
91
|
+
if (isLinkWorkCurrent(runtime, token)) {
|
|
92
|
+
const restorable = previous.status === 'error' ||
|
|
93
|
+
(runtime.desired === 'connected' && runtime.ssh?.connected === true && runtime.tunnel !== null);
|
|
94
|
+
// The rule restores the previous state itself; `unrestorable` is the "link went away while the
|
|
95
|
+
// stop was failing" branch, which publishes the coded failure instead.
|
|
96
|
+
const refusal = {
|
|
97
|
+
type: 'stop.refused',
|
|
98
|
+
view,
|
|
99
|
+
phase,
|
|
100
|
+
previous,
|
|
101
|
+
...(restorable ? {} : { unrestorable: true }),
|
|
102
|
+
};
|
|
103
|
+
await dispatch(createRuntimeEffects(ctx), runtime, refusal, scopeFactsFor(ctx, runtime, refusal), {
|
|
104
|
+
caller: 'script',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
endLinkWork(runtime, token);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function runStopRemote(ctx, runtime, token) {
|
|
114
|
+
const log = (level, ...args) => ctx.progress.log(runtime, level, ...args);
|
|
115
|
+
const layout = layoutForRuntime(runtime);
|
|
116
|
+
const profile = runtime.remote.profile ?? ctx.settings().bootstrap.profile;
|
|
117
|
+
const pid = Number(runtime.remote.pid ?? 0);
|
|
118
|
+
const port = Number(runtime.remote.port ?? 0);
|
|
119
|
+
log('info', `stop remote requested (pid=${isPid(pid) ? pid : 'unknown'}, remote port=${isPid(port) ? port : 'unknown'})`);
|
|
120
|
+
const verdict = await proveOwnership(ctx, runtime, { ...OWNERSHIP_OPTIONS.stop, pid, layout, profile });
|
|
121
|
+
if (!isLinkWorkCurrent(runtime, token)) {
|
|
122
|
+
log('debug', 'stop remote aborted: the link was replaced by a disconnect or reconnect');
|
|
123
|
+
return { ok: true, pid: null };
|
|
124
|
+
}
|
|
125
|
+
// A pid that is already gone is not a refusal for this action: there is nothing to signal, only a
|
|
126
|
+
// stale registry to clear. Every other refusal is the coded error the card explains.
|
|
127
|
+
const gone = verdict.ok !== true && verdict.reason === 'identity-missing';
|
|
128
|
+
if (verdict.ok !== true && !gone) {
|
|
129
|
+
throw errors.stopRemoteUnavailable(describeStopRefusal(verdict.reason, pid), {
|
|
130
|
+
technical: `reason=${verdict.reason} pid=${isPid(pid) ? pid : 'unknown'}`,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (gone) {
|
|
134
|
+
log('info', `stop remote: the bound remote process pid=${pid} is already gone; clearing its run registry`);
|
|
135
|
+
const cleared = await ctx.deps.clearRegistry(runtime.ssh, layout);
|
|
136
|
+
if (cleared?.ok !== true)
|
|
137
|
+
log('warn', 'stop remote: the stale run registry could not be cleared:', cleared?.output ?? 'no output');
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const stopped = await ctx.deps.stopWeb(runtime.ssh, layout, { pid });
|
|
141
|
+
if (stopped?.ok !== true) {
|
|
142
|
+
throw errors.stopRemoteUnavailable({
|
|
143
|
+
zh: '远端 DSH 进程没有报告干净停止;连接保持原样,可以重试或点「重连」。',
|
|
144
|
+
en: 'The remote DSH process did not report a clean stop; the connection is unchanged, so retry or use Reconnect.',
|
|
145
|
+
}, { technical: String(stopped?.output ?? '').slice(0, 1200) || 'no output' });
|
|
146
|
+
}
|
|
147
|
+
log('info', `stop remote: stopped the managed remote web pid=${pid}`);
|
|
148
|
+
if (runtime.launched instanceof Map)
|
|
149
|
+
runtime.launched.delete(pid);
|
|
150
|
+
}
|
|
151
|
+
if (!isLinkWorkCurrent(runtime, token)) {
|
|
152
|
+
log('debug', 'stop remote: the link was replaced after the stop; leaving the state to that action');
|
|
153
|
+
return { ok: true, pid: isPid(pid) ? pid : null };
|
|
154
|
+
}
|
|
155
|
+
runtime.bound = null;
|
|
156
|
+
ctx.progress.log(runtime, 'info', `disconnect requested (keepAlive=${runtime.entry.keepAlive})`);
|
|
157
|
+
// One transition ends the action: `stop.succeeded` is the explicit stop's disconnect (the
|
|
158
|
+
// `stopped:true` form keeps the idle policy from arming a second stop).
|
|
159
|
+
const done = { type: 'stop.succeeded' };
|
|
160
|
+
await dispatch(createRuntimeEffects(ctx), runtime, done, scopeFactsFor(ctx, runtime, done), { caller: 'script' });
|
|
161
|
+
runtime.bound = null;
|
|
162
|
+
log('info', 'stop remote: the remote DSH is stopped; the host is kept and can be reconnected');
|
|
163
|
+
return { ok: true, pid: isPid(pid) ? pid : null };
|
|
164
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { errors } from '../errors.js';
|
|
2
|
+
import { findKnownHostEntries, parseKnownHosts } from '../known-hosts.js';
|
|
3
|
+
import { resolveDestination } from '../ssh-config.js';
|
|
4
|
+
import { isPid } from '../util.js';
|
|
5
|
+
import { noteRemoteUnreachable } from '../watchdog.js';
|
|
6
|
+
import { assertStillWanted, assertTransportOpen } from './guards.js';
|
|
7
|
+
/** The known_hosts fingerprints for one host:port, from the SSH material the caller already read: the one lookup both a direct destination and a jump host use. */
|
|
8
|
+
export function knownFingerprintsFor(material, host, port) {
|
|
9
|
+
return findKnownHostEntries(parseKnownHosts(material.knownHostsText), host, port).map((item) => item.fingerprint);
|
|
10
|
+
}
|
|
11
|
+
export async function resolveTargetForRuntime(ctx, runtime) {
|
|
12
|
+
const material = await ctx.deps.loadSshMaterial({ home: ctx.userHome() });
|
|
13
|
+
const entry = runtime.entry;
|
|
14
|
+
const resolved = resolveDestination(entry.address, { configText: material.configText, home: ctx.userHome() });
|
|
15
|
+
const host = resolved.host || entry.address;
|
|
16
|
+
const port = entry.port && entry.port !== 22 ? entry.port : (resolved.port ?? 22);
|
|
17
|
+
const knownFingerprints = knownFingerprintsFor(material, host, port);
|
|
18
|
+
return {
|
|
19
|
+
host,
|
|
20
|
+
port,
|
|
21
|
+
username: entry.username || resolved.user || null,
|
|
22
|
+
identityFiles: resolved.identityFiles ?? [],
|
|
23
|
+
knownFingerprints,
|
|
24
|
+
jumpHost: entry.jumpHost
|
|
25
|
+
? resolveDestination(entry.jumpHost, { configText: material.configText, home: ctx.userHome() })
|
|
26
|
+
: null,
|
|
27
|
+
entry,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function captureFingerprint(ctx, runtime, info) {
|
|
31
|
+
if (!info?.fingerprint)
|
|
32
|
+
return;
|
|
33
|
+
const pinned = runtime.entry.pinnedFingerprint ?? null;
|
|
34
|
+
const matches = pinned === info.fingerprint;
|
|
35
|
+
runtime.fingerprint = {
|
|
36
|
+
sha256: info.fingerprint,
|
|
37
|
+
keyType: info.keyType ?? 'unknown',
|
|
38
|
+
status: matches ? 'pinned' : pinned ? 'changed' : 'observed',
|
|
39
|
+
pinned,
|
|
40
|
+
};
|
|
41
|
+
ctx.progress.log(runtime, matches ? 'debug' : 'info', `host key ${info.keyType ?? 'unknown'}: ${info.fingerprint}${matches ? ' (matches the pinned key)' : pinned ? ' (CHANGED from the pinned key)' : ''}`);
|
|
42
|
+
ctx.emitState();
|
|
43
|
+
}
|
|
44
|
+
/** Open the loopback tunnel for one remote port; tunnel-level failures feed the liveness confirmation. */
|
|
45
|
+
export async function openTunnelFor(ctx, runtime, remotePort) {
|
|
46
|
+
// Never hand the tunnel factory a missing transport: a listener with no SSH channel would crash the
|
|
47
|
+
// host process on the first connection.
|
|
48
|
+
assertTransportOpen(runtime);
|
|
49
|
+
if (!isPid(remotePort) || remotePort > 65535) {
|
|
50
|
+
throw errors.internal(new Error(`invalid remote port for the tunnel: ${remotePort}`));
|
|
51
|
+
}
|
|
52
|
+
const onError = () => noteRemoteUnreachable(ctx, runtime, 'tunnel');
|
|
53
|
+
const tunnel = await ctx.deps.tunnelFactory({ ssh: runtime.ssh, remotePort, onError });
|
|
54
|
+
const localPort = Number(tunnel?.localPort);
|
|
55
|
+
if (!Number.isInteger(localPort) || !isPid(localPort) || localPort > 65535) {
|
|
56
|
+
try {
|
|
57
|
+
await tunnel?.close?.();
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
// swallow-ok: the listener never carried a caller; the internal error is what matters
|
|
61
|
+
}
|
|
62
|
+
throw errors.internal(new Error(`the tunnel factory returned no usable local port (${tunnel?.localPort})`));
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
assertStillWanted(ctx, runtime);
|
|
66
|
+
assertTransportOpen(runtime);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
try {
|
|
70
|
+
await tunnel.close();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// swallow-ok: the cancellation error must be rethrown, not masked by a close error
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
runtime.tunnel = tunnel;
|
|
78
|
+
runtime.remote.localPort = localPort;
|
|
79
|
+
ctx.progress.log(runtime, 'info', `tunnel open: 127.0.0.1:${localPort} -> 127.0.0.1:${remotePort}`);
|
|
80
|
+
return tunnel;
|
|
81
|
+
}
|
|
82
|
+
export async function settlePasswordStorage(ctx, runtime, log) {
|
|
83
|
+
const save = runtime.savePassword === true;
|
|
84
|
+
const forget = runtime.forgetPassword === true;
|
|
85
|
+
runtime.savePassword = false;
|
|
86
|
+
runtime.forgetPassword = false;
|
|
87
|
+
if (!ctx.secrets || typeof runtime.password !== 'string' || runtime.password === '')
|
|
88
|
+
return;
|
|
89
|
+
if (save) {
|
|
90
|
+
runtime.passwordSaved = await ctx.secrets.set(runtime.id, runtime.password);
|
|
91
|
+
if (runtime.passwordSaved)
|
|
92
|
+
log('debug', 'saved the SSH password in the credential store');
|
|
93
|
+
else
|
|
94
|
+
log('warn', 'could not save the SSH password in the credential store');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (forget) {
|
|
98
|
+
const removed = await ctx.secrets.remove(runtime.id);
|
|
99
|
+
runtime.passwordSaved = !removed;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The `[verifying]` step: the remote UI answers through the new listener and the launch token exchanges for the cookie the carrier needs; both halves are guarded across their awaits. */
|
|
2
|
+
import { TUNNEL_PROBE_TIMEOUT_MS } from '../constants.js';
|
|
3
|
+
import { errors } from '../errors.js';
|
|
4
|
+
import { isPid } from '../util.js';
|
|
5
|
+
import { assertStillWanted, assertTransportOpen } from './guards.js';
|
|
6
|
+
export async function verifyRemoteUi(ctx, runtime, port, token, localPort) {
|
|
7
|
+
const tunnelPort = Number(localPort);
|
|
8
|
+
if (!Number.isInteger(tunnelPort) || !isPid(tunnelPort) || tunnelPort > 65535)
|
|
9
|
+
throw errors.internal(new Error(`the tunnel has no local port to verify (${localPort ?? 'none'})`));
|
|
10
|
+
ctx.progress.log(runtime, 'debug', `verifying the remote UI on 127.0.0.1:${tunnelPort}`);
|
|
11
|
+
const ui = await ctx.deps.waitUi(runtime.ssh, port, { attempts: 20, intervalMs: 500 });
|
|
12
|
+
assertStillWanted(ctx, runtime);
|
|
13
|
+
assertTransportOpen(runtime);
|
|
14
|
+
if (!ui.ok) {
|
|
15
|
+
throw errors.remoteUiUnreachable({
|
|
16
|
+
zh: `远端 DSH 的本地 127.0.0.1:${port} 没有响应(HTTP ${ui.status ?? '无'})。`,
|
|
17
|
+
en: `The remote DSH on 127.0.0.1:${port} did not respond (HTTP ${ui.status ?? 'none'}).`,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
ctx.progress.log(runtime, 'debug', `remote UI answered HTTP ${ui.status}; exchanging the launch token`);
|
|
21
|
+
const exchange = await ctx.deps.exchange(tunnelPort, token, { timeoutMs: TUNNEL_PROBE_TIMEOUT_MS });
|
|
22
|
+
runtime.exchange = exchange;
|
|
23
|
+
if (!exchange.ok) {
|
|
24
|
+
throw errors.remoteUiUnreachable({
|
|
25
|
+
zh: `本地隧道完成了 token 交换,但远端界面返回 HTTP ${exchange.status}(首次状态 ${exchange.firstStatus})。`,
|
|
26
|
+
en: `The tunnel exchanged the token but the remote UI answered HTTP ${exchange.status} (first ${exchange.firstStatus}).`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return exchange;
|
|
30
|
+
}
|