Skip to content

Commit c85eca4

Browse files
authored
Merge pull request #141 from vietthedev/upstream
Refactor fetch with retry and rate limiting handler, remove `FAST_MODE`
2 parents 23b0f07 + 6b1a48e commit c85eca4

12 files changed

+124
-171
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ CLOUDFLARE_API_TOKEN=
22
CLOUDFLARE_ACCOUNT_ID=
33
CLOUDFLARE_LIST_ITEM_LIMIT=300000
44
DRY_RUN=0
5-
FAST_MODE=0
65
BLOCK_PAGE_ENABLED=0
76

87
# Experimental SNI based filtering. Set to 1 to enable.

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Please note that the GitHub Action downloads the recommended blocklists and whit
5858
- `PING_URL`: /Optional/ The HTTP(S) URL to ping (using curl) after the GitHub Action has successfully updated your filters. Useful for monitoring.
5959
- `DISCORD_WEBHOOK_URL`: /Optional/ The Discord (or similar) webhook URL to send notifications to. Good for monitoring as well.
6060
3. Create the following GitHub Actions variables in your repository settings if you desire:
61-
- `FAST_MODE`: Enable the scripts to send the requests simultaneously. Beware that there's a rate limit of 1200 requests per five minutes (<https://developers.cloudflare.com/fundamentals/api/reference/limits/>) so make sure you know what you are doing.
6261
- `ALLOWLIST_URLS`: Uses your own allowlists. One URL per line. Recommended allowlists will be used if this variable is not provided.
6362
- `BLOCKLIST_URLS`: Uses your own blocklists. One URL per line. Recommended blocklists will be used if this variable is not provided.
6463
- `BLOCK_PAGE_ENABLED`: Enable showing block page if host is blocked.

auto_update_github_action.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ jobs:
5050
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
5151
CLOUDFLARE_LIST_ITEM_LIMIT: ${{ secrets.CLOUDFLARE_LIST_ITEM_LIMIT }}
5252
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
53-
FAST_MODE: ${{ vars.FAST_MODE }}
5453

5554
- name: Create new rules and lists
5655
run: npm run cloudflare-create
@@ -60,7 +59,6 @@ jobs:
6059
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
6160
CLOUDFLARE_LIST_ITEM_LIMIT: ${{ secrets.CLOUDFLARE_LIST_ITEM_LIMIT }}
6261
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
63-
FAST_MODE: ${{ vars.FAST_MODE }}
6462

6563
- name: Send ping request
6664
if: env.PING_URL != ''

cf_gateway_rule_create.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getZeroTrustLists, upsertZeroTrustRule } from "./lib/api.js";
22
import { BLOCK_BASED_ON_SNI } from "./lib/constants.js";
3-
import { notifyWebhook } from "./lib/helpers.js";
3+
import { notifyWebhook } from "./lib/utils.js";
44

55
const { result: lists } = await getZeroTrustLists();
66

@@ -20,10 +20,10 @@ await upsertZeroTrustRule(wirefilterDNSExpression.slice(0, -4), "CGPS Filter Lis
2020
if (BLOCK_BASED_ON_SNI) {
2121
const wirefilterSNIExpression = lists.reduce((previous, current) => {
2222
if (!current.name.startsWith("CGPS List")) return previous;
23-
23+
2424
return `${previous} any(net.sni.domains[*] in \$${current.id}) or `;
2525
}, "");
26-
26+
2727
console.log("Creating SNI rule...");
2828
// .slice removes the trailing ' or '
2929
await upsertZeroTrustRule(wirefilterSNIExpression.slice(0, -4), "CGPS Filter Lists - SNI Based Filtering", ["l4"]);

cf_gateway_rule_delete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { deleteZeroTrustRule, getZeroTrustRules } from "./lib/api.js";
2-
import { notifyWebhook } from "./lib/helpers.js";
2+
import { notifyWebhook } from "./lib/utils.js";
33

44
const { result: rules } = await getZeroTrustRules();
55
const cgpsRules = rules.filter(({ name }) => name.startsWith("CGPS Filter Lists"));

cf_list_create.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import { existsSync } from "node:fs";
22
import { resolve } from "node:path";
33

4-
import {
5-
createZeroTrustListsAtOnce,
6-
createZeroTrustListsOneByOne,
7-
} from "./lib/api.js";
4+
import { createZeroTrustListsOneByOne } from "./lib/api.js";
85
import {
96
DEBUG,
107
DRY_RUN,
11-
FAST_MODE,
128
LIST_ITEM_LIMIT,
139
LIST_ITEM_SIZE,
1410
PROCESSING_FILENAME,
1511
} from "./lib/constants.js";
16-
import { normalizeDomain, notifyWebhook } from "./lib/helpers.js";
12+
import { normalizeDomain } from "./lib/helpers.js";
1713
import {
1814
extractDomain,
1915
isComment,
2016
isValidDomain,
2117
memoize,
18+
notifyWebhook,
2219
readFile,
2320
} from "./lib/utils.js";
2421

@@ -135,12 +132,7 @@ console.log("\n\n");
135132
`Creating ${numberOfLists} lists for ${domains.length} domains...`
136133
);
137134

138-
if (FAST_MODE) {
139-
await createZeroTrustListsAtOnce(domains);
140-
} else {
141-
await createZeroTrustListsOneByOne(domains);
142-
}
143-
135+
await createZeroTrustListsOneByOne(domains);
144136
await notifyWebhook(
145137
`CF List Create script finished running (${domains.length} domains, ${numberOfLists} lists)`
146138
);

cf_list_delete.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
2-
deleteZeroTrustListsAtOnce,
32
deleteZeroTrustListsOneByOne,
43
getZeroTrustLists,
54
} from "./lib/api.js";
6-
import { FAST_MODE } from "./lib/constants.js";
7-
import { notifyWebhook } from "./lib/helpers.js";
5+
import { notifyWebhook } from "./lib/utils.js";
86

