From ec933cf730b654184d63a81d45e0c8976906d509 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Fri, 22 Oct 2021 16:46:40 +0200 Subject: [PATCH] A few changes in the proxified-containers object --- src/js/background/assignManager.js | 7 ++- src/js/popup.js | 62 ++++++++------------ src/js/proxified-containers.js | 91 +++++++----------------------- 3 files changed, 50 insertions(+), 110 deletions(-) diff --git a/src/js/background/assignManager.js b/src/js/background/assignManager.js index b8ed05c..b754548 100644 --- a/src/js/background/assignManager.js +++ b/src/js/background/assignManager.js @@ -199,8 +199,11 @@ window.assignManager = { } const tab = await browser.tabs.get(requestInfo.tabId); - const proxy = await proxifiedContainers.retrieveFromBackground(tab.cookieStoreId); - return proxy; + const result = await proxifiedContainers.retrieve(tab.cookieStoreId); + if (result) { + return result.proxy; + } + return Utils.DEFAULT_PROXY; }, // Before a request is handled by the browser we decide if we should diff --git a/src/js/popup.js b/src/js/popup.js index 55f7a3c..b9a134e 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -1455,7 +1455,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, { if (!deactivatedMozProxy) { return; } - proxifiedContainers.set(id.cookieStoreId, deactivatedMozProxy); + + await proxifiedContainers.set(id.cookieStoreId, deactivatedMozProxy); this.switch.checked = false; return; } @@ -1485,7 +1486,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, { } if (proxy) { - proxifiedContainers.set(id.cookieStoreId, proxy); + await proxifiedContainers.set(id.cookieStoreId, proxy); this.switch.checked = true; this.updateProxyDependentUi(proxy); @@ -1810,30 +1811,21 @@ Logic.registerPanel(P_CONTAINER_EDIT, { const mozillaVpnUi = document.querySelector(".moz-vpn-controller-content"); mozillaVpnUi.updateMozVpnStatusDependentUi(); - if (userContextId) { - proxifiedContainers.retrieve(identity.cookieStoreId).then((result) => { - if (result.proxy && result.proxy.mozProxyEnabled && !mozillaVpnConnected) { - return; - } - mozillaVpnUi.updateProxyDependentUi(result.proxy); - }, (error) => { - if(error.error === "uninitialized" || error.error === "doesnotexist") { - proxifiedContainers.set(identity.cookieStoreId, Utils.DEFAULT_PROXY, error.error === "uninitialized").then((result) => { - mozillaVpnUi.updateProxyDependentUi(result.proxy); - }, (error) => { - proxifiedContainers.report_proxy_error(error, "popup.js: unexpected set(...) error"); - }).catch((error) => { - proxifiedContainers.report_proxy_error(error, "popup.js: unexpected set(...) exception"); - }); - } - else { - proxifiedContainers.report_proxy_error(error, "popup.js: unknown error"); - } - }).catch((err) => { - proxifiedContainers.report_proxy_error(err, "popup.js: unexpected retrieve error"); - }); + if (!userContextId) { + return; } - return Promise.resolve(null); + + const proxyData = await proxifiedContainers.retrieve(identity.cookieStoreId); + if (proxyData) { + if (proxyData.proxy && proxyData.proxy.mozProxyEnabled && !mozillaVpnConnected) { + return; + } + mozillaVpnUi.updateProxyDependentUi(proxyData.proxy); + return; + } + + await proxifiedContainers.set(identity.cookieStoreId, Utils.DEFAULT_PROXY); + mozillaVpnUi.updateProxyDependentUi(Utils.DEFAULT_PROXY); }, }); @@ -1909,10 +1901,10 @@ Logic.registerPanel(P_ADVANCED_PROXY_SETTINGS, { advancedProxyInput.value = `${proxy.type}://${proxy.host}:${proxy.port}`; }; - try { - const { proxy } = await proxifiedContainers.retrieve(identity.cookieStoreId); - edit_proxy_dom(proxy); - } catch (e) { + const proxyData = await proxifiedContainers.retrieve(identity.cookieStoreId); + if (proxyData) { + edit_proxy_dom(proxyData.proxy); + } else { advancedProxyInput.value = ""; } @@ -1940,7 +1932,7 @@ Logic.registerPanel(P_MOZILLA_VPN_SERVER_LIST, { mozillaVpnServers ); - proxifiedContainers.set(identity.cookieStoreId, proxy); + await proxifiedContainers.set(identity.cookieStoreId, proxy); Logic.showPanel(P_CONTAINER_EDIT, identity, false, false); Logic.showPreviousPanel(); }); @@ -2052,14 +2044,10 @@ Logic.registerPanel(P_MOZILLA_VPN_SERVER_LIST, { this.makeServerList(mozillaVpnServers); } - try { - const {proxy} = await proxifiedContainers.retrieve(identity.cookieStoreId); - this.checkActiveServer(proxy); - - } catch(e) { - proxifiedContainers.report_proxy_error(e, "MozillaVPN server list"); + const proxyData = await proxifiedContainers.retrieve(identity.cookieStoreId); + if (proxyData) { + this.checkActiveServer(proxyData.proxy); } - return Promise.resolve(null); } }); diff --git a/src/js/proxified-containers.js b/src/js/proxified-containers.js index d8c7910..168a6da 100644 --- a/src/js/proxified-containers.js +++ b/src/js/proxified-containers.js @@ -1,98 +1,47 @@ // This object allows other scripts to access the list mapping containers to their proxies proxifiedContainers = { - // Slightly modified version of 'retrieve' which returns a direct proxy whenever an error is met. - async retrieveFromBackground(cookieStoreId = null) { - try { - const success = await proxifiedContainers.retrieve(cookieStoreId); - return success.proxy; - } catch (e) { - return Utils.DEFAULT_PROXY; + async retrieveAll() { + const result = await browser.storage.local.get("proxifiedContainersKey"); + if(!result || !result["proxifiedContainersKey"]) { + return null; } + + return result["proxifiedContainersKey"]; }, - report_proxy_error(error, identifier = null) { - // Currently I print to console but this is inefficient - const relevant_id_str = identifier === null ? "" : ` call supplied with id: ${identifier.toString()}`; - browser.extension.getBackgroundPage().console.log(`proxifiedContainers error occured ${relevant_id_str}: ${JSON.stringify(error)}`); - }, - - // Resolves to a proxy object which can be used in the return of the listener required for browser.proxy.onRequest.addListener - retrieve(cookieStoreId = null) { - return new Promise((resolve, reject) => { - browser.storage.local.get("proxifiedContainersKey").then((results) => { - // Steps to test: - // 1. Is result empty? If so we must inform the caller to intialize proxifiedContainersStore with some initial info. - // 2. Is cookieStoreId null? This means the caller probably wants everything currently in the proxifiedContainersStore object store - // 3. If there doesn't exist an entry for the associated cookieStoreId, inform the caller of this - // 4. Normal operation - if the cookieStoreId exists in the map, we can simply resolve with the correct proxy value - - const results_array = results["proxifiedContainersKey"]; - if (Object.getOwnPropertyNames(results).length === 0) { - reject({ - error: "uninitialized", - message: "" - }); - } else if (cookieStoreId === null) { - resolve(results_array); - } else { - const val = results_array.find(o => o.cookieStoreId === cookieStoreId); - - if (typeof val !== "object" || val === null) { - reject({ - error: "doesnotexist", - message: "" - }); - } else { - resolve(val); - } - } - - }, (error) => { - reject({ - error: "internal", - message: error - }); - }).catch((error) => { - proxifiedContainers.report_proxy_error(error, "proxified-containers.js: error 1"); - }); - }); - }, - - async set(cookieStoreId, proxy, initialize = false) { - if (initialize === true) { - const proxifiedContainersStore = []; - proxifiedContainersStore.push({ - cookieStoreId: cookieStoreId, - proxy: proxy - }); - await browser.storage.local.set({ - proxifiedContainersKey: proxifiedContainersStore - }); - return proxy; + async retrieve(cookieStoreId) { + const result = await this.retrieveAll(); + if(!result) { + return null; } + + return result.find(o => o.cookieStoreId === cookieStoreId); + }, + + async set(cookieStoreId, proxy) { // Assumes proxy is a properly formatted object - const proxifiedContainersStore = await proxifiedContainers.retrieve(); + let proxifiedContainersStore = await proxifiedContainers.retrieveAll(); + if (!proxifiedContainersStore) proxifiedContainersStore = []; + let index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId); if (index === -1) { proxifiedContainersStore.push({ cookieStoreId: cookieStoreId, proxy: proxy }); - index = proxifiedContainersStore.length - 1; } else { proxifiedContainersStore[index] = { cookieStoreId: cookieStoreId, proxy: proxy }; } + await browser.storage.local.set({ proxifiedContainersKey: proxifiedContainersStore }); - return proxifiedContainersStore[index]; }, - // Parses a proxy description string of the format type://host[:port] or type://username:password@host[:port] (port is optional) parseProxy(proxy_str, mozillaVpnData = null) { const proxyRegexp = /(?(https?)|(socks4?)):\/\/(\b(?\w+):(?\w+)@)?(?((?:\d{1,3}\.){3}\d{1,3}\b)|(\b([\w.-]+)+))(:(?\d+))?/; @@ -115,7 +64,7 @@ proxifiedContainers = { // Deletes the proxy information object for a specified cookieStoreId [useful for cleaning] async delete(cookieStoreId) { // Assumes proxy is a properly formatted object - const proxifiedContainersStore = await proxifiedContainers.retrieve(); + const proxifiedContainersStore = await proxifiedContainers.retrieveAll(); const index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId); if (index !== -1) { proxifiedContainersStore.splice(index, 1);