Further debloating and commenting for the ghetto blacklist.

This commit is contained in:
00Fjongl 2024-07-25 15:27:29 -05:00
parent e19dc1d2ee
commit a5b5f189b5

View file

@ -32,30 +32,50 @@ ww.use({
const uv = new UVServiceWorker();
// Get list of blacklisted domains.
let blacklist;
const blacklist = {};
fetch("/assets/json/blacklist.json").then(request => {
request.json().then(jsonData => {
blacklist = new RegExp(jsonData.map(
domain =>
encodeURIComponent(domain)
// Organize each domain by their tld (top level domain) ending.
jsonData.forEach(domain => {
const domainTld = domain.replace(/.+(?=\.\w)/, "");
if (!blacklist.hasOwnProperty(domainTld))
blacklist[domainTld] = [];
// Store each entry in an array. Each tld has its own array, which will
// later be concatenated into a regular expression.
blacklist[domainTld].push(
encodeURIComponent(domain.slice(0, -domainTld.length))
.replace(/([()])/g, "\\$1")
.replaceAll("*.", "(?:.+\\.)?")
.replaceAll(".", "\\.")
).join("|"));
.replace(/(\*\.)|\./g, (match, firstExpression) =>
firstExpression ? "(?:.+\\.)?" : "\\" + match)
);
});
// Turn each domain list into a regular expression and prevent this
// from being accidentally modified afterward.
for (let [domainTld, domainList] of Object.entries(blacklist))
blacklist[domainTld] = new RegExp(`^(?:${domainList.join("|")})$`);
Object.freeze(blacklist);
});
});
self.addEventListener("fetch", (event) => {
event.respondWith(
(async () => {
// The one and only ghetto domain blacklist.
if (!new URL(event.request.url).pathname.indexOf("/uv/service/")) {
const url = new URL(uv.config.decodeUrl(new URL(event.request.url).pathname.replace(/^\/uv\/service\//, "")));
if (blacklist.test(url.hostname))
return new Response(new Blob(), {status: 406});
}
if (uv.route(event)) {
// The one and only ghetto domain blacklist.
const domain = new URL(uv.config.decodeUrl(
new URL(event.request.url).pathname
.replace(uv.config.prefix, "")
)).hostname,
domainTld = domain.replace(/.+(?=\.\w)/, "");
// If the domain is in the blacklist, return a 406 response code.
if (blacklist.hasOwnProperty(domainTld) &&
blacklist[domainTld].test(domain.slice(0, -domainTld.length))
) return new Response(new Blob(), {status: 406});
return await uv.fetch(event);
}
return await fetch(event.request);