ulduldp commited on
Commit
940518f
ยท
verified ยท
1 Parent(s): 4f49a06

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +55 -9
server.js CHANGED
@@ -2,6 +2,7 @@ const express = require('express');
2
  const http = require('http');
3
  const { Server } = require('socket.io');
4
  const Convert = require('ansi-to-html');
 
5
  const path = require('path');
6
 
7
  const app = express();
@@ -20,7 +21,7 @@ const convert = new Convert({
20
  escapeXML: true
21
  });
22
 
23
- // ๐Ÿง  logs
24
  const logs = [];
25
  const MAX_LOGS = 10000;
26
 
@@ -37,7 +38,7 @@ function addLog(log) {
37
  if (logs.length > MAX_LOGS) logs.shift();
38
  }
39
 
40
- // ===== STATIC FILE =====
41
  app.use(express.static(path.join(__dirname, 'public')));
42
 
43
  // ===== API =====
@@ -49,26 +50,71 @@ app.get('/api/logs', (req, res) => {
49
  io.on('connection', (socket) => {
50
  console.log('Client connected:', socket.id);
51
 
 
52
  socket.on('log', (data) => {
53
  const log = formatLog(data);
54
  addLog(log);
55
  io.emit('log', log);
56
  });
57
 
 
58
  socket.on('command', (data) => {
59
- const cmd = data.command;
60
 
61
- const response = formatLog({
62
- message: "\x1b[36mExecuted:\x1b[0m " + cmd,
63
- level: "info"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  });
65
 
66
- addLog(response);
67
- io.emit('log', response);
68
  });
 
69
  });
70
 
71
  // ===== START =====
72
  server.listen(PORT, () => {
73
- console.log('๐Ÿš€ Server running on', PORT);
74
  });
 
2
  const http = require('http');
3
  const { Server } = require('socket.io');
4
  const Convert = require('ansi-to-html');
5
+ const { exec } = require('child_process');
6
  const path = require('path');
7
 
8
  const app = express();
 
21
  escapeXML: true
22
  });
23
 
24
+ // ๐Ÿง  logs memory
25
  const logs = [];
26
  const MAX_LOGS = 10000;
27
 
 
38
  if (logs.length > MAX_LOGS) logs.shift();
39
  }
40
 
41
+ // ===== STATIC =====
42
  app.use(express.static(path.join(__dirname, 'public')));
43
 
44
  // ===== API =====
 
50
  io.on('connection', (socket) => {
51
  console.log('Client connected:', socket.id);
52
 
53
+ // receive logs from clients
54
  socket.on('log', (data) => {
55
  const log = formatLog(data);
56
  addLog(log);
57
  io.emit('log', log);
58
  });
59
 
60
+ // handle command execution
61
  socket.on('command', (data) => {
62
+ const cmd = data.command.trim();
63
 
64
+ if (!cmd) return;
65
+
66
+ // ๐Ÿ”’ Basic security (edit as needed)
67
+ const blocked = ['rm', 'shutdown', 'reboot', 'mkfs'];
68
+ if (blocked.some(b => cmd.includes(b))) {
69
+ const log = formatLog({
70
+ message: "\x1b[31mBlocked command!\x1b[0m",
71
+ level: "error"
72
+ });
73
+ addLog(log);
74
+ return io.emit('log', log);
75
+ }
76
+
77
+ // show command
78
+ const cmdLog = formatLog({
79
+ message: "\x1b[32m$ " + cmd + "\x1b[0m"
80
+ });
81
+
82
+ addLog(cmdLog);
83
+ io.emit('log', cmdLog);
84
+
85
+ // execute
86
+ exec(cmd, { timeout: 20000 }, (error, stdout, stderr) => {
87
+
88
+ if (stdout) {
89
+ const outLog = formatLog({ message: stdout });
90
+ addLog(outLog);
91
+ io.emit('log', outLog);
92
+ }
93
+
94
+ if (stderr) {
95
+ const errLog = formatLog({
96
+ message: "\x1b[31m" + stderr + "\x1b[0m",
97
+ level: "error"
98
+ });
99
+ addLog(errLog);
100
+ io.emit('log', errLog);
101
+ }
102
+
103
+ if (error) {
104
+ const errLog = formatLog({
105
+ message: "\x1b[31m" + error.message + "\x1b[0m",
106
+ level: "error"
107
+ });
108
+ addLog(errLog);
109
+ io.emit('log', errLog);
110
+ }
111
  });
112
 
 
 
113
  });
114
+
115
  });
116
 
117
  // ===== START =====
118
  server.listen(PORT, () => {
119
+ console.log('๐Ÿš€ Server running on http://localhost:' + PORT);
120
  });