71 lines
5.7 KiB
TypeScript
71 lines
5.7 KiB
TypeScript
import { isIP } from "node:net"
|
|
import { observedBoolean, observedInteger } from "./ospf-observed-values.js"
|
|
import { readInternetPathRows } from "./internet-path-snapshot.js"
|
|
|
|
type Uplink = { id: string; iface: string; name: string; isp: string; ip: string }
|
|
type Row = Record<string, string>
|
|
function ipv4(raw: string | undefined): string | null {
|
|
if (!raw) return null
|
|
const [address, prefix, extra] = raw.trim().split("/")
|
|
return isIP(address) === 4 && extra === undefined && (prefix === undefined || /^\d+$/.test(prefix) && Number(prefix) <= 32) ? address : null
|
|
}
|
|
function gatewayInterface(raw: string | undefined): string | null {
|
|
const parts = raw?.trim().split("%")
|
|
return parts?.length === 2 && parts[1] ? parts[1] : null
|
|
}
|
|
function gatewayAddress(raw: string | undefined): string | null { return ipv4(raw?.split("%")[0]) }
|
|
function unique(values: Array<string | null | undefined>) { return [...new Set(values.filter((value): value is string => !!value))] }
|
|
|
|
/** A local IPv4/main observation, not packet forwarding or a policy-route proof. */
|
|
export function buildWanRuntime(uplinks: Uplink[], sources: { dhcp: unknown; addresses: unknown; routes: unknown }) {
|
|
const dhcp = readInternetPathRows(sources.dhcp, "WAN DHCP", ["interface"])
|
|
const addresses = readInternetPathRows(sources.addresses, "WAN addresses", ["address", "interface"])
|
|
const routes = readInternetPathRows(sources.routes, "WAN routes", ["dst-address"])
|
|
const findings = new Set<string>()
|
|
const defaults = routes.filter(row => row["dst-address"] === "0.0.0.0/0")
|
|
const possibleMain = defaults.filter(row => row["routing-table"] === "main" || !row["routing-table"])
|
|
const unknown = possibleMain.some(row => !row["routing-table"] || observedBoolean(row.active) === null || observedBoolean(row.active) === true && (observedBoolean(row.disabled) === true || observedBoolean(row.inactive) === true || observedInteger(row.distance, 0, 255) === null))
|
|
if (unknown) findings.add("default-route-state-unknown")
|
|
const active = possibleMain.filter(row => row["routing-table"] === "main" && observedBoolean(row.active) === true && observedBoolean(row.disabled) !== true && observedBoolean(row.inactive) !== true && observedInteger(row.distance, 0, 255) !== null)
|
|
const bestDistance = active.length ? Math.min(...active.map(row => Number(row.distance))) : null
|
|
const best = active.filter(row => Number(row.distance) === bestDistance)
|
|
const multipath = best.some(row => row.gateway?.includes(",") || row["immediate-gw"]?.includes(","))
|
|
const defaultRouteStatus = unknown ? "unknown" : best.length > 1 || multipath ? "ambiguous" : best.length === 1 ? "confirmed" : "none"
|
|
if (multipath) findings.add("multipath-default-gateway")
|
|
if (best.length > 1) findings.add("multiple-active-default-routes")
|
|
const selected = defaultRouteStatus === "confirmed" ? best[0] : undefined
|
|
const gateway = selected?.gateway?.trim() || null, immediateGateway = selected?.["immediate-gw"]?.trim() || null
|
|
|
|
const inferInterface = (route: Row): string | null => {
|
|
const explicit = unique([gatewayInterface(route["immediate-gw"]), gatewayInterface(route.gateway), route.interface?.trim()])
|
|
if (explicit.length === 1) return explicit[0]
|
|
if (explicit.length > 1) { findings.add("default-interface-conflict"); return null }
|
|
const target = gatewayAddress(route["immediate-gw"]) ?? gatewayAddress(route.gateway)
|
|
if (!target) return null
|
|
const candidates = unique(dhcp.filter(row => row.status?.toLowerCase() === "bound" && observedBoolean(row.disabled) !== true && gatewayAddress(row.gateway) === target).map(row => row.interface))
|
|
if (candidates.length === 1) return candidates[0]
|
|
if (candidates.length > 1) findings.add("default-interface-ambiguous")
|
|
return null
|
|
}
|
|
const defaultInterface = selected ? inferInterface(selected) : null
|
|
if (selected && !defaultInterface) findings.add("default-interface-unknown")
|
|
const resultUplinks = uplinks.map(uplink => {
|
|
const iface = uplink.iface.trim()
|
|
const clients = dhcp.filter(row => row.interface.trim() === iface && observedBoolean(row.disabled) !== true)
|
|
const leases = unique(clients.filter(row => row.status?.toLowerCase() === "bound").map(row => ipv4(row.address)))
|
|
const interfaceAddresses = unique(addresses.filter(row => row.interface.trim() === iface && observedBoolean(row.disabled) !== true && observedBoolean(row.invalid) !== true).map(row => ipv4(row.address)))
|
|
const statuses = unique(clients.map(row => row.status?.trim()))
|
|
const addressSource = leases.length === 1 ? "dhcp-bound" : leases.length > 1 || interfaceAddresses.length > 1 ? "ambiguous" : interfaceAddresses.length === 1 ? "interface" : "unknown"
|
|
const leasedIp = leases.length === 1 ? leases[0] : leases.length === 0 && interfaceAddresses.length === 1 ? interfaceAddresses[0] : null
|
|
if (addressSource === "ambiguous") findings.add("uplink-address-ambiguous")
|
|
return { id: uplink.id, iface, name: uplink.name, isp: uplink.isp, configuredIp: uplink.ip, leasedIp,
|
|
dhcpStatus: statuses.length === 1 ? statuses[0] : null, isDefault: defaultInterface !== null && defaultInterface === iface,
|
|
addressSource, addresses: interfaceAddresses, defaultKnown: defaultRouteStatus === "none" || defaultRouteStatus === "confirmed" && defaultInterface !== null,
|
|
}
|
|
})
|
|
return { defaultGateway: gateway, immediateGateway, defaultInterface, uplinks: resultUplinks, defaultRouteStatus,
|
|
activeDefaults: active.slice(0, 200).map(row => ({ id: row[".id"] ?? null, gateway: row.gateway ?? null, immediateGateway: row["immediate-gw"] ?? null, distance: Number(row.distance) })),
|
|
activeDefaultsTruncated: active.length > 200, findings: [...findings], family: "ipv4", table: "main", forwardingVerified: false,
|
|
}
|
|
}
|