aripbae commited on
Commit
56db52d
Β·
verified Β·
1 Parent(s): e00a27b

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +34 -106
index.js CHANGED
@@ -1,106 +1,34 @@
1
- // kode dari Qwen-AI
2
- const express = require('express');
3
- const app = express();
4
- const port = 7860;
5
-
6
- // Middleware to parse JSON (but we'll measure raw body too)
7
- app.use(express.json({ limit: '50mb' }));
8
-
9
- let stats = {
10
- totalRequests: 0,
11
- totalTrafficBytes: 0,
12
- startTime: Date.now(),
13
- };
14
-
15
- // πŸ‘‡ Middleware: ukur size body sebelum diproses
16
- app.use((req, res, next) => {
17
- let data = [];
18
- let dataSize = 0;
19
-
20
- // Tangkap stream body untuk ukur ukuran aslinya
21
- req.on('data', chunk => {
22
- data.push(chunk);
23
- dataSize += chunk.length;
24
- });
25
-
26
- req.on('end', () => {
27
- // Gabungkan buffer kalau perlu (opsional)
28
- req.rawBody = Buffer.concat(data);
29
- req.bodySize = dataSize;
30
-
31
- // Update statistik global
32
- stats.totalRequests++;
33
- stats.totalTrafficBytes += dataSize;
34
-
35
- // Log real-time ke console
36
- console.log(`[FLOOD] #${stats.totalRequests} | ${req.ip} | ${req.method} ${req.url} | ${dataSize} bytes | Total: ${(stats.totalTrafficBytes / (1024 * 1024)).toFixed(2)} MB`);
37
-
38
- next();
39
- });
40
- });
41
-
42
- // 🌐 SSE Endpoint β€” kirim update real-time ke browser
43
- app.get('/stream', (req, res) => {
44
- res.setHeader('Content-Type', 'text/event-stream');
45
- res.setHeader('Cache-Control', 'no-cache');
46
- res.setHeader('Connection', 'keep-alive');
47
-
48
- const interval = setInterval(() => {
49
- const uptimeSec = Math.floor((Date.now() - stats.startTime) / 1000);
50
- const mb = (stats.totalTrafficBytes / (1024 * 1024)).toFixed(2);
51
- res.write(`data: ${JSON.stringify({
52
- totalRequests: stats.totalRequests,
53
- totalTrafficMB: mb,
54
- uptimeSeconds: uptimeSec
55
- })}\n\n`);
56
- }, 500); // Kirim tiap 0.5 detik
57
-
58
- req.on('close', () => clearInterval(interval));
59
- });
60
-
61
- // πŸ–₯️ Homepage dengan live dashboard
62
- app.get('/', (req, res) => {
63
- res.send(`
64
- <!DOCTYPE html>
65
- <html>
66
- <head><title>Real-Time Flood Monitor</title></head>
67
- <body>
68
- <h1>πŸ“‘ REAL-TIME FLOOD MONITOR</h1>
69
- <div id="counter">Loading...</div>
70
- <hr>
71
- <p>Hit this endpoint with POST/GET to generate traffic:</p>
72
- <pre>curl -X POST -H "Content-Type: application/json" -d '{"test":"data"}' http://localhost:3000/test</pre>
73
-
74
- <script>
75
- const eventSource = new EventSource('/stream');
76
- eventSource.onmessage = (e) => {
77
- const data = JSON.parse(e.data);
78
- document.getElementById('counter').innerHTML = \`
79
- <p><strong>Total Requests:</strong> \${data.totalRequests}</p>
80
- <p><strong>Total Traffic:</strong> \${data.totalTrafficMB} MB</p>
81
- <p><strong>Uptime:</strong> \${data.uptimeSeconds} seconds</p>
82
- \`;
83
- };
84
- </script>
85
- </body>
86
- </html>
87
- `);
88
- });
89
-
90
- // πŸ”₯ Endpoint dummy buat testing POST flood
91
- app.post('/test', (req, res) => {
92
- res.json({ message: "OK", receivedBytes: req.bodySize || 0 });
93
- });
94
-
95
- // API buat baca stat (opsional)
96
- app.get('/api/stats', (req, res) => {
97
- res.json({
98
- totalRequests: stats.totalRequests,
99
- totalTrafficMB: (stats.totalTrafficBytes / (1024 * 1024)).toFixed(2),
100
- uptimeSeconds: Math.floor((Date.now() - stats.startTime) / 1000),
101
- });
102
- });
103
-
104
- app.listen(port, () => {
105
- console.log(`βœ… Real-time flood monitor running at http://localhost:${port}`);
106
- });
 
1
+ const bytes = require('bytes'),
2
+ express = require('express')
3
+
4
+ let count = 0,
5
+ totalBytes = 0
6
+
7
+ express()
8
+ .use((req, res) => {
9
+ count++
10
+ let size = 0
11
+ req.on('data', chunk => (
12
+ size += chunk.length
13
+ ))
14
+ req.on('end', () => {
15
+ totalBytes += size
16
+ const total = bytes(
17
+ totalBytes,
18
+ { unitSeparator: ' ' }
19
+ )
20
+ console.log([
21
+ `[FLOOD] #${count}`,
22
+ req.ip,
23
+ `${req.method} ${req.url}`,
24
+ `${size} bytes`,
25
+ `Total: ${total}`
26
+ ].join(' | '))
27
+ res.json({
28
+ requests: count,
29
+ totalBytes,
30
+ total
31
+ })
32
+ })
33
+ })
34
+ .listen(7860)