@pyrokine/mcp-ssh 2.3.1 → 2.4.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 +103 -21
- package/README_zh.md +73 -20
- package/dist/command-risk.d.ts +7 -0
- package/dist/command-risk.js +106 -0
- package/dist/directory-verification.d.ts +43 -0
- package/dist/directory-verification.js +485 -0
- package/dist/file-ops.d.ts +56 -13
- package/dist/file-ops.js +531 -145
- package/dist/forward-manager.d.ts +5 -2
- package/dist/forward-manager.js +133 -19
- package/dist/index.js +2 -1
- package/dist/operation-manager.d.ts +52 -0
- package/dist/operation-manager.js +402 -0
- package/dist/pty-manager.d.ts +1 -0
- package/dist/pty-manager.js +11 -7
- package/dist/session-manager.d.ts +21 -10
- package/dist/session-manager.js +328 -75
- package/dist/tools/connection.js +48 -8
- package/dist/tools/exec.js +7 -66
- package/dist/tools/file.js +455 -81
- package/dist/tools/forward.js +12 -4
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/operation.d.ts +2 -0
- package/dist/tools/operation.js +126 -0
- package/dist/tools/utils.d.ts +2 -2
- package/dist/transfer-outcome.d.ts +12 -0
- package/dist/transfer-outcome.js +72 -0
- package/dist/types.d.ts +94 -6
- package/package.json +11 -9
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 负责本地/远程端口转发的完整生命周期
|
|
5
5
|
*/
|
|
6
6
|
import type { Client } from 'ssh2';
|
|
7
|
-
import type { PortForwardInfo } from './types.js';
|
|
7
|
+
import type { ForwardCloseOptions, ForwardCloseResult, PortForwardInfo } from './types.js';
|
|
8
8
|
/** 端口转发所需的外部依赖 */
|
|
9
9
|
export interface ForwardDependencies {
|
|
10
10
|
/** 获取 SSH client */
|
|
@@ -23,10 +23,13 @@ export declare class ForwardManager {
|
|
|
23
23
|
localPort: number;
|
|
24
24
|
}>;
|
|
25
25
|
forwardRemote(deps: ForwardDependencies, alias: string, remotePort: number, localHost: string, localPort: number, remoteHost?: string): Promise<string>;
|
|
26
|
-
close(forwardId: string, deps?: ForwardDependencies):
|
|
26
|
+
close(forwardId: string, deps?: ForwardDependencies, options?: ForwardCloseOptions): Promise<ForwardCloseResult>;
|
|
27
27
|
/** 关闭指定 alias 的所有转发(disconnect 时调用,不需要 unforwardIn) */
|
|
28
28
|
closeByAlias(alias: string): void;
|
|
29
29
|
list(): PortForwardInfo[];
|
|
30
|
+
private closeLocalListener;
|
|
31
|
+
private closeRemoteListener;
|
|
32
|
+
private withTimeout;
|
|
30
33
|
private startIdleSweeper;
|
|
31
34
|
private sweepIdle;
|
|
32
35
|
/**
|
package/dist/forward-manager.js
CHANGED
|
@@ -27,8 +27,12 @@ export class ForwardManager {
|
|
|
27
27
|
fwd.activeConnections += 1;
|
|
28
28
|
fwd.lastActivityAt = Date.now();
|
|
29
29
|
}
|
|
30
|
+
if (fwd) {
|
|
31
|
+
fwd.connections.add(socket);
|
|
32
|
+
}
|
|
30
33
|
const onClose = () => {
|
|
31
34
|
if (fwd) {
|
|
35
|
+
fwd.connections.delete(socket);
|
|
32
36
|
fwd.activeConnections = Math.max(0, fwd.activeConnections - 1);
|
|
33
37
|
fwd.lastActivityAt = Date.now();
|
|
34
38
|
}
|
|
@@ -61,6 +65,9 @@ export class ForwardManager {
|
|
|
61
65
|
active: true,
|
|
62
66
|
activeConnections: 0,
|
|
63
67
|
lastActivityAt: Date.now(),
|
|
68
|
+
connections: new Set(),
|
|
69
|
+
listenerReleased: false,
|
|
70
|
+
remoteUnforwarded: false,
|
|
64
71
|
};
|
|
65
72
|
this.sessions.set(forwardId, session);
|
|
66
73
|
fwdState.ref = session;
|
|
@@ -90,47 +97,87 @@ export class ForwardManager {
|
|
|
90
97
|
active: true,
|
|
91
98
|
activeConnections: 0,
|
|
92
99
|
lastActivityAt: Date.now(),
|
|
100
|
+
connections: new Set(),
|
|
101
|
+
listenerReleased: false,
|
|
102
|
+
remoteUnforwarded: false,
|
|
93
103
|
});
|
|
94
104
|
this.ensureTcpDispatcher(alias, client);
|
|
95
105
|
resolve(forwardId);
|
|
96
106
|
});
|
|
97
107
|
});
|
|
98
108
|
}
|
|
99
|
-
close(forwardId, deps) {
|
|
109
|
+
async close(forwardId, deps, options = {}) {
|
|
110
|
+
const mode = options.mode ?? 'graceful';
|
|
111
|
+
const timeoutMs = options.timeoutMs ?? 5000;
|
|
100
112
|
const fwd = this.sessions.get(forwardId);
|
|
101
113
|
if (!fwd) {
|
|
102
|
-
return
|
|
114
|
+
return {
|
|
115
|
+
success: false,
|
|
116
|
+
forwardId,
|
|
117
|
+
closeMode: mode,
|
|
118
|
+
listenerReleased: false,
|
|
119
|
+
remoteUnforwarded: false,
|
|
120
|
+
activeConnections: 0,
|
|
121
|
+
retryable: false,
|
|
122
|
+
error: 'Forward not found',
|
|
123
|
+
};
|
|
103
124
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
fwd.server.close();
|
|
108
|
-
}
|
|
109
|
-
catch (e) {
|
|
110
|
-
console.warn(`Forward ${forwardId} server close failed:`, e.message);
|
|
125
|
+
if (mode === 'force') {
|
|
126
|
+
for (const connection of fwd.connections) {
|
|
127
|
+
connection.destroy();
|
|
111
128
|
}
|
|
112
129
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
130
|
+
try {
|
|
131
|
+
if (!fwd.closePromise) {
|
|
132
|
+
fwd.closePromise =
|
|
133
|
+
fwd.type === 'local' ? this.closeLocalListener(fwd) : this.closeRemoteListener(fwd, deps);
|
|
134
|
+
fwd.closePromise.catch(() => {
|
|
135
|
+
fwd.closePromise = undefined;
|
|
136
|
+
});
|
|
117
137
|
}
|
|
118
|
-
|
|
119
|
-
|
|
138
|
+
await this.withTimeout(fwd.closePromise, timeoutMs);
|
|
139
|
+
fwd.active = false;
|
|
140
|
+
this.sessions.delete(forwardId);
|
|
141
|
+
if (fwd.type === 'remote') {
|
|
142
|
+
this.removeTcpDispatcherIfEmpty(fwd.alias);
|
|
120
143
|
}
|
|
121
|
-
|
|
144
|
+
return {
|
|
145
|
+
success: true,
|
|
146
|
+
forwardId,
|
|
147
|
+
type: fwd.type,
|
|
148
|
+
closeMode: mode,
|
|
149
|
+
listenerReleased: fwd.listenerReleased,
|
|
150
|
+
remoteUnforwarded: fwd.remoteUnforwarded,
|
|
151
|
+
activeConnections: fwd.activeConnections,
|
|
152
|
+
retryable: false,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
return {
|
|
157
|
+
success: false,
|
|
158
|
+
forwardId,
|
|
159
|
+
type: fwd.type,
|
|
160
|
+
closeMode: mode,
|
|
161
|
+
listenerReleased: fwd.listenerReleased,
|
|
162
|
+
remoteUnforwarded: fwd.remoteUnforwarded,
|
|
163
|
+
activeConnections: fwd.activeConnections,
|
|
164
|
+
retryable: true,
|
|
165
|
+
error: error instanceof Error ? error.message : String(error),
|
|
166
|
+
};
|
|
122
167
|
}
|
|
123
|
-
this.sessions.delete(forwardId);
|
|
124
|
-
return true;
|
|
125
168
|
}
|
|
126
169
|
/** 关闭指定 alias 的所有转发(disconnect 时调用,不需要 unforwardIn) */
|
|
127
170
|
closeByAlias(alias) {
|
|
128
171
|
for (const [id, fwd] of this.sessions) {
|
|
129
172
|
if (fwd.alias === alias) {
|
|
130
173
|
fwd.active = false;
|
|
174
|
+
for (const connection of fwd.connections) {
|
|
175
|
+
connection.destroy();
|
|
176
|
+
}
|
|
131
177
|
if (fwd.type === 'local' && fwd.server) {
|
|
132
178
|
try {
|
|
133
179
|
fwd.server.close();
|
|
180
|
+
fwd.listenerReleased = !fwd.server.listening;
|
|
134
181
|
}
|
|
135
182
|
catch (e) {
|
|
136
183
|
console.warn(`Forward ${id} server close failed:`, e.message);
|
|
@@ -168,6 +215,65 @@ export class ForwardManager {
|
|
|
168
215
|
}
|
|
169
216
|
return result;
|
|
170
217
|
}
|
|
218
|
+
closeLocalListener(fwd) {
|
|
219
|
+
const server = fwd.server;
|
|
220
|
+
if (!server) {
|
|
221
|
+
return Promise.reject(new Error('Local forward listener is unavailable'));
|
|
222
|
+
}
|
|
223
|
+
return new Promise((resolve, reject) => {
|
|
224
|
+
try {
|
|
225
|
+
server.close((error) => {
|
|
226
|
+
if (error) {
|
|
227
|
+
reject(error);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
resolve();
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
fwd.listenerReleased = !server.listening;
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
fwd.listenerReleased = !server.listening;
|
|
237
|
+
reject(error);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
closeRemoteListener(fwd, deps) {
|
|
242
|
+
if (!deps) {
|
|
243
|
+
return Promise.reject(new Error('SSH session is unavailable for remote unforward'));
|
|
244
|
+
}
|
|
245
|
+
return new Promise((resolve, reject) => {
|
|
246
|
+
let client;
|
|
247
|
+
try {
|
|
248
|
+
client = deps.getClient(fwd.alias);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
reject(error);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
client.unforwardIn(fwd.remoteHost, fwd.remotePort, (error) => {
|
|
255
|
+
if (error) {
|
|
256
|
+
reject(error);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
fwd.remoteUnforwarded = true;
|
|
260
|
+
resolve();
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
withTimeout(promise, timeoutMs) {
|
|
266
|
+
return new Promise((resolve, reject) => {
|
|
267
|
+
const timer = setTimeout(() => reject(new Error(`Forward close timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
268
|
+
promise.then((value) => {
|
|
269
|
+
clearTimeout(timer);
|
|
270
|
+
resolve(value);
|
|
271
|
+
}, (error) => {
|
|
272
|
+
clearTimeout(timer);
|
|
273
|
+
reject(error);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
}
|
|
171
277
|
startIdleSweeper() {
|
|
172
278
|
if (this.idleSweeper) {
|
|
173
279
|
return;
|
|
@@ -185,7 +291,11 @@ export class ForwardManager {
|
|
|
185
291
|
}
|
|
186
292
|
if (fwd.activeConnections === 0 && now - fwd.lastActivityAt > this.idleTimeoutMs) {
|
|
187
293
|
console.warn(`[mcp-ssh] Forward ${id} (${fwd.type}, alias=${fwd.alias}) idle ${Math.round((now - fwd.lastActivityAt) / 1000)}s with 0 connections > ${Math.round(this.idleTimeoutMs / 1000)}s, auto-closing`);
|
|
188
|
-
this.close(id, this.dependencies ?? undefined)
|
|
294
|
+
void this.close(id, this.dependencies ?? undefined).then((result) => {
|
|
295
|
+
if (!result.success) {
|
|
296
|
+
console.warn(`[mcp-ssh] Forward ${id} auto-close failed: ${result.error}`);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
189
299
|
}
|
|
190
300
|
}
|
|
191
301
|
}
|
|
@@ -206,6 +316,8 @@ export class ForwardManager {
|
|
|
206
316
|
fwd.remotePort === info.destPort) {
|
|
207
317
|
const stream = accept();
|
|
208
318
|
const socket = net.createConnection(fwd.localPort, fwd.localHost);
|
|
319
|
+
fwd.connections.add(stream);
|
|
320
|
+
fwd.connections.add(socket);
|
|
209
321
|
fwd.activeConnections += 1;
|
|
210
322
|
fwd.lastActivityAt = Date.now();
|
|
211
323
|
let closed = false;
|
|
@@ -214,6 +326,8 @@ export class ForwardManager {
|
|
|
214
326
|
return;
|
|
215
327
|
}
|
|
216
328
|
closed = true;
|
|
329
|
+
fwd.connections.delete(stream);
|
|
330
|
+
fwd.connections.delete(socket);
|
|
217
331
|
fwd.activeConnections = Math.max(0, fwd.activeConnections - 1);
|
|
218
332
|
fwd.lastActivityAt = Date.now();
|
|
219
333
|
};
|
package/dist/index.js
CHANGED
|
@@ -17,12 +17,13 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
|
17
17
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
18
18
|
import { readFileSync } from 'node:fs';
|
|
19
19
|
import { sessionManager } from './session-manager.js';
|
|
20
|
-
import { registerConnectionTools, registerExecTools, registerFileTools, registerForwardTools, registerPtyTools, } from './tools/index.js';
|
|
20
|
+
import { registerConnectionTools, registerExecTools, registerFileTools, registerForwardTools, registerOperationTools, registerPtyTools, } from './tools/index.js';
|
|
21
21
|
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
22
22
|
const server = new McpServer({ name: 'mcp-ssh', version: pkg.version }, { capabilities: { tools: {} } });
|
|
23
23
|
registerConnectionTools(server);
|
|
24
24
|
registerExecTools(server);
|
|
25
25
|
registerFileTools(server);
|
|
26
|
+
registerOperationTools(server);
|
|
26
27
|
registerPtyTools(server);
|
|
27
28
|
registerForwardTools(server);
|
|
28
29
|
async function cleanup() {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ClientChannel } from 'ssh2';
|
|
2
|
+
import type { OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions } from './types.js';
|
|
3
|
+
export declare const DEFAULT_OPERATION_MAX_OUTPUT_BYTES: number;
|
|
4
|
+
export declare const HARD_OPERATION_MAX_OUTPUT_BYTES: number;
|
|
5
|
+
export declare const DEFAULT_OPERATION_READ_BYTES: number;
|
|
6
|
+
export declare const HARD_OPERATION_READ_BYTES: number;
|
|
7
|
+
export declare const DEFAULT_OPERATION_RETENTION_MS: number;
|
|
8
|
+
export declare const MAX_OPERATION_RETENTION_MS: number;
|
|
9
|
+
export interface OperationDependencies {
|
|
10
|
+
openStream(alias: string, command: string, marker: string, options: OperationStartOptions): Promise<ClientChannel>;
|
|
11
|
+
cancelRemote(alias: string, pid: number, marker: string, processGroup: boolean, options: OperationStartOptions): Promise<{
|
|
12
|
+
success: boolean;
|
|
13
|
+
error?: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export declare class OperationManager {
|
|
17
|
+
private readonly dependencies;
|
|
18
|
+
private readonly now;
|
|
19
|
+
private readonly operations;
|
|
20
|
+
private readonly sweeper;
|
|
21
|
+
constructor(dependencies: OperationDependencies, now?: () => number);
|
|
22
|
+
start(alias: string, command: string, options?: OperationStartOptions): Promise<OperationInfo>;
|
|
23
|
+
status(operationId: string): OperationInfo;
|
|
24
|
+
read(operationId: string, options?: {
|
|
25
|
+
stdoutOffset?: number;
|
|
26
|
+
stderrOffset?: number;
|
|
27
|
+
maxBytes?: number;
|
|
28
|
+
}): OperationReadResult;
|
|
29
|
+
cancel(operationId: string): Promise<OperationCancelResult>;
|
|
30
|
+
list(alias?: string): OperationInfo[];
|
|
31
|
+
markAliasDisconnected(alias: string): void;
|
|
32
|
+
sweepExpired(): void;
|
|
33
|
+
close(): void;
|
|
34
|
+
private attachStream;
|
|
35
|
+
private finishPendingTermination;
|
|
36
|
+
private finishStreamError;
|
|
37
|
+
private finishStreamClose;
|
|
38
|
+
private consumeMarker;
|
|
39
|
+
private appendOutput;
|
|
40
|
+
private finish;
|
|
41
|
+
private info;
|
|
42
|
+
private requireOperation;
|
|
43
|
+
private normalizeMaxOutputBytes;
|
|
44
|
+
private normalizeReadBytes;
|
|
45
|
+
private normalizeRetentionMs;
|
|
46
|
+
private normalizeOffset;
|
|
47
|
+
private ensureUtf8Boundary;
|
|
48
|
+
private utf8SafeEnd;
|
|
49
|
+
private trimIncompleteUtf8Sequence;
|
|
50
|
+
private utf8SequenceLength;
|
|
51
|
+
private isUtf8ContinuationByte;
|
|
52
|
+
}
|