Files
I2P_in_Private_Browsing_Mod…/scrub.js

645 lines
19 KiB
JavaScript
Raw Normal View History

2023-05-25 03:49:17 +00:00
/* eslint-disable max-len */
2022-10-24 19:57:51 -04: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");
var blogpref = chrome.i18n.getMessage("blogPreface");
var blogprefpriv = chrome.i18n.getMessage("blogPrefacePrivate");
var torpref = chrome.i18n.getMessage("torPreface");
var torprefpriv = chrome.i18n.getMessage("torPrefacePrivate");
2023-05-25 03:49:17 +00:00
function contextScrub(requestDetails) {
function handleHeaderError() {
// log error message
console.log("Error: Header could not be scrubbed");
}
2023-05-25 03:49:17 +00:00
function headerScrub() {
const titlePrefix = "myob";
const userAgent = "MYOB/6.66 (AN/ON)";
if (requestDetails && requestDetails.name === titlePrefix) {
for (const header of requestDetails.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = userAgent;
2022-10-08 17:21:55 -04:00
}
}
2023-05-25 03:49:17 +00:00
return { requestHeaders: requestDetails.requestHeaders };
}
}
async function getContext(tabInfo) {
try {
const context = await browser.contextualIdentities.get(
tabInfo.cookieStoreId
);
return context;
} catch (error) {
return undefined;
}
}
try {
if (requestDetails.tabId > 0) {
2023-05-25 03:49:17 +00:00
let tab = getTab(requestDetails.tabId);
let context = tab.then(getContext, handleHeaderError);
let req = context.then(headerScrub, handleHeaderError);
return req;
}
} catch (error) {
2022-10-24 19:57:51 -04:00
console.log("(scrub)Not scrubbing non-I2P request.", error);
}
2023-05-25 03:49:17 +00:00
}
2023-05-25 03:49:17 +00:00
async function getTab(tabId) {
try {
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
return undefined;
}
2023-05-25 03:49:17 +00:00
}
2020-03-02 17:34:39 -05:00
2023-05-25 03:49:17 +00:00
function i2pTabFind(tabId) {
console.info("(isolate)Context Discovery browser", tabId);
try {
return forceIntoIsolation(tabId, titlepref, false);
} catch (error) {
console.error("(isolate)Context Error", error);
}
2023-05-25 03:49:17 +00:00
}
function routerTabFind(tabId) {
console.info("(isolate)Context Discovery console");
try {
return forceIntoIsolation(tabId, routerpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function i2ptunnelTabFind(tabId) {
console.info("(isolate)Context Discovery browser");
try {
return forceIntoIsolation(tabId, tunnelpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
async function snarkTabFind(tabId) {
console.info("(isolate)Context Discovery torrents");
try {
var context = await browser.contextualIdentities.query({
name: torrentpref
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
var exemptContext = await browser.contextualIdentities.query({
name: titlepref
});
2023-05-25 03:49:17 +00:00
let tmp = new URL(tabId.url);
console.log("(isolate)tabid host", tmp.host);
if (!requestDetails.url.includes("snark/" + tmp.host)) {
// if (tabId.cookieStoreId != exemptContext[0].cookieStoreId){
function Create() {
function onCreated(tab) {
function closeOldTab(tabs) {
if (tabId.id != tab.id) {
console.log(
2022-10-24 19:57:51 -04:00
"(isolate) Closing un-isolated tab",
tabId.id,
2022-10-24 19:57:51 -04:00
"in favor of",
tab.id,
2022-10-24 19:57:51 -04:00
"with context",
tab.cookieStoreId
);
browser.tabs.remove(tabId.id);
2023-05-25 03:49:17 +00:00
browser.tabs.move(tab.id, { index: 0 });
}
for (let index = 0; index < tabs.length; index++) {
if (index != tabs.length - 1) {
browser.tabs.remove(tabs[index].id);
}
}
}
var pins = browser.tabs.query({
2023-05-25 03:49:17 +00:00
cookieStoreId: context[0].cookieStoreId
});
2023-05-25 03:49:17 +00:00
pins.then(closeOldTab, onScrubError);
}
if (requestDetails.url.endsWith("xhr1.html")) {
let hostname = url.split("/")[2];
let prefix = url.substr(0, url.indexOf("://") + 3);
requestDetails.url = prefix + hostname + "/i2psnark/";
}
var created = browser.tabs.create({
active: true,
2023-05-25 03:49:17 +00:00
pinned: true,
cookieStoreId: context[0].cookieStoreId,
2023-05-25 03:49:17 +00:00
url: requestDetails.url
});
created.then(onCreated, onContextError);
}
var gettab = browser.tabs.get(tabId.id);
gettab.then(Create, onContextError);
return tabId;
}
}
2023-05-25 03:49:17 +00:00
} catch (error) {
console.log("(isolate)Context Error", error);
}
2023-05-25 03:49:17 +00:00
}
function muwireTabFind(tabId) {
console.info("(isolate)Context Discovery muwire");
try {
2023-05-25 03:49:17 +00:00
return forceIntoIsolation(tabId, muwirepref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function i2pboteTabFind(tabId) {
console.info("(isolate)Context Discovery bote");
try {
return forceIntoIsolation(tabId, botepref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function mailTabFind(tabId) {
console.info("(isolate)Context Discovery mail");
try {
return forceIntoIsolation(tabId, mailpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function ircTabFind(tabId) {
console.info("(isolate)Context Discovery irc");
try {
return forceIntoIsolation(tabId, ircpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function torTabFind(tabId) {
console.info("(isolate)Context Discovery tor");
try {
return forceIntoIsolation(tabId, torpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
function blogTabFind(tabId) {
console.info("(isolate)Context Discovery blog");
try {
return forceIntoIsolation(tabId, blogpref, true);
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
async function forceIntoIsolation(tabId, contextidentifier, pin = true) {
console.info("(isolate) forcing context for", tabId, contextidentifier, pin);
try {
var context = await browser.contextualIdentities.query({
name: contextidentifier
});
if (tabId.cookieStoreId != context[0].cookieStoreId) {
function Create(beforeTab) {
console.info("(isolate) isolating before tab:", beforeTab);
function onCreated(afterTab) {
console.info("(isolate) created isolated tab:", afterTab);
function closeOldTab(tabs) {
console.info("(isolate) cleaning up tab:", beforeTab);
console.info(
"(isolate) Closing un-isolated tab",
tabId.id,
"in favor of",
beforeTab.id,
"with context",
beforeTab.cookieStoreId
);
browser.tabs.remove(tabId.id);
if (pin) {
browser.tabs.move(beforeTab.id, { index: 0 });
for (let 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
}
}
}
2023-05-25 03:49:17 +00:00
browser.pageAction.setPopup({
tabId: tabId.id,
popup: "security.html"
});
browser.pageAction.show(tabId.id);
}
2023-05-25 03:49:17 +00:00
var pins = browser.tabs.query({
cookieStoreId: context[0].cookieStoreId
});
pins.then(closeOldTab, onScrubError);
return afterTab;
2022-10-07 19:32:52 -04:00
}
2023-05-25 03:49:17 +00:00
var created = browser.tabs.create({
active: true,
cookieStoreId: context[0].cookieStoreId,
url: beforeTab.url,
pinned: pin
});
2023-05-25 03:49:17 +00:00
return created.then(onCreated, onContextError);
}
2023-05-25 03:49:17 +00:00
var gettab = browser.tabs.get(tabId.id);
var tab = gettab.then(Create, onContextError);
return tab;
}
} catch (error) {
console.error("(isolate)Context Error", error);
}
}
async function findOtherContexts() {
const prefs = [
"titlepref",
"routerpref",
"mailpref",
"torrentpref",
"tunnelpref",
"ircpref",
"muwirepref",
"botepref",
"blogpref",
"torpref"
];
const contexts = await browser.contextualIdentities.query({});
const myContexts = await Promise.all(
prefs.map(pref => browser.contextualIdentities.query({ name: pref }))
);
const otherContexts = contexts.filter(
context => !myContexts.some(
myContext => myContext[0].cookieStoreId === context.cookieStoreId
)
);
return otherContexts;
}
function contextSetup(requestDetails) {
try {
async function tabGet(tabId) {
try {
//console.log("(isolate)Tab ID from Request", tabId);
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
2022-10-24 19:57:51 -04:00
console.log("(isolate)Tab error", error);
}
2023-05-25 03:49:17 +00:00
}
if (requestDetails == undefined) {
return requestDetails;
}
if (isProxyHost(requestDetails)) {
2023-05-25 03:49:17 +00:00
let setcookie = browser.cookies.set({
firstPartyDomain: i2pHostName(requestDetails.url),
url: requestDetails.url,
2023-05-25 03:49:17 +00:00
secure: true
});
setcookie.then(onContextGotLog, onContextError);
return requestDetails;
}
2022-10-07 19:32:52 -04:00
if (requestDetails.tabId > 0) {
var tab = tabGet(requestDetails.tabId);
tab.then(isolate);
2022-10-08 17:21:55 -04:00
2023-05-25 03:49:17 +00:00
function isolate() {
const url = requestDetails.url;
const localhost = isLocalHost(url);
const routerhost = isRouterHost(url);
console.info("routerhost:", routerhost);
console.info("localhost:", localhost);
function tabUpdate(outboundTab) {
if (outboundTab) {
console.info("updating", outboundTab);
browser.tabs.update(outboundTab.id, { url });
}
}
if (routerhost) {
2023-05-25 03:49:17 +00:00
let routertab = {};
switch (routerhost) {
case "i2ptunnelmgr":
routertab = tab.then(i2ptunnelTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
case "i2psnark":
routertab = tab.then(snarkTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
case "webmail":
routertab = tab.then(mailTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
case "muwire":
routertab = tab.then(muwireTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
case "i2pbote":
routertab = tab.then(i2pboteTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
case "routerconsole":
routertab = tab.then(routerTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
default:
return requestDetails;
}
2023-05-25 03:49:17 +00:00
} else if (localhost) {
let routertab = {};
switch (localhost) {
case "blog":
routertab = tab.then(blogTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
2023-05-25 03:49:17 +00:00
case "irc":
routertab = tab.then(ircTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
2023-05-25 03:49:17 +00:00
case "tor":
routertab = tab.then(torTabFind, onContextError);
routertab.then(tabUpdate);
return requestDetails;
default:
return requestDetails;
}
2023-05-25 03:49:17 +00:00
} else if (i2pHost(requestDetails)) {
const thn = i2pHostName(url);
if (url.includes("=" + thn)) {
if (
2023-05-25 03:49:17 +00:00
!url.includes("://github.com") ||
!url.includes("://notabug.org") ||
!url.includes("://i2pgit.org") ||
!url.includes("://gitlab.com")
) {
2023-05-25 03:49:17 +00:00
console.log("(scrub)checking search hostnames =" + thn);
const tpt = url.split("=" + thn, 2);
requestDetails.url =
"http://" + thn + "/" + tpt[1].replace("%2F", "");
}
}
2023-05-25 03:49:17 +00:00
console.log("(scrub) new hostname", url);
const setcookie = browser.cookies.set({
firstPartyDomain: i2pHostName(url),
url,
secure: true
});
setcookie.then(onContextGotLog, onContextError);
2023-05-25 03:49:17 +00:00
const i2ptab = tab.then(i2pTabFind, onContextError);
i2ptab.then(tabUpdate);
return requestDetails;
} else if (isExtensionHost(requestDetails)) {
return requestDetails;
}
}
}
} catch (error) {
2022-10-24 19:57:51 -04:00
console.log("(isolate)Not an I2P request, blackholing", error);
}
2023-05-25 03:49:17 +00:00
}
2023-05-25 03:49:17 +00:00
function coolheadersSetup(incomingHeaders) {
var asyncSetPageAction = new Promise((resolve, reject) => {
window.setTimeout(() => {
2023-05-25 03:49:17 +00:00
if (incomingHeaders.tabId != undefined) {
let popup = browser.pageAction.getPopup({
tabId: incomingHeaders.tabId
});
popup.then(gotPopup);
}
2022-02-15 21:15:47 -05:00
2023-05-25 03:49:17 +00:00
resolve({ responseHeaders: incomingHeaders.responseHeaders });
}, 2000);
});
return asyncSetPageAction;
2023-05-25 03:49:17 +00:00
}
function getTabURL(tab) {
console.log("(scrub)(equiv check) popup check", tab);
2021-10-01 15:07:33 -04:00
if (tab.id != undefined) {
2023-05-25 03:49:17 +00:00
let popup = browser.pageAction.getPopup({ tabId: tab.id });
console.log("(scrub)(equiv check) popup check");
popup.then(gotPopup);
}
2023-05-25 03:49:17 +00:00
}
2022-02-15 21:15:47 -05:00
2023-05-25 03:49:17 +00:00
function gotPopup(pageTest, tab) {
if (pageTest === undefined) {
return;
}
console.info("pageTest:", pageTest);
let isHttps = false;
let isI2p = false;
if (tab === undefined) {
return;
}
isHttps = tab.url.startsWith("https://");
isI2p = tab.url.includes(".i2p");
if (isHttps) {
if (isI2p) {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "security.html"
});
browser.pageAction.setIcon({
path: "icons/infotoopies.png",
tabId: tab.id
});
try {
browser.tabs
.sendMessage(tab.id, { req: "i2p-torrentlocation" })
.then(response => {
if (
response &&
response.content.toUpperCase() !== "NO-ALT-LOCATION"
) {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "torrent.html"
});
browser.pageAction.setIcon({
path: "icons/infotoopiesbt.png",
tabId: tab.id
});
browser.pageAction.setTitle({
tabId: tab.id,
title: response.content
});
browser.pageAction.show(tab.id);
}
});
} catch (err) {
console.error("(scrub)(equiv check)", err);
}
} else {
try {
browser.tabs
2023-05-25 03:49:17 +00:00
.sendMessage(tab.id, { req: "i2p-location" })
.then(response => {
if (
response &&
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);
2022-10-08 17:21:55 -04:00
}
});
2023-05-25 03:49:17 +00:00
} catch (err) {
console.error("(scrub)(equiv check)", err);
}
}
2023-05-25 03:49:17 +00:00
} else {
if (isI2p) {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "security.html"
});
browser.pageAction.setIcon({
path: "icons/infotoopie.png",
tabId: tab.id
});
}
try {
browser.tabs
.sendMessage(tab.id, { req: "i2p-torrentlocation" })
.then(response => {
if (
response &&
response.content.toUpperCase() !== "NO-ALT-LOCATION"
) {
browser.pageAction.setPopup({
tabId: tab.id,
popup: "torrent.html"
});
browser.pageAction.setIcon({
path: "icons/infotoopiebt.png",
tabId: tab.id
});
browser.pageAction.setTitle({
tabId: tab.id,
title: response.content
});
browser.pageAction.show(tab.id);
}
});
} catch (err) {
console.error("(pageaction)", err);
}
}
}
2023-05-25 03:49:17 +00:00
function getClearTab(tab) {
function setupTabs() {
if (typeof tab === "number") {
browser.tabs.get(tab).then(getTabURL, onScrubError);
} else if (typeof tab === "object" && typeof tab.tabId === "number") {
browser.tabs.get(tab.tabId).then(getTabURL, onScrubError);
} else if (typeof tab === "object" && Array.isArray(tab.tabIds)) {
for (let tabId of tab.tabIds) {
browser.tabs.get(tabId).then(getTabURL, onScrubError);
}
}
}
2023-05-25 03:49:17 +00:00
if (tab === undefined) {
browser.tabs.query({}).then(setupTabs);
2023-05-25 03:49:17 +00:00
} else {
setupTabs();
}
2020-09-24 20:06:24 -04:00
}
const filter = {
2023-05-25 03:49:17 +00:00
url: [{ hostContains: ".i2p" }]
};
function logOnDOMContentLoaded(details) {
console.log(`onDOMContentLoaded: ${details.url}`);
}
2020-09-24 20:06:24 -04:00
browser.tabs.onActivated.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.onReplaced.addListener(getClearTab);
browser.pageAction.onClicked.addListener(getClearTab);
function reloadTabs(tabs) {
for (let tab of tabs) {
browser.tabs.reload(tab.id);
}
}
function reloadError(error) {
console.log(`Error: ${error}`);
}
let querying = browser.tabs.query({});
2023-05-25 03:49:17 +00:00
querying.then(reloadTabs, onScrubError);
2023-05-25 03:49:17 +00:00
/* Listen for onHeaderReceived for the target page.
Set "blocking" and "responseHeaders". */
browser.webRequest.onHeadersReceived.addListener(
coolheadersSetup,
2023-05-25 03:49:17 +00:00
{ urls: [
"*://*.i2p/*",
"https://*/*"
] },
["responseHeaders"]
);
browser.webNavigation.onDOMContentLoaded.addListener(getClearTab, filter);
browser.webNavigation.onDOMContentLoaded.addListener(
logOnDOMContentLoaded,
filter
);
browser.webRequest.onBeforeRequest.addListener(contextSetup, {
2023-05-25 03:49:17 +00:00
urls: [
"*://*.i2p/*",
"*://localhost/*",
"*://127.0.0.1/*",
"*://*/*i2p*"
]
});
browser.webRequest.onBeforeSendHeaders.addListener(
contextScrub,
{ urls: ["*://*.i2p/*"] },
["requestHeaders"]
2022-10-08 17:35:07 -04:00
);
2023-05-25 03:49:17 +00:00
function onScrubError(err) {
console.error(err);
}
function onContextGotLog(log) {
console.log(log);
}
function onContextError(err) {
console.error("Context launcher error:", err);
}