Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/views/sw-blacklist.js
15306 views
1
importScripts('{{route}}{{/scram/controller.sw.js}}');
2
importScripts('{{route}}{{/uv/uv.bundle.js}}');
3
importScripts('{{route}}{{/uv/uv.config.js}}');
4
importScripts(self['{{__uv$config}}'].sw || '{{route}}{{/uv/uv.sw.js}}');
5
6
const uv = new UVServiceWorker();
7
8
const SJ_CONTROLLER_PREFIX = '{{route}}{{/scram/network/}}';
9
10
const blacklist = {};
11
fetch('{{route}}{{/assets/txt/blacklist.txt}}').then((request) => {
12
request.text().then((textData) => {
13
textData
14
.split('\n')
15
.filter((domain) => domain.trim())
16
.forEach((domain) => {
17
const domainTld = domain.replace(/.+(?=\.\w)/, '');
18
if (!blacklist.hasOwnProperty(domainTld)) blacklist[domainTld] = [];
19
blacklist[domainTld].push(
20
encodeURIComponent(domain.slice(0, -domainTld.length))
21
.replace(/([()])/g, '\\$1')
22
.replace(/(\*\.)|\./g, (match, exp) =>
23
exp ? '(?:.+\\.)?' : '\\' + match
24
)
25
);
26
});
27
28
for (let [tld, domains] of Object.entries(blacklist))
29
blacklist[tld] = new RegExp(`^(?:${domains.join('|')})$`);
30
Object.freeze(blacklist);
31
});
32
});
33
34
const isBlacklistedDomain = (domain) => {
35
if (!domain) return false;
36
const domainTld = domain.replace(/.+(?=\.\w)/, '');
37
return (
38
blacklist.hasOwnProperty(domainTld) &&
39
blacklist[domainTld].test(domain.slice(0, -domainTld.length))
40
);
41
};
42
43
const targetHostnameForScramjet = (reqUrl) => {
44
try {
45
const path = new URL(reqUrl).pathname;
46
if (!path.startsWith(SJ_CONTROLLER_PREFIX)) return null;
47
const rest = path.slice(SJ_CONTROLLER_PREFIX.length).split('/');
48
if (rest.length < 3) return null;
49
const encoded = rest.slice(2).join('/');
50
if (!encoded) return null;
51
return new URL(decodeURIComponent(encoded)).hostname;
52
} catch {
53
return null;
54
}
55
};
56
57
self.addEventListener('fetch', (event) => {
58
event.respondWith(
59
(async () => {
60
if ($scramjetController.shouldRoute(event)) {
61
const hostname = targetHostnameForScramjet(event.request.url);
62
if (isBlacklistedDomain(hostname))
63
return new Response(new Blob(), { status: 406 });
64
return $scramjetController.route(event);
65
}
66
67
if (uv.route(event)) {
68
try {
69
const hostname = new URL(
70
uv.config.decodeUrl(
71
new URL(event.request.url).pathname.replace(uv.config.prefix, '')
72
)
73
).hostname;
74
if (isBlacklistedDomain(hostname))
75
return new Response(new Blob(), { status: 406 });
76
} catch {
77
}
78
return await uv.fetch(event);
79
}
80
81
return fetch(event.request);
82
})()
83
);
84
});
85
86