mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-12 19:40:02 -04:00
Further debloating and commenting for the ghetto blacklist.
This commit is contained in:
parent
e19dc1d2ee
commit
a5b5f189b5
1 changed files with 34 additions and 14 deletions
|
@ -32,30 +32,50 @@ ww.use({
|
||||||
const uv = new UVServiceWorker();
|
const uv = new UVServiceWorker();
|
||||||
|
|
||||||
// Get list of blacklisted domains.
|
// Get list of blacklisted domains.
|
||||||
let blacklist;
|
const blacklist = {};
|
||||||
fetch("/assets/json/blacklist.json").then(request => {
|
fetch("/assets/json/blacklist.json").then(request => {
|
||||||
request.json().then(jsonData => {
|
request.json().then(jsonData => {
|
||||||
blacklist = new RegExp(jsonData.map(
|
|
||||||
domain =>
|
// Organize each domain by their tld (top level domain) ending.
|
||||||
encodeURIComponent(domain)
|
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")
|
.replace(/([()])/g, "\\$1")
|
||||||
.replaceAll("*.", "(?:.+\\.)?")
|
.replace(/(\*\.)|\./g, (match, firstExpression) =>
|
||||||
.replaceAll(".", "\\.")
|
firstExpression ? "(?:.+\\.)?" : "\\" + match)
|
||||||
).join("|"));
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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) => {
|
self.addEventListener("fetch", (event) => {
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
(async () => {
|
(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)) {
|
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 uv.fetch(event);
|
||||||
}
|
}
|
||||||
return await fetch(event.request);
|
return await fetch(event.request);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue