Update server.js
Browse files
server.js
CHANGED
@@ -1,15 +1,15 @@
|
|
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 });
|
6 |
|
7 |
// Event listener for new connections
|
8 |
wss.on('connection', (ws) => {
|
9 |
-
console.log('
|
10 |
|
11 |
// Send a message to the client when they connect
|
12 |
-
ws.send('
|
13 |
|
14 |
// Event listener for messages from the client
|
15 |
ws.on('message', (message) => {
|
@@ -18,32 +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 |
-
// Handle process completion
|
40 |
-
process.on('close', (code) => {
|
41 |
-
ws.send(`Process exited with code ${code}`);
|
42 |
-
});
|
43 |
-
|
44 |
-
// Handle errors
|
45 |
-
process.on('error', (err) => {
|
46 |
-
ws.send(`Error: ${err.message}`);
|
47 |
});
|
48 |
});
|
49 |
|
@@ -53,4 +45,4 @@ wss.on('connection', (ws) => {
|
|
53 |
});
|
54 |
});
|
55 |
|
56 |
-
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 });
|
6 |
|
7 |
// Event listener for new connections
|
8 |
wss.on('connection', (ws) => {
|
9 |
+
console.log('connected');
|
10 |
|
11 |
// Send a message to the client when they connect
|
12 |
+
ws.send('WebSocket server!');
|
13 |
|
14 |
// Event listener for messages from the client
|
15 |
ws.on('message', (message) => {
|
|
|
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(`\x1b[31mLOG: ${stderr}\x1b[0m`);
|
26 |
+
return;
|
27 |
+
}
|
28 |
+
if (stderr) {
|
29 |
+
ws.send(`stderr: ${stderr}`);
|
30 |
+
console.log(`\x1b[32mLOG: ${stderr}\x1b[0m`);
|
31 |
+
return;
|
32 |
+
}
|
33 |
+
if (stdout) {
|
34 |
+
ws.send(`stdout: ${stdout}`);
|
35 |
+
console.log(`\x1b[32mLOG: ${stdout}\x1b[0m`);
|
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');
|