@pyrokine/mcp-ssh 3.0.0 → 3.2.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/README.md +423 -102
- package/README_zh.md +300 -74
- package/dist/command-risk.js +5 -2
- package/dist/directory-verification.d.ts +10 -3
- package/dist/directory-verification.js +271 -55
- package/dist/file-mode.js +25 -13
- package/dist/file-ops.d.ts +20 -6
- package/dist/file-ops.js +669 -282
- package/dist/forward-manager.js +6 -1
- package/dist/known-hosts.d.ts +22 -0
- package/dist/known-hosts.js +553 -0
- package/dist/openssh.d.ts +51 -0
- package/dist/openssh.js +553 -0
- package/dist/operation-manager.d.ts +2 -3
- package/dist/operation-manager.js +33 -5
- package/dist/pty-manager.d.ts +2 -1
- package/dist/pty-manager.js +81 -19
- package/dist/remote-platform.d.ts +1 -1
- package/dist/remote-platform.js +82 -50
- package/dist/security.d.ts +2 -0
- package/dist/security.js +39 -3
- package/dist/session-manager.d.ts +40 -4
- package/dist/session-manager.js +377 -61
- package/dist/sftp-timeout.d.ts +16 -1
- package/dist/sftp-timeout.js +104 -12
- package/dist/ssh-config.js +1 -1
- package/dist/tools/connection.js +89 -5
- package/dist/tools/exec.js +90 -37
- package/dist/tools/file.js +457 -146
- package/dist/tools/operation.js +7 -5
- package/dist/tools/pty.js +16 -13
- package/dist/tools/utils.d.ts +1 -1
- package/dist/tools/utils.js +2 -2
- package/dist/types.d.ts +46 -5
- package/dist/types.js +1 -0
- package/dist/utf8.d.ts +1 -1
- package/dist/utf8.js +2 -2
- package/dist/windows-acl.d.ts +1 -0
- package/dist/windows-acl.js +167 -80
- package/package.json +6 -5
package/dist/pty-manager.js
CHANGED
|
@@ -44,35 +44,44 @@ export class PtyManager {
|
|
|
44
44
|
maxBufferSize,
|
|
45
45
|
decoder: new StringDecoder('utf8'),
|
|
46
46
|
decoderEnded: false,
|
|
47
|
+
pendingCharacters: 0,
|
|
48
|
+
parsing: Promise.resolve(),
|
|
49
|
+
parseWaiters: new Set(),
|
|
50
|
+
outputPaused: false,
|
|
51
|
+
disposed: false,
|
|
52
|
+
detach: () => undefined,
|
|
47
53
|
active: true,
|
|
48
54
|
closedAt: null,
|
|
49
55
|
};
|
|
50
|
-
|
|
51
|
-
stream.on('data', (data) => {
|
|
56
|
+
const onData = (data) => {
|
|
52
57
|
if (!ptySession.active || ptySession.decoderEnded) {
|
|
53
58
|
return;
|
|
54
59
|
}
|
|
55
60
|
ptySession.lastOutputAt = Date.now();
|
|
56
61
|
this.appendRaw(ptySession, data);
|
|
57
62
|
this.appendOutput(ptySession, ptySession.decoder.write(data));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!this.sessions.has(ptyId)) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
this.endDecoder(ptySession);
|
|
64
|
-
ptySession.active = false;
|
|
65
|
-
ptySession.closedAt = Date.now();
|
|
66
|
-
});
|
|
67
|
-
stream.on('error', (err) => {
|
|
63
|
+
};
|
|
64
|
+
const onClose = () => {
|
|
68
65
|
if (!this.sessions.has(ptyId)) {
|
|
69
66
|
return;
|
|
70
67
|
}
|
|
71
68
|
this.endDecoder(ptySession);
|
|
72
69
|
ptySession.active = false;
|
|
73
70
|
ptySession.closedAt = Date.now();
|
|
74
|
-
|
|
75
|
-
}
|
|
71
|
+
ptySession.detach();
|
|
72
|
+
};
|
|
73
|
+
const onError = (err) => {
|
|
74
|
+
this.appendOutput(ptySession, `\n[PTY Error: ${err.message}]`);
|
|
75
|
+
onClose();
|
|
76
|
+
};
|
|
77
|
+
ptySession.detach = () => {
|
|
78
|
+
stream.off('data', onData);
|
|
79
|
+
stream.off('close', onClose);
|
|
80
|
+
stream.off('error', onError);
|
|
81
|
+
};
|
|
82
|
+
stream.on('data', onData);
|
|
83
|
+
stream.on('close', onClose);
|
|
84
|
+
stream.on('error', onError);
|
|
76
85
|
this.sessions.set(ptyId, ptySession);
|
|
77
86
|
return ptyId;
|
|
78
87
|
}
|
|
@@ -87,9 +96,15 @@ export class PtyManager {
|
|
|
87
96
|
backpressured: !session.stream.write(data),
|
|
88
97
|
};
|
|
89
98
|
}
|
|
90
|
-
read(ptyId, options = {}) {
|
|
99
|
+
async read(ptyId, options = {}) {
|
|
91
100
|
const session = this.getSession(ptyId);
|
|
92
101
|
const mode = options.mode ?? 'screen';
|
|
102
|
+
if (mode === 'screen') {
|
|
103
|
+
await session.parsing;
|
|
104
|
+
if (session.disposed) {
|
|
105
|
+
throw new Error(`PTY session '${ptyId}' not found`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
93
108
|
const clear = options.clear !== false;
|
|
94
109
|
const unreadRawBytes = session.unreadRawBytes;
|
|
95
110
|
const rawBufferTruncated = session.rawBufferTruncated;
|
|
@@ -121,6 +136,7 @@ export class PtyManager {
|
|
|
121
136
|
rawBufferLimit: session.maxBufferSize,
|
|
122
137
|
rawBufferTruncated,
|
|
123
138
|
foregroundProcess: session.command,
|
|
139
|
+
...(session.parserError ? { parserError: session.parserError } : {}),
|
|
124
140
|
};
|
|
125
141
|
}
|
|
126
142
|
resize(ptyId, rows, cols) {
|
|
@@ -160,7 +176,7 @@ export class PtyManager {
|
|
|
160
176
|
}
|
|
161
177
|
this.endDecoder(session);
|
|
162
178
|
try {
|
|
163
|
-
|
|
179
|
+
this.disposeParser(session);
|
|
164
180
|
}
|
|
165
181
|
catch (error) {
|
|
166
182
|
return {
|
|
@@ -198,7 +214,7 @@ export class PtyManager {
|
|
|
198
214
|
}
|
|
199
215
|
this.endDecoder(session);
|
|
200
216
|
try {
|
|
201
|
-
|
|
217
|
+
this.disposeParser(session);
|
|
202
218
|
}
|
|
203
219
|
catch {
|
|
204
220
|
// SSH 连接已失效,继续释放本地状态
|
|
@@ -232,10 +248,56 @@ export class PtyManager {
|
|
|
232
248
|
return result;
|
|
233
249
|
}
|
|
234
250
|
appendOutput(session, output) {
|
|
235
|
-
if (output.length === 0) {
|
|
251
|
+
if (output.length === 0 || session.disposed || session.parserError) {
|
|
236
252
|
return;
|
|
237
253
|
}
|
|
238
|
-
session.
|
|
254
|
+
session.pendingCharacters += output.length;
|
|
255
|
+
if (!session.outputPaused && session.pendingCharacters >= 1024 * 1024) {
|
|
256
|
+
session.outputPaused = true;
|
|
257
|
+
session.stream.pause();
|
|
258
|
+
}
|
|
259
|
+
session.parsing = new Promise((resolve) => {
|
|
260
|
+
session.parseWaiters.add(resolve);
|
|
261
|
+
try {
|
|
262
|
+
session.terminal.write(output, () => {
|
|
263
|
+
session.parseWaiters.delete(resolve);
|
|
264
|
+
if (session.disposed) {
|
|
265
|
+
resolve();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
session.pendingCharacters -= output.length;
|
|
269
|
+
if (session.outputPaused &&
|
|
270
|
+
session.pendingCharacters <= 256 * 1024 &&
|
|
271
|
+
session.active &&
|
|
272
|
+
!session.disposed) {
|
|
273
|
+
session.outputPaused = false;
|
|
274
|
+
session.stream.resume();
|
|
275
|
+
}
|
|
276
|
+
resolve();
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
session.parserError = error instanceof Error ? error.message : String(error);
|
|
281
|
+
session.active = false;
|
|
282
|
+
session.closedAt = Date.now();
|
|
283
|
+
session.detach();
|
|
284
|
+
session.stream.destroy();
|
|
285
|
+
for (const complete of session.parseWaiters) {
|
|
286
|
+
complete();
|
|
287
|
+
}
|
|
288
|
+
session.parseWaiters.clear();
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
disposeParser(session) {
|
|
293
|
+
session.detach();
|
|
294
|
+
session.disposed = true;
|
|
295
|
+
for (const complete of session.parseWaiters) {
|
|
296
|
+
complete();
|
|
297
|
+
}
|
|
298
|
+
session.parseWaiters.clear();
|
|
299
|
+
session.pendingCharacters = 0;
|
|
300
|
+
session.terminal.dispose();
|
|
239
301
|
}
|
|
240
302
|
appendRaw(session, data) {
|
|
241
303
|
const appended = Buffer.concat([session.rawBuffer, data]);
|
|
@@ -9,5 +9,5 @@ export declare function sftpPath(remotePath: string, style: RemotePathStyle): st
|
|
|
9
9
|
export declare function remoteShellPath(remotePath: string, style: RemotePathStyle): string;
|
|
10
10
|
export declare function quotePowershell(value: string): string;
|
|
11
11
|
export declare function encodedPowershell(script: string): string;
|
|
12
|
-
export declare function windowsCaseSemanticsScript(parent: string, names
|
|
12
|
+
export declare function windowsCaseSemanticsScript(parent: string, names?: string[]): string;
|
|
13
13
|
export declare function buildRemoteCommand(command: string, shell: CommandShell, cwd: string | undefined, env: Record<string, string>): string;
|
package/dist/remote-platform.js
CHANGED
|
@@ -36,7 +36,7 @@ export function remoteShellPath(remotePath, style) {
|
|
|
36
36
|
if (style !== 'sftp-posix') {
|
|
37
37
|
return remotePath;
|
|
38
38
|
}
|
|
39
|
-
const drive = /^\/([a-zA-Z]):(?:\/(
|
|
39
|
+
const drive = /^\/(?<drive>[a-zA-Z]):(?:\/(?<path>.*))?$/.exec(remotePath);
|
|
40
40
|
if (drive) {
|
|
41
41
|
return `${drive[1].toUpperCase()}:\\${(drive[2] ?? '').replace(/\//g, '\\')}`;
|
|
42
42
|
}
|
|
@@ -49,56 +49,84 @@ export function quotePowershell(value) {
|
|
|
49
49
|
return `'${value.replace(/'/g, "''")}'`;
|
|
50
50
|
}
|
|
51
51
|
export function encodedPowershell(script) {
|
|
52
|
-
return
|
|
52
|
+
return ('powershell.exe -NoProfile -NonInteractive -EncodedCommand ' +
|
|
53
|
+
`${Buffer.from(script, 'utf16le').toString('base64')}`);
|
|
53
54
|
}
|
|
54
55
|
export function windowsCaseSemanticsScript(parent, names) {
|
|
55
|
-
return
|
|
56
|
-
Add-Type -TypeDefinition @'
|
|
57
|
-
using System
|
|
58
|
-
using System.ComponentModel
|
|
59
|
-
using System.Runtime.InteropServices
|
|
60
|
-
using Microsoft.Win32.SafeHandles
|
|
61
|
-
public static class McpCaseSemantics {
|
|
62
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
63
|
-
public struct FILE_CASE_SENSITIVE_INFO { public uint Flags; }
|
|
64
|
-
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
[DllImport("kernel32.dll",
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
public static
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
'
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
56
|
+
return ("$ErrorActionPreference = 'Stop'\n" +
|
|
57
|
+
"Add-Type -TypeDefinition @'\n" +
|
|
58
|
+
'using System;\n' +
|
|
59
|
+
'using System.ComponentModel;\n' +
|
|
60
|
+
'using System.Runtime.InteropServices;\n' +
|
|
61
|
+
'using Microsoft.Win32.SafeHandles;\n' +
|
|
62
|
+
'public static class McpCaseSemantics {\n' +
|
|
63
|
+
' [StructLayout(LayoutKind.Sequential)]\n' +
|
|
64
|
+
' public struct FILE_CASE_SENSITIVE_INFO { public uint Flags; }\n' +
|
|
65
|
+
' [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = ' +
|
|
66
|
+
'true, ExactSpelling = true)]\n' +
|
|
67
|
+
' public static extern SafeFileHandle CreateFileW(string path, uint ' +
|
|
68
|
+
'access, uint share, IntPtr security, uint disposition, uint attributes, ' +
|
|
69
|
+
'IntPtr template);\n' +
|
|
70
|
+
' [DllImport("kernel32.dll", SetLastError = true)]\n' +
|
|
71
|
+
' [return: MarshalAs(UnmanagedType.Bool)]\n' +
|
|
72
|
+
' public static extern bool ' +
|
|
73
|
+
'GetFileInformationByHandleEx(SafeFileHandle handle, int infoClass, out ' +
|
|
74
|
+
'FILE_CASE_SENSITIVE_INFO info, uint size);\n' +
|
|
75
|
+
' [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = ' +
|
|
76
|
+
'true, ExactSpelling = true)]\n' +
|
|
77
|
+
' public static extern int CompareStringOrdinal(string left, int ' +
|
|
78
|
+
'leftLength, string right, int rightLength, ' +
|
|
79
|
+
'[MarshalAs(UnmanagedType.Bool)] bool ignoreCase);\n' +
|
|
80
|
+
' private static int Compare(string left, string right) {\n' +
|
|
81
|
+
' int result = CompareStringOrdinal(left, left.Length, right, ' +
|
|
82
|
+
'right.Length, true);\n' +
|
|
83
|
+
' if (result == 0) { throw new ' +
|
|
84
|
+
'Win32Exception(Marshal.GetLastWin32Error()); }\n' +
|
|
85
|
+
' return result - 2;\n' +
|
|
86
|
+
' }\n' +
|
|
87
|
+
' public static uint DirectoryFlags(string parent) {\n' +
|
|
88
|
+
' // FILE_READ_ATTRIBUTES; FILE_SHARE_READ | WRITE | DELETE; ' +
|
|
89
|
+
'OPEN_EXISTING; NORMAL | BACKUP_SEMANTICS.\n' +
|
|
90
|
+
' using (SafeFileHandle handle = CreateFileW(parent, 128, 7, ' +
|
|
91
|
+
'IntPtr.Zero, 3, 128 | 33554432, IntPtr.Zero)) {\n' +
|
|
92
|
+
' if (handle.IsInvalid) { throw new ' +
|
|
93
|
+
'Win32Exception(Marshal.GetLastWin32Error()); }\n' +
|
|
94
|
+
' FILE_CASE_SENSITIVE_INFO info;\n' +
|
|
95
|
+
' if (!GetFileInformationByHandleEx(handle, 23, out info, 4)) ' +
|
|
96
|
+
'{ throw new Win32Exception(Marshal.GetLastWin32Error()); }\n' +
|
|
97
|
+
' return info.Flags;\n' +
|
|
98
|
+
' }\n' +
|
|
99
|
+
' }\n' +
|
|
100
|
+
' public static string[] Collision(string[] names) {\n' +
|
|
101
|
+
' Array.Sort(names, Compare);\n' +
|
|
102
|
+
' for (int i = 1; i < names.Length; ++i) {\n' +
|
|
103
|
+
' if (Compare(names[i - 1], names[i]) == 0) { return new ' +
|
|
104
|
+
'string[] { names[i - 1], names[i] }; }\n' +
|
|
105
|
+
' }\n' +
|
|
106
|
+
' return null;\n' +
|
|
107
|
+
' }\n' +
|
|
108
|
+
'}\n' +
|
|
109
|
+
"'@\n" +
|
|
110
|
+
'$parent = ' +
|
|
111
|
+
`${quotePowershell(parent)}` +
|
|
112
|
+
'\n' +
|
|
113
|
+
"if (-not [System.IO.Directory]::Exists($parent)) { throw 'candidate " +
|
|
114
|
+
"parent directory does not exist' }\n" +
|
|
115
|
+
'$caseSensitive = ([McpCaseSemantics]::DirectoryFlags($parent) -band 1) ' +
|
|
116
|
+
'-ne 0\n' +
|
|
117
|
+
'$names = ' +
|
|
118
|
+
`${names === undefined
|
|
119
|
+
? '[string[]](ConvertFrom-Json ' +
|
|
120
|
+
'([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String([Consol' +
|
|
121
|
+
'e]::In.ReadToEnd()))))'
|
|
122
|
+
: `[string[]]@(${names.map(quotePowershell).join(', ')})`}` +
|
|
123
|
+
'\n' +
|
|
124
|
+
'$collision = $null\n' +
|
|
125
|
+
'if (-not $caseSensitive) { $collision = ' +
|
|
126
|
+
'[McpCaseSemantics]::Collision($names) }\n' +
|
|
127
|
+
'[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)\n' +
|
|
128
|
+
'@{ caseSensitive = $caseSensitive; collision = $collision } | ' +
|
|
129
|
+
'ConvertTo-Json -Compress');
|
|
102
130
|
}
|
|
103
131
|
export function buildRemoteCommand(command, shell, cwd, env) {
|
|
104
132
|
const validKey = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
@@ -122,7 +150,11 @@ export function buildRemoteCommand(command, shell, cwd, env) {
|
|
|
122
150
|
...(cwd ? [`Set-Location -LiteralPath ${quotePowershell(cwd)}`] : []),
|
|
123
151
|
...Object.entries(env).map(([key, value]) => `$env:${key} = ${quotePowershell(value)}`),
|
|
124
152
|
];
|
|
125
|
-
return encodedPowershell(
|
|
153
|
+
return encodedPowershell("$ErrorActionPreference = 'Stop'; " +
|
|
154
|
+
`${setup.join('; ')}` +
|
|
155
|
+
'; & cmd.exe /D /V:OFF /S /C ' +
|
|
156
|
+
`${quotePowershell(command)}` +
|
|
157
|
+
'; exit $LASTEXITCODE');
|
|
126
158
|
}
|
|
127
159
|
const setup = [
|
|
128
160
|
...(cwd ? [`cd ${escapeShellArg(cwd)}`] : []),
|
package/dist/security.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export declare function validateKeyFile(p: string): void;
|
|
|
18
18
|
* 同 validateKeyFile:原路径 + realpath 都必须在白名单
|
|
19
19
|
*/
|
|
20
20
|
export declare function validateConfigFile(p: string): void;
|
|
21
|
+
/** known_hosts protects write integrity; public read access is permitted. */
|
|
22
|
+
export declare function validateKnownHostsPath(filePath: string, system?: boolean): string;
|
|
21
23
|
/**
|
|
22
24
|
* 脱敏 ssh2 keyParser 错误,避免 PEM 文件 cipher/kdf 字段被泄露
|
|
23
25
|
*/
|
package/dist/security.js
CHANGED
|
@@ -20,9 +20,12 @@ function getAllowedDirs() {
|
|
|
20
20
|
function isUnderAllowedDir(resolved) {
|
|
21
21
|
const allowed = getAllowedDirs();
|
|
22
22
|
const ok = allowed.some((dir) => {
|
|
23
|
-
const
|
|
24
|
-
return (
|
|
25
|
-
|
|
23
|
+
const roots = fs.existsSync(dir) ? [dir, fs.realpathSync(dir)] : [dir];
|
|
24
|
+
return roots.some((root) => {
|
|
25
|
+
const relative = path.relative(root, resolved);
|
|
26
|
+
return (relative === '' ||
|
|
27
|
+
(relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
|
|
28
|
+
});
|
|
26
29
|
});
|
|
27
30
|
return { ok, allowed };
|
|
28
31
|
}
|
|
@@ -75,6 +78,39 @@ export function validateConfigFile(p) {
|
|
|
75
78
|
throw new Error(`SSH config file too large: ${stats.size} bytes (max ${MAX_CONFIG_FILE_SIZE})`);
|
|
76
79
|
}
|
|
77
80
|
}
|
|
81
|
+
/** known_hosts protects write integrity; public read access is permitted. */
|
|
82
|
+
export function validateKnownHostsPath(filePath, system = false) {
|
|
83
|
+
const resolveRealPath = (candidate) => {
|
|
84
|
+
let ancestor = candidate;
|
|
85
|
+
const suffix = [];
|
|
86
|
+
while (!fs.existsSync(ancestor)) {
|
|
87
|
+
const parent = path.dirname(ancestor);
|
|
88
|
+
if (parent === ancestor) {
|
|
89
|
+
throw new Error('known_hosts path has no existing ancestor');
|
|
90
|
+
}
|
|
91
|
+
suffix.unshift(path.basename(ancestor));
|
|
92
|
+
ancestor = parent;
|
|
93
|
+
}
|
|
94
|
+
return path.join(fs.realpathSync(ancestor), ...suffix);
|
|
95
|
+
};
|
|
96
|
+
const allowed = getAllowedDirs();
|
|
97
|
+
if (system && process.platform === 'win32') {
|
|
98
|
+
allowed.push(path.resolve(process.env.ProgramData ?? 'C:\\ProgramData', 'ssh'));
|
|
99
|
+
}
|
|
100
|
+
// Resolve trusted roots as well as files: system ancestors such as /etc can be aliases.
|
|
101
|
+
const realAllowed = allowed.map(resolveRealPath);
|
|
102
|
+
const under = (candidate, directories) => directories.some((dir) => {
|
|
103
|
+
const relative = path.relative(dir, candidate);
|
|
104
|
+
return (relative === '' ||
|
|
105
|
+
(relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
|
|
106
|
+
});
|
|
107
|
+
const resolved = path.resolve(filePath);
|
|
108
|
+
const real = resolveRealPath(resolved);
|
|
109
|
+
if (!under(resolved, [...allowed, ...realAllowed]) || !under(real, realAllowed)) {
|
|
110
|
+
throw new Error('known_hosts path is outside the allowed SSH directories');
|
|
111
|
+
}
|
|
112
|
+
return real;
|
|
113
|
+
}
|
|
78
114
|
/**
|
|
79
115
|
* 脱敏 ssh2 keyParser 错误,避免 PEM 文件 cipher/kdf 字段被泄露
|
|
80
116
|
*/
|
|
@@ -8,20 +8,41 @@
|
|
|
8
8
|
* - 会话持久化
|
|
9
9
|
*/
|
|
10
10
|
import { Client, SFTPWrapper } from 'ssh2';
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
import { type HostKeyTrust } from './known-hosts.js';
|
|
12
|
+
import { OpenSshConnection } from './openssh.js';
|
|
13
|
+
import { ConnectionFailureDetails, ExecOptions, ExecResult, ExternalTransferCapability, ForwardCloseOptions, ForwardCloseResult, OpenSshMasterInfo, OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions, PortForwardInfo, PtyCloseResult, PtyOptions, PtyReadResult, PtySessionInfo, PtyWriteResult, SSHConnectionConfig, SSHSessionBrief, SSHSessionDetail } from './types.js';
|
|
14
|
+
interface SessionState {
|
|
14
15
|
config: SSHConnectionConfig;
|
|
15
16
|
connectedAt: number;
|
|
16
17
|
lastUsedAt: number;
|
|
17
18
|
reconnectAttempts: number;
|
|
18
19
|
connected: boolean;
|
|
19
20
|
authAgent?: string;
|
|
21
|
+
hostKey?: HostKeyTrust;
|
|
22
|
+
hostKeys?: (HostKeyTrust & {
|
|
23
|
+
host: string;
|
|
24
|
+
port: number;
|
|
25
|
+
})[];
|
|
26
|
+
connectionError?: ConnectionError;
|
|
20
27
|
manualClose: boolean;
|
|
21
28
|
/** 当前正在等待的重连定时器,disconnect 时需要清除以防 zombie 重连 */
|
|
22
29
|
reconnectTimer?: NodeJS.Timeout;
|
|
23
30
|
}
|
|
31
|
+
interface Ssh2Session extends SessionState {
|
|
32
|
+
backend?: 'ssh2';
|
|
33
|
+
client: Client;
|
|
34
|
+
jumpClient?: Client;
|
|
35
|
+
}
|
|
36
|
+
type SSHSession = Ssh2Session | (SessionState & {
|
|
37
|
+
backend: 'openssh';
|
|
38
|
+
transport: OpenSshConnection;
|
|
39
|
+
});
|
|
24
40
|
interface SessionConnectionInfo {
|
|
41
|
+
backend: 'ssh2' | 'openssh';
|
|
42
|
+
masterReused?: boolean;
|
|
43
|
+
identityStatus?: OpenSshMasterInfo['identityStatus'];
|
|
44
|
+
masterPid?: number;
|
|
45
|
+
observedLogin?: OpenSshMasterInfo['observedLogin'];
|
|
25
46
|
alias: string;
|
|
26
47
|
identity: string;
|
|
27
48
|
loginUser: string;
|
|
@@ -35,6 +56,15 @@ interface SessionConnectionInfo {
|
|
|
35
56
|
commandShell: 'posix' | 'cmd' | 'powershell';
|
|
36
57
|
remotePathStyle: 'posix' | 'win32' | 'sftp-posix';
|
|
37
58
|
keyAclStatus?: 'key_path_usable' | 'key_acl_insecure' | 'key_acl_unverified';
|
|
59
|
+
hostKey?: HostKeyTrust;
|
|
60
|
+
hostKeys?: (HostKeyTrust & {
|
|
61
|
+
host: string;
|
|
62
|
+
port: number;
|
|
63
|
+
})[];
|
|
64
|
+
}
|
|
65
|
+
declare class ConnectionError extends Error {
|
|
66
|
+
readonly details: ConnectionFailureDetails;
|
|
67
|
+
constructor(message: string, details: ConnectionFailureDetails);
|
|
38
68
|
}
|
|
39
69
|
export declare class SessionManager {
|
|
40
70
|
private sessions;
|
|
@@ -42,6 +72,7 @@ export declare class SessionManager {
|
|
|
42
72
|
private connectInFlight;
|
|
43
73
|
private pendingConnections;
|
|
44
74
|
private connectionGenerations;
|
|
75
|
+
private readonly knownHosts;
|
|
45
76
|
private readonly ptyManager;
|
|
46
77
|
private readonly forwardManager;
|
|
47
78
|
private readonly operationManager;
|
|
@@ -72,6 +103,7 @@ export declare class SessionManager {
|
|
|
72
103
|
* 获取会话
|
|
73
104
|
*/
|
|
74
105
|
getSession(alias: string): SSHSession;
|
|
106
|
+
getOpenSshConnection(alias: string): OpenSshConnection | undefined;
|
|
75
107
|
listSessions(): SSHSessionBrief[];
|
|
76
108
|
listSessionDetails(): SSHSessionDetail[];
|
|
77
109
|
getSessionConnectionInfo(alias: string): SessionConnectionInfo;
|
|
@@ -119,7 +151,7 @@ export declare class SessionManager {
|
|
|
119
151
|
ptyRead(ptyId: string, options?: {
|
|
120
152
|
mode?: 'screen' | 'raw';
|
|
121
153
|
clear?: boolean;
|
|
122
|
-
}): PtyReadResult
|
|
154
|
+
}): Promise<PtyReadResult>;
|
|
123
155
|
ptyResize(ptyId: string, rows: number, cols: number): boolean;
|
|
124
156
|
ptyClose(ptyId: string): PtyCloseResult;
|
|
125
157
|
ptyList(): PtySessionInfo[];
|
|
@@ -133,6 +165,7 @@ export declare class SessionManager {
|
|
|
133
165
|
}>;
|
|
134
166
|
forwardClose(forwardId: string, options?: ForwardCloseOptions): Promise<ForwardCloseResult>;
|
|
135
167
|
forwardList(): PortForwardInfo[];
|
|
168
|
+
private requireSsh2Session;
|
|
136
169
|
private isSshAgentAvailable;
|
|
137
170
|
private isOpenSshKeyPathUsable;
|
|
138
171
|
private sessionIdentity;
|
|
@@ -142,9 +175,11 @@ export declare class SessionManager {
|
|
|
142
175
|
private appendWithinByteLimit;
|
|
143
176
|
private appendOutputTail;
|
|
144
177
|
private recommendedReadCommand;
|
|
178
|
+
private commandExitMetadata;
|
|
145
179
|
private emptyOutputFailureMetadata;
|
|
146
180
|
private outputMetadata;
|
|
147
181
|
private normalizeMaxOutputSize;
|
|
182
|
+
private openCommandChannel;
|
|
148
183
|
private openOperationStream;
|
|
149
184
|
private cancelRemoteOperation;
|
|
150
185
|
private execDirect;
|
|
@@ -166,6 +201,7 @@ export declare class SessionManager {
|
|
|
166
201
|
private isCurrentPendingConnection;
|
|
167
202
|
private cancelPendingConnection;
|
|
168
203
|
private invalidateAliasResources;
|
|
204
|
+
private stopUntrustedConnection;
|
|
169
205
|
/**
|
|
170
206
|
* 检查连接是否存活
|
|
171
207
|
*/
|