@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.
Files changed (47) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +13 -2
  3. package/README.zh.md +13 -2
  4. package/lib/client.js +726 -109
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +1074 -352
  7. package/lib/types/client/api.d.ts +8 -4
  8. package/lib/types/client/locales.d.ts +27 -1
  9. package/lib/types/client/panel/HostFormDialog.d.ts +3 -3
  10. package/lib/types/client/panel/HostsTab.d.ts +3 -3
  11. package/lib/types/client/panel/KeysTab.d.ts +8 -0
  12. package/lib/types/client/panel/controller.d.ts +1 -1
  13. package/lib/types/connection-trust.d.ts +26 -0
  14. package/lib/types/engine/cluster.d.ts +7 -2
  15. package/lib/types/engine/connection-pool.d.ts +11 -2
  16. package/lib/types/engine/sftp.d.ts +5 -0
  17. package/lib/types/engine/terminal-sessions.d.ts +5 -3
  18. package/lib/types/index.d.ts +6 -5
  19. package/lib/types/protocol.d.ts +41 -0
  20. package/lib/types/routes.d.ts +3 -0
  21. package/lib/types/service.d.ts +148 -0
  22. package/lib/types/store.d.ts +16 -4
  23. package/package.json +1 -1
  24. package/src/client/api.ts +147 -58
  25. package/src/client/locales.ts +55 -2
  26. package/src/client/panel/HostFormDialog.tsx +76 -17
  27. package/src/client/panel/HostsTab.tsx +50 -25
  28. package/src/client/panel/KeysTab.tsx +192 -0
  29. package/src/client/panel/SshPanel.tsx +4 -1
  30. package/src/client/panel/TerminalTab.tsx +14 -9
  31. package/src/client/panel/TunnelsTab.tsx +24 -7
  32. package/src/client/panel/controller.ts +77 -12
  33. package/src/client/panel/helpers.ts +3 -2
  34. package/src/connection-trust.ts +70 -0
  35. package/src/engine/cluster.ts +45 -13
  36. package/src/engine/connection-pool.ts +279 -96
  37. package/src/engine/proxy-command.ts +27 -10
  38. package/src/engine/sftp.ts +69 -35
  39. package/src/engine/terminal-sessions.ts +110 -35
  40. package/src/engine/tunnel.ts +20 -2
  41. package/src/engine.ts +5 -8
  42. package/src/index.ts +113 -39
  43. package/src/protocol.ts +45 -0
  44. package/src/routes.ts +130 -23
  45. package/src/service.ts +158 -0
  46. package/src/store.ts +324 -104
  47. package/src/tools.ts +3 -5
@@ -23,7 +23,7 @@
23
23
  import type { TerminalConnection } from '../api.ts'
24
24
 
25
25
  /** The panel's tab identifiers. */
26
- export type SshTab = 'hosts' | 'terminal' | 'transfer' | 'tunnels' | 'cluster'
26
+ export type SshTab = 'hosts' | 'keys' | 'terminal' | 'transfer' | 'tunnels' | 'cluster'
27
27
 
28
28
  /** Stable id shared by the sidebar panel row and the main-slot page. */
29
29
  export const SSH_PANEL_ID = 'ssh'
@@ -117,13 +117,55 @@ const MAX_RETAINED_BYTES = 256 * 1024
117
117
  */
118
118
  function utf8Bytes(text: string): number {
119
119
  let bytes = 0
120
- for (const char of text) {
121
- const code = char.codePointAt(0) ?? 0
122
- bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4
120
+ for (let i = 0; i < text.length; i += 1) {
121
+ const code = text.charCodeAt(i)
122
+ if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) {
123
+ const low = text.charCodeAt(i + 1)
124
+ if (low >= 0xdc00 && low <= 0xdfff) {
125
+ bytes += 4
126
+ i += 1
127
+ continue
128
+ }
129
+ }
130
+ bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : 3
123
131
  }
124
132
  return bytes
125
133
  }
126
134
 
