mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 05:31:50 +00:00
updated logging for API calls
This commit is contained in:
@@ -7,5 +7,6 @@ const wirefilterExpression = lists.reduce((previous, current) => {
|
||||
return `${previous} any(dns.domains[*] in \$${current.id}) or `;
|
||||
}, "");
|
||||
|
||||
console.log("Creating rule...");
|
||||
// Remove the trailing ' or '
|
||||
await createZeroTrustRule(wirefilterExpression.slice(0, -4));
|
||||
|
||||
@@ -11,6 +11,6 @@ const cgpsRule = rules.find(({ name }) => name === "CGPS Filter Lists");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Deleting rule ${cgpsRule.name}`);
|
||||
console.log(`Deleting rule ${cgpsRule.name}...`);
|
||||
await deleteZeroTrustRule(cgpsRule.id);
|
||||
})();
|
||||
|
||||
+7
-3
@@ -118,15 +118,15 @@ await readFile(resolve(`./${blocklistFilename}`), (line, rl) => {
|
||||
}
|
||||
});
|
||||
|
||||
const numberOfLists = Math.ceil(domains.length / LIST_ITEM_SIZE);
|
||||
|
||||
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 lists to be created: ${Math.ceil(domains.length / LIST_ITEM_SIZE)}`
|
||||
);
|
||||
console.log(`Number of lists to be created: ${numberOfLists}`);
|
||||
console.log("\n\n");
|
||||
|
||||
(async () => {
|
||||
@@ -137,6 +137,10 @@ console.log("\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Creating ${numberOfLists} lists for ${domains.length} domains...`
|
||||
);
|
||||
|
||||
if (FAST_MODE) {
|
||||
await createZeroTrustListsAtOnce(domains);
|
||||
return;
|
||||
|
||||
@@ -28,6 +28,8 @@ import { FAST_MODE } from "./lib/constants.js";
|
||||
`Got ${lists.length} lists, ${cgpsLists.length} of which are CGPS lists that will be deleted.`
|
||||
);
|
||||
|
||||
console.log(`Deleting ${cgpsLists.length} lists...`);
|
||||
|
||||
if (FAST_MODE) {
|
||||
await deleteZeroTrustListsAtOnce(cgpsLists);
|
||||
return;
|
||||
|
||||
+33
-20
@@ -62,7 +62,6 @@ export const createZeroTrustListsOneByOne = async (items) => {
|
||||
* @param {string[]} items The domains.
|
||||
*/
|
||||
export const createZeroTrustListsAtOnce = async (items) => {
|
||||
const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
|
||||
const requests = [];
|
||||
|
||||
for (let i = 0, listNumber = 1; i < items.length; i += LIST_ITEM_SIZE) {
|
||||
@@ -77,7 +76,7 @@ export const createZeroTrustListsAtOnce = async (items) => {
|
||||
|
||||
try {
|
||||
await Promise.all(requests);
|
||||
console.log(`Created ${totalListNumber} lists`);
|
||||
console.log("Created lists successfully");
|
||||
} catch (err) {
|
||||
console.error(`Error occurred while creating lists - ${err.toString()}`);
|
||||
}
|
||||
@@ -125,7 +124,7 @@ export const deleteZeroTrustListsAtOnce = async (lists) => {
|
||||
|
||||
try {
|
||||
await Promise.all(requests);
|
||||
console.log(`Deleted ${lists.length} lists`);
|
||||
console.log("Deleted lists successfully");
|
||||
} catch (err) {
|
||||
console.error(`Error occurred while deleting lists - ${err.toString()}`);
|
||||
}
|
||||
@@ -147,19 +146,26 @@ export const getZeroTrustRules = () =>
|
||||
* @param {string} wirefilterExpression The expression to be used for the rule.
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
export const createZeroTrustRule = (wirefilterExpression) =>
|
||||
requestGateway("/rules", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: "CGPS Filter Lists",
|
||||
description:
|
||||
"Filter lists created by Cloudflare Gateway Pi-hole Scripts. Avoid editing this rule. Changing the name of this rule will break the script.",
|
||||
enabled: true,
|
||||
action: "block",
|
||||
filters: ["dns"],
|
||||
traffic: wirefilterExpression,
|
||||
}),
|
||||
});
|
||||
export const createZeroTrustRule = async (wirefilterExpression) => {
|
||||
try {
|
||||
await requestGateway("/rules", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: "CGPS Filter Lists",
|
||||
description:
|
||||
"Filter lists created by Cloudflare Gateway Pi-hole Scripts. Avoid editing this rule. Changing the name of this rule will break the script.",
|
||||
enabled: true,
|
||||
action: "block",
|
||||
filters: ["dns"],
|
||||
traffic: wirefilterExpression,
|
||||
}),
|
||||
});
|
||||
|
||||
console.log("Created rule successfully");
|
||||
} catch (err) {
|
||||
console.error(`Error occurred while creating rule - ${err.toString()}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes a Zero Trust rule.
|
||||
@@ -168,7 +174,14 @@ export const createZeroTrustRule = (wirefilterExpression) =>
|
||||
* @param {number} id The ID of the rule to be deleted.
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
export const deleteZeroTrustRule = (id) =>
|
||||
requestGateway(`/rules/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
export const deleteZeroTrustRule = async (id) => {
|
||||
try {
|
||||
await requestGateway(`/rules/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
console.log("Deleted rule successfully");
|
||||
} catch (err) {
|
||||
console.error(`Error occurred while deleting rule - ${err.toString()}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user