2021-07-25 08:55:35 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 10:10:34 -07:00
|
|
|
uBlock Origin - a comprehensive, efficient content blocker
|
2021-07-25 08:55:35 -06:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2021-08-21 14:02:32 -06:00
|
|
|
/* globals WebAssembly */
|
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 08:38:24 -06:00
|
|
|
import { createRequire } from 'module';
|
|
|
|
|
2021-08-02 10:32:47 -06:00
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
import { dirname, resolve } from 'path';
|
2021-09-04 06:44:26 -06:00
|
|
|
import { domainToASCII, fileURLToPath } from 'url';
|
2021-08-02 10:32:47 -06:00
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2021-08-23 07:42:27 -06:00
|
|
|
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
import snfe from './js/static-net-filtering.js';
|
2021-07-25 08:55:35 -06:00
|
|
|
import { FilteringContext } from './js/filtering-context.js';
|
2021-07-31 06:38:33 -06:00
|
|
|
import { LineIterator } from './js/text-utils.js';
|
2023-01-23 14:53:18 -07:00
|
|
|
import * as sfp from './js/static-filtering-parser.js';
|
2021-07-25 08:55:35 -06:00
|
|
|
|
|
|
|
import {
|
|
|
|
CompiledListReader,
|
|
|
|
CompiledListWriter,
|
|
|
|
} from './js/static-filtering-io.js';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-02 10:32:47 -06:00
|
|
|
function loadJSON(path) {
|
|
|
|
return JSON.parse(readFileSync(resolve(__dirname, path), 'utf8'));
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
/******************************************************************************/
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2021-07-29 14:54:51 -06:00
|
|
|
async function enableWASM() {
|
2021-07-29 15:04:32 -06:00
|
|
|
const wasmModuleFetcher = function(path) {
|
2021-07-31 05:06:11 -06:00
|
|
|
const require = createRequire(import.meta.url); // jshint ignore:line
|
|
|
|
const wasm = new Uint8Array(require(`${path}.wasm.json`));
|
2021-08-21 14:02:32 -06:00
|
|
|
return WebAssembly.compile(wasm);
|
2021-07-29 14:54:51 -06:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
const results = await Promise.all([
|
2021-08-23 07:42:27 -06:00
|
|
|
publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
|
2021-07-31 05:06:11 -06:00
|
|
|
snfe.enableWASM(wasmModuleFetcher, './js/wasm/'),
|
2021-07-29 14:54:51 -06:00
|
|
|
]);
|
|
|
|
return results.every(a => a === true);
|
|
|
|
} catch(reason) {
|
2021-07-31 05:06:11 -06:00
|
|
|
console.log(reason);
|
2021-07-29 14:54:51 -06:00
|
|
|
}
|
|
|
|
return false;
|
2021-07-25 08:55:35 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
/******************************************************************************/
|
2021-08-01 14:42:05 -06:00
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
function pslInit(raw) {
|
|
|
|
if ( typeof raw === 'string' && raw.trim() !== '' ) {
|
2021-09-04 06:44:26 -06:00
|
|
|
publicSuffixList.parse(raw, domainToASCII);
|
2021-08-23 07:42:27 -06:00
|
|
|
return publicSuffixList;
|
2021-08-08 07:17:14 -06:00
|
|
|
}
|
2021-08-01 14:42:05 -06:00
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
// Use serialized version if available
|
|
|
|
let serialized = null;
|
|
|
|
try {
|
|
|
|
// Use loadJSON() because require() would keep the string in memory.
|
|
|
|
serialized = loadJSON('build/publicsuffixlist.json');
|
|
|
|
} catch (error) {
|
2021-08-09 18:02:58 -06:00
|
|
|
if ( process.env.npm_lifecycle_event !== 'build' ) {
|
|
|
|
// This should never happen except during package building.
|
2021-08-08 07:17:14 -06:00
|
|
|
console.error(error);
|
2021-08-01 14:42:05 -06:00
|
|
|
}
|
2021-08-08 07:17:14 -06:00
|
|
|
}
|
|
|
|
if ( serialized !== null ) {
|
2021-08-23 07:42:27 -06:00
|
|
|
publicSuffixList.fromSelfie(serialized);
|
|
|
|
return publicSuffixList;
|
2021-08-08 07:17:14 -06:00
|
|
|
}
|
2021-08-01 14:42:05 -06:00
|
|
|
|
2021-08-15 09:18:01 -06:00
|
|
|
raw = readFileSync(
|
|
|
|
resolve(__dirname, './assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat'),
|
|
|
|
'utf8'
|
|
|
|
);
|
2021-08-08 07:17:14 -06:00
|
|
|
if ( typeof raw !== 'string' || raw.trim() === '' ) {
|
|
|
|
console.error('Unable to populate public suffix list');
|
|
|
|
return;
|
2021-08-01 13:42:46 -06:00
|
|
|
}
|
2021-09-04 06:44:26 -06:00
|
|
|
publicSuffixList.parse(raw, domainToASCII);
|
2021-08-23 07:42:27 -06:00
|
|
|
return publicSuffixList;
|
2021-07-25 08:55:35 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
/******************************************************************************/
|
2021-08-05 11:37:41 -06:00
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
function compileList({ name, raw }, compiler, writer, options = {}) {
|
|
|
|
const lineIter = new LineIterator(raw);
|
|
|
|
const events = Array.isArray(options.events) ? options.events : undefined;
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
if ( name ) {
|
|
|
|
writer.properties.set('name', name);
|
2021-08-05 11:30:20 -06:00
|
|
|
}
|
|
|
|
|
2023-01-23 14:53:18 -07:00
|
|
|
const parser = new sfp.AstFilterParser({
|
|
|
|
maxTokenLength: snfe.MAX_TOKEN_LENGTH,
|
|
|
|
});
|
2021-08-05 11:30:20 -06:00
|
|
|
|
2021-08-08 07:17:14 -06: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();
|
|
|
|
}
|
2023-01-23 14:53:18 -07:00
|
|
|
parser.parse(line);
|
|
|
|
if ( parser.isFilter() === false ) { continue; }
|
|
|
|
if ( parser.isNetworkFilter() === false ) { continue; }
|
|
|
|
if ( compiler.compile(parser, writer) ) { continue; }
|
2021-08-08 07:17:14 -06:00
|
|
|
if ( compiler.error !== undefined && events !== undefined ) {
|
|
|
|
options.events.push({
|
|
|
|
type: 'error',
|
|
|
|
text: compiler.error
|
|
|
|
});
|
|
|
|
}
|
2021-07-25 08:55:35 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
return writer.toString();
|
2021-08-05 11:30:20 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
async function useLists(lists, options = {}) {
|
2021-08-17 14:57:39 -06:00
|
|
|
if ( useLists.promise !== null ) {
|
|
|
|
throw new Error('Pending useLists() operation');
|
|
|
|
}
|
|
|
|
|
2021-08-05 11:30:20 -06:00
|
|
|
// Remove all filters
|
2021-08-08 07:17:14 -06:00
|
|
|
snfe.reset();
|
2021-08-05 11:30:20 -06:00
|
|
|
|
|
|
|
if ( Array.isArray(lists) === false || lists.length === 0 ) {
|
2021-08-15 09:18:01 -06:00
|
|
|
return;
|
2021-08-05 11:30:20 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
let compiler = null;
|
2021-08-05 11:30:20 -06:00
|
|
|
|
|
|
|
const consumeList = list => {
|
2021-08-08 07:17:14 -06:00
|
|
|
let { compiled } = list;
|
|
|
|
if ( typeof compiled !== 'string' || compiled === '' ) {
|
|
|
|
const writer = new CompiledListWriter();
|
|
|
|
if ( compiler === null ) {
|
2023-01-23 14:53:18 -07:00
|
|
|
compiler = snfe.createCompiler();
|
2021-08-08 07:17:14 -06:00
|
|
|
}
|
|
|
|
compiled = compileList(list, compiler, writer, options);
|
|
|
|
}
|
|
|
|
snfe.fromCompiled(new CompiledListReader(compiled));
|
2021-08-05 11:30:20 -06:00
|
|
|
};
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
// Populate filtering engine with resolved filter lists
|
2021-08-05 11:30:20 -06:00
|
|
|
const promises = [];
|
|
|
|
for ( const list of lists ) {
|
|
|
|
const promise = list instanceof Promise ? list : Promise.resolve(list);
|
|
|
|
promises.push(promise.then(list => consumeList(list)));
|
|
|
|
}
|
|
|
|
|
2021-08-17 14:57:39 -06:00
|
|
|
useLists.promise = Promise.all(promises);
|
|
|
|
await useLists.promise;
|
2021-09-11 07:33:32 -06:00
|
|
|
useLists.promise = null; // eslint-disable-line require-atomic-updates
|
2021-08-05 11:30:20 -06:00
|
|
|
|
|
|
|
// Commit changes
|
|
|
|
snfe.freeze();
|
|
|
|
snfe.optimize();
|
2021-07-25 08:55:35 -06:00
|
|
|
}
|
|
|
|
|
2021-08-17 14:57:39 -06:00
|
|
|
useLists.promise = null;
|
|
|
|
|
2021-08-08 05:22:02 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
const fctx = new FilteringContext();
|
2021-08-15 08:43:36 -06:00
|
|
|
let snfeProxyInstance = null;
|
2021-08-08 05:22:02 -06:00
|
|
|
|
|
|
|
class StaticNetFilteringEngine {
|
|
|
|
constructor() {
|
2021-08-15 08:43:36 -06:00
|
|
|
if ( snfeProxyInstance !== null ) {
|
2021-08-08 05:22:02 -06:00
|
|
|
throw new Error('Only a single instance is supported.');
|
|
|
|
}
|
2021-08-15 08:43:36 -06:00
|
|
|
snfeProxyInstance = this;
|
2021-08-08 05:22:02 -06:00
|
|
|
}
|
|
|
|
|
2021-08-19 05:34:02 -06:00
|
|
|
useLists(lists) {
|
|
|
|
return useLists(lists);
|
2021-08-08 05:22:02 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
matchRequest(details) {
|
|
|
|
return snfe.matchRequest(fctx.fromDetails(details));
|
2021-08-08 05:22:02 -06:00
|
|
|
}
|
|
|
|
|
2021-08-17 10:48:39 -06:00
|
|
|
matchAndFetchModifiers(details, modifier) {
|
|
|
|
return snfe.matchAndFetchModifiers(fctx.fromDetails(details), modifier);
|
|
|
|
}
|
|
|
|
|
2021-08-17 14:49:43 -06:00
|
|
|
hasQuery(details) {
|
|
|
|
return snfe.hasQuery(details);
|
|
|
|
}
|
|
|
|
|
2022-01-12 07:03:38 -07:00
|
|
|
filterQuery(details) {
|
2024-10-05 09:32:59 -06:00
|
|
|
fctx.redirectURL = undefined;
|
2022-01-12 07:03:38 -07:00
|
|
|
const directives = snfe.filterQuery(fctx.fromDetails(details));
|
|
|
|
if ( directives === undefined ) { return; }
|
|
|
|
return { redirectURL: fctx.redirectURL, directives };
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:47:12 -06:00
|
|
|
isBlockImportant() {
|
|
|
|
return snfe.isBlockImportant();
|
|
|
|
}
|
|
|
|
|
2021-08-08 05:22:02 -06:00
|
|
|
toLogData() {
|
|
|
|
return snfe.toLogData();
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
createCompiler(parser) {
|
|
|
|
return snfe.createCompiler(parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
compileList(...args) {
|
|
|
|
return compileList(...args);
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:01:39 -07:00
|
|
|
serialize() {
|
|
|
|
return snfe.serialize();
|
2021-08-09 14:52:41 -06:00
|
|
|
}
|
|
|
|
|
2021-12-06 05:01:39 -07:00
|
|
|
deserialize(serialized) {
|
|
|
|
return snfe.unserialize(serialized);
|
2021-08-09 14:52:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static async create({ noPSL = false } = {}) {
|
2021-08-08 07:17:14 -06:00
|
|
|
const instance = new StaticNetFilteringEngine();
|
|
|
|
|
|
|
|
if ( noPSL !== true && !pslInit() ) {
|
2021-08-08 05:22:02 -06:00
|
|
|
throw new Error('Failed to initialize public suffix list.');
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:17:14 -06:00
|
|
|
return instance;
|
2021-08-08 05:22:02 -06:00
|
|
|
}
|
2021-08-15 08:43:36 -06:00
|
|
|
|
2021-08-15 09:18:01 -06:00
|
|
|
static async release() {
|
|
|
|
if ( snfeProxyInstance === null ) { return; }
|
2021-08-15 08:43:36 -06:00
|
|
|
snfeProxyInstance = null;
|
2021-08-15 09:18:01 -06:00
|
|
|
await useLists([]);
|
2021-08-15 08:43:36 -06:00
|
|
|
}
|
2021-08-08 07:17:14 -06:00
|
|
|
}
|
2021-08-08 05:22:02 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-01 16:33:13 -06:00
|
|
|
// rollup.js needs module.exports to be set back to the local exports object.
|
|
|
|
// This is because some of the code (e.g. publicsuffixlist.js) sets
|
|
|
|
// module.exports. Once all included files are written like ES modules, using
|
|
|
|
// export statements, this should no longer be necessary.
|
2021-08-03 10:03:11 -06:00
|
|
|
if ( typeof module !== 'undefined' && typeof exports !== 'undefined' ) {
|
2021-08-03 08:14:40 -06:00
|
|
|
module.exports = exports;
|
2021-08-01 16:33:13 -06:00
|
|
|
}
|
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
export {
|
|
|
|
enableWASM,
|
|
|
|
pslInit,
|
2021-08-08 07:17:14 -06:00
|
|
|
StaticNetFilteringEngine,
|
2021-07-25 08:55:35 -06:00
|
|
|
};
|