Kano001 commited on
Commit
20d1c63
1 Parent(s): 8b8003d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +22 -30
server.js CHANGED
@@ -1,15 +1,15 @@
1
  const WebSocket = require('ws');
2
- const { spawn } = 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('New client connected');
10
 
11
  // Send a message to the client when they connect
12
- ws.send('Welcome to the WebSocket server!');
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
- // Split command into command and arguments
22
- const [cmd, ...args] = command.split(' ');
23
-
24
- // Spawn a child process to execute the command
25
- const process = spawn(cmd, args, { shell: true });
26
-
27
- // Send stdout data to the WebSocket client
28
- process.stdout.on('data', (data) => {
29
- ws.send(`stdout: ${data}`);
30
- console.log(`Log: ${data}`);
31
- });
32
-
33
- // Send stderr data to the WebSocket client
34
- process.stderr.on('data', (data) => {
35
- ws.send(`stderr: ${data}`);
36
- console.log(`Log: ${data}`);
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');