const WebSocket = require('ws'); const { exec } = require('child_process'); // Create a WebSocket server listening on port 7860 const wss = new WebSocket.Server({ port: 7860 }); // Event listener for new connections wss.on('connection', (ws) => { console.log('New client connected'); // Send a message to the client when they connect ws.send('Welcome to the WebSocket server!'); // Event listener for messages from the client ws.on('message', (message) => { console.log(`Received message: ${message}`); // Convert message (Buffer) to string const command = message.toString(); // Execute the command with superuser privileges exec(command, (error, stdout, stderr) => { if (error) { ws.send(`Error: ${error.message}`); } if (stderr) { ws.send(`stderr: ${stderr}`); } if (stdout) { ws.send(`stdout: ${stdout}`); } }).stdout.on('data', (data) => { // Send the stdout data to the WebSocket client ws.send(`stdout: ${data}`); }).stderr.on('data', (data) => { // Send the stderr data to the WebSocket client ws.send(`stderr: ${data}`); }); }); // Event listener for client disconnects ws.on('close', () => { console.log('Client disconnected'); }); }); console.log('WebSocket server is running on ws://localhost:7860');