52 lines
4.4 KiB
TypeScript
52 lines
4.4 KiB
TypeScript
import { isIP } from "node:net"
|
|
import { readGreComment } from "./gre-comment.js"
|
|
import { readGreNativeRows } from "./gre-observed-state.js"
|
|
import { maskNetworkSecrets, nativeNetworkRows, type NativeNetworkRow } from "./network-observed-read.js"
|
|
import { observedBoolean, observedDurationMs, observedInteger } from "./ospf-observed-values.js"
|
|
|
|
/** Project observations independently from the defaults used by the legacy editor. */
|
|
export function observeGreCatalog(serverId: number, raw: unknown, ipv4: unknown, ipv6: unknown) {
|
|
const rows = readGreNativeRows(nativeNetworkRows(raw, [".id", "name"]))
|
|
const addresses: Array<{ raw: NativeNetworkRow; family: 4 | 6 }> = [nativeNetworkRows(ipv4, [".id", "address", "interface"]).map(row => ({ raw: row, family: 4 as const })), nativeNetworkRows(ipv6, [".id", "address", "interface"]).map(row => ({ raw: row, family: 6 as const }))].flat()
|
|
const identities = new Map<string, Set<string>>()
|
|
for (const row of rows) for (const ref of [row.name, row[".id"]]) identities.set(ref, new Set([...(identities.get(ref) ?? []), row[".id"]]))
|
|
const addressesById = new Map<string, typeof addresses>()
|
|
for (const address of addresses) {
|
|
const candidates = identities.get(address.raw.interface!)
|
|
if (candidates && candidates.size > 1) throw new Error("Неоднозначная привязка адреса GRE")
|
|
const parent = candidates?.values().next().value
|
|
if (parent) { const values = addressesById.get(parent) ?? []; values.push(address); addressesById.set(parent, values) }
|
|
}
|
|
const secrets = rows.map(row => row["ipsec-secret"]).filter(Boolean)
|
|
const tunnels = rows.map(row => {
|
|
const disabled = observedBoolean(row.disabled), running = observedBoolean(row.running)
|
|
const metadata = readGreComment(row.comment)
|
|
const keepalive = row.keepalive?.trim()
|
|
const parts = keepalive?.split(",")
|
|
const interval = parts?.length === 2 ? /^\d+$/.test(parts[0].trim()) ? observedInteger(parts[0].trim()) : (observedDurationMs(parts[0].trim()) ?? NaN) / 1000 : null
|
|
const retries = parts?.length === 2 ? observedInteger(parts[1].trim()) : null
|
|
const keepaliveDisabled = keepalive === "none" || keepalive === "0"
|
|
const keepaliveKnown = keepaliveDisabled || interval !== null && Number.isFinite(interval) && interval >= 0 && retries !== null
|
|
const localAddresses = (addressesById.get(row[".id"]) ?? []).sort((a, b) => a.family - b.family || (a.raw[".id"] ?? "").localeCompare(b.raw[".id"] ?? "")).map(({ raw: address, family: addressFamily }) => {
|
|
const [host, prefix, extra] = (address.address ?? "").split("/"), family = isIP(host)
|
|
const validCidr = family === addressFamily && extra === undefined && /^\d+$/.test(prefix ?? "") && Number(prefix) <= (family === 4 ? 32 : 128)
|
|
return { rosId: address[".id"], address: address.address ?? null, family: addressFamily,
|
|
disabled: observedBoolean(address.disabled), dynamic: observedBoolean(address.dynamic), invalid: observedBoolean(address.invalid),
|
|
configurationVerified: validCidr && observedBoolean(address.disabled) === false && observedBoolean(address.dynamic) === false && observedBoolean(address.invalid) === false }
|
|
})
|
|
return { id: `${serverId}:${row[".id"]}`, rosId: row[".id"], serverId: String(serverId), name: row.name,
|
|
localAddress: row["local-address"] ?? null, remoteAddress: row["remote-address"] ?? null,
|
|
localInnerIpMetadata: row.comment?.match(/address\s*=\s*([0-9a-fA-F:.]+\/\d+)/)?.[1] ?? null, localInnerIpMetadataVerified: false,
|
|
localAddresses, remoteInnerIp: metadata.remoteInnerIp || null, remoteInnerIpSource: "comment-metadata", remoteInnerIpVerified: false,
|
|
comment: metadata.comment, enabled: disabled === null ? null : !disabled, running,
|
|
status: disabled === null || !disabled && running === null ? "unknown" : disabled ? "down" : running ? "up" : "degraded",
|
|
mtu: observedInteger(row.mtu, 68, 65535), dscp: row.dscp === "inherit" ? "inherit" : observedInteger(row.dscp, 0, 63),
|
|
keepalive: keepalive ?? null, keepaliveInterval: keepaliveKnown ? keepaliveDisabled ? 0 : interval : null,
|
|
keepaliveRetries: keepaliveKnown ? keepaliveDisabled ? 0 : retries : null,
|
|
clampTcpMss: observedBoolean(row["clamp-tcp-mss"]), allowFastPath: observedBoolean(row["allow-fast-path"]),
|
|
ipsecConfigured: row["ipsec-secret"] === undefined ? null : row["ipsec-secret"].trim().length > 0,
|
|
forwardingVerified: false }
|
|
})
|
|
return maskNetworkSecrets(tunnels, secrets)
|
|
}
|