66 lines
5.5 KiB
TypeScript
66 lines
5.5 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { MikrotikClient } from "./mikrotik.js"
|
|
import { fetchWireguardRestoreState } from "./wireguard-live.js"
|
|
import { putIpAddress } from "./wireguard-ros.js"
|
|
import { canonicalWireguardSnapshot, planWireguardRestore, InterfaceAddressConflict } from "./entity-snapshots.js"
|
|
import { parseWireguardImport } from "./wireguard-import.js"
|
|
import { generateMikrotikRsc } from "./wireguard-config.js"
|
|
import { wgCreateInterfaceSchema, wgPatchInterfaceSchema } from "@mmapp/contracts/wireguard"
|
|
|
|
const original = MikrotikClient.fromServer
|
|
const calls: string[] = []
|
|
const raw: Record<string, unknown[]> = {
|
|
"/interface/wireguard": [{ ".id": "*w", name: "wg-own", "private-key": "" }],
|
|
"/interface/wireguard/peers": [],
|
|
"/ip/address": [{ ".id": "*1", interface: "wg-own", address: "10.1.0.1/30" }],
|
|
"/ipv6/address": [{ ".id": "*1", interface: "wg-own", address: "fd00:152::1/64", advertise: "true", comment: "own", "no-dad": "true" }, { ".id": "*2", interface: "wg-own", address: "fd00:152::2/64", disabled: "true" }, { ".id": "*3", interface: "wg-own", address: "fe80::1/64", dynamic: "true" }],
|
|
}
|
|
try {
|
|
MikrotikClient.fromServer = () => ({ get: async (path: string) => { calls.push(path); return structuredClone(raw[path]) } }) as unknown as MikrotikClient
|
|
const state = await fetchWireguardRestoreState({ id: 1 } as Parameters<typeof fetchWireguardRestoreState>[0])
|
|
assert.equal(state.addrs.length, 3)
|
|
assert.equal(state.snapshot.interfaces[0]?.innerAddresses?.length, 3)
|
|
assert.equal(planWireguardRestore(state.snapshot, state).filter(op => op.path.includes("/address")).length, 0)
|
|
const legacy = canonicalWireguardSnapshot({ interfaces: [{ name: "wg-own", address: "fd00:152::1/64" }] })
|
|
assert.throws(() => planWireguardRestore(legacy, state), InterfaceAddressConflict)
|
|
const desired = structuredClone(state.snapshot)
|
|
desired.interfaces[0]!.innerAddresses = [{ address: "fd00:152::9/64", disabled: false }]
|
|
const ops = planWireguardRestore(desired, state)
|
|
assert(ops.some(op => op.op === "delete" && op.path === "/ip/address/*1"))
|
|
assert(ops.some(op => op.op === "delete" && op.path === "/ipv6/address/*1"))
|
|
assert(ops.some(op => op.op === "delete" && op.path === "/ipv6/address/*2"))
|
|
assert(!ops.some(op => op.path.endsWith("/*3")))
|
|
assert(ops.some(op => op.op === "put" && op.path === "/ipv6/address" && op.body.advertise === "no"))
|
|
raw["/interface"] = [{ ".id": "*generic", name: "wg-own", type: "wg" }, { ".id": "*w", name: "ether1", type: "ether" }]
|
|
raw["/interface/wireguard/peers"] = [{ ".id": "*p", interface: "*generic", "public-key": "public", "preshared-key": "NEVER_IN_REVISION" }]
|
|
raw["/ip/address"] = [{ ".id": "*1", interface: "*generic", address: "10.1.0.1/30" }, { ".id": "*foreign", interface: "*w", address: "192.0.2.1/24" }]
|
|
const byId = await fetchWireguardRestoreState({ id: 1 } as Parameters<typeof fetchWireguardRestoreState>[0])
|
|
assert(calls.includes("/interface"))
|
|
assert.equal(byId.snapshot.interfaces[0].peers[0]?.presharedKeyConfigured, true)
|
|
assert.equal(JSON.stringify(byId.snapshot).includes("NEVER_IN_REVISION"), false)
|
|
assert.equal(byId.snapshot.interfaces[0].innerAddresses?.some(row => row.address === "10.1.0.1/30"), true)
|
|
assert.equal(byId.snapshot.interfaces[0].innerAddresses?.some(row => row.address === "192.0.2.1/24"), false)
|
|
raw["/interface"] = [{ ".id": "*generic", name: "wg-own", type: "ether" }]
|
|
await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters<typeof fetchWireguardRestoreState>[0]), /не подтверждена ссылка/)
|
|
raw["/interface"] = []
|
|
await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters<typeof fetchWireguardRestoreState>[0]), /не подтверждена ссылка/)
|
|
await putIpAddress({ put: async (path: string, body: Record<string, string>) => { assert.equal(path, "/ipv6/address"); assert.equal(body.advertise, "no") } } as unknown as MikrotikClient, "fd00::1/64", "wg-own")
|
|
MikrotikClient.fromServer = () => ({ get: async (path: string) => { if (path === "/ipv6/address") throw new Error("Unauthorized"); return [] } }) as unknown as MikrotikClient
|
|
await assert.rejects(fetchWireguardRestoreState({ id: 1 } as Parameters<typeof fetchWireguardRestoreState>[0]), /Unauthorized/)
|
|
} finally { MikrotikClient.fromServer = original }
|
|
const key = Buffer.alloc(32, 7).toString("base64")
|
|
const rsc = generateMikrotikRsc({ name: "wg-own", mtu: 1400, address: "fd00:152::1/64", privateKey: key, peers: [] })
|
|
assert(rsc.includes("/ipv6 address add"))
|
|
assert(rsc.includes("advertise=no"))
|
|
assert.equal(parseWireguardImport(rsc, "rsc").interface.address, "fd00:152::1/64")
|
|
assert.throws(() => parseWireguardImport(rsc.replace('interface="wg-own"', 'interface="foreign"'), "rsc"), /другому интерфейсу/)
|
|
assert.throws(() => parseWireguardImport(rsc + "\n/ipv6 address add address=fd00::2/64 interface=wg-own", "rsc"), /нескольких/)
|
|
assert.throws(() => parseWireguardImport(rsc.replace("advertise=no", "advertise=yes"), "rsc"), /специальных/)
|
|
for (const address of ["fd00::1", "fd00::1/129", "bad/64", "10.0.0.1/33"]) {
|
|
assert(!wgCreateInterfaceSchema.safeParse({ serverId: 1, name: "own", address }).success)
|
|
assert(!wgPatchInterfaceSchema.safeParse({ address }).success)
|
|
}
|
|
assert(wgCreateInterfaceSchema.safeParse({ serverId: 1, name: "own", address: "fd00::1/64" }).success)
|
|
assert(wgPatchInterfaceSchema.safeParse({ address: "" }).success)
|
|
console.log("WireGuard IPv6: dual-stack static snapshot, family restore, legacy conflict, failed read, native IPv6 PUT, RSC roundtrip and contracts passed")
|