Files
MikrotikManager/backend/src/modules/mcp/routing-native-endpoint.ts
T

29 lines
1.3 KiB
TypeScript

import { isIP } from "node:net"
export function parseRoutingEndpoint(value: unknown) {
if (typeof value !== "string" || !value.length || value.length > 256) throw Error("missing-or-unsupported-field")
const match = /^([^%@/,\s]+)(?:\/(\d+))?(?:%([^%@/,\s]+))?(?:@([^%@/,\s]+))?$/.exec(value)
if (!match) throw Error("unsupported-endpoint")
const [, address, prefix, interfaceName, vrf] = match
const family = isIP(address)
if (!family) throw Error("unsupported-address")
if (prefix !== undefined && prefix !== String(family === 4 ? 32 : 128)) throw Error("unsupported-peer-range")
return { address, interface: interfaceName, vrf }
}
export function matchesBgpSessionContext(
row: Record<string, unknown>,
expected: { table: string; vrf: string },
local: ReturnType<typeof parseRoutingEndpoint>,
remote: ReturnType<typeof parseRoutingEndpoint>,
) {
if (!Object.hasOwn(row, "routing-table") || row["routing-table"] !== expected.table) return false
if (Object.hasOwn(row, "vrf")) {
return row.vrf === expected.vrf && (local.vrf === undefined || local.vrf === expected.vrf) &&
(remote.vrf === undefined || remote.vrf === expected.vrf)
}
// RouterOS may report the transport VRF only in the two runtime addresses.
// The installation table alone cannot identify it.
return local.vrf === expected.vrf && remote.vrf === expected.vrf
}