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,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH Session Manager
|
|
3
|
+
* Manages persistent SSH sessions with state and context
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
7
|
+
import { logger } from './logger.js';
|
|
8
|
+
|
|
9
|
+
// Map to store active sessions
|
|
10
|
+
const sessions = new Map();
|
|
11
|
+
|
|
12
|
+
// Session states
|
|
13
|
+
export const SESSION_STATES = {
|
|
14
|
+
INITIALIZING: 'initializing',
|
|
15
|
+
READY: 'ready',
|
|
16
|
+
BUSY: 'busy',
|
|
17
|
+
ERROR: 'error',
|
|
18
|
+
CLOSED: 'closed'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
class SSHSession {
|
|
22
|
+
constructor(id, serverName, ssh) {
|
|
23
|
+
this.id = id;
|
|
24
|
+
this.serverName = serverName;
|
|
25
|
+
this.ssh = ssh;
|
|
26
|
+
this.state = SESSION_STATES.INITIALIZING;
|
|
27
|
+
this.context = {
|
|
28
|
+
cwd: null,
|
|
29
|
+
env: {},
|
|
30
|
+
history: [],
|
|
31
|
+
variables: {}
|
|
32
|
+
};
|
|
33
|
+
this.createdAt = new Date();
|
|
34
|
+
this.lastActivity = new Date();
|
|
35
|
+
this.shell = null;
|
|
36
|
+
this.outputBuffer = '';
|
|
37
|
+
this.errorBuffer = '';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the session with a shell
|
|
42
|
+
*/
|
|
43
|
+
async initialize() {
|
|
44
|
+
try {
|
|
45
|
+
logger.info(`Initializing SSH session ${this.id}`, {
|
|
46
|
+
server: this.serverName
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Start an interactive shell
|
|
50
|
+
this.shell = await this.ssh.requestShell({
|
|
51
|
+
term: 'xterm-256color',
|
|
52
|
+
cols: 80,
|
|
53
|
+
rows: 24
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Setup event handlers
|
|
57
|
+
this.shell.on('data', (data) => {
|
|
58
|
+
this.outputBuffer += data.toString();
|
|
59
|
+
this.lastActivity = new Date();
|
|
60
|
+
|
|
61
|
+
// Log output in verbose mode
|
|
62
|
+
if (logger.verbose) {
|
|
63
|
+
logger.debug(`Session ${this.id} output`, {
|
|
64
|
+
data: data.toString().substring(0, 200)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.shell.on('close', () => {
|
|
70
|
+
logger.info(`Session ${this.id} shell closed`);
|
|
71
|
+
this.state = SESSION_STATES.CLOSED;
|
|
72
|
+
this.cleanup();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.shell.stderr.on('data', (data) => {
|
|
76
|
+
this.errorBuffer += data.toString();
|
|
77
|
+
logger.warn(`Session ${this.id} stderr`, {
|
|
78
|
+
error: data.toString()
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Wait for shell prompt
|
|
83
|
+
await this.waitForPrompt();
|
|
84
|
+
|
|
85
|
+
// Get initial working directory
|
|
86
|
+
await this.updateContext();
|
|
87
|
+
|
|
88
|
+
this.state = SESSION_STATES.READY;
|
|
89
|
+
|
|
90
|
+
logger.info(`Session ${this.id} initialized`, {
|
|
91
|
+
server: this.serverName,
|
|
92
|
+
cwd: this.context.cwd
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
} catch (error) {
|
|
96
|
+
this.state = SESSION_STATES.ERROR;
|
|
97
|
+
logger.error(`Failed to initialize session ${this.id}`, {
|
|
98
|
+
error: error.message
|
|
99
|
+
});
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Wait for shell prompt
|
|
106
|
+
*/
|
|
107
|
+
async waitForPrompt(timeout = 5000) {
|
|
108
|
+
const startTime = Date.now();
|
|
109
|
+
|
|
110
|
+
while (Date.now() - startTime < timeout) {
|
|
111
|
+
// Check if we have a prompt (ends with $ or # typically)
|
|
112
|
+
if (this.outputBuffer.match(/[$#>]\s*$/)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Wait a bit
|
|
117
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
throw new Error('Timeout waiting for shell prompt');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Update session context (pwd, env)
|
|
125
|
+
*/
|
|
126
|
+
async updateContext() {
|
|
127
|
+
try {
|
|
128
|
+
// Get current directory
|
|
129
|
+
const pwdResult = await this.execute('pwd', { silent: true });
|
|
130
|
+
if (pwdResult.success) {
|
|
131
|
+
this.context.cwd = pwdResult.output.trim();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Get environment variables (selective)
|
|
135
|
+
const envResult = await this.execute('echo $PATH:$USER:$HOME', { silent: true });
|
|
136
|
+
if (envResult.success) {
|
|
137
|
+
const [path, user, home] = envResult.output.trim().split(':');
|
|
138
|
+
this.context.env = { PATH: path, USER: user, HOME: home };
|
|
139
|
+
}
|
|
140
|
+
} catch (error) {
|
|
141
|
+
logger.warn(`Failed to update context for session ${this.id}`, {
|
|
142
|
+
error: error.message
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Execute a command in the session
|
|
149
|
+
*/
|
|
150
|
+
async execute(command, options = {}) {
|
|
151
|
+
if (this.state !== SESSION_STATES.READY) {
|
|
152
|
+
throw new Error(`Session ${this.id} is not ready (state: ${this.state})`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
this.state = SESSION_STATES.BUSY;
|
|
156
|
+
this.lastActivity = new Date();
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
// Clear buffers
|
|
160
|
+
this.outputBuffer = '';
|
|
161
|
+
this.errorBuffer = '';
|
|
162
|
+
|
|
163
|
+
// Add to history unless silent
|
|
164
|
+
if (!options.silent) {
|
|
165
|
+
this.context.history.push({
|
|
166
|
+
command,
|
|
167
|
+
timestamp: new Date(),
|
|
168
|
+
cwd: this.context.cwd
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
logger.info(`Session ${this.id} executing`, {
|
|
172
|
+
command: command.substring(0, 100),
|
|
173
|
+
server: this.serverName
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Send command
|
|
178
|
+
this.shell.write(command + '\n');
|
|
179
|
+
|
|
180
|
+
// Wait for command to complete
|
|
181
|
+
await this.waitForPrompt(options.timeout || 30000);
|
|
182
|
+
|
|
183
|
+
// Parse output (remove command echo and prompt)
|
|
184
|
+
let output = this.outputBuffer;
|
|
185
|
+
|
|
186
|
+
// Remove the command echo (first line)
|
|
187
|
+
const lines = output.split('\n');
|
|
188
|
+
if (lines[0].includes(command)) {
|
|
189
|
+
lines.shift();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Remove the prompt (last line)
|
|
193
|
+
const lastLine = lines[lines.length - 1];
|
|
194
|
+
if (lastLine.match(/[$#>]\s*$/)) {
|
|
195
|
+
lines.pop();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
output = lines.join('\n').trim();
|
|
199
|
+
|
|
200
|
+
// Check for command success (basic heuristic)
|
|
201
|
+
const success = !this.errorBuffer && !output.includes('command not found');
|
|
202
|
+
|
|
203
|
+
// Update context if command might have changed it
|
|
204
|
+
if (command.startsWith('cd ') || command.startsWith('export ')) {
|
|
205
|
+
await this.updateContext();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
this.state = SESSION_STATES.READY;
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
success,
|
|
212
|
+
output,
|
|
213
|
+
error: this.errorBuffer,
|
|
214
|
+
session: this.id
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
} catch (error) {
|
|
218
|
+
this.state = SESSION_STATES.ERROR;
|
|
219
|
+
logger.error(`Session ${this.id} execution failed`, {
|
|
220
|
+
command,
|
|
221
|
+
error: error.message
|
|
222
|
+
});
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Set session variable
|
|
229
|
+
*/
|
|
230
|
+
setVariable(name, value) {
|
|
231
|
+
this.context.variables[name] = value;
|
|
232
|
+
this.lastActivity = new Date();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Get session variable
|
|
237
|
+
*/
|
|
238
|
+
getVariable(name) {
|
|
239
|
+
return this.context.variables[name];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Get session info
|
|
244
|
+
*/
|
|
245
|
+
getInfo() {
|
|
246
|
+
return {
|
|
247
|
+
id: this.id,
|
|
248
|
+
server: this.serverName,
|
|
249
|
+
state: this.state,
|
|
250
|
+
cwd: this.context.cwd,
|
|
251
|
+
env: this.context.env,
|
|
252
|
+
created: this.createdAt,
|
|
253
|
+
lastActivity: this.lastActivity,
|
|
254
|
+
historyCount: this.context.history.length,
|
|
255
|
+
variables: Object.keys(this.context.variables)
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Close the session
|
|
261
|
+
*/
|
|
262
|
+
close() {
|
|
263
|
+
logger.info(`Closing session ${this.id}`);
|
|
264
|
+
|
|
265
|
+
if (this.shell) {
|
|
266
|
+
this.shell.write('exit\n');
|
|
267
|
+
this.shell.end();
|
|
268
|
+
this.shell = null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
this.state = SESSION_STATES.CLOSED;
|
|
272
|
+
this.cleanup();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Cleanup resources
|
|
277
|
+
*/
|
|
278
|
+
cleanup() {
|
|
279
|
+
sessions.delete(this.id);
|
|
280
|
+
this.outputBuffer = '';
|
|
281
|
+
this.errorBuffer = '';
|
|
282
|
+
this.context.history = [];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Create a new SSH session
|
|
288
|
+
*/
|
|
289
|
+
export async function createSession(serverName, ssh) {
|
|
290
|
+
const sessionId = `ssh_${Date.now()}_${uuidv4().substring(0, 8)}`;
|
|
291
|
+
|
|
292
|
+
const session = new SSHSession(sessionId, serverName, ssh);
|
|
293
|
+
sessions.set(sessionId, session);
|
|
294
|
+
|
|
295
|
+
try {
|
|
296
|
+
await session.initialize();
|
|
297
|
+
|
|
298
|
+
logger.info('SSH session created', {
|
|
299
|
+
id: sessionId,
|
|
300
|
+
server: serverName
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
return session;
|
|
304
|
+
} catch (error) {
|
|
305
|
+
sessions.delete(sessionId);
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Get an existing session
|
|
312
|
+
*/
|
|
313
|
+
export function getSession(sessionId) {
|
|
314
|
+
const session = sessions.get(sessionId);
|
|
315
|
+
|
|
316
|
+
if (!session) {
|
|
317
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (session.state === SESSION_STATES.CLOSED) {
|
|
321
|
+
throw new Error(`Session ${sessionId} is closed`);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return session;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* List all active sessions
|
|
329
|
+
*/
|
|
330
|
+
export function listSessions() {
|
|
331
|
+
const activeSessions = [];
|
|
332
|
+
|
|
333
|
+
for (const [id, session] of sessions.entries()) {
|
|
334
|
+
if (session.state !== SESSION_STATES.CLOSED) {
|
|
335
|
+
activeSessions.push(session.getInfo());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return activeSessions;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Close a session
|
|
344
|
+
*/
|
|
345
|
+
export function closeSession(sessionId) {
|
|
346
|
+
const session = sessions.get(sessionId);
|
|
347
|
+
|
|
348
|
+
if (!session) {
|
|
349
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
session.close();
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Close all sessions for a server
|
|
358
|
+
*/
|
|
359
|
+
export function closeServerSessions(serverName) {
|
|
360
|
+
let closedCount = 0;
|
|
361
|
+
|
|
362
|
+
for (const [id, session] of sessions.entries()) {
|
|
363
|
+
if (session.serverName === serverName) {
|
|
364
|
+
session.close();
|
|
365
|
+
closedCount++;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return closedCount;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Cleanup old sessions
|
|
374
|
+
*/
|
|
375
|
+
export function cleanupSessions(maxAge = 30 * 60 * 1000) { // 30 minutes default
|
|
376
|
+
const now = Date.now();
|
|
377
|
+
let cleanedCount = 0;
|
|
378
|
+
|
|
379
|
+
for (const [id, session] of sessions.entries()) {
|
|
380
|
+
const age = now - session.lastActivity.getTime();
|
|
381
|
+
|
|
382
|
+
if (age > maxAge) {
|
|
383
|
+
logger.info(`Cleaning up inactive session ${id}`, {
|
|
384
|
+
age: Math.floor(age / 1000) + 's'
|
|
385
|
+
});
|
|
386
|
+
session.close();
|
|
387
|
+
cleanedCount++;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return cleanedCount;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Periodic cleanup of inactive sessions
|
|
395
|
+
setInterval(() => {
|
|
396
|
+
const cleaned = cleanupSessions();
|
|
397
|
+
if (cleaned > 0) {
|
|
398
|
+
logger.info(`Cleaned up ${cleaned} inactive sessions`);
|
|
399
|
+
}
|
|
400
|
+
}, 5 * 60 * 1000); // Every 5 minutes
|
|
401
|
+
|
|
402
|
+
export default {
|
|
403
|
+
createSession,
|
|
404
|
+
getSession,
|
|
405
|
+
listSessions,
|
|
406
|
+
closeSession,
|
|
407
|
+
closeServerSessions,
|
|
408
|
+
cleanupSessions,
|
|
409
|
+
SESSION_STATES
|
|
410
|
+
};
|