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.
- package/.claude/settings.local.json +10 -1
- package/CHANGELOG.md +63 -0
- package/CLAUDE.md +16 -0
- package/README.md +117 -4
- package/cli/commands/server.sh +27 -5
- package/cli/commands/tools.sh +603 -0
- package/cli/lib/config.sh +104 -14
- package/cli/lib/menu.sh +124 -0
- package/cli/ssh-manager +17 -7
- package/context-test.txt +1 -0
- package/docs/SOCIAL_MEDIA_ANNOUNCEMENT.md +385 -0
- package/docs/TOOL_MANAGEMENT.md +473 -0
- package/docs/TROUBLESHOOTING.md +143 -0
- package/package.json +5 -4
- package/scripts/validate.sh +12 -0
- package/src/backup-manager.js +12 -12
- package/src/command-aliases.js +16 -16
- package/src/config-loader.js +1 -1
- package/src/config.js +70 -0
- package/src/database-manager.js +38 -40
- package/src/deploy-helper.js +12 -13
- package/src/health-monitor.js +26 -26
- package/src/hooks-system.js +43 -43
- package/src/index.js +899 -841
- package/src/logger.js +53 -53
- package/src/profile-loader.js +14 -14
- package/src/server-aliases.js +11 -11
- package/src/server-groups.js +98 -97
- package/src/session-manager.js +59 -58
- package/src/ssh-key-manager.js +4 -3
- package/src/ssh-manager.js +46 -24
- package/src/tool-config-manager.js +434 -0
- package/src/tool-registry.js +196 -0
- package/src/tunnel-manager.js +98 -98
package/src/session-manager.js
CHANGED
|
@@ -36,7 +36,7 @@ class SSHSession {
|
|
|
36
36
|
this.outputBuffer = '';
|
|
37
37
|
this.errorBuffer = '';
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
/**
|
|
41
41
|
* Initialize the session with a shell
|
|
42
42
|
*/
|
|
@@ -45,19 +45,19 @@ class SSHSession {
|
|
|
45
45
|
logger.info(`Initializing SSH session ${this.id}`, {
|
|
46
46
|
server: this.serverName
|
|
47
47
|
});
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
// Start an interactive shell
|
|
50
50
|
this.shell = await this.ssh.requestShell({
|
|
51
51
|
term: 'xterm-256color',
|
|
52
52
|
cols: 80,
|
|
53
53
|
rows: 24
|
|
54
54
|
});
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
// Setup event handlers
|
|
57
57
|
this.shell.on('data', (data) => {
|
|
58
58
|
this.outputBuffer += data.toString();
|
|
59
59
|
this.lastActivity = new Date();
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
// Log output in verbose mode
|
|
62
62
|
if (logger.verbose) {
|
|
63
63
|
logger.debug(`Session ${this.id} output`, {
|
|
@@ -65,33 +65,34 @@ class SSHSession {
|
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
this.shell.on('close', () => {
|
|
70
70
|
logger.info(`Session ${this.id} shell closed`);
|
|
71
71
|
this.state = SESSION_STATES.CLOSED;
|
|
72
72
|
this.cleanup();
|
|
73
73
|
});
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
this.shell.stderr.on('data', (data) => {
|
|
76
76
|
this.errorBuffer += data.toString();
|
|
77
77
|
logger.warn(`Session ${this.id} stderr`, {
|
|
78
78
|
error: data.toString()
|
|
79
79
|
});
|
|
80
80
|
});
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
// Wait for shell prompt
|
|
83
83
|
await this.waitForPrompt();
|
|
84
|
-
|
|
84
|
+
|
|
85
|
+
// Allow context queries through standard execute flow
|
|
86
|
+
this.state = SESSION_STATES.READY;
|
|
87
|
+
|
|
85
88
|
// Get initial working directory
|
|
86
89
|
await this.updateContext();
|
|
87
|
-
|
|
88
|
-
this.state = SESSION_STATES.READY;
|
|
89
|
-
|
|
90
|
+
|
|
90
91
|
logger.info(`Session ${this.id} initialized`, {
|
|
91
92
|
server: this.serverName,
|
|
92
93
|
cwd: this.context.cwd
|
|
93
94
|
});
|
|
94
|
-
|
|
95
|
+
|
|
95
96
|
} catch (error) {
|
|
96
97
|
this.state = SESSION_STATES.ERROR;
|
|
97
98
|
logger.error(`Failed to initialize session ${this.id}`, {
|
|
@@ -100,26 +101,26 @@ class SSHSession {
|
|
|
100
101
|
throw error;
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
+
|
|
104
105
|
/**
|
|
105
106
|
* Wait for shell prompt
|
|
106
107
|
*/
|
|
107
108
|
async waitForPrompt(timeout = 5000) {
|
|
108
109
|
const startTime = Date.now();
|
|
109
|
-
|
|
110
|
+
|
|
110
111
|
while (Date.now() - startTime < timeout) {
|
|
111
112
|
// Check if we have a prompt (ends with $ or # typically)
|
|
112
113
|
if (this.outputBuffer.match(/[$#>]\s*$/)) {
|
|
113
114
|
return true;
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
+
|
|
116
117
|
// Wait a bit
|
|
117
118
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
118
119
|
}
|
|
119
|
-
|
|
120
|
+
|
|
120
121
|
throw new Error('Timeout waiting for shell prompt');
|
|
121
122
|
}
|
|
122
|
-
|
|
123
|
+
|
|
123
124
|
/**
|
|
124
125
|
* Update session context (pwd, env)
|
|
125
126
|
*/
|
|
@@ -130,7 +131,7 @@ class SSHSession {
|
|
|
130
131
|
if (pwdResult.success) {
|
|
131
132
|
this.context.cwd = pwdResult.output.trim();
|
|
132
133
|
}
|
|
133
|
-
|
|
134
|
+
|
|
134
135
|
// Get environment variables (selective)
|
|
135
136
|
const envResult = await this.execute('echo $PATH:$USER:$HOME', { silent: true });
|
|
136
137
|
if (envResult.success) {
|
|
@@ -143,7 +144,7 @@ class SSHSession {
|
|
|
143
144
|
});
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
|
-
|
|
147
|
+
|
|
147
148
|
/**
|
|
148
149
|
* Execute a command in the session
|
|
149
150
|
*/
|
|
@@ -151,15 +152,15 @@ class SSHSession {
|
|
|
151
152
|
if (this.state !== SESSION_STATES.READY) {
|
|
152
153
|
throw new Error(`Session ${this.id} is not ready (state: ${this.state})`);
|
|
153
154
|
}
|
|
154
|
-
|
|
155
|
+
|
|
155
156
|
this.state = SESSION_STATES.BUSY;
|
|
156
157
|
this.lastActivity = new Date();
|
|
157
|
-
|
|
158
|
+
|
|
158
159
|
try {
|
|
159
160
|
// Clear buffers
|
|
160
161
|
this.outputBuffer = '';
|
|
161
162
|
this.errorBuffer = '';
|
|
162
|
-
|
|
163
|
+
|
|
163
164
|
// Add to history unless silent
|
|
164
165
|
if (!options.silent) {
|
|
165
166
|
this.context.history.push({
|
|
@@ -167,53 +168,53 @@ class SSHSession {
|
|
|
167
168
|
timestamp: new Date(),
|
|
168
169
|
cwd: this.context.cwd
|
|
169
170
|
});
|
|
170
|
-
|
|
171
|
+
|
|
171
172
|
logger.info(`Session ${this.id} executing`, {
|
|
172
173
|
command: command.substring(0, 100),
|
|
173
174
|
server: this.serverName
|
|
174
175
|
});
|
|
175
176
|
}
|
|
176
|
-
|
|
177
|
+
|
|
177
178
|
// Send command
|
|
178
179
|
this.shell.write(command + '\n');
|
|
179
|
-
|
|
180
|
+
|
|
180
181
|
// Wait for command to complete
|
|
181
182
|
await this.waitForPrompt(options.timeout || 30000);
|
|
182
|
-
|
|
183
|
+
|
|
183
184
|
// Parse output (remove command echo and prompt)
|
|
184
185
|
let output = this.outputBuffer;
|
|
185
|
-
|
|
186
|
+
|
|
186
187
|
// Remove the command echo (first line)
|
|
187
188
|
const lines = output.split('\n');
|
|
188
189
|
if (lines[0].includes(command)) {
|
|
189
190
|
lines.shift();
|
|
190
191
|
}
|
|
191
|
-
|
|
192
|
+
|
|
192
193
|
// Remove the prompt (last line)
|
|
193
194
|
const lastLine = lines[lines.length - 1];
|
|
194
195
|
if (lastLine.match(/[$#>]\s*$/)) {
|
|
195
196
|
lines.pop();
|
|
196
197
|
}
|
|
197
|
-
|
|
198
|
+
|
|
198
199
|
output = lines.join('\n').trim();
|
|
199
|
-
|
|
200
|
+
|
|
200
201
|
// Check for command success (basic heuristic)
|
|
201
202
|
const success = !this.errorBuffer && !output.includes('command not found');
|
|
202
|
-
|
|
203
|
+
|
|
203
204
|
// Update context if command might have changed it
|
|
204
205
|
if (command.startsWith('cd ') || command.startsWith('export ')) {
|
|
205
206
|
await this.updateContext();
|
|
206
207
|
}
|
|
207
|
-
|
|
208
|
+
|
|
208
209
|
this.state = SESSION_STATES.READY;
|
|
209
|
-
|
|
210
|
+
|
|
210
211
|
return {
|
|
211
212
|
success,
|
|
212
213
|
output,
|
|
213
214
|
error: this.errorBuffer,
|
|
214
215
|
session: this.id
|
|
215
216
|
};
|
|
216
|
-
|
|
217
|
+
|
|
217
218
|
} catch (error) {
|
|
218
219
|
this.state = SESSION_STATES.ERROR;
|
|
219
220
|
logger.error(`Session ${this.id} execution failed`, {
|
|
@@ -223,7 +224,7 @@ class SSHSession {
|
|
|
223
224
|
throw error;
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
|
-
|
|
227
|
+
|
|
227
228
|
/**
|
|
228
229
|
* Set session variable
|
|
229
230
|
*/
|
|
@@ -231,14 +232,14 @@ class SSHSession {
|
|
|
231
232
|
this.context.variables[name] = value;
|
|
232
233
|
this.lastActivity = new Date();
|
|
233
234
|
}
|
|
234
|
-
|
|
235
|
+
|
|
235
236
|
/**
|
|
236
237
|
* Get session variable
|
|
237
238
|
*/
|
|
238
239
|
getVariable(name) {
|
|
239
240
|
return this.context.variables[name];
|
|
240
241
|
}
|
|
241
|
-
|
|
242
|
+
|
|
242
243
|
/**
|
|
243
244
|
* Get session info
|
|
244
245
|
*/
|
|
@@ -255,23 +256,23 @@ class SSHSession {
|
|
|
255
256
|
variables: Object.keys(this.context.variables)
|
|
256
257
|
};
|
|
257
258
|
}
|
|
258
|
-
|
|
259
|
+
|
|
259
260
|
/**
|
|
260
261
|
* Close the session
|
|
261
262
|
*/
|
|
262
263
|
close() {
|
|
263
264
|
logger.info(`Closing session ${this.id}`);
|
|
264
|
-
|
|
265
|
+
|
|
265
266
|
if (this.shell) {
|
|
266
267
|
this.shell.write('exit\n');
|
|
267
268
|
this.shell.end();
|
|
268
269
|
this.shell = null;
|
|
269
270
|
}
|
|
270
|
-
|
|
271
|
+
|
|
271
272
|
this.state = SESSION_STATES.CLOSED;
|
|
272
273
|
this.cleanup();
|
|
273
274
|
}
|
|
274
|
-
|
|
275
|
+
|
|
275
276
|
/**
|
|
276
277
|
* Cleanup resources
|
|
277
278
|
*/
|
|
@@ -288,18 +289,18 @@ class SSHSession {
|
|
|
288
289
|
*/
|
|
289
290
|
export async function createSession(serverName, ssh) {
|
|
290
291
|
const sessionId = `ssh_${Date.now()}_${uuidv4().substring(0, 8)}`;
|
|
291
|
-
|
|
292
|
+
|
|
292
293
|
const session = new SSHSession(sessionId, serverName, ssh);
|
|
293
294
|
sessions.set(sessionId, session);
|
|
294
|
-
|
|
295
|
+
|
|
295
296
|
try {
|
|
296
297
|
await session.initialize();
|
|
297
|
-
|
|
298
|
+
|
|
298
299
|
logger.info('SSH session created', {
|
|
299
300
|
id: sessionId,
|
|
300
301
|
server: serverName
|
|
301
302
|
});
|
|
302
|
-
|
|
303
|
+
|
|
303
304
|
return session;
|
|
304
305
|
} catch (error) {
|
|
305
306
|
sessions.delete(sessionId);
|
|
@@ -312,15 +313,15 @@ export async function createSession(serverName, ssh) {
|
|
|
312
313
|
*/
|
|
313
314
|
export function getSession(sessionId) {
|
|
314
315
|
const session = sessions.get(sessionId);
|
|
315
|
-
|
|
316
|
+
|
|
316
317
|
if (!session) {
|
|
317
318
|
throw new Error(`Session ${sessionId} not found`);
|
|
318
319
|
}
|
|
319
|
-
|
|
320
|
+
|
|
320
321
|
if (session.state === SESSION_STATES.CLOSED) {
|
|
321
322
|
throw new Error(`Session ${sessionId} is closed`);
|
|
322
323
|
}
|
|
323
|
-
|
|
324
|
+
|
|
324
325
|
return session;
|
|
325
326
|
}
|
|
326
327
|
|
|
@@ -329,13 +330,13 @@ export function getSession(sessionId) {
|
|
|
329
330
|
*/
|
|
330
331
|
export function listSessions() {
|
|
331
332
|
const activeSessions = [];
|
|
332
|
-
|
|
333
|
+
|
|
333
334
|
for (const [id, session] of sessions.entries()) {
|
|
334
335
|
if (session.state !== SESSION_STATES.CLOSED) {
|
|
335
336
|
activeSessions.push(session.getInfo());
|
|
336
337
|
}
|
|
337
338
|
}
|
|
338
|
-
|
|
339
|
+
|
|
339
340
|
return activeSessions;
|
|
340
341
|
}
|
|
341
342
|
|
|
@@ -344,11 +345,11 @@ export function listSessions() {
|
|
|
344
345
|
*/
|
|
345
346
|
export function closeSession(sessionId) {
|
|
346
347
|
const session = sessions.get(sessionId);
|
|
347
|
-
|
|
348
|
+
|
|
348
349
|
if (!session) {
|
|
349
350
|
throw new Error(`Session ${sessionId} not found`);
|
|
350
351
|
}
|
|
351
|
-
|
|
352
|
+
|
|
352
353
|
session.close();
|
|
353
354
|
return true;
|
|
354
355
|
}
|
|
@@ -358,14 +359,14 @@ export function closeSession(sessionId) {
|
|
|
358
359
|
*/
|
|
359
360
|
export function closeServerSessions(serverName) {
|
|
360
361
|
let closedCount = 0;
|
|
361
|
-
|
|
362
|
+
|
|
362
363
|
for (const [id, session] of sessions.entries()) {
|
|
363
364
|
if (session.serverName === serverName) {
|
|
364
365
|
session.close();
|
|
365
366
|
closedCount++;
|
|
366
367
|
}
|
|
367
368
|
}
|
|
368
|
-
|
|
369
|
+
|
|
369
370
|
return closedCount;
|
|
370
371
|
}
|
|
371
372
|
|
|
@@ -375,10 +376,10 @@ export function closeServerSessions(serverName) {
|
|
|
375
376
|
export function cleanupSessions(maxAge = 30 * 60 * 1000) { // 30 minutes default
|
|
376
377
|
const now = Date.now();
|
|
377
378
|
let cleanedCount = 0;
|
|
378
|
-
|
|
379
|
+
|
|
379
380
|
for (const [id, session] of sessions.entries()) {
|
|
380
381
|
const age = now - session.lastActivity.getTime();
|
|
381
|
-
|
|
382
|
+
|
|
382
383
|
if (age > maxAge) {
|
|
383
384
|
logger.info(`Cleaning up inactive session ${id}`, {
|
|
384
385
|
age: Math.floor(age / 1000) + 's'
|
|
@@ -387,7 +388,7 @@ export function cleanupSessions(maxAge = 30 * 60 * 1000) { // 30 minutes default
|
|
|
387
388
|
cleanedCount++;
|
|
388
389
|
}
|
|
389
390
|
}
|
|
390
|
-
|
|
391
|
+
|
|
391
392
|
return cleanedCount;
|
|
392
393
|
}
|
|
393
394
|
|
|
@@ -407,4 +408,4 @@ export default {
|
|
|
407
408
|
closeServerSessions,
|
|
408
409
|
cleanupSessions,
|
|
409
410
|
SESSION_STATES
|
|
410
|
-
};
|
|
411
|
+
};
|
package/src/ssh-key-manager.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { execSync, spawn } from 'child_process';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import os from 'os';
|
|
4
5
|
import crypto from 'crypto';
|
|
5
6
|
import { logger } from './logger.js';
|
|
6
7
|
|
|
7
8
|
// Path to known_hosts file
|
|
8
|
-
const KNOWN_HOSTS_PATH = path.join(
|
|
9
|
-
const KNOWN_HOSTS_BACKUP = path.join(
|
|
9
|
+
const KNOWN_HOSTS_PATH = path.join(os.homedir(), '.ssh', 'known_hosts');
|
|
10
|
+
const KNOWN_HOSTS_BACKUP = path.join(os.homedir(), '.ssh', 'known_hosts.mcp-backup');
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Parse a known_hosts entry
|
|
@@ -375,4 +376,4 @@ export async function handleSSHKeyError(stderr, options = {}) {
|
|
|
375
376
|
...hostInfo,
|
|
376
377
|
message: `Host key has changed for ${hostInfo.host}:${hostInfo.port}. Use ssh_key_manage tool to verify and update the key.`
|
|
377
378
|
};
|
|
378
|
-
}
|
|
379
|
+
}
|
package/src/ssh-manager.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Client } from 'ssh2';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
+
import os from 'os';
|
|
3
4
|
import { promisify } from 'util';
|
|
4
5
|
import crypto from 'crypto';
|
|
5
6
|
import { isHostKnown, getCurrentHostKey, addHostKey, updateHostKey } from './ssh-key-manager.js';
|
|
6
7
|
import { configLoader } from './config-loader.js';
|
|
8
|
+
import { logger } from './logger.js';
|
|
7
9
|
|
|
8
10
|
class SSHManager {
|
|
9
11
|
constructor(config) {
|
|
@@ -48,7 +50,7 @@ class SSHManager {
|
|
|
48
50
|
},
|
|
49
51
|
debug: (info) => {
|
|
50
52
|
if (info.includes('Handshake') || info.includes('error')) {
|
|
51
|
-
|
|
53
|
+
logger.debug('SSH2 Debug', { info });
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
};
|
|
@@ -63,23 +65,27 @@ class SSHManager {
|
|
|
63
65
|
if (isHostKnown(host, port)) {
|
|
64
66
|
// For now, accept all known hosts
|
|
65
67
|
// TODO: Implement proper fingerprint comparison once we understand SSH2's hash format
|
|
66
|
-
|
|
68
|
+
logger.info('Host key verified', { host, port });
|
|
67
69
|
return true;
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
// Host is not known
|
|
71
|
-
|
|
73
|
+
logger.info('New host detected', { host, port });
|
|
72
74
|
|
|
73
75
|
// If autoAcceptHostKey is enabled, accept and add the key
|
|
74
76
|
if (this.autoAcceptHostKey) {
|
|
75
|
-
|
|
77
|
+
logger.info('Auto-accept host key', { host, port });
|
|
76
78
|
// Schedule key addition after connection
|
|
77
79
|
setImmediate(async () => {
|
|
78
80
|
try {
|
|
79
81
|
await addHostKey(host, port);
|
|
80
|
-
|
|
82
|
+
logger.info('Host key added', { host, port });
|
|
81
83
|
} catch (err) {
|
|
82
|
-
|
|
84
|
+
logger.warn('Failed to add host key', {
|
|
85
|
+
host,
|
|
86
|
+
port,
|
|
87
|
+
error: err.message
|
|
88
|
+
});
|
|
83
89
|
}
|
|
84
90
|
});
|
|
85
91
|
return true;
|
|
@@ -87,7 +93,7 @@ class SSHManager {
|
|
|
87
93
|
|
|
88
94
|
// For backward compatibility, accept new hosts by default
|
|
89
95
|
// In production, you might want to prompt the user or check a whitelist
|
|
90
|
-
|
|
96
|
+
logger.warn('Auto-accepting new host', { host, port });
|
|
91
97
|
return true;
|
|
92
98
|
};
|
|
93
99
|
}
|
|
@@ -95,7 +101,7 @@ class SSHManager {
|
|
|
95
101
|
// Add authentication (support both keyPath and keypath for compatibility)
|
|
96
102
|
const keyPath = this.config.keyPath || this.config.keypath;
|
|
97
103
|
if (keyPath) {
|
|
98
|
-
const resolvedKeyPath = keyPath.replace('~',
|
|
104
|
+
const resolvedKeyPath = keyPath.replace('~', os.homedir());
|
|
99
105
|
connConfig.privateKey = fs.readFileSync(resolvedKeyPath);
|
|
100
106
|
} else if (this.config.password) {
|
|
101
107
|
connConfig.password = this.config.password;
|
|
@@ -125,7 +131,7 @@ class SSHManager {
|
|
|
125
131
|
timeoutId = setTimeout(() => {
|
|
126
132
|
if (!completed) {
|
|
127
133
|
completed = true;
|
|
128
|
-
|
|
134
|
+
|
|
129
135
|
// Try multiple ways to kill the stream
|
|
130
136
|
if (stream) {
|
|
131
137
|
try {
|
|
@@ -136,7 +142,7 @@ class SSHManager {
|
|
|
136
142
|
// Ignore errors
|
|
137
143
|
}
|
|
138
144
|
}
|
|
139
|
-
|
|
145
|
+
|
|
140
146
|
// Kill the entire client connection as last resort
|
|
141
147
|
try {
|
|
142
148
|
this.client.end();
|
|
@@ -144,7 +150,7 @@ class SSHManager {
|
|
|
144
150
|
} catch (e) {
|
|
145
151
|
// Ignore errors
|
|
146
152
|
}
|
|
147
|
-
|
|
153
|
+
|
|
148
154
|
reject(new Error(`Command timeout after ${timeout}ms: ${command.substring(0, 100)}...`));
|
|
149
155
|
}
|
|
150
156
|
}, timeout);
|
|
@@ -237,6 +243,22 @@ class SSHManager {
|
|
|
237
243
|
});
|
|
238
244
|
}
|
|
239
245
|
|
|
246
|
+
async requestShell(options = {}) {
|
|
247
|
+
if (!this.connected) {
|
|
248
|
+
throw new Error('Not connected to SSH server');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return new Promise((resolve, reject) => {
|
|
252
|
+
this.client.shell(options, (err, stream) => {
|
|
253
|
+
if (err) {
|
|
254
|
+
reject(err);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
resolve(stream);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
240
262
|
async getSFTP() {
|
|
241
263
|
if (this.sftp) return this.sftp;
|
|
242
264
|
|
|
@@ -258,12 +280,12 @@ class SSHManager {
|
|
|
258
280
|
}
|
|
259
281
|
|
|
260
282
|
let homeDir = null;
|
|
261
|
-
|
|
283
|
+
|
|
262
284
|
// Method 1: Try getent (most reliable)
|
|
263
285
|
try {
|
|
264
|
-
const result = await this.execCommand('getent passwd $USER | cut -d: -f6', {
|
|
265
|
-
timeout: 5000,
|
|
266
|
-
rawCommand: true
|
|
286
|
+
const result = await this.execCommand('getent passwd $USER | cut -d: -f6', {
|
|
287
|
+
timeout: 5000,
|
|
288
|
+
rawCommand: true
|
|
267
289
|
});
|
|
268
290
|
homeDir = result.stdout.trim();
|
|
269
291
|
if (homeDir && homeDir.startsWith('/')) {
|
|
@@ -306,9 +328,9 @@ class SSHManager {
|
|
|
306
328
|
|
|
307
329
|
// Method 4: Last resort - try cd ~ && pwd
|
|
308
330
|
try {
|
|
309
|
-
const result = await this.execCommand('cd ~ && pwd', {
|
|
310
|
-
timeout: 5000,
|
|
311
|
-
rawCommand: true
|
|
331
|
+
const result = await this.execCommand('cd ~ && pwd', {
|
|
332
|
+
timeout: 5000,
|
|
333
|
+
rawCommand: true
|
|
312
334
|
});
|
|
313
335
|
homeDir = result.stdout.trim();
|
|
314
336
|
if (homeDir && homeDir.startsWith('/')) {
|
|
@@ -343,7 +365,7 @@ class SSHManager {
|
|
|
343
365
|
throw new Error(`Failed to resolve home directory for path: ${remotePath}. ${err.message}`);
|
|
344
366
|
}
|
|
345
367
|
}
|
|
346
|
-
|
|
368
|
+
|
|
347
369
|
const sftp = await this.getSFTP();
|
|
348
370
|
return new Promise((resolve, reject) => {
|
|
349
371
|
// Check if local file exists and is readable
|
|
@@ -351,7 +373,7 @@ class SSHManager {
|
|
|
351
373
|
reject(new Error(`Local file does not exist: ${localPath}`));
|
|
352
374
|
return;
|
|
353
375
|
}
|
|
354
|
-
|
|
376
|
+
|
|
355
377
|
sftp.fastPut(localPath, resolvedRemotePath, (err) => {
|
|
356
378
|
if (err) reject(err);
|
|
357
379
|
else resolve();
|
|
@@ -380,7 +402,7 @@ class SSHManager {
|
|
|
380
402
|
throw new Error(`Failed to resolve home directory for path: ${remotePath}. ${err.message}`);
|
|
381
403
|
}
|
|
382
404
|
}
|
|
383
|
-
|
|
405
|
+
|
|
384
406
|
const sftp = await this.getSFTP();
|
|
385
407
|
return new Promise((resolve, reject) => {
|
|
386
408
|
sftp.fastGet(resolvedRemotePath, localPath, (err) => {
|
|
@@ -393,7 +415,7 @@ class SSHManager {
|
|
|
393
415
|
async putFiles(files, options = {}) {
|
|
394
416
|
const sftp = await this.getSFTP();
|
|
395
417
|
const results = [];
|
|
396
|
-
|
|
418
|
+
|
|
397
419
|
for (const file of files) {
|
|
398
420
|
try {
|
|
399
421
|
await this.putFile(file.local, file.remote);
|
|
@@ -403,7 +425,7 @@ class SSHManager {
|
|
|
403
425
|
if (options.stopOnError) break;
|
|
404
426
|
}
|
|
405
427
|
}
|
|
406
|
-
|
|
428
|
+
|
|
407
429
|
return results;
|
|
408
430
|
}
|
|
409
431
|
|
|
@@ -432,4 +454,4 @@ class SSHManager {
|
|
|
432
454
|
}
|
|
433
455
|
}
|
|
434
456
|
|
|
435
|
-
export default SSHManager;
|
|
457
|
+
export default SSHManager;
|