@fish-under-sea/dsh-ssh-fish 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/NOTICE +1 -1
  2. package/README.en.md +5 -5
  3. package/README.md +5 -5
  4. package/README.zh.md +5 -5
  5. package/lib/client.js +783 -170
  6. package/lib/client.js.map +1 -1
  7. package/lib/index.js +128 -14
  8. package/lib/types/client/api.d.ts +21 -0
  9. package/lib/types/client/locales.d.ts +13 -2
  10. package/lib/types/client/panel/TerminalTab.d.ts +12 -7
  11. package/lib/types/client/panel/controller.d.ts +185 -21
  12. package/lib/types/engine/connection-pool.d.ts +52 -1
  13. package/lib/types/engine.d.ts +7 -0
  14. package/lib/types/index.d.ts +10 -0
  15. package/lib/types/loopback.d.ts +41 -1
  16. package/lib/types/protocol.d.ts +18 -1
  17. package/lib/types/store.d.ts +7 -0
  18. package/lib/types/tool-surface.d.ts +1 -0
  19. package/lib/types/tools.d.ts +1 -0
  20. package/locale/en.json +6 -0
  21. package/locale/zh.json +6 -0
  22. package/package.json +24 -21
  23. package/src/client/api.ts +70 -0
  24. package/src/client/index.ts +1 -2
  25. package/src/client/locales.ts +26 -4
  26. package/src/client/panel/HostFormDialog.tsx +16 -2
  27. package/src/client/panel/HostsTab.tsx +3 -0
  28. package/src/client/panel/SshPanel.tsx +7 -15
  29. package/src/client/panel/TerminalTab.tsx +371 -151
  30. package/src/client/panel/controller.ts +370 -30
  31. package/src/client/panel/panel.module.css +108 -0
  32. package/src/client/settings-binding.ts +0 -1
  33. package/src/engine/connection-pool.ts +96 -4
  34. package/src/engine.ts +10 -0
  35. package/src/index.ts +16 -1
  36. package/src/loopback.ts +57 -1
  37. package/src/protocol.ts +18 -1
  38. package/src/routes.ts +4 -1
  39. package/src/store.ts +26 -0
  40. package/src/tool-surface.ts +1 -0
  41. package/src/tools.ts +9 -4
package/src/client/api.ts CHANGED
@@ -503,6 +503,76 @@ export class SshApi {
503
503
  return this.terminalSocket(query({ session: sessionId, cols, rows }))
504
504
  }
505
505
 
506
+ /**
507
+ * End a host session this view is not attached to (closing a background
508
+ * tab). The session has no other handle from the browser: giving it a
509
+ * dedicated HTTP route would widen the host's surface for one frame, so this
510
+ * opens one short-lived attach instead and sends the close frame as soon as
511
+ * the socket is open.
512
+ *
513
+ * The answer is "is this session no longer running", which has three
514
+ * truthful answers and one lie to avoid:
515
+ * - the frame went out on an open socket: the host ends the session;
516
+ * - the host answered with an exit frame: an unknown id is reported as
517
+ * `terminal session expired` and an ended shell also exits, and in both
518
+ * cases nothing is running, so the tab really is closed;
519
+ * - the socket errored, closed before opening, or never settled inside
520
+ * `timeoutMs`: delivery is unproven, so the session may still be alive and
521
+ * the caller must keep the tab instead of claiming it closed.
522
+ * @param sessionId - the session id the host reported on its ready frame.
523
+ * @param timeoutMs - bounded wait for the delivery signal (default 5000).
524
+ * @returns true when the session is known to be gone.
525
+ */
526
+ closeTerminalSession(sessionId: string, timeoutMs = 5000): Promise<boolean> {
527
+ const target = terminalSocketUrl(
528
+ window.location,
529
+ query({ session: sessionId, cols: 2, rows: 1 }),
530
+ transportStreamBaseUrl(),
531
+ )
532
+ if (target === undefined) return Promise.resolve(false)
533
+ let socket: WebSocket
534
+ try {
535
+ socket = new WebSocket(target)
536
+ } catch {
537
+ return Promise.resolve(false)
538
+ }
539
+ return new Promise<boolean>((resolve) => {
540
+ let settled = false
541
+ let timer: ReturnType<typeof setTimeout> | undefined
542
+ const finish = (gone: boolean): void => {
543
+ if (settled) return
544
+ settled = true
545
+ if (timer !== undefined) clearTimeout(timer)
546
+ try { socket.close() } catch { /* already closed */ }
547
+ resolve(gone)
548
+ }
549
+ timer = setTimeout(() => { finish(false) }, timeoutMs)
550
+ socket.onopen = () => {
551
+ const closeFrame: TerminalClientFrame = { type: 'close' }
552
+ try {
553
+ socket.send(JSON.stringify(closeFrame))
554
+ finish(true)
555
+ } catch {
556
+ // The frame never left, which proves nothing either way: wait for the
557
+ // host's own answer (an exit frame for an id that no longer exists),
558
+ // for the socket to fail, or for the bounded wait to expire.
559
+ }
560
+ }
561
+ socket.onmessage = (event: MessageEvent<string>) => {
562
+ let frame: TerminalServerFrame
563
+ try {
564
+ frame = JSON.parse(event.data) as TerminalServerFrame
565
+ } catch {
566
+ return
567
+ }
568
+ // Exit (expired id or an ended shell) means there is nothing left to end.
569
+ if (frame.type === 'exit') finish(true)
570
+ }
571
+ socket.onerror = () => { finish(false) }
572
+ socket.onclose = () => { finish(false) }
573
+ })
574
+ }
575
+
506
576
  /** One terminal socket over either an alias (open) or a session id (attach). */
