dsh-remote 0.8.89 → 0.8.91
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/lib/client.js +26 -9
- package/lib/index.js +33 -704
- package/lib/routes-audit.js +58 -0
- package/lib/routes-forwards.js +72 -0
- package/lib/routes-machines.js +186 -0
- package/lib/routes-misc.js +150 -0
- package/lib/routes-web-attach.js +112 -0
- package/lib/routes-workspace.js +312 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
import { createHostKeyGuard, isHostKeyKnown as _isHostKeyKnown } from './hostkey.js'
|
|
36
36
|
import { compileIgnore, ignorePatternsFor, DEFAULT_IGNORE } from './ignore.js'
|
|
37
37
|
import { friendlyMessage } from './errors.js'
|
|
38
|
-
import {
|
|
38
|
+
import { sshConfigPath, readSshConfigText, resolveMachineSshConfig, resolveLiteralSshConfig, readRawSshConfig, upsertManagedHostBlock, removeManagedHostBlock, writeSshConfig } from './sshconfig.js'
|
|
39
39
|
import { createRemoteFileReference, installFileReferenceOverlay, DEFAULT_EXCLUDED_DIRECTORIES } from './file-reference.js'
|
|
40
40
|
import { getSecret, deleteSecret, platformBackend, persistPassword } from './credential.js'
|
|
41
41
|
import { syncTree, pushTree, loadSyncState, saveSyncState, pushOneFile } from './sync.js'
|
|
@@ -50,19 +50,24 @@ import { installTerminalShell } from './remote-shell.js'
|
|
|
50
50
|
import { resolveMirror, poolKey, lookupSessionCwd, encodeSegmentSafe, readSessionHeaderCwd, requestSessionHint } from './binding.js'
|
|
51
51
|
import { TaskManager } from './tasks.js'
|
|
52
52
|
import { ForwardManager } from './forwards.js'
|
|
53
|
-
import { WebAttach } from './web-attach.js'
|
|
54
53
|
import { formatAuditLine, readAuditLines } from './audit.js'
|
|
55
54
|
import { openLocalFolder } from './open-folder.js'
|
|
56
55
|
|
|
57
56
|
import { selfDir, diskVersion, LOADED_VERSION, gtVersion, fetchLatestVersion, applyUpdate, persistUpdateMode, readUpdateMode, reloadSelf, scheduleSelfReload, lastSelfReload, isInstalledCopy, writeFileAtomic } from './update.js'
|
|
58
|
-
import { sendHeartbeat, pseudonym, heartbeatUrl,
|
|
57
|
+
import { sendHeartbeat, pseudonym, heartbeatUrl, hasPersistedId, installIdPath, telemetryAllowed } from './telemetry.js'
|
|
59
58
|
import { loadMachines as _loadMachines, saveMachines as _saveMachines, sanitizeMachine as _sanitizeMachine, applyMachine as _applyMachine, machineId as _machineId, normalizeMachineGroup, filterMachinesByGroup, resolveProbeCredentials } from './registry.js'
|
|
60
59
|
import { registerHttpTransports } from './http-transport.js'
|
|
61
60
|
import { createHostApi } from './host-api.js'
|
|
62
61
|
import { SshPool, ActivePool, createPool } from './pool.js'
|
|
63
62
|
import { isSystemOpenSshBackend, sshBinaryAvailable, resolveSshBinary } from './openssh.js'
|
|
63
|
+
import { createAuditRoutes } from './routes-audit.js'
|
|
64
|
+
import { createForwardRoutes } from './routes-forwards.js'
|
|
65
|
+
import { createMachineRoutes } from './routes-machines.js'
|
|
66
|
+
import { createMiscRoutes } from './routes-misc.js'
|
|
64
67
|
import { createFsRoutes } from './routes-fs.js'
|
|
65
68
|
import { createUpdateRoutes } from './routes-update.js'
|
|
69
|
+
import { createWebAttachRoutes } from './routes-web-attach.js'
|
|
70
|
+
import { createWorkspaceRoutes } from './routes-workspace.js'
|
|
66
71
|
import { listDirStructured, removeRemoteTree } from './remote-fs.js'
|
|
67
72
|
|
|
68
73
|
export const name = 'dsh-remote'
|
|
@@ -4159,46 +4164,12 @@ export async function apply(ctx, config) {
|
|
|
4159
4164
|
}
|
|
4160
4165
|
},
|
|
4161
4166
|
},
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
// A `const payload` inside `try` is scoped to that block, so the catch
|
|
4169
|
-
// below used to throw `ReferenceError: payload is not defined` BEFORE
|
|
4170
|
-
// it could send anything — the webserver then turned the rejected
|
|
4171
|
-
// handler into an empty HTTP 400 and the real reason never reached the
|
|
4172
|
-
// UI. Hoisting it (and defaulting to {}) makes the failure path total:
|
|
4173
|
-
// this handler now always answers with JSON.
|
|
4174
|
-
let payload = {}
|
|
4175
|
-
try {
|
|
4176
|
-
payload = parseJsonObject(await readBody(req))
|
|
4177
|
-
// This route is a reconnect PROBE ("Connect" button): with the
|
|
4178
|
-
// unchanged-target no-op in setTarget, an identical target would
|
|
4179
|
-
// reuse a possibly half-open cached client and the probe could hang.
|
|
4180
|
-
// invalidate() is the design's forced-reconnect escape hatch — drop
|
|
4181
|
-
// the cached client so the `echo ok` below always re-dials.
|
|
4182
|
-
pool.invalidate()
|
|
4183
|
-
pool.setTarget({
|
|
4184
|
-
host: payload.host,
|
|
4185
|
-
port: payload.port,
|
|
4186
|
-
username: payload.username,
|
|
4187
|
-
password: payload.password !== undefined && payload.password !== '' ? payload.password : undefined,
|
|
4188
|
-
privateKeyPath: payload.privateKeyPath,
|
|
4189
|
-
workspace: payload.workspace,
|
|
4190
|
-
// A credential-less target — including an alias from ~/.ssh/config
|
|
4191
|
-
// whose Host block relies on OpenSSH's default keys — cannot connect
|
|
4192
|
-
// on native; dial it through the user's own ssh instead.
|
|
4193
|
-
backend: backendFor(payload, config),
|
|
4194
|
-
})
|
|
4195
|
-
await pool.exec('echo ok', { timeoutMs: Math.min(config.commandTimeoutMs, 8000) })
|
|
4196
|
-
return sendJson(res, 200, { ok: true, ...status() })
|
|
4197
|
-
} catch (err) {
|
|
4198
|
-
return sendJson(res, (err && err.httpStatus) || 500, { ok: false, error: friendlyMessage(err, { host: payload.host, port: payload.port }) })
|
|
4199
|
-
}
|
|
4200
|
-
},
|
|
4201
|
-
},
|
|
4167
|
+
// Mirror/connection surface (extracted for issue #82).
|
|
4168
|
+
...createWorkspaceRoutes({
|
|
4169
|
+
sendJson, readBody, parseJsonObject, config, pool, backendFor, status, isRemoteDir, persistWorkspace, ensureMirror,
|
|
4170
|
+
aliasOf, activeMachine, startAutoPush, applyRequestedMachine, registerMirroredWorkspace, localPickCapability,
|
|
4171
|
+
pickLocalNative, listLocalDrives, probeMachineRecord, setCurrent, knownHostsFile,
|
|
4172
|
+
}),
|
|
4202
4173
|
...createFsRoutes({
|
|
4203
4174
|
sendJson, readBody, resolveRequestBinding, decodeBuf, encodeText, audit, config, mirrorDirFor,
|
|
4204
4175
|
}),
|
|
@@ -4212,361 +4183,13 @@ export async function apply(ctx, config) {
|
|
|
4212
4183
|
machineKey: (b) => poolKey({ host: b.host, port: b.port, username: b.username }),
|
|
4213
4184
|
}),
|
|
4214
4185
|
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
// Blank-only guard: a filesystem root is a legal workspace (issue
|
|
4223
|
-
// #60); the real directory probe below answers everything else.
|
|
4224
|
-
const rawPath = String(payload.path || '').trim()
|
|
4225
|
-
if (!rawPath) return sendJson(res, 400, { error: 'path must be an absolute directory' })
|
|
4226
|
-
const p = normalizeRemotePath(rawPath)
|
|
4227
|
-
const okDir = await isRemoteDir(p)
|
|
4228
|
-
if (!okDir) return sendJson(res, 400, { ok: false, error: `not a directory: ${p}` })
|
|
4229
|
-
persistWorkspace(p)
|
|
4230
|
-
const local = ensureMirror(p, config.host, config.username, config.port, { alias: aliasOf(activeMachine()) })
|
|
4231
|
-
startAutoPush(local)
|
|
4232
|
-
return sendJson(res, 200, { ok: true, workspace: p, localMirror: local, ...status() })
|
|
4233
|
-
} catch (err) {
|
|
4234
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4235
|
-
}
|
|
4236
|
-
},
|
|
4237
|
-
},
|
|
4238
|
-
{
|
|
4239
|
-
kind: 'exact',
|
|
4240
|
-
path: '/dsh-remote/mirror',
|
|
4241
|
-
handler: async (req, res) => {
|
|
4242
|
-
if (req.method !== 'POST') return sendJson(res, 405, { error: 'method not allowed' })
|
|
4243
|
-
try {
|
|
4244
|
-
const payload = JSON.parse((await readBody(req)) || '{}')
|
|
4245
|
-
// Blank-only guard: a filesystem root is a legal workspace (issue
|
|
4246
|
-
// #60); the real directory probe below answers everything else.
|
|
4247
|
-
const rawPath = String(payload.path || '').trim()
|
|
4248
|
-
if (!rawPath) return sendJson(res, 400, { ok: false, error: 'path must be an absolute directory' })
|
|
4249
|
-
const p = normalizeRemotePath(rawPath)
|
|
4250
|
-
if (!config.host) return sendJson(res, 400, { ok: false, error: 'no remote host configured/connected — connect first' })
|
|
4251
|
-
// The picker names the machine it was browsing. Re-assert it so a
|
|
4252
|
-
// switch made elsewhere between the listing and this commit cannot
|
|
4253
|
-
// mirror onto a machine the user never looked at (the picker sends
|
|
4254
|
-
// /current only once per machine now, so nothing else re-applies it).
|
|
4255
|
-
if (!(await applyRequestedMachine(payload, res))) return
|
|
4256
|
-
const okDir = await isRemoteDir(p)
|
|
4257
|
-
if (!okDir) return sendJson(res, 400, { ok: false, error: `not a directory (or unreachable): ${p}` })
|
|
4258
|
-
const local = ensureMirror(p, config.host, config.username, config.port, { alias: aliasOf(activeMachine()) })
|
|
4259
|
-
persistWorkspace(p)
|
|
4260
|
-
startAutoPush(local)
|
|
4261
|
-
// The native "Add workspace" picker adopts the mirror right after this
|
|
4262
|
-
// response (`workspace.create({ path: localMirror })`). Registering it
|
|
4263
|
-
// here first is the only way to influence the title, because the core
|
|
4264
|
-
// Create API takes no title and would derive "xdfs" from the mirror's
|
|
4265
|
-
// last segment. Callers that only pick a folder (settings page, Remote
|
|
4266
|
-
// Files explorer) opt out with registerWorkspace:false, so no stray
|
|
4267
|
-
// workspace appears in their flow.
|
|
4268
|
-
if (payload.registerWorkspace !== false) await registerMirroredWorkspace(local, p, activeMachine())
|
|
4269
|
-
return sendJson(res, 200, { ok: true, path: p, localMirror: local, ...status() })
|
|
4270
|
-
} catch (err) {
|
|
4271
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4272
|
-
}
|
|
4273
|
-
},
|
|
4274
|
-
},
|
|
4275
|
-
{
|
|
4276
|
-
kind: 'exact',
|
|
4277
|
-
path: '/dsh-remote/local-pick',
|
|
4278
|
-
handler: async (req, res) => {
|
|
4279
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4280
|
-
try {
|
|
4281
|
-
let outcome = null
|
|
4282
|
-
let via = 'service'
|
|
4283
|
-
const cap = await localPickCapability()
|
|
4284
|
-
if (cap && cap.kind === 'native' && typeof cap.pick === 'function') {
|
|
4285
|
-
try {
|
|
4286
|
-
const pickAbort = new AbortController()
|
|
4287
|
-
const p = await Promise.resolve(cap.pick(pickAbort.signal || null))
|
|
4288
|
-
pickAbort.abort()
|
|
4289
|
-
outcome = (typeof p === 'string' && p) ? { path: p } : { cancelled: true }
|
|
4290
|
-
} catch (err) {
|
|
4291
|
-
outcome = null
|
|
4292
|
-
}
|
|
4293
|
-
}
|
|
4294
|
-
// Prefer a REAL OS dialog over the browse backend: operators expect
|
|
4295
|
-
// an Explorer-style chooser, and the own spawn (PowerShell
|
|
4296
|
-
// FolderBrowserDialog / osascript / zenity-kdialog) works even where
|
|
4297
|
-
// the host mounted browse — DSH Desktop (Electron) deliberately
|
|
4298
|
-
// mounts browse on win32 because the native backend's koffi dialog
|
|
4299
|
-
// worker cannot run under Electron, but a plain child process can.
|
|
4300
|
-
if (!outcome) {
|
|
4301
|
-
via = 'own'
|
|
4302
|
-
try {
|
|
4303
|
-
outcome = await pickLocalNative()
|
|
4304
|
-
} catch (err) {
|
|
4305
|
-
// No usable OS dialog (headless host, missing chooser binary):
|
|
4306
|
-
// fall back to the host's browse capability — fs-only, works
|
|
4307
|
-
// display-less — served as an in-app browser by the client
|
|
4308
|
-
// (backed by /dsh-remote/local-list + /dsh-remote/local-mkdir).
|
|
4309
|
-
if (cap && cap.kind === 'browse' && typeof cap.list === 'function') {
|
|
4310
|
-
return sendJson(res, 200, { ok: true, kind: 'browse', via: 'browse' })
|
|
4311
|
-
}
|
|
4312
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) + ' — 可直接在输入框填本地路径' })
|
|
4313
|
-
}
|
|
4314
|
-
}
|
|
4315
|
-
if (outcome.cancelled) return sendJson(res, 200, { ok: true, cancelled: true, via })
|
|
4316
|
-
return sendJson(res, 200, { ok: true, path: outcome.path, via })
|
|
4317
|
-
} catch (err) {
|
|
4318
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4319
|
-
}
|
|
4320
|
-
},
|
|
4321
|
-
},
|
|
4322
|
-
{
|
|
4323
|
-
kind: 'exact',
|
|
4324
|
-
path: '/dsh-remote/local-list',
|
|
4325
|
-
handler: async (req, res) => {
|
|
4326
|
-
if (req.method !== 'GET') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4327
|
-
try {
|
|
4328
|
-
const cap = await localPickCapability()
|
|
4329
|
-
if (!cap || cap.kind !== 'browse' || typeof cap.list !== 'function') return sendJson(res, 400, { ok: false, error: '当前目录选择器不是浏览后端,无法列出本机目录' })
|
|
4330
|
-
const m = (req.url || '').match(/path=([^&]*)/)
|
|
4331
|
-
const raw = m ? decodeURIComponent(m[1]) : ''
|
|
4332
|
-
// No path → the browse backend lists the host home directory.
|
|
4333
|
-
try {
|
|
4334
|
-
const out = await Promise.resolve(cap.list(raw ? raw : undefined))
|
|
4335
|
-
return sendJson(res, 200, { ok: true, ...out, drives: listLocalDrives() })
|
|
4336
|
-
} catch (listErr) {
|
|
4337
|
-
return sendJson(res, 400, { ok: false, error: String((listErr && listErr.message) || listErr), code: (listErr && listErr.code) || 'directory-unreadable' })
|
|
4338
|
-
}
|
|
4339
|
-
} catch (err) {
|
|
4340
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4341
|
-
}
|
|
4342
|
-
},
|
|
4343
|
-
},
|
|
4344
|
-
{
|
|
4345
|
-
kind: 'exact',
|
|
4346
|
-
path: '/dsh-remote/local-mkdir',
|
|
4347
|
-
handler: async (req, res) => {
|
|
4348
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4349
|
-
try {
|
|
4350
|
-
const cap = await localPickCapability()
|
|
4351
|
-
if (!cap || cap.kind !== 'browse' || typeof cap.createDirectory !== 'function') return sendJson(res, 400, { ok: false, error: '当前目录选择器不是浏览后端,无法新建文件夹' })
|
|
4352
|
-
const payload = JSON.parse((await readBody(req)) || '{}')
|
|
4353
|
-
const p = String(payload.path || '')
|
|
4354
|
-
const name = String(payload.name || '')
|
|
4355
|
-
if (!p.trim() || !name.trim()) return sendJson(res, 400, { ok: false, error: 'path and name required' })
|
|
4356
|
-
try {
|
|
4357
|
-
const created = await Promise.resolve(cap.createDirectory(p, name))
|
|
4358
|
-
return sendJson(res, 200, { ok: true, path: created })
|
|
4359
|
-
} catch (mkErr) {
|
|
4360
|
-
return sendJson(res, 400, { ok: false, error: String((mkErr && mkErr.message) || mkErr), code: (mkErr && mkErr.code) || 'directory-create-failed' })
|
|
4361
|
-
}
|
|
4362
|
-
} catch (err) {
|
|
4363
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4364
|
-
}
|
|
4365
|
-
},
|
|
4366
|
-
},
|
|
4367
|
-
{
|
|
4368
|
-
kind: 'exact',
|
|
4369
|
-
path: '/dsh-remote/machines',
|
|
4370
|
-
handler: async (req, res) => {
|
|
4371
|
-
if (req.method === 'GET') {
|
|
4372
|
-
return sendJson(res, 200, { machines: machinesForClient(), currentId: store.currentId })
|
|
4373
|
-
}
|
|
4374
|
-
if (req.method === 'POST') {
|
|
4375
|
-
try {
|
|
4376
|
-
const body = JSON.parse((await readBody(req)) || '{}')
|
|
4377
|
-
const action = body.action || 'add'
|
|
4378
|
-
if (action === 'add' || action === 'update') {
|
|
4379
|
-
try {
|
|
4380
|
-
return sendJson(res, 200, { ok: true, ...(await upsertMachineRecord(body)) })
|
|
4381
|
-
} catch (err) {
|
|
4382
|
-
return sendJson(res, (err && err.httpStatus) || 500, { ok: false, error: String((err && err.message) || err) })
|
|
4383
|
-
}
|
|
4384
|
-
}
|
|
4385
|
-
// Issue #73: one click on a host listed from ~/.ssh/config. The settings
|
|
4386
|
-
// page no longer asks the user to understand "alias" vs "copy": it lists
|
|
4387
|
-
// the file's hosts and this action adopts one — upsert an alias machine
|
|
4388
|
-
// (never a duplicate), make it the current machine, and let the caller
|
|
4389
|
-
// connect/probe next. Nothing about the user's file is rewritten here:
|
|
4390
|
-
// the alias IS the source of truth (issue #50).
|
|
4391
|
-
if (action === 'from-ssh-config') {
|
|
4392
|
-
const alias = String(body.alias || body.host || '').trim()
|
|
4393
|
-
if (!alias) return sendJson(res, 400, { ok: false, error: 'alias required' })
|
|
4394
|
-
const known = listAliases(sshConfigText()).find((a) => a.alias === alias)
|
|
4395
|
-
if (!known) {
|
|
4396
|
-
return sendJson(res, 404, {
|
|
4397
|
-
ok: false,
|
|
4398
|
-
error: `~/.ssh/config 里没有 "${alias}"(改完文件后重新打开设置页即可刷新)`,
|
|
4399
|
-
})
|
|
4400
|
-
}
|
|
4401
|
-
const existing = machines.find((m) => m.useSshConfig && String(m.host) === alias)
|
|
4402
|
-
let rec = existing
|
|
4403
|
-
if (!rec) {
|
|
4404
|
-
rec = {
|
|
4405
|
-
id: machineId(),
|
|
4406
|
-
name: alias,
|
|
4407
|
-
host: alias,
|
|
4408
|
-
useSshConfig: true,
|
|
4409
|
-
port: Number(known.port) || 22,
|
|
4410
|
-
username: String(known.user || ''),
|
|
4411
|
-
password: '',
|
|
4412
|
-
privateKeyPath: '',
|
|
4413
|
-
passphrase: '',
|
|
4414
|
-
workspace: '',
|
|
4415
|
-
hostKeyMode: '',
|
|
4416
|
-
useAgent: false,
|
|
4417
|
-
keyboardInteractive: false,
|
|
4418
|
-
// ~/.ssh/config defines this machine, and the user's own `ssh`
|
|
4419
|
-
// is what interprets that file (default identity discovery,
|
|
4420
|
-
// agent/keychain, ProxyJump, GSSAPI). Native cannot see any of
|
|
4421
|
-
// it, so a Host block with no IdentityFile would be reported as
|
|
4422
|
-
// "no credentials".
|
|
4423
|
-
backend: 'openssh',
|
|
4424
|
-
sshBinary: '',
|
|
4425
|
-
credentialBackend: 'plain',
|
|
4426
|
-
recentWorkspaces: [],
|
|
4427
|
-
lastConnectedAt: null,
|
|
4428
|
-
latencyMs: null,
|
|
4429
|
-
}
|
|
4430
|
-
machines.push(rec)
|
|
4431
|
-
saveMachines(machines, store.currentId, false)
|
|
4432
|
-
} else if (String(rec.backend || 'native') !== 'openssh' && !nativeUsableCredential(rec)) {
|
|
4433
|
-
// A record adopted before this fix carries `native` with no
|
|
4434
|
-
// credential: upgrade it in place, so clicking Connect dials what
|
|
4435
|
-
// `ssh <alias>` already does.
|
|
4436
|
-
rec.backend = 'openssh'
|
|
4437
|
-
saveMachines(machines, store.currentId, false)
|
|
4438
|
-
}
|
|
4439
|
-
// Making it current is the whole point of the click: the picker and
|
|
4440
|
-
// every session route then act on this machine.
|
|
4441
|
-
const okSet = await setCurrent(rec.id)
|
|
4442
|
-
if (!okSet) return sendJson(res, 500, { ok: false, error: 'could not activate the machine' })
|
|
4443
|
-
return sendJson(res, 200, {
|
|
4444
|
-
ok: true,
|
|
4445
|
-
created: !existing,
|
|
4446
|
-
machine: sanitizeMachine(rec),
|
|
4447
|
-
machines: machinesForClient(),
|
|
4448
|
-
currentId: store.currentId,
|
|
4449
|
-
...status(),
|
|
4450
|
-
})
|
|
4451
|
-
}
|
|
4452
|
-
if (action === 'delete') {
|
|
4453
|
-
try {
|
|
4454
|
-
return sendJson(res, 200, { ok: true, ...(await removeMachineRecord(String(body.id || ''))) })
|
|
4455
|
-
} catch (err) {
|
|
4456
|
-
return sendJson(res, (err && err.httpStatus) || 500, { ok: false, error: String((err && err.message) || err) })
|
|
4457
|
-
}
|
|
4458
|
-
}
|
|
4459
|
-
return sendJson(res, 400, { ok: false, error: 'unknown action' })
|
|
4460
|
-
} catch (err) {
|
|
4461
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4462
|
-
}
|
|
4463
|
-
}
|
|
4464
|
-
return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4465
|
-
},
|
|
4466
|
-
},
|
|
4467
|
-
{
|
|
4468
|
-
kind: 'exact',
|
|
4469
|
-
path: '/dsh-remote/test-connect',
|
|
4470
|
-
handler: async (req, res) => {
|
|
4471
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4472
|
-
// Issue #30: `body` lives OUTSIDE the try — a try-scoped `const` is
|
|
4473
|
-
// invisible in catch, so the old catch threw `ReferenceError: body is
|
|
4474
|
-
// not defined` and the probe result never reached the UI (the webserver
|
|
4475
|
-
// answered with an empty 400 for every kind of failure). This route is a
|
|
4476
|
-
// probe: every outcome is a 200 carrying {ok:false, error}.
|
|
4477
|
-
let body = {}
|
|
4478
|
-
try {
|
|
4479
|
-
body = parseJsonObject(await readBody(req))
|
|
4480
|
-
return sendJson(res, 200, await probeMachineRecord(body))
|
|
4481
|
-
} catch (err) {
|
|
4482
|
-
return sendJson(res, 200, { ok: false, error: friendlyMessage(err, { host: body.host, port: body.port }) })
|
|
4483
|
-
}
|
|
4484
|
-
},
|
|
4485
|
-
},
|
|
4486
|
-
{
|
|
4487
|
-
kind: 'exact',
|
|
4488
|
-
path: '/dsh-remote/current',
|
|
4489
|
-
handler: async (req, res) => {
|
|
4490
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4491
|
-
try {
|
|
4492
|
-
const body = JSON.parse((await readBody(req)) || '{}')
|
|
4493
|
-
const id = String(body.id || '').trim()
|
|
4494
|
-
// Issue #13: an empty id is an explicit "active remote = none" —
|
|
4495
|
-
// saved machines stay in the registry but nothing is current.
|
|
4496
|
-
if (!id) {
|
|
4497
|
-
await setCurrent('')
|
|
4498
|
-
return sendJson(res, 200, { ok: true, currentId: null, ...status() })
|
|
4499
|
-
}
|
|
4500
|
-
const okSet = await setCurrent(id)
|
|
4501
|
-
if (!okSet) return sendJson(res, 404, { ok: false, error: 'machine not found' })
|
|
4502
|
-
return sendJson(res, 200, { ok: true, ...status() })
|
|
4503
|
-
} catch (err) {
|
|
4504
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4505
|
-
}
|
|
4506
|
-
},
|
|
4507
|
-
},
|
|
4508
|
-
{
|
|
4509
|
-
kind: 'exact',
|
|
4510
|
-
path: '/dsh-remote/forget-key',
|
|
4511
|
-
handler: async (req, res) => {
|
|
4512
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4513
|
-
createHostKeyGuard(config, knownHostsFile()).forgetHost()
|
|
4514
|
-
return sendJson(res, 200, { ok: true, ...status() })
|
|
4515
|
-
},
|
|
4516
|
-
},
|
|
4517
|
-
{
|
|
4518
|
-
kind: 'exact',
|
|
4519
|
-
path: '/dsh-remote/forwards',
|
|
4520
|
-
handler: async (req, res) => {
|
|
4521
|
-
if (req.method === 'GET') return sendJson(res, 200, { forwards: forwards.list() })
|
|
4522
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4523
|
-
try {
|
|
4524
|
-
const body = JSON.parse((await readBody(req)) || '{}')
|
|
4525
|
-
const action = String(body.action || '')
|
|
4526
|
-
if (action === 'define') {
|
|
4527
|
-
// Validate the port HERE, not at start time: `listen(0)` would silently
|
|
4528
|
-
// pick an ephemeral port and the user would be looking at a forward
|
|
4529
|
-
// that listens nowhere they asked for. The settings UI cannot produce
|
|
4530
|
-
// this, but the HTTP API (and therefore an agent) can.
|
|
4531
|
-
const listenPort = Number(body.listenPort)
|
|
4532
|
-
if (!Number.isInteger(listenPort) || listenPort < 1 || listenPort > 65535) {
|
|
4533
|
-
return sendJson(res, 400, { ok: false, error: 'listenPort 必须是 1–65535 的整数' })
|
|
4534
|
-
}
|
|
4535
|
-
const targetPort = Number(body.targetPort) || listenPort
|
|
4536
|
-
if (!Number.isInteger(targetPort) || targetPort < 1 || targetPort > 65535) {
|
|
4537
|
-
return sendJson(res, 400, { ok: false, error: 'targetPort 必须是 1–65535 的整数' })
|
|
4538
|
-
}
|
|
4539
|
-
const d = forwards.define({
|
|
4540
|
-
direction: body.direction === 'reverse' ? 'reverse' : 'local',
|
|
4541
|
-
listenPort,
|
|
4542
|
-
targetHost: body.targetHost || '127.0.0.1',
|
|
4543
|
-
targetPort,
|
|
4544
|
-
autoStart: !!body.autoStart,
|
|
4545
|
-
machineId: store.currentId,
|
|
4546
|
-
})
|
|
4547
|
-
return sendJson(res, 200, { ok: true, forward: d, forwards: forwards.list() })
|
|
4548
|
-
}
|
|
4549
|
-
if (action === 'start' || action === 'stop') {
|
|
4550
|
-
const d = forwards.list().find((f) => f.id === body.id)
|
|
4551
|
-
if (!d) return sendJson(res, 404, { ok: false, error: 'forward not found' })
|
|
4552
|
-
if (action === 'start') {
|
|
4553
|
-
const r = await forwards.start(d)
|
|
4554
|
-
if (!r.ok) return sendJson(res, 500, { ok: false, error: r.error })
|
|
4555
|
-
} else {
|
|
4556
|
-
forwards.stop(d.id)
|
|
4557
|
-
}
|
|
4558
|
-
return sendJson(res, 200, { ok: true, forwards: forwards.list() })
|
|
4559
|
-
}
|
|
4560
|
-
if (action === 'remove') {
|
|
4561
|
-
forwards.remove(String(body.id || ''))
|
|
4562
|
-
return sendJson(res, 200, { ok: true, forwards: forwards.list() })
|
|
4563
|
-
}
|
|
4564
|
-
return sendJson(res, 400, { ok: false, error: 'unknown action' })
|
|
4565
|
-
} catch (err) {
|
|
4566
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4567
|
-
}
|
|
4568
|
-
},
|
|
4569
|
-
},
|
|
4186
|
+
// Machine registry + ssh-config surfaces (extracted for issue #82).
|
|
4187
|
+
...createMachineRoutes({
|
|
4188
|
+
sendJson, readBody, parseJsonObject, store, machines, machinesForClient, upsertMachineRecord, removeMachineRecord,
|
|
4189
|
+
setCurrent, saveMachines, sanitizeMachine, machineId, sshConfigText, nativeUsableCredential, status, openLocalPath,
|
|
4190
|
+
}),
|
|
4191
|
+
// Port-forward surface (extracted for issue #82).
|
|
4192
|
+
...createForwardRoutes({ sendJson, readBody, forwards, store }),
|
|
4570
4193
|
{
|
|
4571
4194
|
kind: 'exact',
|
|
4572
4195
|
path: '/dsh-remote/task',
|
|
@@ -4602,317 +4225,23 @@ export async function apply(ctx, config) {
|
|
|
4602
4225
|
return sendJson(res, 200, { ok: true, tasks: tasks.list() })
|
|
4603
4226
|
},
|
|
4604
4227
|
},
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
|
|
4611
|
-
return sendJson(res, 200, { ok: true, auditEnabled: !!config.auditLog, file: auditFile(), lines: readAudit(q.get('limit')) })
|
|
4612
|
-
}
|
|
4613
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4614
|
-
// Open the audit FILE itself in the user's own viewer. The browser cannot do
|
|
4615
|
-
// this — the log lives on the machine running the DSH host — so the host
|
|
4616
|
-
// performs the open and reports what it did:
|
|
4617
|
-
// action 'reveal' → highlight the file in the OS file manager
|
|
4618
|
-
// action 'open' → open it with the OS default application (a text editor)
|
|
4619
|
-
// A missing file is created first: "no log yet" would otherwise be
|
|
4620
|
-
// indistinguishable from a broken button.
|
|
4621
|
-
try {
|
|
4622
|
-
const body = parseJsonObject(await readBody(req))
|
|
4623
|
-
const action = String(body.action || 'open')
|
|
4624
|
-
if (action !== 'open' && action !== 'reveal') {
|
|
4625
|
-
return sendJson(res, 400, { ok: false, error: 'action must be open | reveal' })
|
|
4626
|
-
}
|
|
4627
|
-
const file = auditFile()
|
|
4628
|
-
mkdirSync(path.dirname(file), { recursive: true })
|
|
4629
|
-
if (!existsSync(file)) writeFileSync(file, '', 'utf8')
|
|
4630
|
-
const r = await openLocalPath(file, action)
|
|
4631
|
-
if (!r.ok) return sendJson(res, r.httpStatus || 500, { ok: false, error: r.error, file })
|
|
4632
|
-
return sendJson(res, 200, { ok: true, action, file, how: r.how })
|
|
4633
|
-
} catch (err) {
|
|
4634
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4635
|
-
}
|
|
4636
|
-
},
|
|
4637
|
-
},
|
|
4638
|
-
{
|
|
4639
|
-
kind: 'exact',
|
|
4640
|
-
path: '/dsh-remote/ssh-config',
|
|
4641
|
-
handler: async (req, res) => {
|
|
4642
|
-
// GET lists hosts. POST {action:'open'|'reveal'} opens THAT file on the
|
|
4643
|
-
// machine running DSH — the same affordance as the audit log. The path
|
|
4644
|
-
// is always sshConfigPath(); a path in the body is ignored, so this is
|
|
4645
|
-
// not a general file launcher.
|
|
4646
|
-
if (req.method === 'POST') {
|
|
4647
|
-
try {
|
|
4648
|
-
const body = parseJsonObject(await readBody(req))
|
|
4649
|
-
const action = String(body.action || '')
|
|
4650
|
-
if (action !== 'open' && action !== 'reveal') {
|
|
4651
|
-
return sendJson(res, 400, { ok: false, error: 'action must be open | reveal' })
|
|
4652
|
-
}
|
|
4653
|
-
const file = sshConfigPath()
|
|
4654
|
-
mkdirSync(path.dirname(file), { recursive: true })
|
|
4655
|
-
// Create an empty config so the editor has a file. Never truncate
|
|
4656
|
-
// one the user already wrote.
|
|
4657
|
-
if (!existsSync(file)) writeFileSync(file, '', { encoding: 'utf8', mode: 0o600 })
|
|
4658
|
-
const r = await openLocalPath(file, action)
|
|
4659
|
-
if (!r.ok) return sendJson(res, r.httpStatus || 500, { ok: false, error: r.error, file })
|
|
4660
|
-
return sendJson(res, 200, { ok: true, action, file, how: r.how })
|
|
4661
|
-
} catch (err) {
|
|
4662
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4663
|
-
}
|
|
4664
|
-
}
|
|
4665
|
-
if (req.method !== 'GET') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4666
|
-
try {
|
|
4667
|
-
const text = readSshConfigText()
|
|
4668
|
-
// Issue #73: the settings page lists the hosts of ~/.ssh/config the way
|
|
4669
|
-
// VSCode Remote-SSH does, so this must include EVERY concrete alias —
|
|
4670
|
-
// `importableEntries` filtered out a bare `Host mybox` with no options,
|
|
4671
|
-
// which is a perfectly normal entry to connect to. `host` is kept as an
|
|
4672
|
-
// alias of `alias` so existing callers keep working.
|
|
4673
|
-
const entries = listAliases(text).map((a) => ({
|
|
4674
|
-
host: a.alias,
|
|
4675
|
-
alias: a.alias,
|
|
4676
|
-
hostName: a.hostName || a.alias,
|
|
4677
|
-
user: a.user || '',
|
|
4678
|
-
port: Number(a.port) || 22,
|
|
4679
|
-
identityFile: a.identityFile || '',
|
|
4680
|
-
proxyJump: a.proxyJump || '',
|
|
4681
|
-
proxyCommand: a.proxyCommand || '',
|
|
4682
|
-
}))
|
|
4683
|
-
return sendJson(res, 200, { ok: true, file: sshConfigPath(), present: !!text, entries })
|
|
4684
|
-
} catch (err) {
|
|
4685
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4686
|
-
}
|
|
4687
|
-
},
|
|
4688
|
-
},
|
|
4689
|
-
{
|
|
4690
|
-
kind: 'exact',
|
|
4691
|
-
path: '/dsh-remote/web-attach',
|
|
4692
|
-
// Issue #46: open/reuse/close a tunnel to a remote machine's DSH Web UI.
|
|
4693
|
-
// Actions: status (GET) | open | close. `open` either starts a remote
|
|
4694
|
-
// `dsh web` (default) or attaches to one the user already runs by passing
|
|
4695
|
-
// its `http://127.0.0.1:<port>/?token=…` URL.
|
|
4696
|
-
handler: async (req, res) => {
|
|
4697
|
-
try {
|
|
4698
|
-
if (req.method === 'GET') {
|
|
4699
|
-
const q = new URL(req.url, 'http://localhost').searchParams
|
|
4700
|
-
const id = q.get('machineId') || ''
|
|
4701
|
-
const attach = id ? webAttaches.get(id) : undefined
|
|
4702
|
-
return sendJson(res, 200, {
|
|
4703
|
-
ok: true,
|
|
4704
|
-
attach: attach ? attach.describe() : null,
|
|
4705
|
-
attaches: [...webAttaches.values()].map((a) => a.describe()),
|
|
4706
|
-
})
|
|
4707
|
-
}
|
|
4708
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4709
|
-
const body = parseJsonObject(await readBody(req))
|
|
4710
|
-
const action = String(body.action || '')
|
|
4711
|
-
const machineId = String(body.machineId || store.currentId || '').trim()
|
|
4712
|
-
if (!machineId) return sendJson(res, 400, { ok: false, error: '选择一台机器后再操作(missing machineId)' })
|
|
4713
|
-
|
|
4714
|
-
if (action === 'open') {
|
|
4715
|
-
// Reuse a live tunnel for this machine: re-opening would start a
|
|
4716
|
-
// second remote DSH and leak the first one's process.
|
|
4717
|
-
const live = webAttaches.get(machineId)
|
|
4718
|
-
if (live && live.active) return sendJson(res, 200, { ok: true, attach: live.describe(), reused: true })
|
|
4719
|
-
// A tunnel that is no longer listening still owns sockets and a log
|
|
4720
|
-
// path; release it before this machine gets a fresh session. Its
|
|
4721
|
-
// remote process is left alone — stopping it stays an explicit act.
|
|
4722
|
-
if (live) { try { await live.close() } catch { /* already torn down */ } }
|
|
4723
|
-
const machine = machines.find((m) => m.id === machineId)
|
|
4724
|
-
if (!machine) return sendJson(res, 404, { ok: false, error: `机器不存在:${machineId}` })
|
|
4725
|
-
const attachPool = poolForMachine(identityOf(machine))
|
|
4726
|
-
const attach = new WebAttach({
|
|
4727
|
-
localPortStart: config.webAttachPortStart,
|
|
4728
|
-
// Issue #54: follow the chosen pool's lifecycle so a dropped SSH
|
|
4729
|
-
// connection degrades to "disconnected" and then self-heals,
|
|
4730
|
-
// instead of leaving a dead client that crashes the host process.
|
|
4731
|
-
trackPool: true,
|
|
4732
|
-
})
|
|
4733
|
-
try {
|
|
4734
|
-
await attach.open({
|
|
4735
|
-
pool: attachPool,
|
|
4736
|
-
machineId,
|
|
4737
|
-
command: String(body.command || '').trim() || webAttachCommandFor(machine),
|
|
4738
|
-
profile: 'web',
|
|
4739
|
-
existingUrl: String(body.existingUrl || ''),
|
|
4740
|
-
dshHome: config.webAttachDshHome || '',
|
|
4741
|
-
waitSeconds: config.webAttachWaitSeconds,
|
|
4742
|
-
})
|
|
4743
|
-
} catch (err) {
|
|
4744
|
-
try { await attach.close() } catch { /* nothing was listening */ }
|
|
4745
|
-
const msg = String((err && err.message) || err)
|
|
4746
|
-
audit('web-attach', `open ${machine.host} failed: ${msg}`, 1, machine)
|
|
4747
|
-
return sendJson(res, 500, { ok: false, error: msg })
|
|
4748
|
-
}
|
|
4749
|
-
webAttaches.set(machineId, attach)
|
|
4750
|
-
audit('web-attach', `open ${machine.host} → 127.0.0.1:${attach.localPort}`, 0, machine)
|
|
4751
|
-
return sendJson(res, 200, { ok: true, attach: attach.describe() })
|
|
4752
|
-
}
|
|
4753
|
-
|
|
4754
|
-
if (action === 'close') {
|
|
4755
|
-
const attach = webAttaches.get(machineId)
|
|
4756
|
-
if (!attach) return sendJson(res, 200, { ok: true, closed: false, error: '该机器没有活动的远程界面' })
|
|
4757
|
-
const stopRemote = body.stopRemote === true
|
|
4758
|
-
// `close()` refuses to signal anything without a recorded PID, so a
|
|
4759
|
-
// tunnel onto the user's own running instance stays hands-off.
|
|
4760
|
-
const exec = (cmd) => poolForMachine(identityOf(machines.find((m) => m.id === machineId) || {})).exec(cmd, { timeoutMs: 10000 })
|
|
4761
|
-
await attach.close({ stopRemote, exec })
|
|
4762
|
-
webAttaches.delete(machineId)
|
|
4763
|
-
audit('web-attach', `close ${attach.cleanUrl}${stopRemote ? ' (remote stopped)' : ''}`, 0)
|
|
4764
|
-
return sendJson(res, 200, { ok: true, closed: true, stoppedRemote: stopRemote && !!attach.remotePid })
|
|
4765
|
-
}
|
|
4766
|
-
|
|
4767
|
-
return sendJson(res, 400, { ok: false, error: 'action must be open | close' })
|
|
4768
|
-
} catch (err) {
|
|
4769
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4770
|
-
}
|
|
4771
|
-
},
|
|
4772
|
-
},
|
|
4773
|
-
{
|
|
4774
|
-
kind: 'exact',
|
|
4775
|
-
path: '/dsh-remote/home',
|
|
4776
|
-
handler: async (req, res) => {
|
|
4777
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4778
|
-
try {
|
|
4779
|
-
const body = parseJsonObject(await readBody(req))
|
|
4780
|
-
// The picker names the machine it is showing: re-assert it before
|
|
4781
|
-
// answering, so a switch made elsewhere cannot make /home describe a
|
|
4782
|
-
// different host than the tree the user is looking at.
|
|
4783
|
-
if (!(await applyRequestedMachine(body, res))) return
|
|
4784
|
-
const out = await pool.exec('echo ~', { timeoutMs: Math.min(config.commandTimeoutMs, 5000) })
|
|
4785
|
-
const home = String(out.stdout || '').replace(/\s+/g, '').trim()
|
|
4786
|
-
if (!home) return sendJson(res, 200, { ok: true, home: null, hint: 'Windows 远程暂不支持 ~ 解析,请直接输入绝对路径' })
|
|
4787
|
-
return sendJson(res, 200, { ok: true, home })
|
|
4788
|
-
} catch (err) {
|
|
4789
|
-
// `ok:false` — reporting `ok:true` WITH an `error` is a contradiction:
|
|
4790
|
-
// any caller that trusts `ok` treats "could not resolve the home
|
|
4791
|
-
// directory" as success. The picker only reads `home`, so it still falls
|
|
4792
|
-
// back to `hint`/`error` exactly as before.
|
|
4793
|
-
return sendJson(res, 200, {
|
|
4794
|
-
ok: false,
|
|
4795
|
-
home: null,
|
|
4796
|
-
hint: 'Windows 远程暂不支持 ~ 解析,请直接输入绝对路径',
|
|
4797
|
-
error: String((err && err.message) || err),
|
|
4798
|
-
})
|
|
4799
|
-
}
|
|
4800
|
-
},
|
|
4801
|
-
},
|
|
4228
|
+
// Audit surface (extracted for issue #82).
|
|
4229
|
+
...createAuditRoutes({ sendJson, readBody, config, auditFile, readAudit, parseJsonObject, openLocalPath }),
|
|
4230
|
+
// Remote-Web-UI attach surface (extracted for issue #82).
|
|
4231
|
+
...createWebAttachRoutes({
|
|
4232
|
+
sendJson, readBody, parseJsonObject, config, webAttaches, store, machines, poolForMachine, identityOf, webAttachCommandFor, audit,
|
|
4233
|
+
}),
|
|
4802
4234
|
// Self-update surface (extracted for issue #82).
|
|
4803
4235
|
...createUpdateRoutes({
|
|
4804
4236
|
sendJson, readBody, config, loader: ctx.loader, defaultUpdateMode: DEFAULT_UPDATE_MODE,
|
|
4805
4237
|
}),
|
|
4806
4238
|
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
// POST persists the user's choice under $DSH_HOME so it survives
|
|
4814
|
-
// plugin upgrades (the install dir gets overwritten).
|
|
4815
|
-
if (req.method === 'GET') {
|
|
4816
|
-
return sendJson(res, 200, { ok: true, enabled: telemetryAllowed(dshBase(), config.telemetry), configurable: heartbeatEnabled() })
|
|
4817
|
-
}
|
|
4818
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4819
|
-
try {
|
|
4820
|
-
const body = JSON.parse((await readBody(req)) || '{}')
|
|
4821
|
-
if (typeof body.enabled !== 'boolean') return sendJson(res, 400, { ok: false, error: 'enabled must be boolean' })
|
|
4822
|
-
if (!persistTelemetryOpt(dshBase(), body.enabled)) return sendJson(res, 500, { ok: false, error: 'cannot persist the setting (read-only home?)' })
|
|
4823
|
-
return sendJson(res, 200, { ok: true, enabled: telemetryAllowed(dshBase(), config.telemetry) })
|
|
4824
|
-
} catch (err) {
|
|
4825
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4826
|
-
}
|
|
4827
|
-
},
|
|
4828
|
-
},
|
|
4829
|
-
{
|
|
4830
|
-
kind: 'exact',
|
|
4831
|
-
path: '/dsh-remote/term-secret',
|
|
4832
|
-
// Owns its bearer token (per boot, 0600 file): the caller is a local
|
|
4833
|
-
// PROCESS (the askpass helper), not a browser, so the Connection cookie
|
|
4834
|
-
// fence would 401 it. The fallback fence still locks out cross-site pages
|
|
4835
|
-
// (they cannot read the token file).
|
|
4836
|
-
fence: 'bearer',
|
|
4837
|
-
handler: async (req, res) => {
|
|
4838
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4839
|
-
// Self-heal: a caller exists but the payload was never published (the
|
|
4840
|
-
// origin arrived after the first write attempts).
|
|
4841
|
-
if (!existsSync(termSecretFile())) {
|
|
4842
|
-
try {
|
|
4843
|
-
const ws = ctx.get('webServer')
|
|
4844
|
-
if (ws && typeof ws.getOrigin === 'function') writeTermSecretPayload(ws.getOrigin())
|
|
4845
|
-
} catch { /* still no listener: the 403 below answers honestly */ }
|
|
4846
|
-
}
|
|
4847
|
-
// Malformed bodies are a 400 with a reason even before the token check
|
|
4848
|
-
// (the routes matrix pins that readability contract for every POST).
|
|
4849
|
-
let body = {}
|
|
4850
|
-
try { body = JSON.parse((await readBody(req)) || '{}') } catch { return sendJson(res, 400, { ok: false, error: 'invalid JSON body' }) }
|
|
4851
|
-
const presented = String((req.headers && req.headers.authorization) || '').replace(/^Bearer\s+/i, '')
|
|
4852
|
-
if (!presented || !termSecretOk(presented)) return sendJson(res, 403, { ok: false, error: 'forbidden' })
|
|
4853
|
-
const alias = String(body.alias || '')
|
|
4854
|
-
let m = null
|
|
4855
|
-
if (alias) m = machines.find((x) => x && x.useSshConfig && String(x.host) === alias) || null
|
|
4856
|
-
if (!m) {
|
|
4857
|
-
const host = String(body.host || '')
|
|
4858
|
-
const port = Number(body.port) || 22
|
|
4859
|
-
const user = String(body.username || '')
|
|
4860
|
-
m = machines.find((x) => x && !x.useSshConfig && String(x.host) === host && (Number(x.port) || 22) === port && String(x.username || '') === user) || null
|
|
4861
|
-
}
|
|
4862
|
-
if (!m) return sendJson(res, 404, { ok: false, error: 'unknown machine' })
|
|
4863
|
-
const password = await machinePassword(m)
|
|
4864
|
-
return sendJson(res, 200, { ok: true, password })
|
|
4865
|
-
},
|
|
4866
|
-
},
|
|
4867
|
-
{
|
|
4868
|
-
kind: 'exact',
|
|
4869
|
-
path: '/dsh-remote/workspace-mirrors',
|
|
4870
|
-
handler: async (req, res) => {
|
|
4871
|
-
if (req.method !== 'GET') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4872
|
-
// The settings page's cleanup section: every local mirror with its
|
|
4873
|
-
// sessions, sizes and occupancy, straight off the in-process API (same
|
|
4874
|
-
// object the TUI consumes — one implementation, two faces).
|
|
4875
|
-
if (!hostApi) return sendJson(res, 503, { ok: false, error: 'in-process API unavailable' })
|
|
4876
|
-
try {
|
|
4877
|
-
return sendJson(res, 200, { ok: true, mirrors: await hostApi.listWorkspaceMirrors({}) })
|
|
4878
|
-
} catch (err) {
|
|
4879
|
-
return sendJson(res, 500, { ok: false, error: String((err && err.message) || err) })
|
|
4880
|
-
}
|
|
4881
|
-
},
|
|
4882
|
-
},
|
|
4883
|
-
{
|
|
4884
|
-
kind: 'exact',
|
|
4885
|
-
path: '/dsh-remote/workspace-cleanup',
|
|
4886
|
-
handler: async (req, res) => {
|
|
4887
|
-
if (req.method !== 'POST') return sendJson(res, 405, { ok: false, error: 'method not allowed' })
|
|
4888
|
-
if (!hostApi) return sendJson(res, 503, { ok: false, error: 'in-process API unavailable' })
|
|
4889
|
-
try {
|
|
4890
|
-
const body = JSON.parse((await readBody(req)) || '{}')
|
|
4891
|
-
const localMirror = String(body.localMirror || '')
|
|
4892
|
-
const mode = String(body.mode || '')
|
|
4893
|
-
if (!localMirror) return sendJson(res, 400, { ok: false, error: 'localMirror is required' })
|
|
4894
|
-
let result
|
|
4895
|
-
if (mode === 'all') {
|
|
4896
|
-
result = await hostApi.deleteRemoteWorkspace({ localMirror, force: body.force === true })
|
|
4897
|
-
} else if (mode === 'sessions') {
|
|
4898
|
-
result = await hostApi.deleteWorkspaceSessions({ localMirror, force: body.force === true })
|
|
4899
|
-
} else if (mode === 'recent') {
|
|
4900
|
-
result = await hostApi.forgetWorkspaceRecent({ localMirror })
|
|
4901
|
-
} else {
|
|
4902
|
-
return sendJson(res, 400, { ok: false, error: 'mode must be all | sessions | recent' })
|
|
4903
|
-
}
|
|
4904
|
-
return sendJson(res, 200, { ok: true, ...result })
|
|
4905
|
-
} catch (err) {
|
|
4906
|
-
// Coded errors carry user-facing messages; WORKSPACE_BUSY must reach
|
|
4907
|
-
// the UI as 409 so the page can say WHICH sessions hold the workspace.
|
|
4908
|
-
const status = (err && err.code === 'WORKSPACE_BUSY') ? 409 : (err && err.code === 'VALIDATION') ? 400 : 500
|
|
4909
|
-
const out = { ok: false, error: String((err && err.message) || err) }
|
|
4910
|
-
if (err && err.code) out.code = err.code
|
|
4911
|
-
if (err && Array.isArray(err.liveSessions)) out.liveSessions = err.liveSessions
|
|
4912
|
-
return sendJson(res, status, out)
|
|
4913
|
-
}
|
|
4914
|
-
},
|
|
4915
|
-
},
|
|
4239
|
+
// Telemetry / term-secret / workspace-cleanup surfaces (extracted for issue #82).
|
|
4240
|
+
// hostApi is late-bound: it is assembled AFTER route registration below.
|
|
4241
|
+
...createMiscRoutes({
|
|
4242
|
+
sendJson, readBody, config, dshBase, ctx, termSecretFile, writeTermSecretPayload, termSecretOk, machines, machinePassword,
|
|
4243
|
+
getHostApi: () => hostApi,
|
|
4244
|
+
}),
|
|
4916
4245
|
]
|
|
4917
4246
|
|
|
4918
4247
|
registerHttpTransports(ctx, routes)
|