2015-05-21 20:15:17 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
uBlock Origin - a browser extension to black/white list requests.
|
2018-03-11 15:59:39 +01:00
|
|
|
Copyright (C) 2015-2018 Raymond Hill
|
2015-05-21 20:15:17 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
'use strict';
|
2015-05-21 20:15:17 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// The purpose of log filtering is to create ad hoc filtering rules, to
|
|
|
|
// diagnose and assist in the creation of custom filters.
|
|
|
|
|
|
|
|
µBlock.URLNetFiltering = (function() {
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
buckets: map of [hostname + type]
|
|
|
|
bucket: array of rule entries, sorted from shorter to longer url
|
2015-05-21 20:15:17 +02:00
|
|
|
rule entry: { url, action }
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var actionToNameMap = {
|
|
|
|
1: 'block',
|
|
|
|
2: 'allow',
|
|
|
|
3: 'noop'
|
|
|
|
};
|
|
|
|
|
|
|
|
var nameToActionMap = {
|
|
|
|
'block': 1,
|
|
|
|
'allow': 2,
|
|
|
|
'noop': 3
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var RuleEntry = function(url, action) {
|
|
|
|
this.url = url;
|
|
|
|
this.action = action;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
var indexOfURL = function(entries, url) {
|
2015-05-21 20:15:17 +02:00
|
|
|
// TODO: binary search -- maybe, depends on common use cases
|
2017-02-11 17:44:18 +01:00
|
|
|
var urlLen = url.length,
|
|
|
|
entry;
|
|
|
|
// URLs must be ordered by increasing length.
|
|
|
|
for ( var i = 0; i < entries.length; i++ ) {
|
|
|
|
entry = entries[i];
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( entry.url.length > urlLen ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( entry.url === url ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
var indexOfMatch = function(entries, url) {
|
|
|
|
var urlLen = url.length,
|
|
|
|
i = entries.length;
|
2015-05-21 20:15:17 +02:00
|
|
|
while ( i-- ) {
|
2017-02-11 17:44:18 +01:00
|
|
|
if ( entries[i].url.length <= urlLen ) {
|
|
|
|
break;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-11 17:44:18 +01:00
|
|
|
if ( i !== -1 ) {
|
|
|
|
do {
|
|
|
|
if ( url.startsWith(entries[i].url) ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} while ( i-- );
|
|
|
|
}
|
2015-05-21 20:15:17 +02:00
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
var indexFromLength = function(entries, len) {
|
2015-05-21 20:15:17 +02:00
|
|
|
// TODO: binary search -- maybe, depends on common use cases
|
2017-02-11 17:44:18 +01:00
|
|
|
// URLs must be ordered by increasing length.
|
|
|
|
for ( var i = 0; i < entries.length; i++ ) {
|
|
|
|
if ( entries[i].url.length > len ) {
|
2015-05-21 20:15:17 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-02-11 17:44:18 +01:00
|
|
|
var addRuleEntry = function(entries, url, action) {
|
|
|
|
var entry = new RuleEntry(url, action),
|
|
|
|
i = indexFromLength(entries, url.length);
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( i === -1 ) {
|
2017-02-11 17:44:18 +01:00
|
|
|
entries.push(entry);
|
2015-05-21 20:15:17 +02:00
|
|
|
} else {
|
2017-02-11 17:44:18 +01:00
|
|
|
entries.splice(i, 0, entry);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var URLNetFiltering = function() {
|
|
|
|
this.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.reset = function() {
|
2017-02-11 17:44:18 +01:00
|
|
|
this.rules = new Map();
|
2015-05-21 20:15:17 +02:00
|
|
|
// registers, filled with result of last evaluation
|
|
|
|
this.context = '';
|
|
|
|
this.url = '';
|
|
|
|
this.type = '';
|
|
|
|
this.r = 0;
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = false;
|
2018-06-03 19:27:42 +02:00
|
|
|
this.decomposedSource = [];
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.assign = function(other) {
|
|
|
|
// Remove rules not in other
|
2018-03-11 15:59:39 +01:00
|
|
|
for ( var key of this.rules.keys() ) {
|
|
|
|
if ( other.rules.has(key) === false ) {
|
|
|
|
this.rules.delete(key);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add/change rules in other
|
2018-03-11 15:59:39 +01:00
|
|
|
for ( var entry of other.rules ) {
|
|
|
|
this.rules.set(entry[0], entry[1].slice());
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.setRule = function(srcHostname, url, type, action) {
|
|
|
|
if ( action === 0 ) {
|
|
|
|
return this.removeRule(srcHostname, url, type);
|
|
|
|
}
|
2017-02-11 17:44:18 +01:00
|
|
|
var bucketKey = srcHostname + ' ' + type,
|
|
|
|
entries = this.rules.get(bucketKey);
|
|
|
|
if ( entries === undefined ) {
|
|
|
|
entries = [];
|
|
|
|
this.rules.set(bucketKey, entries);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2017-02-11 17:44:18 +01:00
|
|
|
var i = indexOfURL(entries, url),
|
|
|
|
entry;
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( i !== -1 ) {
|
2017-02-11 17:44:18 +01:00
|
|
|
entry = entries[i];
|
|
|
|
if ( entry.action === action ) { return false; }
|
2015-05-21 20:15:17 +02:00
|
|
|
entry.action = action;
|
2017-02-11 17:44:18 +01:00
|
|
|
} else {
|
|
|
|
addRuleEntry(entries, url, action);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-05-21 20:15:17 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.removeRule = function(srcHostname, url, type) {
|
2017-02-11 17:44:18 +01:00
|
|
|
var bucketKey = srcHostname + ' ' + type,
|
|
|
|
entries = this.rules.get(bucketKey);
|
|
|
|
if ( entries === undefined ) {
|
2015-05-21 20:15:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 17:44:18 +01:00
|
|
|
var i = indexOfURL(entries, url);
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( i === -1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 17:44:18 +01:00
|
|
|
entries.splice(i, 1);
|
|
|
|
if ( entries.length === 0 ) {
|
|
|
|
this.rules.delete(bucketKey);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-05-21 20:15:17 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.evaluateZ = function(context, target, type) {
|
2017-02-11 17:44:18 +01:00
|
|
|
this.r = 0;
|
|
|
|
if ( this.rules.size === 0 ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
return 0;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-06-03 19:27:42 +02:00
|
|
|
µBlock.decomposeHostname(context, this.decomposedSource);
|
|
|
|
for ( let shn of this.decomposedSource ) {
|
|
|
|
this.context = shn;
|
|
|
|
let entries = this.rules.get(shn + ' ' + type);
|
|
|
|
if ( entries !== undefined ) {
|
|
|
|
let i = indexOfMatch(entries, target);
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( i !== -1 ) {
|
2018-06-03 19:27:42 +02:00
|
|
|
let entry = entries[i];
|
2015-05-21 20:15:17 +02:00
|
|
|
this.url = entry.url;
|
|
|
|
this.type = type;
|
|
|
|
this.r = entry.action;
|
2017-05-12 16:35:11 +02:00
|
|
|
return this.r;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-03 19:27:42 +02:00
|
|
|
entries = this.rules.get(shn + ' *');
|
|
|
|
if ( entries !== undefined ) {
|
|
|
|
let i = indexOfMatch(entries, target);
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( i !== -1 ) {
|
2018-06-03 19:27:42 +02:00
|
|
|
let entry = entries[i];
|
2015-05-21 20:15:17 +02:00
|
|
|
this.url = entry.url;
|
|
|
|
this.type = '*';
|
|
|
|
this.r = entry.action;
|
2017-05-12 16:35:11 +02:00
|
|
|
return this.r;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.mustAllowCellZ = function(context, target, type) {
|
|
|
|
return this.evaluateZ(context, target, type).r === 2;
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.mustBlockOrAllow = function() {
|
|
|
|
return this.r === 1 || this.r === 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
URLNetFiltering.prototype.toLogData = function() {
|
|
|
|
if ( this.r === 0 ) { return; }
|
|
|
|
return {
|
|
|
|
source: 'dynamicUrl',
|
|
|
|
result: this.r,
|
|
|
|
rule: [
|
|
|
|
this.context,
|
|
|
|
this.url,
|
|
|
|
this.type,
|
|
|
|
this.intToActionMap.get(this.r)
|
|
|
|
],
|
|
|
|
raw: this.context + ' ' +
|
|
|
|
this.url + ' ' +
|
|
|
|
this.type + ' ' +
|
|
|
|
this.intToActionMap.get(this.r)
|
|
|
|
};
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
URLNetFiltering.prototype.intToActionMap = new Map([
|
|
|
|
[ 1, ' block' ],
|
|
|
|
[ 2, ' allow' ],
|
|
|
|
[ 3, ' noop' ]
|
|
|
|
]);
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-22 14:05:55 +02:00
|
|
|
URLNetFiltering.prototype.copyRules = function(other, context, urls, type) {
|
|
|
|
var url, otherOwn, thisOwn;
|
|
|
|
var i = urls.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
url = urls[i];
|
|
|
|
other.evaluateZ(context, url, type);
|
2017-03-14 20:09:40 +01:00
|
|
|
otherOwn = other.r !== 0 && other.context === context && other.url === url && other.type === type;
|
2015-05-22 14:05:55 +02:00
|
|
|
this.evaluateZ(context, url, type);
|
2017-03-14 20:09:40 +01:00
|
|
|
thisOwn = this.r !== 0 && this.context === context && this.url === url && this.type === type;
|
2015-05-22 14:05:55 +02:00
|
|
|
if ( otherOwn && !thisOwn ) {
|
|
|
|
this.setRule(context, url, type, other.r);
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-05-22 14:05:55 +02:00
|
|
|
}
|
|
|
|
if ( !otherOwn && thisOwn ) {
|
|
|
|
this.removeRule(context, url, type);
|
2018-03-11 15:59:39 +01:00
|
|
|
this.changed = true;
|
2015-05-22 14:05:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
return this.changed;
|
2015-05-22 14:05:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-26 14:43:46 +02:00
|
|
|
// "url-filtering:" hostname url type action
|
2015-05-21 20:15:17 +02:00
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
URLNetFiltering.prototype.toArray = function() {
|
2017-02-11 17:44:18 +01:00
|
|
|
var out = [],
|
2017-05-19 16:12:55 +02:00
|
|
|
key, pos, hn, type, entries, i, entry;
|
|
|
|
for ( var item of this.rules ) {
|
|
|
|
key = item[0];
|
2017-02-11 17:44:18 +01:00
|
|
|
pos = key.indexOf(' ');
|
|
|
|
hn = key.slice(0, pos);
|
|
|
|
pos = key.lastIndexOf(' ');
|
|
|
|
type = key.slice(pos + 1);
|
2017-05-19 16:12:55 +02:00
|
|
|
entries = item[1];
|
2017-02-11 17:44:18 +01:00
|
|
|
for ( i = 0; i < entries.length; i++ ) {
|
|
|
|
entry = entries[i];
|
2015-05-21 20:15:17 +02:00
|
|
|
out.push(
|
|
|
|
hn + ' ' +
|
|
|
|
entry.url + ' ' +
|
|
|
|
type + ' ' +
|
|
|
|
actionToNameMap[entry.action]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.toString = function() {
|
|
|
|
return this.toArray().sort().join('\n');
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.fromString = function(text) {
|
|
|
|
this.reset();
|
2018-03-11 15:59:39 +01:00
|
|
|
var lineIter = new µBlock.LineIterator(text);
|
2017-02-11 17:44:18 +01:00
|
|
|
while ( lineIter.eot() === false ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
this.addFromRuleParts(lineIter.next().trim().split(/\s+/));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.validateRuleParts = function(parts) {
|
|
|
|
if ( parts.length !== 4 ) { return; }
|
|
|
|
if ( parts[1].indexOf('://') === -1 ) { return; }
|
|
|
|
if ( nameToActionMap.hasOwnProperty(parts[3]) === false ) { return; }
|
|
|
|
return parts;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.addFromRuleParts = function(parts) {
|
|
|
|
if ( this.validateRuleParts(parts) !== undefined ) {
|
|
|
|
this.setRule(parts[0], parts[1], parts[2], nameToActionMap[parts[3]]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
URLNetFiltering.prototype.removeFromRuleParts = function(parts) {
|
|
|
|
if ( this.validateRuleParts(parts) !== undefined ) {
|
|
|
|
this.removeRule(parts[0], parts[1], parts[2]);
|
|
|
|
return true;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
return false;
|
2015-05-21 20:15:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return URLNetFiltering;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.sessionURLFiltering = new µBlock.URLNetFiltering();
|
|
|
|
µBlock.permanentURLFiltering = new µBlock.URLNetFiltering();
|
|
|
|
|
|
|
|
/******************************************************************************/
|