feat(PingServices): implement UI settings for ping services configuration and enhance ping functionality through RouterOS
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m34s

This commit is contained in:
2026-02-16 23:35:25 +07:00
parent dfcbdb35be
commit 838ca6b220
3 changed files with 188 additions and 88 deletions
+57 -8
View File
@@ -339,9 +339,63 @@ const PING_SERVICES = [
{ id: 'instagram', name: 'Instagram', host: 'instagram.com', port: 443 },
];
// GET /api/ping-services — RTT до Google, Cloudflare, Yandex, Instagram (для карточек на дашборде)
async function getPingServices(req, res) {
/** Загрузить UI-настройки из S3 (для pingServicesSource и др.) */
async function loadUiSettingsSync() {
try {
const data = await s3.send(new GetObjectCommand({ Bucket: BUCKET_NAME, Key: 'bgp_data/rt_ui_settings.json' }));
const jsonText = await streamToString(data.Body);
const parsed = JSON.parse(jsonText || '{}');
return parsed && typeof parsed === 'object' ? parsed : {};
} catch {
return {};
}
}
// GET /api/ping-services — RTT до Google, Cloudflare, Yandex, Instagram (веб или через RouterOS по настройке)
async function getPingServices(req, res) {
const fallbackPayload = {
google: { id: 'google', name: 'Google', host: '8.8.8.8', ms: null },
cloudflare: { id: 'cloudflare', name: 'Cloudflare', host: '1.1.1.1', ms: null },
yandex: { id: 'yandex', name: 'Yandex', host: 'ya.ru', ms: null },
instagram: { id: 'instagram', name: 'Instagram', host: 'instagram.com', ms: null },
};
try {
const uiSettings = await loadUiSettingsSync();
const viaRouter = String(uiSettings.pingServicesSource || 'web').toLowerCase() === 'router';
if (viaRouter) {
const { runPingViaRouter } = require('./mikrotikConfigRoutes');
const { readServersFromS3 } = require('./serversRoutes');
const servers = await readServersFromS3();
const jumphosts = servers.filter((s) => s && String(s.type || '').toLowerCase() === 'jumphost');
const serverId = (uiSettings.pingServicesServerId && String(uiSettings.pingServicesServerId).trim()) || (jumphosts[0] && (jumphosts[0].id || jumphosts[0].dns || jumphosts[0].ip));
const gatewayIp = (uiSettings.pingServicesGatewayIp && String(uiSettings.pingServicesGatewayIp).trim()) || null;
let gatewayIpResolved = gatewayIp;
if (!gatewayIpResolved && serverId && jumphosts.length > 0) {
const server = jumphosts.find((s) => (s.id || s.dns || s.ip) === serverId) || jumphosts[0];
const gateways = Array.isArray(server.gateways) ? server.gateways : [];
const primary = gateways.find((g) => g && g.primary) || gateways[0];
gatewayIpResolved = primary && (primary.ip || primary.remoteIp) ? (primary.ip || primary.remoteIp) : null;
}
if (!serverId || !gatewayIpResolved) {
return res.json(fallbackPayload);
}
const results = await Promise.all(
PING_SERVICES.map(async (svc) => {
try {
const result = await runPingViaRouter(serverId, gatewayIpResolved, svc.host, 3);
const ms = typeof result.avgMs === 'number' ? Math.round(result.avgMs) : null;
return { id: svc.id, name: svc.name, host: svc.host, ms };
} catch (_) {
return { id: svc.id, name: svc.name, host: svc.host, ms: null };
}
})
);
const byId = {};
results.forEach((r) => { byId[r.id] = r; });
return res.json(byId);
}
const results = await Promise.all(
PING_SERVICES.map(async (svc) => {
const ms = await measureTcpRtt(svc.host, svc.port, 6000);
@@ -353,12 +407,7 @@ async function getPingServices(req, res) {
res.json(byId);
} catch (e) {
console.error('ping-services error', e);
res.status(500).json({
google: { id: 'google', name: 'Google', host: '8.8.8.8', ms: null },
cloudflare: { id: 'cloudflare', name: 'Cloudflare', host: '1.1.1.1', ms: null },
yandex: { id: 'yandex', name: 'Yandex', host: 'ya.ru', ms: null },
instagram: { id: 'instagram', name: 'Instagram', host: 'instagram.com', ms: null },
});
res.status(500).json(fallbackPayload);
}
}