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