import type { GreTunnel, Server } from "./data" import { quoteRosString } from "./vxlan-export" const commentLine = (value: string) => value.replace(/[\r\n\x00-\x1f\x7f]/g, " ") /** GRE/PSK fragment: https://help.mikrotik.com/docs/spaces/ROS/pages/24805531/GRE */ export function generateGreRsc(t: GreTunnel, servers: Record): string { const server = servers[t.serverId] const lines = ["# GRE — " + commentLine(t.name)] if (server) lines.push("# Сервер: " + commentLine(server.name) + " (" + commentLine(server.host) + ")") lines.push("# Фрагмент интерфейса и локального IP; удалённая сторона и маршруты не включены", "") const args = [ "name=" + quoteRosString(t.name), "remote-address=" + quoteRosString(t.remoteAddress), "mtu=" + t.mtu, "keepalive=" + (t.keepaliveInterval > 0 ? t.keepaliveInterval + "s," + t.keepaliveRetries : "0"), "dscp=" + t.dscp, "clamp-tcp-mss=" + (t.clampTcpMss ? "yes" : "no"), "allow-fast-path=" + (t.allowFastPath ? "yes" : "no"), "disabled=" + (t.enabled ? "no" : "yes"), "comment=" + quoteRosString(t.comment), ] if (t.localAddress && t.localAddress !== "0.0.0.0") args.push("local-address=" + quoteRosString(t.localAddress)) if (t.ipsec) args.push("ipsec-secret=" + quoteRosString(t.ipsec.secret)) lines.push("/interface/gre/add " + args.join(" ")) if (t.localInnerIp) lines.push("/ip/address/add address=" + quoteRosString(t.localInnerIp) + " interface=" + quoteRosString(t.name)) if (t.ipsec) lines.push("# Peer/policy создаёт RouterOS по ipsec-secret; ручные объекты IPsec не экспортируются") if (t.ipsec && t.allowFastPath) lines.push("# В снимке включён FastPath: для GRE с IPsec документация RouterOS требует allow-fast-path=no") return lines.join("\n") + "\n" }