Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Failing after 3m38s
Docker images / backend-image (push) Skipped
Docker images / frontend-image (push) Successful in 2m11s
Docker images / updater-image (push) Successful in 38s
Docker images / netflow-agent-image (push) Successful in 31s
Docker images / publish-release (push) Skipped
Docker images / notify-webhook (push) Skipped
95 lines
6.0 KiB
TypeScript
95 lines
6.0 KiB
TypeScript
import { createHash } from "node:crypto"
|
|
import type { IxRouterObject, IxSite } from "@mmapp/contracts/internet-exchange"
|
|
import { canonicalBgpPolicyPrefix } from "./bgp-community-policy-model.js"
|
|
|
|
type Binding = IxSite["interfaces"][number]
|
|
|
|
export function ixTransitRemoteInterface(namespace: string, name: string) {
|
|
return namespace.slice(0, 16) + "-" + name.slice(-12)
|
|
}
|
|
|
|
export function compileIxTransit(namespace: string, site: IxSite, binding: Binding, add: (object: IxRouterObject) => void) {
|
|
const transit = binding.provisionTransit!
|
|
const serverId = binding.peerServerId!
|
|
const remote = ixTransitRemoteInterface(namespace, binding.name)
|
|
const remoteVrf = remote + "-v"
|
|
const ipv6 = transit.remoteAddress.includes(":")
|
|
const ip = ipv6 ? "/ipv6" : "/ip"
|
|
const hostMask = ipv6 ? "/128" : "/32"
|
|
const defaultPrefix = ipv6 ? "::/0" : "0.0.0.0/0"
|
|
const clients = site.interfaces.filter(item => {
|
|
const group = site.groups.find(group => group.id === item.linux?.exit)
|
|
return item.linux?.enabled && group && [group.primary.interface, group.backup?.interface].includes(binding.name)
|
|
})
|
|
const sources = [...new Set(clients.flatMap(client => [
|
|
(client.provisionWireguard ?? client.provisionGre)!.peerAddress + hostMask, ...client.prefixes,
|
|
]).map(canonicalBgpPolicyPrefix))].sort()
|
|
const hub = transit.localAddress.split("/")[0]
|
|
const mask = transit.localAddress.split("/")[1]
|
|
const key = (role: string, kind: "private" | "public") => `ix-transit:${kind}:${transit.credentialId}:${role}`
|
|
const emit = (node: number, path: string, suffix: string, fields: Record<string, string>) => add({
|
|
serverId: node, path, fields: { ...fields, comment: namespace + ":transit-" + binding.name + "-" + suffix },
|
|
})
|
|
const emitRemote = (path: string, suffix: string, fields: Record<string, string>) => emit(serverId, path, suffix, fields)
|
|
emitRemote("/interface/wireguard", "wg", {
|
|
name: remote, "listen-port": String(transit.remotePort), "private-key": key("b", "private"),
|
|
})
|
|
emitRemote("/ip/vrf", "vrf", { name: remoteVrf, interfaces: remote })
|
|
emitRemote(ip + "/address", "address", { interface: remote, address: transit.remoteAddress + "/" + mask,
|
|
...(ipv6 && { advertise: "no" }) })
|
|
emitRemote("/interface/wireguard/peers", "peer", {
|
|
interface: remote, "public-key": key("a", "public"), "allowed-address": [hub + hostMask, ...sources].join(","),
|
|
"endpoint-address": transit.localEndpoint, "endpoint-port": String(transit.localPort), "persistent-keepalive": "25s",
|
|
})
|
|
emitRemote(ip + "/route", "default", { "routing-table": remoteVrf, "dst-address": defaultPrefix,
|
|
gateway: transit.remoteWan.nextHop + "%" + transit.remoteWan.interface + "@main",
|
|
})
|
|
emitRemote(ip + "/route", "default-drop", { "routing-table": remoteVrf, "dst-address": defaultPrefix, blackhole: "yes", distance: "254" })
|
|
const filter = ip + "/firewall/filter"
|
|
const srcList = remote + "-src"
|
|
emitRemote("/ip/firewall/filter", "listen", { chain: "input", protocol: "udp", "src-address": transit.localEndpoint + "/32",
|
|
"dst-address": transit.remoteEndpoint + "/32", "dst-port": String(transit.remotePort), action: "accept" })
|
|
emitRemote(filter, "ping", { chain: "input", "in-interface": remoteVrf, protocol: ipv6 ? "icmpv6" : "icmp", "src-address": hub + hostMask, action: "accept" })
|
|
emitRemote(filter, "deny-management", { chain: "input", "in-interface": remoteVrf, action: "drop" })
|
|
emitRemote(filter, "out", { chain: "forward", "in-interface": remoteVrf, "out-interface": transit.remoteWan.interface,
|
|
"src-address-list": srcList, action: "accept" })
|
|
emitRemote(filter, "return-filter", {
|
|
chain: "forward", "dst-address-list": srcList, "connection-state": "established,related", action: "accept",
|
|
})
|
|
emitRemote(filter, "deny-forward", { chain: "forward", "in-interface": remoteVrf, action: "drop" })
|
|
emitRemote(filter, "deny-inbound", { chain: "forward", "dst-address-list": srcList, action: "drop" })
|
|
if (!ipv6) emitRemote("/ip/firewall/nat", "nat", { chain: "srcnat", "out-interface": transit.remoteWan.interface,
|
|
"src-address-list": srcList, action: "masquerade" })
|
|
for (const address of [hub + hostMask, ...sources]) {
|
|
const identity = createHash("sha256").update(address).digest("hex").slice(0, 12)
|
|
emitRemote(ip + "/firewall/address-list", "client-" + identity, { address, list: srcList })
|
|
for (const table of address === hub + hostMask ? ["main"] : [remoteVrf, "main"]) emitRemote(ip + "/route", "return-" + table + "-" + identity, {
|
|
"routing-table": table, "dst-address": address, gateway: hub + "%" + remote + "@" + remoteVrf,
|
|
})
|
|
}
|
|
emit(site.serverId, "/interface/wireguard", "source-wg", {
|
|
name: binding.name, "listen-port": String(transit.localPort), "private-key": key("a", "private"),
|
|
})
|
|
// Source peer/address depend on the local VRF assignment and are emitted by the caller.
|
|
}
|
|
|
|
export function ixTransitSourceObjects(namespace: string, site: IxSite, binding: Binding): IxRouterObject[] {
|
|
const transit = binding.provisionTransit!
|
|
const ipv6 = transit.remoteAddress.includes(":")
|
|
return [
|
|
{ serverId: site.serverId, path: ipv6 ? "/ipv6/address" : "/ip/address", fields: { address: transit.localAddress, interface: binding.name,
|
|
...(ipv6 && { advertise: "no" }),
|
|
comment: namespace + ":transit-" + binding.name + "-source-address" } },
|
|
{ serverId: site.serverId, path: "/interface/wireguard/peers", fields: {
|
|
interface: binding.name, "public-key": `ix-transit:public:${transit.credentialId}:b`, "allowed-address": ipv6 ? "::/0" : "0.0.0.0/0",
|
|
"endpoint-address": transit.remoteEndpoint, "endpoint-port": String(transit.remotePort), "persistent-keepalive": "25s",
|
|
comment: namespace + ":transit-" + binding.name + "-source-peer",
|
|
} },
|
|
{ serverId: site.serverId, path: "/ip/firewall/filter", fields: {
|
|
chain: "input", action: "accept", protocol: "udp", "src-address": transit.remoteEndpoint + "/32",
|
|
"dst-address": transit.localEndpoint + "/32", "dst-port": String(transit.localPort),
|
|
comment: namespace + ":transit-" + binding.name + "-source-listen",
|
|
} },
|
|
]
|
|
}
|