97
(async () => {
108
const { result: lists } = await getZeroTrustLists();
@@ -31,14 +29,6 @@ import { notifyWebhook } from "./lib/helpers.js";
3129

3230
console.log(`Deleting ${cgpsLists.length} lists...`);
3331

34-
if (FAST_MODE) {
35-
await deleteZeroTrustListsAtOnce(cgpsLists);
36-
// TODO: make this less repetitive
37-
await notifyWebhook(`CF List Delete script finished running (${cgpsLists.length} lists)`);
38-
return;
39-
}
40-
4132
await deleteZeroTrustListsOneByOne(cgpsLists);
42-
4333
await notifyWebhook(`CF List Delete script finished running (${cgpsLists.length} lists)`);
4434
})();

extended_guide.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ The Cloudflare list item limit is the maximum number of items (blocked domains)
4040

4141
Processes block/allow lists without actually adding/removing domains from Cloudflare. Avoid using this option unless you know what you are doing.
4242

43-
#### `FAST_MODE`
44-
45-
Sends requests much faster, but might cause rate limiting issues. Use with caution.
46-
4743
## Example usage
4844

4945
These commands should be run in a terminal.

lib/api.js

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const createZeroTrustListsOneByOne = async (items) => {
4848
await createZeroTrustList(listName, chunk);
4949
totalListNumber--;
5050
listNumber++;
51+
5152
console.log(`Created "${listName}" list - ${totalListNumber} left`);
5253
} catch (err) {
5354
console.error(`Could not create "${listName}" - ${err.toString()}`);
@@ -56,32 +57,6 @@ export const createZeroTrustListsOneByOne = async (items) => {
5657
}
5758
};
5859

59-
/**
60-
* Creates all Zero Trust lists at once.
61-
* @param {string[]} items The domains.
62-
*/
63-
export const createZeroTrustListsAtOnce = async (items) => {
64-
const requests = [];
65-
66-
for (let i = 0, listNumber = 1; i < items.length; i += LIST_ITEM_SIZE) {
67-
const chunk = items
68-
.slice(i, i + LIST_ITEM_SIZE)
69-
.map((item) => ({ value: item }));
70-
const listName = `CGPS List - Chunk ${listNumber}`;
71-
72-
requests.push(createZeroTrustList(listName, chunk));
73-
listNumber++;
74-
}
75-
76-
try {
77-
await Promise.all(requests);
78-
console.log("Created lists successfully");
79-
} catch (err) {
80-
console.error(`Error occurred while creating lists - ${err.toString()}`);
81-
throw err;
82-
}
83-
};
84-
8560
/**
8661
* Deletes a Zero Trust list.
8762
*
@@ -105,6 +80,7 @@ export const deleteZeroTrustListsOneByOne = async (lists) => {
10580
try {
10681
await deleteZeroTrustList(id);
10782
totalListNumber--;
83+
10884
console.log(`Deleted ${name} list - ${totalListNumber} left`);
10985
} catch (err) {
11086
console.error(`Could not delete ${name} - ${err.toString()}`);
@@ -113,24 +89,6 @@ export const deleteZeroTrustListsOneByOne = async (lists) => {
11389
}
11490
};
11591

116-
/**
117-
* Deletes all Zero Trust lists at once.
118-
* @param {Object[]} lists The lists to be deleted.
119-
* @param {number} lists[].id The ID of a list.
120-
* @param {string} lists[].name The name of a list.
121-
*/
122-
export const deleteZeroTrustListsAtOnce = async (lists) => {
123-
const requests = lists.map(({ id }) => deleteZeroTrustList(id));
124-
125-
try {
126-
await Promise.all(requests);
127-
console.log("Deleted lists successfully");
128-
} catch (err) {
129-
console.error(`Error occurred while deleting lists - ${err.toString()}`);
130-
throw err;
131-
}
132-
};
133-
13492
/**
13593
* Gets Zero Trust rules.
13694
*

lib/constants.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ export const API_HOST = "https://api.cloudflare.com/client/v4";
2626

2727
export const DRY_RUN = !!parseInt(process.env.DRY_RUN, 10);
2828

29-
export const FAST_MODE = !!parseInt(process.env.FAST_MODE, 10);
30-
3129
export const BLOCK_PAGE_ENABLED = !!parseInt(process.env.BLOCK_PAGE_ENABLED, 10);
3230

3331
export const BLOCK_BASED_ON_SNI = !!parseInt(process.env.BLOCK_BASED_ON_SNI, 10);
3432

3533
export const DEBUG = !!parseInt(process.env.DEBUG, 10);
3634

35+
export const CLOUDFLARE_RATE_LIMITING_COOLDOWN_TIME = 2 * 60 * 1000;
36+
export const RATE_LIMITING_HTTP_ERROR_CODE = 429;
37+
3738
export const PROCESSING_FILENAME = {
3839
ALLOWLIST: "allowlist.txt",
3940
BLOCKLIST: "blocklist.txt",
@@ -63,7 +64,7 @@ export const RECOMMENDED_ALLOWLIST_URLS = [
6364
// Torrent trackers
6465
"https://raw.githubusercontent.com/im-sm/Pi-hole-Torrent-Blocklist/main/all-torrent-trackres.txt",
6566
// Banks
66-
"https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/banks.txt",
67+
"https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/banks.txt",
6768
// Official Discord domains
6869
"https://raw.githubusercontent.com/Dogino/Discord-Phishing-URLs/main/official-domains.txt",
6970
// macOS specific

0 commit comments

Comments
 (0)