adguardHomeJoiner/index.ts

76 lines
No EOL
3.4 KiB
TypeScript

const filterList = [
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.amazon.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.apple.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.huawei.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.winoffice.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.samsung.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.tiktok.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.tiktok.extended.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.lgwebos.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.vivo.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.oppo-realme.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/native.xiaomi.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/fake.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/popupads.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/tif.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/tif-ips.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/pro.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/dyndns.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/hoster.txt"
]
Bun.serve({
async fetch(request, server) {
let rules: string[] = [];
for (const filter of filterList) {
const listReq = await fetch(filter);
let list = await listReq.text();
list = list.replace("[Adblock Plus]", '').trim();
list = list.replace(/^!.*$/gm, '').trim();
const listArray = list.split('\n')
rules = rules.concat(listArray)
}
const originalRuleCount = rules.length;
rules = Array.from(new Set(rules));
const rulesText = makeComment(originalRuleCount, originalRuleCount - rules.length) + rules.join('\n');
return new Response(rulesText, {
headers: {
"content-type": "text/plain; charset=utf-8",
"Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0",
"Pragma": "no-cache",
"Expires": "0",
"Surrogate-Control": "no-store"
},
})
}
})
const makeComment = (originalRuleCount: number, ruleCount: number) => {
const now = new Date();
const second = now.getSeconds();
const minute = now.getMinutes();
const hour = now.getHours();
const day = now.getDate();
const month = now.getMonth() + 1;
const year = now.getFullYear();
return `[Adblock Plus]
! Title: HaGeZi's DNS Blocklists Combined
! Description: A collection of DNS blocklists by HaGeZi, combined into one list, with duplicates removed.
! Expires: 1 hour
! Last Modified: ${new Date().toUTCString().split(", ")[1]}
! Version: ${year}.${month}.${day}.${hour}.${minute}.${second}
! Syntax: AdBlock
! Number of Entries: ${ruleCount}
! Original Number of Entries: ${originalRuleCount}
! Total Removed Entries: ${originalRuleCount - ruleCount}\n`
}