-
Notifications
You must be signed in to change notification settings - Fork 0
/
exclude-bots.ts
29 lines (23 loc) · 936 Bytes
/
exclude-bots.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { isbot } from "https://cdn.jsdelivr.net/npm/isbot@latest/index.mjs";
import { TextLineStream } from "https://deno.land/[email protected]/streams/mod.ts";
// Does not work with masked IPs (e.g. anonip)
// const badIPsRespone = await fetch(
// "https://github.com/LittleJake/ip-blacklist/blob/main/all_blacklist.txt",
// );
// if (!badIPsRespone.ok) {
// throw new Error("Failed to download blacklist " + badIPsRespone.text());
// }
// const badIPs = (await badIPsRespone.text()).split("\n");
const lines = Deno.stdin.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
for await (const line of lines) {
// Works with Apache2 'Combined' log format
const [ip, , , , , , , , , , , ...userAgentParts] = line.split(" ");
const userAgent = userAgentParts.join(" ");
const lineIsOk = !isbot(userAgent);
// lineIsOk = !badIPs.includes(ip))
if (lineIsOk) {
console.log(line);
}
}