dsh-remote-go 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -95
- package/{client.js → dist/client.js} +7805 -5486
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/dist/src/constants.js +67 -0
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +40 -21
- package/vendor/THIRD_PARTY_NOTICES.md +27 -1
- package/src/api.js +0 -340
- package/src/atomic-json.js +0 -208
- package/src/bootstrap.js +0 -100
- package/src/connect-log.js +0 -39
- package/src/connect-pipeline.js +0 -463
- package/src/connect-progress.js +0 -135
- package/src/constants.js +0 -31
- package/src/diagnostics.js +0 -157
- package/src/download-jobs.js +0 -217
- package/src/errors.js +0 -316
- package/src/host-lifecycle.js +0 -187
- package/src/host-registry.js +0 -145
- package/src/host-runtime.js +0 -53
- package/src/host-view.js +0 -51
- package/src/index.js +0 -162
- package/src/key-files.js +0 -49
- package/src/known-hosts.js +0 -94
- package/src/layout.js +0 -35
- package/src/local-download.js +0 -162
- 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 -89
- package/src/prompts.js +0 -132
- package/src/redact.js +0 -54
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -73
- package/src/remote-proc.js +0 -208
- package/src/remote-web.js +0 -300
- package/src/secrets.js +0 -79
- package/src/service.js +0 -250
- package/src/settings.js +0 -135
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -652
- package/src/store.js +0 -143
- package/src/tunnel.js +0 -73
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -28
- package/vendor/ssh2-entry.mjs +0 -12
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
package/src/connect-pipeline.js
DELETED
|
@@ -1,463 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The connect pipeline: SSH -> probe -> managed Node -> plan -> adopt/conflict -> preflight -> start -> tunnel -> verify; the step order is the behavior and every step is injected.
|
|
3
|
-
*/
|
|
4
|
-
import { REMOTE_START_TIMEOUT_MS, SSH_READY_TIMEOUT_MS, TUNNEL_PROBE_TIMEOUT_MS } from './constants.js'
|
|
5
|
-
import { errors, toErrorView } from './errors.js'
|
|
6
|
-
import { runtimeOf } from './host-registry.js'
|
|
7
|
-
import {
|
|
8
|
-
cancelIdleStop,
|
|
9
|
-
cancelReconnect,
|
|
10
|
-
closeTunnel,
|
|
11
|
-
disconnectHost,
|
|
12
|
-
disposeSsh,
|
|
13
|
-
scheduleReconnect,
|
|
14
|
-
teardownRuntime,
|
|
15
|
-
} from './host-lifecycle.js'
|
|
16
|
-
import { findKnownHostEntries, parseKnownHosts } from './known-hosts.js'
|
|
17
|
-
import { remoteLayout } from './layout.js'
|
|
18
|
-
import { applyManagedNodeFacts, managedNodeVersionFromEngine } from './node-runtime.js'
|
|
19
|
-
import { askConflict, requestHostKey } from './prompts.js'
|
|
20
|
-
import { resolveDestination } from './ssh-config.js'
|
|
21
|
-
import { isPid, messageOf, nowIso } from './util.js'
|
|
22
|
-
import { normalizeVersion, versionsEqual } from './version.js'
|
|
23
|
-
|
|
24
|
-
function describeTarget(entry) {
|
|
25
|
-
const user = entry.username ? `${entry.username}@` : ''
|
|
26
|
-
const port = entry.port && entry.port !== 22 ? `:${entry.port}` : ''
|
|
27
|
-
return `${user}${entry.address || '?'}${port}`
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Accept a connect request: apply the one-time inputs, stop any pending retry,
|
|
32
|
-
* and start the pipeline in the background. Progress travels on `state`.
|
|
33
|
-
*/
|
|
34
|
-
export async function startConnect(ctx, id, options = {}) {
|
|
35
|
-
await ctx.init()
|
|
36
|
-
const runtime = runtimeOf(ctx, id)
|
|
37
|
-
if (runtime.connectPromise) return { accepted: false, reason: 'already-connecting' }
|
|
38
|
-
if (runtime.status === 'connected' && runtime.ssh?.connected) return { accepted: false, reason: 'already-connected' }
|
|
39
|
-
runtime.password = options.password ?? runtime.password
|
|
40
|
-
// A password the user asked to remember is written to the credential store the
|
|
41
|
-
// moment it is known, so a failed first attempt still leaves it available.
|
|
42
|
-
if (options.rememberPassword === true && typeof runtime.password === 'string' && runtime.password !== '') {
|
|
43
|
-
runtime.savePassword = true
|
|
44
|
-
}
|
|
45
|
-
runtime.desired = 'connected'
|
|
46
|
-
cancelReconnect(runtime)
|
|
47
|
-
ctx.progress.log(
|
|
48
|
-
runtime,
|
|
49
|
-
'info',
|
|
50
|
-
`connect: ${describeTarget(runtime.entry)} (auth=${runtime.entry.auth?.type ?? 'key'}${
|
|
51
|
-
runtime.entry.jumpHost ? `, jump=${runtime.entry.jumpHost}` : ''
|
|
52
|
-
})`,
|
|
53
|
-
)
|
|
54
|
-
runtime.connectPromise = connectPipeline(ctx, runtime).finally(() => {
|
|
55
|
-
runtime.connectPromise = null
|
|
56
|
-
})
|
|
57
|
-
return { accepted: true }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Drop the current link and connect again from scratch (the "Reconnect" button). */
|
|
61
|
-
export async function reconnectHost(ctx, runtime) {
|
|
62
|
-
await disconnectHost(ctx, runtime, { stopRemote: false })
|
|
63
|
-
cancelIdleStop(runtime)
|
|
64
|
-
await disposeSsh(runtime)
|
|
65
|
-
return startConnect(ctx, runtime.id, {})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Re-enter the pipeline after a dropped link. Lives here rather than in the
|
|
70
|
-
* lifecycle module so `scheduleReconnect` stays a pure timer with a callback.
|
|
71
|
-
*/
|
|
72
|
-
async function reconnectNow(ctx, runtime) {
|
|
73
|
-
if (ctx.state.disposed || runtime.desired !== 'connected') return
|
|
74
|
-
await disposeSsh(runtime)
|
|
75
|
-
runtime.connectPromise = connectPipeline(ctx, runtime).finally(() => {
|
|
76
|
-
runtime.connectPromise = null
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function attachDisconnectHandler(ctx, runtime) {
|
|
81
|
-
const ssh = runtime.ssh
|
|
82
|
-
if (!ssh) return
|
|
83
|
-
ssh.on('close', () => {
|
|
84
|
-
if (runtime.closing || runtime.desired !== 'connected' || runtime.status !== 'connected') return
|
|
85
|
-
runtime.status = 'disconnected'
|
|
86
|
-
runtime.phase = null
|
|
87
|
-
runtime.remote.uiReady = false
|
|
88
|
-
runtime.remote.localPort = null
|
|
89
|
-
runtime.progress = { code: 'reconnecting', params: { attempt: runtime.reconnectAttempts + 1 } }
|
|
90
|
-
ctx.emitState()
|
|
91
|
-
void closeTunnel(runtime)
|
|
92
|
-
scheduleReconnect(ctx, runtime, () => void reconnectNow(ctx, runtime))
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** Resolve the SSH destination from the entry + local ssh config, before dialling. */
|
|
97
|
-
async function resolveTargetForRuntime(ctx, runtime) {
|
|
98
|
-
const material = await ctx.deps.loadSshMaterial({ home: ctx.userHome() })
|
|
99
|
-
const entry = runtime.entry
|
|
100
|
-
const resolved = resolveDestination(entry.address, { configText: material.configText, home: ctx.userHome() })
|
|
101
|
-
const host = resolved.host || entry.address
|
|
102
|
-
const port = entry.port && entry.port !== 22 ? entry.port : (resolved.port ?? 22)
|
|
103
|
-
const knownFingerprints = findKnownHostEntries(parseKnownHosts(material.knownHostsText), host, port).map(
|
|
104
|
-
(item) => item.fingerprint,
|
|
105
|
-
)
|
|
106
|
-
return {
|
|
107
|
-
host,
|
|
108
|
-
port,
|
|
109
|
-
username: entry.username || resolved.user || null,
|
|
110
|
-
identityFiles: resolved.identityFiles ?? [],
|
|
111
|
-
knownFingerprints,
|
|
112
|
-
jumpHost: entry.jumpHost
|
|
113
|
-
? resolveDestination(entry.jumpHost, { configText: material.configText, home: ctx.userHome() })
|
|
114
|
-
: null,
|
|
115
|
-
entry,
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Open the transport: direct, or through a jump host with a forwarded stream. */
|
|
120
|
-
async function openTransport(ctx, runtime, target) {
|
|
121
|
-
const settings = ctx.settings()
|
|
122
|
-
const buildOptions = (resolved, sock) => ({
|
|
123
|
-
host: resolved.host,
|
|
124
|
-
port: resolved.port ?? 22,
|
|
125
|
-
username: resolved.username ?? resolved.user ?? null,
|
|
126
|
-
// The key path expands against the *user* home (`~/.ssh/...`), never against
|
|
127
|
-
// DSH_HOME, so the SSH layer needs to know which one that is.
|
|
128
|
-
home: ctx.userHome(),
|
|
129
|
-
auth: {
|
|
130
|
-
type: runtime.entry.auth?.type ?? 'key',
|
|
131
|
-
keyPath: runtime.entry.auth?.keyPath ?? null,
|
|
132
|
-
password: runtime.password ?? null,
|
|
133
|
-
},
|
|
134
|
-
identityFiles: resolved.identityFiles ?? [],
|
|
135
|
-
hostKeyPolicy: runtime.entry.hostKeyPolicy ?? settings.hostKeyPolicy,
|
|
136
|
-
pinnedFingerprint: runtime.entry.pinnedFingerprint ?? null,
|
|
137
|
-
knownHostFingerprints: resolved.knownFingerprints ?? [],
|
|
138
|
-
acceptUnknownHostKey: false,
|
|
139
|
-
readyTimeoutMs: settings.connectTimeoutSec * 1000 || SSH_READY_TIMEOUT_MS,
|
|
140
|
-
...(sock ? { sock } : {}),
|
|
141
|
-
onHostKey: (info) => requestHostKey(ctx, runtime, info),
|
|
142
|
-
})
|
|
143
|
-
if (!target.jumpHost) {
|
|
144
|
-
runtime.ssh = ctx.deps.sshFactory(buildOptions(target))
|
|
145
|
-
ctx.progress.phase(runtime, 'ssh', 'ssh-connecting', { host: target.host })
|
|
146
|
-
captureFingerprint(ctx, runtime, await runtime.ssh.connect())
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
ctx.progress.phase(runtime, 'ssh', 'ssh-connecting', { host: target.jumpHost.host, jump: true })
|
|
150
|
-
const jumpTarget = {
|
|
151
|
-
...target.jumpHost,
|
|
152
|
-
username: target.jumpHost.user ?? target.username,
|
|
153
|
-
knownFingerprints: findKnownHostEntries(
|
|
154
|
-
parseKnownHosts((await ctx.deps.loadSshMaterial({ home: ctx.userHome() })).knownHostsText),
|
|
155
|
-
target.jumpHost.host,
|
|
156
|
-
target.jumpHost.port ?? 22,
|
|
157
|
-
).map((item) => item.fingerprint),
|
|
158
|
-
}
|
|
159
|
-
runtime.jumpSsh = ctx.deps.sshFactory(buildOptions(jumpTarget))
|
|
160
|
-
await runtime.jumpSsh.connect()
|
|
161
|
-
ctx.progress.phase(runtime, 'ssh', 'ssh-connecting', { host: target.host, via: target.jumpHost.host })
|
|
162
|
-
const stream = await runtime.jumpSsh.forwardOut(target.host, target.port ?? 22)
|
|
163
|
-
runtime.ssh = ctx.deps.sshFactory(buildOptions(target, stream))
|
|
164
|
-
captureFingerprint(ctx, runtime, await runtime.ssh.connect())
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** Remember the session fingerprint so the host card shows it even without a pin. */
|
|
168
|
-
function captureFingerprint(ctx, runtime, info) {
|
|
169
|
-
if (!info?.fingerprint) return
|
|
170
|
-
const pinned = runtime.entry.pinnedFingerprint ?? null
|
|
171
|
-
runtime.fingerprint = {
|
|
172
|
-
sha256: info.fingerprint,
|
|
173
|
-
keyType: info.keyType ?? 'unknown',
|
|
174
|
-
status: pinned === info.fingerprint ? 'pinned' : pinned ? 'changed' : 'observed',
|
|
175
|
-
pinned,
|
|
176
|
-
}
|
|
177
|
-
ctx.progress.log(
|
|
178
|
-
runtime,
|
|
179
|
-
pinned === info.fingerprint ? 'debug' : 'info',
|
|
180
|
-
`host key ${info.keyType ?? 'unknown'}: ${info.fingerprint}${
|
|
181
|
-
pinned === info.fingerprint ? ' (matches the pinned key)' : pinned ? ' (CHANGED from the pinned key)' : ''
|
|
182
|
-
}`,
|
|
183
|
-
)
|
|
184
|
-
ctx.emitState()
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Warn when the remote already runs a DSH this plugin did not start, and let the
|
|
189
|
-
* user decide: stop it, keep it alongside, or cancel.
|
|
190
|
-
*/
|
|
191
|
-
async function resolveForeignConflict(ctx, runtime, layout, profile) {
|
|
192
|
-
const ignorePids = []
|
|
193
|
-
try {
|
|
194
|
-
const registry = await ctx.deps.readRegistry(runtime.ssh, layout)
|
|
195
|
-
const pid = Number(registry?.pid)
|
|
196
|
-
if (isPid(pid) && registry?.dshHome === layout.dshHome && registry?.profile === profile) ignorePids.push(pid)
|
|
197
|
-
} catch (error) {
|
|
198
|
-
// swallow-ok: registry absence is normal; the scan below still finds foreign processes
|
|
199
|
-
ctx.progress.log(runtime, 'debug', 'foreign DSH: registry read failed:', messageOf(error))
|
|
200
|
-
}
|
|
201
|
-
const found = await ctx.deps.findForeign(runtime.ssh, { ignorePids })
|
|
202
|
-
if (found.ok !== true) {
|
|
203
|
-
ctx.progress.log(runtime, 'debug', 'foreign DSH: scan failed:', found.reason ?? found.technical ?? 'unknown')
|
|
204
|
-
return
|
|
205
|
-
}
|
|
206
|
-
if (found.processes.length === 0) return
|
|
207
|
-
const pidList = found.processes.map((entry) => `pid ${entry.pid}`).join(', ')
|
|
208
|
-
ctx.progress.log(
|
|
209
|
-
runtime,
|
|
210
|
-
'warn',
|
|
211
|
-
`foreign DSH on the remote: ${found.processes.length} process(es) sharing this ~/.dsh (${pidList})`,
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
const decision = await askConflict(ctx, runtime, found.processes)
|
|
215
|
-
if (decision.action === 'cancel') throw errors.remoteDshRunning()
|
|
216
|
-
if (decision.action === 'alongside') {
|
|
217
|
-
runtime.warning = {
|
|
218
|
-
zh: '远端已有手工启动的 DSH;已按你的选择并存运行,两者共享 ~/.dsh,请避免同时操作同一会话。',
|
|
219
|
-
en: 'A manually started DSH is already running; it was kept as you chose. Both share ~/.dsh, so avoid editing the same session in both.',
|
|
220
|
-
}
|
|
221
|
-
return
|
|
222
|
-
}
|
|
223
|
-
const stopped = await ctx.deps.stopForeign(runtime.ssh, found.processes)
|
|
224
|
-
if (stopped.ok !== true) {
|
|
225
|
-
throw errors.remoteDshRunning(
|
|
226
|
-
{
|
|
227
|
-
zh: '远端的手工 DSH 进程没有停下来,连接已中止。',
|
|
228
|
-
en: 'The manual remote DSH process did not stop, so the connection was aborted.',
|
|
229
|
-
},
|
|
230
|
-
{
|
|
231
|
-
technical: `alive=${stopped.alive.join(',') || 'unknown'} output=${String(stopped.output ?? '').slice(0, 800)}`,
|
|
232
|
-
},
|
|
233
|
-
)
|
|
234
|
-
}
|
|
235
|
-
ctx.progress.log(runtime, 'info', `stopped ${found.processes.length} manual remote DSH process(es)`)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async function connectPipeline(ctx, runtime) {
|
|
239
|
-
const log = (level, ...args) => ctx.progress.log(runtime, level, ...args)
|
|
240
|
-
const phase = (name, code, params) => ctx.progress.phase(runtime, name, code, params)
|
|
241
|
-
runtime.closing = false
|
|
242
|
-
runtime.reconnectAttempts = 0
|
|
243
|
-
ctx.progress.set(runtime, { status: 'connecting', error: null, warning: null })
|
|
244
|
-
let remoteStarted = false
|
|
245
|
-
try {
|
|
246
|
-
const target = await resolveTargetForRuntime(ctx, runtime)
|
|
247
|
-
const viaJump = target.jumpHost ? ` via ${target.jumpHost.host}` : ''
|
|
248
|
-
log('debug', `target resolved: ${target.username ?? '(default)'}@${target.host}:${target.port}${viaJump}`)
|
|
249
|
-
// A password-auth host may have one remembered: load it before the transport
|
|
250
|
-
// is opened, so reconnect and background retries work without a prompt.
|
|
251
|
-
if (runtime.entry.auth?.type === 'password' && !runtime.password && ctx.secrets) {
|
|
252
|
-
const remembered = await ctx.secrets.get(runtime.id)
|
|
253
|
-
if (remembered) {
|
|
254
|
-
runtime.password = remembered
|
|
255
|
-
runtime.passwordSaved = true
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
phase('ssh', 'ssh-connecting', { host: runtime.entry.name })
|
|
259
|
-
await openTransport(ctx, runtime, target)
|
|
260
|
-
log(
|
|
261
|
-
'info',
|
|
262
|
-
`SSH connected to ${target.host}:${target.port} as ${target.username ?? '(default)'} (fingerprint ${
|
|
263
|
-
runtime.fingerprint?.sha256 ?? 'unknown'
|
|
264
|
-
})`,
|
|
265
|
-
)
|
|
266
|
-
|
|
267
|
-
phase('probe', 'probing')
|
|
268
|
-
let facts = await ctx.deps.probe(runtime.ssh)
|
|
269
|
-
const freeGb = Math.round(Number(facts.diskFreeKb ?? 0) / 1_048_576)
|
|
270
|
-
log(
|
|
271
|
-
'debug',
|
|
272
|
-
`probe: ${facts.os}/${facts.arch} · ${String(facts.osName ?? 'unknown OS')} · node ${
|
|
273
|
-
facts.nodeVersion ?? 'not found'
|
|
274
|
-
} · ${freeGb} GB free`,
|
|
275
|
-
)
|
|
276
|
-
// The runtime always runs on a plugin-owned Node (`~/.dsh/remote-go/node`),
|
|
277
|
-
// whether or not the remote already has one: a shared system/nvm Node is
|
|
278
|
-
// exactly what makes "works for my other project" break the remote DSH.
|
|
279
|
-
// There is no system fallback on purpose, so this failure is the connection's.
|
|
280
|
-
const profile = ctx.settings().bootstrap.profile
|
|
281
|
-
const managedVersion = managedNodeVersionFromEngine(ctx.engineNodeRange())
|
|
282
|
-
const nodeLayout = remoteLayout(facts.home)
|
|
283
|
-
log('info', `managed Node: expecting v${managedVersion} in ${nodeLayout.nodeDir}`)
|
|
284
|
-
const managed = await ctx.deps.ensureNode(runtime.ssh, {
|
|
285
|
-
facts,
|
|
286
|
-
layout: nodeLayout,
|
|
287
|
-
version: managedVersion,
|
|
288
|
-
onProgress: (event) => ctx.progress.nodeProgress(runtime, event),
|
|
289
|
-
})
|
|
290
|
-
facts = applyManagedNodeFacts(facts, managed)
|
|
291
|
-
log(
|
|
292
|
-
'info',
|
|
293
|
-
`managed Node ${managed.version}${managed.installed ? ' installed' : ' reused'} at ${managed.nodeBinDir}`,
|
|
294
|
-
)
|
|
295
|
-
runtime.facts = facts
|
|
296
|
-
const targetVersion = ctx.localVersion()
|
|
297
|
-
if (!targetVersion) {
|
|
298
|
-
throw errors.invalidInput('version', {
|
|
299
|
-
zh: '无法确定本机 DSH 版本,无法用 npx 固定同版本启动远端。请从 DSH 界面启动本插件,让 Host 读到本机版本。',
|
|
300
|
-
en: 'The local DSH version is unknown, so the remote cannot be pinned to the same version. Start the plugin from the DSH UI so the Host can read the local version.',
|
|
301
|
-
})
|
|
302
|
-
}
|
|
303
|
-
const plan = ctx.deps.plan({ facts, targetVersion, profile })
|
|
304
|
-
log('info', `runtime plan ${plan.level}: ${plan.reason.en}`)
|
|
305
|
-
|
|
306
|
-
phase('start', 'starting-remote', { level: plan.level })
|
|
307
|
-
const layout = remoteLayout(runtime.facts?.home ?? null)
|
|
308
|
-
let webInfo = null
|
|
309
|
-
// Adopt an instance left running by a previous local process (keep-alive).
|
|
310
|
-
// Checked before the npx preflight: an adoptable instance should not require
|
|
311
|
-
// registry access just to reconnect.
|
|
312
|
-
try {
|
|
313
|
-
const adopted = await ctx.deps.adopt(runtime.ssh, layout, { profile })
|
|
314
|
-
const adoptedVersion = adopted?.version ? normalizeVersion(adopted.version) : null
|
|
315
|
-
if (adopted && (!adoptedVersion || versionsEqual(adoptedVersion, targetVersion))) {
|
|
316
|
-
webInfo = { ...adopted, logPath: layout.logFile, startedAt: nowIso(), rawLine: '' }
|
|
317
|
-
log('info', `adopted running remote web pid=${adopted.pid} port=${adopted.port}`)
|
|
318
|
-
} else if (adopted) {
|
|
319
|
-
log(
|
|
320
|
-
'info',
|
|
321
|
-
`running remote web version ${adoptedVersion} != ${targetVersion}; stopping it and starting a matching instance`,
|
|
322
|
-
)
|
|
323
|
-
// The old process owns the same `~/.dsh` profile; leaving it running next
|
|
324
|
-
// to the new one would share session files with a stale version.
|
|
325
|
-
try {
|
|
326
|
-
await ctx.deps.stopWeb(runtime.ssh, layout, { pid: adopted.pid })
|
|
327
|
-
} catch (error) {
|
|
328
|
-
log('warn', 'could not stop the stale remote web:', messageOf(error))
|
|
329
|
-
}
|
|
330
|
-
} else {
|
|
331
|
-
log('debug', 'adopt: no running remote web to reuse')
|
|
332
|
-
}
|
|
333
|
-
} catch (error) {
|
|
334
|
-
log('debug', 'adopt check failed:', messageOf(error))
|
|
335
|
-
}
|
|
336
|
-
if (!webInfo) {
|
|
337
|
-
// The registry row we refused to adopt (stale launch marker, unresponsive
|
|
338
|
-
// HTTP, ...) still names a process on this ~/.dsh profile. Starting a second
|
|
339
|
-
// one would let two clients write the same session files.
|
|
340
|
-
const leftover = await ctx.deps.readRegistry(runtime.ssh, layout)
|
|
341
|
-
const leftoverPid = Number(leftover?.pid ?? 0)
|
|
342
|
-
if (leftover?.dshHome === layout.dshHome && leftover?.profile === profile && isPid(leftoverPid)) {
|
|
343
|
-
try {
|
|
344
|
-
await ctx.deps.stopWeb(runtime.ssh, layout, { pid: leftoverPid })
|
|
345
|
-
log('info', `stopped a non-adoptable remote web instance pid=${leftoverPid}`)
|
|
346
|
-
} catch (error) {
|
|
347
|
-
log('warn', 'could not stop the non-adoptable remote web:', messageOf(error))
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
await resolveForeignConflict(ctx, runtime, layout, profile)
|
|
351
|
-
phase('bootstrap', 'bootstrapping', { level: plan.level })
|
|
352
|
-
// Resolve the exact package in the foreground first: a cold npx download
|
|
353
|
-
// belongs in the connect error, not in a detached process that only leaves a
|
|
354
|
-
// log behind. This also warms the cache for the real start below.
|
|
355
|
-
log('info', `npx preflight: resolving @deepseek-ai/dsh@${plan.start.targetVersion}`)
|
|
356
|
-
const onStdout = ctx.progress.bootstrapReporter(runtime, plan.start.targetVersion)
|
|
357
|
-
const onStderr = ctx.progress.bootstrapReporter(runtime, plan.start.targetVersion)
|
|
358
|
-
await ctx.deps.preflight(runtime.ssh, plan, { onStdout, onStderr })
|
|
359
|
-
log('info', `npx preflight ready: @deepseek-ai/dsh@${plan.start.targetVersion}`)
|
|
360
|
-
phase('start', 'starting-remote', { level: plan.level })
|
|
361
|
-
webInfo = await ctx.deps.startWeb(runtime.ssh, {
|
|
362
|
-
plan,
|
|
363
|
-
layout,
|
|
364
|
-
profile,
|
|
365
|
-
timeoutMs: REMOTE_START_TIMEOUT_MS,
|
|
366
|
-
onLine: (line) => log('debug', `remote: ${line}`),
|
|
367
|
-
})
|
|
368
|
-
log('info', `remote DSH started: pid ${webInfo.pid ?? '?'}, port ${webInfo.port}`)
|
|
369
|
-
}
|
|
370
|
-
if (webInfo.browserCarrier === 'patched') {
|
|
371
|
-
log(
|
|
372
|
-
'info',
|
|
373
|
-
'the remote profile disables api-remotes (browser Remote-Event carrier); re-enabled it for this launch',
|
|
374
|
-
)
|
|
375
|
-
}
|
|
376
|
-
remoteStarted = true
|
|
377
|
-
runtime.token = webInfo.token
|
|
378
|
-
runtime.remote = {
|
|
379
|
-
version: targetVersion,
|
|
380
|
-
uiReady: false,
|
|
381
|
-
localPort: null,
|
|
382
|
-
port: webInfo.port,
|
|
383
|
-
pid: webInfo.pid ?? null,
|
|
384
|
-
dshHome: layout.dshHome,
|
|
385
|
-
profile,
|
|
386
|
-
bootstrapLevel: plan.level,
|
|
387
|
-
nodeVersion: facts.nodeVersion,
|
|
388
|
-
nodeSource: 'managed',
|
|
389
|
-
startedAt: webInfo.startedAt ?? nowIso(),
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
phase('tunnel', 'tunneling')
|
|
393
|
-
runtime.tunnel = await ctx.deps.tunnelFactory({ ssh: runtime.ssh, remotePort: webInfo.port })
|
|
394
|
-
runtime.remote.localPort = runtime.tunnel.localPort
|
|
395
|
-
log('info', `tunnel open: 127.0.0.1:${runtime.tunnel.localPort} -> 127.0.0.1:${webInfo.port}`)
|
|
396
|
-
|
|
397
|
-
phase('verifying', 'verifying')
|
|
398
|
-
log('debug', `verifying the remote UI on 127.0.0.1:${webInfo.port}`)
|
|
399
|
-
const ui = await ctx.deps.waitUi(runtime.ssh, webInfo.port, { attempts: 20, intervalMs: 500 })
|
|
400
|
-
if (!ui.ok) {
|
|
401
|
-
throw errors.remoteUiUnreachable({
|
|
402
|
-
zh: `远端 DSH 的本地 127.0.0.1:${webInfo.port} 没有响应(HTTP ${ui.status ?? '无'})。`,
|
|
403
|
-
en: `The remote DSH on 127.0.0.1:${webInfo.port} did not respond (HTTP ${ui.status ?? 'none'}).`,
|
|
404
|
-
})
|
|
405
|
-
}
|
|
406
|
-
log('debug', `remote UI answered HTTP ${ui.status}; exchanging the launch token`)
|
|
407
|
-
const exchange = await ctx.deps.exchange(runtime.tunnel.localPort, webInfo.token, {
|
|
408
|
-
timeoutMs: TUNNEL_PROBE_TIMEOUT_MS,
|
|
409
|
-
})
|
|
410
|
-
runtime.exchange = exchange
|
|
411
|
-
if (!exchange.ok) {
|
|
412
|
-
throw errors.remoteUiUnreachable({
|
|
413
|
-
zh: `本地隧道完成了 token 交换,但远端界面返回 HTTP ${exchange.status}(首次状态 ${exchange.firstStatus})。`,
|
|
414
|
-
en: `The tunnel exchanged the token but the remote UI answered HTTP ${exchange.status} (first ${exchange.firstStatus}).`,
|
|
415
|
-
})
|
|
416
|
-
}
|
|
417
|
-
// X-Frame-Options / CSP no longer matter: the remote UI is rendered by the
|
|
418
|
-
// desktop guest, not embedded in a frame, so a page that refuses framing is
|
|
419
|
-
// still a perfectly usable remote host.
|
|
420
|
-
runtime.remote.uiReady = true
|
|
421
|
-
await ctx.store.patchHost(runtime.id, { lastOpenedAt: nowIso() })
|
|
422
|
-
runtime.entry = ctx.store.getHost(runtime.id) ?? runtime.entry
|
|
423
|
-
if (ctx.settings().saveTokens) await ctx.store.saveToken(runtime.id, webInfo.token)
|
|
424
|
-
// Remember the password now that it is proven, if the user asked for that.
|
|
425
|
-
if (runtime.savePassword === true && typeof runtime.password === 'string' && ctx.secrets) {
|
|
426
|
-
runtime.passwordSaved = await ctx.secrets.set(runtime.id, runtime.password)
|
|
427
|
-
}
|
|
428
|
-
runtime.status = 'connected'
|
|
429
|
-
runtime.phase = 'ready'
|
|
430
|
-
runtime.progress = { code: 'ready' }
|
|
431
|
-
runtime.error = null
|
|
432
|
-
ctx.state.activeHostId = runtime.id
|
|
433
|
-
attachDisconnectHandler(ctx, runtime)
|
|
434
|
-
ctx.emitState()
|
|
435
|
-
log('info', `connected: remote ${runtime.remote.version} on 127.0.0.1:${runtime.tunnel.localPort}`)
|
|
436
|
-
} catch (error) {
|
|
437
|
-
const view = toErrorView(error)
|
|
438
|
-
if (runtime.desired !== 'connected') {
|
|
439
|
-
// A user-initiated disconnect/teardown resolved a pending prompt (or closed
|
|
440
|
-
// the window) while the pipeline was in flight: this is not a failure.
|
|
441
|
-
runtime.error = null
|
|
442
|
-
runtime.status = 'disconnected'
|
|
443
|
-
runtime.phase = null
|
|
444
|
-
runtime.progress = null
|
|
445
|
-
log('info', 'connect cancelled by disconnect')
|
|
446
|
-
} else {
|
|
447
|
-
runtime.error = view
|
|
448
|
-
runtime.status = 'error'
|
|
449
|
-
runtime.phase = null
|
|
450
|
-
runtime.progress = null
|
|
451
|
-
log('warn', `connect failed: ${view.code}`, messageOf(error))
|
|
452
|
-
}
|
|
453
|
-
runtime.remote.uiReady = false
|
|
454
|
-
runtime.remote.localPort = null
|
|
455
|
-
await teardownRuntime(ctx, runtime, {
|
|
456
|
-
stopRemote: remoteStarted && runtime.entry.keepAlive === 'stop',
|
|
457
|
-
keepRemote: remoteStarted && runtime.entry.keepAlive !== 'stop',
|
|
458
|
-
})
|
|
459
|
-
ctx.emitState()
|
|
460
|
-
} finally {
|
|
461
|
-
runtime.connectPromise = null
|
|
462
|
-
}
|
|
463
|
-
}
|
package/src/connect-progress.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Connect progress: the coalesced `/state` frame that carries log lines, the staged progress line and the npm / managed-Node reporters; it never sets a status.
|
|
3
|
-
*/
|
|
4
|
-
import { appendConnectLog, connectLogVisible } from './connect-log.js'
|
|
5
|
-
import { logVisible, nowIso } from './util.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Coalescing window for shipping log lines: the connect console may print many
|
|
9
|
-
* lines a second (npx download, remote stdout), and every one of them would
|
|
10
|
-
* otherwise be its own `/state` frame.
|
|
11
|
-
*/
|
|
12
|
-
const LOG_FLUSH_MS = 150
|
|
13
|
-
|
|
14
|
-
/** Throttle for the npx reporter: one progress frame per 700 ms at most. */
|
|
15
|
-
const REPORT_MIN_INTERVAL_MS = 700
|
|
16
|
-
|
|
17
|
-
function stripAnsi(text) {
|
|
18
|
-
return String(text ?? '').replace(/\u001B\[[0-9;]*m/g, '')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Turn one npm/npx output line into a progress event for the connect line.
|
|
23
|
-
* `npm_config_loglevel=http` makes npm report fetches without a TTY; known shapes
|
|
24
|
-
* get a localized progress code, anything else is kept (truncated) as a detail.
|
|
25
|
-
*/
|
|
26
|
-
export function describePreflightEvent(line, version) {
|
|
27
|
-
const text = stripAnsi(line).trim()
|
|
28
|
-
if (text === '') return null
|
|
29
|
-
// The package's own `--version` output is the success signal, not progress.
|
|
30
|
-
if (/^v?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(text)) return null
|
|
31
|
-
if (/The following package was not found and will be installed/i.test(text)) {
|
|
32
|
-
return { code: 'bootstrap-first-run', params: { version } }
|
|
33
|
-
}
|
|
34
|
-
if (/^npm (http fetch|http request)/i.test(text)) return { code: 'bootstrap-fetching' }
|
|
35
|
-
if (/added \d+ packages? in/i.test(text)) return { code: 'bootstrap-installed' }
|
|
36
|
-
return { code: 'bootstrap-progress', params: { detail: text.slice(0, 160) } }
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function createConnectProgress(options) {
|
|
40
|
-
let flushTimer = null
|
|
41
|
-
|
|
42
|
-
function log(runtime, level, ...args) {
|
|
43
|
-
const message = args.map((value) => (typeof value === 'string' ? value : JSON.stringify(value))).join(' ')
|
|
44
|
-
const entry = appendConnectLog(runtime, { at: nowIso(), level, message })
|
|
45
|
-
scheduleFlush(runtime)
|
|
46
|
-
// Verbosity is a runtime decision: `options.logLevel` (tests, diagnostics) or settings.
|
|
47
|
-
const threshold = options.logLevel ?? options.getSettings()?.logLevel ?? 'info'
|
|
48
|
-
if (!logVisible(level, threshold)) return
|
|
49
|
-
const method = options.logger[level] ?? options.logger.info
|
|
50
|
-
method?.(entry.line)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Ship the log tail to the Client's console without one `/state` frame per line. */
|
|
54
|
-
function scheduleFlush(runtime) {
|
|
55
|
-
if (!connectLogVisible(runtime.status)) return
|
|
56
|
-
if (flushTimer !== null) return
|
|
57
|
-
flushTimer = setTimeout(() => {
|
|
58
|
-
flushTimer = null
|
|
59
|
-
options.emitState()
|
|
60
|
-
}, LOG_FLUSH_MS)
|
|
61
|
-
// A pending log frame must never keep the process alive on its own.
|
|
62
|
-
flushTimer.unref?.()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function set(runtime, patch) {
|
|
66
|
-
Object.assign(runtime, patch)
|
|
67
|
-
options.emitState()
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function phase(runtime, phaseName, code, params) {
|
|
71
|
-
runtime.phase = phaseName
|
|
72
|
-
runtime.progress = { code, ...(params ? { params } : {}) }
|
|
73
|
-
options.emitState()
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Managed-Node provisioning reports coarse steps; surface them in the same
|
|
78
|
-
* progress line the rest of the pipeline uses (the installer already bundles
|
|
79
|
-
* byte progress into ~5s/8 MB events).
|
|
80
|
-
*/
|
|
81
|
-
function nodeProgress(runtime, event) {
|
|
82
|
-
if (event.step === 'check') return
|
|
83
|
-
if (event.step === 'download') {
|
|
84
|
-
const mb = Math.max(0, Math.round(Number(event.bytes ?? 0) / (1024 * 1024)))
|
|
85
|
-
log(runtime, 'info', `downloading managed Node v${event.version} (${mb} MB so far)`)
|
|
86
|
-
phase(runtime, 'bootstrap', 'downloading-node', { version: event.version, mb })
|
|
87
|
-
return
|
|
88
|
-
}
|
|
89
|
-
if (event.step === 'ready') {
|
|
90
|
-
log(runtime, 'info', `managed Node v${event.version} is ready`)
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
log(runtime, 'info', `installing managed Node v${event.version}`)
|
|
94
|
-
phase(runtime, 'bootstrap', 'installing-node', { version: event.version })
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Stream one channel of the npx preflight into the connect progress line. npm
|
|
99
|
-
* has no TTY over SSH, so the command enables `npm_config_loglevel=http`; each
|
|
100
|
-
* line becomes a short detail, throttled so the SSE stream is not flooded.
|
|
101
|
-
*/
|
|
102
|
-
function bootstrapReporter(runtime, version) {
|
|
103
|
-
let buffer = ''
|
|
104
|
-
let lastAt = 0
|
|
105
|
-
let lastKey = ''
|
|
106
|
-
return (chunk) => {
|
|
107
|
-
buffer += String(chunk ?? '')
|
|
108
|
-
const lines = buffer.split(/\r?\n/)
|
|
109
|
-
buffer = lines.pop() ?? ''
|
|
110
|
-
for (const rawLine of lines) {
|
|
111
|
-
const text = stripAnsi(rawLine).trim()
|
|
112
|
-
// npx is the slowest step on a cold cache: print what it says at debug level.
|
|
113
|
-
if (text !== '') log(runtime, 'debug', `npx: ${text}`)
|
|
114
|
-
const event = describePreflightEvent(rawLine, version)
|
|
115
|
-
if (!event) continue
|
|
116
|
-
const key = `${event.code}:${event.params?.detail ?? ''}`
|
|
117
|
-
if (key === lastKey) continue
|
|
118
|
-
const now = Date.now()
|
|
119
|
-
if (now - lastAt < REPORT_MIN_INTERVAL_MS) continue
|
|
120
|
-
lastAt = now
|
|
121
|
-
lastKey = key
|
|
122
|
-
phase(runtime, 'bootstrap', event.code, event.params)
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function dispose() {
|
|
128
|
-
if (flushTimer !== null) {
|
|
129
|
-
clearTimeout(flushTimer)
|
|
130
|
-
flushTimer = null
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return { log, set, phase, nodeProgress, bootstrapReporter, dispose }
|
|
135
|
-
}
|
package/src/constants.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared constants for the remote npx runtime and the plugin surface.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export const DEFAULT_REMOTE_PROFILE = 'web'
|
|
6
|
-
|
|
7
|
-
export const REMOTE_DSH_DIR = '.dsh'
|
|
8
|
-
|
|
9
|
-
export const REMOTE_STATE_DIR = 'remote-go'
|
|
10
|
-
|
|
11
|
-
export const API_PREFIX = '/dsh-remote-go/api'
|
|
12
|
-
|
|
13
|
-
export const API_HEADER = 'x-dsh-remote-go'
|
|
14
|
-
|
|
15
|
-
export const PLUGIN_ID = 'dsh-remote-go'
|
|
16
|
-
export const PLUGIN_VERSION = '0.1.0'
|
|
17
|
-
|
|
18
|
-
/** Host↔Client contract version served by `GET /version` (docs/api.md v2.4). */
|
|
19
|
-
export const API_VERSION = '2.6'
|
|
20
|
-
|
|
21
|
-
export const DEFAULT_KEY_PATH = '~/.ssh/id_rsa'
|
|
22
|
-
|
|
23
|
-
export const REMOTE_START_TIMEOUT_MS = 300_000
|
|
24
|
-
|
|
25
|
-
export const REMOTE_CONFLICT_TIMEOUT_MS = 120_000
|
|
26
|
-
|
|
27
|
-
export const SSH_READY_TIMEOUT_MS = 20_000
|
|
28
|
-
|
|
29
|
-
export const DEFAULT_IDLE_MINUTES = 15
|
|
30
|
-
|
|
31
|
-
export const TUNNEL_PROBE_TIMEOUT_MS = 10_000
|