/
block-log.es
191 lines (172 loc) · 5.37 KB
/
block-log.es
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
/*!
* Copyright (c) 2014-present Cliqz 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 https://mozilla.org/MPL/2.0/.
*/
/* eslint no-restricted-syntax: 'off' */
/* eslint guard-for-in: 'off' */
import * as persist from '../core/persistent-state';
import md5 from '../core/helpers/md5';
import events from '../core/events';
import * as datetime from './time';
export default class BlockLog {
constructor(telemetry, config) {
this.telemetry = telemetry;
this.config = config;
this.blocked = new persist.LazyPersistentObject('blocked');
this.localBlocked = new persist.LazyPersistentObject('localBlocked');
}
get blockReportList() {
return this.config.reportList || {};
}
init() {
this.blocked.load();
this.localBlocked.load();
this.onHourChanged = () => {
const delay = 24;
const hour = datetime.newUTCDate();
hour.setHours(hour.getHours() - delay);
const hourCutoff = datetime.hourString(hour);
this._cleanLocalBlocked(hourCutoff);
this.sendTelemetry();
};
this._hourChangedListener = events.subscribe('attrack:hour_changed', this.onHourChanged);
}
unload() {
if (this._hourChangedListener) {
this._hourChangedListener.unsubscribe();
this._hourChangedListener = null;
}
}
/**
* Add an entry to the block log
* @param {String} tabUrl domain name of where this block happened
* @param {String} tracker the 3rd party tracker hostname which was blocked
* @param {String} key the key for the blocked value
* @param {String} value the blocked value
* @param {String} type the type of blocked value
*/
add(tabUrl, tracker, key, value, type) {
const hour = datetime.getTime();
this.offerToReporter(tabUrl, tracker, key, value, type);
// local logging of blocked tokens
this._addLocalBlocked(tabUrl, tracker, key, value, hour);
}
clear() {
this.blocked.clear();
this.localBlocked.clear();
}
_addBlocked(tracker, key, value, type) {
const bl = this.blocked.value;
if (!(tracker in bl)) {
bl[tracker] = {};
}
if (!(key in bl[tracker])) {
bl[tracker][key] = {};
}
if (!(value in bl[tracker][key])) {
bl[tracker][key][value] = {};
}
if (!(type in bl[tracker][key][value])) {
bl[tracker][key][value][type] = 0;
}
bl[tracker][key][value][type] += 1;
this.blocked.setDirty();
}
_addLocalBlocked(source, s, k, v, hour) {
const lb = this.localBlocked.value;
if (!(source in lb)) {
lb[source] = {};
}
if (!(s in lb[source])) {
lb[source][s] = {};
}
if (!(k in lb[source][s])) {
lb[source][s][k] = {};
}
if (!(v in lb[source][s][k])) {
lb[source][s][k][v] = {};
}
if (!(hour in lb[source][s][k][v])) {
lb[source][s][k][v][hour] = 0;
}
lb[source][s][k][v][hour] += 1;
this.localBlocked.setDirty();
}
_cleanLocalBlocked(hourCutoff) {
// localBlocked
for (const source in this.localBlocked.value) {
for (const s in this.localBlocked.value[source]) {
for (const k in this.localBlocked.value[source][s]) {
for (const v in this.localBlocked.value[source][s][k]) {
for (const h in this.localBlocked.value[source][s][k][v]) {
if (h < hourCutoff) {
delete this.localBlocked.value[source][s][k][v][h];
}
}
if (Object.keys(this.localBlocked.value[source][s][k][v]).length === 0) {
delete this.localBlocked.value[source][s][k][v];
}
}
if (Object.keys(this.localBlocked.value[source][s][k]).length === 0) {
delete this.localBlocked.value[source][s][k];
}
}
if (Object.keys(this.localBlocked.value[source][s]).length === 0) {
delete this.localBlocked.value[source][s];
}
}
if (Object.keys(this.localBlocked.value[source]).length === 0) {
delete this.localBlocked.value[source];
}
}
this.localBlocked.setDirty(true);
this.localBlocked.save();
}
/**
* Check if this block event should be reported via telemetry, and if so, add to the
* block log
* @param {String} tabUrl
* @param {String} tracker
* @param {String} key
* @param {String} value
* @param {String} type
*/
offerToReporter(tabUrl, tracker, key, value, type) {
if (this.isInBlockReportList(tracker, key, value)) {
this._addBlocked(tracker, key, md5(value), type);
}
}
isInBlockReportList(tracker, key, value) {
if (tracker in this.blockReportList) {
const keyList = this.blockReportList[tracker];
if (keyList === '*') {
return true;
}
if (key in keyList || md5(key) in keyList) {
const valueList = keyList[key] || keyList[md5(key)];
if (valueList === '*') {
return true;
}
if (value in valueList || md5(value) in valueList) {
return true;
}
}
}
return false;
}
sendTelemetry() {
if (Object.keys(this.blocked.value).length > 0) {
this.telemetry({
message: {
action: 'attrack.blocked',
payload: this.blocked.value,
}
});
// reset the state
this.blocked.clear();
}
}
}