Files
I2P_in_Private_Browsing_Mod…/scrub.js

962 lines
34 KiB
JavaScript
Raw Normal View History

2020-12-03 12:20:43 -05:00
var titlepref = chrome.i18n.getMessage('titlePreface');
var webpref = chrome.i18n.getMessage('webPreface');
var routerpref = chrome.i18n.getMessage('routerPreface');
var mailpref = chrome.i18n.getMessage('mailPreface');
var torrentpref = chrome.i18n.getMessage('torrentPreface');
var tunnelpref = chrome.i18n.getMessage('i2ptunnelPreface');
var ircpref = chrome.i18n.getMessage('ircPreface');
var extensionpref = chrome.i18n.getMessage('extensionPreface');
var muwirepref = chrome.i18n.getMessage('muwirePreface');
var botepref = chrome.i18n.getMessage('botePreface');
2020-12-03 12:20:43 -05:00
var contextScrub = async function(requestDetails) {
function onHeaderError() {
2020-12-03 12:20:43 -05:00
console.log('Header scrub error');
}
2020-01-14 23:31:41 -05:00
//console.log("(scrub)Scrubbing info from contextualized request");
2019-10-06 15:18:10 -04:00
try {
2020-12-03 12:20:43 -05:00
var headerScrub = function(context) {
var ua = 'MYOB/6.66 (AN/ON)';
2019-10-06 15:18:10 -04:00
if (!context) {
} else if (context.name == titlepref) {
if (i2pHost(requestDetails.url)) {
for (var header of requestDetails.requestHeaders) {
2020-12-03 12:20:43 -05:00
if (header.name.toLowerCase() === 'user-agent') {
header.value = ua;
2020-12-03 12:20:43 -05:00
console.log('(scrub)User-Agent header modified', header.value);
}
2019-10-06 15:18:10 -04:00
}
}
return {
2020-11-10 21:48:08 -05:00
requestHeaders: requestDetails.requestHeaders,
};
} else if (context.name == routerpref) {
if (i2pHost(requestDetails.url)) {
for (var header of requestDetails.requestHeaders) {
2020-12-03 12:20:43 -05:00
if (header.name.toLowerCase() === 'user-agent') {
header.value = ua;
2020-12-03 12:20:43 -05:00
console.log('(scrub)User-Agent header modified', header.value);
}
}
}
return {
2020-11-10 21:48:08 -05:00
requestHeaders: requestDetails.requestHeaders,
};
2019-10-06 15:18:10 -04:00
}
};
2020-12-03 12:20:43 -05:00
var contextGet = async function(tabInfo) {
2019-10-06 15:18:10 -04:00
try {
2020-01-14 23:31:41 -05:00
//console.log("(scrub)Tab info from Function", tabInfo);
2019-12-24 20:58:53 -05:00
let context = await browser.contextualIdentities.get(
tabInfo.cookieStoreId
);
2019-10-06 15:18:10 -04:00
return context;
} catch (error) {
return undefined;
2019-10-06 15:18:10 -04:00
}
};
2020-12-03 12:20:43 -05:00
var tabGet = async function(tabId) {
2019-10-06 15:18:10 -04:00
try {
2020-01-14 23:31:41 -05:00
//console.log("(scrub)Tab ID from Request", tabId);
2019-10-06 15:18:10 -04:00
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
return undefined;
2019-10-06 15:18:10 -04:00
}
};
if (requestDetails.tabId > 0) {
var tab = {};
var context = {};
var req = {};
2019-10-08 19:10:13 -04:00
if (i2pHost(requestDetails.url)) {
2020-01-14 23:31:41 -05:00
//console.log("(scrub)I2P URL detected, ");
tab = tabGet(requestDetails.tabId);
context = tab.then(contextGet, onHeaderError);
req = await context.then(headerScrub, onHeaderError);
2020-01-14 23:31:41 -05:00
//console.log("(scrub)Scrubbing I2P Request", req);
return req;
2019-11-11 16:34:41 -05:00
} else if (routerHost(requestDetails.url)) {
tab = tabGet(requestDetails.tabId);
context = tab.then(contextGet, onHeaderError);
req = await context.then(headerScrub, onHeaderError);
2020-01-14 23:31:41 -05:00
//console.log("(scrub)Scrubbing non-I2P Request", req);
return req;
}
2019-11-11 16:34:41 -05:00
return req;
}
2019-10-06 15:18:10 -04:00
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(scrub)Not scrubbing non-I2P request.', error);
}
2020-12-03 12:20:43 -05:00
}
2020-12-03 12:20:43 -05:00
var notMyContextNotMyProblem = async function() {
2020-03-02 17:35:49 -05:00
var contexts = await browser.contextualIdentities.query({});
2020-03-02 17:34:39 -05:00
var context1 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: titlepref,
2020-03-02 17:34:39 -05:00
});
var context2 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: routerpref,
2020-03-02 17:34:39 -05:00
});
var context3 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: mailpref,
2020-03-02 17:34:39 -05:00
});
var context4 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: torrentpref,
2020-03-02 17:34:39 -05:00
});
var context5 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: tunnelpref,
2020-03-02 17:34:39 -05:00
});
var context6 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: ircpref,
2020-03-02 17:34:39 -05:00
});
var context7 = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: muwirepref,
});
var context7 = await browser.contextualIdentities.query({
name: botepref,
});
2020-03-02 17:35:49 -05:00
var othercontexts = [];
2020-12-03 12:20:43 -05:00
console.log('Contexts:', contexts);
2020-03-02 17:34:39 -05:00
for (context in contexts) {
2020-03-02 17:35:49 -05:00
if (
contexts[context].cookieStoreId == context1[0].cookieStoreId ||
contexts[context].cookieStoreId == context2[0].cookieStoreId ||
contexts[context].cookieStoreId == context3[0].cookieStoreId ||
contexts[context].cookieStoreId == context4[0].cookieStoreId ||
contexts[context].cookieStoreId == context5[0].cookieStoreId ||
contexts[context].cookieStoreId == context6[0].cookieStoreId ||
contexts[context].cookieStoreId == context7[0].cookieStoreId
2020-03-02 17:35:49 -05:00
) {
console.log(
2020-12-03 12:20:43 -05:00
'Context found',
2020-03-02 17:35:49 -05:00
contexts[context].cookieStoreId,
2020-12-03 12:20:43 -05:00
'is my responsibility'
2020-03-02 17:35:49 -05:00
);
} else {
//console.log("Context found", contexts[context].cookieStoreId, "is not my responsibility")
othercontexts.push(contexts[context]);
2020-03-02 17:34:39 -05:00
}
}
2020-03-02 17:35:49 -05:00
return othercontexts;
2020-12-03 12:20:43 -05:00
}
2020-03-02 17:34:39 -05:00
2020-12-03 12:20:43 -05:00
var contextSetup = function(requestDetails) {
function onContextError() {
2020-12-03 12:20:43 -05:00
console.log('Context launcher error');
}
2020-12-05 22:06:15 -05:00
console.log('(scrub) host', requestDetails.url);
2020-01-14 23:31:41 -05:00
//console.log("(isolate)Forcing I2P requests into context");
try {
2020-12-03 12:20:43 -05:00
var i2pTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: titlepref,
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
function onCreated(tab) {
function closeOldTab() {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
}
2020-02-26 13:03:09 -05:00
browser.pageAction.setPopup({
2020-03-05 23:25:08 -05:00
tabId: tabId.id,
2020-12-03 12:20:43 -05:00
popup: 'security.html',
2020-02-26 13:03:09 -05:00
});
2020-02-26 13:00:51 -05:00
browser.pageAction.show(tabId.id);
2019-11-27 00:58:53 -05:00
}
closeOldTab(tab);
2019-10-08 19:10:13 -04:00
}
var created = browser.tabs.create({
2019-10-08 19:10:13 -04:00
active: true,
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
2019-10-08 19:10:13 -04:00
});
created.then(onCreated, onContextError);
2019-10-08 19:10:13 -04:00
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
2019-10-08 19:10:13 -04:00
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
2019-10-08 19:10:13 -04:00
}
};
2020-12-03 12:20:43 -05:00
var routerTabFind = async function(tabId) {
2019-10-08 19:10:13 -04:00
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: routerpref,
2019-10-08 19:10:13 -04:00
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
2019-11-11 16:34:41 -05:00
function onCreated(tab) {
2020-03-17 04:42:24 -04:00
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-11-10 21:48:08 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
2020-03-17 04:42:24 -04:00
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
2019-11-27 00:58:53 -05:00
}
2020-03-17 04:42:24 -04:00
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-03-17 04:42:24 -04:00
});
pins.then(closeOldTab, onError);
2019-11-11 16:34:41 -05:00
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/home';
}
2019-11-11 16:34:41 -05:00
var created = browser.tabs.create({
active: true,
2020-03-17 04:42:24 -04:00
pinned: true,
2019-11-11 16:34:41 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
2019-11-11 16:34:41 -05:00
});
created.then(onCreated, onContextError);
2019-11-11 16:34:41 -05:00
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
2019-11-11 16:34:41 -05:00
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
2019-11-11 16:34:41 -05:00
}
};
2020-12-03 12:20:43 -05:00
var i2ptunnelTabFind = async function(tabId) {
2019-11-11 16:34:41 -05:00
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: tunnelpref,
2019-11-11 16:34:41 -05:00
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
2019-11-11 16:34:41 -05:00
function onCreated(tab) {
2020-03-17 04:42:24 -04:00
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
2020-03-17 04:42:24 -04:00
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
2019-11-27 00:58:53 -05:00
}
2020-03-17 04:42:24 -04:00
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-03-17 04:42:24 -04:00
});
pins.then(closeOldTab, onError);
2019-11-11 16:34:41 -05:00
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/i2ptunnelmgr/';
}
2019-11-11 16:34:41 -05:00
var created = browser.tabs.create({
active: true,
2020-03-17 04:42:24 -04:00
pinned: true,
2019-11-11 16:34:41 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
2019-11-11 16:34:41 -05:00
});
created.then(onCreated, onContextError);
2019-11-11 16:34:41 -05:00
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
2019-11-11 16:34:41 -05:00
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
2019-11-11 16:34:41 -05:00
}
};
2020-12-03 12:20:43 -05:00
var snarkTabFind = async function(tabId) {
2019-11-11 16:34:41 -05:00
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: torrentpref,
2019-11-11 16:34:41 -05:00
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
var exemptContext = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: titlepref,
});
let tmp = new URL(tabId.url);
2020-12-03 12:20:43 -05:00
console.log('(isolate)tabid host', tmp.host);
if (!requestDetails.url.includes('snark/' + tmp.host)) {
2020-11-10 21:48:08 -05:00
// if (tabId.cookieStoreId != exemptContext[0].cookieStoreId){
function Create() {
function onCreated(tab) {
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
2020-03-17 04:42:24 -04:00
}
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
});
pins.then(closeOldTab, onError);
2019-11-27 00:58:53 -05:00
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/i2psnark/';
}
var created = browser.tabs.create({
active: true,
pinned: true,
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
2020-03-17 04:42:24 -04:00
});
created.then(onCreated, onContextError);
2019-11-11 16:34:41 -05:00
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
2019-11-11 16:34:41 -05:00
}
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
2019-11-11 16:34:41 -05:00
}
};
2020-12-03 12:20:43 -05:00
var muwireTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: muwirepref,
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
function onCreated(tab) {
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
}
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
});
pins.then(closeOldTab, onError);
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/muwire/';
}
var created = browser.tabs.create({
active: true,
pinned: true,
cookieStoreId: context[0].cookieStoreId,
url: requestDetails.url,
});
created.then(onCreated, onContextError);
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
}
};
2020-12-03 12:20:43 -05:00
var i2pboteTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
name: botepref,
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
function onCreated(tab) {
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
}
var pins = browser.tabs.query({
cookieStoreId: context[0].cookieStoreId,
});
pins.then(closeOldTab, onError);
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/i2pbote/';
}
var created = browser.tabs.create({
active: true,
pinned: true,
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
});
created.then(onCreated, onContextError);
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
}
};
2020-12-03 12:20:43 -05:00
var mailTabFind = async function(tabId) {
2019-11-11 16:34:41 -05:00
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: mailpref,
2019-11-11 16:34:41 -05:00
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
2019-10-08 19:10:13 -04:00
function onCreated(tab) {
2020-03-17 04:42:24 -04:00
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
}
2020-03-17 04:42:24 -04:00
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
2019-11-27 00:58:53 -05:00
}
2020-03-17 04:42:24 -04:00
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-03-17 04:42:24 -04:00
});
pins.then(closeOldTab, onError);
}
2020-12-03 12:20:43 -05:00
if (requestDetails.url.endsWith('xhr1.html')) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
requestDetails.url = prefix + hostname + '/webmail/';
}
var created = browser.tabs.create({
active: true,
2020-03-17 04:42:24 -04:00
pinned: true,
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
});
created.then(onCreated, onContextError);
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
}
};
2020-12-03 12:20:43 -05:00
var ircTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: ircpref,
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
2020-12-03 12:20:43 -05:00
if (requestDetails.url.includes(':7669')) {
2020-11-10 20:59:04 -05:00
function Create() {
function onCreated(tab) {
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
2020-12-03 12:20:43 -05:00
console.log('(isolate) Closing un-isolated tab', tabId.id);
console.log('in favor of', tab.id);
console.log('with context', tab.cookieStoreId);
2020-11-10 20:59:04 -05:00
browser.tabs.remove(tabId.id);
2020-12-05 22:06:15 -05:00
browser.tabs.move(tab.id, { index: 0 });
2020-11-10 20:59:04 -05:00
}
for (index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1)
browser.tabs.remove(tabs[index].id);
}
}
2020-11-10 20:59:04 -05:00
var pins = browser.tabs.query({
2020-11-10 21:48:08 -05:00
cookieStoreId: context[0].cookieStoreId,
2020-11-10 20:59:04 -05:00
});
pins.then(closeOldTab, onError);
}
2020-11-10 20:59:04 -05:00
var created = browser.tabs.create({
active: true,
pinned: true,
cookieStoreId: context[0].cookieStoreId,
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
2020-11-10 20:59:04 -05:00
});
created.then(onCreated, onContextError);
}
2020-11-10 20:59:04 -05:00
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
}
2020-11-10 20:59:04 -05:00
};
2020-12-03 12:20:43 -05:00
var normalTabFind = async function(tabId) {
if (tabId == undefined) {
return;
}
2019-11-10 22:24:10 -05:00
try {
var anoncontext = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: titlepref,
});
2020-11-10 20:59:04 -05:00
var irccontext = await browser.contextualIdentities.query({
2020-11-10 21:48:08 -05:00
name: ircpref,
2019-11-10 22:24:10 -05:00
});
2020-03-02 17:34:39 -05:00
var othercontexts = await notMyContextNotMyProblem();
var nmp = false;
2020-03-02 17:35:49 -05:00
for (context in othercontexts) {
if (tabId.cookieStoreId == othercontexts[context].cookieStoreId) {
2020-12-03 12:20:43 -05:00
console.log('Not my problem');
2020-03-02 17:35:49 -05:00
nmp = true;
}
2020-03-02 17:34:39 -05:00
}
2019-11-11 16:34:41 -05:00
if (
2020-12-03 12:20:43 -05:00
tabId.cookieStoreId == 'firefox-default' ||
tabId.cookieStoreId == 'firefox-private' ||
tabId.cookieStoreId == anoncontext[0].cookieStoreId ||
2020-11-10 20:59:04 -05:00
tabId.cookieStoreId == irccontext[0].cookieStoreId ||
2020-03-02 17:34:39 -05:00
nmp
) {
console.log(
2020-12-03 12:20:43 -05:00
'(ISOLATE)',
tabId.cookieStoreId,
2020-12-03 12:20:43 -05:00
'not',
anoncontext[0].cookieStoreId,
2020-11-10 20:59:04 -05:00
irccontext[0].cookieStoreId
);
return;
} else {
function Create() {
function onCreated(tab) {
function closeOldTab() {
if (
tabId.id != tab.id &&
tabId.cookieStoreId != tab.cookieStoreId
) {
console.log(
2020-12-03 12:20:43 -05:00
'(isolate) Closing isolated tab',
tabId.id,
2020-12-03 12:20:43 -05:00
'with context',
tabId.cookieStoreId
);
console.log(
2020-12-03 12:20:43 -05:00
'(isolate) in favor of',
tab.id,
2020-12-03 12:20:43 -05:00
'with context',
tab.cookieStoreId
);
browser.tabs.remove(tabId.id);
2019-11-27 00:58:53 -05:00
}
}
closeOldTab(tab);
2019-11-10 22:24:10 -05:00
}
var created = browser.tabs.create({
active: true,
2020-12-03 12:20:43 -05:00
cookieStoreId: 'firefox-default',
2020-11-10 21:48:08 -05:00
url: requestDetails.url,
});
created.then(onCreated, onContextError);
2019-11-10 22:24:10 -05:00
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
2019-11-10 22:24:10 -05:00
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Context Error', error);
2019-11-10 22:24:10 -05:00
}
};
2020-12-03 12:20:43 -05:00
var tabGet = async function(tabId) {
try {
2020-01-14 23:31:41 -05:00
//console.log("(isolate)Tab ID from Request", tabId);
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Tab error', error);
}
};
if (requestDetails == undefined) {
return requestDetails;
}
if (proxyHost(requestDetails.url)) {
setcookie = browser.cookies.set({
firstPartyDomain: i2pHostName(requestDetails.url),
url: requestDetails.url,
2020-11-10 21:48:08 -05:00
secure: true,
});
setcookie.then(onContextGotLog, onContextError);
return requestDetails;
}
2020-01-14 10:26:35 -05:00
if (requestDetails.tabId > 0) {
var tab = tabGet(requestDetails.tabId);
2019-10-08 19:10:13 -04:00
if (i2pHost(requestDetails.url)) {
2020-12-05 22:06:15 -05:00
var thn = i2pHostName(requestDetails.url);
if (requestDetails.url.includes('=' + thn)) {
console.log('(scrub)checking search hostnames =' + thn);
var tpt = requestDetails.url.split('=' + thn, 2);
2020-12-05 23:53:24 -05:00
requestDetails.url =
'http://' + thn + '/' + tpt[1].replace('%2F', '');
2020-12-05 22:06:15 -05:00
}
2020-12-05 23:53:24 -05:00
console.log('(scrub) new hostname', requestDetails.url);
var setcookie = browser.cookies.set({
firstPartyDomain: i2pHostName(requestDetails.url),
url: requestDetails.url,
2020-11-10 21:48:08 -05:00
secure: true,
});
setcookie.then(onContextGotLog, onContextError);
var i2ptab = tab.then(i2pTabFind, onContextError);
return requestDetails;
2019-10-08 19:10:13 -04:00
}
2020-01-14 10:26:35 -05:00
if (extensionHost(requestDetails)) {
return requestDetails;
}
let localhost = localHost(requestDetails.url);
2019-11-11 16:34:41 -05:00
let routerhost = routerHost(requestDetails.url);
if (routerhost) {
2020-12-03 12:20:43 -05:00
if (routerhost === 'i2ptunnelmgr') {
var tunneltab = tab.then(i2ptunnelTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
2020-12-03 12:20:43 -05:00
} else if (routerhost === 'i2psnark') {
var snarktab = tab.then(snarkTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
2020-12-03 12:20:43 -05:00
} else if (routerhost === 'webmail') {
var mailtab = tab.then(mailTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
2020-12-03 12:20:43 -05:00
} else if (routerhost === 'muwire') {
var routertab = tab.then(muwireTabFind, onContextError);
return requestDetails;
2020-12-03 12:20:43 -05:00
} else if (routerhost === 'i2pbote') {
var routertab = tab.then(i2pboteTabFind, onContextError);
return requestDetails;
2020-12-03 12:20:43 -05:00
} else if (routerhost === 'routerconsole') {
var routertab = tab.then(routerTabFind, onContextError);
return requestDetails;
2020-03-24 18:53:27 -04:00
}
2019-11-11 16:34:41 -05:00
} else {
2020-11-10 20:59:04 -05:00
if (localhost) {
var irctab = tab.then(ircTabFind, onContextError);
return requestDetails;
2020-11-10 20:59:04 -05:00
}
var normalTab = tab.then(normalTabFind, onContextError);
return requestDetails;
//return requestDetails;
}
}
} catch (error) {
2020-12-03 12:20:43 -05:00
console.log('(isolate)Not an I2P request, blackholing', error);
2019-10-06 15:18:10 -04:00
}
};
2020-12-03 12:20:43 -05:00
var coolheadersSetup = function(e) {
2020-09-15 19:01:05 -04:00
var asyncSetPageAction = new Promise((resolve, reject) => {
window.setTimeout(() => {
for (i = 0; i < e.responseHeaders.length; i++) {
let header = e.responseHeaders[i];
2020-12-03 12:20:43 -05:00
if (e.url.startsWith('https')) {
2020-11-10 21:48:08 -05:00
if (
2020-12-03 12:20:43 -05:00
header.name.toUpperCase() === 'I2P-LOCATION' ||
header.name.toUpperCase() === 'X-I2P-LOCATION'
2020-11-10 21:48:08 -05:00
) {
2020-09-25 23:20:30 -04:00
browser.pageAction.setPopup({
tabId: e.tabId,
2020-12-03 12:20:43 -05:00
popup: 'location.html',
2020-11-10 21:48:08 -05:00
});
browser.pageAction.setIcon({
2020-12-03 12:20:43 -05:00
path: 'icons/i2plogo.png',
2020-11-10 21:48:08 -05:00
tabId: e.tabId,
2020-09-25 23:20:30 -04:00
});
browser.pageAction.setTitle({
tabId: e.tabId,
2020-11-10 21:48:08 -05:00
title: header.value,
2020-09-25 23:20:30 -04:00
});
browser.pageAction.show(e.tabId);
break;
}
2020-11-10 21:48:08 -05:00
if (
2020-12-03 12:20:43 -05:00
header.name.toUpperCase() === 'I2P-TORRENTLOCATION' ||
header.name.toUpperCase() === 'X-I2P-TORRENTLOCATION'
2020-11-10 21:48:08 -05:00
) {
2020-12-03 12:20:43 -05:00
var imgs = document.getElementsByTagName('img');
for (let img of imgs) {
if (tmpsrc.host == location.host) {
2020-11-10 21:48:08 -05:00
img.src =
2020-12-03 12:20:43 -05:00
'http://127.0.0.1:7657/i2psnark/' +
2020-11-10 21:48:08 -05:00
tmpsrc.host +
tmpsrc.pathname;
2020-12-03 12:20:43 -05:00
img.onerror = function() {
2020-10-08 00:25:38 -04:00
img.src = tmpsrc;
};
}
}
2020-12-03 12:20:43 -05:00
var videos = document.getElementsByTagName('video');
for (let video of videos) {
let tmpsrc = new URL(video.currentSrc);
if (tmpsrc.host == location.host) {
2020-12-03 12:20:43 -05:00
if (!video.innerHTML.includes('127.0.0.1')) {
2020-12-06 13:01:02 -05:00
innerHTML = video.innerHTML;
topInnerHTML = video.innerHTML.replace(
'src="',
'src="http://127.0.0.1:7657/i2psnark/' + location.host + '/'
);
video.innerHTML = topInnerHTML; // + innerHTML;
video.onerror = function() {
video.innerHTML = topInnerHTML + innerHTML;
};
}
}
}
2020-12-03 12:20:43 -05:00
var audios = document.getElementsByTagName('audio');
for (let audio of audios) {
let tmpsrc = new URL(audio.currentSrc);
if (tmpsrc.host == location.host) {
2020-12-03 12:20:43 -05:00
if (!audio.innerHTML.includes('127.0.0.1')) {
innerHTML = audio.innerHTML;
2020-11-10 21:48:08 -05:00
topInnerHTML = audio.innerHTML.replace(
'src="',
2020-12-03 12:20:43 -05:00
'src="http://127.0.0.1:7657/i2psnark/' + location.host + '/'
2020-11-10 21:48:08 -05:00
);
audio.innerHTML = topInnerHTML; // + innerHTML;
audio.onerror = function() {
audio.innerHTML = topInnerHTML + innerHTML;
};
}
}
}
2020-09-25 23:20:30 -04:00
browser.pageAction.setPopup({
tabId: tabId.id,
2020-12-03 12:20:43 -05:00
popup: 'torrent.html',
2020-11-10 21:48:08 -05:00
});
browser.pageAction.setIcon({
2020-12-03 12:20:43 -05:00
path: 'icons/i2plogo.png',
2020-11-10 21:48:08 -05:00
tabId: e.tabId,
2020-09-25 23:20:30 -04:00
});
browser.pageAction.setTitle({
tabId: e.tabId,
2020-11-10 21:48:08 -05:00
title: header.value,
2020-09-25 23:20:30 -04:00
});
browser.pageAction.show(e.tabId);
2020-09-25 23:20:30 -04:00
break;
}
2020-11-10 21:48:08 -05:00
} else {
if (
2020-12-03 12:20:43 -05:00
header.name.toUpperCase() === 'I2P-TORRENTLOCATION' ||
header.name.toUpperCase() === 'X-I2P-TORRENTLOCATION'
2020-11-10 21:48:08 -05:00
) {
2020-12-03 12:20:43 -05:00
var imgs = document.getElementsByTagName('img');
for (let img of imgs) {
if (tmpsrc.host == location.host) {
2020-11-10 21:48:08 -05:00
img.src =
2020-12-03 12:20:43 -05:00
'http://127.0.0.1:7657/i2psnark/' +
2020-11-10 21:48:08 -05:00
tmpsrc.host +
tmpsrc.pathname;
2020-12-03 12:20:43 -05:00
img.onerror = function() {
2020-10-08 00:25:38 -04:00
img.src = tmpsrc;
};
}
}
2020-12-03 12:20:43 -05:00
var videos = document.getElementsByTagName('video');
for (let video of videos) {
let tmpsrc = new URL(video.currentSrc);
if (tmpsrc.host == location.host) {
2020-12-03 12:20:43 -05:00
if (!video.innerHTML.includes('127.0.0.1')) {
2020-12-06 13:01:02 -05:00
innerHTML = video.innerHTML;
topInnerHTML = video.innerHTML.replace(
'src="',
'src="http://127.0.0.1:7657/i2psnark/' + location.host + '/'
);
video.innerHTML = topInnerHTML; // + innerHTML;
video.onerror = function() {
video.innerHTML = topInnerHTML + innerHTML;
};
}
}
}
2020-12-03 12:20:43 -05:00
var audios = document.getElementsByTagName('audio');
for (let audio of audios) {
let tmpsrc = new URL(audio.currentSrc);
if (tmpsrc.host == location.host) {
2020-12-03 12:20:43 -05:00
if (!audio.innerHTML.includes('127.0.0.1')) {
2020-12-06 13:01:02 -05:00
innerHTML = audio.innerHTML;
topInnerHTML = audio.innerHTML.replace(
'src="',
'src="http://127.0.0.1:7657/i2psnark/' + location.host + '/'
);
audio.innerHTML = topInnerHTML; // + innerHTML;
audio.onerror = function() {
audio.innerHTML = topInnerHTML + innerHTML;
};
}
}
}
browser.pageAction.setPopup({
tabId: tabId.id,
2020-12-03 12:20:43 -05:00
popup: 'torrent.html',
2020-11-10 21:48:08 -05:00
});
browser.pageAction.setIcon({
2020-12-03 12:20:43 -05:00
path: 'icons/i2plogo.png',
2020-11-10 21:48:08 -05:00
tabId: e.tabId,
});
browser.pageAction.setTitle({
tabId: e.tabId,
2020-11-10 21:48:08 -05:00
title: header.value,
});
browser.pageAction.show(e.tabId);
break;
}
}
}
2020-11-10 21:48:08 -05:00
resolve({ responseHeaders: e.responseHeaders });
}, 2000);
});
2020-09-15 19:01:05 -04:00
return asyncSetPageAction;
2020-11-10 21:48:08 -05:00
};
function getTabURL(tab) {
console.log("(scrub)", tab);
if (tab.url.startsWith("https")) {
browser.tabs
.sendMessage(tab.id, { req: "i2p-location" })
.then((response) => {
if (response.content.toUpperCase() != "NO-ALT-LOCATION") {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "location.html",
});
browser.pageAction.setIcon({
path: "icons/i2plogo.png",
tabId: tab.id,
});
browser.pageAction.setTitle({
tabId: tab.id,
title: response.content,
});
browser.pageAction.show(tab.id);
}
});
console.log("(pageaction)", tab.id, tab.url);
} else {
browser.tabs
.sendMessage(tab.id, { req: "i2p-torrentlocation" })
.then((response) => {
if (response.content.toUpperCase() != "NO-ALT-LOCATION") {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "torrent.html",
});
browser.pageAction.setIcon({
path: "icons/i2plogo.png",
tabId: tab.id,
});
browser.pageAction.setTitle({
tabId: tab.id,
title: response.content,
});
browser.pageAction.show(tab.id);
}
});
console.log("(pageaction)", tab.id, tab.url);
2020-09-24 20:06:24 -04:00
}
}
function getClearTab(tobj) {
2020-11-10 21:48:08 -05:00
if (typeof tobj == "number") {
browser.tabs.get(tobj).then(getTabURL, onError);
}
if (typeof tobj.tabId == "number") {
console.log("(scrub) tobj", tobj);
2020-11-10 21:48:08 -05:00
browser.tabs.get(tobj.tabId).then(getTabURL, onError);
} else {
for (let tab in tobj.tabIds) {
console.log("(scrub) tab", tobj, tab, tobj.tabIds[tab]);
browser.tabs.get(tobj.tabIds[tab]).then(getTabURL, onError);
}
}
2020-09-24 20:06:24 -04:00
}
const filter = {
url: [{ hostContains: ".i2p" }],
};
function logOnDOMContentLoaded(details) {
console.log(`onDOMContentLoaded: ${details.url}`);
}
browser.webNavigation.onDOMContentLoaded.addListener(getClearTab, filter);
2020-09-24 20:06:24 -04:00
browser.tabs.onActivated.addListener(getClearTab);
browser.tabs.onUpdated.addListener(getClearTab);
browser.tabs.onAttached.addListener(getClearTab);
browser.tabs.onCreated.addListener(getClearTab);
browser.tabs.onDetached.addListener(getClearTab);
browser.tabs.onHighlighted.addListener(getClearTab);
browser.tabs.onMoved.addListener(getClearTab);
browser.tabs.onRemoved.addListener(getClearTab);
browser.tabs.onReplaced.addListener(getClearTab);
browser.tabs.onZoomChange.addListener(getClearTab);
function reloadTabs(tabs) {
for (let tab of tabs) {
2020-11-10 21:48:08 -05:00
browser.tabs.reload(tab.id);
}
}
function reloadError(error) {
console.log(`Error: ${error}`);
}
let querying = browser.tabs.query({});
querying.then(reloadTabs, onError);
// Listen for onHeaderReceived for the target page.
// Set "blocking" and "responseHeaders".
browser.webRequest.onHeadersReceived.addListener(
coolheadersSetup,
2020-11-10 21:48:08 -05:00
{ urls: ["<all_urls>"] },
["responseHeaders"]
);
browser.webNavigation.onDOMContentLoaded.addListener(getClearTab, filter);
browser.webNavigation.onBeforeNavigate.addListener(getClearTab, filter);
browser.webNavigation.onCommitted.addListener(getClearTab, filter);
browser.webNavigation.onCompleted.addListener(getClearTab, filter);
browser.webRequest.onBeforeRequest.addListener(
contextSetup,
2020-11-10 21:48:08 -05:00
{ urls: ["<all_urls>"] },
["blocking"]
);
browser.webRequest.onBeforeSendHeaders.addListener(
2019-10-06 15:18:10 -04:00
contextScrub,
2020-11-10 21:48:08 -05:00
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);