From de6fd2dddb51a6979fd3ef0305356eca8c3f6035 Mon Sep 17 00:00:00 2001 From: shats Date: Tue, 10 Feb 2026 22:54:50 +0700 Subject: [PATCH] feat(ping): enhance interface resolution logic by integrating gateway checks for improved tunnel interface matching --- backend/routes/mikrotikConfigRoutes.js | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/backend/routes/mikrotikConfigRoutes.js b/backend/routes/mikrotikConfigRoutes.js index dd6f1dd..7c3e097 100644 --- a/backend/routes/mikrotikConfigRoutes.js +++ b/backend/routes/mikrotikConfigRoutes.js @@ -12,6 +12,7 @@ const { buildMikrotikConfig, buildMikrotikInterfaceBlocks, buildMikrotikRecursiveRoutes, + getParentGateway, } = require('../utils/mikrotikInterfaceGenerator'); const { createRosClient, applyBlock } = require('../services/mikrotikApplyService'); @@ -318,6 +319,7 @@ async function pingViaInterface(req, res) { const config = await loadNetworkConfig(); const tunnelIfaces = Array.isArray(config.tunnelInterfaces) ? config.tunnelInterfaces : []; + // 1) Пытаемся сопоставить gatewayIp с локальным/удалённым IP туннельного интерфейса const iface = tunnelIfaces.find((i) => { const sameServer = i.serverId === serverId || @@ -330,6 +332,40 @@ async function pingViaInterface(req, res) { if (iface) { ifaceName = iface.name || null; } + + // 2) Если по tunnelInterfaces не нашли — пробуем взять интерфейс из gateways в /network-config + if (!ifaceName && Array.isArray(config.gateways)) { + const gw = config.gateways.find((g) => { + if (!g || !g.ip) return false; + const sameServer = + g.serverId === serverId || + g.serverId === server.ip || + g.serverId === server.dns; + return sameServer && g.ip === gatewayIp; + }); + + if (gw) { + // Вариант А: gateway сам хранит имя интерфейса + if (gw.interfaceName) { + ifaceName = gw.interfaceName; + } + + // Вариант Б: gateway ссылается на интерфейс через parentGatewayId / parentGateways + if (!ifaceName && (gw.parentGatewayId || (Array.isArray(gw.parentGateways) && gw.parentGateways.length > 0))) { + const parentRefs = (gw.parentGateways && gw.parentGateways.length > 0) + ? gw.parentGateways + : [{ id: gw.parentGatewayId }]; + + for (const pref of parentRefs) { + const parent = getParentGateway(pref.id, config); + if (parent && parent.parentType === 'interface' && parent.name) { + ifaceName = parent.name; + break; + } + } + } + } + } } const body = {};