forked from gorhill/uBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnswitches.js
289 lines (245 loc) · 9.33 KB
/
hnswitches.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
uBlock Origin - a Chromium browser extension to black/white list requests.
Copyright (C) 2015-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
*/
/* jshint bitwise: false */
'use strict';
/******************************************************************************/
import punycode from '../lib/punycode.js';
import { decomposeHostname } from './uri-utils.js';
import { LineIterator } from './text-utils.js';
/******************************************************************************/
const decomposedSource = [];
// Object.create(null) is used below to eliminate worries about unexpected
// property names in prototype chain -- and this way we don't have to use
// hasOwnProperty() to avoid this.
const switchBitOffsets = Object.create(null);
Object.assign(switchBitOffsets, {
'no-strict-blocking': 0,
'no-popups': 2,
'no-cosmetic-filtering': 4,
'no-remote-fonts': 6,
'no-large-media': 8,
'no-csp-reports': 10,
'no-scripting': 12,
});
const switchStateToNameMap = Object.create(null);
Object.assign(switchStateToNameMap, {
'1': 'true',
'2': 'false',
});
const nameToSwitchStateMap = Object.create(null);
Object.assign(nameToSwitchStateMap, {
'true': 1,
'false': 2,
'on': 1,
'off': 2,
});
/******************************************************************************/
// For performance purpose, as simple test as possible
const reNotASCII = /[^\x20-\x7F]/;
// http://tools.ietf.org/html/rfc5952
// 4.3: "MUST be represented in lowercase"
// Also: http://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers
/******************************************************************************/
class DynamicSwitchRuleFiltering {
constructor() {
this.reset();
}
reset() {
this.switches = new Map();
this.n = '';
this.z = '';
this.r = 0;
this.changed = true;
}
assign(from) {
// Remove rules not in other
for ( const hn of this.switches.keys() ) {
if ( from.switches.has(hn) === false ) {
this.switches.delete(hn);
this.changed = true;
}
}
// Add/change rules in other
for ( const [hn, bits] of from.switches ) {
if ( this.switches.get(hn) !== bits ) {
this.switches.set(hn, bits);
this.changed = true;
}
}
}
copyRules(from, srcHostname) {
const thisBits = this.switches.get(srcHostname);
const fromBits = from.switches.get(srcHostname);
if ( fromBits !== thisBits ) {
if ( fromBits !== undefined ) {
this.switches.set(srcHostname, fromBits);
} else {
this.switches.delete(srcHostname);
}
this.changed = true;
}
return this.changed;
}
hasSameRules(other, srcHostname) {
return this.switches.get(srcHostname) === other.switches.get(srcHostname);
}
toggle(switchName, hostname, newVal) {
const bitOffset = switchBitOffsets[switchName];
if ( bitOffset === undefined ) { return false; }
if ( newVal === this.evaluate(switchName, hostname) ) { return false; }
let bits = this.switches.get(hostname) || 0;
bits &= ~(3 << bitOffset);
bits |= newVal << bitOffset;
if ( bits === 0 ) {
this.switches.delete(hostname);
} else {
this.switches.set(hostname, bits);
}
this.changed = true;
return true;
}
toggleOneZ(switchName, hostname, newState) {
const bitOffset = switchBitOffsets[switchName];
if ( bitOffset === undefined ) { return false; }
let state = this.evaluateZ(switchName, hostname);
if ( newState === state ) { return false; }
if ( newState === undefined ) {
newState = !state;
}
let bits = this.switches.get(hostname) || 0;
bits &= ~(3 << bitOffset);
if ( bits === 0 ) {
this.switches.delete(hostname);
} else {
this.switches.set(hostname, bits);
}
state = this.evaluateZ(switchName, hostname);
if ( state !== newState ) {
this.switches.set(hostname, bits | ((newState ? 1 : 2) << bitOffset));
}
this.changed = true;
return true;
}
toggleBranchZ(switchName, targetHostname, newState) {
this.toggleOneZ(switchName, targetHostname, newState);
// Turn off all descendant switches, they will inherit the state of the
// branch's origin.
const targetLen = targetHostname.length;
for ( const hostname of this.switches.keys() ) {
if ( hostname === targetHostname ) { continue; }
if ( hostname.length <= targetLen ) { continue; }
if ( hostname.endsWith(targetHostname) === false ) { continue; }
if ( hostname.charAt(hostname.length - targetLen - 1) !== '.' ) {
continue;
}
this.toggle(switchName, hostname, 0);
}
return this.changed;
}
toggleZ(switchName, hostname, deep, newState) {
if ( deep === true ) {
return this.toggleBranchZ(switchName, hostname, newState);
}
return this.toggleOneZ(switchName, hostname, newState);
}
// 0 = inherit from broader scope, up to default state
// 1 = non-default state
// 2 = forced default state (to override a broader non-default state)
evaluate(switchName, hostname) {
const bits = this.switches.get(hostname);
if ( bits === undefined ) { return 0; }
let bitOffset = switchBitOffsets[switchName];
if ( bitOffset === undefined ) { return 0; }
return (bits >>> bitOffset) & 3;
}
evaluateZ(switchName, hostname) {
const bitOffset = switchBitOffsets[switchName];
if ( bitOffset === undefined ) {
this.r = 0;
return false;
}
this.n = switchName;
for ( const shn of decomposeHostname(hostname, decomposedSource) ) {
let bits = this.switches.get(shn);
if ( bits === undefined ) { continue; }
bits = bits >>> bitOffset & 3;
if ( bits === 0 ) { continue; }
this.z = shn;
this.r = bits;
return bits === 1;
}
this.r = 0;
return false;
}
toLogData() {
return {
source: 'switch',
result: this.r,
raw: `${this.n}: ${this.z} true`
};
}
toArray() {
const out = [];
for ( const hostname of this.switches.keys() ) {
const prettyHn = hostname.includes('xn--') && punycode
? punycode.toUnicode(hostname)
: hostname;
for ( const switchName in switchBitOffsets ) {
if ( switchBitOffsets[switchName] === undefined ) { continue; }
const val = this.evaluate(switchName, hostname);
if ( val === 0 ) { continue; }
out.push(`${switchName}: ${prettyHn} ${switchStateToNameMap[val]}`);
}
}
return out;
}
toString() {
return this.toArray().join('\n');
}
fromString(text, append) {
const lineIter = new LineIterator(text);
if ( append !== true ) { this.reset(); }
while ( lineIter.eot() === false ) {
this.addFromRuleParts(lineIter.next().trim().split(/\s+/));
}
}
validateRuleParts(parts) {
if ( parts.length < 3 ) { return; }
if ( parts[0].endsWith(':') === false ) { return; }
if ( nameToSwitchStateMap[parts[2]] === undefined ) { return; }
if ( reNotASCII.test(parts[1]) && punycode !== undefined ) {
parts[1] = punycode.toASCII(parts[1]);
}
return parts;
}
addFromRuleParts(parts) {
if ( this.validateRuleParts(parts) === undefined ) { return false; }
const switchName = parts[0].slice(0, -1);
if ( switchBitOffsets[switchName] === undefined ) { return false; }
this.toggle(switchName, parts[1], nameToSwitchStateMap[parts[2]]);
return true;
}
removeFromRuleParts(parts) {
if ( this.validateRuleParts(parts) !== undefined ) {
this.toggle(parts[0].slice(0, -1), parts[1], 0);
return true;
}
return false;
}
}
/******************************************************************************/
export default DynamicSwitchRuleFiltering;
/******************************************************************************/