@pyrokine/mcp-ssh 2.4.1 → 2.4.2
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 +174 -130
- package/README_zh.md +232 -128
- package/dist/directory-verification.js +29 -4
- package/dist/file-ops.d.ts +13 -8
- package/dist/file-ops.js +191 -115
- package/dist/forward-manager.js +9 -3
- package/dist/operation-manager.d.ts +0 -5
- package/dist/operation-manager.js +13 -55
- package/dist/pty-manager.d.ts +3 -3
- package/dist/pty-manager.js +61 -15
- package/dist/session-manager.d.ts +3 -3
- package/dist/session-manager.js +44 -20
- package/dist/sftp-timeout.d.ts +9 -0
- package/dist/sftp-timeout.js +49 -0
- package/dist/tools/connection.js +75 -29
- package/dist/tools/exec.js +16 -6
- package/dist/tools/file.js +101 -55
- package/dist/tools/forward.js +6 -5
- package/dist/tools/operation.js +2 -1
- package/dist/tools/pty.js +17 -13
- package/dist/tools/schema.d.ts +4 -0
- package/dist/tools/schema.js +9 -0
- package/dist/types.d.ts +15 -0
- package/dist/utf8.d.ts +3 -0
- package/dist/utf8.js +44 -0
- package/package.json +6 -6
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { ensureUtf8Boundary, utf8SafeEnd } from './utf8.js';
|
|
2
3
|
export const DEFAULT_OPERATION_MAX_OUTPUT_BYTES = 1024 * 1024;
|
|
3
4
|
export const HARD_OPERATION_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
|
|
4
5
|
export const DEFAULT_OPERATION_READ_BYTES = 64 * 1024;
|
|
@@ -99,7 +100,8 @@ export class OperationManager {
|
|
|
99
100
|
startTimeoutMs,
|
|
100
101
|
status: 'unknown',
|
|
101
102
|
retryable: false,
|
|
102
|
-
suggestion: 'SSH channel request 状态无法确认;使用 ssh_operation_status
|
|
103
|
+
suggestion: 'SSH channel request 状态无法确认;使用 ssh_operation_status 查询记录,' +
|
|
104
|
+
'必要时用 ssh_disconnect 释放底层 pending channel',
|
|
103
105
|
}));
|
|
104
106
|
}, startTimeoutMs);
|
|
105
107
|
}),
|
|
@@ -148,21 +150,25 @@ export class OperationManager {
|
|
|
148
150
|
const maxReadBytes = this.normalizeReadBytes(options.maxBytes);
|
|
149
151
|
const stdoutOffset = this.normalizeOffset(options.stdoutOffset, record.stdout.length, 'stdoutOffset');
|
|
150
152
|
const stderrOffset = this.normalizeOffset(options.stderrOffset, record.stderr.length, 'stderrOffset');
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
153
|
+
ensureUtf8Boundary(record.stdout, stdoutOffset, 'stdoutOffset');
|
|
154
|
+
ensureUtf8Boundary(record.stderr, stderrOffset, 'stderrOffset');
|
|
155
|
+
const stdoutRequestedEnd = Math.min(record.stdout.length, stdoutOffset + maxReadBytes);
|
|
156
|
+
const stdoutEnd = utf8SafeEnd(record.stdout, stdoutOffset, stdoutRequestedEnd, 'stdout');
|
|
154
157
|
const stdout = record.stdout.subarray(stdoutOffset, stdoutEnd);
|
|
158
|
+
const nextStdoutOffset = record.stdoutTruncated && stdoutRequestedEnd === record.stdout.length ? stdoutRequestedEnd : stdoutEnd;
|
|
155
159
|
const remaining = maxReadBytes - stdout.length;
|
|
156
|
-
const
|
|
160
|
+
const stderrRequestedEnd = Math.min(record.stderr.length, stderrOffset + remaining);
|
|
161
|
+
const stderrEnd = utf8SafeEnd(record.stderr, stderrOffset, stderrRequestedEnd, 'stderr');
|
|
157
162
|
const stderr = record.stderr.subarray(stderrOffset, stderrEnd);
|
|
163
|
+
const nextStderrOffset = record.stderrTruncated && stderrRequestedEnd === record.stderr.length ? stderrRequestedEnd : stderrEnd;
|
|
158
164
|
return {
|
|
159
165
|
...this.info(record),
|
|
160
166
|
stdout: stdout.toString('utf8'),
|
|
161
167
|
stderr: stderr.toString('utf8'),
|
|
162
168
|
stdoutOffset,
|
|
163
169
|
stderrOffset,
|
|
164
|
-
nextStdoutOffset
|
|
165
|
-
nextStderrOffset
|
|
170
|
+
nextStdoutOffset,
|
|
171
|
+
nextStderrOffset,
|
|
166
172
|
readBytes: stdout.length + stderr.length,
|
|
167
173
|
maxReadBytes,
|
|
168
174
|
};
|
|
@@ -494,52 +500,4 @@ export class OperationManager {
|
|
|
494
500
|
}
|
|
495
501
|
return normalized;
|
|
496
502
|
}
|
|
497
|
-
ensureUtf8Boundary(buffer, offset, field) {
|
|
498
|
-
if (offset < buffer.length && this.isUtf8ContinuationByte(buffer[offset])) {
|
|
499
|
-
throw new Error(`${field} must point to a UTF-8 character boundary`);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
utf8SafeEnd(buffer, start, requestedEnd, stream) {
|
|
503
|
-
const limitedByMaxBytes = requestedEnd < buffer.length;
|
|
504
|
-
let end = requestedEnd;
|
|
505
|
-
if (limitedByMaxBytes) {
|
|
506
|
-
while (end > start && this.isUtf8ContinuationByte(buffer[end])) {
|
|
507
|
-
--end;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
end = this.trimIncompleteUtf8Sequence(buffer, start, end);
|
|
511
|
-
if (end === start && requestedEnd > start && limitedByMaxBytes) {
|
|
512
|
-
throw new Error(`maxBytes is too small to read the next UTF-8 character from ${stream}`);
|
|
513
|
-
}
|
|
514
|
-
return end;
|
|
515
|
-
}
|
|
516
|
-
trimIncompleteUtf8Sequence(buffer, start, end) {
|
|
517
|
-
if (end <= start) {
|
|
518
|
-
return end;
|
|
519
|
-
}
|
|
520
|
-
let sequenceStart = end - 1;
|
|
521
|
-
while (sequenceStart > start && this.isUtf8ContinuationByte(buffer[sequenceStart])) {
|
|
522
|
-
--sequenceStart;
|
|
523
|
-
}
|
|
524
|
-
const expectedLength = this.utf8SequenceLength(buffer[sequenceStart]);
|
|
525
|
-
return sequenceStart + expectedLength > end ? sequenceStart : end;
|
|
526
|
-
}
|
|
527
|
-
utf8SequenceLength(value) {
|
|
528
|
-
if ((value & 0x80) === 0) {
|
|
529
|
-
return 1;
|
|
530
|
-
}
|
|
531
|
-
if ((value & 0xe0) === 0xc0) {
|
|
532
|
-
return 2;
|
|
533
|
-
}
|
|
534
|
-
if ((value & 0xf0) === 0xe0) {
|
|
535
|
-
return 3;
|
|
536
|
-
}
|
|
537
|
-
if ((value & 0xf8) === 0xf0) {
|
|
538
|
-
return 4;
|
|
539
|
-
}
|
|
540
|
-
return 1;
|
|
541
|
-
}
|
|
542
|
-
isUtf8ContinuationByte(value) {
|
|
543
|
-
return (value & 0xc0) === 0x80;
|
|
544
|
-
}
|
|
545
503
|
}
|
package/dist/pty-manager.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 负责 PTY 会话的完整生命周期:创建、读写、调整大小、关闭
|
|
5
5
|
*/
|
|
6
6
|
import type { ClientChannel } from 'ssh2';
|
|
7
|
-
import type { PtyOptions, PtySessionInfo } from './types.js';
|
|
7
|
+
import type { PtyCloseResult, PtyOptions, PtySessionInfo, PtyWriteResult } from './types.js';
|
|
8
8
|
export declare const HARD_PTY_BUFFER_SIZE: number;
|
|
9
9
|
/** PTY 创建所需的外部依赖 */
|
|
10
10
|
export interface PtyDependencies {
|
|
@@ -20,7 +20,7 @@ export declare class PtyManager {
|
|
|
20
20
|
private idleSweeper;
|
|
21
21
|
constructor();
|
|
22
22
|
start(deps: PtyDependencies, alias: string, command: string, options?: PtyOptions): Promise<string>;
|
|
23
|
-
write(ptyId: string, data: string):
|
|
23
|
+
write(ptyId: string, data: string): PtyWriteResult;
|
|
24
24
|
read(ptyId: string, options?: {
|
|
25
25
|
mode?: 'screen' | 'raw';
|
|
26
26
|
clear?: boolean;
|
|
@@ -37,7 +37,7 @@ export declare class PtyManager {
|
|
|
37
37
|
foregroundProcess: string;
|
|
38
38
|
};
|
|
39
39
|
resize(ptyId: string, rows: number, cols: number): boolean;
|
|
40
|
-
close(ptyId: string):
|
|
40
|
+
close(ptyId: string): PtyCloseResult;
|
|
41
41
|
/** 关闭指定 alias 的所有 PTY 会话 */
|
|
42
42
|
closeByAlias(alias: string): void;
|
|
43
43
|
list(): PtySessionInfo[];
|
package/dist/pty-manager.js
CHANGED
|
@@ -19,8 +19,8 @@ export class PtyManager {
|
|
|
19
19
|
}
|
|
20
20
|
async start(deps, alias, command, options = {}) {
|
|
21
21
|
const ptyId = this.generateId();
|
|
22
|
-
const rows = options.rows
|
|
23
|
-
const cols = options.cols
|
|
22
|
+
const rows = options.rows ?? 24;
|
|
23
|
+
const cols = options.cols ?? 80;
|
|
24
24
|
const maxBufferSize = this.normalizeBufferSize(options.bufferSize);
|
|
25
25
|
const stream = await deps.execPty(alias, command, options);
|
|
26
26
|
// 创建 xterm headless 终端仿真器
|
|
@@ -81,11 +81,14 @@ export class PtyManager {
|
|
|
81
81
|
throw new Error(`PTY session '${ptyId}' is closed`);
|
|
82
82
|
}
|
|
83
83
|
session.lastInputAt = Date.now();
|
|
84
|
-
return
|
|
84
|
+
return {
|
|
85
|
+
accepted: true,
|
|
86
|
+
backpressured: !session.stream.write(data),
|
|
87
|
+
};
|
|
85
88
|
}
|
|
86
89
|
read(ptyId, options = {}) {
|
|
87
90
|
const session = this.getSession(ptyId);
|
|
88
|
-
const mode = options.mode
|
|
91
|
+
const mode = options.mode ?? 'screen';
|
|
89
92
|
const clear = options.clear !== false;
|
|
90
93
|
let data;
|
|
91
94
|
if (mode === 'screen') {
|
|
@@ -126,30 +129,73 @@ export class PtyManager {
|
|
|
126
129
|
close(ptyId) {
|
|
127
130
|
const session = this.sessions.get(ptyId);
|
|
128
131
|
if (!session) {
|
|
129
|
-
return
|
|
132
|
+
return {
|
|
133
|
+
success: false,
|
|
134
|
+
ptyId,
|
|
135
|
+
status: 'not_found',
|
|
136
|
+
retryable: false,
|
|
137
|
+
};
|
|
130
138
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
139
|
+
if (session.active) {
|
|
140
|
+
try {
|
|
141
|
+
session.stream.close();
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
return {
|
|
145
|
+
success: false,
|
|
146
|
+
ptyId,
|
|
147
|
+
status: 'failed',
|
|
148
|
+
retryable: true,
|
|
149
|
+
error: error instanceof Error ? error.message : String(error),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
136
152
|
}
|
|
137
153
|
try {
|
|
138
154
|
session.terminal.dispose();
|
|
139
155
|
}
|
|
140
|
-
catch (
|
|
141
|
-
|
|
156
|
+
catch (error) {
|
|
157
|
+
return {
|
|
158
|
+
success: false,
|
|
159
|
+
ptyId,
|
|
160
|
+
status: session.active ? 'unknown' : 'failed',
|
|
161
|
+
retryable: true,
|
|
162
|
+
error: error instanceof Error ? error.message : String(error),
|
|
163
|
+
};
|
|
142
164
|
}
|
|
143
165
|
session.active = false;
|
|
144
166
|
this.sessions.delete(ptyId);
|
|
145
|
-
return
|
|
167
|
+
return {
|
|
168
|
+
success: true,
|
|
169
|
+
ptyId,
|
|
170
|
+
status: 'closed',
|
|
171
|
+
retryable: false,
|
|
172
|
+
};
|
|
146
173
|
}
|
|
147
174
|
/** 关闭指定 alias 的所有 PTY 会话 */
|
|
148
175
|
closeByAlias(alias) {
|
|
149
176
|
for (const [id, session] of this.sessions) {
|
|
150
|
-
if (session.alias
|
|
151
|
-
|
|
177
|
+
if (session.alias !== alias) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const result = this.close(id);
|
|
181
|
+
if (result.success) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
session.stream.destroy();
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
// SSH 连接已失效,继续释放本地状态
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
session.terminal.dispose();
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
// SSH 连接已失效,继续释放本地状态
|
|
152
195
|
}
|
|
196
|
+
session.active = false;
|
|
197
|
+
this.sessions.delete(id);
|
|
198
|
+
console.warn(`PTY ${id} 在 SSH 连接失效时强制清理: ${result.status}`);
|
|
153
199
|
}
|
|
154
200
|
}
|
|
155
201
|
list() {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - 会话持久化
|
|
9
9
|
*/
|
|
10
10
|
import { Client, SFTPWrapper } from 'ssh2';
|
|
11
|
-
import { ExecOptions, ExecResult, ExternalTransferCapability, ForwardCloseOptions, ForwardCloseResult, OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions, PortForwardInfo, PtyOptions, PtySessionInfo, SSHConnectionConfig, SSHSessionBrief, SSHSessionDetail } from './types.js';
|
|
11
|
+
import { ExecOptions, ExecResult, ExternalTransferCapability, ForwardCloseOptions, ForwardCloseResult, OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions, PortForwardInfo, PtyCloseResult, PtyOptions, PtySessionInfo, PtyWriteResult, SSHConnectionConfig, SSHSessionBrief, SSHSessionDetail } from './types.js';
|
|
12
12
|
interface SSHSession {
|
|
13
13
|
client: Client;
|
|
14
14
|
config: SSHConnectionConfig;
|
|
@@ -105,7 +105,7 @@ export declare class SessionManager {
|
|
|
105
105
|
*/
|
|
106
106
|
getSftp(alias: string): Promise<SFTPWrapper>;
|
|
107
107
|
ptyStart(alias: string, command: string, options?: PtyOptions): Promise<string>;
|
|
108
|
-
ptyWrite(ptyId: string, data: string):
|
|
108
|
+
ptyWrite(ptyId: string, data: string): PtyWriteResult;
|
|
109
109
|
ptyRead(ptyId: string, options?: {
|
|
110
110
|
mode?: 'screen' | 'raw';
|
|
111
111
|
clear?: boolean;
|
|
@@ -122,7 +122,7 @@ export declare class SessionManager {
|
|
|
122
122
|
foregroundProcess: string;
|
|
123
123
|
};
|
|
124
124
|
ptyResize(ptyId: string, rows: number, cols: number): boolean;
|
|
125
|
-
ptyClose(ptyId: string):
|
|
125
|
+
ptyClose(ptyId: string): PtyCloseResult;
|
|
126
126
|
ptyList(): PtySessionInfo[];
|
|
127
127
|
forwardLocal(alias: string, localPort: number, remoteHost: string, remotePort: number, localHost?: string): Promise<{
|
|
128
128
|
forwardId: string;
|
package/dist/session-manager.js
CHANGED
|
@@ -200,7 +200,7 @@ export class SessionManager {
|
|
|
200
200
|
alias,
|
|
201
201
|
identity: this.sessionIdentity(session),
|
|
202
202
|
host: session.config.host,
|
|
203
|
-
port: session.config.port
|
|
203
|
+
port: session.config.port ?? 22,
|
|
204
204
|
username: session.config.username,
|
|
205
205
|
runAs: session.config.runAs,
|
|
206
206
|
authMethod: session.config.privateKeyPath
|
|
@@ -225,7 +225,7 @@ export class SessionManager {
|
|
|
225
225
|
loginUser: config.username,
|
|
226
226
|
runAs: config.runAs,
|
|
227
227
|
host: config.host,
|
|
228
|
-
port: config.port
|
|
228
|
+
port: config.port ?? 22,
|
|
229
229
|
defaultEnvKeys: config.defaultEnv ? Object.keys(config.defaultEnv) : [],
|
|
230
230
|
envKeys: config.env ? Object.keys(config.env) : [],
|
|
231
231
|
hasJumpHost: Boolean(config.jumpHost),
|
|
@@ -264,7 +264,7 @@ export class SessionManager {
|
|
|
264
264
|
alias,
|
|
265
265
|
identity: this.sessionIdentity(session),
|
|
266
266
|
host: config.host,
|
|
267
|
-
port: config.port
|
|
267
|
+
port: config.port ?? 22,
|
|
268
268
|
username: config.username,
|
|
269
269
|
authMethod,
|
|
270
270
|
keyPath: keyPathUsable ? expandedKeyPath : undefined,
|
|
@@ -339,7 +339,28 @@ export class SessionManager {
|
|
|
339
339
|
const startTime = Date.now();
|
|
340
340
|
const timeout = options.timeout ?? this.defaultTimeout;
|
|
341
341
|
const maxOutputSize = this.normalizeMaxOutputSize(options.maxOutputSize);
|
|
342
|
-
const
|
|
342
|
+
const runAs = options.useLoginUser ? undefined : (options.runAs ?? session.config.runAs);
|
|
343
|
+
let fullCommand;
|
|
344
|
+
let context;
|
|
345
|
+
if (runAs) {
|
|
346
|
+
if (!this.isValidUsername(runAs)) {
|
|
347
|
+
throw new Error(`Invalid username: ${runAs}`);
|
|
348
|
+
}
|
|
349
|
+
const loadProfile = options.loadProfile ?? true;
|
|
350
|
+
const targetCommand = this.buildCommand(`${loadProfile ? this.getLoadProfileCommand() : ''}sudo -S ${command}`, session, options);
|
|
351
|
+
const env = { ...session.config.defaultEnv, ...session.config.env, ...options.env };
|
|
352
|
+
fullCommand = `su - ${runAs} -c ${this.escapeShellArg(targetCommand)}`;
|
|
353
|
+
context = {
|
|
354
|
+
runAs,
|
|
355
|
+
loadProfile,
|
|
356
|
+
cwd: options.cwd,
|
|
357
|
+
envInjectedKeys: Object.keys(env).filter((key) => this.isValidEnvKey(key)),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
fullCommand = this.buildCommand(`sudo -S ${command}`, session, options);
|
|
362
|
+
context = { cwd: options.cwd };
|
|
363
|
+
}
|
|
343
364
|
return new Promise((resolve, reject) => {
|
|
344
365
|
let settled = false;
|
|
345
366
|
let streamRef = null;
|
|
@@ -373,7 +394,7 @@ export class SessionManager {
|
|
|
373
394
|
catch {
|
|
374
395
|
// ignore
|
|
375
396
|
}
|
|
376
|
-
reject(this.createCommandTimeoutError(alias, timeout,
|
|
397
|
+
reject(this.createCommandTimeoutError(alias, timeout, context, outputPreview()));
|
|
377
398
|
}, timeout);
|
|
378
399
|
session.client.exec(fullCommand, {}, (err, stream) => {
|
|
379
400
|
if (!this.isCurrentSession(alias, session)) {
|
|
@@ -418,7 +439,7 @@ export class SessionManager {
|
|
|
418
439
|
stderrTruncated: stderrTruncated || undefined,
|
|
419
440
|
...this.outputMetadata(outputPreview()),
|
|
420
441
|
...this.emptyOutputFailureMetadata(code, outputPreview()),
|
|
421
|
-
...this.resultMetadata(session, options,
|
|
442
|
+
...this.resultMetadata(session, options, context),
|
|
422
443
|
});
|
|
423
444
|
});
|
|
424
445
|
stream.on('data', (data) => {
|
|
@@ -543,7 +564,7 @@ export class SessionManager {
|
|
|
543
564
|
return this.configIdentity(session.config);
|
|
544
565
|
}
|
|
545
566
|
configIdentity(config) {
|
|
546
|
-
return `${config.username}@${config.host}:${config.port
|
|
567
|
+
return `${config.username}@${config.host}:${config.port ?? 22}`;
|
|
547
568
|
}
|
|
548
569
|
connectionFingerprint(config) {
|
|
549
570
|
const secretHash = (value) => value === undefined ? undefined : createHash('sha256').update(value).digest('hex');
|
|
@@ -552,7 +573,7 @@ export class SessionManager {
|
|
|
552
573
|
: Object.entries(values).sort(([left], [right]) => left.localeCompare(right));
|
|
553
574
|
const payload = (value) => ({
|
|
554
575
|
host: value.host,
|
|
555
|
-
port: value.port
|
|
576
|
+
port: value.port ?? 22,
|
|
556
577
|
username: value.username,
|
|
557
578
|
alias: value.alias,
|
|
558
579
|
template: value.template,
|
|
@@ -594,7 +615,8 @@ export class SessionManager {
|
|
|
594
615
|
return next.length > OUTPUT_PREVIEW_CHARS ? next.slice(-OUTPUT_PREVIEW_CHARS) : next;
|
|
595
616
|
}
|
|
596
617
|
recommendedReadCommand() {
|
|
597
|
-
return '将远端命令输出重定向到文件,例如 command > /tmp/mcp-ssh-output.txt 2>&1
|
|
618
|
+
return ('将远端命令输出重定向到文件,例如 command > /tmp/mcp-ssh-output.txt 2>&1,' +
|
|
619
|
+
'再用 ssh_read_file(remotePath="/tmp/mcp-ssh-output.txt", tail=true, maxBytes=65536) 分块读取');
|
|
598
620
|
}
|
|
599
621
|
emptyOutputFailureMetadata(exitCode, preview) {
|
|
600
622
|
if (exitCode === 0 || preview.stdoutBytes > 0 || preview.stderrBytes > 0) {
|
|
@@ -603,7 +625,8 @@ export class SessionManager {
|
|
|
603
625
|
return {
|
|
604
626
|
emptyOutputFailure: true,
|
|
605
627
|
recommendedReadCommand: this.recommendedReadCommand(),
|
|
606
|
-
suggestion: '远端命令以非零退出码结束,但没有 stdout/stderr。请检查 cwd、effectiveUser、shell/profile
|
|
628
|
+
suggestion: '远端命令以非零退出码结束,但没有 stdout/stderr。请检查 cwd、effectiveUser、shell/profile,' +
|
|
629
|
+
'或用 set -x / 显式重定向输出到文件后再用 ssh_read_file 读取',
|
|
607
630
|
};
|
|
608
631
|
}
|
|
609
632
|
outputMetadata(preview) {
|
|
@@ -684,7 +707,8 @@ export class SessionManager {
|
|
|
684
707
|
`pid=${pid}`,
|
|
685
708
|
`expected=${this.escapeShellArg(expectedMarker)}`,
|
|
686
709
|
'if [ ! -r "/proc/$pid/environ" ]; then printf "operation marker unavailable" >&2; exit 3; fi',
|
|
687
|
-
'if ! tr "\\000" "\\n" < "/proc/$pid/environ" | grep -Fqx -- "$expected"; then
|
|
710
|
+
'if ! tr "\\000" "\\n" < "/proc/$pid/environ" | grep -Fqx -- "$expected"; then ' +
|
|
711
|
+
'printf "operation marker mismatch" >&2; exit 4; fi',
|
|
688
712
|
`kill -TERM -- ${signalTarget}`,
|
|
689
713
|
].join('; ');
|
|
690
714
|
try {
|
|
@@ -724,9 +748,9 @@ export class SessionManager {
|
|
|
724
748
|
const execOptions = {};
|
|
725
749
|
if (options.pty) {
|
|
726
750
|
execOptions.pty = {
|
|
727
|
-
rows: options.rows
|
|
728
|
-
cols: options.cols
|
|
729
|
-
term: options.term
|
|
751
|
+
rows: options.rows ?? 24,
|
|
752
|
+
cols: options.cols ?? 80,
|
|
753
|
+
term: options.term ?? 'xterm-256color',
|
|
730
754
|
};
|
|
731
755
|
}
|
|
732
756
|
const outputPreview = () => ({
|
|
@@ -899,7 +923,7 @@ export class SessionManager {
|
|
|
899
923
|
suggestion: '重新连接跳板机,或检查 jumpHost 配置和网络路径',
|
|
900
924
|
});
|
|
901
925
|
}
|
|
902
|
-
connectConfig.sock = await this.forwardConnection(jumpSession.client, config.host, config.port
|
|
926
|
+
connectConfig.sock = await this.forwardConnection(jumpSession.client, config.host, config.port ?? 22, readyTimeout);
|
|
903
927
|
}
|
|
904
928
|
if (!this.isCurrentPendingConnection(alias, pendingConnection)) {
|
|
905
929
|
client.end();
|
|
@@ -983,9 +1007,9 @@ export class SessionManager {
|
|
|
983
1007
|
return new Promise((resolve, reject) => {
|
|
984
1008
|
session.client.exec(fullCommand, {
|
|
985
1009
|
pty: {
|
|
986
|
-
rows: options.rows
|
|
987
|
-
cols: options.cols
|
|
988
|
-
term: options.term
|
|
1010
|
+
rows: options.rows ?? 24,
|
|
1011
|
+
cols: options.cols ?? 80,
|
|
1012
|
+
term: options.term ?? 'xterm-256color',
|
|
989
1013
|
},
|
|
990
1014
|
}, (err, stream) => {
|
|
991
1015
|
if (!this.isCurrentSession(alias, session)) {
|
|
@@ -1175,7 +1199,7 @@ export class SessionManager {
|
|
|
1175
1199
|
});
|
|
1176
1200
|
}
|
|
1177
1201
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
1178
|
-
const target = `${config.username}@${config.host}:${config.port
|
|
1202
|
+
const target = `${config.username}@${config.host}:${config.port ?? 22}`;
|
|
1179
1203
|
return new ConnectionError(`SSH connection to ${target} failed: ${sanitizeKeyError(err.message)}`, {
|
|
1180
1204
|
...this.classifyConnectionError(err, readyTimeout),
|
|
1181
1205
|
connectionStep,
|
|
@@ -1277,7 +1301,7 @@ export class SessionManager {
|
|
|
1277
1301
|
data.push({
|
|
1278
1302
|
alias,
|
|
1279
1303
|
host: session.config.host,
|
|
1280
|
-
port: session.config.port
|
|
1304
|
+
port: session.config.port ?? 22,
|
|
1281
1305
|
username: session.config.username,
|
|
1282
1306
|
runAs: session.config.runAs,
|
|
1283
1307
|
connectedAt: session.connectedAt,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const DEFAULT_SFTP_OPERATION_TIMEOUT_MS = 600000;
|
|
2
|
+
export declare const HARD_SFTP_OPERATION_TIMEOUT_MS = 3600000;
|
|
3
|
+
export declare class SftpOperationTimeoutError extends Error {
|
|
4
|
+
readonly timeout: number;
|
|
5
|
+
readonly details: Record<string, unknown>;
|
|
6
|
+
constructor(timeout: number);
|
|
7
|
+
}
|
|
8
|
+
export declare function normalizeSftpOperationTimeout(timeout: number | undefined): number;
|
|
9
|
+
export declare function withSftpOperationTimeout<T>(operation: Promise<T>, timeout: number | undefined, onTimeout: () => void): Promise<T>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export const DEFAULT_SFTP_OPERATION_TIMEOUT_MS = 600_000;
|
|
2
|
+
export const HARD_SFTP_OPERATION_TIMEOUT_MS = 3_600_000;
|
|
3
|
+
export class SftpOperationTimeoutError extends Error {
|
|
4
|
+
timeout;
|
|
5
|
+
details;
|
|
6
|
+
constructor(timeout) {
|
|
7
|
+
super(`SFTP operation timed out after ${timeout}ms; remote transfer state is unknown`);
|
|
8
|
+
this.timeout = timeout;
|
|
9
|
+
this.name = 'SftpOperationTimeoutError';
|
|
10
|
+
this.details = {
|
|
11
|
+
operationStatus: 'unknown',
|
|
12
|
+
retryable: true,
|
|
13
|
+
timeout,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function normalizeSftpOperationTimeout(timeout) {
|
|
18
|
+
const normalized = timeout ?? DEFAULT_SFTP_OPERATION_TIMEOUT_MS;
|
|
19
|
+
if (!Number.isSafeInteger(normalized) || normalized <= 0 || normalized > HARD_SFTP_OPERATION_TIMEOUT_MS) {
|
|
20
|
+
throw new Error(`SFTP timeout must be between 1 and ${HARD_SFTP_OPERATION_TIMEOUT_MS}ms`);
|
|
21
|
+
}
|
|
22
|
+
return normalized;
|
|
23
|
+
}
|
|
24
|
+
export function withSftpOperationTimeout(operation, timeout, onTimeout) {
|
|
25
|
+
const normalizedTimeout = normalizeSftpOperationTimeout(timeout);
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let settled = false;
|
|
28
|
+
const settle = (callback) => {
|
|
29
|
+
if (settled) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
settled = true;
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
callback();
|
|
35
|
+
};
|
|
36
|
+
const timer = setTimeout(() => {
|
|
37
|
+
settle(() => {
|
|
38
|
+
try {
|
|
39
|
+
onTimeout();
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// 超时响应优先于释放 SFTP channel 失败
|
|
43
|
+
}
|
|
44
|
+
reject(new SftpOperationTimeoutError(normalizedTimeout));
|
|
45
|
+
});
|
|
46
|
+
}, normalizedTimeout);
|
|
47
|
+
operation.then((value) => settle(() => resolve(value)), (error) => settle(() => reject(error)));
|
|
48
|
+
});
|
|
49
|
+
}
|