@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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared panel helpers: the active-dictionary pick (document-language based,
|
|
3
|
+
* task-board precedent) bound to the dsh-ssh interpolator in locales.ts, plus
|
|
4
|
+
* a small error-message extractor. All copy stays in the locale dictionaries.
|
|
5
|
+
*/
|
|
6
|
+
import { type SshKey } from '../locales.ts';
|
|
7
|
+
/** Template values accepted by the interpolator. */
|
|
8
|
+
export type TranslateValues = Record<string, string | number>;
|
|
9
|
+
/** Active dictionary, picked by the document language at call time. */
|
|
10
|
+
export declare function dictionary(): Record<string, string>;
|
|
11
|
+
/** Wire the SDK translate seat; pass undefined to restore the document-language pick. */
|
|
12
|
+
export declare function setRuntimeTranslate(t: ((key: SshKey, values?: TranslateValues) => string) | undefined): void;
|
|
13
|
+
/** Translate a key with optional {name} template params (current language). */
|
|
14
|
+
export declare function tt(key: SshKey, values?: TranslateValues): string;
|
|
15
|
+
/** Human-readable error text from an unknown thrown value. */
|
|
16
|
+
export declare function errorMessage(error: unknown): string;
|
|
17
|
+
/**
|
|
18
|
+
* The terminal font stack used before issue #577, kept as the last resort
|
|
19
|
+
* when neither the plugin setting nor a CSS custom property names a font.
|
|
20
|
+
*/
|
|
21
|
+
export declare const TERMINAL_FONT_FALLBACK = "Menlo, Consolas, \"Liberation Mono\", monospace";
|
|
22
|
+
/**
|
|
23
|
+
* Live source of the user's configured terminal font family: the client
|
|
24
|
+
* entry binds the dsh-ssh settings namespace into one of these and hands it
|
|
25
|
+
* to the panel, so a settings change re-applies without a remount.
|
|
26
|
+
*/
|
|
27
|
+
export interface TerminalFontSource {
|
|
28
|
+
/** Current configured family, or undefined when unset / not yet loaded. */
|
|
29
|
+
get(): string | undefined;
|
|
30
|
+
/** Subscribe to changes; the returned function unsubscribes. */
|
|
31
|
+
subscribe(listener: () => void): () => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolve the xterm `fontFamily` (issue #577). xterm's DOM renderer takes
|
|
35
|
+
* the font only from constructor/options, so a plain stylesheet rule cannot
|
|
36
|
+
* retarget it — the value must be read back into the options. The chain,
|
|
37
|
+
* first non-empty value wins:
|
|
38
|
+
* 1. `override` — the `terminalFontFamily` plugin setting;
|
|
39
|
+
* 2. `--dsh-ssh-terminal-font` — dedicated hook for skins and user CSS
|
|
40
|
+
* (e.g. a Nerd Font for powerline glyphs);
|
|
41
|
+
* 3. `--ds-font-family-code` — the official code-font token several skins
|
|
42
|
+
* already remap;
|
|
43
|
+
* 4. {@link TERMINAL_FONT_FALLBACK}.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveTerminalFontFamily(override?: string): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settings reader of the dsh-ssh browser half.
|
|
3
|
+
*
|
|
4
|
+
* The plugin's browser half owns one preference: the xterm `fontFamily` the
|
|
5
|
+
* panel applies to its terminals. Under the 0.1.7 settings model that field
|
|
6
|
+
* belongs to the plugin's own profile entry (its Cordis Config schema), and
|
|
7
|
+
* the browser surface addresses settings as one form per profile entry id — so
|
|
8
|
+
* the entry has to be resolved before its section can be read:
|
|
9
|
+
*
|
|
10
|
+
* - the family binder (`ctx.get('webUiSettings')`, published while
|
|
11
|
+
* dsh-web-settings is loaded) resolves the dsh-ssh namespace to its entry id
|
|
12
|
+
* through the Host bridge and hands back the native shared form;
|
|
13
|
+
* - without that group the shared configuration forms serve the form directly,
|
|
14
|
+
* and the entry is recognised by the one field this plugin's own schema
|
|
15
|
+
* declares: the entry whose resolved section carries `terminalFontFamily`.
|
|
16
|
+
*/
|
|
17
|
+
import type { Context as ClientContext } from '@deepseek-ai/cordis';
|
|
18
|
+
import type { ConfigForm, ConfigFormSnapshot } from '@deepseek-ai/dsh-client-ui-settings/client';
|
|
19
|
+
/** Domain-owned description of one settings namespace a family surface binds. */
|
|
20
|
+
export interface ConfigFormSpec<T> {
|
|
21
|
+
/** Settings namespace the surface edits. */
|
|
22
|
+
namespace: string;
|
|
23
|
+
/**
|
|
24
|
+
* Narrow one wire section; undefined keeps the section the Host resolved.
|
|
25
|
+
* The shared form already validates against the namespace's serialized
|
|
26
|
+
* schema, so a decoder exists only to narrow beyond it.
|
|
27
|
+
*/
|
|
28
|
+
decode?: (section: unknown) => T | undefined;
|
|
29
|
+
}
|
|
30
|
+
declare module '@deepseek-ai/cordis' {
|
|
31
|
+
interface Context {
|
|
32
|
+
/**
|
|
33
|
+
* Optional family settings binder provided by dsh-web-settings; absent when
|
|
34
|
+
* that group plugin is not installed, so callers fall back to the shared
|
|
35
|
+
* configuration forms service (`ctx.configForms`).
|
|
36
|
+
*/
|
|
37
|
+
webUiSettings?: {
|
|
38
|
+
bind<S>(spec: ConfigFormSpec<S>): ConfigForm<S>;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The read face this plugin needs from its own settings entry. */
|
|
43
|
+
export interface SettingsReader<T> {
|
|
44
|
+
/** @returns the current snapshot (stable reference until the next change). */
|
|
45
|
+
getSnapshot(): ConfigFormSnapshot<T>;
|
|
46
|
+
/** Observe snapshot replacements. */
|
|
47
|
+
subscribe(listener: () => void): () => void;
|
|
48
|
+
}
|
|
49
|
+
/** A reader plus the release of every subscription it installed. */
|
|
50
|
+
export interface SettingsBinding<T> extends SettingsReader<T> {
|
|
51
|
+
/** Release the reader's subscriptions; later calls are no-ops. */
|
|
52
|
+
dispose(): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Bind this plugin's settings reader.
|
|
56
|
+
* @param ctx - client context carrying the family binder and/or the shared forms service.
|
|
57
|
+
* @param namespace - the family settings namespace this plugin owns.
|
|
58
|
+
* @param field - the field this plugin's own schema declares, used to recognise its entry when the family binder is absent.
|
|
59
|
+
* @returns the reader and the release of its subscriptions.
|
|
60
|
+
*/
|
|
61
|
+
export declare function bindSettingsReader<T>(ctx: ClientContext, namespace: string, field: string): SettingsBinding<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
3
|
+
* environment override wins, the platform home fallback follows. Mirrors
|
|
4
|
+
* what dsh-pet and dsh-liangshen each used to implement locally.
|
|
5
|
+
*/
|
|
6
|
+
/** Expand a leading ~ (or ~user) in a path, platform-style. */
|
|
7
|
+
export declare function expandHome(path: string, home?: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the DSH home directory.
|
|
10
|
+
* @param env - process environment to read DSH_HOME from.
|
|
11
|
+
* @param home - platform home directory fallback (test seam).
|
|
12
|
+
* @returns the absolute DSH home path.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveDshHome(env?: NodeJS.ProcessEnv, home?: string): string;
|
|
15
|
+
/** Resolve the DSH home directory from the live environment. */
|
|
16
|
+
export declare function dshHome(): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cluster execution: run one command concurrently across selected hosts; the
|
|
3
|
+
* caller must provide at least one non-empty aliases / environment / tags filter.
|
|
4
|
+
*/
|
|
5
|
+
import type { ClusterResult } from '../protocol.ts';
|
|
6
|
+
import { type PoolEngine } from './connection-pool.ts';
|
|
7
|
+
interface ClusterOptions {
|
|
8
|
+
command: string;
|
|
9
|
+
aliases?: string[];
|
|
10
|
+
environment?: string;
|
|
11
|
+
tags?: string[];
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
maxWorkers?: number;
|
|
14
|
+
/** Caller cancellation, forwarded to every host's command. */
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
/** Run one command against many hosts concurrently. */
|
|
18
|
+
export declare function cluster(engine: PoolEngine, options: ClusterOptions): Promise<ClusterResult[]>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection pool: per-alias persistent ssh2 connections with multi-hop jump
|
|
3
|
+
* support, the acquire / dispose / sweep lifecycle, and the pooled exec path.
|
|
4
|
+
*/
|
|
5
|
+
import { Client, type ConnectConfig } from 'ssh2';
|
|
6
|
+
import type { ExecResult, SshHostEntry } from '../protocol.ts';
|
|
7
|
+
import { type HostStore } from '../store.ts';
|
|
8
|
+
/** Default engine knobs. */
|
|
9
|
+
export interface EngineOptions {
|
|
10
|
+
/** Connections idle longer than this are closed (ms). */
|
|
11
|
+
idleTimeoutMs?: number;
|
|
12
|
+
/** SSH handshake timeout (ms). */
|
|
13
|
+
connectTimeoutMs?: number;
|
|
14
|
+
/** Keepalive ping interval (ms). */
|
|
15
|
+
keepaliveIntervalMs?: number;
|
|
16
|
+
/** Cap on captured stdout/stderr bytes per exec (ms). */
|
|
17
|
+
maxOutputBytes?: number;
|
|
18
|
+
/** Default exec timeout (ms). */
|
|
19
|
+
defaultExecTimeoutMs?: number;
|
|
20
|
+
/** Default cluster concurrency. */
|
|
21
|
+
defaultMaxWorkers?: number;
|
|
22
|
+
/** SFTP concurrent channel count for transfers. */
|
|
23
|
+
sftpConcurrency?: number;
|
|
24
|
+
}
|
|
25
|
+
/** Default engine knobs (applied when an option is omitted). */
|
|
26
|
+
export declare const DEFAULTS: Required<EngineOptions>;
|
|
27
|
+
/** One pooled connection record. */
|
|
28
|
+
export interface PoolRecord {
|
|
29
|
+
client: Client;
|
|
30
|
+
/** Jump-chain clients kept alive under the target. */
|
|
31
|
+
hops: Client[];
|
|
32
|
+
idleAt: number;
|
|
33
|
+
/** Pinned connections (tunnels) are never swept. */
|
|
34
|
+
pinned: boolean;
|
|
35
|
+
broken: boolean;
|
|
36
|
+
/** Operations currently running on this connection (sweep guard). */
|
|
37
|
+
inFlight: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The slice of the engine the pool and exec paths need. The host class
|
|
41
|
+
* (engine.ts facade) satisfies this structurally.
|
|
42
|
+
*/
|
|
43
|
+
export interface PoolEngine {
|
|
44
|
+
readonly store: HostStore;
|
|
45
|
+
readonly opts: Required<EngineOptions>;
|
|
46
|
+
readonly pool: Map<string, PoolRecord>;
|
|
47
|
+
readonly acquireQueue: Map<string, Promise<PoolRecord>>;
|
|
48
|
+
}
|
|
49
|
+
/** Build the ssh2 connect config for one entry (key read from disk). */
|
|
50
|
+
export declare function buildConnectConfig(entry: SshHostEntry, sock: ConnectConfig['sock'] | undefined, opts: Required<EngineOptions>): ConnectConfig;
|
|
51
|
+
/** Resolve the ssh2 agent path for 'agent' auth. */
|
|
52
|
+
export declare function resolveAgentPath(agentPath?: string): string | undefined;
|
|
53
|
+
/** Keyboard-interactive handler callback for 2FA / dynamic prompt flow. */
|
|
54
|
+
export type KeyboardInteractiveHandler = (name: string, instructions: string, instructionsLang: string, prompts: Array<{
|
|
55
|
+
prompt: string;
|
|
56
|
+
echo: boolean;
|
|
57
|
+
}>, finish: (responses: string[]) => void) => void;
|
|
58
|
+
/** Connect one ssh2 client (resolve on ready, reject on error/close). */
|
|
59
|
+
export declare function connectClient(config: ConnectConfig, onKeyboardInteractive?: KeyboardInteractiveHandler): Promise<Client>;
|
|
60
|
+
/** Cap captured output at the configured byte budget (marks truncation). */
|
|
61
|
+
export declare function appendOutput(target: {
|
|
62
|
+
text: string;
|
|
63
|
+
truncated: boolean;
|
|
64
|
+
}, chunk: Buffer, maxBytes: number): void;
|
|
65
|
+
/**
|
|
66
|
+
* Parse an OpenSSH jump hop written as an address: `[user@]host[:port]`
|
|
67
|
+
* (an IPv6 literal must be bracketed). Returns undefined when the value is not
|
|
68
|
+
* an address at all.
|
|
69
|
+
*/
|
|
70
|
+
export declare function parseJumpSpec(spec: string): {
|
|
71
|
+
host: string;
|
|
72
|
+
port?: number;
|
|
73
|
+
user?: string;
|
|
74
|
+
} | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Resolve one ProxyJump spec: an alias configured in this plugin wins, and any
|
|
77
|
+
* other value is read as an OpenSSH address whose credentials come from the
|
|
78
|
+
* target entry (an ad-hoc hop has no stored auth of its own).
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveHop(engine: PoolEngine, entry: SshHostEntry, spec: string): SshHostEntry;
|
|
81
|
+
/**
|
|
82
|
+
* Build one full jump chain for an entry: hop clients connected through in
|
|
83
|
+
* order, each forwarding a stream to the next destination, ending with the
|
|
84
|
+
* target client. Shared by the pool and standalone shell sessions.
|
|
85
|
+
*
|
|
86
|
+
* A ProxyCommand is the transport that reaches its own host, so the entry's
|
|
87
|
+
* own command seeds the chain when there is no jump chain, and the first hop's
|
|
88
|
+
* command is used when the hop carries one.
|
|
89
|
+
*/
|
|
90
|
+
export declare function connectChain(engine: PoolEngine, entry: SshHostEntry, onKeyboardInteractive?: KeyboardInteractiveHandler): Promise<{
|
|
91
|
+
client: Client;
|
|
92
|
+
hops: Client[];
|
|
93
|
+
}>;
|
|
94
|
+
/** Connect (or reuse) the pooled chain for one alias; pins nothing. */
|
|
95
|
+
export declare function acquire(engine: PoolEngine, alias: string): Promise<PoolRecord>;
|
|
96
|
+
/**
|
|
97
|
+
* Tear down one alias's record. When `record` is given and no longer the
|
|
98
|
+
* pooled record for the alias (a concurrent acquire replaced it), nothing
|
|
99
|
+
* is torn down — the connection belongs to someone else now.
|
|
100
|
+
*/
|
|
101
|
+
export declare function disposeRecord(engine: PoolEngine, alias: string, record?: PoolRecord): void;
|
|
102
|
+
/** End one record's client and hop chain (best-effort, safe to repeat). */
|
|
103
|
+
export declare function endRecordChain(record: PoolRecord): void;
|
|
104
|
+
/** Close connections idle beyond the threshold (skips pinned and in-flight). */
|
|
105
|
+
export declare function sweepPool(engine: PoolEngine): void;
|
|
106
|
+
/**
|
|
107
|
+
* Run `fn` with a live client for `alias`, reconnecting (up to the
|
|
108
|
+
* attempt budget) when the connection broke mid-flight.
|
|
109
|
+
*/
|
|
110
|
+
export declare function withClient<T>(engine: PoolEngine, alias: string, fn: (client: Client) => Promise<T>, attempts?: number, signal?: AbortSignal): Promise<T>;
|
|
111
|
+
/** Run one command on `alias` (reusing the pooled connection). */
|
|
112
|
+
export declare function execCommand(engine: PoolEngine, alias: string, command: string, timeoutMs?: number, signal?: AbortSignal): Promise<ExecResult>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenSSH-compatible ProxyCommand transport: run the command with the user's
|
|
3
|
+
* shell and expose its stdio as a Duplex that ssh2 accepts as `sock`.
|
|
4
|
+
*
|
|
5
|
+
* The command comes verbatim from the user's own 0600 store file and runs with
|
|
6
|
+
* the DSH host process's privileges — the same trust the `ssh(1)` client gives
|
|
7
|
+
* the identical line in `~/.ssh/config`.
|
|
8
|
+
*/
|
|
9
|
+
import { type ChildProcess } from 'node:child_process';
|
|
10
|
+
import { Duplex } from 'node:stream';
|
|
11
|
+
/** Token source for one host: the values %h / %p / %r / %n expand to. */
|
|
12
|
+
export interface ProxyTarget {
|
|
13
|
+
host: string;
|
|
14
|
+
port: number;
|
|
15
|
+
user: string;
|
|
16
|
+
/** The name the user addresses this host by (OpenSSH's %n). */
|
|
17
|
+
alias: string;
|
|
18
|
+
}
|
|
19
|
+
/** Expand the OpenSSH ProxyCommand tokens; unknown %X sequences stay verbatim. */
|
|
20
|
+
export declare function expandProxyTokens(command: string, target: ProxyTarget): string;
|
|
21
|
+
/** One running proxy command: its transport plus the process behind it. */
|
|
22
|
+
export interface ProxyCommandProcess {
|
|
23
|
+
/** The duplex transport handed to ssh2 as `sock`. */
|
|
24
|
+
stream: Duplex;
|
|
25
|
+
/** The spawned shell; killing the stream kills it (and its process group). */
|
|
26
|
+
child: ChildProcess;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Start one ProxyCommand and bridge its stdio into a Duplex.
|
|
30
|
+
*
|
|
31
|
+
* Failure modes are reported through the stream: a spawn error, or an exit
|
|
32
|
+
* before the SSH handshake finished, destroys it with the exit status and the
|
|
33
|
+
* tail of the command's stderr instead of letting ssh2 wait for its own
|
|
34
|
+
* `readyTimeout`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function startProxyCommand(rawCommand: string, target: ProxyTarget): ProxyCommandProcess;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PTY shell sessions for the web terminal: a standalone (non-pooled)
|
|
3
|
+
* ssh2 connection with a long-lived shell channel, resize, and pausable
|
|
4
|
+
* output delivery.
|
|
5
|
+
*/
|
|
6
|
+
import { type KeyboardInteractiveHandler, type PoolEngine } from './connection-pool.ts';
|
|
7
|
+
/** A live PTY shell session. */
|
|
8
|
+
export interface ShellSession {
|
|
9
|
+
/** Assign to receive remote output. */
|
|
10
|
+
onData?: (data: Buffer) => void;
|
|
11
|
+
/** Assign to be notified when the channel closes. */
|
|
12
|
+
onExit?: (code: number | null, error?: string) => void;
|
|
13
|
+
/** Write raw input to the shell. */
|
|
14
|
+
send(data: string): void;
|
|
15
|
+
/** Resize the remote PTY. */
|
|
16
|
+
resize(cols: number, rows: number): void;
|
|
17
|
+
/** Close the session and its channel. */
|
|
18
|
+
close(): void;
|
|
19
|
+
/** Pause remote output delivery (transport backpressure). */
|
|
20
|
+
pause(): void;
|
|
21
|
+
/** Resume remote output delivery. */
|
|
22
|
+
resume(): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Open a PTY shell session for the web terminal (standalone connection).
|
|
26
|
+
* The shell is a long-lived exclusive stream: it uses its own connection so
|
|
27
|
+
* closing it can never tear down a pooled exec/tunnel sharing the alias.
|
|
28
|
+
*/
|
|
29
|
+
export declare function openShell(engine: PoolEngine, alias: string, size: {
|
|
30
|
+
cols: number;
|
|
31
|
+
rows: number;
|
|
32
|
+
}, onKeyboardInteractive?: KeyboardInteractiveHandler): Promise<ShellSession>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SFTP transfers: upload (file or recursive tree), single-file download, and
|
|
3
|
+
* remote directory listing. Every channel is opened once per operation and
|
|
4
|
+
* released exactly once so sshd's MaxSessions cap is never exhausted.
|
|
5
|
+
*/
|
|
6
|
+
import type { RemoteDirEntry, TransferProgress } from '../protocol.ts';
|
|
7
|
+
import { type PoolEngine } from './connection-pool.ts';
|
|
8
|
+
/** Walk a local directory, collecting relative paths of every file. */
|
|
9
|
+
export declare function walkLocalDir(root: string): string[];
|
|
10
|
+
/** Upload one local file (or directory tree) to a remote path. */
|
|
11
|
+
export declare function upload(engine: PoolEngine, alias: string, localPath: string, remotePath: string, recursive: boolean, onProgress?: (progress: TransferProgress) => void): Promise<{
|
|
12
|
+
bytes: number;
|
|
13
|
+
files: number;
|
|
14
|
+
}>;
|
|
15
|
+
/** Download one remote file to a local path. */
|
|
16
|
+
export declare function download(engine: PoolEngine, alias: string, remotePath: string, localPath: string, onProgress?: (progress: TransferProgress) => void): Promise<{
|
|
17
|
+
bytes: number;
|
|
18
|
+
}>;
|
|
19
|
+
/** List a remote directory (file browser). */
|
|
20
|
+
export declare function ls(engine: PoolEngine, alias: string, path: string): Promise<RemoteDirEntry[]>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
|
+
import type { KeyboardInteractiveHandler } from './connection-pool.ts';
|
|
3
|
+
import type { ShellSession } from './pty.ts';
|
|
4
|
+
/** What the registry needs from the engine (swappable in tests). */
|
|
5
|
+
export interface TerminalSessionDeps {
|
|
6
|
+
/** Open one PTY shell for the alias (engine.openShell). */
|
|
7
|
+
openShell(alias: string, size: {
|
|
8
|
+
cols: number;
|
|
9
|
+
rows: number;
|
|
10
|
+
}, onKeyboardInteractive?: KeyboardInteractiveHandler): Promise<ShellSession>;
|
|
11
|
+
/** @returns the current time (defaults to Date.now). */
|
|
12
|
+
now?: () => number;
|
|
13
|
+
/** Bounded scrollback per session. */
|
|
14
|
+
maxBufferBytes?: number;
|
|
15
|
+
/** Idle window before a detached session is reaped. */
|
|
16
|
+
idleMs?: number;
|
|
17
|
+
/** Grace window an exited session is kept for reattaching views. */
|
|
18
|
+
exitGraceMs?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The host's terminal session table: open one session per connect, attach any
|
|
22
|
+
* number of sockets, and keep the shell alive across detach.
|
|
23
|
+
*/
|
|
24
|
+
export declare class TerminalSessionRegistry {
|
|
25
|
+
private readonly sessions;
|
|
26
|
+
private readonly deps;
|
|
27
|
+
private readonly maxBufferBytes;
|
|
28
|
+
private readonly idleMs;
|
|
29
|
+
private readonly exitGraceMs;
|
|
30
|
+
constructor(deps: TerminalSessionDeps);
|
|
31
|
+
/** Open a new session for the alias and attach the requesting socket to it. */
|
|
32
|
+
open(ws: WebSocket, alias: string, size: {
|
|
33
|
+
cols: number;
|
|
34
|
+
rows: number;
|
|
35
|
+
}): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Attach a socket to an existing session, replaying its scrollback first.
|
|
38
|
+
* @returns false when the session is unknown (expired or already reaped).
|
|
39
|
+
*/
|
|
40
|
+
attach(ws: WebSocket, sessionId: string, size: {
|
|
41
|
+
cols: number;
|
|
42
|
+
rows: number;
|
|
43
|
+
}): boolean;
|
|
44
|
+
/** End every session (host teardown, plugin dispose). */
|
|
45
|
+
dispose(): void;
|
|
46
|
+
/** Number of live sessions (introspection for tests and diagnostics). */
|
|
47
|
+
size(): number;
|
|
48
|
+
/** Wire one socket's frames and lifecycle onto a session. */
|
|
49
|
+
private bind;
|
|
50
|
+
/** Remove a socket from its session, keeping the shell alive. */
|
|
51
|
+
private detach;
|
|
52
|
+
/** End the session a socket is attached to (explicit close from the view). */
|
|
53
|
+
private end;
|
|
54
|
+
/** Mark a session exited and keep it briefly so a reattaching view sees it. */
|
|
55
|
+
private finish;
|
|
56
|
+
/** Append output to the replay buffer, trimmed to the byte cap. */
|
|
57
|
+
private pushOutput;
|
|
58
|
+
/** Broadcast one frame to every attached socket, honoring backpressure. */
|
|
59
|
+
private send;
|
|
60
|
+
private sendTo;
|
|
61
|
+
/** Pause while any attached socket is backed up; resume once all drained. */
|
|
62
|
+
private applyBackpressure;
|
|
63
|
+
/** Answer a keyboard-interactive challenge through whoever is attached. */
|
|
64
|
+
private keyboardInteractive;
|
|
65
|
+
private scheduleReap;
|
|
66
|
+
private clearReap;
|
|
67
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local port-forward tunnels: one loopback-only listener per tunnel, pinned
|
|
3
|
+
* to a pooled connection so an idle sweep never closes it, with per-tunnel
|
|
4
|
+
* socket tracking and stop (single / alias-scoped / all).
|
|
5
|
+
*/
|
|
6
|
+
import { type Server as NetServer, type Socket } from 'node:net';
|
|
7
|
+
import type { TunnelInfo } from '../protocol.ts';
|
|
8
|
+
import { type PoolEngine, type PoolRecord } from './connection-pool.ts';
|
|
9
|
+
/** One active tunnel record (server + pinned connection + live sockets). */
|
|
10
|
+
export interface TunnelRecord {
|
|
11
|
+
info: TunnelInfo;
|
|
12
|
+
server: NetServer;
|
|
13
|
+
alias: string;
|
|
14
|
+
/** The pooled connection this tunnel pins; siblings on one alias may share it. */
|
|
15
|
+
record: PoolRecord;
|
|
16
|
+
sockets: Set<Socket>;
|
|
17
|
+
/** Detach the link-death hook startTunnel installs on the pooled client. */
|
|
18
|
+
detachLink?: () => void;
|
|
19
|
+
}
|
|
20
|
+
/** The engine slice the tunnel module needs (adds the tunnel registry). */
|
|
21
|
+
export interface TunnelEngine extends PoolEngine {
|
|
22
|
+
readonly tunnels: Map<string, TunnelRecord>;
|
|
23
|
+
nextTunnelId: number;
|
|
24
|
+
}
|
|
25
|
+
/** Start a local port-forward tunnel (listens on 127.0.0.1 only). */
|
|
26
|
+
export declare function startTunnel(engine: TunnelEngine, alias: string, options: {
|
|
27
|
+
remotePort: number;
|
|
28
|
+
remoteHost?: string;
|
|
29
|
+
localPort?: number;
|
|
30
|
+
}): Promise<TunnelInfo>;
|
|
31
|
+
/** All active tunnels. */
|
|
32
|
+
export declare function listTunnels(engine: TunnelEngine): TunnelInfo[];
|
|
33
|
+
/** Stop one tunnel (closes the listener, live sockets, and the pinned connection). */
|
|
34
|
+
export declare function stopTunnel(engine: TunnelEngine, id: string): boolean;
|
|
35
|
+
/** Stop all tunnels (optionally for one alias). */
|
|
36
|
+
export declare function stopAllTunnels(engine: TunnelEngine, alias?: string): number;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SSH engine facade: a per-alias persistent connection pool (ssh2) with
|
|
3
|
+
* multi-hop jump support, command execution, PTY shells, SFTP transfers,
|
|
4
|
+
* local port-forward tunnels and cluster execution. The heavy lifting lives
|
|
5
|
+
* in the engine/ modules (connection-pool, pty, sftp, tunnel, cluster); this
|
|
6
|
+
* class composes them behind one new SshEngine instance per plugin apply.
|
|
7
|
+
*/
|
|
8
|
+
import type { ClusterResult, ExecResult, SshHostSummary, TestResult, TransferProgress, TunnelInfo } from './protocol.ts';
|
|
9
|
+
import type { HostStore } from './store.ts';
|
|
10
|
+
import { type EngineOptions, type KeyboardInteractiveHandler, type PoolRecord } from './engine/connection-pool.ts';
|
|
11
|
+
import { type ShellSession } from './engine/pty.ts';
|
|
12
|
+
import { type TunnelRecord } from './engine/tunnel.ts';
|
|
13
|
+
export type { EngineOptions, KeyboardInteractiveHandler } from './engine/connection-pool.ts';
|
|
14
|
+
export type { ShellSession } from './engine/pty.ts';
|
|
15
|
+
export type { PoolRecord } from './engine/connection-pool.ts';
|
|
16
|
+
export type { TunnelRecord } from './engine/tunnel.ts';
|
|
17
|
+
/**
|
|
18
|
+
* The engine. Owns the pool, tunnels, and all operations. One instance per
|
|
19
|
+
* plugin apply; dispose() closes every connection.
|
|
20
|
+
*/
|
|
21
|
+
export declare class SshEngine {
|
|
22
|
+
readonly store: HostStore;
|
|
23
|
+
readonly opts: Required<EngineOptions>;
|
|
24
|
+
readonly pool: Map<string, PoolRecord>;
|
|
25
|
+
readonly acquireQueue: Map<string, Promise<PoolRecord>>;
|
|
26
|
+
readonly tunnels: Map<string, TunnelRecord>;
|
|
27
|
+
nextTunnelId: number;
|
|
28
|
+
private sweepTimer;
|
|
29
|
+
/**
|
|
30
|
+
* @param store - the host config store.
|
|
31
|
+
* @param options - engine knobs (defaults applied).
|
|
32
|
+
*/
|
|
33
|
+
constructor(store: HostStore, options?: EngineOptions);
|
|
34
|
+
/** Secret-free host list (filtered by the optional query). */
|
|
35
|
+
list(query?: string): SshHostSummary[];
|
|
36
|
+
/** One host summary by alias. */
|
|
37
|
+
find(alias: string): SshHostSummary | undefined;
|
|
38
|
+
/** Run one command on `alias` (reusing the pooled connection). */
|
|
39
|
+
exec(alias: string, command: string, timeoutMs?: number, signal?: AbortSignal): Promise<ExecResult>;
|
|
40
|
+
/** Run one command against many hosts concurrently. */
|
|
41
|
+
cluster(options: {
|
|
42
|
+
command: string;
|
|
43
|
+
aliases?: string[];
|
|
44
|
+
environment?: string;
|
|
45
|
+
tags?: string[];
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
maxWorkers?: number;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
}): Promise<ClusterResult[]>;
|
|
50
|
+
/** Open a PTY shell session for the web terminal (standalone connection). */
|
|
51
|
+
openShell(alias: string, size: {
|
|
52
|
+
cols: number;
|
|
53
|
+
rows: number;
|
|
54
|
+
}, onKeyboardInteractive?: KeyboardInteractiveHandler): Promise<ShellSession>;
|
|
55
|
+
/** Upload one local file (or directory tree) to a remote path. */
|
|
56
|
+
upload(alias: string, localPath: string, remotePath: string, recursive: boolean, onProgress?: (progress: TransferProgress) => void): Promise<{
|
|
57
|
+
bytes: number;
|
|
58
|
+
files: number;
|
|
59
|
+
}>;
|
|
60
|
+
/** Download one remote file to a local path. */
|
|
61
|
+
download(alias: string, remotePath: string, localPath: string, onProgress?: (progress: TransferProgress) => void): Promise<{
|
|
62
|
+
bytes: number;
|
|
63
|
+
}>;
|
|
64
|
+
/** List a remote directory (file browser). */
|
|
65
|
+
ls(alias: string, path: string): Promise<import('./protocol.ts').RemoteDirEntry[]>;
|
|
66
|
+
/** Start a local port-forward tunnel (listens on 127.0.0.1 only). */
|
|
67
|
+
startTunnel(alias: string, options: {
|
|
68
|
+
remotePort: number;
|
|
69
|
+
remoteHost?: string;
|
|
70
|
+
localPort?: number;
|
|
71
|
+
}): Promise<TunnelInfo>;
|
|
72
|
+
/** All active tunnels. */
|
|
73
|
+
listTunnels(): TunnelInfo[];
|
|
74
|
+
/** Stop one tunnel (closes the listener, live sockets, and the pinned connection). */
|
|
75
|
+
stopTunnel(id: string): boolean;
|
|
76
|
+
/** Stop all tunnels (optionally for one alias). */
|
|
77
|
+
stopAllTunnels(alias?: string): number;
|
|
78
|
+
/**
|
|
79
|
+
* Drop every live artifact bound to one alias: stop its tunnels and close
|
|
80
|
+
* the pooled connection. Host entries that are deleted or whose connection
|
|
81
|
+
* fields change must never keep serving a stale, previously authenticated
|
|
82
|
+
* connection — the next operation re-connects from the current config.
|
|
83
|
+
*/
|
|
84
|
+
dropAlias(alias: string): void;
|
|
85
|
+
/** Probe connectivity with a cross-platform shell command. */
|
|
86
|
+
test(alias: string): Promise<TestResult>;
|
|
87
|
+
/** Close every pooled connection and tunnel. */
|
|
88
|
+
dispose(): void;
|
|
89
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared JSON body/response helpers for the host route families: one strict
|
|
3
|
+
* bounded body reader, one lenient bounded body reader, one JSON object
|
|
4
|
+
* narrow, one JSON writer, and the outbound request-init helper that keeps a
|
|
5
|
+
* host fetch body decodable (withIdentityEncoding). Previously the readers
|
|
6
|
+
* were copy-pasted across the
|
|
7
|
+
* package route files (routes.ts, update-routes.ts, mobile-api.ts, and each
|
|
8
|
+
* family's route module) with drifting contracts: body caps ranging 4 KiB to
|
|
9
|
+
* 1 MiB and four distinct overflow behaviors (reject, undefined, null, throw).
|
|
10
|
+
*
|
|
11
|
+
* Packages receive this file as a generated copy via scripts/sync-shared.mjs;
|
|
12
|
+
* edit this shared source and re-run the sync instead of editing a copy.
|
|
13
|
+
* Consumer code is migrated onto it in follow-up waves; no call site changes
|
|
14
|
+
* belong in the same change as its introduction.
|
|
15
|
+
* @module dsh-web-shared/host/http
|
|
16
|
+
*/
|
|
17
|
+
import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'node:http';
|
|
18
|
+
/**
|
|
19
|
+
* Strict bounded body reader: parse a request body of at most maxBytes as
|
|
20
|
+
* JSON.
|
|
21
|
+
* @throws 'body too large' past the cap, or the JSON.parse error for an
|
|
22
|
+
* invalid or empty payload.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readBoundedJson(req: IncomingMessage, maxBytes: number): Promise<unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Lenient bounded body reader: parse a request body as JSON, or null on an
|
|
27
|
+
* empty body, invalid JSON, or a body past maxBytes (default 64 KiB).
|
|
28
|
+
* Overflow destroys the request instead of draining the remainder (no drain
|
|
29
|
+
* call, matching the current repo-wide behavior); callers must not keep
|
|
30
|
+
* reading the request afterwards. With objectOnly, non-JSON-object payloads
|
|
31
|
+
* also yield null.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readJsonBody(req: IncomingMessage, opts?: {
|
|
34
|
+
maxBytes?: number;
|
|
35
|
+
objectOnly?: boolean;
|
|
36
|
+
}): Promise<unknown | null>;
|
|
37
|
+
/** Narrow a value to a JSON object, or undefined when it is not one. */
|
|
38
|
+
export declare function asJsonObject(value: unknown): Record<string, unknown> | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Request init that asks a remote origin for an uncompressed body.
|
|
41
|
+
*
|
|
42
|
+
* The DSH host boots `@deepseek-ai/dsh-http-proxy`, whose top-level import of
|
|
43
|
+
* the npm `undici` copy replaces the legacy `undici.globalDispatcher.1` slot
|
|
44
|
+
* Node's built-in `fetch()` reads. That cross-major wrapper drops the
|
|
45
|
+
* `content-encoding` header and automatic decompression, so a plain fetch
|
|
46
|
+
* resolves to raw gzip/brotli/zstd bytes — compressed noise no JSON or text
|
|
47
|
+
* consumer can read. Every host fetch that parses a remote body must request
|
|
48
|
+
* identity encoding. Caller headers survive; `accept-encoding` is forced to
|
|
49
|
+
* `identity` because a caller value has no decoder behind it in this host.
|
|
50
|
+
* @param init - the caller's request init (signal, headers, method, ...).
|
|
51
|
+
* @returns a copy with `accept-encoding: identity` merged in.
|
|
52
|
+
*/
|
|
53
|
+
export declare function withIdentityEncoding(init?: RequestInit): RequestInit;
|
|
54
|
+
/**
|
|
55
|
+
* Write one JSON response. Default headers are the family defaults
|
|
56
|
+
* (content-type and referrer-policy); caller headers are appended or
|
|
57
|
+
* override them.
|
|
58
|
+
*/
|
|
59
|
+
export declare function writeJson(res: ServerResponse, status: number, body: unknown, headers?: OutgoingHttpHeaders): void;
|