zlh-api/src/scripts/testDnsSync.js

75 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ZeroLagHub DNS Sync Audit v2
* Compares DB, Technitium, and Cloudflare for divergence.
* Returns which hostnames are missing, duplicated, or orphaned.
*/
import prisma from "../services/prisma.js";
import * as technitium from "../services/technitiumClient.js";
import * as cloudflare from "../services/cloudflareClient.js";
async function testDnsSync() {
console.log("🔍 Running DNS sync test...");
// --- 1⃣ Get hostnames from DB ---
const dbInstances = await prisma.containerInstance.findMany({
select: { hostname: true },
});
const dbHostnames = dbInstances.map((i) => i.hostname);
console.log(`🗃️ DB hostnames: ${dbHostnames.length}`);
// --- 2⃣ Get Technitium ---
const techRecords = await technitium.listRecords();
const techHosts = new Set(
techRecords
.filter((r) => ["A", "SRV"].includes(r.type))
.map((r) =>
r.type === "SRV"
? r.name.replace(/^_minecraft\._tcp\./, "")
: r.name
)
);
console.log(`🧩 Technitium records: ${techHosts.size}`);
// --- 3⃣ Get Cloudflare ---
const cfRecords = await cloudflare.listAllRecords();
const cfHosts = new Set(
cfRecords
.filter((r) => ["A", "SRV"].includes(r.type))
.map((r) =>
r.type === "SRV"
? r.name.replace(/^_minecraft\._tcp\./, "")
: r.name
)
);
console.log(`☁️ Cloudflare records: ${cfHosts.size}`);
// --- 4⃣ Compute sets ---
const techOnly = [...techHosts].filter((h) => !dbHostnames.includes(h));
const cfOnly = [...cfHosts].filter((h) => !dbHostnames.includes(h));
const inBoth = [...techHosts].filter((h) => cfHosts.has(h));
const dbMissing = dbHostnames.filter(
(h) => !techHosts.has(h) && !cfHosts.has(h)
);
// --- 5⃣ Display results ---
console.log("\n🧾 ===== DNS Audit Summary =====");
console.log(`Technitium-only records (${techOnly.length}):`);
techOnly.forEach((h) => console.log(` - ${h}`));
console.log(`\nCloudflare-only records (${cfOnly.length}):`);
cfOnly.forEach((h) => console.log(` - ${h}`));
console.log(`\nIn both (${inBoth.length}):`);
inBoth.forEach((h) => console.log(` - ${h}`));
console.log(`\nMissing from both (${dbMissing.length}):`);
dbMissing.forEach((h) => console.log(` - ${h}`));
console.log("\n✅ Done.\n");
}
testDnsSync()
.catch((err) => console.error("❌ DNS sync test failed:", err))
.finally(async () => await prisma.$disconnect());