135
+ /** Trim leading code units from an oversized chunk so its tail fits maxBytes without splitting surrogate pairs. */
136
+ function trimTrailingUtf8(text: string, maxBytes: number): { text: string; bytes: number } {
137
+ let bytes = 0
138
+ let start = text.length
139
+ for (let i = text.length - 1; i >= 0; i -= 1) {
140
+ const code = text.charCodeAt(i)
141
+ let charBytes: number
142
+ let stepStart = i
143
+ if (code >= 0xdc00 && code <= 0xdfff && i > 0) {
144
+ const high = text.charCodeAt(i - 1)
145
+ if (high >= 0xd800 && high <= 0xdbff) {
146
+ charBytes = 4
147
+ stepStart = i - 1
148
+ } else {
149
+ charBytes = 3
150
+ }
151
+ } else {
152
+ charBytes = code < 0x80 ? 1 : code < 0x800 ? 2 : 3
153
+ }
154
+ if (bytes + charBytes > maxBytes) break
155
+ bytes += charBytes
156
+ start = stepStart
157
+ i = stepStart
158
+ }
159
+ return { text: start === 0 ? text : text.slice(start), bytes }
160
+ }
161
+
162
+ interface RetainedScreen {
163
+ chunks: string[]
164
+ chunkBytes: number[]
165
+ head: number
166
+ bytes: number
167
+ }
168
+
127
169
  /** The panel state owner the sidebar row and the view both read. */
