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,435 @@
|
|
|
1
|
+
import { Client } from 'ssh2';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
import { isHostKnown, getCurrentHostKey, addHostKey, updateHostKey } from './ssh-key-manager.js';
|
|
6
|
+
import { configLoader } from './config-loader.js';
|
|
7
|
+
|
|
8
|
+
class SSHManager {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.client = new Client();
|
|
12
|
+
this.connected = false;
|
|
13
|
+
this.sftp = null;
|
|
14
|
+
this.cachedHomeDir = null;
|
|
15
|
+
this.autoAcceptHostKey = config.autoAcceptHostKey || false;
|
|
16
|
+
this.hostKeyVerification = config.hostKeyVerification !== false; // Default true
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async connect() {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
this.client.on('ready', () => {
|
|
22
|
+
this.connected = true;
|
|
23
|
+
resolve();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
this.client.on('error', (err) => {
|
|
27
|
+
this.connected = false;
|
|
28
|
+
reject(err);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
this.client.on('end', () => {
|
|
32
|
+
this.connected = false;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Build connection config
|
|
36
|
+
const connConfig = {
|
|
37
|
+
host: this.config.host,
|
|
38
|
+
port: this.config.port || 22,
|
|
39
|
+
username: this.config.user,
|
|
40
|
+
readyTimeout: 60000, // Increased from 20000 to 60000 for slow connections
|
|
41
|
+
keepaliveInterval: 10000,
|
|
42
|
+
// Add compatibility options for problematic servers
|
|
43
|
+
algorithms: {
|
|
44
|
+
kex: ['ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1'],
|
|
45
|
+
cipher: ['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm', 'aes256-gcm', 'aes128-cbc', 'aes192-cbc', 'aes256-cbc'],
|
|
46
|
+
serverHostKey: ['ssh-rsa', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-ed25519'],
|
|
47
|
+
hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1']
|
|
48
|
+
},
|
|
49
|
+
debug: (info) => {
|
|
50
|
+
if (info.includes('Handshake') || info.includes('error')) {
|
|
51
|
+
console.log('SSH2 Debug:', info);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Add host key verification callback if enabled
|
|
57
|
+
if (this.hostKeyVerification) {
|
|
58
|
+
connConfig.hostVerifier = (hashedKey) => {
|
|
59
|
+
const port = this.config.port || 22;
|
|
60
|
+
const host = this.config.host;
|
|
61
|
+
|
|
62
|
+
// Check if host is already known
|
|
63
|
+
if (isHostKnown(host, port)) {
|
|
64
|
+
// For now, accept all known hosts
|
|
65
|
+
// TODO: Implement proper fingerprint comparison once we understand SSH2's hash format
|
|
66
|
+
console.log(`✅ Host ${host}:${port} is known, accepting connection`);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Host is not known
|
|
71
|
+
console.log(`🔑 New host detected: ${host}:${port}`);
|
|
72
|
+
|
|
73
|
+
// If autoAcceptHostKey is enabled, accept and add the key
|
|
74
|
+
if (this.autoAcceptHostKey) {
|
|
75
|
+
console.log(`Auto-accepting and adding host key for ${host}:${port}`);
|
|
76
|
+
// Schedule key addition after connection
|
|
77
|
+
setImmediate(async () => {
|
|
78
|
+
try {
|
|
79
|
+
await addHostKey(host, port);
|
|
80
|
+
console.log(`✅ Host key added for ${host}:${port}`);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.warn(`Failed to add host key: ${err.message}`);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// For backward compatibility, accept new hosts by default
|
|
89
|
+
// In production, you might want to prompt the user or check a whitelist
|
|
90
|
+
console.log(`Auto-accepting new host ${host}:${port} (set autoAcceptHostKey:false to reject)`);
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Add authentication (support both keyPath and keypath for compatibility)
|
|
96
|
+
const keyPath = this.config.keyPath || this.config.keypath;
|
|
97
|
+
if (keyPath) {
|
|
98
|
+
const resolvedKeyPath = keyPath.replace('~', process.env.HOME);
|
|
99
|
+
connConfig.privateKey = fs.readFileSync(resolvedKeyPath);
|
|
100
|
+
} else if (this.config.password) {
|
|
101
|
+
connConfig.password = this.config.password;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.client.connect(connConfig);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async execCommand(command, options = {}) {
|
|
109
|
+
if (!this.connected) {
|
|
110
|
+
throw new Error('Not connected to SSH server');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const { timeout = 30000, cwd, rawCommand = false } = options;
|
|
114
|
+
const fullCommand = (cwd && !rawCommand) ? `cd ${cwd} && ${command}` : command;
|
|
115
|
+
|
|
116
|
+
return new Promise((resolve, reject) => {
|
|
117
|
+
let stdout = '';
|
|
118
|
+
let stderr = '';
|
|
119
|
+
let completed = false;
|
|
120
|
+
let stream = null;
|
|
121
|
+
let timeoutId = null;
|
|
122
|
+
|
|
123
|
+
// Setup timeout first
|
|
124
|
+
if (timeout > 0) {
|
|
125
|
+
timeoutId = setTimeout(() => {
|
|
126
|
+
if (!completed) {
|
|
127
|
+
completed = true;
|
|
128
|
+
|
|
129
|
+
// Try multiple ways to kill the stream
|
|
130
|
+
if (stream) {
|
|
131
|
+
try {
|
|
132
|
+
stream.write('\x03'); // Send Ctrl+C
|
|
133
|
+
stream.end();
|
|
134
|
+
stream.destroy();
|
|
135
|
+
} catch (e) {
|
|
136
|
+
// Ignore errors
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Kill the entire client connection as last resort
|
|
141
|
+
try {
|
|
142
|
+
this.client.end();
|
|
143
|
+
this.connected = false;
|
|
144
|
+
} catch (e) {
|
|
145
|
+
// Ignore errors
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
reject(new Error(`Command timeout after ${timeout}ms: ${command.substring(0, 100)}...`));
|
|
149
|
+
}
|
|
150
|
+
}, timeout);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.client.exec(fullCommand, (err, streamObj) => {
|
|
154
|
+
if (err) {
|
|
155
|
+
completed = true;
|
|
156
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
157
|
+
reject(err);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
stream = streamObj;
|
|
162
|
+
|
|
163
|
+
stream.on('close', (code, signal) => {
|
|
164
|
+
if (!completed) {
|
|
165
|
+
completed = true;
|
|
166
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
167
|
+
resolve({
|
|
168
|
+
stdout,
|
|
169
|
+
stderr,
|
|
170
|
+
code: code || 0,
|
|
171
|
+
signal
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
stream.on('data', (data) => {
|
|
177
|
+
stdout += data.toString();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
stream.stderr.on('data', (data) => {
|
|
181
|
+
stderr += data.toString();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
stream.on('error', (err) => {
|
|
185
|
+
if (!completed) {
|
|
186
|
+
completed = true;
|
|
187
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
188
|
+
reject(err);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async execCommandStream(command, options = {}) {
|
|
196
|
+
if (!this.connected) {
|
|
197
|
+
throw new Error('Not connected to SSH server');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const { cwd, onStdout, onStderr } = options;
|
|
201
|
+
const fullCommand = cwd ? `cd ${cwd} && ${command}` : command;
|
|
202
|
+
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
this.client.exec(fullCommand, (err, stream) => {
|
|
205
|
+
if (err) {
|
|
206
|
+
reject(err);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let stdout = '';
|
|
211
|
+
let stderr = '';
|
|
212
|
+
|
|
213
|
+
stream.on('close', (code, signal) => {
|
|
214
|
+
resolve({
|
|
215
|
+
stdout,
|
|
216
|
+
stderr,
|
|
217
|
+
code: code || 0,
|
|
218
|
+
signal,
|
|
219
|
+
stream
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
stream.on('data', (data) => {
|
|
224
|
+
const chunk = data.toString();
|
|
225
|
+
stdout += chunk;
|
|
226
|
+
if (onStdout) onStdout(chunk);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
stream.stderr.on('data', (data) => {
|
|
230
|
+
const chunk = data.toString();
|
|
231
|
+
stderr += chunk;
|
|
232
|
+
if (onStderr) onStderr(chunk);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
stream.on('error', reject);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async getSFTP() {
|
|
241
|
+
if (this.sftp) return this.sftp;
|
|
242
|
+
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
this.client.sftp((err, sftp) => {
|
|
245
|
+
if (err) {
|
|
246
|
+
reject(err);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this.sftp = sftp;
|
|
250
|
+
resolve(sftp);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async resolveHomePath() {
|
|
256
|
+
if (this.cachedHomeDir) {
|
|
257
|
+
return this.cachedHomeDir;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let homeDir = null;
|
|
261
|
+
|
|
262
|
+
// Method 1: Try getent (most reliable)
|
|
263
|
+
try {
|
|
264
|
+
const result = await this.execCommand('getent passwd $USER | cut -d: -f6', {
|
|
265
|
+
timeout: 5000,
|
|
266
|
+
rawCommand: true
|
|
267
|
+
});
|
|
268
|
+
homeDir = result.stdout.trim();
|
|
269
|
+
if (homeDir && homeDir.startsWith('/')) {
|
|
270
|
+
this.cachedHomeDir = homeDir;
|
|
271
|
+
return homeDir;
|
|
272
|
+
}
|
|
273
|
+
} catch (err) {
|
|
274
|
+
// getent might not be available, try next method
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Method 2: Try env -i to get clean HOME
|
|
278
|
+
try {
|
|
279
|
+
const result = await this.execCommand('env -i HOME=$HOME bash -c "echo $HOME"', {
|
|
280
|
+
timeout: 5000,
|
|
281
|
+
rawCommand: true
|
|
282
|
+
});
|
|
283
|
+
homeDir = result.stdout.trim();
|
|
284
|
+
if (homeDir && homeDir.startsWith('/')) {
|
|
285
|
+
this.cachedHomeDir = homeDir;
|
|
286
|
+
return homeDir;
|
|
287
|
+
}
|
|
288
|
+
} catch (err) {
|
|
289
|
+
// env method failed, try next
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Method 3: Parse /etc/passwd directly
|
|
293
|
+
try {
|
|
294
|
+
const result = await this.execCommand('grep "^$USER:" /etc/passwd | cut -d: -f6', {
|
|
295
|
+
timeout: 5000,
|
|
296
|
+
rawCommand: true
|
|
297
|
+
});
|
|
298
|
+
homeDir = result.stdout.trim();
|
|
299
|
+
if (homeDir && homeDir.startsWith('/')) {
|
|
300
|
+
this.cachedHomeDir = homeDir;
|
|
301
|
+
return homeDir;
|
|
302
|
+
}
|
|
303
|
+
} catch (err) {
|
|
304
|
+
// /etc/passwd parsing failed, try last resort
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Method 4: Last resort - try cd ~ && pwd
|
|
308
|
+
try {
|
|
309
|
+
const result = await this.execCommand('cd ~ && pwd', {
|
|
310
|
+
timeout: 5000,
|
|
311
|
+
rawCommand: true
|
|
312
|
+
});
|
|
313
|
+
homeDir = result.stdout.trim();
|
|
314
|
+
if (homeDir && homeDir.startsWith('/')) {
|
|
315
|
+
this.cachedHomeDir = homeDir;
|
|
316
|
+
return homeDir;
|
|
317
|
+
}
|
|
318
|
+
} catch (err) {
|
|
319
|
+
// All methods failed
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
throw new Error('Unable to determine home directory on remote server');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async putFile(localPath, remotePath) {
|
|
326
|
+
// SFTP doesn't resolve ~ automatically, we need to get the real path
|
|
327
|
+
let resolvedRemotePath = remotePath;
|
|
328
|
+
if (remotePath.includes('~')) {
|
|
329
|
+
try {
|
|
330
|
+
const homeDir = await this.resolveHomePath();
|
|
331
|
+
// Replace ~ with the actual home directory
|
|
332
|
+
// Handle both ~/path and ~ alone
|
|
333
|
+
if (remotePath === '~') {
|
|
334
|
+
resolvedRemotePath = homeDir;
|
|
335
|
+
} else if (remotePath.startsWith('~/')) {
|
|
336
|
+
resolvedRemotePath = homeDir + remotePath.substring(1);
|
|
337
|
+
} else {
|
|
338
|
+
// If ~ is not at the beginning, don't replace it
|
|
339
|
+
resolvedRemotePath = remotePath;
|
|
340
|
+
}
|
|
341
|
+
} catch (err) {
|
|
342
|
+
// If we can't resolve home, throw a more descriptive error
|
|
343
|
+
throw new Error(`Failed to resolve home directory for path: ${remotePath}. ${err.message}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const sftp = await this.getSFTP();
|
|
348
|
+
return new Promise((resolve, reject) => {
|
|
349
|
+
// Check if local file exists and is readable
|
|
350
|
+
if (!fs.existsSync(localPath)) {
|
|
351
|
+
reject(new Error(`Local file does not exist: ${localPath}`));
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
sftp.fastPut(localPath, resolvedRemotePath, (err) => {
|
|
356
|
+
if (err) reject(err);
|
|
357
|
+
else resolve();
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async getFile(localPath, remotePath) {
|
|
363
|
+
// SFTP doesn't resolve ~ automatically, we need to get the real path
|
|
364
|
+
let resolvedRemotePath = remotePath;
|
|
365
|
+
if (remotePath.includes('~')) {
|
|
366
|
+
try {
|
|
367
|
+
const homeDir = await this.resolveHomePath();
|
|
368
|
+
// Replace ~ with the actual home directory
|
|
369
|
+
// Handle both ~/path and ~ alone
|
|
370
|
+
if (remotePath === '~') {
|
|
371
|
+
resolvedRemotePath = homeDir;
|
|
372
|
+
} else if (remotePath.startsWith('~/')) {
|
|
373
|
+
resolvedRemotePath = homeDir + remotePath.substring(1);
|
|
374
|
+
} else {
|
|
375
|
+
// If ~ is not at the beginning, don't replace it
|
|
376
|
+
resolvedRemotePath = remotePath;
|
|
377
|
+
}
|
|
378
|
+
} catch (err) {
|
|
379
|
+
// If we can't resolve home, throw a more descriptive error
|
|
380
|
+
throw new Error(`Failed to resolve home directory for path: ${remotePath}. ${err.message}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const sftp = await this.getSFTP();
|
|
385
|
+
return new Promise((resolve, reject) => {
|
|
386
|
+
sftp.fastGet(resolvedRemotePath, localPath, (err) => {
|
|
387
|
+
if (err) reject(err);
|
|
388
|
+
else resolve();
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async putFiles(files, options = {}) {
|
|
394
|
+
const sftp = await this.getSFTP();
|
|
395
|
+
const results = [];
|
|
396
|
+
|
|
397
|
+
for (const file of files) {
|
|
398
|
+
try {
|
|
399
|
+
await this.putFile(file.local, file.remote);
|
|
400
|
+
results.push({ ...file, success: true });
|
|
401
|
+
} catch (error) {
|
|
402
|
+
results.push({ ...file, success: false, error: error.message });
|
|
403
|
+
if (options.stopOnError) break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return results;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
isConnected() {
|
|
411
|
+
return this.connected && this.client && !this.client.destroyed;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
dispose() {
|
|
415
|
+
if (this.sftp) {
|
|
416
|
+
this.sftp.end();
|
|
417
|
+
this.sftp = null;
|
|
418
|
+
}
|
|
419
|
+
if (this.client) {
|
|
420
|
+
this.client.end();
|
|
421
|
+
this.connected = false;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async ping() {
|
|
426
|
+
try {
|
|
427
|
+
const result = await this.execCommand('echo "ping"', { timeout: 5000 });
|
|
428
|
+
return result.stdout.trim() === 'ping';
|
|
429
|
+
} catch (error) {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export default SSHManager;
|