ulduldp commited on
Commit
b0ebb84
·
verified ·
1 Parent(s): 3212189

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +27 -189
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
 
6
  const app = express();
7
  const server = http.createServer(app);
@@ -11,7 +12,7 @@ const io = new Server(server, {
11
 
12
  const PORT = 7860;
13
 
14
- // ANSI → HTML converter
15
  const convert = new Convert({
16
  fg: '#e2e8f0',
17
  bg: '#0b1020',
@@ -19,218 +20,55 @@ const convert = new Convert({
19
  escapeXML: true
20
  });
21
 
22
- // 🧠 In-memory DB
23
  const logs = [];
24
  const MAX_LOGS = 10000;
25
 
 
 
 
 
 
 
 
 
26
  function addLog(log) {
27
  logs.push(log);
28
  if (logs.length > MAX_LOGS) logs.shift();
29
  }
30
 
31
- // ===== SOCKET =====
32
- io.on('connection', (socket) => {
33
- console.log('Client connected:', socket.id);
34
-
35
- socket.on('log', (data) => {
36
- const log = {
37
- message: data.message || '',
38
- level: data.level || 'info',
39
- timestamp: new Date(),
40
- };
41
-
42
- addLog(log);
43
-
44
- // send to UI
45
- io.emit('log', log);
46
- });
47
- });
48
 
49
  // ===== API =====
50
  app.get('/api/logs', (req, res) => {
51
  res.json(logs);
52
  });
53
 
54
- // ===== UI =====
55
- app.get('/logs', (req, res) => {
56
- res.send(`
57
- <!DOCTYPE html>
58
- <html>
59
- <head>
60
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
61
- <title>Live Logs</title>
62
-
63
- <style>
64
- * {
65
- box-sizing: border-box;
66
- }
67
-
68
- body {
69
- margin: 0;
70
- background: #0b1020;
71
- color: #e2e8f0;
72
- font-family: monospace;
73
- display: flex;
74
- flex-direction: column;
75
- height: 100vh;
76
- }
77
-
78
- header {
79
- padding: 10px;
80
- background: #111827;
81
- border-bottom: 1px solid #1f2937;
82
- font-size: 14px;
83
- }
84
-
85
- #logs {
86
- flex: 1;
87
- overflow-y: auto;
88
- padding: 10px;
89
- font-size: 13px;
90
- }
91
-
92
- .log {
93
- margin-bottom: 6px;
94
- word-wrap: break-word;
95
- }
96
-
97
- .timestamp {
98
- color: #64748b;
99
- margin-right: 6px;
100
- }
101
-
102
- .info { color: #38bdf8; }
103
- .error { color: #ef4444; }
104
- .warn { color: #facc15; }
105
-
106
- footer {
107
- padding: 8px;
108
- border-top: 1px solid #1f2937;
109
- background: #111827;
110
- display: flex;
111
- }
112
-
113
- input {
114
- flex: 1;
115
- padding: 10px;
116
- border: none;
117
- outline: none;
118
- background: #0b1020;
119
- color: #e2e8f0;
120
- border-radius: 6px;
121
- }
122
-
123
- button {
124
- margin-left: 8px;
125
- padding: 10px 14px;
126
- border: none;
127
- background: #2563eb;
128
- color: white;
129
- border-radius: 6px;
130
- cursor: pointer;
131
- }
132
-
133
- button:active {
134
- background: #1d4ed8;
135
- }
136
-
137
- @media (max-width: 600px) {
138
- #logs {
139
- font-size: 12px;
140
- }
141
- }
142
- </style>
143
- </head>
144
-
145
- <body>
146
-
147
- <header>📡 Live Logs Console</header>
148
-
149
- <div id="logs"></div>
150
-
151
- <footer>
152
- <input id="cmd" placeholder="Type command..." />
153
- <button onclick="sendCmd()">Send</button>
154
- </footer>
155
-
156
- <script src="/socket.io/socket.io.js"></script>
157
- <script>
158
- const socket = io();
159
- const logsDiv = document.getElementById('logs');
160
- const input = document.getElementById('cmd');
161
-
162
- function formatTime(date) {
163
- return new Date(date).toLocaleString('en-IN', {
164
- timeZone: 'Asia/Kolkata',
165
- hour12: false
166
- });
167
- }
168
-
169
- function addLog(log) {
170
- const div = document.createElement('div');
171
- div.className = 'log ' + (log.level || 'info');
172
-
173
- const time = formatTime(log.timestamp);
174
-
175
- div.innerHTML = \`
176
- <span class="timestamp">[\${time}]</span>
177
- \${log.message}
178
- \`;
179
-
180
- logsDiv.appendChild(div);
181
-
182
- // auto scroll
183
- logsDiv.scrollTop = logsDiv.scrollHeight;
184
- }
185
-
186
- // load old logs
187
- fetch('/api/logs')
188
- .then(res => res.json())
189
- .then(data => {
190
- data.forEach(addLog);
191
- });
192
 
193
- socket.on('log', (log) => {
 
194
  addLog(log);
 
195
  });
196
 
197
- function sendCmd() {
198
- const value = input.value.trim();
199
- if (!value) return;
200
 
201
- socket.emit('command', { command: value });
202
-
203
- addLog({
204
- message: '> ' + value,
205
- level: 'info',
206
- timestamp: new Date()
207
  });
208
 
209
- input.value = '';
210
- }
211
-
212
- input.addEventListener('keypress', (e) => {
213
- if (e.key === 'Enter') {
214
- sendCmd();
215
- }
216
  });
217
- </script>
218
-
219
- </body>
220
- </html>
221
- `);
222
- });
223
-
224
- // ===== FORMAT ROUTE (IMPORTANT) =====
225
- app.use(express.json());
226
-
227
- app.post('/format', (req, res) => {
228
- const raw = req.body.message || '';
229
- const html = convert.toHtml(raw);
230
- res.send(html);
231
  });
232
 
233
  // ===== START =====
234
  server.listen(PORT, () => {
235
- console.log('🚀 Log server running on', PORT);
236
  });
 
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();
8
  const server = http.createServer(app);
 
12
 
13
  const PORT = 7860;
14
 
15
+ // 🎨 ANSI → HTML
16
  const convert = new Convert({
17
  fg: '#e2e8f0',
18
  bg: '#0b1020',
 
20
  escapeXML: true
21
  });
22
 
23
+ // 🧠 logs
24
  const logs = [];
25
  const MAX_LOGS = 10000;
26
 
27
+ function formatLog(data) {
28
+ return {
29
+ html: convert.toHtml(data.message || ''),
30
+ level: data.level || 'info',
31
+ timestamp: new Date()
32
+ };
33
+ }
34
+
35
  function addLog(log) {
36
  logs.push(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 =====
44
  app.get('/api/logs', (req, res) => {
45
  res.json(logs);
46
  });
47
 
48
+ // ===== SOCKET =====
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
  });