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.
Files changed (56) hide show
  1. package/.claude/settings.local.json +87 -0
  2. package/.flake8 +15 -0
  3. package/.hooks-config.json +131 -0
  4. package/.pre-commit-config.yaml +105 -0
  5. package/CHANGELOG.md +137 -0
  6. package/CLAUDE.md +190 -0
  7. package/CONTRIBUTING.md +114 -0
  8. package/DESCRIPTION +1 -0
  9. package/INSTALLATION.md +180 -0
  10. package/LICENSE +21 -0
  11. package/QUICKSTART.md +77 -0
  12. package/README.md +718 -0
  13. package/cli/README.md +286 -0
  14. package/cli/build.sh +199 -0
  15. package/cli/commands/server.sh +284 -0
  16. package/cli/demo.sh +107 -0
  17. package/cli/install.sh +164 -0
  18. package/cli/lib/colors.sh +174 -0
  19. package/cli/lib/config.sh +284 -0
  20. package/cli/lib/menu.sh +430 -0
  21. package/cli/migrate.sh +168 -0
  22. package/cli/ssh-manager +525 -0
  23. package/docs/ALIASES_AND_HOOKS.md +285 -0
  24. package/docs/BACKUP_GUIDE.md +500 -0
  25. package/docs/DEPLOYMENT_GUIDE.md +241 -0
  26. package/docs/SEO.md +257 -0
  27. package/examples/backup-workflow.js +481 -0
  28. package/examples/claude-code-config.example.json +40 -0
  29. package/examples/codex-ssh-config.example.toml +71 -0
  30. package/examples/deploy-workflow.py +183 -0
  31. package/glama.json +6 -0
  32. package/package.json +85 -0
  33. package/profiles/README.md +40 -0
  34. package/profiles/default.json +52 -0
  35. package/profiles/docker.json +62 -0
  36. package/profiles/frappe.json +107 -0
  37. package/profiles/nodejs.json +63 -0
  38. package/pyproject.toml +17 -0
  39. package/scripts/setup-hooks.sh +60 -0
  40. package/scripts/validate.sh +76 -0
  41. package/src/backup-manager.js +469 -0
  42. package/src/command-aliases.js +151 -0
  43. package/src/config-loader.js +281 -0
  44. package/src/database-manager.js +555 -0
  45. package/src/deploy-helper.js +176 -0
  46. package/src/health-monitor.js +428 -0
  47. package/src/hooks-system.js +327 -0
  48. package/src/index.js +4499 -0
  49. package/src/logger.js +276 -0
  50. package/src/profile-loader.js +187 -0
  51. package/src/server-aliases.js +150 -0
  52. package/src/server-groups.js +409 -0
  53. package/src/session-manager.js +410 -0
  54. package/src/ssh-key-manager.js +378 -0
  55. package/src/ssh-manager.js +435 -0
  56. package/src/tunnel-manager.js +576 -0
