From f896f6ef9236ac334d64fabbf092f423690cea27 Mon Sep 17 00:00:00 2001 From: idk Date: Tue, 24 Dec 2019 18:31:44 -0500 Subject: [PATCH] add a mode for semi-safe localhost browsing --- Makefile | 1 - _locales/en/messages.json | 8 +++++++ background.js | 47 ++++++++++++++++++++++++++++++++++----- proxy.js | 6 ++--- scrub.js | 36 +++++++++++++++++++++++++++++- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 23b393b..7da69ef 100644 --- a/Makefile +++ b/Makefile @@ -176,7 +176,6 @@ upload-deb: fmt: cleancss -O1 all -O2 all --format beautify home.css -o .home.css && mv .home.css home.css cleancss -O1 all -O2 all --format beautify info.css -o .info.css && mv .info.css info.css - #find . -path ./node_modules -prune -o -name '*.css' -exec cleancss -O1 --format beautify {} \; find . -path ./node_modules -prune -o -name '*.js*' -exec prettier --write {} \; lint: diff --git a/_locales/en/messages.json b/_locales/en/messages.json index c358d00..b64626d 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -23,6 +23,14 @@ "message": "Web Browser (Private)", "description": "Preface for the browser titlebar" }, + "localPreface": { + "message": "Localhost Browser", + "description": "Preface for the browser titlebar" + }, + "localPrefacePrivate": { + "message": "Localhost Browser (Private)", + "description": "Preface for the browser titlebar" + }, "routerPreface": { "message": "Router Console", "description": "Preface for the browser titlebar" diff --git a/background.js b/background.js index 862a648..040abe8 100644 --- a/background.js +++ b/background.js @@ -10,6 +10,7 @@ var torrentpref = chrome.i18n.getMessage("torrentPreface"); var torrentprefpriv = chrome.i18n.getMessage("torrentPrefacePrivate"); var tunnelpref = chrome.i18n.getMessage("i2ptunnelPreface"); var tunnelprefpriv = chrome.i18n.getMessage("i2ptunnelPrefacePrivate"); +var localpref = chrome.i18n.getMessage("localPreface"); function onContextsGot(contexts) { var ids = []; @@ -72,6 +73,15 @@ function onContextsGot(contexts) { }) .then(onCreated, onError); } + if (ids.indexOf(localpref) == -1) { + browser.contextualIdentities + .create({ + name: localpref, + color: "red", + icon: "fence" + }) + .then(onCreated, onError); + } } function onCreated(context) { @@ -113,8 +123,33 @@ function themeWindowByTab(tabId) { } } +function isEmpty(obj) { + if (obj == undefined || obj == null) return true; + for (var key in obj) { + if (obj.hasOwnProperty(key)) return false; + } + return true; +} + +var oldtheme; + +var getOldTheme = async function getOldTheme() { + foundtheme = await browser.theme.getCurrent(); + if (!isEmpty(foundtheme)) { + oldtheme = foundtheme; + console.log("Found old theme", oldtheme); + } + return oldtheme; +}; + +getOldTheme(); + function themeWindow(window) { // Check if the window is in private browsing + function onThemeError() { + console.log("got theme", oldtheme); + browser.theme.update(oldtheme); + } function logTabs(tabInfo) { function onContextGotTheme(context) { if (context.name == titlepref) { @@ -204,7 +239,8 @@ function themeWindow(window) { } } else { console.log("Not active in I2P window"); - browser.theme.reset(window.id); + if (!isEmpty(oldtheme)) browser.theme.update(window.id, oldtheme); + else browser.theme.reset(); } } if ( @@ -213,9 +249,10 @@ function themeWindow(window) { ) { browser.contextualIdentities .get(tabInfo[0].cookieStoreId) - .then(onContextGotTheme, onError); + .then(onContextGotTheme, onThemeError); } else { - browser.theme.reset(window.id); + if (!isEmpty(oldtheme)) browser.theme.update(window.id, oldtheme); + else browser.theme.reset(); } } @@ -228,8 +265,6 @@ function themeWindow(window) { function setTitle(window) { function logTabs(tabInfo) { - console.log(tabInfo); - function onContextGotTitle(context) { if (context.name == titlepref) { console.log("Active in I2P window"); @@ -370,7 +405,7 @@ gettingInfo.then(got => { }); function handleUpdated(updateInfo) { - if (updateInfo.theme.colors) { + if (updateInfo.theme) { console.log(`Theme was applied: ${updateInfo.theme}`); } else { console.log("Theme was removed"); diff --git a/proxy.js b/proxy.js index b1fd7f8..cea04a2 100644 --- a/proxy.js +++ b/proxy.js @@ -103,14 +103,14 @@ var handleContextProxyRequest = async function(requestDetails) { if (!routerHost(requestDetails.url)) { if (localHost(requestDetails.url)) { console.log( - "(proxy) non-routerconsole localhost url, dropping", + "(proxy) non-routerconsole localhost url, will not interfere", requestDetails.url ); - proxy = { + /*proxy = { type: "http", host: "localhost", port: "65535" - }; + };*/ } } else if (i2pHost(requestDetails.url)) { proxy = { diff --git a/scrub.js b/scrub.js index c0725ee..681fbcf 100644 --- a/scrub.js +++ b/scrub.js @@ -237,6 +237,34 @@ var contextSetup = async function(requestDetails) { 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(window) { + function onCreated(tab) { + if (tabId != undefined) { + console.log("(isolate) Closing old, un-isolated tab"); + browser.tabs.remove(tabId.id); + } + } + var created = browser.tabs.create({ + active: true, + cookieStoreId: context[0].cookieStoreId, + url: requestDetails.url + }); + created.then(onCreated, onError); + } + var getting = browser.tabs.getCurrent(); + getting.then(Create, onError); + return tabId; + } + } catch (error) { + console.log("(isolate)Context Error", error); + } + }; var anyTabFind = async function(tabId) { try { var context = await browser.contextualIdentities.query({ @@ -310,7 +338,13 @@ var contextSetup = async function(requestDetails) { var mtab = tab.then(i2pTabFind, onError); return requestDetails; } + let localhost = localHost(requestDetails.url); let routerhost = routerHost(requestDetails.url); + if (localhost && !routerhost) { + var tab = tabGet(requestDetails.tabId); + var mtab = tab.then(localTabFind, onError); + return requestDetails; + } if (routerhost) { if (routerhost === "i2ptunnelmgr") { var tab = tabGet(requestDetails.tabId); @@ -331,7 +365,7 @@ var contextSetup = async function(requestDetails) { } } else { var tab = tabGet(requestDetails.tabId); - var mtab = tab.then(anyTabFind, onError); + var mtab = tab.then(routerTabFind, onError); return requestDetails; } }