Zhofang commited on
Commit
53e940f
·
verified ·
1 Parent(s): 3fa3e56

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +148 -0
app.js ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const http = require('http');
2
+ const fs = require('fs');
3
+ const cluster = require('cluster');
4
+ const os = require('os');
5
+ const { URL } = require('url');
6
+ const requests = require('requests'); // Still used for Telegram
7
+
8
+ const TELEGRAM_BOT_TOKEN = '7408530224:AAGLPps_bWOHQ7mQDBe-BsXTiaJA8JmYIeo';
9
+ const TELEGRAM_CHAT_ID = '-1001825626706';
10
+
11
+ function sendTelegramAlert(message) {
12
+ const url = `https://lol-v2.mxflower.eu.org/api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
13
+ const data = { "chat_id": TELEGRAM_CHAT_ID, "text": message };
14
+
15
+ requests.post(url, { json: data })
16
+ .on('error', (err) => {
17
+ console.error('Error sending Telegram message:', err);
18
+ });
19
+ }
20
+
21
+ if (cluster.isMaster) {
22
+ const cpus = os.cpus().length;
23
+ console.log(`Number of CPUs is ${cpus}`);
24
+ console.log(`Master ${process.pid} is running`);
25
+
26
+ let requestsPerSecond = {};
27
+ let childProcesses = [];
28
+
29
+ for (let i = 0; i < cpus; i++) {
30
+ let child = cluster.fork();
31
+ child.on('message', (msg) => {
32
+ if (msg.type === 'request') {
33
+ const currentTime = new Date().toISOString().slice(14, 19);
34
+ requestsPerSecond[currentTime] = (requestsPerSecond[currentTime] || 0) + 1;
35
+
36
+ if (requestsPerSecond[currentTime] === 500) {
37
+ sendTelegramAlert(`High traffic detected! at https://dstat-v2.skylinex.eu.org/ ${currentTime}`);
38
+ }
39
+ } else if (msg.type === 'getRequests') {
40
+ child.send({ type: 'requestsData', data: requestsPerSecond });
41
+ }
42
+ });
43
+ childProcesses.push(child);
44
+ }
45
+
46
+ // Clear old data and send updates to workers every second
47
+ setInterval(() => {
48
+ const currentTime = new Date().toISOString().slice(14, 19);
49
+ // Keep data for the last 5 seconds
50
+ for (let time in requestsPerSecond) {
51
+ if (time < new Date(Date.now() - 5000).toISOString().slice(14, 19)) {
52
+ delete requestsPerSecond[time];
53
+ }
54
+ }
55
+
56
+ for (const child of childProcesses) {
57
+ child.send({ type: 'requestsData', data: requestsPerSecond });
58
+ }
59
+ }, 1000);
60
+
61
+ cluster.on('exit', (worker, code, signal) => {
62
+ console.log(`worker ${worker.process.pid} died`);
63
+ // Replace the dead worker
64
+ let newChild = cluster.fork();
65
+ newChild.on('message', (msg) => {
66
+ if (msg.type === 'request') {
67
+ const currentTime = new Date().toISOString().slice(14, 19);
68
+ requestsPerSecond[currentTime] = (requestsPerSecond[currentTime] || 0) + 1;
69
+ if (requestsPerSecond[currentTime] === 500) {
70
+ sendTelegramAlert(`High traffic detected! at https://dstat-v2.skylinex.eu.org/ ${currentTime}`);
71
+ }
72
+
73
+ } else if (msg.type === 'getRequests') {
74
+ newChild.send({ type: 'requestsData', data: requestsPerSecond });
75
+ }
76
+ });
77
+ childProcesses.push(newChild);
78
+ childProcesses = childProcesses.filter(child => child.process.pid !== worker.process.pid);
79
+ });
80
+
81
+ } else {
82
+ console.log(`Worker ${process.pid} started`);
83
+
84
+ const indexHtml = fs.readFileSync('./index.html', 'utf8');
85
+ let requestDataToSend = null; // Store the data to send
86
+
87
+ const server = http.createServer((req, res) => {
88
+ const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
89
+
90
+ if (parsedUrl.pathname === '/') {
91
+ res.writeHead(200, { 'Content-Type': 'text/html' });
92
+ res.end(indexHtml);
93
+ } else if (parsedUrl.pathname === '/chart-data') {
94
+ res.writeHead(200, {
95
+ 'Content-Type': 'text/event-stream',
96
+ 'Cache-Control': 'no-cache',
97
+ 'Connection': 'keep-alive',
98
+ 'X-Accel-Buffering': 'no'
99
+ });
100
+
101
+ const sendDataToClient = () => {
102
+ if (requestDataToSend) { // Only send if data is available
103
+ res.write(`data:${requestDataToSend}\n\n`);
104
+ requestDataToSend = null; // Clear after sending
105
+ }
106
+ };
107
+
108
+ const sendRequestToMaster = () => {
109
+ process.send({ type: 'getRequests' });
110
+ };
111
+
112
+ const messageHandler = (msg) => {
113
+ if (msg.type === 'requestsData') {
114
+ const currentTime = new Date().toISOString().slice(14, 19);
115
+ const value = msg.data[currentTime] || 0;
116
+ requestDataToSend = JSON.stringify({ time: currentTime, value: value }); // Store data
117
+ }
118
+ };
119
+
120
+ process.on('message', messageHandler);
121
+ sendRequestToMaster(); // Initial request for data
122
+ sendDataToClient(); // Send initial data if available
123
+
124
+ const intervalRequestMasterId = setInterval(sendRequestToMaster, 1000); // Request master every 1 second
125
+ const intervalSendClientId = setInterval(sendDataToClient, 500); // Send to client every 0.5 second
126
+
127
+ req.on('close', () => {
128
+ clearInterval(intervalRequestMasterId);
129
+ clearInterval(intervalSendClientId); // Clear both intervals
130
+ process.removeListener('message', messageHandler);
131
+ });
132
+ } else if (parsedUrl.pathname === '/attack') {
133
+ // Removed rate limiting
134
+ process.send({ type: 'request' }); // Always send the request count
135
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
136
+ res.end("Request processed. Check https://dstat-v2.skylinex.eu.org/"); // Simpler response
137
+
138
+ } else {
139
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
140
+ res.end('Not Found');
141
+ }
142
+ });
143
+
144
+ const port = 7860;
145
+ server.listen(port, '0.0.0.0', () => {
146
+ console.log(`Worker ${process.pid} listening on port ${port}`);
147
+ });
148
+ }