From 70a2dfa89feebe1f08246f58c87692d11d888884 Mon Sep 17 00:00:00 2001 From: Viet Huynh Date: Sun, 19 Nov 2023 17:46:39 +0700 Subject: [PATCH] refactored domain processing code to improve performance and memory usage --- cf_list_create.js | 60 ++++++++++++++++++++--------------------------- lib/utils.js | 18 +++++++------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/cf_list_create.js b/cf_list_create.js index 68670cb..cfecb22 100644 --- a/cf_list_create.js +++ b/cf_list_create.js @@ -73,40 +73,31 @@ await readFile(resolve(`./${blocklistFilename}`), (line, rl) => { processedDomainCount++; - // Get all the levels of the domain and check from the highest - // because we are blocking all subdomains - // Example: fourth.third.example.com => ["example.com", "third.example.com", "fourth.third.example.com"] - const anyDomainExists = extractDomain(domain) - .reverse() - .some((item) => { - if (blocklist.has(item)) { - if (item === domain) { - // The exact domain is already blocked - console.log(`Found ${item} in blocklist already - Skipping`); - duplicateDomainCount++; - } else { - // The higher-level domain is already blocked - // so it's not necessary to block this domain - console.log( - `Found ${item} in blocklist already - Skipping ${domain}` - ); - unnecessaryDomainCount++; - } - - return true; - } - - return false; - }); - - if (anyDomainExists) return; - if (allowlist.has(domain)) { console.log(`Found ${domain} in allowlist - Skipping`); allowedDomainCount++; return; } + if (blocklist.has(domain)) { + console.log(`Found ${domain} in blocklist already - Skipping`); + duplicateDomainCount++; + return; + } + + // Get all the levels of the domain and check from the highest + // because we are blocking all subdomains + // Example: fourth.third.example.com => ["example.com", "third.example.com", "fourth.third.example.com"] + for (const item of extractDomain(domain).slice(1)) { + if (!blocklist.has(item)) continue; + + // The higher-level domain is already blocked + // so it's not necessary to block this domain + console.log(`Found ${item} in blocklist already - Skipping ${domain}`); + unnecessaryDomainCount++; + return; + } + blocklist.set(domain, 1); domains.push(domain); @@ -124,8 +115,8 @@ console.log("\n\n"); console.log(`Number of processed domains: ${processedDomainCount}`); console.log(`Number of duplicate domains: ${duplicateDomainCount}`); console.log(`Number of unnecessary domains: ${unnecessaryDomainCount}`); -console.log(`Number of blocked domains: ${domains.length}`); console.log(`Number of allowed domains: ${allowedDomainCount}`); +console.log(`Number of blocked domains: ${domains.length}`); console.log(`Number of lists to be created: ${numberOfLists}`); console.log("\n\n"); @@ -143,12 +134,11 @@ console.log("\n\n"); if (FAST_MODE) { await createZeroTrustListsAtOnce(domains); - // TODO: make this less repetitive - await notifyWebhook(`CF List Create script finished running (${domains.length} domains, ${numberOfLists} lists)`); - return; + } else { + await createZeroTrustListsOneByOne(domains); } - await createZeroTrustListsOneByOne(domains); - - await notifyWebhook(`CF List Create script finished running (${domains.length} domains, ${numberOfLists} lists)`); + await notifyWebhook( + `CF List Create script finished running (${domains.length} domains, ${numberOfLists} lists)` + ); })(); diff --git a/lib/utils.js b/lib/utils.js index 1c60b57..a263a2f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -19,18 +19,18 @@ export const isValidDomain = (value) => * @param {string} domain The domain to be extracted. * @returns {string[]} */ -export const extractDomain = (domain) => - domain.split(".").reduce((previous, current, index, array) => { - const nextIndex = index + 1; +export const extractDomain = (domain) => { + const parts = domain.split("."); + const extractedDomains = []; - if (nextIndex > array.length - 1) return previous; + for (let i = 0; i < parts.length; i++) { + const subdomains = parts.slice(i).join("."); - const domain = [current, ...array.slice(nextIndex)].join("."); + extractedDomains.unshift(subdomains); + } - previous.push(domain); - - return previous; - }, []); + return extractedDomains; +}; /** * Checks if the value is a comment.