forked from Xelio/requestly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
635 lines (534 loc) · 20.9 KB
/
background.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
var payableURLs = {}; // map from payable URL to payment headers
var instrHeaders = {}; // map from payable URL to instr headers
var cookieData = {}; // map from payable URL to map of cookie data ("scheme_id": "expiration")
var PayableHeaderNames = [
"Username",
"Price",
"Bitcoin-Payment-Channel-Server",
"Bitcoin-Address"
];
var OptionalHeaderNames = [
"Rate",
"Scheme_id",
]
var paymentModuleIP = "http://10.8.125.119:8080"
var enabled = true;
chrome.browserAction.onClicked.addListener(setEnabled);
function setEnabled() {
enabled = !enabled;
chrome.browserAction.setIcon({path: "resources/images/38x38" + (enabled ? ".png" : "_greyscale.png")});
if (enabled) {
chrome.webRequest.onHeadersReceived.addListener(
BG.Methods.payableResponseHeadersListener, { urls: ['<all_urls>'] }, ['blocking', 'responseHeaders']
);
}
else {
chrome.webRequest.onHeadersReceived.removeListener(BG.Methods.payableResponseHeadersListener);
}
}
// show popup on install
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install" || details.reason == "update") { // other: "chrome_update"
chrome.windows.create({
'url': 'src/background/install.html',
'type': 'normal'
});
}
});
function requestOffChainHeaders(url, payload) {
$.ajax({
type:"POST",
url: paymentModuleIP + "/headers",
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(payload),
success: function(resp) {
// console.log("Proxy response: ", resp);
payableURLs[url].push({
"Authorization": resp["Authorization"],
"Bitcoin-Transfer": resp["Bitcoin-Transfer"],
"Expiration": ((url in cookieData) && (payload["headers"]["scheme_id"] in cookieData[url]) ?
cookieData[url][payload["headers"]["scheme_id"]] : null)
});
console.log("Got headers. Current array: ", JSON.stringify(payableURLs[url]));
},
failure: function(err) {
alert(err)
}
});
};
function requestChannelsHeaders(payload) {
$.ajax({
type:"POST",
url: paymentModuleIP + "/headers-channels",
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(payload),
success: function(resp) {
console.log("Proxy response: ", resp);
tokenHeaders.push(resp['bitcoin-payment-channel-token']);
},
failure: function(err) {
alert(err)
}
});
};
var BG = {
Methods: {},
statusSettings: {
id: RQ.STORAGE_KEYS.REQUESTLY_SETTINGS,
avoidCache: true,
isExtensionEnabled: true
},
extensionStatusContextMenuId: -1
};
BG.Methods.matchUrlWithReplaceRulePairs = function(rule, url) {
var pairs = rule.pairs,
pair = null,
from = null,
isFromPartRegex,
resultingUrl = null;
for (var i = 0; i < pairs.length; i++) {
pair = pairs[i];
pair.from = pair.from || '';
// When string pair.from looks like a RegExp, create a RegExp object from it
from = RQ.Utils.toRegex(pair.from);
isFromPartRegex = (from !== null);
from = from || pair.from;
// Use String.match method when from is Regex otherwise use indexOf
// Issue-86: String.match("?a=1") fails with an error
if ((isFromPartRegex && url.match(from)) || (url.indexOf(from) !== -1)) {
resultingUrl = url.replace(from, pair.to);
break;
}
}
return resultingUrl;
};
BG.Methods.removeHeader = function(headers, name) {
for (var i = headers.length - 1; i >= 0; i--) {
if (headers[i].name.toLowerCase() === name.toLowerCase()) {
headers.splice(i, 1);
break;
}
}
};
BG.Methods.modifyHeaderIfExists = function(headers, newHeader) {
for (var i = headers.length - 1; i >= 0; i--) {
if (headers[i].name.toLowerCase() === newHeader.name.toLowerCase()) {
headers[i].value = newHeader.value;
break;
}
}
};
BG.Methods.getHeaderIfExists = function(headers, targetHeaderName) {
for (var i = headers.length - 1; i >= 0; i--) {
if (headers[i].name.toLowerCase() === targetHeaderName.toLowerCase()) {
return headers[i];
}
}
return null;
};
/* Returns base URL of given URL by omitting path after root domain */
function getBaseURL(url) {
pathArray = url.split('/');
protocol = pathArray[0];
host = pathArray[2];
return (protocol + '//' + host);
}
/* Returns true if arr contains entry with name 'header', false otherwise */
function containsHeader(arr, header) {
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i]["name"] === header)
return true;
}
return false;
}
/**
*
* @param originalHeaders Original Headers present in the HTTP(s) request
* @param headersTarget Request/Response (Where Modification is to be done)
* @param details (Actual details object)
* @returns originalHeaders with modifications if modified else returns {code}null{/code}
*/
BG.Methods.modifyHeaders = function(originalHeaders, headersTarget, details, isRequest) {
var rule,
headerPairs,
isRuleApplied = false,
modification,
url = details.url;
for (var i = 0; i < StorageService.records.length; i++) {
rule = StorageService.records[i];
if (rule.status !== RQ.RULE_STATUS.ACTIVE || rule.ruleType !== RQ.RULE_TYPES.HEADERS) {
continue;
}
headerPairs = rule.pairs || [];
for (var index = 0; index < headerPairs.length; index++) {
modification = headerPairs[index];
modification.source = modification.source || {};
if (modification.target !== headersTarget || !modification.header) {
continue;
}
// If Source Value exists and does not match, proceed with next pair
if (modification.source.value && BG.Methods.matchUrlWithRuleSource(modification.source, null, url) === null) {
continue;
}
isRuleApplied = true;
switch (modification.type) {
case RQ.MODIFICATION_TYPES.ADD:
originalHeaders.push({ name: modification.header, value: modification.value });
break;
case RQ.MODIFICATION_TYPES.REMOVE:
BG.Methods.removeHeader(originalHeaders, modification.header);
break;
case RQ.MODIFICATION_TYPES.MODIFY:
BG.Methods.modifyHeaderIfExists(originalHeaders, {
name: modification.header,
value: modification.value
});
break;
}
}
}
// return if not on payable URL or if in response handler
if (!payableURLs || !(getBaseURL(url) in payableURLs)
|| !isRequest || containsHeader(originalHeaders, "Access-Control-Request-Method")) {
return isRuleApplied ? originalHeaders : null;
}
// add payment headers to outgoing requests to payable URLs
// console.log("Headers: ", JSON.stringify(originalHeaders));
baseURL = getBaseURL(url);
offChainHeaders = payableURLs[baseURL];
// Drop expired payment headers
if (url.indexOf("timerated") == -1) {
while (offChainHeaders.length > 0) {
if (offChainHeaders[0]['Expiration'] != null &&
(offChainHeaders[0]['Expiration'] < new Date().getTime())) {
console.log("Headers expired!");
offChainHeaders.splice(0, 1);
if (offChainHeaders.length == 0) {
return isRuleApplied ? originalHeaders : null;
}
}
else {
break;
}
}
}
if (offChainHeaders.length > 0) {
isRuleApplied = true;
// console.log("Headers array: ", JSON.stringify(offChainHeaders));
console.log("Now adding headers...", "[" + url + "]");
// console.log("Authorization: " + offChainHeaders[0]['Authorization'],
// "Bitcoin-Transfer: " + offChainHeaders[0]['Bitcoin-Transfer']);
originalHeaders.push({ name: 'Authorization', value: offChainHeaders[0]['Authorization']});
originalHeaders.push({ name: 'Bitcoin-Transfer', value: offChainHeaders[0]['Bitcoin-Transfer']});
offChainHeaders.splice(0, 1);
requestOffChainHeaders(baseURL, {
"headers": instrHeaders[baseURL],
"url": url
});
}
else if (offChainHeaders.length == 0) {
// console.log("Needed payment headers, but didn't have them...");
console.log("Requesting headers...", "[" + url + "]");
requestOffChainHeaders(baseURL, {
"headers": instrHeaders[baseURL],
"url": url
});
}
return isRuleApplied ? originalHeaders : null;
};
/**
*
* @param originalHeaders Original Headers present in the HTTP(s) request
* @param headersTarget Request/Response (Where Modification is to be done)
* @param details (Actual details object)
* @returns originalHeaders with modifications if modified else returns {code}null{/code}
*/
BG.Methods.getPayableHeaders = function(originalHeaders, headersTarget, details) {
var payableHeaders = {};
for (var i = 0; i < PayableHeaderNames.length; i++) {
var header = BG.Methods.getHeaderIfExists(originalHeaders, PayableHeaderNames[i]);
if (header === null)
return null;
payableHeaders[PayableHeaderNames[i].toLowerCase()] = header.value;
}
for (var i = 0; i < OptionalHeaderNames.length; i++) {
var header = BG.Methods.getHeaderIfExists(originalHeaders, OptionalHeaderNames[i]);
if (header !== null) {
payableHeaders[OptionalHeaderNames[i].toLowerCase()] = header.value;
}
}
// console.log("Payable headers: ", payableHeaders);
return payableHeaders;
};
/**
* Checks if intercepted HTTP Request Url matches with any Rule
*
* @param sourceObject Object e.g. { key: 'Url', operator: 'Contains', value: 'google' }
* @param destination String e.g. 'http://www.google.com'
* @param url Url for which HTTP Request is intercepted.
*
* @returns String destinationUrl if Rule should be applied to intercepted Url else returns {code}null{/code}
*/
BG.Methods.matchUrlWithRuleSource = function(sourceObject, destination, url) {
var operator = sourceObject.operator,
destinationUrl = destination || '', // Destination Url is not present in all rule types (Cancel)
value = sourceObject.value,
blackListedDomains = RQ.BLACK_LIST_DOMAINS || [];
for (var index = 0; index < blackListedDomains.length; index++) {
if (url.indexOf(blackListedDomains[index]) !== -1) {
return null;
}
}
switch (operator) {
case RQ.RULE_OPERATORS.EQUALS: if (value === url) { return destinationUrl; }
break;
case RQ.RULE_OPERATORS.CONTAINS: if (url.indexOf(value) !== -1) { return destinationUrl; }
break;
case RQ.RULE_OPERATORS.MATCHES: {
var regex = RQ.Utils.toRegex(value),
matches;
// Do not match when regex is invalid or regex does not match with Url
if (!regex || url.search(regex) === -1) {
return null;
}
matches = regex.exec(url) || [];
matches.forEach(function (matchValue, index) {
// First match is the full string followed by parentheses/group values
if (index === 0) {
return;
}
// Issue: 73 We should not leave $i in the Url otherwise browser will encode that.
// Even if match is not found, just replace that placeholder with empty string
matchValue = matchValue || '';
// Replace all $index values in destinationUrl with the matched groups
destinationUrl = destinationUrl.replace(new RegExp('[\$]' + index, 'g'), matchValue);
});
return destinationUrl;
}
}
return null;
};
BG.Methods.modifyUrl = function(details) {
var resultingUrl,
pair,
pairIndex;
for (var i = 0; i < StorageService.records.length; i++) {
var rule = StorageService.records[i];
if (rule.status !== RQ.RULE_STATUS.ACTIVE) {
continue;
}
switch(rule.ruleType) {
case RQ.RULE_TYPES.REDIRECT:
// Introduce Pairs: Transform the Redirect Rule Model to new Model to support multiple entries (pairs)
if (typeof rule.source !== 'undefined' && typeof rule.destination !== 'undefined') {
rule.pairs = [{
source: { key: RQ.RULE_KEYS.URL, operator: rule.source.operator, value: rule.source.values[0] },
destination: rule.destination
}];
delete rule.source;
delete rule.destination;
}
for (pairIndex = 0; pairIndex < rule.pairs.length; pairIndex++) {
pair = rule.pairs[pairIndex];
resultingUrl = BG.Methods.matchUrlWithRuleSource(pair.source, pair.destination, details.url);
if (resultingUrl !== null) {
return { redirectUrl: resultingUrl };
}
}
break;
// In case of Cancel Request, destination url is 'javascript:'
case RQ.RULE_TYPES.CANCEL:
// Introduce Pairs: Transform the Cancel Rule Model to new Model to support multiple entries (pairs)
if (typeof rule.source !== 'undefined') {
rule.pairs = [{
source: { key: RQ.RULE_KEYS.URL, operator: rule.source.operator, value: rule.source.values[0] }
}];
delete rule.source;
}
for (pairIndex = 0; pairIndex < rule.pairs.length; pairIndex++) {
pair = rule.pairs[pairIndex];
resultingUrl = BG.Methods.matchUrlWithRuleSource(pair.source, null, details.url);
if (resultingUrl !== null) {
return { redirectUrl: 'javascript:' };
}
}
break;
case RQ.RULE_TYPES.REPLACE:
resultingUrl = BG.Methods.matchUrlWithReplaceRulePairs(rule, details.url);
if (resultingUrl !== null) {
return { redirectUrl: resultingUrl };
}
break;
}
}
};
BG.Methods.modifyRequestHeadersListener = function(details) {
var isRequest = true;
var modifiedHeaders = BG.Methods.modifyHeaders(details.requestHeaders, RQ.HEADERS_TARGET.REQUEST, details, isRequest);
if (modifiedHeaders !== null) {
return { requestHeaders: modifiedHeaders };
}
};
BG.Methods.modifyResponseHeadersListener = function(details) {
var isRequest = false;
var modifiedHeaders = BG.Methods.modifyHeaders(details.responseHeaders, RQ.HEADERS_TARGET.RESPONSE, details, isRequest);
if (modifiedHeaders !== null) {
return { responseHeaders: modifiedHeaders };
}
};
/* Callback invoked when any HTTP response is received */
BG.Methods.payableResponseHeadersListener = function(details) {
var payableHeaders = BG.Methods.getPayableHeaders(details.responseHeaders, RQ.HEADERS_TARGET.RESPONSE, details);
if (payableHeaders !== null) {
console.log("402 response received...", "[" + details.url + "]");
baseURL = getBaseURL(details.url);
if (!(baseURL in payableURLs)) {
payableURLs[baseURL] = [];
// console.log("URLs: ", payableURLs);
}
/* If received (instr) headers contain time-rating scheme... */
if ("rate" in payableHeaders) {
/* Request proxy for payment headers, if none stored */
if (payableURLs[baseURL].length == 0) {
requestOffChainHeaders(baseURL, {
"headers": payableHeaders,
"url": details.url
});
}
/* Save instructional headers */
instrHeaders[baseURL] = {
"price": payableHeaders["rate"],
"username": payableHeaders["username"],
"bitcoin-address": payableHeaders["bitcoin-address"],
"bitcoin-payment-channel-server": payableHeaders["bitcoin-payment-channel-server"],
"scheme_id": payableHeaders["scheme_id"]
};
// console.log("Set time-rated headers: ", instrHeaders[baseURL]);
}
}
};
BG.Methods.onCompletedListener = function(details) {
var baseURL = getBaseURL(details.url);
if (baseURL in payableURLs) {
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
chrome.cookies.get({"url": tabs[0].url, "name": "payments-cookie"}, function (cookie) {
if (cookie) {
if (!(baseURL in cookieData)) {
cookieData[baseURL] = {};
}
cookieData[baseURL][cookie.value] = cookie.expirationDate;
console.log("Cookie:", cookie.value, "expires:", cookie.expirationDate);
/* Set scheme ID on all headers with no scheme ID */
for (var i = 0; i < payableURLs[baseURL].length; i++) {
if (payableURLs[baseURL][i]["Expiration"] == null) {
payableURLs[baseURL][i]["Expiration"] = cookie.expirationDate;
console.log("Set expiration on previously received headers...");
console.log("Modified headers: ", JSON.stringify(payableURLs[baseURL]));
}
}
}
else {
console.log("No cookie found");
}
});
});
}
}
BG.Methods.registerListeners = function() {
if (!chrome.webRequest.onBeforeRequest.hasListener(BG.Methods.modifyUrl)) {
chrome.webRequest.onBeforeRequest.addListener(
BG.Methods.modifyUrl, { urls: ['<all_urls>'] }, ['blocking']
);
}
if (!chrome.webRequest.onBeforeSendHeaders.hasListener(BG.Methods.modifyRequestHeadersListener)) {
chrome.webRequest.onBeforeSendHeaders.addListener(
BG.Methods.modifyRequestHeadersListener, { urls: ['<all_urls>'] }, ['blocking', 'requestHeaders']
);
}
if (!chrome.webRequest.onHeadersReceived.hasListener(BG.Methods.modifyResponseHeadersListener)) {
chrome.webRequest.onHeadersReceived.addListener(
BG.Methods.modifyResponseHeadersListener, { urls: ['<all_urls>'] }, ['blocking', 'responseHeaders']
);
}
if (enabled && !chrome.webRequest.onHeadersReceived.hasListener(BG.Methods.payableResponseHeadersListener)) {
chrome.webRequest.onHeadersReceived.addListener(
BG.Methods.payableResponseHeadersListener, { urls: ['<all_urls>'] }, ['blocking', 'responseHeaders']
);
}
if (enabled && !chrome.webRequest.onCompleted.hasListener(BG.Methods.onCompletedListener)) {
chrome.webRequest.onCompleted.addListener(
BG.Methods.onCompletedListener, { urls: ['<all_urls>'] }
);
}
};
// http://stackoverflow.com/questions/23001428/chrome-webrequest-onbeforerequest-removelistener-how-to-stop-a-chrome-web
// Documentation: https://developer.chrome.com/extensions/events
BG.Methods.unregisterListeners = function() {
chrome.webRequest.onBeforeRequest.removeListener(BG.Methods.modifyUrl);
chrome.webRequest.onBeforeSendHeaders.removeListener(BG.Methods.modifyRequestHeadersListener);
chrome.webRequest.onHeadersReceived.removeListener(BG.Methods.modifyResponseHeadersListener);
chrome.webRequest.onHeadersReceived.removeListener(BG.Methods.payableResponseHeadersListener);
chrome.webRequest.onCompleted.removeListener(BG.Methods.onCompletedListener);
};
BG.Methods.disableExtension = function() {
BG.statusSettings['isExtensionEnabled'] = false;
StorageService.saveRecord({ rq_settings: BG.statusSettings }, BG.Methods.handleExtensionDisabled);
};
BG.Methods.enableExtension = function() {
BG.statusSettings['isExtensionEnabled'] = true;
StorageService.saveRecord({ rq_settings: BG.statusSettings }, BG.Methods.handleExtensionEnabled);
};
BG.Methods.handleExtensionDisabled = function() {
BG.Methods.unregisterListeners();
chrome.contextMenus.update(BG.extensionStatusContextMenuId, {
title: 'Activate Requestly',
onclick: BG.Methods.enableExtension
});
chrome.browserAction.setIcon({ path: RQ.RESOURCES.EXTENSION_ICON_GREYSCALE });
BG.Methods.sendMessage({ isExtensionEnabled: false });
console.log('Requestly disabled');
};
BG.Methods.handleExtensionEnabled = function() {
BG.Methods.registerListeners();
chrome.contextMenus.update(BG.extensionStatusContextMenuId, {
title: 'Deactivate Requestly',
onclick: BG.Methods.disableExtension
});
chrome.browserAction.setIcon({ path: RQ.RESOURCES.EXTENSION_ICON });
BG.Methods.sendMessage({ isExtensionEnabled: true });
console.log('Requestly enabled');
};
BG.Methods.readExtensionStatus = function() {
StorageService.getRecord(RQ.STORAGE_KEYS.REQUESTLY_SETTINGS, function(response) {
response = response || {};
var settings = response[RQ.STORAGE_KEYS.REQUESTLY_SETTINGS] || BG.statusSettings;
settings['isExtensionEnabled'] ? BG.Methods.handleExtensionEnabled() : BG.Methods.handleExtensionDisabled();
});
};
/* chrome.browserAction.onClicked.addListener(function () {
chrome.tabs.create({'url': RQ.WEB_URL }, function(tab) {
// Tab opened.
});
}); */
// Create contextMenu Action to Enable/Disable Requestly (Default Options)
chrome.contextMenus.removeAll();
BG.extensionStatusContextMenuId = chrome.contextMenus.create({
title: 'Deactivate Requestly',
type: 'normal',
contexts: ['browser_action'],
onclick: function() { console.log('Requestly Default handler executed'); }
});
BG.Methods.sendMessage = function(messageObject, callback) {
callback = callback || function() { console.log('DefaultHandler: Sending Message to Runtime: ', messageObject); };
chrome.tabs.query({ url: RQ.WEB_URL_PATTERN }, function(tabs) {
// Send message to each opened tab which matches the url
for (var tabIndex = 0; tabIndex < tabs.length; tabIndex++) {
chrome.tabs.sendMessage(tabs[tabIndex].id, messageObject, callback);
}
});
};
StorageService.getRecords({ callback: BG.Methods.readExtensionStatus });