Files
I2P_in_Private_Browsing_Mod…/scrub.js

440 lines
15 KiB
JavaScript
Raw Normal View History

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 localpref = chrome.i18n.getMessage("localPreface");
var extensionpref = chrome.i18n.getMessage("extensionPreface");
var contextScrub = async function(requestDetails) {
function onHeaderError() {
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 {
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) {
if (header.name.toLowerCase() === "user-agent") {
header.value = ua;
console.log("(scrub)User-Agent header modified", header.value);
}
2019-10-06 15:18:10 -04:00
}
}
return {
requestHeaders: requestDetails.requestHeaders
};
} else if (context.name == routerpref) {
if (i2pHost(requestDetails.url)) {
for (var header of requestDetails.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = ua;
console.log("(scrub)User-Agent header modified", header.value);
}
}
}
return {
requestHeaders: requestDetails.requestHeaders
};
2019-10-06 15:18:10 -04:00
}
};
var contextGet = async function(tabInfo) {
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
}
};
var tabGet = async function(tabId) {
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) {
console.log("(scrub)Not scrubbing non-I2P request.", error);
}
};
var contextSetup = function(requestDetails) {
function onContextError() {
console.log("Context launcher error");
}
2020-01-14 23:31:41 -05:00
//console.log("(isolate)Forcing I2P requests into context");
try {
var i2pTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
name: titlepref
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
function onCreated(tab) {
function closeOldTab() {
if (tabId.id != tab.id) {
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:00:51 -05:00
browser.pageAction.setPopup({tabId: tabId[0].id, popup: "security.html"})
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,
2019-11-27 00:34:48 -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) {
console.log("(isolate)Context Error", error);
2019-10-08 19:10:13 -04:00
}
};
var routerTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
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) {
function closeOldTab() {
if (tabId.id != tab.id) {
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);
}
2019-11-27 00:58:53 -05:00
}
closeOldTab(tab);
2019-11-11 16:34:41 -05:00
}
var created = browser.tabs.create({
active: true,
cookieStoreId: context[0].cookieStoreId,
2019-11-27 00:34:48 -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) {
console.log("(isolate)Context Error", error);
2019-11-11 16:34:41 -05:00
}
};
var i2ptunnelTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
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) {
function closeOldTab() {
if (tabId.id != tab.id) {
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);
}
2019-11-27 00:58:53 -05:00
}
closeOldTab(tab);
2019-11-11 16:34:41 -05:00
}
var created = browser.tabs.create({
active: true,
cookieStoreId: context[0].cookieStoreId,
2019-11-27 00:34:48 -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) {
console.log("(isolate)Context Error", error);
2019-11-11 16:34:41 -05:00
}
};
var snarkTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
name: torrentpref
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) {
function closeOldTab() {
if (tabId.id != tab.id) {
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);
}
2019-11-27 00:58:53 -05:00
}
closeOldTab(tab);
2019-11-11 16:34:41 -05:00
}
var created = browser.tabs.create({
active: true,
cookieStoreId: context[0].cookieStoreId,
2019-11-27 00:34:48 -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) {
console.log("(isolate)Context Error", error);
2019-11-11 16:34:41 -05:00
}
};
var mailTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
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) {
function closeOldTab() {
if (tabId.id != tab.id) {
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);
}
2019-11-27 00:58:53 -05:00
}
closeOldTab(tab);
}
var created = browser.tabs.create({
active: true,
cookieStoreId: context[0].cookieStoreId,
2019-11-27 00:34:48 -05:00
url: requestDetails.url
});
created.then(onCreated, onContextError);
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
} catch (error) {
console.log("(isolate)Context Error", error);
}
};
var localTabFind = async function(tabId) {
try {
var context = await browser.contextualIdentities.query({
name: localpref
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create() {
function onCreated(tab) {
function closeOldTab() {
if (tabId.id != tab.id) {
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);
}
}
closeOldTab(tab);
}
var created = browser.tabs.create({
active: 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) {
console.log("(isolate)Context Error", error);
}
};
var normalTabFind = async function(tabId) {
if (tabId == undefined) {
return;
}
2019-11-10 22:24:10 -05:00
try {
var anoncontext = await browser.contextualIdentities.query({
name: titlepref
});
var localcontext = await browser.contextualIdentities.query({
name: localpref
2019-11-10 22:24:10 -05:00
});
2019-11-11 16:34:41 -05:00
if (
tabId.cookieStoreId == "firefox-default" ||
tabId.cookieStoreId == "firefox-private" ||
tabId.cookieStoreId == anoncontext[0].cookieStoreId ||
tabId.cookieStoreId == localcontext[0].cookieStoreId
) {
console.log(
"(ISOLATE)",
tabId.cookieStoreId,
"not",
anoncontext[0].cookieStoreId,
localcontext[0].cookieStoreId
);
return;
} else {
function Create() {
function onCreated(tab) {
function closeOldTab() {
if (
tabId.id != tab.id &&
tabId.cookieStoreId != tab.cookieStoreId
) {
console.log(
"(isolate) Closing isolated tab",
tabId.id,
"with context",
tabId.cookieStoreId
);
console.log(
"(isolate) in favor of",
tab.id,
"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,
cookieStoreId: "firefox-default",
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) {
console.log("(isolate)Context Error", error);
2019-11-10 22:24:10 -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) {
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,
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)) {
var setcookie = browser.cookies.set({
firstPartyDomain: i2pHostName(requestDetails.url),
url: requestDetails.url,
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) {
if (routerhost === "i2ptunnelmgr") {
var tunneltab = tab.then(i2ptunnelTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
} else if (routerhost === "i2psnark") {
var snarktab = tab.then(snarkTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
} else if (routerhost === "webmail") {
var mailtab = tab.then(mailTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
} else if (routerhost === "routerconsole") {
var routertab = tab.then(routerTabFind, onContextError);
2019-11-11 16:34:41 -05:00
return requestDetails;
}
} else {
if (localhost) {
var localtab = tab.then(localTabFind, onContextError);
return requestDetails;
}
var normalTab = tab.then(normalTabFind, onContextError);
return requestDetails;
//return requestDetails;
}
}
} catch (error) {
console.log("(isolate)Not an I2P request, blackholing", error);
2019-10-06 15:18:10 -04:00
}
};
browser.webRequest.onBeforeRequest.addListener(
contextSetup,
{ urls: ["<all_urls>"] },
["blocking"]
);
browser.webRequest.onBeforeSendHeaders.addListener(
2019-10-06 15:18:10 -04:00
contextScrub,
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);