@wonderwhy-er/desktop-commander
Version:
MCP server for terminal operations and file editing
161 lines (160 loc) • 5.51 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
import { existsSync } from 'fs';
import { mkdir } from 'fs/promises';
import os from 'os';
/**
* Singleton config manager for the server
*/
class ConfigManager {
constructor() {
this.config = {};
this.initialized = false;
// Get user's home directory
const homeDir = os.homedir();
// Define config directory and file paths
const configDir = path.join(homeDir, '.claude-server-commander');
this.configPath = path.join(configDir, 'config.json');
}
/**
* Initialize configuration - load from disk or create default
*/
async init() {
if (this.initialized)
return;
try {
// Ensure config directory exists
const configDir = path.dirname(this.configPath);
if (!existsSync(configDir)) {
await mkdir(configDir, { recursive: true });
}
// Check if config file exists
try {
await fs.access(this.configPath);
// Load existing config
const configData = await fs.readFile(this.configPath, 'utf8');
this.config = JSON.parse(configData);
}
catch (error) {
// Config file doesn't exist, create default
this.config = this.getDefaultConfig();
await this.saveConfig();
}
this.initialized = true;
}
catch (error) {
console.error('Failed to initialize config:', error);
// Fall back to default config in memory
this.config = this.getDefaultConfig();
this.initialized = true;
}
}
/**
* Alias for init() to maintain backward compatibility
*/
async loadConfig() {
return this.init();
}
/**
* Create default configuration
*/
getDefaultConfig() {
return {
blockedCommands: [
// Disk and partition management
"mkfs", // Create a filesystem on a device
"format", // Format a storage device (cross-platform)
"mount", // Mount a filesystem
"umount", // Unmount a filesystem
"fdisk", // Manipulate disk partition tables
"dd", // Convert and copy files, can write directly to disks
"parted", // Disk partition manipulator
"diskpart", // Windows disk partitioning utility
// System administration and user management
"sudo", // Execute command as superuser
"su", // Substitute user identity
"passwd", // Change user password
"adduser", // Add a user to the system
"useradd", // Create a new user
"usermod", // Modify user account
"groupadd", // Create a new group
"chsh", // Change login shell
"visudo", // Edit the sudoers file
// System control
"shutdown", // Shutdown the system
"reboot", // Restart the system
"halt", // Stop the system
"poweroff", // Power off the system
"init", // Change system runlevel
// Network and security
"iptables", // Linux firewall administration
"firewall", // Generic firewall command
"netsh", // Windows network configuration
// Windows system commands
"sfc", // System File Checker
"bcdedit", // Boot Configuration Data editor
"reg", // Windows registry editor
"net", // Network/user/service management
"sc", // Service Control manager
"runas", // Execute command as another user
"cipher", // Encrypt/decrypt files or wipe data
"takeown" // Take ownership of files
],
defaultShell: os.platform() === 'win32' ? 'powershell.exe' : 'bash',
allowedDirectories: []
};
}
/**
* Save config to disk
*/
async saveConfig() {
try {
await fs.writeFile(this.configPath, JSON.stringify(this.config, null, 2), 'utf8');
}
catch (error) {
console.error('Failed to save config:', error);
throw error;
}
}
/**
* Get the entire config
*/
async getConfig() {
await this.init();
return { ...this.config };
}
/**
* Get a specific configuration value
*/
async getValue(key) {
await this.init();
return this.config[key];
}
/**
* Set a specific configuration value
*/
async setValue(key, value) {
await this.init();
this.config[key] = value;
await this.saveConfig();
}
/**
* Update multiple configuration values at once
*/
async updateConfig(updates) {
await this.init();
this.config = { ...this.config, ...updates };
await this.saveConfig();
return { ...this.config };
}
/**
* Reset configuration to defaults
*/
async resetConfig() {
this.config = this.getDefaultConfig();
await this.saveConfig();
return { ...this.config };
}
}
// Export singleton instance
export const configManager = new ConfigManager();