@pyrokine/mcp-ssh 1.1.3 → 1.2.1
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 +3 -1
- package/README_zh.md +3 -1
- package/dist/file-ops.js +3 -1
- package/dist/forward-manager.d.ts +33 -0
- package/dist/forward-manager.js +185 -0
- package/dist/pty-manager.d.ts +37 -0
- package/dist/pty-manager.js +169 -0
- package/dist/session-manager.d.ts +15 -67
- package/dist/session-manager.js +104 -365
- package/package.json +2 -2
- package/src/file-ops.ts +3 -1
- package/src/forward-manager.ts +257 -0
- package/src/pty-manager.ts +220 -0
- package/src/session-manager.ts +136 -450
package/dist/session-manager.js
CHANGED
|
@@ -7,18 +7,16 @@
|
|
|
7
7
|
* - 自动重连
|
|
8
8
|
* - 会话持久化
|
|
9
9
|
*/
|
|
10
|
-
import xterm from '@xterm/headless';
|
|
11
10
|
import * as fs from 'fs';
|
|
12
|
-
import * as
|
|
11
|
+
import * as os from 'os';
|
|
13
12
|
import * as path from 'path';
|
|
14
13
|
import { Client } from 'ssh2';
|
|
15
|
-
|
|
14
|
+
import { ForwardManager } from './forward-manager.js';
|
|
15
|
+
import { PtyManager } from './pty-manager.js';
|
|
16
16
|
export class SessionManager {
|
|
17
17
|
sessions = new Map();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
ptyIdCounter = 0;
|
|
21
|
-
forwardIdCounter = 0;
|
|
18
|
+
ptyManager = new PtyManager();
|
|
19
|
+
forwardManager = new ForwardManager();
|
|
22
20
|
persistPath;
|
|
23
21
|
defaultKeepaliveInterval = 30000; // 30秒
|
|
24
22
|
defaultKeepaliveCountMax = 3;
|
|
@@ -26,9 +24,8 @@ export class SessionManager {
|
|
|
26
24
|
maxReconnectAttempts = 3;
|
|
27
25
|
defaultMaxOutputSize = 10 * 1024 * 1024; // 10MB
|
|
28
26
|
reconnectDelay = 5000; // 5秒
|
|
29
|
-
defaultPtyBufferSize = 1024 * 1024; // 1MB
|
|
30
27
|
constructor(persistPath) {
|
|
31
|
-
this.persistPath = persistPath || path.join(
|
|
28
|
+
this.persistPath = persistPath || path.join(os.homedir(), '.ssh-mcp-pro', 'sessions.json');
|
|
32
29
|
this.ensurePersistDir();
|
|
33
30
|
}
|
|
34
31
|
/**
|
|
@@ -83,6 +80,7 @@ export class SessionManager {
|
|
|
83
80
|
lastUsedAt: Date.now(),
|
|
84
81
|
reconnectAttempts: 0,
|
|
85
82
|
connected: true,
|
|
83
|
+
manualClose: false,
|
|
86
84
|
};
|
|
87
85
|
this.sessions.set(alias, session);
|
|
88
86
|
this.persistSessions();
|
|
@@ -90,23 +88,24 @@ export class SessionManager {
|
|
|
90
88
|
});
|
|
91
89
|
client.on('error', (err) => {
|
|
92
90
|
const session = this.sessions.get(alias);
|
|
93
|
-
if (session) {
|
|
91
|
+
if (session && session.client === client) {
|
|
94
92
|
session.connected = false;
|
|
95
93
|
}
|
|
96
|
-
|
|
94
|
+
const target = `${connectConfig.username}@${connectConfig.host}:${connectConfig.port}`;
|
|
95
|
+
const suggestion = this.diagnoseConnectionError(err);
|
|
96
|
+
reject(new Error(`SSH connection to ${target} failed: ${err.message}${suggestion ?
|
|
97
|
+
` (${suggestion})` :
|
|
98
|
+
''}`));
|
|
97
99
|
});
|
|
98
100
|
client.on('close', () => {
|
|
99
101
|
const session = this.sessions.get(alias);
|
|
100
|
-
if (session) {
|
|
102
|
+
if (session && session.client === client) {
|
|
101
103
|
session.connected = false;
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
console.error(`Auto-reconnect failed for ${alias}:`, err.message);
|
|
108
|
-
});
|
|
109
|
-
}, this.reconnectDelay);
|
|
104
|
+
// 清理绑定旧 client 的 forward/PTY 资源
|
|
105
|
+
this.forwardManager.closeByAlias(alias);
|
|
106
|
+
this.ptyManager.closeByAlias(alias);
|
|
107
|
+
if (!session.manualClose) {
|
|
108
|
+
this.scheduleReconnect(alias);
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
});
|
|
@@ -121,6 +120,8 @@ export class SessionManager {
|
|
|
121
120
|
if (!session) {
|
|
122
121
|
throw new Error(`Session ${alias} not found`);
|
|
123
122
|
}
|
|
123
|
+
session.manualClose = true;
|
|
124
|
+
session.connected = false;
|
|
124
125
|
try {
|
|
125
126
|
session.client.end();
|
|
126
127
|
}
|
|
@@ -134,6 +135,10 @@ export class SessionManager {
|
|
|
134
135
|
disconnect(alias) {
|
|
135
136
|
const session = this.sessions.get(alias);
|
|
136
137
|
if (session) {
|
|
138
|
+
session.manualClose = true;
|
|
139
|
+
// 清理关联的 PTY 和 forward 资源
|
|
140
|
+
this.ptyManager.closeByAlias(alias);
|
|
141
|
+
this.forwardManager.closeByAlias(alias);
|
|
137
142
|
try {
|
|
138
143
|
session.client.end();
|
|
139
144
|
}
|
|
@@ -301,296 +306,64 @@ export class SessionManager {
|
|
|
301
306
|
});
|
|
302
307
|
});
|
|
303
308
|
}
|
|
304
|
-
|
|
305
|
-
* 启动持久化 PTY 会话
|
|
306
|
-
*/
|
|
309
|
+
// ===== PTY 委托 =====
|
|
307
310
|
async ptyStart(alias, command, options = {}) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
const cols = options.cols || 80;
|
|
312
|
-
const term = options.term || 'xterm-256color';
|
|
313
|
-
const maxBufferSize = options.bufferSize || this.defaultPtyBufferSize;
|
|
314
|
-
const fullCommand = this.buildCommand(command, session, options);
|
|
315
|
-
return new Promise((resolve, reject) => {
|
|
316
|
-
session.client.exec(fullCommand, {
|
|
317
|
-
pty: { rows, cols, term },
|
|
318
|
-
}, (err, stream) => {
|
|
319
|
-
if (err) {
|
|
320
|
-
reject(new Error(`PTY start failed: ${err.message}`));
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
// 创建 xterm headless 终端仿真器
|
|
324
|
-
const terminal = new Terminal({
|
|
325
|
-
rows,
|
|
326
|
-
cols,
|
|
327
|
-
allowProposedApi: true,
|
|
328
|
-
});
|
|
329
|
-
const ptySession = {
|
|
330
|
-
id: ptyId,
|
|
331
|
-
alias,
|
|
332
|
-
command,
|
|
333
|
-
stream,
|
|
334
|
-
terminal,
|
|
335
|
-
rows,
|
|
336
|
-
cols,
|
|
337
|
-
createdAt: Date.now(),
|
|
338
|
-
lastReadAt: Date.now(),
|
|
339
|
-
rawBuffer: '',
|
|
340
|
-
maxBufferSize,
|
|
341
|
-
active: true,
|
|
342
|
-
};
|
|
343
|
-
// 监听输出数据
|
|
344
|
-
stream.on('data', (data) => {
|
|
345
|
-
if (!ptySession.active) {
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
const chunk = data.toString('utf-8');
|
|
349
|
-
// 写入终端仿真器(解析 ANSI 序列)
|
|
350
|
-
terminal.write(chunk);
|
|
351
|
-
// 同时保留原始流(用于 raw 模式)
|
|
352
|
-
ptySession.rawBuffer += chunk;
|
|
353
|
-
if (ptySession.rawBuffer.length > maxBufferSize) {
|
|
354
|
-
ptySession.rawBuffer = ptySession.rawBuffer.slice(-maxBufferSize);
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
stream.on('close', () => {
|
|
358
|
-
ptySession.active = false;
|
|
359
|
-
});
|
|
360
|
-
stream.on('error', (err) => {
|
|
361
|
-
ptySession.active = false;
|
|
362
|
-
terminal.write(`\n[PTY Error: ${err.message}]`);
|
|
363
|
-
});
|
|
364
|
-
this.ptySessions.set(ptyId, ptySession);
|
|
365
|
-
resolve(ptyId);
|
|
366
|
-
});
|
|
367
|
-
});
|
|
311
|
+
return this.ptyManager.start({
|
|
312
|
+
execPty: (a, cmd, opts) => this.execPtyStream(a, cmd, opts),
|
|
313
|
+
}, alias, command, options);
|
|
368
314
|
}
|
|
369
|
-
/**
|
|
370
|
-
* 向 PTY 写入数据
|
|
371
|
-
*/
|
|
372
315
|
ptyWrite(ptyId, data) {
|
|
373
|
-
|
|
374
|
-
if (!ptySession) {
|
|
375
|
-
throw new Error(`PTY session '${ptyId}' not found`);
|
|
376
|
-
}
|
|
377
|
-
if (!ptySession.active) {
|
|
378
|
-
throw new Error(`PTY session '${ptyId}' is closed`);
|
|
379
|
-
}
|
|
380
|
-
return ptySession.stream.write(data);
|
|
316
|
+
return this.ptyManager.write(ptyId, data);
|
|
381
317
|
}
|
|
382
|
-
/**
|
|
383
|
-
* 读取 PTY 输出
|
|
384
|
-
*
|
|
385
|
-
* options.mode: 'screen' 返回当前屏幕内容,'raw' 返回原始 ANSI 流
|
|
386
|
-
*/
|
|
387
318
|
ptyRead(ptyId, options = {}) {
|
|
388
|
-
|
|
389
|
-
if (!ptySession) {
|
|
390
|
-
throw new Error(`PTY session '${ptyId}' not found`);
|
|
391
|
-
}
|
|
392
|
-
const mode = options.mode || 'screen';
|
|
393
|
-
const clear = options.clear !== false;
|
|
394
|
-
let data;
|
|
395
|
-
if (mode === 'screen') {
|
|
396
|
-
// 返回当前屏幕内容(解析后的纯文本)
|
|
397
|
-
data = this.getScreenContent(ptySession.terminal);
|
|
398
|
-
}
|
|
399
|
-
else {
|
|
400
|
-
// 返回原始 ANSI 流
|
|
401
|
-
data = ptySession.rawBuffer;
|
|
402
|
-
if (clear) {
|
|
403
|
-
ptySession.rawBuffer = '';
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
ptySession.lastReadAt = Date.now();
|
|
407
|
-
return {
|
|
408
|
-
data,
|
|
409
|
-
active: ptySession.active,
|
|
410
|
-
rows: ptySession.rows,
|
|
411
|
-
cols: ptySession.cols,
|
|
412
|
-
};
|
|
319
|
+
return this.ptyManager.read(ptyId, options);
|
|
413
320
|
}
|
|
414
|
-
/**
|
|
415
|
-
* 调整 PTY 窗口大小
|
|
416
|
-
*/
|
|
417
321
|
ptyResize(ptyId, rows, cols) {
|
|
418
|
-
|
|
419
|
-
if (!ptySession) {
|
|
420
|
-
throw new Error(`PTY session '${ptyId}' not found`);
|
|
421
|
-
}
|
|
422
|
-
if (!ptySession.active) {
|
|
423
|
-
throw new Error(`PTY session '${ptyId}' is closed`);
|
|
424
|
-
}
|
|
425
|
-
// 调整远程 PTY 窗口
|
|
426
|
-
ptySession.stream.setWindow(rows, cols, 0, 0);
|
|
427
|
-
// 调整本地终端仿真器
|
|
428
|
-
ptySession.terminal.resize(cols, rows);
|
|
429
|
-
ptySession.rows = rows;
|
|
430
|
-
ptySession.cols = cols;
|
|
431
|
-
return true;
|
|
322
|
+
return this.ptyManager.resize(ptyId, rows, cols);
|
|
432
323
|
}
|
|
433
|
-
/**
|
|
434
|
-
* 关闭 PTY 会话
|
|
435
|
-
*/
|
|
436
324
|
ptyClose(ptyId) {
|
|
437
|
-
|
|
438
|
-
if (!ptySession) {
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
try {
|
|
442
|
-
ptySession.stream.close();
|
|
443
|
-
}
|
|
444
|
-
catch { /* 忽略清理异常 */
|
|
445
|
-
}
|
|
446
|
-
try {
|
|
447
|
-
ptySession.terminal.dispose();
|
|
448
|
-
}
|
|
449
|
-
catch { /* 忽略清理异常 */
|
|
450
|
-
}
|
|
451
|
-
ptySession.active = false;
|
|
452
|
-
this.ptySessions.delete(ptyId);
|
|
453
|
-
return true;
|
|
325
|
+
return this.ptyManager.close(ptyId);
|
|
454
326
|
}
|
|
455
|
-
/**
|
|
456
|
-
* 列出所有 PTY 会话
|
|
457
|
-
*/
|
|
458
327
|
ptyList() {
|
|
459
|
-
|
|
460
|
-
for (const [id, pty] of this.ptySessions) {
|
|
461
|
-
result.push({
|
|
462
|
-
id,
|
|
463
|
-
alias: pty.alias,
|
|
464
|
-
command: pty.command,
|
|
465
|
-
rows: pty.rows,
|
|
466
|
-
cols: pty.cols,
|
|
467
|
-
createdAt: pty.createdAt,
|
|
468
|
-
lastReadAt: pty.lastReadAt,
|
|
469
|
-
bufferSize: pty.rawBuffer.length,
|
|
470
|
-
active: pty.active,
|
|
471
|
-
});
|
|
472
|
-
}
|
|
473
|
-
return result;
|
|
328
|
+
return this.ptyManager.list();
|
|
474
329
|
}
|
|
475
|
-
|
|
476
|
-
* 创建本地端口转发
|
|
477
|
-
* 本地监听 localHost:localPort,转发到远程 remoteHost:remotePort
|
|
478
|
-
*/
|
|
330
|
+
// ===== Forward 委托 =====
|
|
479
331
|
async forwardLocal(alias, localPort, remoteHost, remotePort, localHost = '127.0.0.1') {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
socket.pipe(stream).pipe(socket);
|
|
490
|
-
});
|
|
491
|
-
});
|
|
492
|
-
server.on('error', (err) => {
|
|
493
|
-
reject(new Error(`Local forward failed: ${err.message}`));
|
|
494
|
-
});
|
|
495
|
-
server.listen(localPort, localHost, () => {
|
|
496
|
-
const fwdSession = {
|
|
497
|
-
id: forwardId,
|
|
498
|
-
alias,
|
|
499
|
-
type: 'local',
|
|
500
|
-
localHost,
|
|
501
|
-
localPort,
|
|
502
|
-
remoteHost,
|
|
503
|
-
remotePort,
|
|
504
|
-
server,
|
|
505
|
-
createdAt: Date.now(),
|
|
506
|
-
active: true,
|
|
507
|
-
};
|
|
508
|
-
this.forwardSessions.set(forwardId, fwdSession);
|
|
509
|
-
resolve(forwardId);
|
|
510
|
-
});
|
|
332
|
+
return this.forwardManager.forwardLocal({ getClient: (a) => this.getSession(a).client }, alias, localPort, remoteHost, remotePort, localHost);
|
|
333
|
+
}
|
|
334
|
+
async forwardRemote(alias, remotePort, localHost, localPort, remoteHost = '127.0.0.1') {
|
|
335
|
+
return this.forwardManager.forwardRemote({ getClient: (a) => this.getSession(a).client }, alias, remotePort, localHost, localPort, remoteHost);
|
|
336
|
+
}
|
|
337
|
+
forwardClose(forwardId) {
|
|
338
|
+
return this.forwardManager.close(forwardId, {
|
|
339
|
+
getClient: (a) => this.getSession(a).client,
|
|
511
340
|
});
|
|
512
341
|
}
|
|
342
|
+
forwardList() {
|
|
343
|
+
return this.forwardManager.list();
|
|
344
|
+
}
|
|
513
345
|
/**
|
|
514
|
-
*
|
|
515
|
-
* 远程监听 remoteHost:remotePort,转发到本地 localHost:localPort
|
|
346
|
+
* 执行带 PTY 的命令,返回原始 stream(供 PtyManager 使用)
|
|
516
347
|
*/
|
|
517
|
-
|
|
348
|
+
execPtyStream(alias, command, options) {
|
|
518
349
|
const session = this.getSession(alias);
|
|
519
|
-
const
|
|
350
|
+
const fullCommand = this.buildCommand(command, session, { env: options.env, cwd: options.cwd });
|
|
520
351
|
return new Promise((resolve, reject) => {
|
|
521
|
-
session.client.
|
|
352
|
+
session.client.exec(fullCommand, {
|
|
353
|
+
pty: {
|
|
354
|
+
rows: options.rows || 24,
|
|
355
|
+
cols: options.cols || 80,
|
|
356
|
+
term: options.term || 'xterm-256color',
|
|
357
|
+
},
|
|
358
|
+
}, (err, stream) => {
|
|
522
359
|
if (err) {
|
|
523
|
-
reject(new Error(`
|
|
524
|
-
return;
|
|
360
|
+
reject(new Error(`PTY exec failed: ${err.message}`));
|
|
525
361
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
alias,
|
|
529
|
-
type: 'remote',
|
|
530
|
-
localHost,
|
|
531
|
-
localPort,
|
|
532
|
-
remoteHost,
|
|
533
|
-
remotePort,
|
|
534
|
-
createdAt: Date.now(),
|
|
535
|
-
active: true,
|
|
536
|
-
};
|
|
537
|
-
this.forwardSessions.set(forwardId, fwdSession);
|
|
538
|
-
// 确保该 session 有共享的 tcp dispatcher
|
|
539
|
-
this.ensureTcpDispatcher(session, alias);
|
|
540
|
-
resolve(forwardId);
|
|
541
|
-
});
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* 关闭端口转发
|
|
546
|
-
*/
|
|
547
|
-
forwardClose(forwardId) {
|
|
548
|
-
const fwdSession = this.forwardSessions.get(forwardId);
|
|
549
|
-
if (!fwdSession) {
|
|
550
|
-
return false;
|
|
551
|
-
}
|
|
552
|
-
fwdSession.active = false;
|
|
553
|
-
if (fwdSession.type === 'local' && fwdSession.server) {
|
|
554
|
-
try {
|
|
555
|
-
fwdSession.server.close();
|
|
556
|
-
}
|
|
557
|
-
catch { /* 忽略清理异常 */
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
else if (fwdSession.type === 'remote') {
|
|
561
|
-
const session = this.sessions.get(fwdSession.alias);
|
|
562
|
-
if (session) {
|
|
563
|
-
try {
|
|
564
|
-
session.client.unforwardIn(fwdSession.remoteHost, fwdSession.remotePort);
|
|
565
|
-
}
|
|
566
|
-
catch { /* 忽略清理异常 */
|
|
362
|
+
else {
|
|
363
|
+
resolve(stream);
|
|
567
364
|
}
|
|
568
|
-
// 检查是否需要移除共享 dispatcher
|
|
569
|
-
this.removeTcpDispatcherIfEmpty(session, fwdSession.alias);
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
this.forwardSessions.delete(forwardId);
|
|
573
|
-
return true;
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* 列出所有端口转发
|
|
577
|
-
*/
|
|
578
|
-
forwardList() {
|
|
579
|
-
const result = [];
|
|
580
|
-
for (const [id, fwd] of this.forwardSessions) {
|
|
581
|
-
result.push({
|
|
582
|
-
id,
|
|
583
|
-
alias: fwd.alias,
|
|
584
|
-
type: fwd.type,
|
|
585
|
-
localHost: fwd.localHost,
|
|
586
|
-
localPort: fwd.localPort,
|
|
587
|
-
remoteHost: fwd.remoteHost,
|
|
588
|
-
remotePort: fwd.remotePort,
|
|
589
|
-
createdAt: fwd.createdAt,
|
|
590
|
-
active: fwd.active,
|
|
591
365
|
});
|
|
592
|
-
}
|
|
593
|
-
return result;
|
|
366
|
+
});
|
|
594
367
|
}
|
|
595
368
|
ensurePersistDir() {
|
|
596
369
|
const dir = path.dirname(this.persistPath);
|
|
@@ -625,6 +398,50 @@ export class SessionManager {
|
|
|
625
398
|
isAlive(session) {
|
|
626
399
|
return session.connected;
|
|
627
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* 调度自动重连(支持多次重试)
|
|
403
|
+
*/
|
|
404
|
+
scheduleReconnect(alias) {
|
|
405
|
+
const session = this.sessions.get(alias);
|
|
406
|
+
if (!session || session.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
session.reconnectAttempts++;
|
|
410
|
+
const attempt = session.reconnectAttempts;
|
|
411
|
+
setTimeout(async () => {
|
|
412
|
+
try {
|
|
413
|
+
await this.reconnect(alias);
|
|
414
|
+
}
|
|
415
|
+
catch (err) {
|
|
416
|
+
console.error(`Auto-reconnect failed for ${alias} (${attempt}/${this.maxReconnectAttempts}):`, err.message);
|
|
417
|
+
// reconnect 失败后继续调度下一次尝试
|
|
418
|
+
this.scheduleReconnect(alias);
|
|
419
|
+
}
|
|
420
|
+
}, this.reconnectDelay);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* 根据错误类型生成排查建议
|
|
424
|
+
*/
|
|
425
|
+
diagnoseConnectionError(err) {
|
|
426
|
+
const msg = err.message || '';
|
|
427
|
+
const code = err.code || '';
|
|
428
|
+
if (code === 'ECONNREFUSED' || msg.includes('ECONNREFUSED')) {
|
|
429
|
+
return 'check if SSH service is running on the target host';
|
|
430
|
+
}
|
|
431
|
+
if (code === 'ETIMEDOUT' || msg.includes('ETIMEDOUT') || msg.includes('Timed out')) {
|
|
432
|
+
return 'check host reachability, firewall rules, or try a jump host';
|
|
433
|
+
}
|
|
434
|
+
if (code === 'ENOTFOUND' || msg.includes('ENOTFOUND') || msg.includes('getaddrinfo')) {
|
|
435
|
+
return 'hostname cannot be resolved, check DNS or use IP address';
|
|
436
|
+
}
|
|
437
|
+
if (msg.includes('All configured authentication methods failed')) {
|
|
438
|
+
return 'check password, key path, or key permissions (chmod 600)';
|
|
439
|
+
}
|
|
440
|
+
if (msg.includes('ECONNRESET') || msg.includes('Connection reset')) {
|
|
441
|
+
return 'connection was reset by the remote host';
|
|
442
|
+
}
|
|
443
|
+
return '';
|
|
444
|
+
}
|
|
628
445
|
/**
|
|
629
446
|
* 转义 shell 参数(使用单引号方式)
|
|
630
447
|
*/
|
|
@@ -697,84 +514,6 @@ export class SessionManager {
|
|
|
697
514
|
// 忽略写入错误
|
|
698
515
|
}
|
|
699
516
|
}
|
|
700
|
-
/**
|
|
701
|
-
* 生成 PTY 会话 ID
|
|
702
|
-
*/
|
|
703
|
-
generatePtyId() {
|
|
704
|
-
return `pty_${++this.ptyIdCounter}_${Date.now()}`;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* 从终端仿真器获取当前屏幕内容
|
|
708
|
-
*/
|
|
709
|
-
getScreenContent(terminal) {
|
|
710
|
-
const buffer = terminal.buffer.active;
|
|
711
|
-
const lines = [];
|
|
712
|
-
for (let i = 0; i < terminal.rows; i++) {
|
|
713
|
-
const line = buffer.getLine(i);
|
|
714
|
-
if (line) {
|
|
715
|
-
lines.push(line.translateToString(true));
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
// 移除尾部空行
|
|
719
|
-
while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
|
|
720
|
-
lines.pop();
|
|
721
|
-
}
|
|
722
|
-
return lines.join('\n');
|
|
723
|
-
}
|
|
724
|
-
/**
|
|
725
|
-
* 生成端口转发 ID
|
|
726
|
-
*/
|
|
727
|
-
generateForwardId() {
|
|
728
|
-
return `fwd_${++this.forwardIdCounter}_${Date.now()}`;
|
|
729
|
-
}
|
|
730
|
-
/**
|
|
731
|
-
* 确保 SSH session 有共享的 tcp connection dispatcher
|
|
732
|
-
* 所有 remote forward 共用一个 dispatcher,根据 destIP/destPort 路由
|
|
733
|
-
*/
|
|
734
|
-
ensureTcpDispatcher(session, alias) {
|
|
735
|
-
if (session.tcpDispatcher) {
|
|
736
|
-
return; // 已存在
|
|
737
|
-
}
|
|
738
|
-
const dispatcher = (info, accept, rejectConn) => {
|
|
739
|
-
// 查找匹配的 remote forward
|
|
740
|
-
for (const fwd of this.forwardSessions.values()) {
|
|
741
|
-
if (fwd.type === 'remote' &&
|
|
742
|
-
fwd.active &&
|
|
743
|
-
fwd.alias === alias &&
|
|
744
|
-
fwd.remoteHost === info.destIP &&
|
|
745
|
-
fwd.remotePort === info.destPort) {
|
|
746
|
-
// 找到匹配的 forward,建立连接
|
|
747
|
-
const stream = accept();
|
|
748
|
-
const socket = net.createConnection(fwd.localPort, fwd.localHost);
|
|
749
|
-
socket.pipe(stream).pipe(socket);
|
|
750
|
-
socket.on('error', () => stream.close());
|
|
751
|
-
stream.on('error', () => socket.destroy());
|
|
752
|
-
return;
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
// 没有匹配的 forward,拒绝连接
|
|
756
|
-
rejectConn();
|
|
757
|
-
};
|
|
758
|
-
session.tcpDispatcher = dispatcher;
|
|
759
|
-
session.client.on('tcp connection', dispatcher);
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* 移除 SSH session 的 tcp dispatcher(当没有 remote forward 时)
|
|
763
|
-
*/
|
|
764
|
-
removeTcpDispatcherIfEmpty(session, alias) {
|
|
765
|
-
if (!session.tcpDispatcher) {
|
|
766
|
-
return;
|
|
767
|
-
}
|
|
768
|
-
// 检查是否还有该 session 的 remote forward
|
|
769
|
-
for (const fwd of this.forwardSessions.values()) {
|
|
770
|
-
if (fwd.type === 'remote' && fwd.alias === alias && fwd.active) {
|
|
771
|
-
return; // 还有活跃的 remote forward
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
// 没有了,移除 dispatcher
|
|
775
|
-
session.client.removeListener('tcp connection', session.tcpDispatcher);
|
|
776
|
-
session.tcpDispatcher = undefined;
|
|
777
|
-
}
|
|
778
517
|
}
|
|
779
518
|
// 全局单例
|
|
780
519
|
export const sessionManager = new SessionManager();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrokine/mcp-ssh",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A comprehensive SSH MCP Server for Claude Code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"build": "tsc",
|
|
12
12
|
"start": "node dist/index.js",
|
|
13
13
|
"dev": "tsc && node dist/index.js",
|
|
14
|
-
"clean": "
|
|
14
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
15
15
|
"prepublishOnly": "npm run build"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
package/src/file-ops.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import {execSync} from 'child_process'
|
|
6
6
|
import * as fs from 'fs'
|
|
7
|
+
import * as os from 'os'
|
|
7
8
|
import * as path from 'path'
|
|
8
9
|
import {SFTPWrapper, Stats} from 'ssh2'
|
|
9
10
|
import {sessionManager} from './session-manager.js'
|
|
@@ -382,7 +383,8 @@ async function syncWithRsync(
|
|
|
382
383
|
// 检查本地是否有 rsync
|
|
383
384
|
let hasLocalRsync = false
|
|
384
385
|
try {
|
|
385
|
-
|
|
386
|
+
const cmd = os.platform() === 'win32' ? 'where rsync' : 'which rsync'
|
|
387
|
+
execSync(cmd, {stdio: 'pipe'})
|
|
386
388
|
hasLocalRsync = true
|
|
387
389
|
} catch {
|
|
388
390
|
}
|