89 lines
6.5 KiB
TypeScript
89 lines
6.5 KiB
TypeScript
import { isIP } from "node:net"
|
|
import { observedInteger, observedBoolean } from "./ospf-observed-values.js"
|
|
import { isManagedRecursiveRoute, userRecursiveComment, type MappedRecursiveRoute } from "./config-apply-plan.js"
|
|
import { isOwnedRecursiveComment } from "../managed-markers.js"
|
|
|
|
function rawRows(payload: unknown): Array<Record<string, unknown>> {
|
|
if (!Array.isArray(payload) || payload.some(row => row === null || typeof row !== "object" || Array.isArray(row))) throw new Error("Некорректный ответ маршрутов RouterOS")
|
|
const rows = payload as Array<Record<string, unknown>>
|
|
if (rows.some(row => row.comment !== undefined && typeof row.comment !== "string")) throw new Error("Некорректные метки владения маршрутов")
|
|
const ids = rows.map(row => row[".id"])
|
|
if (ids.some(id => typeof id !== "string" || !id.trim()) || new Set(ids).size !== ids.length) throw new Error("Неоднозначные ID маршрутов RouterOS")
|
|
return rows
|
|
}
|
|
function rosText(value: unknown): string | undefined { return typeof value === "string" ? value : undefined }
|
|
function text(value: unknown): string | null { return typeof value === "string" && value.trim() ? value.trim() : null }
|
|
export type RecursiveObservedProperty = "disabled" | "check-gateway"
|
|
|
|
/** RouterOS print can omit unset properties. Ask get for the effective value,
|
|
* rather than interpreting an incomplete snapshot as enabled/no-check. */
|
|
export async function readRecursiveEffectiveProperties(
|
|
payload: unknown,
|
|
readProperty: (id: string, property: RecursiveObservedProperty) => Promise<unknown>,
|
|
): Promise<Array<Record<string, unknown>>> {
|
|
const rows = rawRows(payload).map(row => ({ ...row }))
|
|
const tasks: Array<{ row: Record<string, unknown>; property: RecursiveObservedProperty }> = []
|
|
for (const row of rows) {
|
|
if (typeof row.comment !== "string" || !isOwnedRecursiveComment(row.comment)) continue
|
|
for (const property of ["disabled", "check-gateway"] as const) {
|
|
if (row[property] === undefined) tasks.push({ row, property })
|
|
}
|
|
}
|
|
let cursor = 0, failed = false
|
|
await Promise.all(Array.from({ length: Math.min(2, tasks.length) }, async () => {
|
|
while (!failed && cursor < tasks.length) {
|
|
const { row, property } = tasks[cursor++]!
|
|
try {
|
|
const response = await readProperty(String(row[".id"]), property)
|
|
// An empty successful /get reply means the optional check is unset.
|
|
// Do not treat null, malformed envelopes or failed reads as defaults.
|
|
const unset = property === "check-gateway" && Array.isArray(response) && response.length === 0
|
|
const value = response && typeof response === "object" && !Array.isArray(response) && "ret" in response ? response.ret : undefined
|
|
if (property === "disabled") {
|
|
if (typeof value !== "string" || observedBoolean(value) === null) throw new Error("Не подтверждён disabled маршрута " + row[".id"])
|
|
row.disabled = observedBoolean(value) ? "true" : "false"
|
|
} else {
|
|
if (!unset && (typeof value !== "string" || !["none", "arp", "ping", "bfd"].includes(value))) throw new Error("Не подтверждён check-gateway маршрута " + row[".id"])
|
|
row[property] = unset ? "none" : value
|
|
}
|
|
} catch (error) { failed = true; throw error }
|
|
}
|
|
}))
|
|
return rows
|
|
}
|
|
export function parseRecursiveCatalog(payload: unknown): MappedRecursiveRoute[] {
|
|
return rawRows(payload).filter(row => typeof row.comment === "string" && isOwnedRecursiveComment(row.comment)).map(row => {
|
|
const id = String(row[".id"])
|
|
// The editor replaces the entire managed set. Never silently omit an owned row.
|
|
const destination = text(row["dst-address"]), gateway = text(row.gateway)
|
|
const [ip, prefix] = destination?.split("/") ?? []
|
|
const gatewayIp = gateway?.split("%")[0]
|
|
if (!destination || !gateway || isIP(ip ?? "") !== 4 || prefix !== undefined && (!/^\d+$/.test(prefix) || Number(prefix) > 32) || isIP(gatewayIp ?? "") !== 4 || !isManagedRecursiveRoute(row) || observedBoolean(rosText(row.static)) !== true) throw new Error("Управляемый маршрут " + id + " не подтверждён как поддерживаемый статический маршрут")
|
|
const distance = observedInteger(rosText(row.distance), 0, 255), disabled = observedBoolean(rosText(row.disabled))
|
|
const routingTable = text(row["routing-table"]), checkGateway = text(row["check-gateway"])
|
|
if (distance === null || disabled === null || routingTable === null || !checkGateway || !["none", "arp", "ping", "bfd"].includes(checkGateway)) throw new Error("Не подтверждены параметры управляемого маршрута " + id)
|
|
const scope = row.scope === undefined || row.scope === "" ? null : observedInteger(rosText(row.scope), 0, 255)
|
|
const targetScope = row["target-scope"] === undefined || row["target-scope"] === "" ? null : observedInteger(rosText(row["target-scope"]), 0, 255)
|
|
if (row.scope !== undefined && row.scope !== "" && scope === null || row["target-scope"] !== undefined && row["target-scope"] !== "" && targetScope === null) throw new Error("Некорректный scope управляемого маршрута " + id)
|
|
return { id, dstAddress: String(row["dst-address"]), gateway: String(row.gateway), distance, disabled, routingTable, checkGateway, scope, targetScope, country: "", comment: userRecursiveComment(String(row.comment)) }
|
|
})
|
|
}
|
|
export type RecursiveGatewayObservation = { id: string; ip: string; name: string; status: "active" | "inactive" | "unknown" }
|
|
export function parseRecursiveGatewayCatalog(payload: unknown): RecursiveGatewayObservation[] {
|
|
const result = new Map<string, RecursiveGatewayObservation>()
|
|
for (const row of rawRows(payload)) {
|
|
if (typeof row.gateway !== "string") continue
|
|
const [ip, name = ip] = row.gateway.trim().split("%")
|
|
if (!/^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || ip.split(".").some(part => Number(part) > 255) || !name) continue
|
|
const id = ip + "%" + name
|
|
const active = typeof row.active === "boolean" ? row.active : observedBoolean(rosText(row.active))
|
|
const inactive = typeof row.inactive === "boolean" ? row.inactive : observedBoolean(rosText(row.inactive))
|
|
let status: RecursiveGatewayObservation["status"] = "unknown"
|
|
if (active === true && inactive !== true) status = "active"
|
|
if (active !== true && (active === false || inactive === true)) status = "inactive"
|
|
const previous = result.get(id)
|
|
result.set(id, { id, ip, name, status: previous?.status === "active" || status === "active" ? "active" : previous?.status === "unknown" || status === "unknown" ? "unknown" : "inactive" })
|
|
}
|
|
return [...result.values()]
|
|
}
|