70 lines
5.3 KiB
TypeScript
70 lines
5.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { readFile, writeFile } from "node:fs/promises"
|
|
import { setTimeout as delay } from "node:timers/promises"
|
|
import { MikrotikClient } from "../../services/mikrotik.js"
|
|
import { configurationRows, resourceById } from "./resources.js"
|
|
import { fingerprint } from "./security.js"
|
|
|
|
assert.equal(process.env.MMAPP_TEST_CHR_SOURCE_DIAGNOSTIC, "true")
|
|
assert.ok(process.stdin.isTTY, "Private terminal required")
|
|
const original = JSON.parse(await readFile(new URL("../../../../docs/mcp-chr-ospf-source-interrupted.json", import.meta.url), "utf8"))
|
|
assert.equal(original.marker, "mcp-test-c74d0d3c"); assert.equal(original.complete, false); assert.ok(original.finishedAt)
|
|
const baselines = original.checks.find((row: { overlayPreflight?: boolean }) => row.overlayPreflight).baselineFingerprints as { serverId: number; resource: string; fingerprint: string }[]
|
|
assert.equal(baselines.length, 14)
|
|
process.stdout.write("CHR password (hidden): ")
|
|
let secret = await new Promise<string>((resolve, reject) => {
|
|
let value = ""
|
|
const finish = (error?: Error) => { clearTimeout(timer); process.stdin.off("data", read); process.stdin.setRawMode(false); process.stdin.pause(); process.stdout.write("\n"); if (error) reject(error); else resolve(value) }
|
|
const read = (chunk: Buffer) => { for (const c of chunk.toString()) { if (c === "\r" || c === "\n") { finish(); return } if (c === "\u0003") { finish(new Error("Cancelled")); return } if (c === "\b" || c === "\u007f") value = value.slice(0, -1); else value += c } }
|
|
const timer = setTimeout(() => finish(new Error("Password timeout")), 60000)
|
|
process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.on("data", read)
|
|
})
|
|
const checks: Record<string, unknown>[] = [], report = { startedAt: new Date().toISOString(), originalMarker: original.marker, deviceMutations: 0, complete: false, allExact: false, checks }
|
|
try {
|
|
for (const serverId of [1, 2]) {
|
|
const client = new MikrotikClient({ host: "127.0.0.1", port: serverId === 1 ? 19081 : 19082, useSsl: false, verifySsl: true, username: "admin", password: secret })
|
|
const system = await client.get<Record<string, unknown>>("/system/resource")
|
|
checks.push({ serverId, phase: "system", version: system.version, uptime: system.uptime })
|
|
for (const baseline of baselines.filter(row => row.serverId === serverId)) {
|
|
await delay(500)
|
|
const spec = resourceById(baseline.resource), rows = configurationRows(spec, await client.get(spec.path))
|
|
const hash = fingerprint(rows), ownRemaining = rows.some(row => JSON.stringify(row).includes(original.marker))
|
|
const check: Record<string, unknown> = { serverId, resource: baseline.resource, expected: baseline.fingerprint, actual: hash, exact: hash === baseline.fingerprint, ownRemaining, rows: rows.length }
|
|
if (!check.exact) {
|
|
// Diagnose one generated native ID without changing any router value.
|
|
const dynamic = rows.flatMap((row, index) => [true, "true", "yes"].includes(row.dynamic as string | boolean) ? [index] : [])
|
|
if (dynamic.length === 1) {
|
|
const index = dynamic[0]
|
|
for (let n = 0; n < 8192; n++) {
|
|
const oldId = "*" + n.toString(16).toUpperCase(), candidate = rows.map((row, i) => i === index ? { ...row, ".id": oldId } : row).sort((a, b) => String(a[".id"]).localeCompare(String(b[".id"])))
|
|
if (fingerprint(candidate) === baseline.fingerprint) { check.onlyGeneratedIdChanged = { previous: oldId, current: rows[index][".id"] }; break }
|
|
}
|
|
}
|
|
if (!check.onlyGeneratedIdChanged && dynamic.length > 0 && dynamic.length <= 10 && dynamic.every(index => /^\*[a-f0-9]{1,16}$/i.test(String(rows[index][".id"])))) {
|
|
const indices = new Set(dynamic)
|
|
for (let offset = -8192; offset <= 8192; offset++) {
|
|
const candidate = rows.map((row, index) => {
|
|
if (!indices.has(index)) return row
|
|
const value = BigInt("0x" + String(row[".id"]).slice(1)) + BigInt(offset)
|
|
return { ...row, ".id": value >= 0n ? "*" + value.toString(16).toUpperCase() : "invalid" }
|
|
}).sort((a, b) => String(a[".id"]).localeCompare(String(b[".id"])))
|
|
if (fingerprint(candidate) === baseline.fingerprint) {
|
|
check.onlyGeneratedIdsChanged = { commonOffset: offset, count: dynamic.length }
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if (baseline.resource.endsWith("_route")) check.routeIdentities = rows.map(row => ({ id: row[".id"], dynamic: row.dynamic, table: row["routing-table"] }))
|
|
if (baseline.resource === "ipv4_address") check.safeAddressRows = rows.map(row => ({ id: row[".id"], address: row.address, interface: row.interface, disabled: row.disabled, dynamic: row.dynamic, hasComment: row.comment !== undefined }))
|
|
}
|
|
checks.push(check)
|
|
await writeFile(new URL("../../../../docs/mcp-chr-ospf-source-baseline-diagnostic.json", import.meta.url), JSON.stringify(report, null, 2) + "\n")
|
|
}
|
|
}
|
|
report.allExact = checks.filter(row => row.resource).every(row => row.exact)
|
|
report.complete = true
|
|
} finally {
|
|
secret = ""
|
|
await writeFile(new URL("../../../../docs/mcp-chr-ospf-source-baseline-diagnostic.json", import.meta.url), JSON.stringify({ ...report, finishedAt: new Date().toISOString() }, null, 2) + "\n")
|
|
}
|