mcp-ssh-manager 3.0.0 → 3.1.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.
@@ -47,40 +47,40 @@ class SSHTunnel {
47
47
  errors: 0
48
48
  };
49
49
  }
50
-
50
+
51
51
  /**
52
52
  * Start the tunnel
53
53
  */
54
54
  async start() {
55
55
  try {
56
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}`);
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
71
  }
72
-
72
+
73
73
  this.state = TUNNEL_STATES.ACTIVE;
74
74
  this.lastActivity = new Date();
75
-
75
+
76
76
  logger.info(`SSH tunnel ${this.id} started`, {
77
77
  type: this.type,
78
78
  server: this.serverName,
79
79
  local: `${this.config.localHost}:${this.config.localPort}`,
80
- remote: this.type !== TUNNEL_TYPES.DYNAMIC ?
80
+ remote: this.type !== TUNNEL_TYPES.DYNAMIC ?
81
81
  `${this.config.remoteHost}:${this.config.remotePort}` : 'SOCKS'
82
82
  });
83
-
83
+
84
84
  } catch (error) {
85
85
  this.state = TUNNEL_STATES.FAILED;
86
86
  logger.error(`Failed to start tunnel ${this.id}`, {
@@ -89,24 +89,24 @@ class SSHTunnel {
89
89
  throw error;
90
90
  }
91
91
  }
92
-
92
+
93
93
  /**
94
94
  * Start local port forwarding
95
95
  */
96
96
  async startLocalForwarding() {
97
97
  const { localHost, localPort, remoteHost, remotePort } = this.config;
98
-
98
+
99
99
  // Create local server
100
100
  this.server = net.createServer(async (localSocket) => {
101
101
  this.stats.connectionsTotal++;
102
102
  this.stats.connectionsActive++;
103
103
  this.connections.add(localSocket);
104
104
  this.lastActivity = new Date();
105
-
105
+
106
106
  logger.debug(`New connection to tunnel ${this.id}`, {
107
107
  from: localSocket.remoteAddress
108
108
  });
109
-
109
+
110
110
  try {
111
111
  // Forward to remote via SSH
112
112
  const stream = await this.ssh.forwardOut(
@@ -115,21 +115,21 @@ class SSHTunnel {
115
115
  remoteHost,
116
116
  remotePort
117
117
  );
118
-
118
+
119
119
  // Pipe data between local and remote
120
120
  localSocket.pipe(stream).pipe(localSocket);
121
-
121
+
122
122
  // Track data transfer
123
123
  localSocket.on('data', (chunk) => {
124
124
  this.stats.bytesTransferred += chunk.length;
125
125
  this.lastActivity = new Date();
126
126
  });
127
-
127
+
128
128
  stream.on('data', (chunk) => {
129
129
  this.stats.bytesTransferred += chunk.length;
130
130
  this.lastActivity = new Date();
131
131
  });
132
-
132
+
133
133
  // Handle disconnection
134
134
  const cleanup = () => {
135
135
  this.stats.connectionsActive--;
@@ -137,22 +137,22 @@ class SSHTunnel {
137
137
  localSocket.destroy();
138
138
  stream.destroy();
139
139
  };
140
-
140
+
141
141
  localSocket.on('close', cleanup);
142
142
  localSocket.on('error', cleanup);
143
143
  stream.on('close', cleanup);
144
144
  stream.on('error', cleanup);
145
-
145
+
146
146
  } catch (error) {
147
147
  this.stats.errors++;
148
- logger.error(`Tunnel forwarding error`, {
148
+ logger.error('Tunnel forwarding error', {
149
149
  tunnel: this.id,
150
150
  error: error.message
151
151
  });
152
152
  localSocket.destroy();
153
153
  }
154
154
  });
155
-
155
+
156
156
  // Start listening
157
157
  await new Promise((resolve, reject) => {
158
158
  this.server.listen(localPort, localHost, (err) => {
@@ -160,19 +160,19 @@ class SSHTunnel {
160
160
  else resolve();
161
161
  });
162
162
  });
163
-
164
- logger.info(`Local forwarding established`, {
163
+
164
+ logger.info('Local forwarding established', {
165
165
  local: `${localHost}:${localPort}`,
166
166
  remote: `${remoteHost}:${remotePort}`
167
167
  });
168
168
  }
169
-
169
+
170
170
  /**
171
171
  * Start remote port forwarding
172
172
  */
173
173
  async startRemoteForwarding() {
174
174
  const { localHost, localPort, remoteHost, remotePort } = this.config;
175
-
175
+
176
176
  // Request remote forwarding from SSH server
177
177
  await new Promise((resolve, reject) => {
178
178
  this.ssh.forwardIn(remoteHost, remotePort, (err) => {
@@ -180,90 +180,90 @@ class SSHTunnel {
180
180
  else resolve();
181
181
  });
182
182
  });
183
-
183
+
184
184
  // Handle incoming connections from remote
185
185
  this.ssh.on('tcp connection', (info, accept, reject) => {
186
186
  if (info.destPort !== remotePort) return;
187
-
187
+
188
188
  this.stats.connectionsTotal++;
189
189
  this.stats.connectionsActive++;
190
190
  this.lastActivity = new Date();
191
-
191
+
192
192
  const remoteSocket = accept();
193
-
193
+
194
194
  // Connect to local service
195
195
  const localSocket = net.connect(localPort, localHost, () => {
196
196
  // Pipe data between remote and local
197
197
  remoteSocket.pipe(localSocket).pipe(remoteSocket);
198
-
198
+
199
199
  // Track data transfer
200
200
  remoteSocket.on('data', (chunk) => {
201
201
  this.stats.bytesTransferred += chunk.length;
202
202
  this.lastActivity = new Date();
203
203
  });
204
-
204
+
205
205
  localSocket.on('data', (chunk) => {
206
206
  this.stats.bytesTransferred += chunk.length;
207
207
  this.lastActivity = new Date();
208
208
  });
209
209
  });
210
-
210
+
211
211
  // Handle errors and cleanup
212
212
  const cleanup = () => {
213
213
  this.stats.connectionsActive--;
214
214
  remoteSocket.destroy();
215
215
  localSocket.destroy();
216
216
  };
217
-
217
+
218
218
  localSocket.on('error', (err) => {
219
219
  this.stats.errors++;
220
- logger.error(`Remote forwarding error`, {
220
+ logger.error('Remote forwarding error', {
221
221
  tunnel: this.id,
222
222
  error: err.message
223
223
  });
224
224
  cleanup();
225
225
  });
226
-
226
+
227
227
  remoteSocket.on('close', cleanup);
228
228
  localSocket.on('close', cleanup);
229
229
  });
230
-
231
- logger.info(`Remote forwarding established`, {
230
+
231
+ logger.info('Remote forwarding established', {
232
232
  local: `${localHost}:${localPort}`,
233
233
  remote: `${remoteHost}:${remotePort}`
234
234
  });
235
235
  }
236
-
236
+
237
237
  /**
238
238
  * Start dynamic port forwarding (SOCKS proxy)
239
239
  */
240
240
  async startDynamicForwarding() {
241
241
  const { localHost, localPort } = this.config;
242
-
242
+
243
243
  // Create SOCKS server
244
244
  this.server = net.createServer(async (localSocket) => {
245
245
  this.stats.connectionsTotal++;
246
246
  this.stats.connectionsActive++;
247
247
  this.connections.add(localSocket);
248
248
  this.lastActivity = new Date();
249
-
249
+
250
250
  let targetHost = null;
251
251
  let targetPort = null;
252
252
  let stream = null;
253
-
253
+
254
254
  // Simple SOCKS5 implementation (basic)
255
255
  localSocket.once('data', async (chunk) => {
256
256
  // Parse SOCKS request (simplified)
257
257
  if (chunk[0] === 0x05) { // SOCKS5
258
258
  // Send auth method response
259
259
  localSocket.write(Buffer.from([0x05, 0x00]));
260
-
260
+
261
261
  localSocket.once('data', async (chunk2) => {
262
262
  // Parse connection request
263
263
  if (chunk2[0] === 0x05 && chunk2[1] === 0x01) { // CONNECT
264
264
  const addrType = chunk2[3];
265
265
  let offset = 4;
266
-
266
+
267
267
  if (addrType === 0x01) { // IPv4
268
268
  targetHost = `${chunk2[4]}.${chunk2[5]}.${chunk2[6]}.${chunk2[7]}`;
269
269
  offset = 8;
@@ -272,16 +272,16 @@ class SSHTunnel {
272
272
  targetHost = chunk2.slice(5, 5 + domainLen).toString();
273
273
  offset = 5 + domainLen;
274
274
  }
275
-
275
+
276
276
  targetPort = (chunk2[offset] << 8) | chunk2[offset + 1];
277
-
277
+
278
278
  try {
279
279
  // Create SSH forwarding stream
280
280
  stream = await this.ssh.forwardOut(
281
281
  '127.0.0.1', 0,
282
282
  targetHost, targetPort
283
283
  );
284
-
284
+
285
285
  // Send success response
286
286
  const response = Buffer.from([
287
287
  0x05, 0x00, 0x00, 0x01,
@@ -289,21 +289,21 @@ class SSHTunnel {
289
289
  0, 0 // Bind port
290
290
  ]);
291
291
  localSocket.write(response);
292
-
292
+
293
293
  // Pipe data
294
294
  localSocket.pipe(stream).pipe(localSocket);
295
-
295
+
296
296
  // Track data
297
297
  localSocket.on('data', (chunk) => {
298
298
  this.stats.bytesTransferred += chunk.length;
299
299
  this.lastActivity = new Date();
300
300
  });
301
-
301
+
302
302
  stream.on('data', (chunk) => {
303
303
  this.stats.bytesTransferred += chunk.length;
304
304
  this.lastActivity = new Date();
305
305
  });
306
-
306
+
307
307
  } catch (error) {
308
308
  // Send error response
309
309
  const response = Buffer.from([
@@ -321,14 +321,14 @@ class SSHTunnel {
321
321
  localSocket.destroy();
322
322
  }
323
323
  });
324
-
324
+
325
325
  // Cleanup on disconnect
326
326
  localSocket.on('close', () => {
327
327
  this.stats.connectionsActive--;
328
328
  this.connections.delete(localSocket);
329
329
  if (stream) stream.destroy();
330
330
  });
331
-
331
+
332
332
  localSocket.on('error', () => {
333
333
  this.stats.errors++;
334
334
  this.stats.connectionsActive--;
@@ -336,7 +336,7 @@ class SSHTunnel {
336
336
  if (stream) stream.destroy();
337
337
  });
338
338
  });
339
-
339
+
340
340
  // Start listening
341
341
  await new Promise((resolve, reject) => {
342
342
  this.server.listen(localPort, localHost, (err) => {
@@ -344,12 +344,12 @@ class SSHTunnel {
344
344
  else resolve();
345
345
  });
346
346
  });
347
-
348
- logger.info(`SOCKS proxy established`, {
347
+
348
+ logger.info('SOCKS proxy established', {
349
349
  local: `${localHost}:${localPort}`
350
350
  });
351
351
  }
352
-
352
+
353
353
  /**
354
354
  * Get tunnel information
355
355
  */
@@ -371,35 +371,35 @@ class SSHTunnel {
371
371
  activeConnections: this.connections.size
372
372
  };
373
373
  }
374
-
374
+
375
375
  /**
376
376
  * Close the tunnel
377
377
  */
378
378
  close() {
379
379
  logger.info(`Closing tunnel ${this.id}`);
380
-
380
+
381
381
  this.state = TUNNEL_STATES.CLOSED;
382
-
382
+
383
383
  // Close all active connections
384
384
  for (const conn of this.connections) {
385
385
  conn.destroy();
386
386
  }
387
387
  this.connections.clear();
388
-
388
+
389
389
  // Close server
390
390
  if (this.server) {
391
391
  this.server.close();
392
392
  this.server = null;
393
393
  }
394
-
394
+
395
395
  // Cancel remote forwarding if needed
396
396
  if (this.type === TUNNEL_TYPES.REMOTE) {
397
397
  this.ssh.unforwardIn(this.config.remoteHost, this.config.remotePort);
398
398
  }
399
-
399
+
400
400
  tunnels.delete(this.id);
401
401
  }
402
-
402
+
403
403
  /**
404
404
  * Reconnect tunnel
405
405
  */
@@ -409,14 +409,14 @@ class SSHTunnel {
409
409
  this.state = TUNNEL_STATES.FAILED;
410
410
  return false;
411
411
  }
412
-
412
+
413
413
  this.reconnectAttempts++;
414
414
  this.state = TUNNEL_STATES.RECONNECTING;
415
-
415
+
416
416
  logger.info(`Reconnecting tunnel ${this.id}`, {
417
417
  attempt: this.reconnectAttempts
418
418
  });
419
-
419
+
420
420
  try {
421
421
  await this.start();
422
422
  this.reconnectAttempts = 0;
@@ -425,11 +425,11 @@ class SSHTunnel {
425
425
  logger.error(`Reconnect failed for tunnel ${this.id}`, {
426
426
  error: error.message
427
427
  });
428
-
428
+
429
429
  // Retry with exponential backoff
430
430
  const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
431
431
  setTimeout(() => this.reconnect(), delay);
432
-
432
+
433
433
  return false;
434
434
  }
435
435
  }
@@ -440,37 +440,37 @@ class SSHTunnel {
440
440
  */
441
441
  export async function createTunnel(serverName, ssh, config) {
442
442
  const tunnelId = `tunnel_${Date.now()}_${uuidv4().substring(0, 8)}`;
443
-
443
+
444
444
  // Validate config
445
445
  if (!config.type || !Object.values(TUNNEL_TYPES).includes(config.type)) {
446
446
  throw new Error(`Invalid tunnel type: ${config.type}`);
447
447
  }
448
-
448
+
449
449
  // Set defaults
450
450
  config.localHost = config.localHost || '127.0.0.1';
451
-
451
+
452
452
  if (config.type !== TUNNEL_TYPES.DYNAMIC) {
453
453
  if (!config.remoteHost || !config.remotePort) {
454
454
  throw new Error('Remote host and port required for port forwarding');
455
455
  }
456
456
  }
457
-
457
+
458
458
  if (!config.localPort) {
459
459
  throw new Error('Local port required');
460
460
  }
461
-
461
+
462
462
  const tunnel = new SSHTunnel(tunnelId, serverName, ssh, config);
463
463
  tunnels.set(tunnelId, tunnel);
464
-
464
+
465
465
  try {
466
466
  await tunnel.start();
467
-
467
+
468
468
  logger.info('SSH tunnel created', {
469
469
  id: tunnelId,
470
470
  type: config.type,
471
471
  server: serverName
472
472
  });
473
-
473
+
474
474
  return tunnel;
475
475
  } catch (error) {
476
476
  tunnels.delete(tunnelId);
@@ -483,11 +483,11 @@ export async function createTunnel(serverName, ssh, config) {
483
483
  */
484
484
  export function getTunnel(tunnelId) {
485
485
  const tunnel = tunnels.get(tunnelId);
486
-
486
+
487
487
  if (!tunnel) {
488
488
  throw new Error(`Tunnel ${tunnelId} not found`);
489
489
  }
490
-
490
+
491
491
  return tunnel;
492
492
  }
493
493
 
@@ -496,7 +496,7 @@ export function getTunnel(tunnelId) {
496
496
  */
497
497
  export function listTunnels(serverName = null) {
498
498
  const activeTunnels = [];
499
-
499
+
500
500
  for (const [id, tunnel] of tunnels.entries()) {
501
501
  if (tunnel.state !== TUNNEL_STATES.CLOSED) {
502
502
  if (!serverName || tunnel.serverName === serverName) {
@@ -504,7 +504,7 @@ export function listTunnels(serverName = null) {
504
504
  }
505
505
  }
506
506
  }
507
-
507
+
508
508
  return activeTunnels;
509
509
  }
510
510
 
@@ -513,11 +513,11 @@ export function listTunnels(serverName = null) {
513
513
  */
514
514
  export function closeTunnel(tunnelId) {
515
515
  const tunnel = tunnels.get(tunnelId);
516
-
516
+
517
517
  if (!tunnel) {
518
518
  throw new Error(`Tunnel ${tunnelId} not found`);
519
519
  }
520
-
520
+
521
521
  tunnel.close();
522
522
  return true;
523
523
  }
@@ -527,14 +527,14 @@ export function closeTunnel(tunnelId) {
527
527
  */
528
528
  export function closeServerTunnels(serverName) {
529
529
  let closedCount = 0;
530
-
530
+
531
531
  for (const [id, tunnel] of tunnels.entries()) {
532
532
  if (tunnel.serverName === serverName) {
533
533
  tunnel.close();
534
534
  closedCount++;
535
535
  }
536
536
  }
537
-
537
+
538
538
  return closedCount;
539
539
  }
540
540
 
@@ -544,16 +544,16 @@ export function closeServerTunnels(serverName) {
544
544
  export function monitorTunnels() {
545
545
  const now = Date.now();
546
546
  const healthTimeout = 60 * 1000; // 1 minute
547
-
547
+
548
548
  for (const [id, tunnel] of tunnels.entries()) {
549
549
  if (tunnel.state === TUNNEL_STATES.ACTIVE) {
550
550
  const idle = now - tunnel.lastActivity.getTime();
551
-
551
+
552
552
  // Check if tunnel is still healthy
553
553
  if (idle > healthTimeout && tunnel.connections.size === 0) {
554
554
  logger.debug(`Tunnel ${id} idle for ${idle}ms`);
555
555
  }
556
-
556
+
557
557
  // Auto-reconnect failed tunnels
558
558
  if (tunnel.state === TUNNEL_STATES.FAILED) {
559
559
  tunnel.reconnect();
@@ -573,4 +573,4 @@ export default {
573
573
  closeServerTunnels,
574
574
  TUNNEL_TYPES,
575
575
  TUNNEL_STATES
576
- };
576
+ };