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,480 @@
|
|
|
1
|
+
import { stillWanted } from './fsm.js';
|
|
2
|
+
/** The two bilingual texts the FSM itself owns. */
|
|
3
|
+
export const RESTART_STOP_WARNING = {
|
|
4
|
+
zh: '远端受管 DSH 没有确认停止,本次没有启动新实例;请稍后重试,或先断开再重连。',
|
|
5
|
+
en: 'The managed remote DSH did not confirm its stop, so no new instance was started; retry shortly, or disconnect and reconnect.',
|
|
6
|
+
};
|
|
7
|
+
export const STOP_FAILED_LABEL = {
|
|
8
|
+
zh: '关闭远端 DSH 失败',
|
|
9
|
+
en: 'Stopping the remote DSH failed',
|
|
10
|
+
};
|
|
11
|
+
function move(state, changes) {
|
|
12
|
+
return { ...state, ...changes };
|
|
13
|
+
}
|
|
14
|
+
/** Adopt a caller's token without ever rewinding the epoch: `beginLinkWork` passes `epoch + 1`, so a stale timer
|
|
15
|
+
* token can never take ownership back. */
|
|
16
|
+
function epochAfter(state, token) {
|
|
17
|
+
return Math.max(state.epoch, token);
|
|
18
|
+
}
|
|
19
|
+
/** One progress card: the code plus the values the Client renders next to it. */
|
|
20
|
+
function progressOf(code, params) {
|
|
21
|
+
return params === undefined ? { code } : { code, params };
|
|
22
|
+
}
|
|
23
|
+
/* Effect constructors: a rule reads as the event table row it implements. */
|
|
24
|
+
const emit = () => ({ type: 'emit' });
|
|
25
|
+
const settlePrompts = () => ({ type: 'settlePrompts' });
|
|
26
|
+
const closeLink = () => ({ type: 'closeLink' });
|
|
27
|
+
const disarm = (timer) => ({ type: 'disarm', timer });
|
|
28
|
+
const arm = (timer, ms) => ({ type: 'arm', timer, ms });
|
|
29
|
+
const log = (level, message) => ({ type: 'log', level, message });
|
|
30
|
+
const logFailure = (phase, label, view) => ({
|
|
31
|
+
type: 'logFailure',
|
|
32
|
+
phase,
|
|
33
|
+
...(label === undefined ? {} : { label }),
|
|
34
|
+
// Only a rule that restores a *previous* error names the failure it reports; otherwise the executor logs the error
|
|
35
|
+
// the transition just wrote.
|
|
36
|
+
...(view === undefined ? {} : { view }),
|
|
37
|
+
});
|
|
38
|
+
const apply = (patch) => ({ type: 'apply', patch });
|
|
39
|
+
const teardown = (options) => ({ type: 'teardown', options });
|
|
40
|
+
const run = (activity, token) => ({ type: 'run', activity, token });
|
|
41
|
+
// Frozen shared effect lists: freezing stops a caller that sorted/pushed a result from corrupting later transitions.
|
|
42
|
+
const EMIT_ONLY = Object.freeze([emit()]);
|
|
43
|
+
const NO_EFFECTS = Object.freeze([]);
|
|
44
|
+
const needsActivity = (state) => (state.activity === null ? 'illegal-transition' : true);
|
|
45
|
+
const ownsActivity = (activity) => (state) => state.activity === activity ? true : 'illegal-transition';
|
|
46
|
+
const promptGuard = (status) => (state, _event, scope) => !scope.promptsPending ? 'stale-prompt' : state.status === status ? true : 'prompt-not-pending';
|
|
47
|
+
/** The shared effect list of the four request events: stop every timer, publish the `connecting` card (today's
|
|
48
|
+
* `progress.set` published it) and start the action script. */
|
|
49
|
+
const requestEffects = (state, token, activity) => [
|
|
50
|
+
disarm('heartbeat'),
|
|
51
|
+
disarm('reconnect'),
|
|
52
|
+
disarm('idle'),
|
|
53
|
+
emit(),
|
|
54
|
+
run(activity, epochAfter(state, token)),
|
|
55
|
+
];
|
|
56
|
+
/** Enter one one-shot action: it takes the link lock, adopts the token, and clears the previous error on entry. */
|
|
57
|
+
const enter = (state, activity, token, progress, extra = {}) => move(state, {
|
|
58
|
+
activity,
|
|
59
|
+
epoch: epochAfter(state, token),
|
|
60
|
+
status: 'connecting',
|
|
61
|
+
phase: 'start',
|
|
62
|
+
progress,
|
|
63
|
+
error: null,
|
|
64
|
+
...extra,
|
|
65
|
+
});
|
|
66
|
+
const released = (state) => move(state, {
|
|
67
|
+
status: 'disconnected',
|
|
68
|
+
desired: 'idle',
|
|
69
|
+
epoch: state.epoch + 1,
|
|
70
|
+
phase: null,
|
|
71
|
+
progress: null,
|
|
72
|
+
activity: null,
|
|
73
|
+
});
|
|
74
|
+
const toError = (state, view) => move(state, { status: 'error', error: view, phase: null, progress: null, activity: null });
|
|
75
|
+
function transportCloseBranch(state, scope) {
|
|
76
|
+
// This close is a teardown's own closing, or the user already let go: nothing to report.
|
|
77
|
+
if (state.closing || state.desired !== 'connected')
|
|
78
|
+
return 'ignored';
|
|
79
|
+
// A newer transport owns the link; this one is the previous attempt's.
|
|
80
|
+
if (!scope.transportIsCurrent)
|
|
81
|
+
return 'stale';
|
|
82
|
+
// `live` mirrors today's `status === 'connected' || linkBusy !== null`; a connect attempt has its own branch below.
|
|
83
|
+
if (state.status === 'connected' || (state.activity !== null && state.activity !== 'connect'))
|
|
84
|
+
return 'dropped';
|
|
85
|
+
if (state.activity === 'connect')
|
|
86
|
+
return 'handshake';
|
|
87
|
+
return 'idle';
|
|
88
|
+
}
|
|
89
|
+
/** The restart/stop entry guard: connected now, or a `remote-unreachable` error that can recover. */
|
|
90
|
+
function entryGuard(state, _event, scope) {
|
|
91
|
+
if (state.activity !== null)
|
|
92
|
+
return 'busy';
|
|
93
|
+
if (state.status === 'connected')
|
|
94
|
+
return true;
|
|
95
|
+
if (state.status === 'error')
|
|
96
|
+
return scope.recoverable ? true : 'not-recoverable';
|
|
97
|
+
return 'not-connected';
|
|
98
|
+
}
|
|
99
|
+
export const TABLE = {
|
|
100
|
+
'connect.requested': {
|
|
101
|
+
guard(state, _event, scope) {
|
|
102
|
+
if (state.activity === 'connect')
|
|
103
|
+
return 'already-connecting';
|
|
104
|
+
if (state.activity !== null)
|
|
105
|
+
return 'already-restarting';
|
|
106
|
+
if (state.status === 'connected' && scope.linkAlive)
|
|
107
|
+
return 'already-connected';
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
apply: (state, event) => move(state, {
|
|
111
|
+
status: 'connecting',
|
|
112
|
+
desired: 'connected',
|
|
113
|
+
activity: 'connect',
|
|
114
|
+
epoch: epochAfter(state, event.token),
|
|
115
|
+
// A scheduled retry re-enters the pipeline; only a user-requested connect resets the budget.
|
|
116
|
+
reconnectAttempts: event.retry === true ? state.reconnectAttempts : 0,
|
|
117
|
+
connectCancelled: false,
|
|
118
|
+
transportLost: false,
|
|
119
|
+
error: null,
|
|
120
|
+
warning: null,
|
|
121
|
+
warningSource: null,
|
|
122
|
+
phase: null,
|
|
123
|
+
progress: null,
|
|
124
|
+
closing: false,
|
|
125
|
+
}),
|
|
126
|
+
effects: (state, event) => [
|
|
127
|
+
disarm('idle'),
|
|
128
|
+
disarm('reconnect'),
|
|
129
|
+
disarm('heartbeat'),
|
|
130
|
+
emit(),
|
|
131
|
+
run('connect', epochAfter(state, event.token)),
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
'step.entered': {
|
|
135
|
+
guard: needsActivity,
|
|
136
|
+
apply: (state, event) => move(state, { phase: event.phase, progress: progressOf(event.code, event.params) }),
|
|
137
|
+
effects: () => EMIT_ONLY,
|
|
138
|
+
},
|
|
139
|
+
'step.done': {
|
|
140
|
+
guard: needsActivity,
|
|
141
|
+
apply: (state) => state,
|
|
142
|
+
// Today probe/node/tunnel/verify completions only log; they do not publish a frame.
|
|
143
|
+
effects: (_state, event) => [log('debug', `step done: ${event.step}`)],
|
|
144
|
+
},
|
|
145
|
+
'transport.ready': {
|
|
146
|
+
guard: needsActivity,
|
|
147
|
+
apply: (state) => move(state, { transportLost: false }),
|
|
148
|
+
effects: () => NO_EFFECTS,
|
|
149
|
+
},
|
|
150
|
+
'transport.closed': {
|
|
151
|
+
guard: () => true,
|
|
152
|
+
apply: (state, _event, scope) => {
|
|
153
|
+
const branch = transportCloseBranch(state, scope);
|
|
154
|
+
if (branch === 'dropped')
|
|
155
|
+
return move(state, transportDropPatch(state));
|
|
156
|
+
if (branch === 'handshake')
|
|
157
|
+
return move(state, { transportLost: true });
|
|
158
|
+
return state;
|
|
159
|
+
},
|
|
160
|
+
effects: (state, _event, scope) => {
|
|
161
|
+
const branch = transportCloseBranch(state, scope);
|
|
162
|
+
if (branch === 'dropped') {
|
|
163
|
+
return [
|
|
164
|
+
apply({ kind: 'mark-detached' }),
|
|
165
|
+
settlePrompts(),
|
|
166
|
+
disarm('heartbeat'),
|
|
167
|
+
closeLink(),
|
|
168
|
+
arm('reconnect', scope.delays.reconnectMs),
|
|
169
|
+
emit(),
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
if (branch === 'handshake') {
|
|
173
|
+
return [settlePrompts(), log('warn', 'the SSH transport closed while the connection was being set up')];
|
|
174
|
+
}
|
|
175
|
+
return NO_EFFECTS;
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
'hostkey.pending': {
|
|
179
|
+
guard: (state) => (state.status === 'connecting' ? true : 'illegal-transition'),
|
|
180
|
+
apply: (state, event) => {
|
|
181
|
+
// The same keys `prompts.ts` publishes, including the jump identity (never the target's).
|
|
182
|
+
const params = { sha256: event.info.sha256, keyType: event.info.keyType };
|
|
183
|
+
const progress = event.role === 'jump'
|
|
184
|
+
? progressOf('host-key-confirm', { ...params, role: 'jump', host: event.host })
|
|
185
|
+
: progressOf('host-key-confirm', params);
|
|
186
|
+
return move(state, { status: 'fingerprint-pending', phase: 'ssh', progress });
|
|
187
|
+
},
|
|
188
|
+
effects: () => EMIT_ONLY,
|
|
189
|
+
},
|
|
190
|
+
'hostkey.answered': {
|
|
191
|
+
guard: promptGuard('fingerprint-pending'),
|
|
192
|
+
apply: (state, event) => move(state, { status: 'connecting', progress: progressOf('auth-checking'), connectCancelled: !event.accept }),
|
|
193
|
+
effects: () => EMIT_ONLY,
|
|
194
|
+
},
|
|
195
|
+
'hostkey.timeout': {
|
|
196
|
+
guard: promptGuard('fingerprint-pending'),
|
|
197
|
+
apply: (state, event) => move(state, { status: 'error', error: event.view, phase: null, progress: null, connectCancelled: true }),
|
|
198
|
+
effects: () => EMIT_ONLY,
|
|
199
|
+
},
|
|
200
|
+
'conflict.pending': {
|
|
201
|
+
guard: (state) => (state.status === 'connecting' ? true : 'illegal-transition'),
|
|
202
|
+
apply: (state, event) => move(state, { status: 'conflict-pending', progress: progressOf('remote-dsh-running', { count: event.count }) }),
|
|
203
|
+
effects: () => EMIT_ONLY,
|
|
204
|
+
},
|
|
205
|
+
'conflict.answered': {
|
|
206
|
+
guard: promptGuard('conflict-pending'),
|
|
207
|
+
apply: (state, event) => move(state, {
|
|
208
|
+
status: 'connecting',
|
|
209
|
+
progress: progressOf('probing'),
|
|
210
|
+
// The user said "no": a retry would ask again, so the failure that follows must not schedule one.
|
|
211
|
+
connectCancelled: event.action === 'cancel' ? true : state.connectCancelled,
|
|
212
|
+
}),
|
|
213
|
+
effects: () => [apply({ kind: 'clear-conflict' }), emit()],
|
|
214
|
+
},
|
|
215
|
+
'connect.succeeded': {
|
|
216
|
+
guard: (state) => {
|
|
217
|
+
const mine = state.activity === 'connect' || state.activity === 'restart' || state.activity === 'reattach';
|
|
218
|
+
return mine && state.status === 'connecting' ? true : 'illegal-transition';
|
|
219
|
+
},
|
|
220
|
+
apply: (state) => {
|
|
221
|
+
// A fresh attach clears a stale restart-stop warning; an `alongside` warning describes a running process.
|
|
222
|
+
const clearsWarning = state.warningSource === 'restart-stop';
|
|
223
|
+
return move(state, {
|
|
224
|
+
status: 'connected',
|
|
225
|
+
phase: 'ready',
|
|
226
|
+
progress: progressOf('ready'),
|
|
227
|
+
error: null,
|
|
228
|
+
reconnectAttempts: 0,
|
|
229
|
+
connectCancelled: false,
|
|
230
|
+
activity: null,
|
|
231
|
+
warning: clearsWarning ? null : state.warning,
|
|
232
|
+
warningSource: clearsWarning ? null : state.warningSource,
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
effects: (_state, _event, scope) => [
|
|
236
|
+
disarm('idle'),
|
|
237
|
+
arm('heartbeat', scope.delays.heartbeatMs),
|
|
238
|
+
apply({ kind: 'mark-attached' }),
|
|
239
|
+
apply({ kind: 'publish-active-host', hostId: scope.hostId }),
|
|
240
|
+
emit(),
|
|
241
|
+
],
|
|
242
|
+
},
|
|
243
|
+
'connect.cancelled': {
|
|
244
|
+
guard: needsActivity,
|
|
245
|
+
apply: (state) => move(state, { status: 'disconnected', error: null, phase: null, progress: null, activity: null }),
|
|
246
|
+
// Today's cancel branch published `uiReady:false`/`localPort:null` too: a cancelled attempt that had already set
|
|
247
|
+
// `uiReady` must not leave a half-attached remote view behind.
|
|
248
|
+
effects: () => [teardown({ stopRemote: false, keepRemote: true }), apply({ kind: 'mark-detached' }), emit()],
|
|
249
|
+
},
|
|
250
|
+
'connect.failed': {
|
|
251
|
+
// The one explicit idempotent self-transition: the host-key timeout and the pipeline catch report the same failure,
|
|
252
|
+
// so `error` → `error` is allowed, and any status may fail (a transport closing under a parked prompt).
|
|
253
|
+
// `activity !== null` is the real guard: a failure belongs to the script that owns the link.
|
|
254
|
+
guard: needsActivity,
|
|
255
|
+
apply: (state, event) => move(state, {
|
|
256
|
+
status: 'error',
|
|
257
|
+
error: event.view,
|
|
258
|
+
phase: null,
|
|
259
|
+
progress: null,
|
|
260
|
+
activity: null,
|
|
261
|
+
// Today's pipeline clears the flag after the retry decision; the next attempt starts "not cancelled".
|
|
262
|
+
connectCancelled: false,
|
|
263
|
+
}),
|
|
264
|
+
effects: (state, event, scope) => [
|
|
265
|
+
apply({ kind: 'mark-detached' }),
|
|
266
|
+
logFailure(event.phase),
|
|
267
|
+
teardown({
|
|
268
|
+
stopRemote: scope.remoteStarted && scope.keepAlive === 'stop',
|
|
269
|
+
keepRemote: scope.remoteStarted && scope.keepAlive !== 'stop',
|
|
270
|
+
}),
|
|
271
|
+
// The retry decision (policy, budget, delay) stays in the executor; this only says a retry makes sense.
|
|
272
|
+
...(state.desired === 'connected' && !state.connectCancelled ? [arm('reconnect', scope.delays.reconnectMs)] : []),
|
|
273
|
+
emit(),
|
|
274
|
+
],
|
|
275
|
+
},
|
|
276
|
+
'disconnect.requested': {
|
|
277
|
+
// Total on purpose: the user may cancel during any connect step, and a repeat disconnect is idempotent.
|
|
278
|
+
guard: () => true,
|
|
279
|
+
apply: (state) => move(state, { ...released(state), closing: false }),
|
|
280
|
+
effects: (_state, event, scope) => {
|
|
281
|
+
const alreadyStopped = event.stopped === true;
|
|
282
|
+
const stopRemote = alreadyStopped ? false : event.stopRemote === true || scope.keepAlive === 'stop';
|
|
283
|
+
const keepSshForIdle = scope.keepAlive === 'idle' && !stopRemote && !alreadyStopped;
|
|
284
|
+
return [
|
|
285
|
+
disarm('reconnect'),
|
|
286
|
+
disarm('idle'),
|
|
287
|
+
disarm('heartbeat'),
|
|
288
|
+
settlePrompts(),
|
|
289
|
+
teardown({ stopRemote, keepSshForIdle }),
|
|
290
|
+
apply({ kind: 'mark-detached' }),
|
|
291
|
+
...(keepSshForIdle ? [arm('idle', scope.delays.idleMs)] : []),
|
|
292
|
+
emit(),
|
|
293
|
+
];
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
'idle.elapsed': {
|
|
297
|
+
// A timer that fires for a host the user wants again is stale. Refusing keeps "run the idle script" unambiguous;
|
|
298
|
+
// the script re-checks `stillWanted`, and this refusal is logged at debug, not the generic warn level.
|
|
299
|
+
guard: (state, _event, scope) => (stillWanted(state, scope) ? 'illegal-transition' : true),
|
|
300
|
+
apply: (state) => state,
|
|
301
|
+
effects: () => NO_EFFECTS,
|
|
302
|
+
},
|
|
303
|
+
'repair.requested': {
|
|
304
|
+
guard: (state, _event, scope) => {
|
|
305
|
+
if (state.activity !== null)
|
|
306
|
+
return 'busy';
|
|
307
|
+
if (state.status !== 'connected' || !scope.tunnelAlive)
|
|
308
|
+
return 'not-connected';
|
|
309
|
+
return true;
|
|
310
|
+
},
|
|
311
|
+
apply: (state, event) => move(state, { activity: 'repair', epoch: epochAfter(state, event.token) }),
|
|
312
|
+
effects: (state, event) => [run('repair', epochAfter(state, event.token))],
|
|
313
|
+
},
|
|
314
|
+
'repair.finished': {
|
|
315
|
+
guard: ownsActivity('repair'),
|
|
316
|
+
apply: (state) => move(state, { activity: null }),
|
|
317
|
+
// Today a successful rebuild publishes the frame and marks the UI up; a failed one is silent (the repair script
|
|
318
|
+
// logs its own reason).
|
|
319
|
+
effects: (_state, event) => (event.ok ? [apply({ kind: 'mark-ui-up' }), emit()] : NO_EFFECTS),
|
|
320
|
+
},
|
|
321
|
+
'restart.requested': {
|
|
322
|
+
guard: entryGuard,
|
|
323
|
+
apply: (state, event) => enter(state, 'restart', event.token, progressOf('restarting-remote', { pid: Number(event.pid) || 0, port: Number(event.port) || 0 }), {
|
|
324
|
+
desired: 'connected',
|
|
325
|
+
}),
|
|
326
|
+
effects: (state, event) => requestEffects(state, event.token, 'restart'),
|
|
327
|
+
},
|
|
328
|
+
'restart.resumed': {
|
|
329
|
+
guard: ownsActivity('restart'),
|
|
330
|
+
apply: (state, event) => {
|
|
331
|
+
// Only the "managed stop was not confirmed" caller warns; a restart cancelled before that resumes plainly.
|
|
332
|
+
const keptInstance = event.keptInstance === true;
|
|
333
|
+
return move(state, {
|
|
334
|
+
status: 'connected',
|
|
335
|
+
warning: keptInstance ? RESTART_STOP_WARNING : state.warning,
|
|
336
|
+
warningSource: keptInstance ? 'restart-stop' : state.warningSource,
|
|
337
|
+
phase: 'ready',
|
|
338
|
+
progress: progressOf('ready'),
|
|
339
|
+
error: null,
|
|
340
|
+
activity: null,
|
|
341
|
+
});
|
|
342
|
+
},
|
|
343
|
+
effects: (_state, _event, scope) => [
|
|
344
|
+
apply({ kind: 'mark-ui-up' }),
|
|
345
|
+
arm('heartbeat', scope.delays.heartbeatMs),
|
|
346
|
+
emit(),
|
|
347
|
+
],
|
|
348
|
+
},
|
|
349
|
+
'restart.failed': {
|
|
350
|
+
guard: ownsActivity('restart'),
|
|
351
|
+
apply: (state, event) => toError(state, event.view),
|
|
352
|
+
effects: (_state, event, scope) => [
|
|
353
|
+
apply({ kind: 'mark-detached' }),
|
|
354
|
+
logFailure(event.phase),
|
|
355
|
+
teardown({ stopRemote: scope.keepAlive === 'stop' }),
|
|
356
|
+
emit(),
|
|
357
|
+
],
|
|
358
|
+
},
|
|
359
|
+
'stop.requested': {
|
|
360
|
+
guard: entryGuard,
|
|
361
|
+
apply: (state, event) => enter(state, 'stop', event.token, progressOf('stopping-remote', { pid: Number(event.pid) || 0, port: Number(event.port) || 0 })),
|
|
362
|
+
effects: (state, event) => requestEffects(state, event.token, 'stop'),
|
|
363
|
+
},
|
|
364
|
+
'stop.refused': {
|
|
365
|
+
guard: ownsActivity('stop'),
|
|
366
|
+
apply(state, event) {
|
|
367
|
+
// Unrestorable: the link vanished while the stop was failing, so publish the coded failure, not a fake state.
|
|
368
|
+
if (event.unrestorable === true)
|
|
369
|
+
return toError(state, event.view);
|
|
370
|
+
const previous = event.previous;
|
|
371
|
+
return move(state, {
|
|
372
|
+
status: previous.status,
|
|
373
|
+
error: previous.error,
|
|
374
|
+
phase: previous.phase,
|
|
375
|
+
progress: previous.progress,
|
|
376
|
+
activity: null,
|
|
377
|
+
});
|
|
378
|
+
},
|
|
379
|
+
effects: (_state, event, scope) => [
|
|
380
|
+
event.unrestorable === true
|
|
381
|
+
? apply({ kind: 'mark-detached' })
|
|
382
|
+
: apply({ kind: 'restore-stop', uiReady: event.previous.uiReady, localPort: event.previous.localPort }),
|
|
383
|
+
// A restored `connected` link is live again: `resumeConnected` restarted the heartbeat the stop disarmed.
|
|
384
|
+
...(event.unrestorable === true || event.previous.status !== 'connected'
|
|
385
|
+
? []
|
|
386
|
+
: [arm('heartbeat', scope.delays.heartbeatMs)]),
|
|
387
|
+
logFailure(event.phase, STOP_FAILED_LABEL, event.view),
|
|
388
|
+
emit(),
|
|
389
|
+
],
|
|
390
|
+
},
|
|
391
|
+
'stop.succeeded': {
|
|
392
|
+
guard: ownsActivity('stop'),
|
|
393
|
+
apply: (state) => released(state),
|
|
394
|
+
effects: () => [teardown({ stopRemote: false, stopped: true }), apply({ kind: 'mark-detached' }), emit()],
|
|
395
|
+
},
|
|
396
|
+
'reattach.requested': {
|
|
397
|
+
guard: (state, _event, scope) => {
|
|
398
|
+
if (state.activity !== null)
|
|
399
|
+
return 'busy';
|
|
400
|
+
if (state.status !== 'connected' || !scope.tunnelAlive)
|
|
401
|
+
return 'not-connected';
|
|
402
|
+
return true;
|
|
403
|
+
},
|
|
404
|
+
apply: (state, event) => enter(state, 'reattach', event.token, progressOf('restarting-remote', { reason: event.reason })),
|
|
405
|
+
effects: (state, event) => requestEffects(state, event.token, 'reattach'),
|
|
406
|
+
},
|
|
407
|
+
'reattach.failed': {
|
|
408
|
+
guard: ownsActivity('reattach'),
|
|
409
|
+
apply: (state, event) => toError(state, event.view),
|
|
410
|
+
effects: (state) => [
|
|
411
|
+
apply({ kind: 'mark-detached' }),
|
|
412
|
+
closeLink(),
|
|
413
|
+
disarm('heartbeat'),
|
|
414
|
+
logFailure(state.phase),
|
|
415
|
+
emit(),
|
|
416
|
+
],
|
|
417
|
+
},
|
|
418
|
+
'reconnect.scheduled': {
|
|
419
|
+
guard: (state) => (state.desired === 'connected' ? true : 'illegal-transition'),
|
|
420
|
+
apply: (state, event) => move(state, {
|
|
421
|
+
reconnectAttempts: event.attempt,
|
|
422
|
+
progress: progressOf('reconnecting', { attempt: event.attempt, max: event.max }),
|
|
423
|
+
}),
|
|
424
|
+
effects: (_state, event) => [arm('reconnect', event.delayMs), emit()],
|
|
425
|
+
},
|
|
426
|
+
'reconnect.gaveup': {
|
|
427
|
+
guard: (state) => (state.desired === 'connected' ? true : 'illegal-transition'),
|
|
428
|
+
apply: (state, event) => move(state, { status: 'error', error: event.view, progress: null }),
|
|
429
|
+
effects: () => EMIT_ONLY,
|
|
430
|
+
},
|
|
431
|
+
'reconnect.skipped': {
|
|
432
|
+
// The executor already took the policy decision; the machine only records the outcome the Client renders.
|
|
433
|
+
// `progress` is deliberately left alone: today's `scheduleReconnect` patched only the status, so a dropped link
|
|
434
|
+
// that was not retried keeps the `reconnecting` line it had published.
|
|
435
|
+
guard: () => true,
|
|
436
|
+
apply: (state) => move(state, { status: 'disconnected' }),
|
|
437
|
+
effects: () => EMIT_ONLY,
|
|
438
|
+
},
|
|
439
|
+
'link.cancelled': {
|
|
440
|
+
guard: () => true,
|
|
441
|
+
// Today's `cancelLinkWork` clears unconditionally; the event carries no token, so nothing can be checked here.
|
|
442
|
+
apply: (state) => move(state, { epoch: state.epoch + 1, activity: null }),
|
|
443
|
+
effects: () => [log('debug', 'link work cancelled')],
|
|
444
|
+
},
|
|
445
|
+
shutdown: {
|
|
446
|
+
guard: () => true,
|
|
447
|
+
// `activity: null` is today's `cancelLinkWork`; a leftover action name would describe work that no longer runs.
|
|
448
|
+
apply: (state) => move(state, { closing: true, desired: 'idle', epoch: state.epoch + 1, activity: null }),
|
|
449
|
+
effects: (_state, _event, scope) => [
|
|
450
|
+
disarm('reconnect'),
|
|
451
|
+
disarm('idle'),
|
|
452
|
+
disarm('heartbeat'),
|
|
453
|
+
disarm('remoteCheck'),
|
|
454
|
+
settlePrompts(),
|
|
455
|
+
teardown({ stopRemote: scope.keepAlive === 'stop', shutdown: true }),
|
|
456
|
+
],
|
|
457
|
+
},
|
|
458
|
+
'host.removed': {
|
|
459
|
+
guard: () => true,
|
|
460
|
+
apply: (state) => move(state, { desired: 'idle', activity: null, epoch: state.epoch + 1 }),
|
|
461
|
+
effects: (_state, event) => [
|
|
462
|
+
disarm('reconnect'),
|
|
463
|
+
disarm('idle'),
|
|
464
|
+
settlePrompts(),
|
|
465
|
+
teardown({ stopRemote: event.keepAlive === 'stop' }),
|
|
466
|
+
emit(),
|
|
467
|
+
],
|
|
468
|
+
},
|
|
469
|
+
};
|
|
470
|
+
/** The state the "the transport dropped under a live link" branch produces. */
|
|
471
|
+
function transportDropPatch(state) {
|
|
472
|
+
return {
|
|
473
|
+
status: 'disconnected',
|
|
474
|
+
transportLost: true,
|
|
475
|
+
activity: null,
|
|
476
|
+
epoch: state.epoch + 1,
|
|
477
|
+
phase: null,
|
|
478
|
+
progress: progressOf('reconnecting', { attempt: state.reconnectAttempts + 1 }),
|
|
479
|
+
};
|
|
480
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** Per-host SSH password storage in DSH's own credential store (`ctx.credentials`), never in `hosts.json`; with no
|
|
2
|
+
* credential service nothing is persisted. The record API and the `<scope>/<id>` grammar live in `kernel-adapter.ts`.
|
|
3
|
+
* A moved grammar (`changed`) does not change the keys this plugin generates — the stored form is the one already on
|
|
4
|
+
* users' machines — but it is reported once, because a later refused save must not look like a silent no-op. A service
|
|
5
|
+
* whose members changed shape gets the same warning: the store answers "not stored" as without a service, and silence
|
|
6
|
+
* would look like amnesia. */
|
|
7
|
+
import { credentialRecordKey, createKernelAdapter } from './kernel-adapter.js';
|
|
8
|
+
import { HOST_ID_RE } from './settings.js';
|
|
9
|
+
import { isPlainObject } from './util.js';
|
|
10
|
+
const RECORD_SCOPE = 'dsh-remote-go';
|
|
11
|
+
const RECORD_ID_PREFIX = 'ssh-password-';
|
|
12
|
+
const PAYLOAD_VERSION = 1;
|
|
13
|
+
/** DSH addresses a record as `<scope>/<id>` and both segments must match `/^[a-z][a-z0-9-]*$/` (`parseCredentialKey` in
|
|
14
|
+
* `@deepseek-ai/dsh-credentials`): a colon-separated key would be stored as given and rejected on the next read,
|
|
15
|
+
* failing every other credential with it. A host id is `h_<base36>`, whose only out-of-grammar character is the
|
|
16
|
+
* underscore; it maps onto `-` without collisions (the suffix never contains one), so only our ids are addressable. */
|
|
17
|
+
export function passwordRecordKey(hostId) {
|
|
18
|
+
if (typeof hostId !== 'string' || !HOST_ID_RE.test(hostId))
|
|
19
|
+
return null;
|
|
20
|
+
return credentialRecordKey(RECORD_SCOPE, RECORD_ID_PREFIX + hostId.replace(/_/g, '-'));
|
|
21
|
+
}
|
|
22
|
+
/** Build the store for one plugin activation; every method answers "not stored"/"not saved" instead of throwing. */
|
|
23
|
+
export function createSecretStore(options = {}) {
|
|
24
|
+
const logger = options.logger ?? console;
|
|
25
|
+
const kernel = createKernelAdapter(options.ctx);
|
|
26
|
+
let grammarChecked = false;
|
|
27
|
+
let serviceChangedNoted = false;
|
|
28
|
+
// Report a moved key grammar once per store. The keys keep their stored form on purpose: a changed
|
|
29
|
+
// grammar is a kernel we do not recognize, not a reason to orphan every saved password.
|
|
30
|
+
function noteGrammar() {
|
|
31
|
+
if (grammarChecked)
|
|
32
|
+
return;
|
|
33
|
+
grammarChecked = true;
|
|
34
|
+
const probe = kernel.credentialKeyGrammar();
|
|
35
|
+
if (probe.state !== 'changed')
|
|
36
|
+
return;
|
|
37
|
+
logger.warn?.('[dsh-remote-go] the kernel credential key grammar changed; keys keep their stored form and saves may stop working:', probe.detail);
|
|
38
|
+
}
|
|
39
|
+
/** The record API, or `null` when this composition has no credential service. */
|
|
40
|
+
function credentials() {
|
|
41
|
+
const probe = kernel.credentialService();
|
|
42
|
+
if (probe.state === 'ok' && probe.value !== null) {
|
|
43
|
+
noteGrammar();
|
|
44
|
+
return probe.value;
|
|
45
|
+
}
|
|
46
|
+
// A present service whose members changed shape is not "no credential service": answering null
|
|
47
|
+
// silently would present every remembered password as forgotten. Report it once per store.
|
|
48
|
+
if (probe.state === 'changed') {
|
|
49
|
+
if (!serviceChangedNoted) {
|
|
50
|
+
serviceChangedNoted = true;
|
|
51
|
+
logger.warn?.('[dsh-remote-go] the kernel credential service changed shape; saved passwords cannot be read or written:', probe.detail);
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
if (probe.reason === 'lookup-failed') {
|
|
56
|
+
logger.warn?.('[dsh-remote-go] credential service lookup failed:', probe.detail);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
available() {
|
|
62
|
+
return credentials() !== null;
|
|
63
|
+
},
|
|
64
|
+
async get(hostId) {
|
|
65
|
+
const service = credentials();
|
|
66
|
+
const key = passwordRecordKey(hostId);
|
|
67
|
+
if (!service || key === null)
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
const record = await service.readRecord(key);
|
|
71
|
+
const payload = record && record.kind === 'grant' ? record.payload : null;
|
|
72
|
+
// A stored payload is untrusted JSON from the credential document: narrow it, never cast it.
|
|
73
|
+
if (!isPlainObject(payload))
|
|
74
|
+
return null;
|
|
75
|
+
if (payload.version !== PAYLOAD_VERSION || typeof payload.password !== 'string')
|
|
76
|
+
return null;
|
|
77
|
+
return payload.password === '' ? null : payload.password;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
logger.warn?.('[dsh-remote-go] could not read the saved password:', error);
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
async has(hostId) {
|
|
85
|
+
return (await this.get(hostId)) !== null;
|
|
86
|
+
},
|
|
87
|
+
async set(hostId, password) {
|
|
88
|
+
const service = credentials();
|
|
89
|
+
const key = passwordRecordKey(hostId);
|
|
90
|
+
if (!service || key === null || typeof password !== 'string' || password === '')
|
|
91
|
+
return false;
|
|
92
|
+
try {
|
|
93
|
+
await service.modifyRecord(key, async () => ({
|
|
94
|
+
kind: 'grant',
|
|
95
|
+
payload: { version: PAYLOAD_VERSION, password },
|
|
96
|
+
}));
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
logger.warn?.('[dsh-remote-go] could not save the password:', error);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
async remove(hostId) {
|
|
105
|
+
const service = credentials();
|
|
106
|
+
const key = passwordRecordKey(hostId);
|
|
107
|
+
if (!service || key === null || typeof service.deleteRecord !== 'function')
|
|
108
|
+
return false;
|
|
109
|
+
try {
|
|
110
|
+
await service.deleteRecord(key);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
logger.warn?.('[dsh-remote-go] could not delete the saved password:', error);
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|