Update server.js
Browse files
server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
const WebSocket = require('ws');
|
| 2 |
-
const {
|
| 3 |
|
| 4 |
// Create a WebSocket server listening on port 7860
|
| 5 |
const wss = new WebSocket.Server({ port: 7860 });
|
|
@@ -18,30 +18,24 @@ wss.on('connection', (ws) => {
|
|
| 18 |
// Convert message (Buffer) to string
|
| 19 |
const command = message.toString();
|
| 20 |
|
| 21 |
-
//
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
ws.send(`Error: ${error.message}`);
|
| 40 |
-
console.log(`Error: ${error.message}`);
|
| 41 |
-
});
|
| 42 |
-
|
| 43 |
-
child.on('close', (code) => {
|
| 44 |
-
console.log(`Child process exited with code ${code}`);
|
| 45 |
});
|
| 46 |
});
|
| 47 |
|
|
@@ -51,4 +45,4 @@ wss.on('connection', (ws) => {
|
|
| 51 |
});
|
| 52 |
});
|
| 53 |
|
| 54 |
-
console.log('WebSocket server is running on ws://localhost:7860');
|
|
|
|
| 1 |
const WebSocket = require('ws');
|
| 2 |
+
const { exec } = require('child_process');
|
| 3 |
|
| 4 |
// Create a WebSocket server listening on port 7860
|
| 5 |
const wss = new WebSocket.Server({ port: 7860 });
|
|
|
|
| 18 |
// Convert message (Buffer) to string
|
| 19 |
const command = message.toString();
|
| 20 |
|
| 21 |
+
// Execute the command without sudo
|
| 22 |
+
exec(command, (error, stdout, stderr) => {
|
| 23 |
+
if (error) {
|
| 24 |
+
ws.send(`Error: ${error.message}`);
|
| 25 |
+
console.log(`Error: ${stderr}`);
|
| 26 |
+
return;
|
| 27 |
+
}
|
| 28 |
+
if (stderr) {
|
| 29 |
+
ws.send(`stderr: ${stderr}`);
|
| 30 |
+
console.log(`Process: ${stderr}`);
|
| 31 |
+
return;
|
| 32 |
+
}
|
| 33 |
+
if (stdout) {
|
| 34 |
+
ws.send(`stdout: ${stdout}`);
|
| 35 |
+
console.log(`Log: ${stdout}`);
|
| 36 |
+
return;
|
| 37 |
+
}
|
| 38 |
+
//ws.send(`stdout: ${stdout}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
});
|
| 40 |
});
|
| 41 |
|
|
|
|
| 45 |
});
|
| 46 |
});
|
| 47 |
|
| 48 |
+
console.log('WebSocket server is running on ws://localhost:7860');
|