@fish-under-sea/dsh-ssh-fish 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +23 -0
- package/README.en.md +29 -0
- package/README.i18n.yaml +6 -0
- package/README.md +28 -2
- package/README.zh.md +29 -0
- package/cordis.patch.yml +12 -0
- package/icon.svg +3 -0
- package/lib/client.js +16487 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +3511 -0
- package/lib/types/client/api.d.ts +141 -0
- package/lib/types/client/index.d.ts +37 -0
- package/lib/types/client/locales.d.ts +164 -0
- package/lib/types/client/native-panel.d.ts +70 -0
- package/lib/types/client/panel/ClusterTab.d.ts +7 -0
- package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
- package/lib/types/client/panel/HostsTab.d.ts +25 -0
- package/lib/types/client/panel/SshPanel.d.ts +14 -0
- package/lib/types/client/panel/TerminalTab.d.ts +22 -0
- package/lib/types/client/panel/TransferTab.d.ts +7 -0
- package/lib/types/client/panel/TunnelsTab.d.ts +19 -0
- package/lib/types/client/panel/controller.d.ts +89 -0
- package/lib/types/client/panel/helpers.d.ts +45 -0
- package/lib/types/client/panel/xterm.css.d.ts +2 -0
- package/lib/types/client/settings-binding.d.ts +61 -0
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/engine/cluster.d.ts +19 -0
- package/lib/types/engine/connection-pool.d.ts +112 -0
- package/lib/types/engine/proxy-command.d.ts +36 -0
- package/lib/types/engine/pty.d.ts +32 -0
- package/lib/types/engine/sftp.d.ts +20 -0
- package/lib/types/engine/terminal-sessions.d.ts +67 -0
- package/lib/types/engine/tunnel.d.ts +36 -0
- package/lib/types/engine.d.ts +89 -0
- package/lib/types/http.d.ts +59 -0
- package/lib/types/index.d.ts +83 -0
- package/lib/types/loopback.d.ts +24 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/lib/types/protocol.d.ts +239 -0
- package/lib/types/routes.d.ts +33 -0
- package/lib/types/ssh-config.d.ts +34 -0
- package/lib/types/store.d.ts +71 -0
- package/lib/types/tool-surface.d.ts +71 -0
- package/lib/types/tools.d.ts +42 -0
- package/package.json +109 -4
- package/src/client/api.ts +562 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +137 -0
- package/src/client/locales.ts +338 -0
- package/src/client/native-panel.tsx +141 -0
- package/src/client/panel/ClusterTab.tsx +123 -0
- package/src/client/panel/HostFormDialog.tsx +258 -0
- package/src/client/panel/HostsTab.tsx +325 -0
- package/src/client/panel/SshPanel.tsx +90 -0
- package/src/client/panel/TerminalTab.tsx +330 -0
- package/src/client/panel/TransferTab.tsx +232 -0
- package/src/client/panel/TunnelsTab.tsx +244 -0
- package/src/client/panel/controller.ts +167 -0
- package/src/client/panel/helpers.ts +84 -0
- package/src/client/panel/panel.module.css +876 -0
- package/src/client/panel/xterm.css.ts +2 -0
- package/src/client/settings-binding.ts +165 -0
- package/src/dsh-home.ts +42 -0
- package/src/engine/cluster.ts +60 -0
- package/src/engine/connection-pool.ts +543 -0
- package/src/engine/proxy-command.ts +248 -0
- package/src/engine/pty.ts +78 -0
- package/src/engine/sftp.ts +226 -0
- package/src/engine/terminal-sessions.ts +280 -0
- package/src/engine/tunnel.ts +194 -0
- package/src/engine.ts +176 -0
- package/src/http.ts +127 -0
- package/src/index.ts +234 -0
- package/src/loopback.ts +63 -0
- package/src/mount-once.ts +142 -0
- package/src/protocol.ts +230 -0
- package/src/routes.ts +494 -0
- package/src/ssh-config.ts +153 -0
- package/src/store.ts +419 -0
- package/src/tool-surface.ts +95 -0
- package/src/tools.ts +404 -0
package/package.json
CHANGED
|
@@ -1,6 +1,111 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fish-under-sea/dsh-ssh-fish",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Remote SSH operations for the dsh web GUI: host config store (~/.dsh/dsh-ssh.json, import from ~/.ssh/config), a persistent ssh2 connection pool with jump-host support, exec / PTY web terminal / SFTP transfer / local port-forward tunnels / cluster execution, plus agent tools (ssh_list, ssh_exec, ssh_upload, ssh_download, ssh_tunnel, ssh_cluster). Hot-pluggable — mounted via ~/.dsh/cordis.patch.yml + a profile node_modules symlink, no dsh source changes.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"icon": "icon.svg",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@11.7.0",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
10
|
+
},
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"types": "lib/types/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./lib/types/index.d.ts",
|
|
16
|
+
"default": "./lib/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./client": {
|
|
19
|
+
"types": "./lib/types/client/index.d.ts",
|
|
20
|
+
"default": "./lib/client.js"
|
|
21
|
+
},
|
|
22
|
+
"./src/*": "./src/*",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"dsh": {
|
|
26
|
+
"engines": {
|
|
27
|
+
"dsh": ">=0.2.0-rc.2"
|
|
28
|
+
},
|
|
29
|
+
"bundle": {
|
|
30
|
+
"patch": "./cordis.patch.yml"
|
|
31
|
+
},
|
|
32
|
+
"client": {
|
|
33
|
+
"inject": [
|
|
34
|
+
"@deepseek-ai/dsh-client-locale",
|
|
35
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
36
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
37
|
+
"@deepseek-ai/dsh-client-ui-layout"
|
|
38
|
+
],
|
|
39
|
+
"platform": "web"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -b tsconfig.build.json && tsdown",
|
|
44
|
+
"bundle": "tsdown",
|
|
45
|
+
"watch": "tsdown --watch",
|
|
46
|
+
"typecheck": "tsc --noEmit"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@deepseek-ai/schemastery": "^3.18.4",
|
|
50
|
+
"@xterm/addon-fit": "^0.11.0",
|
|
51
|
+
"@xterm/xterm": "^6.0.0",
|
|
52
|
+
"ssh2": "^1.17.0",
|
|
53
|
+
"ws": "^8.18.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@deepseek-ai/dsh": ">=0.2.0-rc.2",
|
|
57
|
+
"react": "^18.2.0",
|
|
58
|
+
"react-dom": "^18.2.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@deepseek-ai/cordis": "^4.0.4",
|
|
62
|
+
"@deepseek-ai/dsh-client-connection": "^0.2.0-rc.2",
|
|
63
|
+
"@deepseek-ai/dsh-client-locale": "^0.2.0-rc.2",
|
|
64
|
+
"@deepseek-ai/dsh-client-ui-renderer": "^0.2.0-rc.2",
|
|
65
|
+
"@deepseek-ai/dsh-client-ui-layout": "^0.2.0-rc.2",
|
|
66
|
+
"@deepseek-ai/dsh-client-ui-settings": "^0.2.0-rc.2",
|
|
67
|
+
"@deepseek-ai/dsh-client-ui-sidebar": "^0.2.0-rc.2",
|
|
68
|
+
"@deepseek-ai/dsh-client-ui-slots": "^0.2.0-rc.2",
|
|
69
|
+
"@deepseek-ai/dsh-host-webserver": "^0.2.0-rc.2",
|
|
70
|
+
"@deepseek-ai/dsh-llm": "^0.2.0-rc.2",
|
|
71
|
+
"@deepseek-ai/dsh-scope": "^0.2.0-rc.2",
|
|
72
|
+
"@deepseek-ai/dsh-settings": "^0.2.0-rc.2",
|
|
73
|
+
"@deepseek-ai/dsh-system-prompt": "^0.2.0-rc.2",
|
|
74
|
+
"@deepseek-ai/dsh-timeout": "^0.2.0-rc.2",
|
|
75
|
+
"@deepseek-ai/dsh-tools": "^0.2.0-rc.2",
|
|
76
|
+
"@types/node": "^22.20.0",
|
|
77
|
+
"@types/react": "~18.3.1",
|
|
78
|
+
"@types/react-dom": "^18.3.5",
|
|
79
|
+
"@types/ssh2": "^1.15.5",
|
|
80
|
+
"@types/ws": "^8.5.13",
|
|
81
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
82
|
+
"jsdom": "^25.0.0",
|
|
83
|
+
"react": "^18.3.1",
|
|
84
|
+
"react-dom": "^18.3.1",
|
|
85
|
+
"tsdown": "0.22.2",
|
|
86
|
+
"typescript": "~5.7.2",
|
|
87
|
+
"vitest": "^4.1.11"
|
|
88
|
+
},
|
|
89
|
+
"files": [
|
|
90
|
+
"lib/**/*.js",
|
|
91
|
+
"lib/**/*.js.map",
|
|
92
|
+
"lib/**/*.d.ts",
|
|
93
|
+
"lib/**/*.d.ts.map",
|
|
94
|
+
"src",
|
|
95
|
+
"cordis.patch.yml",
|
|
96
|
+
"README.md",
|
|
97
|
+
"icon.svg",
|
|
98
|
+
"README.en.md",
|
|
99
|
+
"LICENSE",
|
|
100
|
+
"NOTICE"
|
|
101
|
+
],
|
|
102
|
+
"publishConfig": {
|
|
103
|
+
"access": "public"
|
|
104
|
+
},
|
|
105
|
+
"license": "Apache-2.0",
|
|
106
|
+
"repository": {
|
|
107
|
+
"type": "git",
|
|
108
|
+
"url": "https://github.com/Fish-under-sea/dsh-fish.git",
|
|
109
|
+
"directory": "packages/dsh-ssh-fish"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side API client for the /api/dsh-ssh route family. The only data
|
|
3
|
+
* access path the panel components use — plain fetch/WebSocket, same origin.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
SSH_API,
|
|
8
|
+
type ClusterResult,
|
|
9
|
+
type ExecResult,
|
|
10
|
+
type HostPayload,
|
|
11
|
+
type ImportResult,
|
|
12
|
+
type RemoteDirEntry,
|
|
13
|
+
type SshHostSummary,
|
|
14
|
+
type TerminalClientFrame,
|
|
15
|
+
type TerminalServerFrame,
|
|
16
|
+
type TestResult,
|
|
17
|
+
type TransferProgress,
|
|
18
|
+
type TransferStreamLine,
|
|
19
|
+
type TunnelInfo,
|
|
20
|
+
} from '../protocol.ts'
|
|
21
|
+
import { tt } from './panel/helpers.ts'
|
|
22
|
+
|
|
23
|
+
/** Minimal File System Access API surface (not in all lib.dom versions). */
|
|
24
|
+
interface WindowWithFileSystemAccess {
|
|
25
|
+
showSaveFilePicker?: (options: { suggestedName?: string }) => Promise<{
|
|
26
|
+
createWritable: () => Promise<{ write: (data: Uint8Array) => Promise<void>; close: () => Promise<void> }>
|
|
27
|
+
}>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Error carrying the route's JSON error message. */
|
|
31
|
+
export class SshApiError extends Error {
|
|
32
|
+
constructor(message: string) {
|
|
33
|
+
super(message)
|
|
34
|
+
this.name = 'SshApiError'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Extract the `.error` message from a JSON error body; undefined when absent. */
|
|
39
|
+
function extractError(text: string): string | undefined {
|
|
40
|
+
if (text === '') return undefined
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(text) as { error?: unknown } | null
|
|
43
|
+
return typeof parsed?.error === 'string' ? parsed.error : undefined
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Parse a JSON response or throw an SshApiError. */
|
|
50
|
+
async function readJson<T>(response: Response): Promise<T> {
|
|
51
|
+
let body: unknown
|
|
52
|
+
try {
|
|
53
|
+
body = await response.json()
|
|
54
|
+
} catch {
|
|
55
|
+
if (response.status === 404) {
|
|
56
|
+
throw new SshApiError(tt('error.disabled'))
|
|
57
|
+
}
|
|
58
|
+
throw new SshApiError(`HTTP ${response.status}: invalid JSON response`)
|
|
59
|
+
}
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
const message = typeof body === 'object' && body !== null && typeof (body as { error?: unknown }).error === 'string'
|
|
62
|
+
? (body as { error: string }).error
|
|
63
|
+
: (response.status === 404 ? tt('error.disabled') : `HTTP ${response.status}`)
|
|
64
|
+
throw new SshApiError(message)
|
|
65
|
+
}
|
|
66
|
+
return body as T
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Query-string helper. */
|
|
70
|
+
function query(params: Record<string, string | number | undefined>): string {
|
|
71
|
+
const search = new URLSearchParams()
|
|
72
|
+
for (const [key, value] of Object.entries(params)) {
|
|
73
|
+
if (value !== undefined && value !== '') search.set(key, String(value))
|
|
74
|
+
}
|
|
75
|
+
const text = search.toString()
|
|
76
|
+
return text === '' ? '' : '?' + text
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The location facts a terminal socket URL is built from. */
|
|
80
|
+
interface PageLocation {
|
|
81
|
+
/** Page scheme with its colon (for example `https:` or `dsh-app:`). */
|
|
82
|
+
protocol: string
|
|
83
|
+
/** Page authority, empty for a scheme that carries none. */
|
|
84
|
+
host: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The WebSocket URL the terminal route is reached at, or undefined when this
|
|
89
|
+
* page cannot carry one.
|
|
90
|
+
*
|
|
91
|
+
* A web page resolves the socket against its own origin, exactly as before.
|
|
92
|
+
*
|
|
93
|
+
* A page delivered by an application on this machine (the official DSH Desktop
|
|
94
|
+
* shell serves its Web GUI from `dsh-app://app/`) cannot carry a socket on its
|
|
95
|
+
* own scheme: the protocol handler forwards HTTP to the local Host but has no
|
|
96
|
+
* upgrade to forward. That is exactly the case the official transport hook
|
|
97
|
+
* covers - the shell publishes `__DSH_TRANSPORT__.streamBaseUrl`, the loopback
|
|
98
|
+
* authority of the Host it owns and already forwards every other request to
|
|
99
|
+
* (issue #1744). The socket is therefore dialed there instead, with the `ws`/
|
|
100
|
+
* `wss` scheme derived from that base, which is what the shell's
|
|
101
|
+
* `onBeforeSendHeaders` hook expects to see before it attaches its
|
|
102
|
+
* authority-bound credential.
|
|
103
|
+
*
|
|
104
|
+
* The base is used only on an application-delivered page. A web page that
|
|
105
|
+
* somehow carries the hook (the remote channel grants `ownsHost` to a paired
|
|
106
|
+
* LAN page, and that page is fenced behind a pairing channel) keeps resolving
|
|
107
|
+
* against its own origin, so this cannot reroute gated traffic onto a host the
|
|
108
|
+
* page is not entitled to reach. A blank authority is treated the same as an
|
|
109
|
+
* application scheme with no base: there is nothing to dial, and the caller
|
|
110
|
+
* gets the actionable reason instead of a socket that can only fail.
|
|
111
|
+
*
|
|
112
|
+
* @param location - the page location to read.
|
|
113
|
+
* @param search - the query string carrying `alias` or `session`.
|
|
114
|
+
* @param streamBaseUrl - `__DSH_TRANSPORT__.streamBaseUrl`, when the shell published one.
|
|
115
|
+
* @returns the absolute `ws:`/`wss:` URL, or undefined when the page cannot carry one.
|
|
116
|
+
*/
|
|
117
|
+
export function terminalSocketUrl(
|
|
118
|
+
location: PageLocation,
|
|
119
|
+
search: string,
|
|
120
|
+
streamBaseUrl?: string | undefined,
|
|
121
|
+
): string | undefined {
|
|
122
|
+
if (WEB_PAGE_PROTOCOLS.includes(location.protocol)) {
|
|
123
|
+
const scheme = location.protocol === 'https:' ? 'wss' : 'ws'
|
|
124
|
+
return scheme + '://' + location.host + SSH_API.terminal + search
|
|
125
|
+
}
|
|
126
|
+
// Application-delivered page: the shell-owned Host authority is the only
|
|
127
|
+
// origin the socket can reach. An unparsable or non-network base is ignored
|
|
128
|
+
// rather than guessed at, so the caller reports the actionable reason.
|
|
129
|
+
const base = hostAuthorityOf(streamBaseUrl)
|
|
130
|
+
if (base === undefined) return undefined
|
|
131
|
+
const scheme = base.protocol === 'https:' ? 'wss' : 'ws'
|
|
132
|
+
return scheme + '://' + base.authority + SSH_API.terminal + search
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The authority and scheme of a shell-owned Host base, or undefined when the
|
|
137
|
+
* value is absent, unparsable, or not a network origin this Host can serve
|
|
138
|
+
* (an application scheme could only resolve back to the page that has no
|
|
139
|
+
* socket transport in the first place).
|
|
140
|
+
* @param base - the raw `streamBaseUrl` as published by the transport hook.
|
|
141
|
+
*/
|
|
142
|
+
function hostAuthorityOf(base: string | undefined): { protocol: string; authority: string } | undefined {
|
|
143
|
+
if (base === undefined || base === '') return undefined
|
|
144
|
+
let url: URL
|
|
145
|
+
try {
|
|
146
|
+
url = new URL(base)
|
|
147
|
+
} catch {
|
|
148
|
+
return undefined
|
|
149
|
+
}
|
|
150
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return undefined
|
|
151
|
+
// A base carrying userinfo or credentials would put them on the wire.
|
|
152
|
+
if (url.username !== '' || url.password !== '') return undefined
|
|
153
|
+
if (url.host === '') return undefined
|
|
154
|
+
return { protocol: url.protocol, authority: url.host }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The shell-owned Host authority the page is told to reach, read from the
|
|
159
|
+
* official transport hook. The global is written by the shell before any boot
|
|
160
|
+
* entry runs, so it is absent on every page it does not own.
|
|
161
|
+
* @param globals - the global object to read (defaults to the real one).
|
|
162
|
+
* @returns the published base URL, or undefined when the page carries none.
|
|
163
|
+
*/
|
|
164
|
+
function transportStreamBaseUrl(
|
|
165
|
+
globals: { __DSH_TRANSPORT__?: { streamBaseUrl?: unknown } } = globalThis as never,
|
|
166
|
+
): string | undefined {
|
|
167
|
+
const published = (globals as { __DSH_TRANSPORT__?: { streamBaseUrl?: unknown } }).__DSH_TRANSPORT__?.streamBaseUrl
|
|
168
|
+
return typeof published === 'string' ? published : undefined
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Schemes a network page can be delivered with. Every other scheme belongs to
|
|
173
|
+
* an application on this machine, which is the same classification the remote
|
|
174
|
+
* channel and the update seat use (`isWebPageProtocol` / `isApplicationDeliveredPage`).
|
|
175
|
+
*/
|
|
176
|
+
const WEB_PAGE_PROTOCOLS: readonly string[] = [
|
|
177
|
+
'http:',
|
|
178
|
+
'https:',
|
|
179
|
+
'blob:',
|
|
180
|
+
'data:',
|
|
181
|
+
'about:',
|
|
182
|
+
'filesystem:',
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A terminal connection that never opened: it reports one failure once the
|
|
187
|
+
* view has attached its `onExit`, so the tab shows why instead of sitting on a
|
|
188
|
+
* spinner, and every later frame is a no-op.
|
|
189
|
+
*/
|
|
190
|
+
function failedTerminal(reason: string): TerminalConnection {
|
|
191
|
+
const connection: TerminalConnection = {
|
|
192
|
+
onReady: undefined,
|
|
193
|
+
onOutput: undefined,
|
|
194
|
+
onExit: undefined,
|
|
195
|
+
onAuthPrompt: undefined,
|
|
196
|
+
send: () => undefined,
|
|
197
|
+
resize: () => undefined,
|
|
198
|
+
sendAuthResponse: () => undefined,
|
|
199
|
+
detach: () => undefined,
|
|
200
|
+
close: () => undefined,
|
|
201
|
+
}
|
|
202
|
+
// The view binds onExit after the open() call returns, so the failure is
|
|
203
|
+
// delivered on the next microtask - never synchronously, which would race
|
|
204
|
+
// the handler the caller is about to assign.
|
|
205
|
+
queueMicrotask(() => { connection.onExit?.(null, reason) })
|
|
206
|
+
return connection
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** One open terminal connection (WebSocket JSON frames). */
|
|
210
|
+
export interface TerminalConnection {
|
|
211
|
+
/** Fired on the ready frame (shell is up); carries the host session id and alias. */
|
|
212
|
+
onReady: ((sessionId: string, alias: string) => void) | undefined
|
|
213
|
+
/** Fired on every output frame. */
|
|
214
|
+
onOutput: ((data: string) => void) | undefined
|
|
215
|
+
/** Fired on the exit frame (or transport error). */
|
|
216
|
+
onExit: ((code: number | null, error?: string) => void) | undefined
|
|
217
|
+
/** Fired when the server requests keyboard-interactive 2FA. */
|
|
218
|
+
onAuthPrompt: ((name: string, instructions: string, prompts: Array<{ prompt: string; echo: boolean }>) => void) | undefined
|
|
219
|
+
/** Send raw input to the remote shell. */
|
|
220
|
+
send(data: string): void
|
|
221
|
+
/** Resize the remote PTY. */
|
|
222
|
+
resize(cols: number, rows: number): void
|
|
223
|
+
/** Send interactive 2FA response codes to the remote server. */
|
|
224
|
+
sendAuthResponse(responses: string[]): void
|
|
225
|
+
/**
|
|
226
|
+
* Leave the socket while the host keeps the remote shell alive, so a later
|
|
227
|
+
* {@link SshApi.attachTerminal} picks the session up where it left off.
|
|
228
|
+
* This is what a view does on unmount; it is not a disconnect.
|
|
229
|
+
*/
|
|
230
|
+
detach(): void
|
|
231
|
+
/** Close the socket and end the remote session. */
|
|
232
|
+
close(): void
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The browser half's only data entry point. */
|
|
236
|
+
export class SshApi {
|
|
237
|
+
// -------------------------------------------------------------- hosts
|
|
238
|
+
async listHosts(queryText?: string): Promise<SshHostSummary[]> {
|
|
239
|
+
const response = await fetch(SSH_API.hosts + query({ query: queryText }))
|
|
240
|
+
const body = await readJson<{ hosts: SshHostSummary[] }>(response)
|
|
241
|
+
return body.hosts
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async createHost(payload: HostPayload): Promise<SshHostSummary> {
|
|
245
|
+
const response = await fetch(SSH_API.hosts, {
|
|
246
|
+
method: 'POST',
|
|
247
|
+
headers: { 'content-type': 'application/json' },
|
|
248
|
+
body: JSON.stringify(payload),
|
|
249
|
+
})
|
|
250
|
+
const body = await readJson<{ host: SshHostSummary }>(response)
|
|
251
|
+
return body.host
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async updateHost(alias: string, patch: HostPayload): Promise<SshHostSummary> {
|
|
255
|
+
const response = await fetch(SSH_API.hosts + query({ alias }), {
|
|
256
|
+
method: 'PATCH',
|
|
257
|
+
headers: { 'content-type': 'application/json' },
|
|
258
|
+
body: JSON.stringify(patch),
|
|
259
|
+
})
|
|
260
|
+
const body = await readJson<{ host: SshHostSummary }>(response)
|
|
261
|
+
return body.host
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async deleteHost(alias: string): Promise<void> {
|
|
265
|
+
const response = await fetch(SSH_API.hosts + query({ alias }), { method: 'DELETE' })
|
|
266
|
+
await readJson<{ ok: boolean }>(response)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async importSshConfig(): Promise<ImportResult> {
|
|
270
|
+
const response = await fetch(SSH_API.importSshConfig, { method: 'POST' })
|
|
271
|
+
const body = await readJson<{ result: ImportResult }>(response)
|
|
272
|
+
return body.result
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ---------------------------------------------------------------- ops
|
|
276
|
+
async testHost(alias: string): Promise<TestResult> {
|
|
277
|
+
const response = await fetch(SSH_API.test, {
|
|
278
|
+
method: 'POST',
|
|
279
|
+
headers: { 'content-type': 'application/json' },
|
|
280
|
+
body: JSON.stringify({ alias }),
|
|
281
|
+
})
|
|
282
|
+
const body = await readJson<{ result: TestResult }>(response)
|
|
283
|
+
return body.result
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async exec(alias: string, command: string, timeoutMs?: number): Promise<ExecResult> {
|
|
287
|
+
const response = await fetch(SSH_API.exec, {
|
|
288
|
+
method: 'POST',
|
|
289
|
+
headers: { 'content-type': 'application/json' },
|
|
290
|
+
body: JSON.stringify({ alias, command, timeoutMs }),
|
|
291
|
+
})
|
|
292
|
+
const body = await readJson<{ result: ExecResult }>(response)
|
|
293
|
+
return body.result
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async cluster(options: {
|
|
297
|
+
command: string
|
|
298
|
+
aliases?: string[]
|
|
299
|
+
environment?: string
|
|
300
|
+
tags?: string[]
|
|
301
|
+
timeoutMs?: number
|
|
302
|
+
maxWorkers?: number
|
|
303
|
+
}): Promise<ClusterResult[]> {
|
|
304
|
+
const response = await fetch(SSH_API.cluster, {
|
|
305
|
+
method: 'POST',
|
|
306
|
+
headers: { 'content-type': 'application/json' },
|
|
307
|
+
body: JSON.stringify(options),
|
|
308
|
+
})
|
|
309
|
+
const body = await readJson<{ results: ClusterResult[] }>(response)
|
|
310
|
+
return body.results
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ----------------------------------------------------------------- ls
|
|
314
|
+
async ls(alias: string, path: string): Promise<RemoteDirEntry[]> {
|
|
315
|
+
const response = await fetch(SSH_API.ls + query({ alias, path }))
|
|
316
|
+
const body = await readJson<{ entries: RemoteDirEntry[] }>(response)
|
|
317
|
+
return body.entries
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ------------------------------------------------------------- tunnel
|
|
321
|
+
async listTunnels(): Promise<TunnelInfo[]> {
|
|
322
|
+
const response = await fetch(SSH_API.tunnel, {
|
|
323
|
+
method: 'POST',
|
|
324
|
+
headers: { 'content-type': 'application/json' },
|
|
325
|
+
body: JSON.stringify({ action: 'list' }),
|
|
326
|
+
})
|
|
327
|
+
const body = await readJson<{ tunnels: TunnelInfo[] }>(response)
|
|
328
|
+
return body.tunnels
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async startTunnel(options: { alias: string; remotePort: number; remoteHost?: string; localPort?: number }): Promise<TunnelInfo> {
|
|
332
|
+
const response = await fetch(SSH_API.tunnel, {
|
|
333
|
+
method: 'POST',
|
|
334
|
+
headers: { 'content-type': 'application/json' },
|
|
335
|
+
body: JSON.stringify({ action: 'start', ...options }),
|
|
336
|
+
})
|
|
337
|
+
const body = await readJson<{ tunnel: TunnelInfo }>(response)
|
|
338
|
+
return body.tunnel
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async stopTunnel(tunnelId: string): Promise<boolean> {
|
|
342
|
+
const response = await fetch(SSH_API.tunnel, {
|
|
343
|
+
method: 'POST',
|
|
344
|
+
headers: { 'content-type': 'application/json' },
|
|
345
|
+
body: JSON.stringify({ action: 'stop', tunnelId }),
|
|
346
|
+
})
|
|
347
|
+
const body = await readJson<{ ok: boolean }>(response)
|
|
348
|
+
return body.ok
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async stopAllTunnels(alias?: string): Promise<number> {
|
|
352
|
+
const response = await fetch(SSH_API.tunnel, {
|
|
353
|
+
method: 'POST',
|
|
354
|
+
headers: { 'content-type': 'application/json' },
|
|
355
|
+
body: JSON.stringify({ action: 'stop-all', alias }),
|
|
356
|
+
})
|
|
357
|
+
const body = await readJson<{ stopped: number }>(response)
|
|
358
|
+
return body.stopped
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ------------------------------------------------------------ transfer
|
|
362
|
+
/**
|
|
363
|
+
* Upload one file (raw bytes) to a remote path. Progress arrives through
|
|
364
|
+
* the NDJSON response stream; resolves when the result frame lands.
|
|
365
|
+
*/
|
|
366
|
+
async uploadFile(
|
|
367
|
+
file: File,
|
|
368
|
+
alias: string,
|
|
369
|
+
remotePath: string,
|
|
370
|
+
onProgress?: (progress: TransferProgress) => void,
|
|
371
|
+
): Promise<{ transferredBytes: number }> {
|
|
372
|
+
const response = await fetch(SSH_API.upload + query({ alias, remotePath }), {
|
|
373
|
+
method: 'POST',
|
|
374
|
+
body: file,
|
|
375
|
+
})
|
|
376
|
+
if (!response.ok || response.body === null) {
|
|
377
|
+
const text = await response.text().catch(() => '')
|
|
378
|
+
const error = extractError(text)
|
|
379
|
+
throw new SshApiError(error ?? `upload failed: HTTP ${response.status}`)
|
|
380
|
+
}
|
|
381
|
+
const reader = response.body.getReader()
|
|
382
|
+
const decoder = new TextDecoder()
|
|
383
|
+
let buffer = ''
|
|
384
|
+
let finalError: string | undefined
|
|
385
|
+
let sawResult = false
|
|
386
|
+
let transferredBytes = 0
|
|
387
|
+
for (;;) {
|
|
388
|
+
const { done, value } = await reader.read()
|
|
389
|
+
if (done) break
|
|
390
|
+
buffer += decoder.decode(value, { stream: true })
|
|
391
|
+
const lines = buffer.split('\n')
|
|
392
|
+
buffer = lines.pop() ?? ''
|
|
393
|
+
for (const line of lines) {
|
|
394
|
+
if (line.trim() === '') continue
|
|
395
|
+
let parsed: TransferStreamLine
|
|
396
|
+
try {
|
|
397
|
+
parsed = JSON.parse(line) as TransferStreamLine
|
|
398
|
+
} catch {
|
|
399
|
+
continue
|
|
400
|
+
}
|
|
401
|
+
if (parsed.type === 'progress') {
|
|
402
|
+
onProgress?.(parsed.progress)
|
|
403
|
+
} else if (parsed.type === 'result') {
|
|
404
|
+
sawResult = true
|
|
405
|
+
if (parsed.ok) transferredBytes = parsed.transferredBytes ?? 0
|
|
406
|
+
finalError = parsed.ok ? undefined : parsed.error ?? 'upload failed'
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (finalError !== undefined) throw new SshApiError(finalError)
|
|
411
|
+
if (!sawResult) throw new SshApiError('upload ended without a result frame — the transfer did not complete')
|
|
412
|
+
return { transferredBytes }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Download a remote file with client-side progress. Streams straight to
|
|
417
|
+
* disk when the File System Access API is available (no full-file RAM
|
|
418
|
+
* copy); otherwise falls back to an in-memory Blob.
|
|
419
|
+
*/
|
|
420
|
+
async downloadFile(
|
|
421
|
+
alias: string,
|
|
422
|
+
remotePath: string,
|
|
423
|
+
onProgress?: (progress: TransferProgress) => void,
|
|
424
|
+
): Promise<{ blob?: Blob; filename: string; streamed: boolean; bytes: number }> {
|
|
425
|
+
const response = await fetch(SSH_API.download + query({ alias, remotePath }))
|
|
426
|
+
if (!response.ok || response.body === null) {
|
|
427
|
+
const text = await response.text().catch(() => '')
|
|
428
|
+
const error = extractError(text)
|
|
429
|
+
throw new SshApiError(error ?? `download failed: HTTP ${response.status}`)
|
|
430
|
+
}
|
|
431
|
+
const total = Number(response.headers.get('content-length') ?? '0')
|
|
432
|
+
const disposition = response.headers.get('content-disposition') ?? ''
|
|
433
|
+
const match = /filename="([^"]+)"/.exec(disposition)
|
|
434
|
+
const filename = match?.[1] ?? remotePath.split('/').pop() ?? 'download'
|
|
435
|
+
const reader = response.body.getReader()
|
|
436
|
+
const picker = typeof window !== 'undefined'
|
|
437
|
+
? (window as WindowWithFileSystemAccess).showSaveFilePicker
|
|
438
|
+
: undefined
|
|
439
|
+
let streamed = false
|
|
440
|
+
let writable: { write: (data: Uint8Array) => Promise<void>; close: () => Promise<void> } | undefined
|
|
441
|
+
const chunks: Uint8Array<ArrayBuffer>[] = []
|
|
442
|
+
let received = 0
|
|
443
|
+
const progress = (): void => {
|
|
444
|
+
onProgress?.({
|
|
445
|
+
phase: 'transferring',
|
|
446
|
+
file: remotePath,
|
|
447
|
+
transferred: received,
|
|
448
|
+
total,
|
|
449
|
+
percent: total > 0 ? Math.round((received / total) * 1000) / 10 : 0,
|
|
450
|
+
})
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
if (picker !== undefined) {
|
|
454
|
+
const handle = await picker.call(window, { suggestedName: filename })
|
|
455
|
+
writable = await handle.createWritable()
|
|
456
|
+
streamed = true
|
|
457
|
+
}
|
|
458
|
+
} catch {
|
|
459
|
+
// User cancelled the save dialog or the API is unavailable: fall back.
|
|
460
|
+
}
|
|
461
|
+
for (;;) {
|
|
462
|
+
const { done, value } = await reader.read()
|
|
463
|
+
if (done) break
|
|
464
|
+
if (writable !== undefined) {
|
|
465
|
+
await writable.write(value as Uint8Array)
|
|
466
|
+
} else {
|
|
467
|
+
chunks.push(value as Uint8Array<ArrayBuffer>)
|
|
468
|
+
}
|
|
469
|
+
received += value.length
|
|
470
|
+
progress()
|
|
471
|
+
}
|
|
472
|
+
if (writable !== undefined) await writable.close()
|
|
473
|
+
onProgress?.({ phase: 'done', file: remotePath, transferred: received, total: received > 0 ? received : total, percent: 100 })
|
|
474
|
+
return {
|
|
475
|
+
blob: streamed ? undefined : new Blob(chunks),
|
|
476
|
+
filename,
|
|
477
|
+
streamed,
|
|
478
|
+
bytes: received,
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// ------------------------------------------------------------ terminal
|
|
483
|
+
/**
|
|
484
|
+
* Open a new host terminal session for the alias.
|
|
485
|
+
* @param alias - the configured host alias.
|
|
486
|
+
* @param cols - initial PTY width.
|
|
487
|
+
* @param rows - initial PTY height.
|
|
488
|
+
* @returns the live connection; its ready callback carries the session id
|
|
489
|
+
* {@link attachTerminal} uses after a view detach.
|
|
490
|
+
*/
|
|
491
|
+
openTerminal(alias: string, cols: number, rows: number): TerminalConnection {
|
|
492
|
+
return this.terminalSocket(query({ alias, cols, rows }))
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Reattach to a host-side session that outlived its view (a panel switch, a
|
|
497
|
+
* page navigation). The host replays its scrollback before the ready frame.
|
|
498
|
+
* @param sessionId - the id the previous connection reported on ready.
|
|
499
|
+
* @param cols - the view's current width.
|
|
500
|
+
* @param rows - the view's current height.
|
|
501
|
+
*/
|
|
502
|
+
attachTerminal(sessionId: string, cols: number, rows: number): TerminalConnection {
|
|
503
|
+
return this.terminalSocket(query({ session: sessionId, cols, rows }))
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** One terminal socket over either an alias (open) or a session id (attach). */
|
|
507
|
+
private terminalSocket(search: string): TerminalConnection {
|
|
508
|
+
const target = terminalSocketUrl(window.location, search, transportStreamBaseUrl())
|
|
509
|
+
if (target === undefined) {
|
|
510
|
+
// This page has no WebSocket transport at all: an application-delivered
|
|
511
|
+
// page whose shell published no Host authority to dial. Say so plainly
|
|
512
|
+
// instead of opening a socket that can only report "connection error".
|
|
513
|
+
return failedTerminal(tt('terminal.noWebSocket'))
|
|
514
|
+
}
|
|
515
|
+
let socket: WebSocket
|
|
516
|
+
try {
|
|
517
|
+
socket = new WebSocket(target)
|
|
518
|
+
} catch (error) {
|
|
519
|
+
return failedTerminal(error instanceof Error ? error.message : String(error))
|
|
520
|
+
}
|
|
521
|
+
// A deliberate detach/close must not surface as a transport error: the
|
|
522
|
+
// view tears down silently, and only an UNEXPECTED close reports exit.
|
|
523
|
+
let leaving = false
|
|
524
|
+
const sendFrame = (frame: TerminalClientFrame): void => {
|
|
525
|
+
if (socket.readyState === WebSocket.OPEN) socket.send(JSON.stringify(frame))
|
|
526
|
+
}
|
|
527
|
+
const connection: TerminalConnection = {
|
|
528
|
+
onReady: undefined,
|
|
529
|
+
onOutput: undefined,
|
|
530
|
+
onExit: undefined,
|
|
531
|
+
onAuthPrompt: undefined,
|
|
532
|
+
send: (data) => { sendFrame({ type: 'input', data }) },
|
|
533
|
+
resize: (cols, rows) => { sendFrame({ type: 'resize', cols, rows }) },
|
|
534
|
+
sendAuthResponse: (responses) => { sendFrame({ type: 'auth_response', responses }) },
|
|
535
|
+
detach: () => {
|
|
536
|
+
leaving = true
|
|
537
|
+
sendFrame({ type: 'detach' })
|
|
538
|
+
try { socket.close() } catch { /* already closed */ }
|
|
539
|
+
},
|
|
540
|
+
close: () => {
|
|
541
|
+
leaving = true
|
|
542
|
+
sendFrame({ type: 'close' })
|
|
543
|
+
try { socket.close() } catch { /* already closed */ }
|
|
544
|
+
},
|
|
545
|
+
}
|
|
546
|
+
socket.onmessage = (event: MessageEvent<string>) => {
|
|
547
|
+
let frame: TerminalServerFrame
|
|
548
|
+
try {
|
|
549
|
+
frame = JSON.parse(event.data) as TerminalServerFrame
|
|
550
|
+
} catch {
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
if (frame.type === 'ready') connection.onReady?.(frame.sessionId, frame.alias)
|
|
554
|
+
else if (frame.type === 'output') connection.onOutput?.(frame.data)
|
|
555
|
+
else if (frame.type === 'exit') connection.onExit?.(frame.code, frame.error)
|
|
556
|
+
else if (frame.type === 'auth_prompt') connection.onAuthPrompt?.(frame.name, frame.instructions, frame.prompts)
|
|
557
|
+
}
|
|
558
|
+
socket.onclose = () => { if (!leaving) connection.onExit?.(null, 'connection closed') }
|
|
559
|
+
socket.onerror = () => { if (!leaving) connection.onExit?.(null, 'connection error') }
|
|
560
|
+
return connection
|
|
561
|
+
}
|
|
562
|
+
}
|