@@ -0,0 +1,281 @@
1
+ import * as dotenv from 'dotenv';
2
+ import TOML from '@iarna/toml';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import os from 'os';
6
+ import { logger } from './logger.js';
7
+
8
+ export class ConfigLoader {
9
+ constructor() {
10
+ this.servers = new Map();
11
+ this.configSource = null;
12
+ }
13
+
14
+ /**
15
+ * Load configuration from multiple sources with priority:
16
+ * 1. Environment variables (highest priority)
17
+ * 2. .env file
18
+ * 3. TOML config file (lowest priority)
19
+ */
20
+ async load(options = {}) {
21
+ const {
22
+ envPath = path.join(process.cwd(), '.env'),
23
+ tomlPath = process.env.SSH_CONFIG_PATH || path.join(os.homedir(), '.codex', 'ssh-config.toml'),
24
+ preferToml = false
25
+ } = options;
26
+
27
+ // Clear existing servers
28
+ this.servers.clear();
29
+
30
+ // Load in reverse priority order (lowest to highest)
31
+ let loadedFromToml = false;
32
+ let loadedFromEnv = false;
33
+
34
+ // Try loading TOML config first (lowest priority)
35
+ if (fs.existsSync(tomlPath)) {
36
+ try {
37
+ await this.loadTomlConfig(tomlPath);
38
+ loadedFromToml = true;
39
+ logger.info(`Loaded SSH configuration from TOML: ${tomlPath}`);
40
+ } catch (error) {
41
+ logger.warn(`Failed to load TOML config: ${error.message}`);
42
+ }
43
+ }
44
+
45
+ // Load .env file (higher priority, overwrites TOML)
46
+ if (!preferToml && fs.existsSync(envPath)) {
47
+ try {
48
+ this.loadEnvConfig(envPath);
49
+ loadedFromEnv = true;
50
+ logger.info(`Loaded SSH configuration from .env: ${envPath}`);
51
+ } catch (error) {
52
+ logger.warn(`Failed to load .env config: ${error.message}`);
53
+ }
54
+ }
55
+
56
+ // Load from environment variables (highest priority, overwrites everything)
57
+ this.loadEnvironmentVariables();
58
+
59
+ // Determine primary config source
60
+ if (loadedFromEnv) {
61
+ this.configSource = 'env';
62
+ } else if (loadedFromToml) {
63
+ this.configSource = 'toml';
64
+ } else if (this.servers.size > 0) {
65
+ this.configSource = 'environment';
66
+ } else {
67
+ this.configSource = null;
68
+ logger.warn('No SSH server configurations found');
69
+ }
70
+
71
+ return this.servers;
72
+ }
73
+
74
+ /**
75
+ * Load configuration from TOML file
76
+ */
77
+ async loadTomlConfig(tomlPath) {
78
+ const content = fs.readFileSync(tomlPath, 'utf8');
79
+ const config = TOML.parse(content);
80
+
81
+ if (config.ssh_servers) {
82
+ for (const [name, serverConfig] of Object.entries(config.ssh_servers)) {
83
+ const normalizedName = name.toLowerCase();
84
+ this.servers.set(normalizedName, {
85
+ name: normalizedName,
86
+ host: serverConfig.host,
87
+ user: serverConfig.user || serverConfig.username,
88
+ password: serverConfig.password,
89
+ keyPath: serverConfig.key_path || serverConfig.keypath || serverConfig.ssh_key,
90
+ port: serverConfig.port || 22,
91
+ defaultDir: serverConfig.default_dir || serverConfig.default_directory || serverConfig.cwd,
92
+ sudoPassword: serverConfig.sudo_password,
93
+ description: serverConfig.description,
94
+ source: 'toml'
95
+ });
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Load configuration from .env file
102
+ */
103
+ loadEnvConfig(envPath) {
104
+ dotenv.config({ path: envPath });
105
+ this.parseEnvVariables(process.env);
106
+ }
107
+
108
+ /**
109
+ * Load configuration from environment variables
110
+ */
111
+ loadEnvironmentVariables() {
112
+ this.parseEnvVariables(process.env);
113
+ }
114
+
115
+ /**
116
+ * Parse environment variables for SSH server configurations
117
+ */
118
+ parseEnvVariables(env) {
119
+ const serverPattern = /^SSH_SERVER_([A-Z0-9_]+)_HOST$/;
120
+ const processedServers = new Set();
121
+
122
+ for (const [key, value] of Object.entries(env)) {
123
+ const match = key.match(serverPattern);
124
+ if (match) {
125
+ const serverName = match[1].toLowerCase();
126
+
127
+ // Skip if already processed from a higher priority source
128
+ if (processedServers.has(serverName)) continue;
129
+
130
+ const server = {
131
+ name: serverName,
132
+ host: value,
133
+ user: env[`SSH_SERVER_${match[1]}_USER`],
134
+ password: env[`SSH_SERVER_${match[1]}_PASSWORD`],
135
+ keyPath: env[`SSH_SERVER_${match[1]}_KEYPATH`],
136
+ port: parseInt(env[`SSH_SERVER_${match[1]}_PORT`] || '22'),
137
+ defaultDir: env[`SSH_SERVER_${match[1]}_DEFAULT_DIR`],
138
+ sudoPassword: env[`SSH_SERVER_${match[1]}_SUDO_PASSWORD`],
139
+ description: env[`SSH_SERVER_${match[1]}_DESCRIPTION`],
140
+ source: 'env'
141
+ };
142
+
143
+ this.servers.set(serverName, server);
144
+ processedServers.add(serverName);
145
+ }
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Get server configuration by name
151
+ */
152
+ getServer(name) {
153
+ return this.servers.get(name.toLowerCase());
154
+ }
155
+
156
+ /**
157
+ * Get all server configurations
158
+ */
159
+ getAllServers() {
160
+ return Array.from(this.servers.values());
161
+ }
162
+
163
+ /**
164
+ * Check if server exists
165
+ */
166
+ hasServer(name) {
167
+ return this.servers.has(name.toLowerCase());
168
+ }
169
+
170
+ /**
171
+ * Export current configuration to TOML format
172
+ */
173
+ exportToToml() {
174
+ const config = {
175
+ ssh_servers: {}
176
+ };
177
+
178
+ for (const [name, server] of this.servers) {
179
+ const serverConfig = {
180
+ host: server.host,
181
+ user: server.user,
182
+ port: server.port
183
+ };
184
+
185
+ if (server.password) serverConfig.password = server.password;
186
+ if (server.keyPath) serverConfig.key_path = server.keyPath;
187
+ if (server.defaultDir) serverConfig.default_dir = server.defaultDir;
188
+ if (server.sudoPassword) serverConfig.sudo_password = server.sudoPassword;
189
+ if (server.description) serverConfig.description = server.description;
190
+
191
+ config.ssh_servers[name] = serverConfig;
192
+ }
193
+
194
+ return TOML.stringify(config);
195
+ }
196
+
197
+ /**
198
+ * Export current configuration to .env format
199
+ */
200
+ exportToEnv() {
201
+ const lines = ['# SSH Server Configuration'];
202
+ lines.push('# Generated by MCP SSH Manager');
203
+ lines.push('');
204
+
205
+ for (const [name, server] of this.servers) {
206
+ const upperName = name.toUpperCase();
207
+ lines.push(`# Server: ${name}`);
208
+ lines.push(`SSH_SERVER_${upperName}_HOST=${server.host}`);
209
+ lines.push(`SSH_SERVER_${upperName}_USER=${server.user}`);
210
+ if (server.password) lines.push(`SSH_SERVER_${upperName}_PASSWORD="${server.password}"`);
211
+ if (server.keyPath) lines.push(`SSH_SERVER_${upperName}_KEYPATH=${server.keyPath}`);
212
+ lines.push(`SSH_SERVER_${upperName}_PORT=${server.port || 22}`);
213
+ if (server.defaultDir) lines.push(`SSH_SERVER_${upperName}_DEFAULT_DIR=${server.defaultDir}`);
214
+ if (server.sudoPassword) lines.push(`SSH_SERVER_${upperName}_SUDO_PASSWORD="${server.sudoPassword}"`);
215
+ if (server.description) lines.push(`SSH_SERVER_${upperName}_DESCRIPTION="${server.description}"`);
216
+ lines.push('');
217
+ }
218
+
219
+ return lines.join('\n');
220
+ }
221
+
222
+ /**
223
+ * Save configuration to Codex TOML format
224
+ */
225
+ async saveToCodexConfig(codexConfigPath = path.join(os.homedir(), '.codex', 'config.toml')) {
226
+ let config = {};
227
+
228
+ // Load existing config if it exists
229
+ if (fs.existsSync(codexConfigPath)) {
230
+ const content = fs.readFileSync(codexConfigPath, 'utf8');
231
+ config = TOML.parse(content);
232
+ }
233
+
234
+ // Add MCP server configuration
235
+ if (!config.mcp_servers) {
236
+ config.mcp_servers = {};
237
+ }
238
+
239
+ config.mcp_servers['ssh-manager'] = {
240
+ command: 'node',
241
+ args: [path.join(process.cwd(), 'src', 'index.js')],
242
+ env: {
243
+ SSH_CONFIG_PATH: path.join(os.homedir(), '.codex', 'ssh-config.toml')
244
+ },
245
+ startup_timeout_ms: 20000
246
+ };
247
+
248
+ // Write back to config file
249
+ const tomlContent = TOML.stringify(config);
250
+ fs.writeFileSync(codexConfigPath, tomlContent, 'utf8');
251
+
252
+ logger.info(`Updated Codex configuration at ${codexConfigPath}`);
253
+ }
254
+
255
+ /**
256
+ * Migrate .env configuration to TOML
257
+ */
258
+ async migrateEnvToToml(envPath, tomlPath) {
259
+ // Load from .env
260
+ this.servers.clear();
261
+ this.loadEnvConfig(envPath);
262
+
263
+ // Export to TOML
264
+ const tomlContent = this.exportToToml();
265
+
266
+ // Ensure directory exists
267
+ const tomlDir = path.dirname(tomlPath);
268
+ if (!fs.existsSync(tomlDir)) {
269
+ fs.mkdirSync(tomlDir, { recursive: true });
270
+ }
271
+
272
+ // Write TOML file
273
+ fs.writeFileSync(tomlPath, tomlContent, 'utf8');
274
+
275
+ logger.info(`Migrated ${this.servers.size} servers from ${envPath} to ${tomlPath}`);
276
+ return this.servers.size;
277
+ }
278
+ }
279
+
280
+ // Export singleton instance
281
+ export const configLoader = new ConfigLoader();