Files
MikrotikManager/backend/src/modules/mcp/server-connection-destination-policy.ts
T

25 lines
1.5 KiB
TypeScript

import { isIP } from "node:net"
import { z } from "zod"
/** Pure shared destination policy: no configured pool, actions or services. */
export const serverConnectionHostSchema = z.string().trim().min(1).max(253).transform(value => value.toLowerCase()).refine(value => isIP(value) !== 0 || /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(value) && value.split(".").every(part => part.length > 0 && part.length <= 63), "Некорректный адрес RouterOS")
export function isAllowedServerConnectionAddress(address: string): boolean {
const family = isIP(address)
if (family === 4) {
const [a,b] = address.split(".").map(Number)
// RFC1918 and loopback are intentional managed RouterOS destinations. Link
// local/metadata, unspecified and multicast destinations are never eligible.
return a !== 0 && a < 224 && !(a === 169 && b === 254) && !["168.63.129.16","100.100.100.200"].includes(address)
}
if (family !== 6) return false
const normalized = new URL(`http://[${address}]/`).hostname.slice(1,-1).toLowerCase()
if (normalized.startsWith("::ffff:")) {
const hex = normalized.slice(7).split(":")
if (hex.length !== 2) return false
const bytes = hex.flatMap(part => [parseInt(part,16) >> 8, parseInt(part,16) & 255])
return isAllowedServerConnectionAddress(bytes.join("."))
}
return (normalized === "::1" || !normalized.startsWith("::")) && normalized !== "fd00:ec2::254" && !normalized.startsWith("ff") && !/^fe[89abcdef]/.test(normalized) && !normalized.startsWith("64:ff9b:") && !normalized.startsWith("2002:")
}