Update index.js
Browse files
index.js
CHANGED
|
@@ -1,106 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|