Slightly debloated blacklist domain testing

This commit is contained in:
00Fjongl 2024-07-25 03:38:51 -05:00
parent a829af651d
commit e19dc1d2ee

View file

@ -35,14 +35,13 @@ const uv = new UVServiceWorker();
let blacklist; let blacklist;
fetch("/assets/json/blacklist.json").then(request => { fetch("/assets/json/blacklist.json").then(request => {
request.json().then(jsonData => { request.json().then(jsonData => {
blacklist = jsonData.map( blacklist = new RegExp(jsonData.map(
domain => new RegExp( domain =>
encodeURIComponent(domain) encodeURIComponent(domain)
.replace(/([()])/g, "\\$1") .replace(/([()])/g, "\\$1")
.replaceAll("*.", "(?:.+\\.)?") .replaceAll("*.", "(?:.+\\.)?")
.replaceAll(".", "\\.") .replaceAll(".", "\\.")
) ).join("|"));
);
}); });
}); });
@ -52,7 +51,7 @@ self.addEventListener("fetch", (event) => {
// The one and only ghetto domain blacklist. // The one and only ghetto domain blacklist.
if (!new URL(event.request.url).pathname.indexOf("/uv/service/")) { 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\//, ""))); const url = new URL(uv.config.decodeUrl(new URL(event.request.url).pathname.replace(/^\/uv\/service\//, "")));
if (blacklist.some(domain => domain.test(url.hostname))) if (blacklist.test(url.hostname))
return new Response(new Blob(), {status: 406}); return new Response(new Blob(), {status: 406});
} }