@pyrokine/mcp-ssh 2.3.2 → 2.4.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 +141 -27
- package/README_zh.md +76 -27
- 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 +77 -14
- package/dist/file-ops.js +839 -205
- package/dist/forward-manager.d.ts +22 -3
- package/dist/forward-manager.js +501 -85
- package/dist/index.js +2 -1
- package/dist/operation-manager.d.ts +64 -0
- package/dist/operation-manager.js +545 -0
- package/dist/pty-manager.d.ts +1 -0
- package/dist/pty-manager.js +11 -7
- package/dist/session-manager.d.ts +47 -11
- package/dist/session-manager.js +564 -114
- package/dist/tools/connection.js +58 -17
- package/dist/tools/exec.js +7 -66
- package/dist/tools/file.d.ts +1 -0
- package/dist/tools/file.js +575 -119
- package/dist/tools/forward.js +18 -8
- 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 +134 -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 +95 -6
- package/package.json +10 -8
package/dist/forward-manager.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* 负责本地/远程端口转发的完整生命周期
|
|
5
5
|
*/
|
|
6
6
|
import * as net from 'net';
|
|
7
|
+
const MAX_PENDING_CHANNEL_OPENS_PER_ALIAS = 32;
|
|
7
8
|
export class ForwardManager {
|
|
8
9
|
sessions = new Map();
|
|
9
10
|
dispatchers = new Map(); // alias → dispatcher
|
|
@@ -17,126 +18,337 @@ export class ForwardManager {
|
|
|
17
18
|
}
|
|
18
19
|
async forwardLocal(deps, alias, localPort, remoteHost, remotePort, localHost = '127.0.0.1') {
|
|
19
20
|
this.dependencies = deps;
|
|
21
|
+
for (const existing of this.sessions.values()) {
|
|
22
|
+
if (existing.alias === alias &&
|
|
23
|
+
existing.type === 'local' &&
|
|
24
|
+
existing.lifecycle === 'closing' &&
|
|
25
|
+
existing.pendingChannelOpens > 0) {
|
|
26
|
+
throw new Error(`Alias '${alias}' still has an unresolved SSH channel open; retry closing the existing forward or disconnect the alias`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
20
29
|
const client = deps.getClient(alias);
|
|
21
30
|
const forwardId = this.generateId();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
const server = net.createServer();
|
|
32
|
+
const session = {
|
|
33
|
+
id: forwardId,
|
|
34
|
+
alias,
|
|
35
|
+
type: 'local',
|
|
36
|
+
localHost,
|
|
37
|
+
localPort,
|
|
38
|
+
remoteHost,
|
|
39
|
+
remotePort,
|
|
40
|
+
server,
|
|
41
|
+
client,
|
|
42
|
+
createdAt: Date.now(),
|
|
43
|
+
active: false,
|
|
44
|
+
lifecycle: 'pending',
|
|
45
|
+
activeConnections: 0,
|
|
46
|
+
lastActivityAt: Date.now(),
|
|
47
|
+
connections: new Set(),
|
|
48
|
+
connectionDrainWaiters: new Set(),
|
|
49
|
+
pendingChannelOpens: 0,
|
|
50
|
+
channelOpenDrainWaiters: new Set(),
|
|
51
|
+
listenerReleased: false,
|
|
52
|
+
remoteUnforwarded: false,
|
|
53
|
+
};
|
|
54
|
+
this.sessions.set(forwardId, session);
|
|
55
|
+
server.on('connection', (socket) => {
|
|
56
|
+
if (!this.isCurrent(session) || session.lifecycle !== 'active' || !session.active) {
|
|
57
|
+
socket.destroy();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (this.pendingChannelOpensForAlias(alias) >= MAX_PENDING_CHANNEL_OPENS_PER_ALIAS) {
|
|
61
|
+
console.warn(`Local forward ${session.id} rejected a connection because alias '${alias}' already has ${MAX_PENDING_CHANNEL_OPENS_PER_ALIAS} pending SSH channel opens`);
|
|
62
|
+
socket.destroy();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
session.activeConnections += 1;
|
|
66
|
+
session.lastActivityAt = Date.now();
|
|
67
|
+
session.connections.add(socket);
|
|
68
|
+
let stream;
|
|
69
|
+
let closed = false;
|
|
70
|
+
const closeConnection = () => {
|
|
71
|
+
if (closed) {
|
|
72
|
+
return;
|
|
29
73
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
74
|
+
closed = true;
|
|
75
|
+
session.connections.delete(socket);
|
|
76
|
+
if (stream) {
|
|
77
|
+
session.connections.delete(stream);
|
|
78
|
+
if (!stream.destroyed) {
|
|
79
|
+
stream.destroy();
|
|
34
80
|
}
|
|
35
|
-
}
|
|
36
|
-
socket.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
81
|
+
}
|
|
82
|
+
if (!socket.destroyed) {
|
|
83
|
+
socket.destroy();
|
|
84
|
+
}
|
|
85
|
+
session.activeConnections = Math.max(0, session.activeConnections - 1);
|
|
86
|
+
session.lastActivityAt = Date.now();
|
|
87
|
+
if (session.activeConnections === 0) {
|
|
88
|
+
for (const resolve of session.connectionDrainWaiters) {
|
|
89
|
+
resolve();
|
|
90
|
+
}
|
|
91
|
+
session.connectionDrainWaiters.clear();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
socket.once('close', closeConnection);
|
|
95
|
+
socket.once('error', closeConnection);
|
|
96
|
+
const settleChannelOpen = () => {
|
|
97
|
+
session.pendingChannelOpens = Math.max(0, session.pendingChannelOpens - 1);
|
|
98
|
+
if (session.pendingChannelOpens === 0) {
|
|
99
|
+
for (const resolve of session.channelOpenDrainWaiters) {
|
|
100
|
+
resolve();
|
|
101
|
+
}
|
|
102
|
+
session.channelOpenDrainWaiters.clear();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
session.pendingChannelOpens += 1;
|
|
106
|
+
try {
|
|
107
|
+
client.forwardOut(socket.remoteAddress || '127.0.0.1', socket.remotePort || 0, remoteHost, remotePort, (err, connectedStream) => {
|
|
108
|
+
settleChannelOpen();
|
|
109
|
+
if (err || !this.isCurrent(session) || session.lifecycle !== 'active' || !session.active) {
|
|
110
|
+
connectedStream?.destroy();
|
|
111
|
+
closeConnection();
|
|
40
112
|
return;
|
|
41
113
|
}
|
|
114
|
+
stream = connectedStream;
|
|
115
|
+
session.connections.add(stream);
|
|
116
|
+
stream.once('close', closeConnection);
|
|
117
|
+
stream.once('error', closeConnection);
|
|
42
118
|
socket.pipe(stream).pipe(socket);
|
|
43
119
|
});
|
|
44
|
-
}
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
settleChannelOpen();
|
|
123
|
+
closeConnection();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
session.rejectCreation = reject;
|
|
45
128
|
server.on('error', (err) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
localHost,
|
|
56
|
-
localPort: actualPort,
|
|
57
|
-
remoteHost,
|
|
58
|
-
remotePort,
|
|
59
|
-
server,
|
|
60
|
-
createdAt: Date.now(),
|
|
61
|
-
active: true,
|
|
62
|
-
activeConnections: 0,
|
|
63
|
-
lastActivityAt: Date.now(),
|
|
64
|
-
};
|
|
65
|
-
this.sessions.set(forwardId, session);
|
|
66
|
-
fwdState.ref = session;
|
|
67
|
-
resolve({ forwardId, localPort: actualPort });
|
|
129
|
+
if (!this.isCurrent(session)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (session.lifecycle === 'pending') {
|
|
133
|
+
this.failPendingCreation(session, new Error(`Local forward failed: ${err.message}`));
|
|
134
|
+
}
|
|
135
|
+
else if (session.lifecycle !== 'closed') {
|
|
136
|
+
console.warn(`Local forward ${session.id} listener error: ${err.message}`);
|
|
137
|
+
}
|
|
68
138
|
});
|
|
139
|
+
try {
|
|
140
|
+
server.listen(localPort, localHost, () => {
|
|
141
|
+
if (!this.isCurrent(session) || session.lifecycle !== 'pending') {
|
|
142
|
+
this.closeServerAfterCancelledCreation(server);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const addr = server.address();
|
|
146
|
+
const actualPort = addr && typeof addr === 'object' && typeof addr.port === 'number' ? addr.port : localPort;
|
|
147
|
+
session.localPort = actualPort;
|
|
148
|
+
session.lifecycle = 'active';
|
|
149
|
+
session.active = true;
|
|
150
|
+
session.rejectCreation = undefined;
|
|
151
|
+
resolve({ forwardId, localPort: actualPort });
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
this.failPendingCreation(session, error instanceof Error ? error : new Error(String(error)));
|
|
156
|
+
}
|
|
69
157
|
});
|
|
70
158
|
}
|
|
71
159
|
async forwardRemote(deps, alias, remotePort, localHost, localPort, remoteHost = '127.0.0.1') {
|
|
72
160
|
this.dependencies = deps;
|
|
73
161
|
const client = deps.getClient(alias);
|
|
74
162
|
const forwardId = this.generateId();
|
|
163
|
+
const session = {
|
|
164
|
+
id: forwardId,
|
|
165
|
+
alias,
|
|
166
|
+
type: 'remote',
|
|
167
|
+
localHost,
|
|
168
|
+
localPort,
|
|
169
|
+
remoteHost,
|
|
170
|
+
remotePort,
|
|
171
|
+
client,
|
|
172
|
+
createdAt: Date.now(),
|
|
173
|
+
active: false,
|
|
174
|
+
lifecycle: 'pending',
|
|
175
|
+
activeConnections: 0,
|
|
176
|
+
lastActivityAt: Date.now(),
|
|
177
|
+
connections: new Set(),
|
|
178
|
+
connectionDrainWaiters: new Set(),
|
|
179
|
+
pendingChannelOpens: 0,
|
|
180
|
+
channelOpenDrainWaiters: new Set(),
|
|
181
|
+
listenerReleased: false,
|
|
182
|
+
remoteUnforwarded: false,
|
|
183
|
+
};
|
|
184
|
+
this.sessions.set(forwardId, session);
|
|
75
185
|
return new Promise((resolve, reject) => {
|
|
76
|
-
|
|
186
|
+
session.rejectCreation = reject;
|
|
187
|
+
client.forwardIn(remoteHost, remotePort, (err, allocatedPort) => {
|
|
77
188
|
if (err) {
|
|
78
|
-
|
|
189
|
+
if (this.isCurrent(session) && session.lifecycle === 'pending') {
|
|
190
|
+
this.failPendingCreation(session, new Error(`Remote forward failed: ${err.message}`));
|
|
191
|
+
}
|
|
192
|
+
else if (this.isCurrent(session) && session.lifecycle === 'closing') {
|
|
193
|
+
this.completePendingRemoteCloseWithoutListener(session);
|
|
194
|
+
}
|
|
79
195
|
return;
|
|
80
196
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
197
|
+
const actualPort = Number.isSafeInteger(allocatedPort) && allocatedPort > 0
|
|
198
|
+
? allocatedPort
|
|
199
|
+
: remotePort > 0
|
|
200
|
+
? remotePort
|
|
201
|
+
: undefined;
|
|
202
|
+
if (actualPort === undefined) {
|
|
203
|
+
const error = new Error('Remote forward did not return the dynamically allocated port');
|
|
204
|
+
if (this.isCurrent(session) && session.lifecycle === 'pending') {
|
|
205
|
+
this.failPendingCreation(session, error);
|
|
206
|
+
}
|
|
207
|
+
else if (this.isCurrent(session) && session.lifecycle === 'closing') {
|
|
208
|
+
this.failPendingRemoteClose(session, error);
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (this.isCurrent(session) && session.lifecycle === 'closing') {
|
|
213
|
+
session.remotePort = actualPort;
|
|
214
|
+
this.unforwardPendingRemoteClose(session, actualPort);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (!this.isCurrent(session) || session.lifecycle !== 'pending') {
|
|
218
|
+
this.unforwardCancelledCreation(session, actualPort);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
session.remotePort = actualPort;
|
|
222
|
+
session.lifecycle = 'active';
|
|
223
|
+
session.active = true;
|
|
224
|
+
session.rejectCreation = undefined;
|
|
94
225
|
this.ensureTcpDispatcher(alias, client);
|
|
95
|
-
resolve(forwardId);
|
|
226
|
+
resolve({ forwardId, remotePort: actualPort });
|
|
96
227
|
});
|
|
97
228
|
});
|
|
98
229
|
}
|
|
99
|
-
close(forwardId, deps) {
|
|
230
|
+
async close(forwardId, deps, options = {}) {
|
|
231
|
+
const mode = options.mode ?? 'graceful';
|
|
232
|
+
const timeoutMs = options.timeoutMs ?? 5000;
|
|
100
233
|
const fwd = this.sessions.get(forwardId);
|
|
101
234
|
if (!fwd) {
|
|
102
|
-
return
|
|
235
|
+
return {
|
|
236
|
+
success: false,
|
|
237
|
+
forwardId,
|
|
238
|
+
closeMode: mode,
|
|
239
|
+
listenerReleased: false,
|
|
240
|
+
remoteUnforwarded: false,
|
|
241
|
+
activeConnections: 0,
|
|
242
|
+
retryable: false,
|
|
243
|
+
error: 'Forward not found',
|
|
244
|
+
};
|
|
103
245
|
}
|
|
104
|
-
fwd.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
246
|
+
if (fwd.lifecycle === 'pending') {
|
|
247
|
+
if (fwd.type === 'local') {
|
|
248
|
+
this.cancelPendingCreation(fwd, 'Forward closed before listener creation completed');
|
|
249
|
+
return {
|
|
250
|
+
success: true,
|
|
251
|
+
forwardId,
|
|
252
|
+
type: fwd.type,
|
|
253
|
+
closeMode: mode,
|
|
254
|
+
listenerReleased: true,
|
|
255
|
+
remoteUnforwarded: false,
|
|
256
|
+
activeConnections: 0,
|
|
257
|
+
retryable: false,
|
|
258
|
+
};
|
|
108
259
|
}
|
|
109
|
-
|
|
110
|
-
|
|
260
|
+
this.beginPendingRemoteClose(fwd, 'Forward closed before listener creation completed');
|
|
261
|
+
}
|
|
262
|
+
if (mode === 'force') {
|
|
263
|
+
for (const connection of fwd.connections) {
|
|
264
|
+
connection.destroy();
|
|
111
265
|
}
|
|
112
266
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
267
|
+
try {
|
|
268
|
+
if (!fwd.closePromise) {
|
|
269
|
+
fwd.lifecycle = 'closing';
|
|
270
|
+
fwd.closePromise =
|
|
271
|
+
fwd.type === 'local' ? this.closeLocalListener(fwd) : this.closeRemoteListener(fwd, deps);
|
|
272
|
+
fwd.closePromise.catch(() => {
|
|
273
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
274
|
+
if (fwd.active) {
|
|
275
|
+
fwd.lifecycle = 'active';
|
|
276
|
+
}
|
|
277
|
+
fwd.closePromise = undefined;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
117
280
|
}
|
|
118
|
-
|
|
119
|
-
|
|
281
|
+
const channelOpenDrain = fwd.type === 'local' ? this.waitForChannelOpenDrain(fwd) : Promise.resolve();
|
|
282
|
+
const closeCompletion = mode === 'force'
|
|
283
|
+
? Promise.all([fwd.closePromise, this.waitForConnectionDrain(fwd), channelOpenDrain]).then(() => undefined)
|
|
284
|
+
: Promise.all([fwd.closePromise, channelOpenDrain]).then(() => undefined);
|
|
285
|
+
await this.withTimeout(closeCompletion, timeoutMs);
|
|
286
|
+
if (this.isCurrent(fwd) && fwd.lifecycle !== 'closed') {
|
|
287
|
+
fwd.active = false;
|
|
288
|
+
fwd.lifecycle = 'closed';
|
|
289
|
+
this.sessions.delete(forwardId);
|
|
290
|
+
if (fwd.type === 'remote') {
|
|
291
|
+
this.removeTcpDispatcherIfEmpty(fwd.alias);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
success: true,
|
|
296
|
+
forwardId,
|
|
297
|
+
type: fwd.type,
|
|
298
|
+
closeMode: mode,
|
|
299
|
+
listenerReleased: fwd.listenerReleased,
|
|
300
|
+
remoteUnforwarded: fwd.remoteUnforwarded,
|
|
301
|
+
activeConnections: fwd.activeConnections,
|
|
302
|
+
retryable: false,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
if (!this.isCurrent(fwd) || fwd.lifecycle === 'closed') {
|
|
307
|
+
return {
|
|
308
|
+
success: true,
|
|
309
|
+
forwardId,
|
|
310
|
+
type: fwd.type,
|
|
311
|
+
closeMode: mode,
|
|
312
|
+
listenerReleased: fwd.type === 'local' ? !fwd.server?.listening : fwd.listenerReleased,
|
|
313
|
+
remoteUnforwarded: fwd.remoteUnforwarded,
|
|
314
|
+
activeConnections: fwd.activeConnections,
|
|
315
|
+
retryable: false,
|
|
316
|
+
};
|
|
120
317
|
}
|
|
121
|
-
|
|
318
|
+
return {
|
|
319
|
+
success: false,
|
|
320
|
+
forwardId,
|
|
321
|
+
type: fwd.type,
|
|
322
|
+
closeMode: mode,
|
|
323
|
+
listenerReleased: fwd.listenerReleased,
|
|
324
|
+
remoteUnforwarded: fwd.remoteUnforwarded,
|
|
325
|
+
activeConnections: fwd.activeConnections,
|
|
326
|
+
retryable: true,
|
|
327
|
+
error: error instanceof Error ? error.message : String(error),
|
|
328
|
+
};
|
|
122
329
|
}
|
|
123
|
-
this.sessions.delete(forwardId);
|
|
124
|
-
return true;
|
|
125
330
|
}
|
|
126
331
|
/** 关闭指定 alias 的所有转发(disconnect 时调用,不需要 unforwardIn) */
|
|
127
332
|
closeByAlias(alias) {
|
|
128
|
-
for (const
|
|
129
|
-
if (fwd.alias
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
333
|
+
for (const fwd of this.sessions.values()) {
|
|
334
|
+
if (fwd.alias !== alias) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (fwd.lifecycle === 'pending') {
|
|
338
|
+
this.cancelPendingCreation(fwd, `SSH session '${alias}' disconnected during forward creation`);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
fwd.active = false;
|
|
342
|
+
fwd.lifecycle = 'closed';
|
|
343
|
+
for (const connection of fwd.connections) {
|
|
344
|
+
connection.destroy();
|
|
345
|
+
}
|
|
346
|
+
this.sessions.delete(fwd.id);
|
|
347
|
+
if (fwd.type === 'local' && fwd.server && !fwd.closePromise) {
|
|
348
|
+
this.closeServerAfterCancelledCreation(fwd.server);
|
|
349
|
+
}
|
|
350
|
+
if (fwd.type === 'local' && fwd.server) {
|
|
351
|
+
fwd.listenerReleased = !fwd.server.listening;
|
|
140
352
|
}
|
|
141
353
|
}
|
|
142
354
|
// 清理该 alias 的 dispatcher
|
|
@@ -154,6 +366,9 @@ export class ForwardManager {
|
|
|
154
366
|
list() {
|
|
155
367
|
const result = [];
|
|
156
368
|
for (const [id, fwd] of this.sessions) {
|
|
369
|
+
if (!fwd.active || (fwd.lifecycle !== 'active' && fwd.lifecycle !== 'closing')) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
157
372
|
result.push({
|
|
158
373
|
id,
|
|
159
374
|
alias: fwd.alias,
|
|
@@ -168,6 +383,198 @@ export class ForwardManager {
|
|
|
168
383
|
}
|
|
169
384
|
return result;
|
|
170
385
|
}
|
|
386
|
+
pendingChannelOpensForAlias(alias) {
|
|
387
|
+
let pending = 0;
|
|
388
|
+
for (const session of this.sessions.values()) {
|
|
389
|
+
if (session.alias === alias && session.type === 'local' && session.lifecycle !== 'closed') {
|
|
390
|
+
pending += session.pendingChannelOpens;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return pending;
|
|
394
|
+
}
|
|
395
|
+
waitForConnectionDrain(fwd) {
|
|
396
|
+
if (fwd.activeConnections === 0) {
|
|
397
|
+
return Promise.resolve();
|
|
398
|
+
}
|
|
399
|
+
return new Promise((resolve) => {
|
|
400
|
+
fwd.connectionDrainWaiters.add(resolve);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
waitForChannelOpenDrain(fwd) {
|
|
404
|
+
if (fwd.pendingChannelOpens === 0) {
|
|
405
|
+
return Promise.resolve();
|
|
406
|
+
}
|
|
407
|
+
return new Promise((resolve) => {
|
|
408
|
+
fwd.channelOpenDrainWaiters.add(resolve);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
closeLocalListener(fwd) {
|
|
412
|
+
const server = fwd.server;
|
|
413
|
+
if (!server) {
|
|
414
|
+
return Promise.reject(new Error('Local forward listener is unavailable'));
|
|
415
|
+
}
|
|
416
|
+
return new Promise((resolve, reject) => {
|
|
417
|
+
try {
|
|
418
|
+
server.close((error) => {
|
|
419
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
420
|
+
fwd.listenerReleased = !server.listening;
|
|
421
|
+
}
|
|
422
|
+
if (error) {
|
|
423
|
+
reject(error);
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
resolve();
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
430
|
+
fwd.listenerReleased = !server.listening;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
catch (error) {
|
|
434
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
435
|
+
fwd.listenerReleased = !server.listening;
|
|
436
|
+
}
|
|
437
|
+
reject(error);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
closeRemoteListener(fwd, deps) {
|
|
442
|
+
if (!deps) {
|
|
443
|
+
return Promise.reject(new Error('SSH session is unavailable for remote unforward'));
|
|
444
|
+
}
|
|
445
|
+
return new Promise((resolve, reject) => {
|
|
446
|
+
let client;
|
|
447
|
+
try {
|
|
448
|
+
client = deps.getClient(fwd.alias);
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
reject(error);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
client.unforwardIn(fwd.remoteHost, fwd.remotePort, (error) => {
|
|
455
|
+
if (error) {
|
|
456
|
+
reject(error);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
460
|
+
fwd.remoteUnforwarded = true;
|
|
461
|
+
}
|
|
462
|
+
resolve();
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
isCurrent(fwd) {
|
|
468
|
+
return this.sessions.get(fwd.id) === fwd;
|
|
469
|
+
}
|
|
470
|
+
failPendingCreation(fwd, error) {
|
|
471
|
+
if (!this.isCurrent(fwd) || fwd.lifecycle !== 'pending') {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
fwd.active = false;
|
|
475
|
+
fwd.lifecycle = 'closed';
|
|
476
|
+
this.sessions.delete(fwd.id);
|
|
477
|
+
if (fwd.type === 'local' && fwd.server) {
|
|
478
|
+
this.closeServerAfterCancelledCreation(fwd.server);
|
|
479
|
+
}
|
|
480
|
+
const reject = fwd.rejectCreation;
|
|
481
|
+
fwd.rejectCreation = undefined;
|
|
482
|
+
reject?.(error);
|
|
483
|
+
}
|
|
484
|
+
beginPendingRemoteClose(fwd, message) {
|
|
485
|
+
if (!this.isCurrent(fwd) || fwd.type !== 'remote' || fwd.lifecycle !== 'pending') {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
fwd.active = false;
|
|
489
|
+
fwd.lifecycle = 'closing';
|
|
490
|
+
const rejectCreation = fwd.rejectCreation;
|
|
491
|
+
fwd.rejectCreation = undefined;
|
|
492
|
+
rejectCreation?.(new Error(message));
|
|
493
|
+
fwd.closePromise = new Promise((resolve, reject) => {
|
|
494
|
+
fwd.resolvePendingClose = resolve;
|
|
495
|
+
fwd.rejectPendingClose = reject;
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
completePendingRemoteCloseWithoutListener(fwd) {
|
|
499
|
+
if (!this.isCurrent(fwd) || fwd.lifecycle !== 'closing') {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
fwd.remoteUnforwarded = true;
|
|
503
|
+
this.finishPendingRemoteClose(fwd);
|
|
504
|
+
}
|
|
505
|
+
finishPendingRemoteClose(fwd) {
|
|
506
|
+
const resolve = fwd.resolvePendingClose;
|
|
507
|
+
fwd.resolvePendingClose = undefined;
|
|
508
|
+
fwd.rejectPendingClose = undefined;
|
|
509
|
+
fwd.active = false;
|
|
510
|
+
fwd.lifecycle = 'closed';
|
|
511
|
+
this.sessions.delete(fwd.id);
|
|
512
|
+
this.removeTcpDispatcherIfEmpty(fwd.alias);
|
|
513
|
+
resolve?.();
|
|
514
|
+
}
|
|
515
|
+
failPendingRemoteClose(fwd, error) {
|
|
516
|
+
if (!this.isCurrent(fwd) || fwd.lifecycle !== 'closing') {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
fwd.closePromise = undefined;
|
|
520
|
+
const reject = fwd.rejectPendingClose;
|
|
521
|
+
fwd.resolvePendingClose = undefined;
|
|
522
|
+
fwd.rejectPendingClose = undefined;
|
|
523
|
+
reject?.(error);
|
|
524
|
+
}
|
|
525
|
+
unforwardPendingRemoteClose(fwd, actualPort) {
|
|
526
|
+
try {
|
|
527
|
+
fwd.client.unforwardIn(fwd.remoteHost, actualPort, (error) => {
|
|
528
|
+
if (!this.isCurrent(fwd) || fwd.lifecycle !== 'closing') {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (error) {
|
|
532
|
+
this.failPendingRemoteClose(fwd, error);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
fwd.remoteUnforwarded = true;
|
|
536
|
+
this.finishPendingRemoteClose(fwd);
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
this.failPendingRemoteClose(fwd, error instanceof Error ? error : new Error(String(error)));
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
cancelPendingCreation(fwd, message) {
|
|
544
|
+
this.failPendingCreation(fwd, new Error(message));
|
|
545
|
+
}
|
|
546
|
+
closeServerAfterCancelledCreation(server) {
|
|
547
|
+
try {
|
|
548
|
+
server.close(() => undefined);
|
|
549
|
+
}
|
|
550
|
+
catch {
|
|
551
|
+
/* listener 尚未创建或已关闭 */
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
unforwardCancelledCreation(fwd, actualPort) {
|
|
555
|
+
try {
|
|
556
|
+
fwd.client.unforwardIn(fwd.remoteHost, actualPort, (error) => {
|
|
557
|
+
if (error) {
|
|
558
|
+
console.warn(`Cancelled remote forward ${fwd.id} cleanup failed: ${error.message}`);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
catch (error) {
|
|
563
|
+
console.warn(`Cancelled remote forward ${fwd.id} cleanup failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
withTimeout(promise, timeoutMs) {
|
|
567
|
+
return new Promise((resolve, reject) => {
|
|
568
|
+
const timer = setTimeout(() => reject(new Error(`Forward close timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
569
|
+
promise.then((value) => {
|
|
570
|
+
clearTimeout(timer);
|
|
571
|
+
resolve(value);
|
|
572
|
+
}, (error) => {
|
|
573
|
+
clearTimeout(timer);
|
|
574
|
+
reject(error);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
}
|
|
171
578
|
startIdleSweeper() {
|
|
172
579
|
if (this.idleSweeper) {
|
|
173
580
|
return;
|
|
@@ -185,7 +592,11 @@ export class ForwardManager {
|
|
|
185
592
|
}
|
|
186
593
|
if (fwd.activeConnections === 0 && now - fwd.lastActivityAt > this.idleTimeoutMs) {
|
|
187
594
|
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)
|
|
595
|
+
void this.close(id, this.dependencies ?? undefined).then((result) => {
|
|
596
|
+
if (!result.success) {
|
|
597
|
+
console.warn(`[mcp-ssh] Forward ${id} auto-close failed: ${result.error}`);
|
|
598
|
+
}
|
|
599
|
+
});
|
|
189
600
|
}
|
|
190
601
|
}
|
|
191
602
|
}
|
|
@@ -200,12 +611,15 @@ export class ForwardManager {
|
|
|
200
611
|
const handler = (info, accept, rejectConn) => {
|
|
201
612
|
for (const fwd of this.sessions.values()) {
|
|
202
613
|
if (fwd.type === 'remote' &&
|
|
614
|
+
fwd.lifecycle === 'active' &&
|
|
203
615
|
fwd.active &&
|
|
204
616
|
fwd.alias === alias &&
|
|
205
617
|
fwd.remoteHost === info.destIP &&
|
|
206
618
|
fwd.remotePort === info.destPort) {
|
|
207
619
|
const stream = accept();
|
|
208
620
|
const socket = net.createConnection(fwd.localPort, fwd.localHost);
|
|
621
|
+
fwd.connections.add(stream);
|
|
622
|
+
fwd.connections.add(socket);
|
|
209
623
|
fwd.activeConnections += 1;
|
|
210
624
|
fwd.lastActivityAt = Date.now();
|
|
211
625
|
let closed = false;
|
|
@@ -214,6 +628,8 @@ export class ForwardManager {
|
|
|
214
628
|
return;
|
|
215
629
|
}
|
|
216
630
|
closed = true;
|
|
631
|
+
fwd.connections.delete(stream);
|
|
632
|
+
fwd.connections.delete(socket);
|
|
217
633
|
fwd.activeConnections = Math.max(0, fwd.activeConnections - 1);
|
|
218
634
|
fwd.lastActivityAt = Date.now();
|
|
219
635
|
};
|