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,378 @@
|
|
|
1
|
+
import { execSync, spawn } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
import { logger } from './logger.js';
|
|
6
|
+
|
|
7
|
+
// Path to known_hosts file
|
|
8
|
+
const KNOWN_HOSTS_PATH = path.join(process.env.HOME, '.ssh', 'known_hosts');
|
|
9
|
+
const KNOWN_HOSTS_BACKUP = path.join(process.env.HOME, '.ssh', 'known_hosts.mcp-backup');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Parse a known_hosts entry
|
|
13
|
+
*/
|
|
14
|
+
function parseKnownHostEntry(line) {
|
|
15
|
+
if (!line || line.startsWith('#')) return null;
|
|
16
|
+
|
|
17
|
+
const parts = line.split(' ');
|
|
18
|
+
if (parts.length < 3) return null;
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
host: parts[0],
|
|
22
|
+
keyType: parts[1],
|
|
23
|
+
key: parts[2],
|
|
24
|
+
comment: parts.slice(3).join(' ') || ''
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the SSH host key fingerprint for a server
|
|
30
|
+
*/
|
|
31
|
+
export async function getHostKeyFingerprint(host, port = 22) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const cmd = spawn('ssh-keyscan', ['-p', port.toString(), '-t', 'ed25519,rsa,ecdsa', host]);
|
|
34
|
+
let stdout = '';
|
|
35
|
+
let stderr = '';
|
|
36
|
+
|
|
37
|
+
cmd.stdout.on('data', (data) => {
|
|
38
|
+
stdout += data.toString();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
cmd.stderr.on('data', (data) => {
|
|
42
|
+
stderr += data.toString();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
cmd.on('close', (code) => {
|
|
46
|
+
if (code !== 0) {
|
|
47
|
+
reject(new Error(`Failed to get host key: ${stderr}`));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const lines = stdout.trim().split('\n').filter(l => l && !l.startsWith('#'));
|
|
52
|
+
const fingerprints = [];
|
|
53
|
+
|
|
54
|
+
for (const line of lines) {
|
|
55
|
+
const entry = parseKnownHostEntry(line);
|
|
56
|
+
if (entry) {
|
|
57
|
+
// Calculate SHA256 fingerprint
|
|
58
|
+
const keyData = Buffer.from(entry.key, 'base64');
|
|
59
|
+
const hash = crypto.createHash('sha256').update(keyData).digest('base64');
|
|
60
|
+
|
|
61
|
+
fingerprints.push({
|
|
62
|
+
host: entry.host,
|
|
63
|
+
type: entry.keyType,
|
|
64
|
+
fingerprint: `SHA256:${hash}`,
|
|
65
|
+
fullKey: line
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
resolve(fingerprints);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Check if a host key exists in known_hosts
|
|
77
|
+
*/
|
|
78
|
+
export function isHostKnown(host, port = 22) {
|
|
79
|
+
if (!fs.existsSync(KNOWN_HOSTS_PATH)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const content = fs.readFileSync(KNOWN_HOSTS_PATH, 'utf8');
|
|
84
|
+
const lines = content.split('\n');
|
|
85
|
+
|
|
86
|
+
// Format host entry as SSH does
|
|
87
|
+
const hostEntry = port === 22 ? host : `[${host}]:${port}`;
|
|
88
|
+
|
|
89
|
+
for (const line of lines) {
|
|
90
|
+
if (line.includes(hostEntry)) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get current host key from known_hosts
|
|
100
|
+
*/
|
|
101
|
+
export function getCurrentHostKey(host, port = 22) {
|
|
102
|
+
if (!fs.existsSync(KNOWN_HOSTS_PATH)) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const content = fs.readFileSync(KNOWN_HOSTS_PATH, 'utf8');
|
|
107
|
+
const lines = content.split('\n');
|
|
108
|
+
|
|
109
|
+
// Format host entry as SSH does
|
|
110
|
+
const hostEntry = port === 22 ? host : `[${host}]:${port}`;
|
|
111
|
+
const keys = [];
|
|
112
|
+
|
|
113
|
+
for (const line of lines) {
|
|
114
|
+
if (line.includes(hostEntry)) {
|
|
115
|
+
const entry = parseKnownHostEntry(line);
|
|
116
|
+
if (entry) {
|
|
117
|
+
const keyData = Buffer.from(entry.key, 'base64');
|
|
118
|
+
const hash = crypto.createHash('sha256').update(keyData).digest('base64');
|
|
119
|
+
|
|
120
|
+
keys.push({
|
|
121
|
+
host: entry.host,
|
|
122
|
+
type: entry.keyType,
|
|
123
|
+
fingerprint: `SHA256:${hash}`,
|
|
124
|
+
fullKey: line
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return keys.length > 0 ? keys : null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove a host from known_hosts
|
|
135
|
+
*/
|
|
136
|
+
export function removeHostKey(host, port = 22) {
|
|
137
|
+
try {
|
|
138
|
+
const hostEntry = port === 22 ? host : `[${host}]:${port}`;
|
|
139
|
+
|
|
140
|
+
// Use ssh-keygen to remove the host
|
|
141
|
+
execSync(`ssh-keygen -R "${hostEntry}"`, { stdio: 'ignore' });
|
|
142
|
+
|
|
143
|
+
logger.info('Host key removed', { host, port });
|
|
144
|
+
return true;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
logger.error('Failed to remove host key', { host, port, error: error.message });
|
|
147
|
+
throw new Error(`Failed to remove host key: ${error.message}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a host key to known_hosts
|
|
153
|
+
*/
|
|
154
|
+
export async function addHostKey(host, port = 22, keyData = null) {
|
|
155
|
+
try {
|
|
156
|
+
// Backup current known_hosts
|
|
157
|
+
if (fs.existsSync(KNOWN_HOSTS_PATH)) {
|
|
158
|
+
fs.copyFileSync(KNOWN_HOSTS_PATH, KNOWN_HOSTS_BACKUP);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// If no key data provided, fetch it
|
|
162
|
+
if (!keyData) {
|
|
163
|
+
const fingerprints = await getHostKeyFingerprint(host, port);
|
|
164
|
+
if (fingerprints.length === 0) {
|
|
165
|
+
throw new Error('No host keys found');
|
|
166
|
+
}
|
|
167
|
+
keyData = fingerprints.map(fp => fp.fullKey).join('\n');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Ensure .ssh directory exists
|
|
171
|
+
const sshDir = path.dirname(KNOWN_HOSTS_PATH);
|
|
172
|
+
if (!fs.existsSync(sshDir)) {
|
|
173
|
+
fs.mkdirSync(sshDir, { mode: 0o700, recursive: true });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Append to known_hosts
|
|
177
|
+
fs.appendFileSync(KNOWN_HOSTS_PATH, keyData + '\n');
|
|
178
|
+
|
|
179
|
+
logger.info('Host key added', { host, port });
|
|
180
|
+
return true;
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logger.error('Failed to add host key', { host, port, error: error.message });
|
|
183
|
+
throw new Error(`Failed to add host key: ${error.message}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Update a host key (remove old, add new)
|
|
189
|
+
*/
|
|
190
|
+
export async function updateHostKey(host, port = 22) {
|
|
191
|
+
try {
|
|
192
|
+
// Remove old key
|
|
193
|
+
removeHostKey(host, port);
|
|
194
|
+
|
|
195
|
+
// Add new key
|
|
196
|
+
await addHostKey(host, port);
|
|
197
|
+
|
|
198
|
+
logger.info('Host key updated', { host, port });
|
|
199
|
+
return true;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
logger.error('Failed to update host key', { host, port, error: error.message });
|
|
202
|
+
throw new Error(`Failed to update host key: ${error.message}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Verify if host key has changed
|
|
208
|
+
*/
|
|
209
|
+
export async function hasHostKeyChanged(host, port = 22) {
|
|
210
|
+
try {
|
|
211
|
+
const currentKeys = getCurrentHostKey(host, port);
|
|
212
|
+
if (!currentKeys || currentKeys.length === 0) {
|
|
213
|
+
// No key in known_hosts
|
|
214
|
+
return { changed: false, reason: 'not_in_known_hosts' };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const newKeys = await getHostKeyFingerprint(host, port);
|
|
218
|
+
if (!newKeys || newKeys.length === 0) {
|
|
219
|
+
return { changed: false, reason: 'cannot_fetch_key' };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Check if any current key matches any new key
|
|
223
|
+
for (const currentKey of currentKeys) {
|
|
224
|
+
for (const newKey of newKeys) {
|
|
225
|
+
if (currentKey.fingerprint === newKey.fingerprint) {
|
|
226
|
+
return { changed: false, reason: 'key_matches' };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Keys don't match
|
|
232
|
+
return {
|
|
233
|
+
changed: true,
|
|
234
|
+
reason: 'key_mismatch',
|
|
235
|
+
currentFingerprints: currentKeys.map(k => k.fingerprint),
|
|
236
|
+
newFingerprints: newKeys.map(k => k.fingerprint)
|
|
237
|
+
};
|
|
238
|
+
} catch (error) {
|
|
239
|
+
logger.error('Failed to verify host key', { host, port, error: error.message });
|
|
240
|
+
return { changed: false, reason: 'verification_error', error: error.message };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* List all known hosts
|
|
246
|
+
*/
|
|
247
|
+
export function listKnownHosts() {
|
|
248
|
+
if (!fs.existsSync(KNOWN_HOSTS_PATH)) {
|
|
249
|
+
return [];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const content = fs.readFileSync(KNOWN_HOSTS_PATH, 'utf8');
|
|
253
|
+
const lines = content.split('\n');
|
|
254
|
+
const hosts = new Map();
|
|
255
|
+
|
|
256
|
+
for (const line of lines) {
|
|
257
|
+
if (line && !line.startsWith('#')) {
|
|
258
|
+
const entry = parseKnownHostEntry(line);
|
|
259
|
+
if (entry) {
|
|
260
|
+
// Extract host and port
|
|
261
|
+
let host = entry.host;
|
|
262
|
+
let port = 22;
|
|
263
|
+
|
|
264
|
+
if (host.startsWith('[')) {
|
|
265
|
+
const match = host.match(/\[([^\]]+)\]:(\d+)/);
|
|
266
|
+
if (match) {
|
|
267
|
+
host = match[1];
|
|
268
|
+
port = parseInt(match[2]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const keyData = Buffer.from(entry.key, 'base64');
|
|
273
|
+
const hash = crypto.createHash('sha256').update(keyData).digest('base64');
|
|
274
|
+
|
|
275
|
+
const hostKey = `${host}:${port}`;
|
|
276
|
+
if (!hosts.has(hostKey)) {
|
|
277
|
+
hosts.set(hostKey, {
|
|
278
|
+
host,
|
|
279
|
+
port,
|
|
280
|
+
keys: []
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
hosts.get(hostKey).keys.push({
|
|
285
|
+
type: entry.keyType,
|
|
286
|
+
fingerprint: `SHA256:${hash}`
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return Array.from(hosts.values());
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Detect SSH key error in command output
|
|
297
|
+
*/
|
|
298
|
+
export function detectSSHKeyError(stderr) {
|
|
299
|
+
const keyErrorPatterns = [
|
|
300
|
+
'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED',
|
|
301
|
+
'Host key verification failed',
|
|
302
|
+
'The authenticity of host',
|
|
303
|
+
'ECDSA host key for .* has changed',
|
|
304
|
+
'RSA host key for .* has changed',
|
|
305
|
+
'ED25519 host key for .* has changed',
|
|
306
|
+
'Offending key in',
|
|
307
|
+
'Add correct host key in'
|
|
308
|
+
];
|
|
309
|
+
|
|
310
|
+
for (const pattern of keyErrorPatterns) {
|
|
311
|
+
if (stderr.includes(pattern)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Extract host info from SSH error
|
|
321
|
+
*/
|
|
322
|
+
export function extractHostFromSSHError(stderr) {
|
|
323
|
+
// Try to extract host and port from error message
|
|
324
|
+
const patterns = [
|
|
325
|
+
/Offending (?:RSA|ECDSA|ED25519) key in .+:(\d+)/i,
|
|
326
|
+
/Host key for \[([^\]]+)\]:(\d+) has changed/i,
|
|
327
|
+
/Host key for ([^\s]+) has changed/i,
|
|
328
|
+
/The authenticity of host '\[([^\]]+)\]:(\d+)'/i,
|
|
329
|
+
/The authenticity of host '([^\s]+) \(/i
|
|
330
|
+
];
|
|
331
|
+
|
|
332
|
+
for (const pattern of patterns) {
|
|
333
|
+
const match = stderr.match(pattern);
|
|
334
|
+
if (match) {
|
|
335
|
+
if (match[2]) {
|
|
336
|
+
// Host and port
|
|
337
|
+
return { host: match[1], port: parseInt(match[2]) };
|
|
338
|
+
} else {
|
|
339
|
+
// Just host
|
|
340
|
+
return { host: match[1], port: 22 };
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Handle SSH key error automatically
|
|
350
|
+
*/
|
|
351
|
+
export async function handleSSHKeyError(stderr, options = {}) {
|
|
352
|
+
const { autoAccept = false, interactive = true } = options;
|
|
353
|
+
|
|
354
|
+
const hostInfo = extractHostFromSSHError(stderr);
|
|
355
|
+
if (!hostInfo) {
|
|
356
|
+
throw new Error('Could not extract host information from SSH error');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
logger.warn('SSH host key verification failed', hostInfo);
|
|
360
|
+
|
|
361
|
+
if (autoAccept) {
|
|
362
|
+
// Automatically update the key
|
|
363
|
+
await updateHostKey(hostInfo.host, hostInfo.port);
|
|
364
|
+
return { action: 'updated', ...hostInfo };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!interactive) {
|
|
368
|
+
throw new Error(`Host key verification failed for ${hostInfo.host}:${hostInfo.port}. Use ssh_key_manage tool to update the key.`);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// In interactive mode, we would prompt the user
|
|
372
|
+
// For now, just return the error info
|
|
373
|
+
return {
|
|
374
|
+
action: 'prompt_required',
|
|
375
|
+
...hostInfo,
|
|
376
|
+
message: `Host key has changed for ${hostInfo.host}:${hostInfo.port}. Use ssh_key_manage tool to verify and update the key.`
|
|
377
|
+
};
|
|
378
|
+
}
|