Last active 1741105183

melanie revised this gist 1741105183. Go to revision

No changes

melanie revised this gist 1732922276. Go to revision

No changes

melanie revised this gist 1732918358. Go to revision

1 file changed, 129 insertions

bunny-ai-blocking.js(file created)

@@ -0,0 +1,129 @@
1 + const config = {
2 + ACCESS_KEY: "",
3 + CLEANUP: false,
4 + DEBUG: false,
5 + PULL_ZONES: [69, 420],
6 + };
7 +
8 + const consts = {
9 + AI_ROBOTS_FILE:
10 + "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/robots.txt",
11 + API_HOST: "https://api.bunny.net/pullzone/",
12 + EDGE_RULE_TITLE: "Block AI Bots",
13 + PREFIX: "🐰🚫🤖",
14 + };
15 +
16 + function chunk(arr, size, initArray = []) {
17 + const cpy = [...arr];
18 + if (size <= 0) {
19 + return initArray;
20 + }
21 + while (cpy.length) {
22 + initArray.push(cpy.splice(0, size));
23 + }
24 + return initArray;
25 + }
26 +
27 + async function cleanupPullZone(pullZoneId) {
28 + const res = await fetch(`${consts.API_HOST}${pullZoneId}`, {
29 + headers: {
30 + "content-type": "application/json",
31 + AccessKey: config.ACCESS_KEY,
32 + },
33 + });
34 + const data = await res.json();
35 + const guids = data.EdgeRules.filter(({ Description }) =>
36 + Description.includes(consts.EDGE_RULE_TITLE)
37 + ).map(({ Guid }) => Guid);
38 +
39 + guids.map(async (guid) => {
40 + const res = await fetch(
41 + `${consts.API_HOST}${pullZoneId}/edgerules/${guid}`,
42 + {
43 + method: "DELETE",
44 + headers: {
45 + "content-type": "application/json",
46 + AccessKey: config.ACCESS_KEY,
47 + },
48 + }
49 + );
50 + const data = await res.json();
51 + console.log(`${consts.PREFIX} ${pullZoneId}: Deleted edge rule ${guid}`);
52 + if (config.DEBUG) {
53 + console.log(data);
54 + }
55 + });
56 + }
57 +
58 + async function cleanup() {
59 + console.log(`${consts.PREFIX} Running clean up`);
60 + config.PULL_ZONES.flatMap((pullZoneId) => cleanupPullZone(pullZoneId));
61 + console.log(`${consts.PREFIX} Done cleaning up`);
62 + }
63 +
64 + function buildTriggers(robots) {
65 + return chunk(robots, 5).map((PatternMatches) => ({
66 + Type: 1,
67 + PatternMatchingType: 0,
68 + PatternMatches: PatternMatches.map((i) => `*${i}*`),
69 + Parameter1: "User-Agent",
70 + }));
71 + }
72 +
73 + async function fetchRobots() {
74 + return await fetch(consts.AI_ROBOTS_FILE)
75 + .then((res) => res.text())
76 + .then((txt) =>
77 + txt
78 + .replaceAll("User-agent: ", "")
79 + .replace("\nDisallow: /\n", "")
80 + .split("\n")
81 + );
82 + }
83 +
84 + async function createEdgeRules(triggers) {
85 + const triggerChunks = chunk(triggers, 5);
86 + for (const pullZoneID of config.PULL_ZONES) {
87 + for (const Triggers of triggerChunks) {
88 + const idx = triggerChunks.indexOf(Triggers);
89 + const res = await fetch(
90 + `${consts.API_HOST}${pullZoneID}/edgerules/addOrUpdate`,
91 + {
92 + method: "POST",
93 + headers: {
94 + "content-type": "application/json",
95 + AccessKey: config.ACCESS_KEY,
96 + },
97 + body: JSON.stringify({
98 + ActionType: 4,
99 + TriggerMatchingType: 0,
100 + Enabled: true,
101 + Triggers,
102 + Description: `${consts.EDGE_RULE_TITLE}, Group ${idx + 1}`,
103 + }),
104 + }
105 + );
106 + const data = await res.json();
107 + console.log(`${consts.PREFIX} ${pullZoneID}: Processed batch ${idx + 1}`);
108 + if (config.DEBUG) {
109 + console.log(data);
110 + }
111 + }
112 + }
113 + }
114 +
115 + console.log(
116 + `${consts.PREFIX} Preparing to work on pullzones: ${config.PULL_ZONES.join(
117 + ", "
118 + )}`
119 + );
120 +
121 + if (config.CLEANUP) {
122 + await cleanup();
123 + }
124 +
125 + const robots = await fetchRobots();
126 + const triggers = buildTriggers(robots);
127 + await createEdgeRules(triggers);
128 +
129 + console.log(`${consts.PREFIX} Completed creating edge rules.`);
Newer Older