feat: Add server availability check API and integrate it into the Dashboard and ServerManager components for enhanced server monitoring and user experience
Publish Fast Tabler Docker image / build-and-push-fast (push) Has been cancelled
Publish Fast Tabler Docker image / build-and-push-fast (push) Has been cancelled
This commit is contained in:
@@ -5,6 +5,7 @@ const cors = require('cors');
|
||||
const path = require('path');
|
||||
const compression = require('compression');
|
||||
const Ajv = require('ajv');
|
||||
const net = require('net');
|
||||
|
||||
const app = express();
|
||||
const port = 3001;
|
||||
@@ -1395,6 +1396,55 @@ app.get('/api/auto-urls', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// --- Servers availability check ---
|
||||
// Simple TCP connect check to 80 then 443 with short timeouts
|
||||
function tcpCheck(host, port, timeoutMs) {
|
||||
return new Promise((resolve) => {
|
||||
const socket = new net.Socket();
|
||||
let settled = false;
|
||||
const onOk = () => { if (!settled) { settled = true; try{ socket.destroy(); }catch{} resolve(true); } };
|
||||
const onFail = () => { if (!settled) { settled = true; try{ socket.destroy(); }catch{} resolve(false); } };
|
||||
socket.setTimeout(timeoutMs);
|
||||
socket.once('error', onFail);
|
||||
socket.once('timeout', onFail);
|
||||
socket.connect(port, host, onOk);
|
||||
});
|
||||
}
|
||||
|
||||
async function checkOneServer(srv, timeoutMs = 1500) {
|
||||
const hostCandidates = [];
|
||||
if (srv.ip) hostCandidates.push(srv.ip);
|
||||
if (srv.dns) hostCandidates.push(srv.dns);
|
||||
for (const host of hostCandidates) {
|
||||
// try 80 then 443
|
||||
const ok80 = await tcpCheck(host, 80, timeoutMs);
|
||||
if (ok80) return true;
|
||||
const ok443 = await tcpCheck(host, 443, timeoutMs);
|
||||
if (ok443) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
app.get('/api/servers/availability', async (req, res) => {
|
||||
try {
|
||||
const data = await s3.getObject({ Bucket: BUCKET_NAME, Key: 'servers.json' }).promise();
|
||||
let servers = [];
|
||||
try {
|
||||
servers = JSON.parse(data.Body.toString('utf-8'));
|
||||
if (!Array.isArray(servers)) servers = [];
|
||||
} catch {
|
||||
servers = [];
|
||||
}
|
||||
const checks = await Promise.allSettled(servers.map(s => checkOneServer(s)));
|
||||
const statuses = servers.map((s, i) => ({ ip: s.ip, dns: s.dns, online: checks[i].status === 'fulfilled' ? Boolean(checks[i].value) : false }));
|
||||
const online = statuses.filter(x => x.online).length;
|
||||
res.json({ online, total: servers.length, statuses });
|
||||
} catch (e) {
|
||||
console.error('availability error', e);
|
||||
res.status(500).json({ online: 0, total: 0, statuses: [] });
|
||||
}
|
||||
});
|
||||
|
||||
// Update auto URLs in S3
|
||||
app.post('/api/auto-urls', async (req, res) => {
|
||||
const { urls } = req.body;
|
||||
|
||||
Reference in New Issue
Block a user