mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
refactored list deletion
This commit is contained in:
+9
-49
@@ -1,58 +1,18 @@
|
||||
import 'dotenv/config';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
const API_TOKEN = process.env.CLOUDFLARE_API_KEY;
|
||||
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
|
||||
const ACCOUNT_EMAIL = process.env.CLOUDFLARE_ACCOUNT_EMAIL;
|
||||
|
||||
// Function to read Cloudflare Zero Trust lists
|
||||
async function getZeroTrustLists() {
|
||||
const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/lists`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${API_TOKEN}`,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Auth-Email': ACCOUNT_EMAIL,
|
||||
'X-Auth-Key': API_TOKEN,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
return data.result;
|
||||
}
|
||||
import { deleteZeroTrustListsAtOnce, deleteZeroTrustListsOneByOne, getZeroTrustLists } from "./lib/api.js";
|
||||
import { FAST_MODE } from "./lib/constants.js";
|
||||
|
||||
;(async() => {
|
||||
const lists = await getZeroTrustLists();
|
||||
const { result: lists } = await getZeroTrustLists();
|
||||
if (!lists) return console.warn("No file lists found - this is not an issue if it's your first time running this script. Exiting.");
|
||||
const cgps_lists = lists.filter(list => list.name.startsWith('CGPS List'));
|
||||
if (!cgps_lists.length) return console.warn("No lists with matching name found - this is not an issue if you haven't created any filter lists before. Exiting.");
|
||||
|
||||
if (!process.env.CI) console.log(`Got ${lists.length} lists, ${cgps_lists.length} of which are CGPS lists that will be deleted.`);
|
||||
|
||||
let lists_processed = 0;
|
||||
for (const list of cgps_lists) {
|
||||
console.log(`Deleting list`, process.env.CI ? "(info redacted, running in CI)" : `${list.name} with ID ${list.id}, ${cgps_lists.length - lists_processed - 1} left`);
|
||||
|
||||
const resp = await fetch(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/lists/${list.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${API_TOKEN}`,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Auth-Email': ACCOUNT_EMAIL,
|
||||
'X-Auth-Key': API_TOKEN,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await resp.json();
|
||||
console.log('Success:', data.success);
|
||||
lists_processed++;
|
||||
|
||||
await sleep(350); // Cloudflare API rate limit is 1200 requests per 5 minutes, so we sleep for 350ms to be safe
|
||||
}
|
||||
})();
|
||||
if (FAST_MODE) {
|
||||
await deleteZeroTrustListsAtOnce(cgps_lists);
|
||||
return;
|
||||
}
|
||||
|
||||
async function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
await deleteZeroTrustListsOneByOne(cgps_lists);
|
||||
})();
|
||||
|
||||
+56
@@ -1,5 +1,15 @@
|
||||
import { LIST_ITEM_SIZE } from "./constants.js";
|
||||
import { requestGateway } from "./helpers.js";
|
||||
import { sleep } from "./utils.js";
|
||||
|
||||
/**
|
||||
* Gets Zero Trust lists.
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
export const getZeroTrustLists = () =>
|
||||
requestGateway("/lists", {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a Zero Trust list.
|
||||
@@ -70,3 +80,49 @@ export const createZeroTrustListsAtOnce = async (items) => {
|
||||
console.error(`Error occurred while creating lists - ${err.toString()}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes a Zero Trust list.
|
||||
* @param {number} id The ID of the list.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
const deleteZeroTrustList = (id) =>
|
||||
requestGateway(`/lists/${id}`, { method: "DELETE" });
|
||||
|
||||
/**
|
||||
* Deletes Zero Trust lists sequentially.
|
||||
* @param {Object[]} lists The lists to be deleted.
|
||||
* @param {number} lists[].id The ID of a list.
|
||||
* @param {string} lists[].name The name of a list.
|
||||
*/
|
||||
export const deleteZeroTrustListsOneByOne = async (lists) => {
|
||||
let totalListNumber = lists.length;
|
||||
|
||||
for (const { id, name } of lists) {
|
||||
try {
|
||||
await deleteZeroTrustList(id);
|
||||
await sleep();
|
||||
totalListNumber--;
|
||||
console.log(`Deleted ${name} list - ${totalListNumber} left`);
|
||||
} catch (err) {
|
||||
console.error(`Could not delete ${name} - ${err.toString()}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes all Zero Trust lists at once.
|
||||
* @param {Object[]} lists The lists to be deleted.
|
||||
* @param {number} lists[].id The ID of a list.
|
||||
* @param {string} lists[].name The name of a list.
|
||||
*/
|
||||
export const deleteZeroTrustListsAtOnce = async (lists) => {
|
||||
const requests = lists.map(({ id }) => deleteZeroTrustList(id));
|
||||
|
||||
try {
|
||||
await Promise.all(requests);
|
||||
console.log(`Deleted ${lists.length} lists`);
|
||||
} catch (err) {
|
||||
console.error(`Error occurred while deleting lists - ${err.toString()}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user