updated logging for API calls

This commit is contained in:
Viet Huynh
2023-11-02 02:42:28 +07:00
parent 6f1ba625c0
commit ac535c927a
5 changed files with 44 additions and 24 deletions
+1
View File
@@ -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));
+1 -1
View File
@@ -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
View File
@@ -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;
+2
View File
@@ -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
View File
@@ -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()}`);
}
};