Path: blob/master/views/sw-blacklist.js
15306 views
importScripts('{{route}}{{/scram/controller.sw.js}}');1importScripts('{{route}}{{/uv/uv.bundle.js}}');2importScripts('{{route}}{{/uv/uv.config.js}}');3importScripts(self['{{__uv$config}}'].sw || '{{route}}{{/uv/uv.sw.js}}');45const uv = new UVServiceWorker();67const SJ_CONTROLLER_PREFIX = '{{route}}{{/scram/network/}}';89const blacklist = {};10fetch('{{route}}{{/assets/txt/blacklist.txt}}').then((request) => {11request.text().then((textData) => {12textData13.split('\n')14.filter((domain) => domain.trim())15.forEach((domain) => {16const domainTld = domain.replace(/.+(?=\.\w)/, '');17if (!blacklist.hasOwnProperty(domainTld)) blacklist[domainTld] = [];18blacklist[domainTld].push(19encodeURIComponent(domain.slice(0, -domainTld.length))20.replace(/([()])/g, '\\$1')21.replace(/(\*\.)|\./g, (match, exp) =>22exp ? '(?:.+\\.)?' : '\\' + match23)24);25});2627for (let [tld, domains] of Object.entries(blacklist))28blacklist[tld] = new RegExp(`^(?:${domains.join('|')})$`);29Object.freeze(blacklist);30});31});3233const isBlacklistedDomain = (domain) => {34if (!domain) return false;35const domainTld = domain.replace(/.+(?=\.\w)/, '');36return (37blacklist.hasOwnProperty(domainTld) &&38blacklist[domainTld].test(domain.slice(0, -domainTld.length))39);40};4142const targetHostnameForScramjet = (reqUrl) => {43try {44const path = new URL(reqUrl).pathname;45if (!path.startsWith(SJ_CONTROLLER_PREFIX)) return null;46const rest = path.slice(SJ_CONTROLLER_PREFIX.length).split('/');47if (rest.length < 3) return null;48const encoded = rest.slice(2).join('/');49if (!encoded) return null;50return new URL(decodeURIComponent(encoded)).hostname;51} catch {52return null;53}54};5556self.addEventListener('fetch', (event) => {57event.respondWith(58(async () => {59if ($scramjetController.shouldRoute(event)) {60const hostname = targetHostnameForScramjet(event.request.url);61if (isBlacklistedDomain(hostname))62return new Response(new Blob(), { status: 406 });63return $scramjetController.route(event);64}6566if (uv.route(event)) {67try {68const hostname = new URL(69uv.config.decodeUrl(70new URL(event.request.url).pathname.replace(uv.config.prefix, '')71)72).hostname;73if (isBlacklistedDomain(hostname))74return new Response(new Blob(), { status: 406 });75} catch {76}77return await uv.fetch(event);78}7980return fetch(event.request);81})()82);83});848586