UNPKG

@light-merlin-dark/vssh

Version:

MCP-native SSH proxy for AI agents. CLI & MCP Server, plugin system, AI safety guards.

158 lines 6.16 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ProxyService = void 0; const config_1 = require("../config"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); class ProxyService { constructor(config, sshService, commandGuard) { this.isLocal = false; this.config = config; this.sshService = sshService; this.commandGuard = commandGuard; } setLocalMode(local) { this.isLocal = local; } isLocalMode() { return this.isLocal; } async executeCommand(command, options = {}) { const timestamp = new Date().toISOString(); const startTime = Date.now(); const outputMode = options.outputMode || 'raw'; // Check command safety unless explicitly skipped if (!options.skipGuard) { const guardResult = this.commandGuard.checkCommand(command); // Display warnings if (guardResult.reasons.length > 0) { guardResult.reasons.forEach(reason => { if (reason.startsWith('⚠️')) { console.warn(reason); } }); } if (guardResult.isBlocked) { this.commandGuard.displayBlockedMessage(command, guardResult); this.commandGuard.logBlockedCommand(command, guardResult); throw new Error(`Command blocked: ${guardResult.reasons.join(', ')}`); } } // Log command start (to file only, no console output for SSH purity) if (!options.skipLogging) { const logEntry = `[${timestamp}] ${this.isLocal ? 'LOCAL' : 'REMOTE'} COMMAND: ${command}\n`; this.ensureLogsDirectory(); fs.appendFileSync(path.join(config_1.LOGS_PATH, 'proxy_commands.log'), logEntry); } let output; try { if (this.isLocal) { // Execute locally using child_process const { execSync } = await Promise.resolve().then(() => __importStar(require('child_process'))); output = execSync(command, { encoding: 'utf8', cwd: options.workingDirectory, maxBuffer: 10 * 1024 * 1024 // 10MB buffer }); } else { // Execute via SSH output = await this.sshService.executeCommand(command); } } catch (error) { const duration = Date.now() - startTime; // Log error if (!options.skipLogging) { const errorLog = `[${timestamp}] ERROR [${duration}ms]: ${error.message}\n${'='.repeat(80)}\n`; fs.appendFileSync(path.join(config_1.LOGS_PATH, 'proxy_commands.log'), errorLog); } throw error; } const duration = Date.now() - startTime; // Log result (to file only, no console output for SSH purity) if (!options.skipLogging) { const resultLog = `[${timestamp}] RESULT [${duration}ms]:\n${output}\n${'='.repeat(80)}\n`; fs.appendFileSync(path.join(config_1.LOGS_PATH, 'proxy_commands.log'), resultLog); } return { output, duration, timestamp, command, isLocal: this.isLocal, exitCode: 0 }; } ensureLogsDirectory() { if (!fs.existsSync(config_1.LOGS_PATH)) { fs.mkdirSync(config_1.LOGS_PATH, { recursive: true }); } } formatJSONResponse(result, error) { const response = { success: !error, command: result.command, duration: result.duration, timestamp: result.timestamp, output: result.output, error: error?.message, metadata: { isLocal: result.isLocal, exitCode: error ? 1 : result.exitCode || 0 } }; // Apply field filtering if specified if (this.jsonFields && this.jsonFields.length > 0) { const filteredResponse = {}; this.jsonFields.forEach(field => { if (field in response) { filteredResponse[field] = response[field]; } if (field === 'metadata' && response.metadata) { filteredResponse.metadata = response.metadata; } }); return JSON.stringify(filteredResponse, null, 2); } return JSON.stringify(response, null, 2); } setJSONFields(fields) { this.jsonFields = fields; } } exports.ProxyService = ProxyService; //# sourceMappingURL=proxy-service.js.map