Path: blob/master/views/uv/sw-blacklist.js
5227 views
importScripts('{{route}}{{/uv/uv.bundle.js}}');1importScripts('{{route}}{{/uv/uv.config.js}}');2importScripts(self['{{__uv$config}}'].sw || '{{route}}{{/uv/uv.sw.js}}');34/*56Workerware does not work yet due to one of the following possibilities:781. UV or the bare client is not updated to support workerware yet.92. Workerware is unfinished.103. We are doofuses and do not know how to use workerware properly.1112Going to implement a ghetto domain blacklist for now.1314importScripts("./workerware.js");1516const ww = new WorkerWare({17debug: true,18randomNames: true,19timing: true20});212223ww.use({24function: event => console.log(event),25events: ["fetch", "message"]26});2728*/2930const uv = new UVServiceWorker();3132// Get list of blacklisted domains.33const blacklist = {};34fetch('{{route}}{{/assets/json/blacklist.json}}').then((request) => {35request.json().then((jsonData) => {36// Organize each domain by their tld (top level domain) ending.37jsonData.forEach((domain) => {38const domainTld = domain.replace(/.+(?=\.\w)/, '');39if (!blacklist.hasOwnProperty(domainTld)) blacklist[domainTld] = [];4041// Store each entry in an array. Each tld has its own array, which will42// later be concatenated into a regular expression.43blacklist[domainTld].push(44encodeURIComponent(domain.slice(0, -domainTld.length))45.replace(/([()])/g, '\\$1')46.replace(/(\*\.)|\./g, (match, firstExpression) =>47firstExpression ? '(?:.+\\.)?' : '\\' + match48)49);50});5152// Turn each domain list into a regular expression and prevent this53// from being accidentally modified afterward.54for (let [domainTld, domainList] of Object.entries(blacklist))55blacklist[domainTld] = new RegExp(`^(?:${domainList.join('|')})$`);56Object.freeze(blacklist);57});58});5960self.addEventListener('fetch', (event) => {61event.respondWith(62(async () => {63if (uv.route(event)) {64// The one and only ghetto domain blacklist.65const domain = new URL(66uv.config.decodeUrl(67new URL(event.request.url).pathname.replace(uv.config.prefix, '')68)69).hostname,70domainTld = domain.replace(/.+(?=\.\w)/, '');7172// If the domain is in the blacklist, return a 406 response code.73if (74blacklist.hasOwnProperty(domainTld) &&75blacklist[domainTld].test(domain.slice(0, -domainTld.length))76)77return new Response(new Blob(), { status: 406 });7879return await uv.fetch(event);80}81return await fetch(event.request);82})()83);84});858687