Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/views/uv/sw-blacklist.js
5227 views
1
importScripts('{{route}}{{/uv/uv.bundle.js}}');
2
importScripts('{{route}}{{/uv/uv.config.js}}');
3
importScripts(self['{{__uv$config}}'].sw || '{{route}}{{/uv/uv.sw.js}}');
4
5
/*
6
7
Workerware does not work yet due to one of the following possibilities:
8
9
1. UV or the bare client is not updated to support workerware yet.
10
2. Workerware is unfinished.
11
3. We are doofuses and do not know how to use workerware properly.
12
13
Going to implement a ghetto domain blacklist for now.
14
15
importScripts("./workerware.js");
16
17
const ww = new WorkerWare({
18
debug: true,
19
randomNames: true,
20
timing: true
21
});
22
23
24
ww.use({
25
function: event => console.log(event),
26
events: ["fetch", "message"]
27
});
28
29
*/
30
31
const uv = new UVServiceWorker();
32
33
// Get list of blacklisted domains.
34
const blacklist = {};
35
fetch('{{route}}{{/assets/json/blacklist.json}}').then((request) => {
36
request.json().then((jsonData) => {
37
// Organize each domain by their tld (top level domain) ending.
38
jsonData.forEach((domain) => {
39
const domainTld = domain.replace(/.+(?=\.\w)/, '');
40
if (!blacklist.hasOwnProperty(domainTld)) blacklist[domainTld] = [];
41
42
// Store each entry in an array. Each tld has its own array, which will
43
// later be concatenated into a regular expression.
44
blacklist[domainTld].push(
45
encodeURIComponent(domain.slice(0, -domainTld.length))
46
.replace(/([()])/g, '\\$1')
47
.replace(/(\*\.)|\./g, (match, firstExpression) =>
48
firstExpression ? '(?:.+\\.)?' : '\\' + match
49
)
50
);
51
});
52
53
// Turn each domain list into a regular expression and prevent this
54
// from being accidentally modified afterward.
55
for (let [domainTld, domainList] of Object.entries(blacklist))
56
blacklist[domainTld] = new RegExp(`^(?:${domainList.join('|')})$`);
57
Object.freeze(blacklist);
58
});
59
});
60
61
self.addEventListener('fetch', (event) => {
62
event.respondWith(
63
(async () => {
64
if (uv.route(event)) {
65
// The one and only ghetto domain blacklist.
66
const domain = new URL(
67
uv.config.decodeUrl(
68
new URL(event.request.url).pathname.replace(uv.config.prefix, '')
69
)
70
).hostname,
71
domainTld = domain.replace(/.+(?=\.\w)/, '');
72
73
// If the domain is in the blacklist, return a 406 response code.
74
if (
75
blacklist.hasOwnProperty(domainTld) &&
76
blacklist[domainTld].test(domain.slice(0, -domainTld.length))
77
)
78
return new Response(new Blob(), { status: 406 });
79
80
return await uv.fetch(event);
81
}
82
return await fetch(event.request);
83
})()
84
);
85
});
86
87