bunny-ai-blocking.js
· 3.2 KiB · JavaScript
原始文件
const config = {
ACCESS_KEY: "",
CLEANUP: false,
DEBUG: false,
PULL_ZONES: [69, 420],
};
const consts = {
AI_ROBOTS_FILE:
"https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/robots.txt",
API_HOST: "https://api.bunny.net/pullzone/",
EDGE_RULE_TITLE: "Block AI Bots",
PREFIX: "🐰🚫🤖",
};
function chunk(arr, size, initArray = []) {
const cpy = [...arr];
if (size <= 0) {
return initArray;
}
while (cpy.length) {
initArray.push(cpy.splice(0, size));
}
return initArray;
}
async function cleanupPullZone(pullZoneId) {
const res = await fetch(`${consts.API_HOST}${pullZoneId}`, {
headers: {
"content-type": "application/json",
AccessKey: config.ACCESS_KEY,
},
});
const data = await res.json();
const guids = data.EdgeRules.filter(({ Description }) =>
Description.includes(consts.EDGE_RULE_TITLE)
).map(({ Guid }) => Guid);
guids.map(async (guid) => {
const res = await fetch(
`${consts.API_HOST}${pullZoneId}/edgerules/${guid}`,
{
method: "DELETE",
headers: {
"content-type": "application/json",
AccessKey: config.ACCESS_KEY,
},
}
);
const data = await res.json();
console.log(`${consts.PREFIX} ${pullZoneId}: Deleted edge rule ${guid}`);
if (config.DEBUG) {
console.log(data);
}
});
}
async function cleanup() {
console.log(`${consts.PREFIX} Running clean up`);
config.PULL_ZONES.flatMap((pullZoneId) => cleanupPullZone(pullZoneId));
console.log(`${consts.PREFIX} Done cleaning up`);
}
function buildTriggers(robots) {
return chunk(robots, 5).map((PatternMatches) => ({
Type: 1,
PatternMatchingType: 0,
PatternMatches: PatternMatches.map((i) => `*${i}*`),
Parameter1: "User-Agent",
}));
}
async function fetchRobots() {
return await fetch(consts.AI_ROBOTS_FILE)
.then((res) => res.text())
.then((txt) =>
txt
.replaceAll("User-agent: ", "")
.replace("\nDisallow: /\n", "")
.split("\n")
);
}
async function createEdgeRules(triggers) {
const triggerChunks = chunk(triggers, 5);
for (const pullZoneID of config.PULL_ZONES) {
for (const Triggers of triggerChunks) {
const idx = triggerChunks.indexOf(Triggers);
const res = await fetch(
`${consts.API_HOST}${pullZoneID}/edgerules/addOrUpdate`,
{
method: "POST",
headers: {
"content-type": "application/json",
AccessKey: config.ACCESS_KEY,
},
body: JSON.stringify({
ActionType: 4,
TriggerMatchingType: 0,
Enabled: true,
Triggers,
Description: `${consts.EDGE_RULE_TITLE}, Group ${idx + 1}`,
}),
}
);
const data = await res.json();
console.log(`${consts.PREFIX} ${pullZoneID}: Processed batch ${idx + 1}`);
if (config.DEBUG) {
console.log(data);
}
}
}
}
console.log(
`${consts.PREFIX} Preparing to work on pullzones: ${config.PULL_ZONES.join(
", "
)}`
);
if (config.CLEANUP) {
await cleanup();
}
const robots = await fetchRobots();
const triggers = buildTriggers(robots);
await createEdgeRules(triggers);
console.log(`${consts.PREFIX} Completed creating edge rules.`);
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.`); |
130 |