-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathengines.js
409 lines (348 loc) · 11.2 KB
/
engines.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* Ghostery Browser Extension
* https://www.ghostery.com/
*
* Copyright 2017-present Ghostery GmbH. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/
import * as IDB from 'idb';
import {
FiltersEngine,
ENGINE_VERSION,
getLinesWithFilters,
mergeDiffs,
Config,
} from '@cliqz/adblocker';
import { registerDatabase } from '/utils/indexeddb.js';
export const CUSTOM_ENGINE = 'custom-filters';
const checkUserAgent = (pattern) => navigator.userAgent.indexOf(pattern) !== -1;
const ENV = new Map([
['ext_ghostery', true],
['cap_html_filtering', true],
['env_firefox', checkUserAgent('Firefox')],
['env_chromium', checkUserAgent('Chrome')],
['env_edge', checkUserAgent('Edg')],
['env_mobile', checkUserAgent('Mobile')],
]);
const engines = new Map();
function loadFromMemory(name) {
return engines.get(name);
}
function saveToMemory(name, engine) {
engines.set(name, engine);
}
// custom filter exceptions should apply to all engines
function shareExceptions(name, engine) {
if (name === CUSTOM_ENGINE) return;
// Network exceptions
const matchExceptions = engine.exceptions.match.bind(engine.exceptions);
engine.exceptions.match = (...args) => {
return (
matchExceptions(...args) || get(CUSTOM_ENGINE).exceptions.match(...args)
);
};
// Cosmetic exceptions
const iterMatchingFiltersUnhide =
engine.cosmetics.unhideIndex.iterMatchingFilters.bind(
engine.cosmetics.unhideIndex,
);
engine.cosmetics.unhideIndex.iterMatchingFilters = (...args) => {
iterMatchingFiltersUnhide(...args);
get(CUSTOM_ENGINE).cosmetics.unhideIndex.iterMatchingFilters(...args);
};
const matchAllUnhideExceptions = engine.hideExceptions.matchAll.bind(
engine.hideExceptions,
);
engine.hideExceptions.matchAll = (...args) => {
return (
matchAllUnhideExceptions(...args) ||
get(CUSTOM_ENGINE).hideExceptions.matchAll(...args)
);
};
}
const DB_NAME = registerDatabase('engines');
async function getDB() {
if (!getDB.current) {
getDB.current = IDB.openDB(DB_NAME, 1, {
upgrade(db) {
db.createObjectStore('engines');
},
async blocking() {
const db = await getDB.current;
db.close();
getDB.current = null;
},
});
}
return getDB.current;
}
async function loadFromStorage(name) {
try {
const db = await getDB();
const tx = db.transaction('engines');
const table = tx.objectStore('engines');
const engineBytes = await table.get(name);
if (engineBytes) {
const engine = FiltersEngine.deserialize(engineBytes);
engine.updateEnv(ENV);
shareExceptions(name, engine);
saveToMemory(name, engine);
return engine;
}
} catch (e) {
console.error(`Failed to load engine "${name}" from storage`, e);
}
return null;
}
async function saveToStorage(name) {
try {
const engine = loadFromMemory(name);
const db = await getDB();
const tx = db.transaction('engines', 'readwrite');
const table = tx.objectStore('engines');
await table.put(engine.serialize(), name);
} catch (e) {
console.error(`Failed to save engine "${name}" to storage`, e);
}
}
function check(response) {
if (!response.ok) {
throw new Error(
`Failed to fetch engine "${name}": ${response.status}: ${response.statusText}`,
);
}
return response;
}
async function update(name) {
if (name === CUSTOM_ENGINE) return;
try {
const urlName =
name === 'trackerdb'
? 'trackerdbMv3'
: `dnr${__PLATFORM__ === 'firefox' ? '' : '-cosmetics'}-${name}`;
const data = await fetch(
`https://cdn.ghostery.com/adblocker/configs/${urlName}/allowed-lists.json`,
)
.then(check)
.then((res) => res.json());
if (!data.engines[ENGINE_VERSION]) {
throw new Error(
`Engine "${name}" for "${ENGINE_VERSION}" engine version not found`,
);
}
// Get current engine
let engine = loadFromMemory(name);
// Check if some lists need to be removed from the engine: either because
// there are lists removed from allowed-lists.json or because some region
// lists need to be disabled. In this case, we just reset the engine for
// simplicity. Doing so also allows us to save memory because we do not have
// to keep track of which filters belong to which list.
//
// We also make sure that all lists which need to be updated have an
// available diff. If not, the operation would be equivalent to first
// deleting the list then adding the new version. Because of this, we also
// reset the engine if that happens.
let foundListsToRemove = false;
for (const [name, checksum] of engine.lists.entries()) {
// If engine has a list which is not "enabled"
if (!data.lists[name]) {
foundListsToRemove = true;
break;
}
// If engine has an out-dated list which does not have a diff available
if (
data.lists[name].checksum !== checksum &&
data.lists[name].diffs[checksum] === undefined
) {
foundListsToRemove = true;
break;
}
}
// Make a full update if we need to remove some lists
if (foundListsToRemove) {
const arrayBuffer = await fetch(data.engines[ENGINE_VERSION].url)
.then(check)
.then((res) => res.arrayBuffer());
const engineBytes = new Uint8Array(arrayBuffer);
engine = FiltersEngine.deserialize(engineBytes);
engine.updateEnv(ENV);
shareExceptions(name, engine);
// Save the new engine to memory and storage
saveToMemory(name, engine);
saveToStorage(name);
console.log(`Engine "${name}" reloaded`);
return engine;
}
// At this point we know that no list needs to be removed anymore. What
// remains to be done is: *add new lists* and *update existing lists with
// their respective diffs*.
const diffs = [];
/**
* Helper function used to fetch a full list, parse it, accumulate
* parsed filters, then update the checksum in engine if previous
* steps were successful.
*/
const fetchListToAdd = async ({ name, checksum, url }) => {
try {
// Create new diff and update version of the list in `this.engine`
diffs.push({
added: Array.from(
getLinesWithFilters(
await fetch(url)
.then(check)
.then((res) => res.text()),
engine.config,
),
),
});
engine.lists.set(name, checksum);
} catch (e) {
console.error(`Failed to add list "${name}"`, e);
}
};
/**
* Helper function used to fetch a list diff, parse it, accumulate
* parsed filters, then update the checksum in engine if previous
* steps were successful.
*/
const fetchListToUpdate = async ({ name, checksum, url }) => {
try {
// Create new diff and update version of the list in engine
diffs.push(
await fetch(url)
.then(check)
.then((res) => res.json()),
);
engine.lists.set(name, checksum);
} catch (e) {
console.error(`Failed to update list "${name}"`, e);
}
};
// Go over enabled list and start fetching the ones which need to be added
// or updated. All of this will happen concurrently.
const promises = [];
for (const name of Object.keys(data.lists)) {
const checksum = engine.lists.get(name);
if (checksum === undefined) {
promises.push(
fetchListToAdd({
name,
checksum: data.lists[name].checksum,
url: data.lists[name].url,
}),
);
} else if (checksum !== data.lists[name].checksum) {
promises.push(
fetchListToUpdate({
name,
checksum: data.lists[name].checksum,
url: data.lists[name].diffs[checksum],
}),
);
}
}
// Wait for all lists to have been fetched and parsed
await Promise.all(promises);
// `engine.update` method will return `true` if anything was
// updated and `false` otherwise.
const cumulativeDiff = mergeDiffs(diffs);
let updated = engine.updateFromDiff(cumulativeDiff, ENV);
// Last but not least, check if resources.txt should be updated. This can be
// done independently of filters as the data is stored in a separate object.
if (
data.resources &&
data.resources.checksum !== engine.resources.checksum
) {
engine.updateResources(
await fetch(data.resources.url)
.then(check)
.then((res) => res.text()),
data.resources.checksum,
);
updated = true;
}
if (updated) {
console.log(`Engine "${name}" updated`);
// Save the new engine to storage
saveToStorage(name);
}
return engine;
} catch (e) {
console.error(`Failed to update engine "${name}"`, e);
throw e;
}
}
export function updateAll() {
return Promise.all(Array.from(engines.keys()).map((name) => update(name)));
}
async function loadFromDisk(name) {
try {
const response = await fetch(
chrome.runtime.getURL(
`rule_resources/engine-${name}${
__PLATFORM__ === 'firefox' || name === 'trackerdb' ? '' : '-cosmetics'
}.dat`,
),
);
const engineBytes = new Uint8Array(await response.arrayBuffer());
const engine = FiltersEngine.deserialize(engineBytes);
engine.updateEnv(ENV);
shareExceptions(name, engine);
saveToMemory(name, engine);
saveToStorage(name);
// After initial load from disk, schedule an update
// as it is done only once on the first run.
// After loading from disk, it should be loaded from the storage
update(name).catch(() => null);
return engine;
} catch (e) {
console.error(`Failed to load engine "${name}" from disk`, e);
return new FiltersEngine();
}
}
export function get(name) {
return loadFromMemory(name);
}
const ALARM_PREFIX = 'engines:update:';
const ALARM_DELAY = 60; // 1 hour
export async function init(name) {
if (name === CUSTOM_ENGINE) {
return (
get(CUSTOM_ENGINE) ||
(await loadFromStorage(CUSTOM_ENGINE)) ||
(await createCustomEngine())
);
}
// Schedule an alarm to update engines once a day
chrome.alarms.get(`${ALARM_PREFIX}${name}`, (alarm) => {
if (!alarm) {
chrome.alarms.create(`${ALARM_PREFIX}${name}`, {
delayInMinutes: ALARM_DELAY,
});
}
});
return (
get(name) || (await loadFromStorage(name)) || (await loadFromDisk(name))
);
}
export async function createCustomEngine(filters = '') {
const config = new Config({
enableHtmlFiltering: true,
});
const engine = FiltersEngine.parse(filters, config);
saveToMemory(CUSTOM_ENGINE, engine);
saveToStorage(CUSTOM_ENGINE);
return engine;
}
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name.startsWith(ALARM_PREFIX)) {
const name = alarm.name.slice(ALARM_PREFIX.length);
update(name).catch(() => null);
chrome.alarms.create(alarm.name, {
delayInMinutes: ALARM_DELAY,
});
}
});