@linxin666/dsh-ssh 0.4.19 → 0.4.21
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.i18n.yaml +2 -2
- package/README.md +13 -2
- package/README.zh.md +13 -2
- package/lib/client.js +726 -109
- package/lib/client.js.map +1 -1
- package/lib/index.js +1074 -352
- package/lib/types/client/api.d.ts +8 -4
- package/lib/types/client/locales.d.ts +27 -1
- package/lib/types/client/panel/HostFormDialog.d.ts +3 -3
- package/lib/types/client/panel/HostsTab.d.ts +3 -3
- package/lib/types/client/panel/KeysTab.d.ts +8 -0
- package/lib/types/client/panel/controller.d.ts +1 -1
- package/lib/types/connection-trust.d.ts +26 -0
- package/lib/types/engine/cluster.d.ts +7 -2
- package/lib/types/engine/connection-pool.d.ts +11 -2
- package/lib/types/engine/sftp.d.ts +5 -0
- package/lib/types/engine/terminal-sessions.d.ts +5 -3
- package/lib/types/index.d.ts +6 -5
- package/lib/types/protocol.d.ts +41 -0
- package/lib/types/routes.d.ts +3 -0
- package/lib/types/service.d.ts +148 -0
- package/lib/types/store.d.ts +16 -4
- package/package.json +1 -1
- package/src/client/api.ts +147 -58
- package/src/client/locales.ts +55 -2
- package/src/client/panel/HostFormDialog.tsx +76 -17
- package/src/client/panel/HostsTab.tsx +50 -25
- package/src/client/panel/KeysTab.tsx +192 -0
- package/src/client/panel/SshPanel.tsx +4 -1
- package/src/client/panel/TerminalTab.tsx +14 -9
- package/src/client/panel/TunnelsTab.tsx +24 -7
- package/src/client/panel/controller.ts +77 -12
- package/src/client/panel/helpers.ts +3 -2
- package/src/connection-trust.ts +70 -0
- package/src/engine/cluster.ts +45 -13
- package/src/engine/connection-pool.ts +279 -96
- package/src/engine/proxy-command.ts +27 -10
- package/src/engine/sftp.ts +69 -35
- package/src/engine/terminal-sessions.ts +110 -35
- package/src/engine/tunnel.ts +20 -2
- package/src/engine.ts +5 -8
- package/src/index.ts +113 -39
- package/src/protocol.ts +45 -0
- package/src/routes.ts +130 -23
- package/src/service.ts +158 -0
- package/src/store.ts +324 -104
- package/src/tools.ts +3 -5
package/src/store.ts
CHANGED
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
* as ssh-skill's annotated ssh-config comments; document it, never log it.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { randomBytes, randomUUID } from 'node:crypto'
|
|
11
12
|
import { homedir } from 'node:os'
|
|
12
|
-
import { dirname, join, resolve } from 'node:path'
|
|
13
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path'
|
|
13
14
|
import { dshHome } from './dsh-home.ts'
|
|
14
|
-
import type { HostPayload, ImportResult, ImportSkipBlock, ImportSkipReason, SshHostEntry, SshHostSummary } from './protocol.ts'
|
|
15
|
+
import type { HostPayload, ImportResult, ImportSkipBlock, ImportSkipReason, SshHostEntry, SshHostSummary, SshKeyEntry, SshKeyPayload, SshKeySummary, SshPanelHostSummary } from './protocol.ts'
|
|
15
16
|
import { readSshConfigBlocks } from './ssh-config.ts'
|
|
16
17
|
|
|
17
18
|
/** File format version. */
|
|
@@ -30,6 +31,26 @@ export function sshConfigPath(): string {
|
|
|
30
31
|
interface StoreFile {
|
|
31
32
|
version: number
|
|
32
33
|
hosts: SshHostEntry[]
|
|
34
|
+
/** Incremental managed-key collection; absent in every pre-feature store. */
|
|
35
|
+
keys?: SshKeyEntry[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Validate an auth block shape; returns an error message or undefined. */
|
|
39
|
+
function validateAuth(auth: unknown): string | undefined {
|
|
40
|
+
if (typeof auth !== 'object' || auth === null) return 'auth must be an object'
|
|
41
|
+
const a = auth as Record<string, unknown>
|
|
42
|
+
if (a.kind !== 'key' && a.kind !== 'password' && a.kind !== 'agent') return 'auth.kind must be key, password or agent'
|
|
43
|
+
if (a.kind === 'key') {
|
|
44
|
+
if (typeof a.keyPath !== 'string' || a.keyPath.trim() === '') return 'auth.keyPath is required for key auth'
|
|
45
|
+
if (a.passphrase !== undefined && typeof a.passphrase !== 'string') return 'auth.passphrase must be a string when provided'
|
|
46
|
+
}
|
|
47
|
+
if (a.kind === 'password' && a.password !== undefined && typeof a.password !== 'string') {
|
|
48
|
+
return 'auth.password must be a string when provided'
|
|
49
|
+
}
|
|
50
|
+
if (a.kind === 'agent' && a.agentPath !== undefined && typeof a.agentPath !== 'string') {
|
|
51
|
+
return 'auth.agentPath must be a string when provided'
|
|
52
|
+
}
|
|
53
|
+
return undefined
|
|
33
54
|
}
|
|
34
55
|
|
|
35
56
|
/** Validate the wire shape of a host payload; returns a message or undefined. */
|
|
@@ -38,19 +59,9 @@ export function validateHostPayload(payload: unknown): string | undefined {
|
|
|
38
59
|
const p = payload as Record<string, unknown>
|
|
39
60
|
if (typeof p.host !== 'string' || p.host.trim() === '') return 'host is required'
|
|
40
61
|
if (typeof p.user !== 'string' || p.user.trim() === '') return 'user is required'
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
if (auth.kind !== 'key' && auth.kind !== 'password' && auth.kind !== 'agent') return 'auth.kind must be key, password or agent'
|
|
45
|
-
if (auth.kind === 'key' && (typeof auth.keyPath !== 'string' || auth.keyPath.trim() === '')) {
|
|
46
|
-
return 'auth.keyPath is required for key auth'
|
|
47
|
-
}
|
|
48
|
-
if (auth.kind === 'password' && auth.password !== undefined && typeof auth.password !== 'string') {
|
|
49
|
-
return 'auth.password must be a string when provided'
|
|
50
|
-
}
|
|
51
|
-
if (auth.kind === 'agent' && auth.agentPath !== undefined && typeof auth.agentPath !== 'string') {
|
|
52
|
-
return 'auth.agentPath must be a string when provided'
|
|
53
|
-
}
|
|
62
|
+
if (p.auth !== undefined) {
|
|
63
|
+
const authError = validateAuth(p.auth)
|
|
64
|
+
if (authError !== undefined) return authError
|
|
54
65
|
}
|
|
55
66
|
if (p.port !== undefined && (typeof p.port !== 'number' || !Number.isInteger(p.port) || p.port < 1 || p.port > 65535)) {
|
|
56
67
|
return 'port must be an integer in 1..65535'
|
|
@@ -78,6 +89,41 @@ export function validateHostPayload(payload: unknown): string | undefined {
|
|
|
78
89
|
return undefined
|
|
79
90
|
}
|
|
80
91
|
|
|
92
|
+
/** Validate a full creation payload and build its normalized entry. */
|
|
93
|
+
function buildValidatedHostEntry(payload: HostPayload, now: number): SshHostEntry {
|
|
94
|
+
const alias = payload.alias?.trim()
|
|
95
|
+
if (!alias) throw new Error('alias is required')
|
|
96
|
+
const aliasError = validateAlias(alias)
|
|
97
|
+
if (aliasError !== undefined) throw new Error(aliasError)
|
|
98
|
+
const bodyError = validateHostPayload(payload)
|
|
99
|
+
if (bodyError !== undefined) throw new Error(bodyError)
|
|
100
|
+
if (payload.auth === undefined) throw new Error('auth is required')
|
|
101
|
+
return {
|
|
102
|
+
alias,
|
|
103
|
+
host: payload.host.trim(),
|
|
104
|
+
port: payload.port ?? 22,
|
|
105
|
+
user: payload.user.trim(),
|
|
106
|
+
auth: {
|
|
107
|
+
kind: payload.auth.kind,
|
|
108
|
+
keyPath: payload.auth.kind === 'key' ? expandHome(payload.auth.keyPath?.trim() ?? '') : undefined,
|
|
109
|
+
passphrase: payload.auth.kind === 'key' ? payload.auth.passphrase || undefined : undefined,
|
|
110
|
+
password: payload.auth.kind === 'password' ? payload.auth.password : undefined,
|
|
111
|
+
agentPath: payload.auth.kind === 'agent' ? normalizeAgentPath(payload.auth.agentPath) : undefined,
|
|
112
|
+
},
|
|
113
|
+
proxyJump: [...(payload.proxyJump ?? [])],
|
|
114
|
+
proxyCommand: normalizeProxyCommand(payload.proxyCommand),
|
|
115
|
+
description: payload.description?.trim() || undefined,
|
|
116
|
+
environment: payload.environment?.trim() || undefined,
|
|
117
|
+
tags: [...(payload.tags ?? [])].map(tag => tag.trim()).filter(tag => tag !== ''),
|
|
118
|
+
location: payload.location?.trim() || undefined,
|
|
119
|
+
// Stored only when enabled, so a store file written without agent
|
|
120
|
+
// forwarding stays byte-identical to the pre-feature format.
|
|
121
|
+
forwardAgent: payload.forwardAgent === true ? true : undefined,
|
|
122
|
+
createdAt: now,
|
|
123
|
+
updatedAt: now,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
81
127
|
/** Alias grammar: letters/digits plus dots, hyphens, underscores (IP/domain aliases included). */
|
|
82
128
|
const ALIAS_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/
|
|
83
129
|
|
|
@@ -105,22 +151,29 @@ export class HostStore {
|
|
|
105
151
|
this.sshConfigOverride = sshConfigOverride
|
|
106
152
|
}
|
|
107
153
|
|
|
108
|
-
/** Load
|
|
109
|
-
list(): SshHostEntry[] {
|
|
154
|
+
/** Load host entries, optionally filtered by the panel's search text. */
|
|
155
|
+
list(query?: string): SshHostEntry[] {
|
|
110
156
|
const file = this.load()
|
|
111
|
-
|
|
157
|
+
const needle = query?.trim().toLowerCase()
|
|
158
|
+
if (needle === undefined || needle === '') return [...file.hosts]
|
|
159
|
+
return file.hosts.filter(entry => entry.alias.toLowerCase().includes(needle)
|
|
160
|
+
|| (entry.description ?? '').toLowerCase().includes(needle)
|
|
161
|
+
|| entry.host.toLowerCase().includes(needle)
|
|
162
|
+
|| entry.tags.some(tag => tag.toLowerCase().includes(needle)))
|
|
112
163
|
}
|
|
113
164
|
|
|
114
165
|
/** Find one entry by alias. */
|
|
115
166
|
find(alias: string): SshHostEntry | undefined {
|
|
116
|
-
return this.
|
|
167
|
+
return this.load().hosts.find(entry => entry.alias === alias)
|
|
117
168
|
}
|
|
118
169
|
|
|
119
170
|
/** Secret-free projection for the browser and agent surfaces. */
|
|
120
171
|
summarize(entry: SshHostEntry): SshHostSummary {
|
|
121
172
|
let keyReady = true
|
|
122
|
-
if (entry.auth.kind === 'key'
|
|
123
|
-
keyReady =
|
|
173
|
+
if (entry.auth.kind === 'key') {
|
|
174
|
+
keyReady = entry.auth.keyPath !== undefined
|
|
175
|
+
&& entry.auth.keyPath !== ''
|
|
176
|
+
&& existsSync(expandHome(entry.auth.keyPath))
|
|
124
177
|
} else if (entry.auth.kind === 'agent') {
|
|
125
178
|
keyReady = false
|
|
126
179
|
}
|
|
@@ -147,54 +200,40 @@ export class HostStore {
|
|
|
147
200
|
}
|
|
148
201
|
}
|
|
149
202
|
|
|
203
|
+
/** Browser-only projection that can match a stored path to a managed key. */
|
|
204
|
+
summarizePanel(entry: SshHostEntry): SshPanelHostSummary {
|
|
205
|
+
return {
|
|
206
|
+
...this.summarize(entry),
|
|
207
|
+
...(entry.auth.kind === 'key' && entry.auth.keyPath !== undefined && entry.auth.keyPath !== ''
|
|
208
|
+
? { keyPath: entry.auth.keyPath }
|
|
209
|
+
: {}),
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
150
213
|
/** Create one entry. Throws on alias collision or invalid payload. */
|
|
151
214
|
create(payload: HostPayload): SshHostEntry {
|
|
152
|
-
const
|
|
153
|
-
if (!alias) throw new Error('alias is required')
|
|
154
|
-
const aliasError = validateAlias(alias)
|
|
155
|
-
if (aliasError !== undefined) throw new Error(aliasError)
|
|
156
|
-
const bodyError = validateHostPayload(payload)
|
|
157
|
-
if (bodyError !== undefined) throw new Error(bodyError)
|
|
158
|
-
if (payload.auth === undefined) throw new Error('auth is required')
|
|
215
|
+
const entry = buildValidatedHostEntry(payload, Date.now())
|
|
159
216
|
const file = this.load()
|
|
160
|
-
if (file.hosts.some(
|
|
161
|
-
|
|
162
|
-
const entry: SshHostEntry = {
|
|
163
|
-
alias,
|
|
164
|
-
host: payload.host.trim(),
|
|
165
|
-
port: payload.port ?? 22,
|
|
166
|
-
user: payload.user.trim(),
|
|
167
|
-
auth: {
|
|
168
|
-
kind: payload.auth.kind,
|
|
169
|
-
keyPath: payload.auth.kind === 'key' ? expandHome(payload.auth.keyPath?.trim() ?? '') : undefined,
|
|
170
|
-
passphrase: payload.auth.kind === 'key' ? payload.auth.passphrase ?? undefined : undefined,
|
|
171
|
-
password: payload.auth.kind === 'password' ? payload.auth.password : undefined,
|
|
172
|
-
agentPath: payload.auth.kind === 'agent' ? normalizeAgentPath(payload.auth.agentPath) : undefined,
|
|
173
|
-
},
|
|
174
|
-
proxyJump: [...(payload.proxyJump ?? [])],
|
|
175
|
-
proxyCommand: normalizeProxyCommand(payload.proxyCommand),
|
|
176
|
-
description: payload.description?.trim() || undefined,
|
|
177
|
-
environment: payload.environment?.trim() || undefined,
|
|
178
|
-
tags: [...(payload.tags ?? [])].map(tag => tag.trim()).filter(tag => tag !== ''),
|
|
179
|
-
location: payload.location?.trim() || undefined,
|
|
180
|
-
// Stored only when enabled, so a store file written without agent
|
|
181
|
-
// forwarding stays byte-identical to the pre-feature format.
|
|
182
|
-
forwardAgent: payload.forwardAgent === true ? true : undefined,
|
|
183
|
-
createdAt: now,
|
|
184
|
-
updatedAt: now,
|
|
217
|
+
if (file.hosts.some(existing => existing.alias === entry.alias)) {
|
|
218
|
+
throw new Error(`alias '${entry.alias}' already exists`)
|
|
185
219
|
}
|
|
186
|
-
|
|
187
|
-
|
|
220
|
+
const nextFile: StoreFile = {
|
|
221
|
+
version: file.version,
|
|
222
|
+
hosts: [...file.hosts, entry],
|
|
223
|
+
...(file.keys !== undefined ? { keys: file.keys } : {}),
|
|
224
|
+
}
|
|
225
|
+
this.save(nextFile)
|
|
188
226
|
return entry
|
|
189
227
|
}
|
|
190
228
|
|
|
191
229
|
/** Update the fields present in `patch`; unknown aliases throw. */
|
|
192
230
|
update(alias: string, patch: Partial<HostPayload>): SshHostEntry {
|
|
193
231
|
const file = this.load()
|
|
194
|
-
const
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
//
|
|
232
|
+
const index = file.hosts.findIndex(candidate => candidate.alias === alias)
|
|
233
|
+
if (index < 0) throw new Error(`alias '${alias}' not found`)
|
|
234
|
+
const entry = file.hosts[index]!
|
|
235
|
+
// Validate every present field before constructing the updated entry so a
|
|
236
|
+
// failed patch never partially mutates cached store state.
|
|
198
237
|
if (patch.host !== undefined && (typeof patch.host !== 'string' || patch.host.trim() === '')) {
|
|
199
238
|
throw new Error('host is required')
|
|
200
239
|
}
|
|
@@ -224,31 +263,33 @@ export class HostStore {
|
|
|
224
263
|
if (patch.tags !== undefined && (!Array.isArray(patch.tags) || patch.tags.some(x => typeof x !== 'string'))) {
|
|
225
264
|
throw new Error('tags must be an array of strings')
|
|
226
265
|
}
|
|
227
|
-
if (patch.
|
|
228
|
-
|
|
229
|
-
|
|
266
|
+
if (patch.description !== undefined && typeof patch.description !== 'string') {
|
|
267
|
+
throw new Error('description must be a string when provided')
|
|
268
|
+
}
|
|
269
|
+
if (patch.environment !== undefined && typeof patch.environment !== 'string') {
|
|
270
|
+
throw new Error('environment must be a string when provided')
|
|
271
|
+
}
|
|
272
|
+
if (patch.location !== undefined && typeof patch.location !== 'string') {
|
|
273
|
+
throw new Error('location must be a string when provided')
|
|
274
|
+
}
|
|
275
|
+
if (patch.auth !== undefined) {
|
|
276
|
+
const authError = validateAuth(patch.auth)
|
|
277
|
+
if (authError !== undefined) throw new Error(authError)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let nextAuth = entry.auth
|
|
230
281
|
if (patch.auth !== undefined) {
|
|
231
282
|
const auth = patch.auth
|
|
232
|
-
if (auth.kind !== 'key' && auth.kind !== 'password' && auth.kind !== 'agent') throw new Error('auth.kind must be key, password or agent')
|
|
233
|
-
if (auth.kind === 'key' && (typeof auth.keyPath !== 'string' || auth.keyPath.trim() === '')) {
|
|
234
|
-
throw new Error('auth.keyPath is required for key auth')
|
|
235
|
-
}
|
|
236
|
-
if (auth.kind === 'password' && auth.password !== undefined && typeof auth.password !== 'string') {
|
|
237
|
-
throw new Error('auth.password must be a string when provided')
|
|
238
|
-
}
|
|
239
|
-
if (auth.kind === 'agent' && auth.agentPath !== undefined && typeof auth.agentPath !== 'string') {
|
|
240
|
-
throw new Error('auth.agentPath must be a string when provided')
|
|
241
|
-
}
|
|
242
283
|
// A changed key path with no passphrase means the new key has none;
|
|
243
284
|
// only keep the old passphrase when the key path is unchanged.
|
|
244
285
|
const keyChanged = auth.kind === 'key'
|
|
245
286
|
&& auth.keyPath !== undefined
|
|
246
287
|
&& expandHome(auth.keyPath.trim()) !== entry.auth.keyPath
|
|
247
|
-
|
|
288
|
+
nextAuth = {
|
|
248
289
|
kind: auth.kind,
|
|
249
290
|
keyPath: auth.kind === 'key' ? expandHome(auth.keyPath?.trim() ?? '') : undefined,
|
|
250
291
|
passphrase: auth.kind === 'key'
|
|
251
|
-
? (auth.passphrase !== undefined ? auth.passphrase : (keyChanged ? undefined : entry.auth.passphrase))
|
|
292
|
+
? (auth.passphrase !== undefined ? auth.passphrase || undefined : (keyChanged ? undefined : entry.auth.passphrase))
|
|
252
293
|
: undefined,
|
|
253
294
|
password: auth.kind === 'password' ? auth.password : undefined,
|
|
254
295
|
agentPath: auth.kind === 'agent'
|
|
@@ -256,17 +297,34 @@ export class HostStore {
|
|
|
256
297
|
: undefined,
|
|
257
298
|
}
|
|
258
299
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
300
|
+
|
|
301
|
+
const nextEntry: SshHostEntry = {
|
|
302
|
+
...entry,
|
|
303
|
+
host: patch.host !== undefined ? patch.host.trim() : entry.host,
|
|
304
|
+
port: patch.port !== undefined ? patch.port : entry.port,
|
|
305
|
+
user: patch.user !== undefined ? patch.user.trim() : entry.user,
|
|
306
|
+
auth: nextAuth,
|
|
307
|
+
proxyJump: patch.proxyJump !== undefined ? [...patch.proxyJump] : entry.proxyJump,
|
|
308
|
+
proxyCommand: patch.proxyCommand !== undefined ? mergedProxyCommand : entry.proxyCommand,
|
|
309
|
+
description: patch.description !== undefined ? patch.description.trim() || undefined : entry.description,
|
|
310
|
+
environment: patch.environment !== undefined ? patch.environment.trim() || undefined : entry.environment,
|
|
311
|
+
tags: patch.tags !== undefined
|
|
312
|
+
? [...patch.tags].map(tag => tag.trim()).filter(tag => tag !== '')
|
|
313
|
+
: entry.tags,
|
|
314
|
+
location: patch.location !== undefined ? patch.location.trim() || undefined : entry.location,
|
|
315
|
+
// PATCH semantics: an omitted key keeps the stored value, false clears it.
|
|
316
|
+
forwardAgent: patch.forwardAgent !== undefined
|
|
317
|
+
? (patch.forwardAgent === true ? true : undefined)
|
|
318
|
+
: entry.forwardAgent,
|
|
319
|
+
updatedAt: Date.now(),
|
|
320
|
+
}
|
|
321
|
+
const nextFile: StoreFile = {
|
|
322
|
+
version: file.version,
|
|
323
|
+
hosts: file.hosts.map((candidate, candidateIndex) => (candidateIndex === index ? nextEntry : candidate)),
|
|
324
|
+
...(file.keys !== undefined ? { keys: file.keys } : {}),
|
|
325
|
+
}
|
|
326
|
+
this.save(nextFile)
|
|
327
|
+
return nextEntry
|
|
270
328
|
}
|
|
271
329
|
|
|
272
330
|
/** Remove one entry. */
|
|
@@ -274,8 +332,131 @@ export class HostStore {
|
|
|
274
332
|
const file = this.load()
|
|
275
333
|
const index = file.hosts.findIndex(candidate => candidate.alias === alias)
|
|
276
334
|
if (index < 0) throw new Error(`alias '${alias}' not found`)
|
|
277
|
-
|
|
278
|
-
|
|
335
|
+
const nextFile: StoreFile = {
|
|
336
|
+
version: file.version,
|
|
337
|
+
hosts: file.hosts.filter((_, candidateIndex) => candidateIndex !== index),
|
|
338
|
+
...(file.keys !== undefined ? { keys: file.keys } : {}),
|
|
339
|
+
}
|
|
340
|
+
this.save(nextFile)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** List managed keys with file-existence and host-reference metadata. */
|
|
344
|
+
listKeys(): SshKeySummary[] {
|
|
345
|
+
const file = this.load()
|
|
346
|
+
const keys = file.keys ?? []
|
|
347
|
+
if (keys.length === 0) return []
|
|
348
|
+
const usedByPath = buildUsedByPathMap(file.hosts)
|
|
349
|
+
return keys.map(key => this.summarizeKey(key, file, usedByPath))
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Create one managed key reference without reading its contents. */
|
|
353
|
+
createKey(payload: SshKeyPayload): SshKeySummary {
|
|
354
|
+
const name = requiredKeyName(payload.name)
|
|
355
|
+
const path = requiredKeyPath(payload.path)
|
|
356
|
+
const file = this.load()
|
|
357
|
+
const now = Date.now()
|
|
358
|
+
const entry: SshKeyEntry = {
|
|
359
|
+
id: randomUUID(),
|
|
360
|
+
name,
|
|
361
|
+
path,
|
|
362
|
+
passphrase: typeof payload.passphrase === 'string' && payload.passphrase !== '' ? payload.passphrase : undefined,
|
|
363
|
+
note: typeof payload.note === 'string' ? payload.note.trim() || undefined : undefined,
|
|
364
|
+
createdAt: now,
|
|
365
|
+
updatedAt: now,
|
|
366
|
+
}
|
|
367
|
+
const nextFile: StoreFile = {
|
|
368
|
+
version: file.version,
|
|
369
|
+
hosts: file.hosts,
|
|
370
|
+
keys: [...(file.keys ?? []), entry],
|
|
371
|
+
}
|
|
372
|
+
this.save(nextFile)
|
|
373
|
+
return this.summarizeKey(entry, nextFile)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Update a managed key; omitted fields and a blank passphrase keep old values. */
|
|
377
|
+
updateKey(id: string, patch: SshKeyPayload): SshKeySummary {
|
|
378
|
+
const file = this.load()
|
|
379
|
+
const keys = file.keys ?? []
|
|
380
|
+
const index = keys.findIndex(candidate => candidate.id === id)
|
|
381
|
+
if (index < 0) throw new Error(`managed key '${id}' not found`)
|
|
382
|
+
const entry = keys[index]!
|
|
383
|
+
const nextName = patch.name !== undefined ? requiredKeyName(patch.name) : entry.name
|
|
384
|
+
const nextPath = patch.path !== undefined ? requiredKeyPath(patch.path) : entry.path
|
|
385
|
+
const nextPassphrase = patch.passphrase !== undefined
|
|
386
|
+
? (patch.passphrase === null || patch.passphrase === '' ? undefined : patch.passphrase)
|
|
387
|
+
: entry.passphrase
|
|
388
|
+
const nextNote = patch.note !== undefined
|
|
389
|
+
? (typeof patch.note === 'string' ? patch.note.trim() || undefined : undefined)
|
|
390
|
+
: entry.note
|
|
391
|
+
const nextEntry: SshKeyEntry = {
|
|
392
|
+
...entry,
|
|
393
|
+
name: nextName,
|
|
394
|
+
path: nextPath,
|
|
395
|
+
passphrase: nextPassphrase,
|
|
396
|
+
note: nextNote,
|
|
397
|
+
updatedAt: Date.now(),
|
|
398
|
+
}
|
|
399
|
+
const nextFile: StoreFile = {
|
|
400
|
+
version: file.version,
|
|
401
|
+
hosts: file.hosts,
|
|
402
|
+
keys: keys.map((candidate, candidateIndex) => (candidateIndex === index ? nextEntry : candidate)),
|
|
403
|
+
}
|
|
404
|
+
this.save(nextFile)
|
|
405
|
+
return this.summarizeKey(nextEntry, nextFile)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Delete only the managed reference; stored host auth.keyPath values stay untouched. */
|
|
409
|
+
deleteKey(id: string): void {
|
|
410
|
+
const file = this.load()
|
|
411
|
+
const keys = file.keys ?? []
|
|
412
|
+
const index = keys.findIndex(candidate => candidate.id === id)
|
|
413
|
+
if (index < 0) throw new Error(`managed key '${id}' not found`)
|
|
414
|
+
const nextFile: StoreFile = {
|
|
415
|
+
version: file.version,
|
|
416
|
+
hosts: file.hosts,
|
|
417
|
+
keys: keys.filter((_, candidateIndex) => candidateIndex !== index),
|
|
418
|
+
}
|
|
419
|
+
this.save(nextFile)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Resolve a transient managedKeyId into the host's existing keyPath/passphrase shape. */
|
|
423
|
+
resolveManagedKey<T extends HostPayload | Partial<HostPayload>>(payload: T): T {
|
|
424
|
+
if (payload.managedKeyId === undefined) return payload
|
|
425
|
+
if (typeof payload.managedKeyId !== 'string' || payload.managedKeyId === '') throw new Error('managedKeyId must be a non-empty string')
|
|
426
|
+
if (payload.auth !== undefined && payload.auth.kind !== 'key') throw new Error('managedKeyId requires key auth')
|
|
427
|
+
const key = this.load().keys?.find(candidate => candidate.id === payload.managedKeyId)
|
|
428
|
+
if (key === undefined) throw new Error(`managed key '${payload.managedKeyId}' not found`)
|
|
429
|
+
const { managedKeyId: _managedKeyId, ...rest } = payload
|
|
430
|
+
return {
|
|
431
|
+
...rest,
|
|
432
|
+
auth: {
|
|
433
|
+
...payload.auth,
|
|
434
|
+
kind: 'key',
|
|
435
|
+
keyPath: key.path,
|
|
436
|
+
// An explicit empty value clears an old host passphrase when the managed key has none.
|
|
437
|
+
passphrase: key.passphrase ?? '',
|
|
438
|
+
},
|
|
439
|
+
} as T
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private summarizeKey(key: SshKeyEntry, file: StoreFile, usedByPath?: Map<string, string[]>): SshKeySummary {
|
|
443
|
+
const resolvedPath = expandHome(key.path)
|
|
444
|
+
const usedBy = usedByPath !== undefined
|
|
445
|
+
? [...(usedByPath.get(resolvedPath) ?? [])]
|
|
446
|
+
: file.hosts
|
|
447
|
+
.filter(host => host.auth.kind === 'key' && host.auth.keyPath !== undefined && host.auth.keyPath !== '' && expandHome(host.auth.keyPath) === resolvedPath)
|
|
448
|
+
.map(host => host.alias)
|
|
449
|
+
return {
|
|
450
|
+
id: key.id,
|
|
451
|
+
name: key.name,
|
|
452
|
+
path: key.path,
|
|
453
|
+
hasPassphrase: key.passphrase !== undefined && key.passphrase !== '',
|
|
454
|
+
note: key.note,
|
|
455
|
+
exists: existsSync(resolvedPath),
|
|
456
|
+
usedBy,
|
|
457
|
+
createdAt: key.createdAt,
|
|
458
|
+
updatedAt: key.updatedAt,
|
|
459
|
+
}
|
|
279
460
|
}
|
|
280
461
|
|
|
281
462
|
/**
|
|
@@ -288,25 +469,27 @@ export class HostStore {
|
|
|
288
469
|
* @returns import statistics.
|
|
289
470
|
*/
|
|
290
471
|
importFromSshConfig(): ImportResult {
|
|
291
|
-
// Per-run statistics: a later import must not report earlier runs' skips.
|
|
292
|
-
this.skippedBlocks = []
|
|
293
472
|
const configPath = this.sshConfigOverride ?? sshConfigPath()
|
|
294
473
|
if (!existsSync(configPath)) return { parsed: 0, added: 0, skipped: 0, skippedBlocks: [] }
|
|
295
474
|
const { blocks, skipped } = readSshConfigBlocks(configPath)
|
|
296
|
-
|
|
475
|
+
const skippedBlocks: ImportSkipBlock[] = [...skipped]
|
|
476
|
+
const skippedNames = new Set(skipped.map(block => block.name))
|
|
297
477
|
const skip = (name: string, reason: ImportSkipReason): void => {
|
|
298
|
-
if (name === '') return
|
|
299
|
-
|
|
300
|
-
|
|
478
|
+
if (name === '' || skippedNames.has(name)) return
|
|
479
|
+
skippedNames.add(name)
|
|
480
|
+
skippedBlocks.push({ name, reason })
|
|
301
481
|
}
|
|
302
|
-
|
|
482
|
+
const file = this.load()
|
|
483
|
+
const existingAliases = new Set(file.hosts.map(entry => entry.alias))
|
|
484
|
+
const addedEntries: SshHostEntry[] = []
|
|
485
|
+
const now = Date.now()
|
|
303
486
|
for (const block of blocks) {
|
|
304
487
|
const pattern = block.pattern.split(/\s+/)[0]
|
|
305
488
|
if (pattern.includes('*') || pattern.includes('?')) {
|
|
306
489
|
skip(pattern, 'wildcard')
|
|
307
490
|
continue
|
|
308
491
|
}
|
|
309
|
-
if (
|
|
492
|
+
if (existingAliases.has(pattern)) {
|
|
310
493
|
skip(pattern, 'existing')
|
|
311
494
|
continue
|
|
312
495
|
}
|
|
@@ -343,18 +526,25 @@ export class HostStore {
|
|
|
343
526
|
forwardAgent: parseForwardAgentValue(block.props.forwardagent),
|
|
344
527
|
}
|
|
345
528
|
try {
|
|
346
|
-
|
|
347
|
-
|
|
529
|
+
const entry = buildValidatedHostEntry(payload, now)
|
|
530
|
+
existingAliases.add(entry.alias)
|
|
531
|
+
addedEntries.push(entry)
|
|
348
532
|
} catch {
|
|
349
533
|
// Unusable entry (bad alias grammar, conflicting transports, ...).
|
|
350
534
|
skip(pattern, 'invalid')
|
|
351
535
|
}
|
|
352
536
|
}
|
|
353
|
-
|
|
537
|
+
if (addedEntries.length > 0) {
|
|
538
|
+
const nextFile: StoreFile = {
|
|
539
|
+
version: file.version,
|
|
540
|
+
hosts: [...file.hosts, ...addedEntries],
|
|
541
|
+
...(file.keys !== undefined ? { keys: file.keys } : {}),
|
|
542
|
+
}
|
|
543
|
+
this.save(nextFile)
|
|
544
|
+
}
|
|
545
|
+
return { parsed: blocks.length, added: addedEntries.length, skipped: skippedBlocks.length, skippedBlocks }
|
|
354
546
|
}
|
|
355
547
|
|
|
356
|
-
private skippedBlocks: ImportSkipBlock[] = []
|
|
357
|
-
|
|
358
548
|
/**
|
|
359
549
|
* Last parsed store keyed by file identity. list/find ride every acquire
|
|
360
550
|
* and GUI refresh; re-reading and re-parsing the whole file each call is
|
|
@@ -395,15 +585,33 @@ export class HostStore {
|
|
|
395
585
|
private save(file: StoreFile): void {
|
|
396
586
|
const dir = dirname(this.path)
|
|
397
587
|
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
398
|
-
const tmp = this.path
|
|
588
|
+
const tmp = `${this.path}.tmp-${process.pid}-${randomBytes(4).toString('hex')}`
|
|
399
589
|
// Secrets live in this file: keep it readable by the owner only. The
|
|
400
590
|
// tmp file carries the 0600 mode through the rename.
|
|
401
|
-
|
|
402
|
-
|
|
591
|
+
try {
|
|
592
|
+
writeFileSync(tmp, JSON.stringify(file, null, 2) + '\n', { encoding: 'utf8', mode: 0o600 })
|
|
593
|
+
renameSync(tmp, this.path)
|
|
594
|
+
} catch (error) {
|
|
595
|
+
try { unlinkSync(tmp) } catch { /* best effort */ }
|
|
596
|
+
throw error
|
|
597
|
+
}
|
|
403
598
|
this.cache = undefined
|
|
404
599
|
}
|
|
405
600
|
}
|
|
406
601
|
|
|
602
|
+
/** Group aliases by expanded private-key path in a single pass. */
|
|
603
|
+
function buildUsedByPathMap(hosts: readonly SshHostEntry[]): Map<string, string[]> {
|
|
604
|
+
const usedByPath = new Map<string, string[]>()
|
|
605
|
+
for (const host of hosts) {
|
|
606
|
+
if (host.auth.kind !== 'key' || !host.auth.keyPath) continue
|
|
607
|
+
const resolvedPath = expandHome(host.auth.keyPath)
|
|
608
|
+
const existing = usedByPath.get(resolvedPath)
|
|
609
|
+
if (existing === undefined) usedByPath.set(resolvedPath, [host.alias])
|
|
610
|
+
else existing.push(host.alias)
|
|
611
|
+
}
|
|
612
|
+
return usedByPath
|
|
613
|
+
}
|
|
614
|
+
|
|
407
615
|
/** Normalize an agent endpoint for storage: trim, expand `~`, and resolve the SSH_AUTH_SOCK token. */
|
|
408
616
|
export function normalizeAgentPath(agentPath: string | undefined): string | undefined {
|
|
409
617
|
const trimmed = agentPath?.trim()
|
|
@@ -437,6 +645,18 @@ export function parseForwardAgentValue(raw: string | undefined): boolean {
|
|
|
437
645
|
return value === 'yes' || value === 'true' || value === 'on'
|
|
438
646
|
}
|
|
439
647
|
|
|
648
|
+
function requiredKeyName(value: string | undefined): string {
|
|
649
|
+
if (typeof value !== 'string' || value.trim() === '') throw new Error('key name is required')
|
|
650
|
+
return value.trim()
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function requiredKeyPath(value: string | undefined): string {
|
|
654
|
+
if (typeof value !== 'string' || value.trim() === '') throw new Error('key path is required')
|
|
655
|
+
const path = expandHome(value.trim())
|
|
656
|
+
if (!isAbsolute(path)) throw new Error('key path must be absolute')
|
|
657
|
+
return path
|
|
658
|
+
}
|
|
659
|
+
|
|
440
660
|
/** Expand a leading `~` in a filesystem path. */
|
|
441
661
|
export function expandHome(path: string): string {
|
|
442
662
|
if (path === '~') return homedir()
|
package/src/tools.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
8
8
|
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
9
9
|
import type { SshEngine } from './engine.ts'
|
|
10
|
+
import { hasClusterSelector } from './engine/cluster.ts'
|
|
10
11
|
import type { ClusterResult, ExecResult, SshAuthKind, SshHostSummary, TunnelInfo } from './protocol.ts'
|
|
11
12
|
|
|
12
13
|
/** One text content block (the only render shape these tools emit). */
|
|
@@ -336,7 +337,7 @@ export function sshTunnelTool(engine: SshEngine) {
|
|
|
336
337
|
remoteHost: args.remoteHost,
|
|
337
338
|
localPort: args.localPort,
|
|
338
339
|
})
|
|
339
|
-
return { ok:
|
|
340
|
+
return { ok: tunnel.state !== 'failed', tunnel }
|
|
340
341
|
} catch (error) {
|
|
341
342
|
return { ok: false, tunnel: { id: '', alias: args.alias, localPort: 0, remoteHost: args.remoteHost ?? '127.0.0.1', remotePort: args.remotePort, state: 'failed' as const, error: error instanceof Error ? error.message : String(error), startedAt: Date.now() } }
|
|
342
343
|
}
|
|
@@ -398,10 +399,7 @@ export function sshClusterTool(engine: SshEngine) {
|
|
|
398
399
|
render: (_args, value: { results?: ClusterResult[] }) => text(renderCluster(value.results ?? [])),
|
|
399
400
|
},
|
|
400
401
|
async execute(args, exec) {
|
|
401
|
-
|
|
402
|
-
(typeof args.environment === 'string' && args.environment.trim() !== '') ||
|
|
403
|
-
(Array.isArray(args.tags) && args.tags.some((tag) => typeof tag === 'string' && tag.trim() !== ''))
|
|
404
|
-
if (!hasSelector) throw new Error('ssh_cluster requires aliases, environment, or tags to limit the target set')
|
|
402
|
+
if (!hasClusterSelector(args)) throw new Error('ssh_cluster requires aliases, environment, or tags to limit the target set')
|
|
405
403
|
// Cancellation reaches every host's in-flight command.
|
|
406
404
|
return { results: await engine.cluster({ ...args, signal: exec.signal }) }
|
|
407
405
|
},
|