128
170
  export class PanelController {
129
171
  private panelOpen = false
@@ -138,7 +180,7 @@ export class PanelController {
138
180
  * not re-render the panel on every chunk, and only the terminal view reads
139
181
  * this (when a tab has no session left to replay from).
140
182
  */
141
- private readonly retainedScreens = new Map<string, { chunks: string[]; bytes: number }>()
183
+ private readonly retainedScreens = new Map<string, RetainedScreen>()
142
184
  /**
143
185
  * Sockets that outlived their view while still connecting. Their session id
144
186
  * is unknown, so they cannot be detached yet; keeping them here is what
@@ -403,19 +445,42 @@ export class PanelController {
403
445
 
404
446
  /** Append one output chunk to the retained screen of a tab. */
405
447
  appendTerminalOutput(id: string, chunk: string): void {
406
- const screen = this.retainedScreens.get(id) ?? { chunks: [], bytes: 0 }
448
+ if (chunk === '') return
449
+ const size = utf8Bytes(chunk)
450
+ if (size > MAX_RETAINED_BYTES) {
451
+ const trimmed = trimTrailingUtf8(chunk, MAX_RETAINED_BYTES)
452
+ this.retainedScreens.set(id, {
453
+ chunks: trimmed.text === '' ? [] : [trimmed.text],
454
+ chunkBytes: trimmed.text === '' ? [] : [trimmed.bytes],
455
+ head: 0,
456
+ bytes: trimmed.bytes,
457
+ })
458
+ return
459
+ }
460
+ let screen = this.retainedScreens.get(id)
461
+ if (screen === undefined) {
462
+ this.retainedScreens.set(id, { chunks: [chunk], chunkBytes: [size], head: 0, bytes: size })
463
+ return
464
+ }
407
465
  screen.chunks.push(chunk)
408
- screen.bytes += utf8Bytes(chunk)
409
- while (screen.bytes > MAX_RETAINED_BYTES && screen.chunks.length > 1) {
410
- const dropped = screen.chunks.shift() ?? ''
411
- screen.bytes -= utf8Bytes(dropped)
466
+ screen.chunkBytes.push(size)
467
+ screen.bytes += size
468
+ while (screen.bytes > MAX_RETAINED_BYTES && screen.chunks.length - screen.head > 1) {
469
+ screen.bytes -= screen.chunkBytes[screen.head]!
470
+ screen.head += 1
471
+ }
472
+ if (screen.head >= 256 && screen.head * 2 >= screen.chunks.length) {
473
+ screen.chunks = screen.chunks.slice(screen.head)
474
+ screen.chunkBytes = screen.chunkBytes.slice(screen.head)
475
+ screen.head = 0
412
476
  }
413
- this.retainedScreens.set(id, screen)
414
477
  }
415
478
 
416
479
  /** The retained screen of a tab, concatenated for a redraw. */
417
480
  readTerminalOutput(id: string): string {
418
- return (this.retainedScreens.get(id)?.chunks ?? []).join('')
481
+ const screen = this.retainedScreens.get(id)
482
+ if (screen === undefined) return ''
483
+ return screen.head === 0 ? screen.chunks.join('') : screen.chunks.slice(screen.head).join('')
419
484
  }
420
485
 
421
486
  /** Forget the retained screen (a fresh replay from the host replaces it). */
@@ -10,8 +10,9 @@ export type TranslateValues = Record<string, string | number>
10
10
 
11
11
  /** Active dictionary, picked by the document language at call time. */
12
12
  export function dictionary(): Record<string, string> {
13
- const lang = typeof document !== 'undefined' ? document.documentElement.lang : 'zh'
14
- return lang.toLowerCase().startsWith('en') ? { ...en } : { ...zh }
13
+ // Chinese keeps the Chinese table; every other UI language falls back to English.
14
+ const lang = (typeof document !== 'undefined' && document.documentElement.lang !== '' ? document.documentElement.lang : 'zh').toLowerCase()
15
+ return lang.startsWith('zh') ? { ...zh } : { ...en }
15
16
  }
16
17
 
17
18
  /**
@@ -0,0 +1,70 @@
1
+ // Generated by scripts/sync-shared.mjs from shared/host/connection-trust.ts. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
2
+ /**
3
+ * Optional bridge to the Host connection trust decision. Newer hosts expose
4
+ * admit(request); the rc.3 SDK cohort exposes requestRejection(request). A
5
+ * missing or throwing bridge is not an admission: callers must retain their
6
+ * existing loopback or pairing fence.
7
+ */
8
+
9
+ import type { IncomingMessage } from 'node:http'
10
+ import type { Context } from '@deepseek-ai/cordis'
11
+
12
+ /** Structural host connection face across supported SDK cohorts. */
13
+ export interface HostConnectionService {
14
+ requestRejection?: (request: IncomingMessage) => unknown
15
+ admit?: (request: IncomingMessage) => { readonly peer?: unknown; readonly rejection?: unknown }
16
+ }
17
+
18
+ /** Lazily read the service captured in the plugin's dependency scope. */
19
+ export type HostConnectionReader = () => HostConnectionService | undefined
20
+
21
+ function resolveConnection(ctx: Context): HostConnectionService | undefined {
22
+ try {
23
+ const candidate = typeof ctx.get === 'function'
24
+ ? (ctx.get('connection', false) ?? ctx.get('connection'))
25
+ : (ctx as { connection?: HostConnectionService }).connection
26
+ return candidate !== undefined && candidate !== null && (
27
+ typeof candidate.admit === 'function' || typeof candidate.requestRejection === 'function'
28
+ )
29
+ ? candidate
30
+ : undefined
31
+ } catch {
32
+ return undefined
33
+ }
34
+ }
35
+
36
+ /** Whether the Host's own authenticated/trusted-host decision admits a request. */
37
+ export function connectionAdmits(connection: HostConnectionService | undefined, request: IncomingMessage): boolean {
38
+ if (connection === undefined) return false
39
+ try {
40
+ if (typeof connection.admit === 'function') {
41
+ const admission = connection.admit(request)
42
+ return admission.rejection === undefined && 'peer' in admission && admission.peer !== undefined
43
+ }
44
+ if (typeof connection.requestRejection === 'function') return connection.requestRejection(request) === undefined
45
+ return false
46
+ } catch {
47
+ return false
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Capture connection in a scope that also injects connection.authentication:
53
+ * admit(request) resolves that service through the calling fiber. The returned
54
+ * reader falls back to a bare optional lookup on cohorts without that service.
55
+ */
56
+ export function captureConnection(ctx: Context, effectName: string): HostConnectionReader {
57
+ let captured: HostConnectionService | undefined
58
+ const inject = (ctx as { inject?: (names: readonly string[], callback: (scoped: Context) => unknown) => unknown }).inject
59
+ if (typeof inject === 'function') {
60
+ inject.call(ctx, ['connection', 'connection.authentication'], (scoped: Context) => {
61
+ scoped.effect(() => {
62
+ captured = resolveConnection(scoped)
63
+ return () => {
64
+ captured = undefined
65
+ }
66
+ }, effectName)
67
+ })
68
+ }
69
+ return () => captured ?? resolveConnection(ctx)
70
+ }
@@ -6,11 +6,32 @@
6
6
  import type { ClusterResult } from '../protocol.ts'
7
7
  import { execCommand, type PoolEngine } from './connection-pool.ts'
8
8
 
9
- interface ClusterOptions {
10
- command: string
9
+ /** Target selector fields shared by cluster engine, routes, and agent tool. */
10
+ export interface ClusterSelector {
11
11
  aliases?: string[]
12
12
  environment?: string
13
13
  tags?: string[]
14
+ }
15
+
16
+ function normalizeStringList(values: unknown): string[] | undefined {
17
+ if (!Array.isArray(values)) return undefined
18
+ return values
19
+ .filter((value): value is string => typeof value === 'string')
20
+ .map(value => value.trim())
21
+ .filter(value => value !== '')
22
+ }
23
+
24
+ /** Whether at least one trimmed non-empty cluster target filter is present. */
25
+ export function hasClusterSelector(options: ClusterSelector): boolean {
26
+ const aliases = normalizeStringList(options.aliases)
27
+ if (aliases !== undefined && aliases.length > 0) return true
28
+ if (typeof options.environment === 'string' && options.environment.trim() !== '') return true
29
+ const tags = normalizeStringList(options.tags)
30
+ return tags !== undefined && tags.length > 0
31
+ }
32
+
33
+ interface ClusterOptions extends ClusterSelector {
34
+ command: string
14
35
  timeoutMs?: number
15
36
  maxWorkers?: number
16
37
  /** Caller cancellation, forwarded to every host's command. */
@@ -19,23 +40,25 @@ interface ClusterOptions {
19
40
 
20
41
  /** Run one command against many hosts concurrently. */
21
42
  export async function cluster(engine: PoolEngine, options: ClusterOptions): Promise<ClusterResult[]> {
22
- const hasAliases = Array.isArray(options.aliases) && options.aliases.some(alias => typeof alias === 'string' && alias.trim() !== '')
23
- const hasEnvironment = typeof options.environment === 'string' && options.environment.trim() !== ''
24
- const hasTags = Array.isArray(options.tags) && options.tags.some(tag => typeof tag === 'string' && tag.trim() !== '')
25
- if (!hasAliases && !hasEnvironment && !hasTags) {
43
+ const aliases = normalizeStringList(options.aliases)
44
+ const environment = typeof options.environment === 'string' && options.environment.trim() !== ''
45
+ ? options.environment.trim()
46
+ : undefined
47
+ const tags = normalizeStringList(options.tags)
48
+ if (!hasClusterSelector({ aliases, environment, tags })) {
26
49
  throw new Error('ssh_cluster requires aliases, environment, or tags to limit the target set')
27
50
  }
28
51
 
29
52
  let targets = engine.store.list()
30
- if (options.aliases !== undefined && options.aliases.length > 0) {
31
- targets = targets.filter(entry => options.aliases!.includes(entry.alias))
53
+ if (aliases !== undefined && aliases.length > 0) {
54
+ targets = targets.filter(entry => aliases.includes(entry.alias))
32
55
  }
33
- if (options.environment !== undefined && options.environment !== '') {
34
- targets = targets.filter(entry => entry.environment === options.environment)
56
+ if (environment !== undefined) {
57
+ targets = targets.filter(entry => entry.environment === environment)
35
58
  }
36
- if (options.tags !== undefined && options.tags.length > 0) {
59
+ if (tags !== undefined && tags.length > 0) {
37
60
  // ALL semantics (matches the ssh_cluster tool description).
38
- targets = targets.filter(entry => options.tags!.every(tag => entry.tags.includes(tag)))
61
+ targets = targets.filter(entry => tags.every(tag => entry.tags.includes(tag)))
39
62
  }
40
63
  if (targets.length === 0) return []
41
64
  if (options.maxWorkers !== undefined && (!Number.isInteger(options.maxWorkers) || options.maxWorkers < 1)) {
@@ -49,7 +72,16 @@ export async function cluster(engine: PoolEngine, options: ClusterOptions): Prom
49
72
  const entry = queue.shift()!
50
73
  try {
51
74
  const result = await execCommand(engine, entry.alias, options.command, options.timeoutMs, options.signal)
52
- results.push({ alias: entry.alias, ok: result.success, exitCode: result.exitCode, timedOut: result.timedOut, stdout: result.stdout, stderr: result.stderr, durationMs: result.durationMs })
75
+ results.push({
76
+ alias: entry.alias,
77
+ ok: result.success,
78
+ exitCode: result.exitCode,
79
+ timedOut: result.timedOut,
80
+ stdout: result.stdout,
81
+ stderr: result.stderr,
82
+ durationMs: result.durationMs,
83
+ ...(result.error !== undefined ? { error: result.error } : {}),
84
+ })
53
85
  } catch (error) {
54
86
  results.push({ alias: entry.alias, ok: false, error: error instanceof Error ? error.message : String(error) })
55
87
  }