From 3cd7168bc0e8b9eda139ed8fb12c863ee816eb25 Mon Sep 17 00:00:00 2001 From: andrewstech Date: Wed, 16 Oct 2024 09:57:27 +0000 Subject: [PATCH] New CI --- dnsconfig.js | 6 +-- zone.js | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 zone.js diff --git a/dnsconfig.js b/dnsconfig.js index 9b52705af..00d2d5c8e 100644 --- a/dnsconfig.js +++ b/dnsconfig.js @@ -8,7 +8,7 @@ var proxy = { // Function to validate filenames according to the given rules function isValidFilename(filename) { - var regex = /^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+(-[a-z0-9]+)*)*\.json$/; + var regex = /\.json$/; return regex.test(filename); } @@ -20,7 +20,7 @@ function getDomainsList(filesPath) { var basename = files[i].split('/').reverse()[0]; if (!isValidFilename(basename)) { - console.log(`Skipping invalid file: ${basename}`); + console.log("Skipping invalid file:" + basename); continue; } @@ -66,7 +66,7 @@ for (var idx in domains) { // Handle CNAME record if (domainData.record.CNAME) { - commit[domainData.record.domain].push( + commit[domainData.domain].push( CNAME(domainData.subdomain, domainData.record.CNAME + ".", proxyState) ); } diff --git a/zone.js b/zone.js new file mode 100644 index 000000000..fb49a395b --- /dev/null +++ b/zone.js @@ -0,0 +1,129 @@ +const fs = require('fs'); // Required for writing JSON to file + +var proxy = { + on: { "cloudflare_proxy": "on" }, + off: { "cloudflare_proxy": "off" } +}; + +// Function to validate filenames according to the given rules +function isValidFilename(filename) { + var regex = /^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+(-[a-z0-9]+)*)*\.json$/; + return regex.test(filename); +} + +// Function to get the domains list from the domains folder +function getDomainsList(filesPath) { + var result = []; + var files = glob.apply(null, [filesPath, true, '.json']); + + for (var i = 0; i < files.length; i++) { + var basename = files[i].split('/').reverse()[0]; + + if (!isValidFilename(basename)) { + console.log(`Skipping invalid file: ${basename}`); + continue; + } + + var name = basename.split('.')[0]; + result.push({ name: name, data: require(files[i]) }); + } + + return result; +} + +var domains = getDomainsList('./domains'); +var zoneFile = {}; // JSON object to hold all the DNS records + +for (var idx in domains) { + var domainData = domains[idx].data; + var proxyState = proxy.on; // enabled by default + + // Initialize the domain entry in the zone file if not already there + if (!zoneFile[domainData.record.domain]) { + zoneFile[domainData.record.domain] = []; + } + + // Check if proxy should be turned off + if (domainData.record.proxied === false) { + proxyState = proxy.off; + } + + // Handle CNAME record + if (domainData.record.CNAME) { + zoneFile[domainData.record.domain].push({ + type: "CNAME", + subdomain: domainData.subdomain, + value: domainData.record.CNAME + ".", + proxy: proxyState + }); + } + + // Handle A records + if (domainData.record.A) { + for (var a in domainData.record.A) { + zoneFile[domainData.record.domain].push({ + type: "A", + subdomain: domainData.subdomain, + value: domainData.record.A[a], + proxy: proxyState + }); + } + } + + // Handle AAAA records + if (domainData.record.AAAA) { + for (var aaaa in domainData.record.AAAA) { + zoneFile[domainData.record.domain].push({ + type: "AAAA", + subdomain: domainData.subdomain, + value: domainData.record.AAAA[aaaa], + proxy: proxyState + }); + } + } + + // Handle MX records + if (domainData.record.MX) { + for (var mx in domainData.record.MX) { + zoneFile[domainData.record.domain].push({ + type: "MX", + subdomain: domainData.subdomain, + value: domainData.record.MX[mx] + ".", + priority: 10 + }); + } + } + + // Handle URL records (redirect) + if (domainData.record.URL) { + zoneFile[domainData.record.domain].push({ + type: "URL", + subdomain: domainData.subdomain, + value: domainData.record.URL + }); + } + + // Handle TXT records + if (domainData.record.TXT) { + if (Array.isArray(domainData.record.TXT)) { + for (var txt in domainData.record.TXT) { + zoneFile[domainData.record.domain].push({ + type: "TXT", + subdomain: domainData.subdomain, + value: domainData.record.TXT[txt] + }); + } + } else { + zoneFile[domainData.record.domain].push({ + type: "TXT", + subdomain: domainData.subdomain, + value: domainData.record.TXT + }); + } + } +} + +// Write the JSON zone file to disk +fs.writeFileSync('zoneFile.json', JSON.stringify(zoneFile, null, 2), 'utf-8'); + +console.log("Zone file has been written to zoneFile.json");