507
577
  private terminalSocket(search: string): TerminalConnection {
508
578
  const target = terminalSocketUrl(window.location, search, transportStreamBaseUrl())
@@ -71,7 +71,6 @@ export type { SshKey } from './locales.ts'
71
71
  * @param ctx - client root context (locale service).
72
72
  */
73
73
  export function apply(ctx: ClientContext): void {
74
-
75
74
  ctx.effect(() => {
76
75
  try {
77
76
  return ctx.locale.register(NS, { zh, en })
@@ -134,4 +133,4 @@ export function apply(ctx: ClientContext): void {
134
133
  ctx.effect(() => () => {
135
134
  for (const dispose of disposers.splice(0)) dispose()
136
135
  }, 'dsh-ssh: ui mounts')
137
- }
136
+ }
@@ -39,6 +39,8 @@ export const zh = {
39
39
  'hosts.deleteConfirm': '确定删除主机「{alias}」?将同时停止其隧道。',
40
40
  'hosts.imported': '解析 {parsed} 个配置块,新增 {added} 台主机,跳过 {skipped} 个。',
41
41
  'hosts.proxyBadge': '代理',
42
+ 'hosts.forwardAgentBadge': 'Agent 转发',
43
+ 'hosts.forwardAgentHint': '仅对可信主机开启:远端可用本机私钥签名(等价 ssh -A)。',
42
44
  'import.reason.wildcard': '通配符 pattern,需手动配置',
43
45
  'import.reason.existing': '别名已存在',
44
46
  'import.reason.match': 'Match 条件块,不支持',
@@ -62,8 +64,8 @@ export const zh = {
62
64
  'form.auth.password': '密码',
63
65
  'form.auth.agent': 'SSH Agent',
64
66
  'form.agentPath': 'Agent 路径',
65
- 'form.agentPathHint': '留空自动检测(SSH_AUTH_SOCK / Windows Pageant);也可填 socket 路径或 pageant',
66
- 'form.authKeepHint': '编辑时密钥/密码/Agent 留空将保留原认证方式与已存凭据;如需更换认证方式请填写新凭据。',
67
+ 'form.agentPathHint': '留空自动检测(SSH_AUTH_SOCK;Windows 上为原生 ssh-agent 命名管道);也可填 socket 路径或 pageant',
68
+ 'form.authKeepHint': '编辑时密码 / 私钥留空将保留已存凭据;Agent 路径留空是合法的自动检测,选「SSH Agent」即切换到 agent 认证。',
67
69
  'form.keyPath': '私钥路径',
68
70
  'form.keyPathHint': '如 ~/.ssh/id_ed25519',
69
71
  'form.passphrase': '密钥口令',
@@ -74,6 +76,8 @@ export const zh = {
74
76
  'form.proxyJumpHint': '逗号分隔的本地主机别名,或 [user@]host[:port] 地址,按顺序连接',
75
77
  'form.proxyCommand': 'ProxyCommand',
76
78
  'form.proxyCommandHint': 'OpenSSH ProxyCommand:%h 主机、%p 端口、%r 用户、%n 别名;留空或填 none 表示不使用。与跳板机别名互斥。',
79
+ 'form.forwardAgent': '启用 Agent 转发',
80
+ 'form.forwardAgentHint': '把本机 SSH Agent 转发到该主机(等价 ssh -A):远端可用本机私钥签名。仅对可信主机开启。',
77
81
  'form.description': '备注',
78
82
  'form.environment': '环境',
79
83
  'form.environmentHint': '如 development / production',
@@ -99,6 +103,13 @@ export const zh = {
99
103
  'terminal.auth.cancel': '取消',
100
104
  'terminal.auth.placeholder': '请输入验证码或口令',
101
105
  'terminal.auth.hint': '服务器请求进行交互式二次验证(输入仅本次生效,不落盘):',
106
+ 'terminal.tabs': '终端标签',
107
+ 'terminal.newTab': '按当前主机新开标签',
108
+ 'terminal.closeTab': '关闭标签',
109
+ 'terminal.authPending': '待验证',
110
+ 'terminal.tabExited': '已退出',
111
+ 'terminal.tabError': '出错',
112
+ 'terminal.closeFailed': '无法确认该标签已关闭:关闭帧未送达宿主。标签已保留——可重试,或切到该标签前台,用它自己的连接关闭。',
102
113
  // transfer
103
114
  'transfer.selectHost': '选择主机',
104
115
  'transfer.remotePath': '远程路径',
@@ -202,6 +213,8 @@ export const en: Record<keyof typeof zh, string> = {
202
213
  'hosts.deleteConfirm': 'Delete host "{alias}"? Its tunnels will be stopped.',
203
214
  'hosts.imported': 'Parsed {parsed} blocks, added {added} hosts, skipped {skipped}.',
204
215
  'hosts.proxyBadge': 'proxy',
216
+ 'hosts.forwardAgentBadge': 'agent-fwd',
217
+ 'hosts.forwardAgentHint': 'Enable only for hosts you trust: the remote can sign with your local private keys (equivalent to ssh -A).',
205
218
  'import.reason.wildcard': 'wildcard pattern — configure it by hand',
206
219
  'import.reason.existing': 'alias already configured',
207
220
  'import.reason.match': 'Match block — not supported',
@@ -224,8 +237,8 @@ export const en: Record<keyof typeof zh, string> = {
224
237
  'form.auth.password': 'Password',
225
238
  'form.auth.agent': 'SSH Agent',
226
239
  'form.agentPath': 'Agent path',
227
- 'form.agentPathHint': 'Leave empty to auto-detect (SSH_AUTH_SOCK / Pageant on Windows), or set a socket path or "pageant"',
228
- 'form.authKeepHint': 'While editing, empty secret fields keep the stored authentication and credentials; enter new values to switch methods.',
240
+ 'form.agentPathHint': 'Leave empty to auto-detect (SSH_AUTH_SOCK; the native ssh-agent named pipe on Windows), or set a socket path or "pageant"',
241
+ 'form.authKeepHint': 'While editing, an empty password or key field keeps the stored credential; an empty agent path is a valid auto-detect, so choosing SSH Agent switches the host to agent auth.',
229
242
  'form.keyPath': 'Private key path',
230
243
  'form.keyPathHint': 'e.g. ~/.ssh/id_ed25519',
231
244
  'form.passphrase': 'Key passphrase',
@@ -236,6 +249,8 @@ export const en: Record<keyof typeof zh, string> = {
236
249
  'form.proxyJumpHint': 'Comma-separated local aliases or [user@]host[:port] addresses, chained in order',
237
250
  'form.proxyCommand': 'ProxyCommand',
238
251
  'form.proxyCommandHint': 'OpenSSH ProxyCommand: %h host, %p port, %r user, %n alias; empty or none disables it. Mutually exclusive with the jump chain.',
252
+ 'form.forwardAgent': 'Enable agent forwarding',
253
+ 'form.forwardAgentHint': 'Forward your local SSH agent to this host (equivalent to ssh -A): the remote can sign with your private keys. Enable it only for hosts you trust.',
239
254
  'form.description': 'Description',
240
255
  'form.environment': 'Environment',
241
256
  'form.environmentHint': 'e.g. development / production',
@@ -260,6 +275,13 @@ export const en: Record<keyof typeof zh, string> = {
260
275
  'terminal.auth.cancel': 'Cancel',
261
276
  'terminal.auth.placeholder': 'Enter code or passphrase',
262
277
  'terminal.auth.hint': 'The server requested interactive 2FA (session only, never saved):',
278
+ 'terminal.tabs': 'Terminal tabs',
279
+ 'terminal.newTab': 'New tab on the current host',
280
+ 'terminal.closeTab': 'Close tab',
281
+ 'terminal.authPending': '2FA pending',
282
+ 'terminal.tabExited': 'exited',
283
+ 'terminal.tabError': 'error',
284
+ 'terminal.closeFailed': 'Could not confirm the tab closed: the close never reached the host, so the tab was kept. Retry, or bring the tab to the front and close it over its own connection.',
263
285
  'transfer.selectHost': 'Select host',
264
286
  'transfer.remotePath': 'Remote path',
265
287
  'transfer.remotePathHint': 'e.g. /tmp/ or /home/user/app.tar.gz',
@@ -32,6 +32,7 @@ interface FormState {
32
32
  agentPath: string
33
33
  proxyJump: string
34
34
  proxyCommand: string
35
+ forwardAgent: boolean
35
36
  description: string
36
37
  environment: string
37
38
  tags: string
@@ -57,6 +58,7 @@ function blankOf(editing: SshHostSummary | null | undefined): FormState {
57
58
  agentPath: '',
58
59
  proxyJump: (editing?.proxyJump ?? []).join(', '),
59
60
  proxyCommand: editing?.proxyCommand ?? '',
61
+ forwardAgent: editing?.forwardAgent === true,
60
62
  description: editing?.description ?? '',
61
63
  environment: editing?.environment ?? '',
62
64
  tags: (editing?.tags ?? []).join(', '),
@@ -104,12 +106,15 @@ export function HostFormDialog({ api, editing, onClose, onSaved }: HostFormDialo
104
106
  }
105
107
  // Secrets are never echoed back by the API: when editing and the secret
106
108
  // fields are left empty, the auth block is omitted so the stored
107
- // authentication is preserved.
109
+ // authentication is preserved. The agent path is not a secret — an empty
110
+ // one is the documented "auto-detect" choice — so an agent form always
111
+ // submits its auth block; omitting it would make the PATCH a silent no-op
112
+ // that keeps the previous auth kind (#1910).
108
113
  const secretEmpty = form.authKind === 'password'
109
114
  ? form.password === ''
110
115
  : form.authKind === 'key'
111
116
  ? form.keyPath.trim() === ''
112
- : form.agentPath.trim() === ''
117
+ : false
113
118
  const auth: HostPayload['auth'] = editing != null && secretEmpty
114
119
  ? undefined
115
120
  : form.authKind === 'password'
@@ -126,6 +131,10 @@ export function HostFormDialog({ api, editing, onClose, onSaved }: HostFormDialo
126
131
  // Always sent (never omitted): an empty value is the explicit clear,
127
132
  // because the API cannot express "remove this field" any other way.
128
133
  proxyCommand: form.proxyCommand.trim(),
134
+ // Always sent (never omitted): the flag is a plain boolean, so saving the
135
+ // form can both enable and clear forwarding (omitting it would keep the
136
+ // stored value under PATCH semantics).
137
+ forwardAgent: form.forwardAgent,
129
138
  description: form.description.trim() === '' ? undefined : form.description.trim(),
130
139
  environment: form.environment.trim() === '' ? undefined : form.environment.trim(),
131
140
  tags: splitList(form.tags),
@@ -227,6 +236,11 @@ export function HostFormDialog({ api, editing, onClose, onSaved }: HostFormDialo
227
236
  />
228
237
  <span className={css.hint}>{tt('form.proxyCommandHint')}</span>
229
238
  </label>
239
+ <label className={css.field}>
240
+ <span className={css.fieldLabel}>{tt('form.forwardAgent')}</span>
241
+ <input type="checkbox" checked={form.forwardAgent} onChange={event => { set('forwardAgent', event.target.checked) }} />
242
+ <span className={css.hint}>{tt('form.forwardAgentHint')}</span>
243
+ </label>
230
244
  <div className={css.formRow}>
231
245
  <label className={css.field}>
232
246
  <span className={css.fieldLabel}>{tt('form.environment')}</span>
@@ -187,6 +187,9 @@ export function HostsTab({ api, onConnect }: HostsTabProps) {
187
187
  {host.proxyCommand !== undefined && (
188
188
  <span className={css.badge} data-kind="proxy" title={host.proxyCommand}>{tt('hosts.proxyBadge')}</span>
189
189
  )}
190
+ {host.forwardAgent === true && (
191
+ <span className={css.badge} data-kind="forward-agent" title={tt('hosts.forwardAgentHint')}>{tt('hosts.forwardAgentBadge')}</span>
192
+ )}
190
193
  </td>
191
194
  <td>{host.user}</td>
192
195
  <td><span className={css.badge} data-kind={host.auth}>{host.auth === 'key' ? tt('form.auth.key') : host.auth === 'password' ? tt('form.auth.password') : tt('form.auth.agent')}</span></td>
@@ -2,12 +2,11 @@
2
2
  * The SSH operations panel shell: a header with a close control, a five-tab
3
3
  * bar, and the active tab's content.
4
4
  *
5
- * The active tab, the pending connect request and the live terminal session id
6
- * live in the controller, not in component state: the layout mounts this page
7
- * only while the panel is selected, so local state would reset the tab on
8
- * every panel switch and lose the id the terminal tab needs to reattach its
9
- * host-side session. Inactive tabs unmount, so each tab fetches its own data
10
- * on activation.
5
+ * The active tab and the terminal tabs live in the controller, not in
6
+ * component state: the layout mounts this page only while the panel is
7
+ * selected, so local state would reset the tab on every panel switch and lose
8
+ * the ids the terminal tab needs to reattach its host-side sessions. Inactive
9
+ * tabs unmount, so each tab fetches its own data on activation.
11
10
  */
12
11
  import { useCallback, useSyncExternalStore } from 'react'
13
12
  import type { SshApi } from '../api.ts'
@@ -41,7 +40,7 @@ const TABS: ReadonlyArray<{ id: SshTab; label: () => string }> = [
41
40
 
42
41
  /** The tabbed SSH panel. */
43
42
  export function SshPanel({ controller, api, terminalFont }: SshPanelProps) {
44
- const { panelOpen, activeTab, connectRequest, terminalSessionId } = useSyncExternalStore(
43
+ const { panelOpen, activeTab } = useSyncExternalStore(
45
44
  useCallback(listener => controller.subscribe(listener), [controller]),
46
45
  useCallback(() => controller.getSnapshot(), [controller]),
47
46
  )
@@ -72,14 +71,7 @@ export function SshPanel({ controller, api, terminalFont }: SshPanelProps) {
72
71
  <div className={css.panelContent}>
73
72
  {activeTab === 'hosts' && <HostsTab api={api} onConnect={alias => { controller.requestConnect(alias) }} />}
74
73
  {activeTab === 'terminal' && (
75
- <TerminalTab
76
- api={api}
77
- controller={controller}
78
- presetAlias={connectRequest?.alias}
79
- requestId={connectRequest?.nonce}
80
- sessionId={terminalSessionId}
81
- terminalFont={terminalFont}
82
- />
74
+ <TerminalTab api={api} controller={controller} terminalFont={terminalFont} />
83
75
  )}
84
76
  {activeTab === 'transfer' && <TransferTab api={api} />}
85
77
  {activeTab === 'tunnels' && <TunnelsTab api={api} active={panelOpen} />}