mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
refactored rule deletion
This commit is contained in:
@@ -1,58 +1,12 @@
|
|||||||
import 'dotenv/config';
|
import { deleteZeroTrustRule, getZeroTrustRules } from './lib/api.js';
|
||||||
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 rules
|
|
||||||
async function getZeroTrustRules() {
|
|
||||||
const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/rules`;
|
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.result;
|
|
||||||
}
|
|
||||||
|
|
||||||
;(async() => {
|
;(async() => {
|
||||||
const rules = await getZeroTrustRules();
|
const { result: rules } = await getZeroTrustRules();
|
||||||
const [filtered_rule] = rules.filter(rule => rule.name === "CGPS Filter Lists");
|
const [filtered_rule] = rules.filter(rule => rule.name === "CGPS Filter Lists");
|
||||||
|
|
||||||
if (!filtered_rule) return console.warn("No rule with matching name found - this is not an issue if you haven't run the create script yet. Exiting.");
|
if (!filtered_rule) return console.warn("No rule with matching name found - this is not an issue if you haven't run the create script yet. Exiting.");
|
||||||
|
|
||||||
console.log(`Deleting rule`, process.env.CI ? "(redacted, running in CI)" : `${filtered_rule.name} with ID ${filtered_rule.id}`);
|
console.log(`Deleting rule`, process.env.CI ? "(redacted, running in CI)" : `${filtered_rule.name} with ID ${filtered_rule.id}`);
|
||||||
|
|
||||||
const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/rules/${filtered_rule.id}`;
|
await deleteZeroTrustRule(filtered_rule.id);
|
||||||
|
|
||||||
const resp = await fetch(url, {
|
|
||||||
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);
|
|
||||||
await sleep(350); // Cloudflare API rate limit is 1200 requests per 5 minutes, so we sleep for 350ms to be safe
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
async function sleep(ms) {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
}
|
|
||||||
+18
-1
@@ -127,10 +127,17 @@ export const deleteZeroTrustListsAtOnce = async (lists) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets Zero Trust rules.
|
||||||
|
* @returns {Promise<Object>}
|
||||||
|
*/
|
||||||
|
export const getZeroTrustRules = () =>
|
||||||
|
requestGateway("/rules", { method: "GET" });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Zero Trust rule.
|
* Creates a Zero Trust rule.
|
||||||
* @param {string} wirefilterExpression The expression to be used for the rule.
|
* @param {string} wirefilterExpression The expression to be used for the rule.
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<Object>}
|
||||||
*/
|
*/
|
||||||
export const createZeroTrustRule = (wirefilterExpression) =>
|
export const createZeroTrustRule = (wirefilterExpression) =>
|
||||||
requestGateway("/rules", {
|
requestGateway("/rules", {
|
||||||
@@ -145,3 +152,13 @@ export const createZeroTrustRule = (wirefilterExpression) =>
|
|||||||
traffic: wirefilterExpression,
|
traffic: wirefilterExpression,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a Zero Trust rule.
|
||||||
|
* @param {number} id The ID of the rule to be deleted.
|
||||||
|
* @returns {Promise<Object>}
|
||||||
|
*/
|
||||||
|
export const deleteZeroTrustRule = (id) =>
|
||||||
|
requestGateway(`/rules/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user