From 8e96c87c27a9d70894905fe03337f6af17037ab1 Mon Sep 17 00:00:00 2001 From: Viet Huynh Date: Sat, 9 Sep 2023 23:12:22 +0700 Subject: [PATCH] refactored rule creation --- cf_gateway_rule_create.js | 62 ++------------------------------------- lib/api.js | 19 ++++++++++++ lib/helpers.js | 6 +++- 3 files changed, 27 insertions(+), 60 deletions(-) diff --git a/cf_gateway_rule_create.js b/cf_gateway_rule_create.js index 26e239c..d29ba2f 100644 --- a/cf_gateway_rule_create.js +++ b/cf_gateway_rule_create.js @@ -1,34 +1,7 @@ -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, - }, - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - return data.result; -} +import { createZeroTrustRule, getZeroTrustLists } from './lib/api.js'; ;(async() => { - const lists = await getZeroTrustLists(); + const { result: lists } = await getZeroTrustLists(); const filtered_lists = lists.filter(list => list.name.startsWith('CGPS List')); let wirefilter_expression = ''; @@ -44,34 +17,5 @@ async function getZeroTrustLists() { wirefilter_expression = wirefilter_expression.trim().replace('\n', ''); if (!process.env.CI) console.log(`Firewall expression contains ${wirefilter_expression.length} characters, and checks against ${filtered_lists.length} filter lists.`) - const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/rules`; - - const response = await fetch(url, { - method: 'POST', - headers: { - 'Authorization': `Bearer ${API_TOKEN}`, - 'Content-Type': 'application/json', - 'X-Auth-Email': ACCOUNT_EMAIL, - 'X-Auth-Key': API_TOKEN, - }, - 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": wirefilter_expression, - }), - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - console.log('Success:', data.success); + await createZeroTrustRule(wirefilter_expression); })(); - -async function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} \ No newline at end of file diff --git a/lib/api.js b/lib/api.js index e2b0dda..91d30aa 100644 --- a/lib/api.js +++ b/lib/api.js @@ -126,3 +126,22 @@ export const deleteZeroTrustListsAtOnce = async (lists) => { console.error(`Error occurred while deleting lists - ${err.toString()}`); } }; + +/** + * Creates a Zero Trust rule. + * @param {string} wirefilterExpression The expression to be used for the rule. + * @returns {Promise} + */ +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, + }), + }); diff --git a/lib/helpers.js b/lib/helpers.js index 8fd511a..bc724a1 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -21,7 +21,11 @@ const request = async (url, options) => { throw new Error(`HTTP error! Status: ${response.status}`); } - return response.json(); + const data = response.json(); + + console.log(data.success); + + return data; }; /**