-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (39 loc) · 1.25 KB
/
server.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const ping = require('ping');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.static(path.join(__dirname, 'public')));
const ips = [
'foxic.ir',
'bruh.ir',
'm1ch.ir'
];
const responseTimes = {};
async function getStatuses() {
const statuses = await Promise.all(ips.map(async (ip) => {
const res = await ping.promise.probe(ip);
const responseTime = res.time;
if (!responseTimes[ip]) {
responseTimes[ip] = [];
}
responseTimes[ip].push(responseTime);
const avgResponseTime = responseTimes[ip].reduce((a, b) => a + b, 0) / responseTimes[ip].length;
const uptime = res.alive ? 100 : 0;
return {
ip,
status: res.alive ? 'up' : 'down',
response_time: responseTime,
avg_response_time: avgResponseTime.toFixed(2),
uptime: uptime
};
}));
return statuses;
}
app.get('/api/status', async (req, res) => {
const statuses = await getStatuses();
res.json(statuses);
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});