mcp-ssh-manager 3.0.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/.claude/settings.local.json +87 -0
- package/.flake8 +15 -0
- package/.hooks-config.json +131 -0
- package/.pre-commit-config.yaml +105 -0
- package/CHANGELOG.md +137 -0
- package/CLAUDE.md +190 -0
- package/CONTRIBUTING.md +114 -0
- package/DESCRIPTION +1 -0
- package/INSTALLATION.md +180 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +77 -0
- package/README.md +718 -0
- package/cli/README.md +286 -0
- package/cli/build.sh +199 -0
- package/cli/commands/server.sh +284 -0
- package/cli/demo.sh +107 -0
- package/cli/install.sh +164 -0
- package/cli/lib/colors.sh +174 -0
- package/cli/lib/config.sh +284 -0
- package/cli/lib/menu.sh +430 -0
- package/cli/migrate.sh +168 -0
- package/cli/ssh-manager +525 -0
- package/docs/ALIASES_AND_HOOKS.md +285 -0
- package/docs/BACKUP_GUIDE.md +500 -0
- package/docs/DEPLOYMENT_GUIDE.md +241 -0
- package/docs/SEO.md +257 -0
- package/examples/backup-workflow.js +481 -0
- package/examples/claude-code-config.example.json +40 -0
- package/examples/codex-ssh-config.example.toml +71 -0
- package/examples/deploy-workflow.py +183 -0
- package/glama.json +6 -0
- package/package.json +85 -0
- package/profiles/README.md +40 -0
- package/profiles/default.json +52 -0
- package/profiles/docker.json +62 -0
- package/profiles/frappe.json +107 -0
- package/profiles/nodejs.json +63 -0
- package/pyproject.toml +17 -0
- package/scripts/setup-hooks.sh +60 -0
- package/scripts/validate.sh +76 -0
- package/src/backup-manager.js +469 -0
- package/src/command-aliases.js +151 -0
- package/src/config-loader.js +281 -0
- package/src/database-manager.js +555 -0
- package/src/deploy-helper.js +176 -0
- package/src/health-monitor.js +428 -0
- package/src/hooks-system.js +327 -0
- package/src/index.js +4499 -0
- package/src/logger.js +276 -0
- package/src/profile-loader.js +187 -0
- package/src/server-aliases.js +150 -0
- package/src/server-groups.js +409 -0
- package/src/session-manager.js +410 -0
- package/src/ssh-key-manager.js +378 -0
- package/src/ssh-manager.js +435 -0
- package/src/tunnel-manager.js +576 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH Tunnel Manager
|
|
3
|
+
* Manages SSH port forwarding and SOCKS proxy tunnels
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
7
|
+
import net from 'net';
|
|
8
|
+
import { logger } from './logger.js';
|
|
9
|
+
|
|
10
|
+
// Map to store active tunnels
|
|
11
|
+
const tunnels = new Map();
|
|
12
|
+
|
|
13
|
+
// Tunnel types
|
|
14
|
+
export const TUNNEL_TYPES = {
|
|
15
|
+
LOCAL: 'local', // Local port forwarding (access remote service locally)
|
|
16
|
+
REMOTE: 'remote', // Remote port forwarding (expose local service remotely)
|
|
17
|
+
DYNAMIC: 'dynamic' // SOCKS proxy
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Tunnel states
|
|
21
|
+
export const TUNNEL_STATES = {
|
|
22
|
+
CONNECTING: 'connecting',
|
|
23
|
+
ACTIVE: 'active',
|
|
24
|
+
RECONNECTING: 'reconnecting',
|
|
25
|
+
FAILED: 'failed',
|
|
26
|
+
CLOSED: 'closed'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class SSHTunnel {
|
|
30
|
+
constructor(id, serverName, ssh, config) {
|
|
31
|
+
this.id = id;
|
|
32
|
+
this.serverName = serverName;
|
|
33
|
+
this.ssh = ssh;
|
|
34
|
+
this.type = config.type;
|
|
35
|
+
this.config = config;
|
|
36
|
+
this.state = TUNNEL_STATES.CONNECTING;
|
|
37
|
+
this.createdAt = new Date();
|
|
38
|
+
this.lastActivity = new Date();
|
|
39
|
+
this.connections = new Set();
|
|
40
|
+
this.server = null;
|
|
41
|
+
this.reconnectAttempts = 0;
|
|
42
|
+
this.maxReconnectAttempts = 5;
|
|
43
|
+
this.stats = {
|
|
44
|
+
bytesTransferred: 0,
|
|
45
|
+
connectionsTotal: 0,
|
|
46
|
+
connectionsActive: 0,
|
|
47
|
+
errors: 0
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Start the tunnel
|
|
53
|
+
*/
|
|
54
|
+
async start() {
|
|
55
|
+
try {
|
|
56
|
+
switch (this.type) {
|
|
57
|
+
case TUNNEL_TYPES.LOCAL:
|
|
58
|
+
await this.startLocalForwarding();
|
|
59
|
+
break;
|
|
60
|
+
|
|
61
|
+
case TUNNEL_TYPES.REMOTE:
|
|
62
|
+
await this.startRemoteForwarding();
|
|
63
|
+
break;
|
|
64
|
+
|
|
65
|
+
case TUNNEL_TYPES.DYNAMIC:
|
|
66
|
+
await this.startDynamicForwarding();
|
|
67
|
+
break;
|
|
68
|
+
|
|
69
|
+
default:
|
|
70
|
+
throw new Error(`Unknown tunnel type: ${this.type}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.state = TUNNEL_STATES.ACTIVE;
|
|
74
|
+
this.lastActivity = new Date();
|
|
75
|
+
|
|
76
|
+
logger.info(`SSH tunnel ${this.id} started`, {
|
|
77
|
+
type: this.type,
|
|
78
|
+
server: this.serverName,
|
|
79
|
+
local: `${this.config.localHost}:${this.config.localPort}`,
|
|
80
|
+
remote: this.type !== TUNNEL_TYPES.DYNAMIC ?
|
|
81
|
+
`${this.config.remoteHost}:${this.config.remotePort}` : 'SOCKS'
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
} catch (error) {
|
|
85
|
+
this.state = TUNNEL_STATES.FAILED;
|
|
86
|
+
logger.error(`Failed to start tunnel ${this.id}`, {
|
|
87
|
+
error: error.message
|
|
88
|
+
});
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Start local port forwarding
|
|
95
|
+
*/
|
|
96
|
+
async startLocalForwarding() {
|
|
97
|
+
const { localHost, localPort, remoteHost, remotePort } = this.config;
|
|
98
|
+
|
|
99
|
+
// Create local server
|
|
100
|
+
this.server = net.createServer(async (localSocket) => {
|
|
101
|
+
this.stats.connectionsTotal++;
|
|
102
|
+
this.stats.connectionsActive++;
|
|
103
|
+
this.connections.add(localSocket);
|
|
104
|
+
this.lastActivity = new Date();
|
|
105
|
+
|
|
106
|
+
logger.debug(`New connection to tunnel ${this.id}`, {
|
|
107
|
+
from: localSocket.remoteAddress
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
// Forward to remote via SSH
|
|
112
|
+
const stream = await this.ssh.forwardOut(
|
|
113
|
+
localSocket.remoteAddress || '127.0.0.1',
|
|
114
|
+
localSocket.remotePort || 0,
|
|
115
|
+
remoteHost,
|
|
116
|
+
remotePort
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Pipe data between local and remote
|
|
120
|
+
localSocket.pipe(stream).pipe(localSocket);
|
|
121
|
+
|
|
122
|
+
// Track data transfer
|
|
123
|
+
localSocket.on('data', (chunk) => {
|
|
124
|
+
this.stats.bytesTransferred += chunk.length;
|
|
125
|
+
this.lastActivity = new Date();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
stream.on('data', (chunk) => {
|
|
129
|
+
this.stats.bytesTransferred += chunk.length;
|
|
130
|
+
this.lastActivity = new Date();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Handle disconnection
|
|
134
|
+
const cleanup = () => {
|
|
135
|
+
this.stats.connectionsActive--;
|
|
136
|
+
this.connections.delete(localSocket);
|
|
137
|
+
localSocket.destroy();
|
|
138
|
+
stream.destroy();
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
localSocket.on('close', cleanup);
|
|
142
|
+
localSocket.on('error', cleanup);
|
|
143
|
+
stream.on('close', cleanup);
|
|
144
|
+
stream.on('error', cleanup);
|
|
145
|
+
|
|
146
|
+
} catch (error) {
|
|
147
|
+
this.stats.errors++;
|
|
148
|
+
logger.error(`Tunnel forwarding error`, {
|
|
149
|
+
tunnel: this.id,
|
|
150
|
+
error: error.message
|
|
151
|
+
});
|
|
152
|
+
localSocket.destroy();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Start listening
|
|
157
|
+
await new Promise((resolve, reject) => {
|
|
158
|
+
this.server.listen(localPort, localHost, (err) => {
|
|
159
|
+
if (err) reject(err);
|
|
160
|
+
else resolve();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
logger.info(`Local forwarding established`, {
|
|
165
|
+
local: `${localHost}:${localPort}`,
|
|
166
|
+
remote: `${remoteHost}:${remotePort}`
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Start remote port forwarding
|
|
172
|
+
*/
|
|
173
|
+
async startRemoteForwarding() {
|
|
174
|
+
const { localHost, localPort, remoteHost, remotePort } = this.config;
|
|
175
|
+
|
|
176
|
+
// Request remote forwarding from SSH server
|
|
177
|
+
await new Promise((resolve, reject) => {
|
|
178
|
+
this.ssh.forwardIn(remoteHost, remotePort, (err) => {
|
|
179
|
+
if (err) reject(err);
|
|
180
|
+
else resolve();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Handle incoming connections from remote
|
|
185
|
+
this.ssh.on('tcp connection', (info, accept, reject) => {
|
|
186
|
+
if (info.destPort !== remotePort) return;
|
|
187
|
+
|
|
188
|
+
this.stats.connectionsTotal++;
|
|
189
|
+
this.stats.connectionsActive++;
|
|
190
|
+
this.lastActivity = new Date();
|
|
191
|
+
|
|
192
|
+
const remoteSocket = accept();
|
|
193
|
+
|
|
194
|
+
// Connect to local service
|
|
195
|
+
const localSocket = net.connect(localPort, localHost, () => {
|
|
196
|
+
// Pipe data between remote and local
|
|
197
|
+
remoteSocket.pipe(localSocket).pipe(remoteSocket);
|
|
198
|
+
|
|
199
|
+
// Track data transfer
|
|
200
|
+
remoteSocket.on('data', (chunk) => {
|
|
201
|
+
this.stats.bytesTransferred += chunk.length;
|
|
202
|
+
this.lastActivity = new Date();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
localSocket.on('data', (chunk) => {
|
|
206
|
+
this.stats.bytesTransferred += chunk.length;
|
|
207
|
+
this.lastActivity = new Date();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Handle errors and cleanup
|
|
212
|
+
const cleanup = () => {
|
|
213
|
+
this.stats.connectionsActive--;
|
|
214
|
+
remoteSocket.destroy();
|
|
215
|
+
localSocket.destroy();
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
localSocket.on('error', (err) => {
|
|
219
|
+
this.stats.errors++;
|
|
220
|
+
logger.error(`Remote forwarding error`, {
|
|
221
|
+
tunnel: this.id,
|
|
222
|
+
error: err.message
|
|
223
|
+
});
|
|
224
|
+
cleanup();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
remoteSocket.on('close', cleanup);
|
|
228
|
+
localSocket.on('close', cleanup);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
logger.info(`Remote forwarding established`, {
|
|
232
|
+
local: `${localHost}:${localPort}`,
|
|
233
|
+
remote: `${remoteHost}:${remotePort}`
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Start dynamic port forwarding (SOCKS proxy)
|
|
239
|
+
*/
|
|
240
|
+
async startDynamicForwarding() {
|
|
241
|
+
const { localHost, localPort } = this.config;
|
|
242
|
+
|
|
243
|
+
// Create SOCKS server
|
|
244
|
+
this.server = net.createServer(async (localSocket) => {
|
|
245
|
+
this.stats.connectionsTotal++;
|
|
246
|
+
this.stats.connectionsActive++;
|
|
247
|
+
this.connections.add(localSocket);
|
|
248
|
+
this.lastActivity = new Date();
|
|
249
|
+
|
|
250
|
+
let targetHost = null;
|
|
251
|
+
let targetPort = null;
|
|
252
|
+
let stream = null;
|
|
253
|
+
|
|
254
|
+
// Simple SOCKS5 implementation (basic)
|
|
255
|
+
localSocket.once('data', async (chunk) => {
|
|
256
|
+
// Parse SOCKS request (simplified)
|
|
257
|
+
if (chunk[0] === 0x05) { // SOCKS5
|
|
258
|
+
// Send auth method response
|
|
259
|
+
localSocket.write(Buffer.from([0x05, 0x00]));
|
|
260
|
+
|
|
261
|
+
localSocket.once('data', async (chunk2) => {
|
|
262
|
+
// Parse connection request
|
|
263
|
+
if (chunk2[0] === 0x05 && chunk2[1] === 0x01) { // CONNECT
|
|
264
|
+
const addrType = chunk2[3];
|
|
265
|
+
let offset = 4;
|
|
266
|
+
|
|
267
|
+
if (addrType === 0x01) { // IPv4
|
|
268
|
+
targetHost = `${chunk2[4]}.${chunk2[5]}.${chunk2[6]}.${chunk2[7]}`;
|
|
269
|
+
offset = 8;
|
|
270
|
+
} else if (addrType === 0x03) { // Domain
|
|
271
|
+
const domainLen = chunk2[4];
|
|
272
|
+
targetHost = chunk2.slice(5, 5 + domainLen).toString();
|
|
273
|
+
offset = 5 + domainLen;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
targetPort = (chunk2[offset] << 8) | chunk2[offset + 1];
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
// Create SSH forwarding stream
|
|
280
|
+
stream = await this.ssh.forwardOut(
|
|
281
|
+
'127.0.0.1', 0,
|
|
282
|
+
targetHost, targetPort
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
// Send success response
|
|
286
|
+
const response = Buffer.from([
|
|
287
|
+
0x05, 0x00, 0x00, 0x01,
|
|
288
|
+
0, 0, 0, 0, // Bind address (0.0.0.0)
|
|
289
|
+
0, 0 // Bind port
|
|
290
|
+
]);
|
|
291
|
+
localSocket.write(response);
|
|
292
|
+
|
|
293
|
+
// Pipe data
|
|
294
|
+
localSocket.pipe(stream).pipe(localSocket);
|
|
295
|
+
|
|
296
|
+
// Track data
|
|
297
|
+
localSocket.on('data', (chunk) => {
|
|
298
|
+
this.stats.bytesTransferred += chunk.length;
|
|
299
|
+
this.lastActivity = new Date();
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
stream.on('data', (chunk) => {
|
|
303
|
+
this.stats.bytesTransferred += chunk.length;
|
|
304
|
+
this.lastActivity = new Date();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
} catch (error) {
|
|
308
|
+
// Send error response
|
|
309
|
+
const response = Buffer.from([
|
|
310
|
+
0x05, 0x01, 0x00, 0x01,
|
|
311
|
+
0, 0, 0, 0, 0, 0
|
|
312
|
+
]);
|
|
313
|
+
localSocket.write(response);
|
|
314
|
+
localSocket.destroy();
|
|
315
|
+
this.stats.errors++;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
} else {
|
|
320
|
+
// Not SOCKS5, close connection
|
|
321
|
+
localSocket.destroy();
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// Cleanup on disconnect
|
|
326
|
+
localSocket.on('close', () => {
|
|
327
|
+
this.stats.connectionsActive--;
|
|
328
|
+
this.connections.delete(localSocket);
|
|
329
|
+
if (stream) stream.destroy();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
localSocket.on('error', () => {
|
|
333
|
+
this.stats.errors++;
|
|
334
|
+
this.stats.connectionsActive--;
|
|
335
|
+
this.connections.delete(localSocket);
|
|
336
|
+
if (stream) stream.destroy();
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// Start listening
|
|
341
|
+
await new Promise((resolve, reject) => {
|
|
342
|
+
this.server.listen(localPort, localHost, (err) => {
|
|
343
|
+
if (err) reject(err);
|
|
344
|
+
else resolve();
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
logger.info(`SOCKS proxy established`, {
|
|
349
|
+
local: `${localHost}:${localPort}`
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Get tunnel information
|
|
355
|
+
*/
|
|
356
|
+
getInfo() {
|
|
357
|
+
return {
|
|
358
|
+
id: this.id,
|
|
359
|
+
server: this.serverName,
|
|
360
|
+
type: this.type,
|
|
361
|
+
state: this.state,
|
|
362
|
+
config: {
|
|
363
|
+
localHost: this.config.localHost,
|
|
364
|
+
localPort: this.config.localPort,
|
|
365
|
+
remoteHost: this.config.remoteHost,
|
|
366
|
+
remotePort: this.config.remotePort
|
|
367
|
+
},
|
|
368
|
+
stats: this.stats,
|
|
369
|
+
created: this.createdAt,
|
|
370
|
+
lastActivity: this.lastActivity,
|
|
371
|
+
activeConnections: this.connections.size
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Close the tunnel
|
|
377
|
+
*/
|
|
378
|
+
close() {
|
|
379
|
+
logger.info(`Closing tunnel ${this.id}`);
|
|
380
|
+
|
|
381
|
+
this.state = TUNNEL_STATES.CLOSED;
|
|
382
|
+
|
|
383
|
+
// Close all active connections
|
|
384
|
+
for (const conn of this.connections) {
|
|
385
|
+
conn.destroy();
|
|
386
|
+
}
|
|
387
|
+
this.connections.clear();
|
|
388
|
+
|
|
389
|
+
// Close server
|
|
390
|
+
if (this.server) {
|
|
391
|
+
this.server.close();
|
|
392
|
+
this.server = null;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Cancel remote forwarding if needed
|
|
396
|
+
if (this.type === TUNNEL_TYPES.REMOTE) {
|
|
397
|
+
this.ssh.unforwardIn(this.config.remoteHost, this.config.remotePort);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
tunnels.delete(this.id);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Reconnect tunnel
|
|
405
|
+
*/
|
|
406
|
+
async reconnect() {
|
|
407
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
408
|
+
logger.error(`Max reconnect attempts reached for tunnel ${this.id}`);
|
|
409
|
+
this.state = TUNNEL_STATES.FAILED;
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
this.reconnectAttempts++;
|
|
414
|
+
this.state = TUNNEL_STATES.RECONNECTING;
|
|
415
|
+
|
|
416
|
+
logger.info(`Reconnecting tunnel ${this.id}`, {
|
|
417
|
+
attempt: this.reconnectAttempts
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
try {
|
|
421
|
+
await this.start();
|
|
422
|
+
this.reconnectAttempts = 0;
|
|
423
|
+
return true;
|
|
424
|
+
} catch (error) {
|
|
425
|
+
logger.error(`Reconnect failed for tunnel ${this.id}`, {
|
|
426
|
+
error: error.message
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// Retry with exponential backoff
|
|
430
|
+
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
|
|
431
|
+
setTimeout(() => this.reconnect(), delay);
|
|
432
|
+
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Create a new SSH tunnel
|
|
440
|
+
*/
|
|
441
|
+
export async function createTunnel(serverName, ssh, config) {
|
|
442
|
+
const tunnelId = `tunnel_${Date.now()}_${uuidv4().substring(0, 8)}`;
|
|
443
|
+
|
|
444
|
+
// Validate config
|
|
445
|
+
if (!config.type || !Object.values(TUNNEL_TYPES).includes(config.type)) {
|
|
446
|
+
throw new Error(`Invalid tunnel type: ${config.type}`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Set defaults
|
|
450
|
+
config.localHost = config.localHost || '127.0.0.1';
|
|
451
|
+
|
|
452
|
+
if (config.type !== TUNNEL_TYPES.DYNAMIC) {
|
|
453
|
+
if (!config.remoteHost || !config.remotePort) {
|
|
454
|
+
throw new Error('Remote host and port required for port forwarding');
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (!config.localPort) {
|
|
459
|
+
throw new Error('Local port required');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const tunnel = new SSHTunnel(tunnelId, serverName, ssh, config);
|
|
463
|
+
tunnels.set(tunnelId, tunnel);
|
|
464
|
+
|
|
465
|
+
try {
|
|
466
|
+
await tunnel.start();
|
|
467
|
+
|
|
468
|
+
logger.info('SSH tunnel created', {
|
|
469
|
+
id: tunnelId,
|
|
470
|
+
type: config.type,
|
|
471
|
+
server: serverName
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
return tunnel;
|
|
475
|
+
} catch (error) {
|
|
476
|
+
tunnels.delete(tunnelId);
|
|
477
|
+
throw error;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Get an existing tunnel
|
|
483
|
+
*/
|
|
484
|
+
export function getTunnel(tunnelId) {
|
|
485
|
+
const tunnel = tunnels.get(tunnelId);
|
|
486
|
+
|
|
487
|
+
if (!tunnel) {
|
|
488
|
+
throw new Error(`Tunnel ${tunnelId} not found`);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return tunnel;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* List all active tunnels
|
|
496
|
+
*/
|
|
497
|
+
export function listTunnels(serverName = null) {
|
|
498
|
+
const activeTunnels = [];
|
|
499
|
+
|
|
500
|
+
for (const [id, tunnel] of tunnels.entries()) {
|
|
501
|
+
if (tunnel.state !== TUNNEL_STATES.CLOSED) {
|
|
502
|
+
if (!serverName || tunnel.serverName === serverName) {
|
|
503
|
+
activeTunnels.push(tunnel.getInfo());
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return activeTunnels;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Close a tunnel
|
|
513
|
+
*/
|
|
514
|
+
export function closeTunnel(tunnelId) {
|
|
515
|
+
const tunnel = tunnels.get(tunnelId);
|
|
516
|
+
|
|
517
|
+
if (!tunnel) {
|
|
518
|
+
throw new Error(`Tunnel ${tunnelId} not found`);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
tunnel.close();
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Close all tunnels for a server
|
|
527
|
+
*/
|
|
528
|
+
export function closeServerTunnels(serverName) {
|
|
529
|
+
let closedCount = 0;
|
|
530
|
+
|
|
531
|
+
for (const [id, tunnel] of tunnels.entries()) {
|
|
532
|
+
if (tunnel.serverName === serverName) {
|
|
533
|
+
tunnel.close();
|
|
534
|
+
closedCount++;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return closedCount;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Monitor tunnel health
|
|
543
|
+
*/
|
|
544
|
+
export function monitorTunnels() {
|
|
545
|
+
const now = Date.now();
|
|
546
|
+
const healthTimeout = 60 * 1000; // 1 minute
|
|
547
|
+
|
|
548
|
+
for (const [id, tunnel] of tunnels.entries()) {
|
|
549
|
+
if (tunnel.state === TUNNEL_STATES.ACTIVE) {
|
|
550
|
+
const idle = now - tunnel.lastActivity.getTime();
|
|
551
|
+
|
|
552
|
+
// Check if tunnel is still healthy
|
|
553
|
+
if (idle > healthTimeout && tunnel.connections.size === 0) {
|
|
554
|
+
logger.debug(`Tunnel ${id} idle for ${idle}ms`);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Auto-reconnect failed tunnels
|
|
558
|
+
if (tunnel.state === TUNNEL_STATES.FAILED) {
|
|
559
|
+
tunnel.reconnect();
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Monitor tunnels periodically
|
|
566
|
+
setInterval(monitorTunnels, 30 * 1000); // Every 30 seconds
|
|
567
|
+
|
|
568
|
+
export default {
|
|
569
|
+
createTunnel,
|
|
570
|
+
getTunnel,
|
|
571
|
+
listTunnels,
|
|
572
|
+
closeTunnel,
|
|
573
|
+
closeServerTunnels,
|
|
574
|
+
TUNNEL_TYPES,
|
|
575
|
+
TUNNEL_STATES
|
|
576
|
+
};
|