Watch
1
0
Fork
You've already forked cloudflare-gateway-pihole-scripts
0
mirror of https://github.com/mrrfv/cloudflare-gateway-pihole-scripts.git synced 2026-11-16 19:37:01 +00:00
cloudflare-gateway-pihole-s.../cf_defragment.js
Ben Sykes c5c1d7b4a1 Add script 'cloudflare-defragment'
Which consolidates domains into fewer lists, deleting any empty lists.
Domains are sorted by the time they were first added to one of our lists. This is done
so that more stable entries end up in the earlier lists and domains that come and go are in the later lists.
This enables the patching to be more efficient, touching only the later lists.
2025-11-11 07:58:00 -08:00

42 lines
1.6 KiB
JavaScript

import {
defragmentZeroTrustLists,
upsertZeroTrustDNSRule,
upsertZeroTrustSNIRule,
deleteZeroTrustListsOneByOne
} from "./lib/api.js";
import { BLOCK_BASED_ON_SNI } from "./lib/constants.js";
import { notifyWebhook } from "./lib/utils.js";
// Defragment the lists and rewrite the rules
const { emptyLists, nonEmptyLists, stats } = await defragmentZeroTrustLists();
// If we don't have any empty lists, there's no change to the rules
if (emptyLists.length > 0) {
console.log('Updating rules...');
// We have any empty lists, first rewrite the rule(s) using the non-empty lists
await upsertZeroTrustDNSRule(nonEmptyLists, "CGPS Filter Lists");
// Optionally create a rule that matches the SNI.
// This only works for users who proxy their traffic through Cloudflare.
if (BLOCK_BASED_ON_SNI) {
await upsertZeroTrustSNIRule(nonEmptyLists, "CGPS Filter Lists - SNI Based Filtering");
}
// Now the lists are no longer referenced, we can delete them
console.log('Deleting empty lists...');
await deleteZeroTrustListsOneByOne(emptyLists);
}
// Print a summary of what we did
console.log(`Defragmented ${stats.chunks} lists into ${stats.assignedLists} lists`);
console.log(`Patches made to ${stats.patches} lists, moving ${stats.entriesToMove} entries`);
// Continue summary if we deletes lists or rewrote rules
if (emptyLists.length > 0) {
console.log(`Updated rules using ${stats.nonEmptyLists} lists`);
console.log(`Deleted ${stats.emptyLists} empty lists`);
}
// Send a notification to the webhook
await notifyWebhook("CF Defragment script finished running");