@pyrokine/mcp-ssh 2.4.0 → 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 +60 -20
- package/README_zh.md +20 -16
- package/dist/file-ops.d.ts +22 -2
- package/dist/file-ops.js +347 -99
- package/dist/forward-manager.d.ts +17 -1
- package/dist/forward-manager.js +391 -89
- package/dist/operation-manager.d.ts +13 -1
- package/dist/operation-manager.js +188 -45
- package/dist/session-manager.d.ts +26 -1
- package/dist/session-manager.js +246 -49
- package/dist/tools/connection.js +10 -9
- package/dist/tools/file.d.ts +1 -0
- package/dist/tools/file.js +219 -137
- package/dist/tools/forward.js +6 -4
- package/dist/tools/operation.js +11 -3
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -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,212 @@ 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
|
-
|
|
74
|
+
closed = true;
|
|
75
|
+
session.connections.delete(socket);
|
|
76
|
+
if (stream) {
|
|
77
|
+
session.connections.delete(stream);
|
|
78
|
+
if (!stream.destroyed) {
|
|
79
|
+
stream.destroy();
|
|
80
|
+
}
|
|
32
81
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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();
|
|
38
90
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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();
|
|
44
112
|
return;
|
|
45
113
|
}
|
|
114
|
+
stream = connectedStream;
|
|
115
|
+
session.connections.add(stream);
|
|
116
|
+
stream.once('close', closeConnection);
|
|
117
|
+
stream.once('error', closeConnection);
|
|
46
118
|
socket.pipe(stream).pipe(socket);
|
|
47
119
|
});
|
|
48
|
-
}
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
settleChannelOpen();
|
|
123
|
+
closeConnection();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
session.rejectCreation = reject;
|
|
49
128
|
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 });
|
|
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
|
+
}
|
|
75
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
|
+
}
|
|
76
157
|
});
|
|
77
158
|
}
|
|
78
159
|
async forwardRemote(deps, alias, remotePort, localHost, localPort, remoteHost = '127.0.0.1') {
|
|
79
160
|
this.dependencies = deps;
|
|
80
161
|
const client = deps.getClient(alias);
|
|
81
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);
|
|
82
185
|
return new Promise((resolve, reject) => {
|
|
83
|
-
|
|
186
|
+
session.rejectCreation = reject;
|
|
187
|
+
client.forwardIn(remoteHost, remotePort, (err, allocatedPort) => {
|
|
84
188
|
if (err) {
|
|
85
|
-
|
|
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
|
+
}
|
|
86
195
|
return;
|
|
87
196
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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;
|
|
104
225
|
this.ensureTcpDispatcher(alias, client);
|
|
105
|
-
resolve(forwardId);
|
|
226
|
+
resolve({ forwardId, remotePort: actualPort });
|
|
106
227
|
});
|
|
107
228
|
});
|
|
108
229
|
}
|
|
@@ -122,6 +243,22 @@ export class ForwardManager {
|
|
|
122
243
|
error: 'Forward not found',
|
|
123
244
|
};
|
|
124
245
|
}
|
|
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
|
+
};
|
|
259
|
+
}
|
|
260
|
+
this.beginPendingRemoteClose(fwd, 'Forward closed before listener creation completed');
|
|
261
|
+
}
|
|
125
262
|
if (mode === 'force') {
|
|
126
263
|
for (const connection of fwd.connections) {
|
|
127
264
|
connection.destroy();
|
|
@@ -129,17 +266,30 @@ export class ForwardManager {
|
|
|
129
266
|
}
|
|
130
267
|
try {
|
|
131
268
|
if (!fwd.closePromise) {
|
|
269
|
+
fwd.lifecycle = 'closing';
|
|
132
270
|
fwd.closePromise =
|
|
133
271
|
fwd.type === 'local' ? this.closeLocalListener(fwd) : this.closeRemoteListener(fwd, deps);
|
|
134
272
|
fwd.closePromise.catch(() => {
|
|
135
|
-
fwd.
|
|
273
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
274
|
+
if (fwd.active) {
|
|
275
|
+
fwd.lifecycle = 'active';
|
|
276
|
+
}
|
|
277
|
+
fwd.closePromise = undefined;
|
|
278
|
+
}
|
|
136
279
|
});
|
|
137
280
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
+
}
|
|
143
293
|
}
|
|
144
294
|
return {
|
|
145
295
|
success: true,
|
|
@@ -153,6 +303,18 @@ export class ForwardManager {
|
|
|
153
303
|
};
|
|
154
304
|
}
|
|
155
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
|
+
};
|
|
317
|
+
}
|
|
156
318
|
return {
|
|
157
319
|
success: false,
|
|
158
320
|
forwardId,
|
|
@@ -168,22 +330,25 @@ export class ForwardManager {
|
|
|
168
330
|
}
|
|
169
331
|
/** 关闭指定 alias 的所有转发(disconnect 时调用,不需要 unforwardIn) */
|
|
170
332
|
closeByAlias(alias) {
|
|
171
|
-
for (const
|
|
172
|
-
if (fwd.alias
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this.
|
|
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;
|
|
187
352
|
}
|
|
188
353
|
}
|
|
189
354
|
// 清理该 alias 的 dispatcher
|
|
@@ -201,6 +366,9 @@ export class ForwardManager {
|
|
|
201
366
|
list() {
|
|
202
367
|
const result = [];
|
|
203
368
|
for (const [id, fwd] of this.sessions) {
|
|
369
|
+
if (!fwd.active || (fwd.lifecycle !== 'active' && fwd.lifecycle !== 'closing')) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
204
372
|
result.push({
|
|
205
373
|
id,
|
|
206
374
|
alias: fwd.alias,
|
|
@@ -215,6 +383,31 @@ export class ForwardManager {
|
|
|
215
383
|
}
|
|
216
384
|
return result;
|
|
217
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
|
+
}
|
|
218
411
|
closeLocalListener(fwd) {
|
|
219
412
|
const server = fwd.server;
|
|
220
413
|
if (!server) {
|
|
@@ -223,6 +416,9 @@ export class ForwardManager {
|
|
|
223
416
|
return new Promise((resolve, reject) => {
|
|
224
417
|
try {
|
|
225
418
|
server.close((error) => {
|
|
419
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
420
|
+
fwd.listenerReleased = !server.listening;
|
|
421
|
+
}
|
|
226
422
|
if (error) {
|
|
227
423
|
reject(error);
|
|
228
424
|
}
|
|
@@ -230,10 +426,14 @@ export class ForwardManager {
|
|
|
230
426
|
resolve();
|
|
231
427
|
}
|
|
232
428
|
});
|
|
233
|
-
fwd.
|
|
429
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
430
|
+
fwd.listenerReleased = !server.listening;
|
|
431
|
+
}
|
|
234
432
|
}
|
|
235
433
|
catch (error) {
|
|
236
|
-
fwd.
|
|
434
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
435
|
+
fwd.listenerReleased = !server.listening;
|
|
436
|
+
}
|
|
237
437
|
reject(error);
|
|
238
438
|
}
|
|
239
439
|
});
|
|
@@ -256,12 +456,113 @@ export class ForwardManager {
|
|
|
256
456
|
reject(error);
|
|
257
457
|
}
|
|
258
458
|
else {
|
|
259
|
-
fwd.
|
|
459
|
+
if (this.isCurrent(fwd) && fwd.lifecycle === 'closing') {
|
|
460
|
+
fwd.remoteUnforwarded = true;
|
|
461
|
+
}
|
|
260
462
|
resolve();
|
|
261
463
|
}
|
|
262
464
|
});
|
|
263
465
|
});
|
|
264
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
|
+
}
|
|
265
566
|
withTimeout(promise, timeoutMs) {
|
|
266
567
|
return new Promise((resolve, reject) => {
|
|
267
568
|
const timer = setTimeout(() => reject(new Error(`Forward close timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
@@ -310,6 +611,7 @@ export class ForwardManager {
|
|
|
310
611
|
const handler = (info, accept, rejectConn) => {
|
|
311
612
|
for (const fwd of this.sessions.values()) {
|
|
312
613
|
if (fwd.type === 'remote' &&
|
|
614
|
+
fwd.lifecycle === 'active' &&
|
|
313
615
|
fwd.active &&
|
|
314
616
|
fwd.alias === alias &&
|
|
315
617
|
fwd.remoteHost === info.destIP &&
|
|
@@ -6,8 +6,14 @@ export declare const DEFAULT_OPERATION_READ_BYTES: number;
|
|
|
6
6
|
export declare const HARD_OPERATION_READ_BYTES: number;
|
|
7
7
|
export declare const DEFAULT_OPERATION_RETENTION_MS: number;
|
|
8
8
|
export declare const MAX_OPERATION_RETENTION_MS: number;
|
|
9
|
+
export declare const DEFAULT_OPERATION_START_TIMEOUT_MS = 30000;
|
|
10
|
+
export declare const MAX_OPERATION_START_TIMEOUT_MS = 600000;
|
|
11
|
+
export declare class OperationStartError extends Error {
|
|
12
|
+
readonly details: Record<string, unknown>;
|
|
13
|
+
constructor(message: string, details: Record<string, unknown>);
|
|
14
|
+
}
|
|
9
15
|
export interface OperationDependencies {
|
|
10
|
-
openStream(alias: string, command: string, marker: string, options: OperationStartOptions): Promise<
|
|
16
|
+
openStream(alias: string, command: string, marker: string, options: OperationStartOptions, adopt: (stream: ClientChannel) => void): Promise<void>;
|
|
11
17
|
cancelRemote(alias: string, pid: number, marker: string, processGroup: boolean, options: OperationStartOptions): Promise<{
|
|
12
18
|
success: boolean;
|
|
13
19
|
error?: string;
|
|
@@ -17,6 +23,7 @@ export declare class OperationManager {
|
|
|
17
23
|
private readonly dependencies;
|
|
18
24
|
private readonly now;
|
|
19
25
|
private readonly operations;
|
|
26
|
+
private readonly pendingStarts;
|
|
20
27
|
private readonly sweeper;
|
|
21
28
|
constructor(dependencies: OperationDependencies, now?: () => number);
|
|
22
29
|
start(alias: string, command: string, options?: OperationStartOptions): Promise<OperationInfo>;
|
|
@@ -36,13 +43,18 @@ export declare class OperationManager {
|
|
|
36
43
|
private finishStreamError;
|
|
37
44
|
private finishStreamClose;
|
|
38
45
|
private consumeMarker;
|
|
46
|
+
private failMarker;
|
|
47
|
+
private flushMarkerBuffer;
|
|
48
|
+
private appendStderr;
|
|
39
49
|
private appendOutput;
|
|
40
50
|
private finish;
|
|
41
51
|
private info;
|
|
52
|
+
private clearPendingStart;
|
|
42
53
|
private requireOperation;
|
|
43
54
|
private normalizeMaxOutputBytes;
|
|
44
55
|
private normalizeReadBytes;
|
|
45
56
|
private normalizeRetentionMs;
|
|
57
|
+
private normalizeStartTimeoutMs;
|
|
46
58
|
private normalizeOffset;
|
|
47
59
|
private ensureUtf8Boundary;
|
|
48
60
|
private utf8SafeEnd;
|