2021-07-25 16:55:35 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 16:38:24 +02:00
|
|
|
import { createRequire } from 'module';
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
import './lib/punycode.js';
|
|
|
|
import './lib/publicsuffixlist/publicsuffixlist.js';
|
|
|
|
|
|
|
|
import globals from './js/globals.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
import snfe from './js/static-net-filtering.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
import { FilteringContext } from './js/filtering-context.js';
|
|
|
|
import { LineIterator } from './js/text-iterators.js';
|
|
|
|
import { StaticFilteringParser } from './js/static-filtering-parser.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CompiledListReader,
|
|
|
|
CompiledListWriter,
|
|
|
|
} from './js/static-filtering-io.js';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
function compileList(rawText, writer, options = {}) {
|
2021-07-25 16:55:35 +02:00
|
|
|
const lineIter = new LineIterator(rawText);
|
|
|
|
const parser = new StaticFilteringParser(true);
|
2021-07-29 01:48:38 +02:00
|
|
|
const events = Array.isArray(options.events) ? options.events : undefined;
|
2021-07-25 16:55:35 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
parser.setMaxTokenLength(snfe.MAX_TOKEN_LENGTH);
|
2021-07-25 16:55:35 +02:00
|
|
|
|
|
|
|
while ( lineIter.eot() === false ) {
|
|
|
|
let line = lineIter.next();
|
|
|
|
while ( line.endsWith(' \\') ) {
|
|
|
|
if ( lineIter.peek(4) !== ' ' ) { break; }
|
|
|
|
line = line.slice(0, -2).trim() + lineIter.next().trim();
|
|
|
|
}
|
|
|
|
parser.analyze(line);
|
|
|
|
if ( parser.shouldIgnore() ) { continue; }
|
|
|
|
if ( parser.category !== parser.CATStaticNetFilter ) { continue; }
|
|
|
|
if ( parser.patternHasUnicode() && parser.toASCII() === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( snfe.compile(parser, writer) ) { continue; }
|
|
|
|
if ( snfe.error !== undefined && events !== undefined ) {
|
|
|
|
options.events.push({
|
2021-07-25 16:55:35 +02:00
|
|
|
type: 'error',
|
2021-07-29 01:48:38 +02:00
|
|
|
text: snfe.error
|
|
|
|
});
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyList(name, raw) {
|
|
|
|
const writer = new CompiledListWriter();
|
|
|
|
writer.properties.set('name', name);
|
|
|
|
const compiled = compileList(raw, writer);
|
|
|
|
const reader = new CompiledListReader(compiled);
|
2021-07-29 01:48:38 +02:00
|
|
|
snfe.fromCompiled(reader);
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 22:54:51 +02:00
|
|
|
async function enableWASM() {
|
|
|
|
const wasmModuleFetcher = async function(path) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const require = createRequire(import.meta.url); // jshint ignore:line
|
|
|
|
const fs = require('fs');
|
|
|
|
fs.readFile(`${path}.wasm`, null, (err, data) => {
|
|
|
|
if ( err ) { return reject(err); }
|
|
|
|
return globals.WebAssembly.compile(data).then(module => {
|
|
|
|
resolve(module);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const results = await Promise.all([
|
|
|
|
globals.publicSuffixList.enableWASM(
|
|
|
|
wasmModuleFetcher,
|
|
|
|
'./lib/publicsuffixlist/wasm/'
|
|
|
|
),
|
|
|
|
snfe.enableWASM(
|
|
|
|
wasmModuleFetcher,
|
|
|
|
'./js/wasm/'
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
return results.every(a => a === true);
|
|
|
|
} catch(reason) {
|
|
|
|
console.info(reason);
|
|
|
|
}
|
|
|
|
return false;
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function pslInit(raw) {
|
|
|
|
if ( typeof raw !== 'string' || raw.trim() === '' ) {
|
2021-07-28 16:38:24 +02:00
|
|
|
const require = createRequire(import.meta.url); // jshint ignore:line
|
|
|
|
raw = require('./data/effective_tld_names.json');
|
|
|
|
if ( typeof raw !== 'string' || raw.trim() === '' ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
console.error('Unable to populate public suffix list');
|
2021-07-28 16:38:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
globals.publicSuffixList.parse(raw, globals.punycode.toASCII);
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
function restart(lists, options = {}) {
|
2021-07-25 16:55:35 +02:00
|
|
|
// Remove all filters
|
|
|
|
reset();
|
|
|
|
|
|
|
|
if ( Array.isArray(lists) && lists.length !== 0 ) {
|
|
|
|
// Populate filtering engine with filter lists
|
|
|
|
for ( const { name, raw } of lists ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
applyList(name, raw, options);
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
// Commit changes
|
2021-07-29 01:48:38 +02:00
|
|
|
snfe.freeze();
|
|
|
|
snfe.optimize();
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
return snfe;
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
2021-07-29 01:48:38 +02:00
|
|
|
snfe.reset();
|
2021-07-25 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
FilteringContext,
|
|
|
|
enableWASM,
|
|
|
|
pslInit,
|
|
|
|
restart,
|
|
|
|
};
|