2021-07-25 08:55:35 -06: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-08-23 07:42:27 -06:00
|
|
|
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
|
2021-08-22 10:03:59 -06:00
|
|
|
import punycode from './lib/punycode.js';
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2021-08-04 13:14:48 -06:00
|
|
|
import staticNetFilteringEngine 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';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function compileList(rawText, writer) {
|
|
|
|
const lineIter = new LineIterator(rawText);
|
2023-01-23 14:53:18 -07:00
|
|
|
const parser = new sfp.AstFilterParser({
|
|
|
|
interactive: true,
|
|
|
|
maxTokenLength: staticNetFilteringEngine.MAX_TOKEN_LENGTH,
|
|
|
|
});
|
|
|
|
const compiler = staticNetFilteringEngine.createCompiler();
|
2021-07-25 08:55:35 -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);
|
2021-07-25 08:55:35 -06:00
|
|
|
|
2023-01-23 14:53:18 -07:00
|
|
|
if ( parser.isFilter() === false ) { continue; }
|
|
|
|
if ( parser.isNetworkFilter() === false ) { continue; }
|
2021-08-04 13:14:48 -06:00
|
|
|
if ( compiler.compile(parser, writer) ) { continue; }
|
|
|
|
if ( compiler.error !== undefined ) {
|
2021-07-25 08:55:35 -06:00
|
|
|
console.info(JSON.stringify({
|
|
|
|
realm: 'message',
|
|
|
|
type: 'error',
|
2021-08-04 13:14:48 -06:00
|
|
|
text: compiler.error
|
2021-07-25 08:55:35 -06: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);
|
|
|
|
staticNetFilteringEngine.fromCompiled(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableWASM(path) {
|
|
|
|
return Promise.all([
|
2021-08-23 07:42:27 -06:00
|
|
|
publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
|
2021-07-25 08:55:35 -06:00
|
|
|
staticNetFilteringEngine.enableWASM(`${path}/js`),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function pslInit(raw) {
|
|
|
|
if ( typeof raw !== 'string' || raw.trim() === '' ) {
|
|
|
|
console.info('Unable to populate public suffix list');
|
|
|
|
return;
|
|
|
|
}
|
2021-08-23 07:42:27 -06:00
|
|
|
publicSuffixList.parse(raw, punycode.toASCII);
|
2021-07-25 08:55:35 -06:00
|
|
|
console.info('Public suffix list populated');
|
|
|
|
}
|
|
|
|
|
|
|
|
function restart(lists) {
|
|
|
|
// Remove all filters
|
|
|
|
reset();
|
|
|
|
|
|
|
|
if ( Array.isArray(lists) && lists.length !== 0 ) {
|
|
|
|
// Populate filtering engine with filter lists
|
|
|
|
for ( const { name, raw } of lists ) {
|
|
|
|
applyList(name, raw);
|
|
|
|
}
|
|
|
|
// Commit changes
|
|
|
|
staticNetFilteringEngine.freeze();
|
|
|
|
staticNetFilteringEngine.optimize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return staticNetFilteringEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
staticNetFilteringEngine.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
FilteringContext,
|
|
|
|
enableWASM,
|
|
|
|
pslInit,
|
|
|
|
restart,
|
|
|
|
};
|