forked from gorhill/httpswitchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreciper.js
More file actions
433 lines (382 loc) · 12.6 KB
/
reciper.js
File metadata and controls
433 lines (382 loc) · 12.6 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*******************************************************************************
httpswitchboard - a Chromium browser extension to black/white list requests.
Copyright (C) 2013 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/httpswitchboard
*/
/******************************************************************************/
HTTPSB.reciper = (function() {
/******************************************************************************/
// http://stackoverflow.com/a/106223
// Thanks!
var reValidHostname = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
var fromFriendlyType = {
'*': '*',
'main_frame': 'main_frame',
'page': 'main_frame',
'cookie': 'cookie',
'stylesheet': 'stylesheet',
'css': 'stylesheet',
'image': 'image',
'img': 'image',
'script': 'script',
'object': 'object',
'plugin': 'object',
'xmlhttprequest': 'xmlhttprequest',
'xhr': 'xmlhttprequest',
'sub_frame': 'sub_frame',
'frame': 'sub_frame',
'other': 'other'
};
var toFriendlyType = {
'*': '*',
'main_frame': 'page',
'page': 'page',
'cookie': 'cookie',
'stylesheet': 'css',
'css': 'css',
'image': 'image',
'img': 'image',
'script': 'script',
'object': 'plugin',
'plugin': 'plugin',
'xmlhttprequest': 'xhr',
'xhr': 'xhr',
'sub_frame': 'frame',
'frame': 'frame',
'other': 'other'
};
var fromFriendlyList = {
'white': 'white',
'whitelist': 'white',
'black': 'black',
'blacklist': 'black',
'gray': 'gray',
'graylist': 'gray'
};
var toFriendlyList = {
'white': 'whitelist',
'whitelist': 'whitelist',
'black': 'blacklist',
'blacklist': 'blacklist',
'gray': 'graylist',
'graylist': 'graylist'
};
/******************************************************************************/
// A journal is just an array of directives to create rules in one or
// more scopes
// Example:
// scopeKey
// \tlistKey
// \t\ttype hostname
var journalFromRecipe = function(s) {
var recipe;
try {
recipe = decodeURIComponent(s.replace(/\s+/g, ''));
// rhill 2014-27-01: Remove scheme from scope keys.
// https://github.com/gorhill/httpswitchboard/issues/165
recipe = recipe.replace(/(^|\n)https?:\/\//g, '$1');
}
catch (e) {
return [];
}
var httpsb = HTTPSB;
var journal = [];
var lines = recipe.split('\n');
var scopeKey, listKey;
var type, hostname;
var line, pos;
while ( lines.length ) {
line = lines.shift();
if ( line.length === 0 ) {
continue;
}
type = hostname = undefined;
if ( line.charAt(0) !== '\t' ) {
scopeKey = line.trim();
listKey = undefined;
} else if ( line.charAt(1) !== '\t' ) {
listKey = fromFriendlyList[line.trim()];
} else if ( line.charAt(2) !== '\t' ) {
pos = line.indexOf(' ');
if ( pos > 0 ) {
type = fromFriendlyType[line.slice(0, pos).trim()];
hostname = line.slice(pos).trim();
}
}
if ( scopeKey === undefined || listKey === undefined || type === undefined || hostname === undefined ) {
continue;
}
if ( httpsb.isValidScopeKey(scopeKey) === false ) {
continue;
}
if ( hostname !== '*' && reValidHostname.test(hostname) === false ) {
continue;
}
journal.push(['addRule', scopeKey, listKey, type, hostname].join('|'));
}
return journal;
};
/******************************************************************************/
var recipeFromJournal = function(journal) {
var recipe = [];
var entry, parts;
var scopeKey, listKey;
while ( entry = journal.shift() ) {
if ( !entry ) {
continue;
}
parts = entry.split('|');
switch ( parts[0] ) {
case 'addRule':
if ( parts[1] !== scopeKey ) {
scopeKey = parts[1];
recipe.push(scopeKey);
listKey = undefined;
}
if ( parts[2] !== listKey ) {
listKey = toFriendlyList[parts[2]];
recipe.push('\t' + listKey);
}
recipe.push('\t\t' + toFriendlyType[parts[3]] + ' ' + parts[4]);
break;
default:
break;
}
}
recipe = encodeURIComponent(recipe.join('\n'));
var s = [];
while ( recipe.length ) {
s.push(recipe.slice(0, 40));
recipe = recipe.slice(40);
}
return s.join('\n');
};
/******************************************************************************/
var journalFromScope = function(scopeKey) {
var journal = [];
var scope = HTTPSB.temporaryScopeFromScopeKey(scopeKey);
var listKeys = ['white', 'black', 'gray'];
var listKey, list, ruleKey;
while ( listKey = listKeys.shift() ) {
list = scope[listKey].list;
listKey += 'list';
for ( ruleKey in list ) {
if ( list.hasOwnProperty(ruleKey) === false ) {
continue;
}
journal.push(['addRule', scopeKey, listKey, ruleKey].join('|'));
}
}
return journal;
};
/******************************************************************************/
var recipeFromScope = function(scopeKey) {
return recipeFromJournal(journalFromScope(scopeKey));
};
/******************************************************************************/
var applyRules = function(scopeKey, rules) {
var httpsb = HTTPSB;
var rule;
while ( rule = rules.pop() ) {
httpsb.addTemporaryRule(scopeKey, rule.list, rule.type, rule.host);
}
};
/******************************************************************************/
var applyEverywhereRules = function(scopeKey, rules) {
var httpsb = HTTPSB;
var rule;
while ( rule = rules.pop() ) {
if ( rule.host !== '*' ) {
continue;
}
httpsb.addTemporaryRule(scopeKey, rule.list, rule.type, rule.host);
}
};
/******************************************************************************/
// Logic to determine what rules get copied where, depending on source and
// destination scopes.
//
// global into global: copy rules
// global into domain: copy rules
// global into site: copy rules
//
// domain into global: create scope
// copy rules
// domain into domain: copy rules if same domain
// domain into site: create domain scope
// copy rules
// copy site scope rule if same domain
// remove site scope if same domain
//
// site into global: create scope
// copy rules
// site into domain: copy rules if same domain
// site into site: copy rules if same domain
var applyJournalEntries = function(srcScopeKey, rules, dstScopeKey) {
var httpsb = HTTPSB;
// global to global:
// copy rules
// global to domain:
// copy rules
// global to site:
// copy rules
if ( httpsb.isGlobalScopeKey(srcScopeKey) ) {
applyRules(dstScopeKey, rules);
return;
}
var srcDomain = httpsb.domainFromScopeKey(srcScopeKey);
var dstDomain = httpsb.domainFromScopeKey(dstScopeKey);
var sameDomain = !!srcDomain && !!dstDomain && srcDomain === dstDomain;
var srcScope, dstScope;
// domain to global:
// create scope
// copy rules
// domain to domain:
// copy rules if same domain
// domain to site
// create domain scope
// copy rules
// copy site scope rule if same domain
// remove site scope if same domain
if ( httpsb.isDomainScopeKey(srcScopeKey) ) {
// domain to global:
// create scope
// copy rules
if ( httpsb.isGlobalScopeKey(dstScopeKey) ) {
httpsb.createTemporaryScopeFromScopeKey(srcScopeKey);
applyRules(srcScopeKey, rules);
return;
}
// domain to domain:
// copy rules if same domain
if ( httpsb.isDomainScopeKey(dstScopeKey) ) {
if ( sameDomain ) {
applyRules(dstScopeKey, rules);
} else {
applyEverywhereRules(dstScopeKey, rules);
}
return;
}
// domain to site
// create domain scope
// copy rules
// copy site scope rule if same domain
// remove site scope if same domain
if ( httpsb.isSiteScopeKey(dstScopeKey) ) {
if ( sameDomain ) {
srcScope = httpsb.createTemporaryScopeFromScopeKey(srcScopeKey);
applyRules(srcScopeKey, rules);
dstScope = httpsb.temporaryScopeFromScopeKey(dstScopeKey);
if ( dstScope ) {
srcScope.add(dstScope);
httpsb.removeTemporaryScopeFromScopeKey(dstScopeKey);
}
} else {
applyEverywhereRules(dstScopeKey, rules);
}
return;
}
return;
}
// site to global:
// create scope
// copy rules
// site to domain:
// copy rules if same domain
// site to site:
// copy rules if same domain
if ( httpsb.isSiteScopeKey(srcScopeKey) ) {
// site to global:
// create scope
// copy rules
if ( httpsb.isGlobalScopeKey(dstScopeKey) ) {
httpsb.createTemporaryScopeFromScopeKey(srcScopeKey);
applyRules(srcScopeKey, rules);
return;
}
// site to domain:
// copy rules if same domain
if ( httpsb.isDomainScopeKey(dstScopeKey) ) {
if ( sameDomain ) {
applyRules(dstScopeKey, rules);
} else {
applyEverywhereRules(dstScopeKey, rules);
}
return;
}
// site to site:
// copy rules if same domain
if ( httpsb.isSiteScopeKey(dstScopeKey) ) {
if ( sameDomain ) {
applyRules(dstScopeKey, rules);
} else {
applyEverywhereRules(dstScopeKey, rules);
}
return;
}
return;
}
};
/******************************************************************************/
var applyJournal = function(journal, dstScopeKey) {
var entry, parts;
var srcScopeKey;
var scopeKeyToRulesMap = {};
while ( entry = journal.shift() ) {
if ( !entry ) {
continue;
}
parts = entry.split('|');
if ( parts.length === 0 ) {
continue;
}
if ( parts[0] !== 'addRule' ) {
continue;
}
srcScopeKey = parts[1];
if ( scopeKeyToRulesMap.hasOwnProperty(srcScopeKey) === false ) {
scopeKeyToRulesMap[srcScopeKey] = [];
}
scopeKeyToRulesMap[srcScopeKey].push({
list: parts[2],
type: parts[3],
host: parts[4]
});
}
for ( srcScopeKey in scopeKeyToRulesMap ) {
if ( scopeKeyToRulesMap.hasOwnProperty(srcScopeKey) === false ) {
continue;
}
applyJournalEntries(
srcScopeKey,
scopeKeyToRulesMap[srcScopeKey],
dstScopeKey
);
}
};
/******************************************************************************/
var applyRecipe = function(recipe, dstScopeKey) {
applyJournal(journalFromRecipe(recipe), dstScopeKey);
};
/******************************************************************************/
var validateRecipe = function(recipe) {
return journalFromRecipe(recipe).length > 0;
};
/******************************************************************************/
return {
extract: recipeFromScope,
apply: applyRecipe,
validate: validateRecipe
};
/******************************************************************************/
})();