A few changes in the proxified-containers object

This commit is contained in:
Andrea Marchesini 2021-10-22 16:46:40 +02:00
parent 9cf2d765aa
commit ec933cf730
3 changed files with 50 additions and 110 deletions

View file

@ -199,8 +199,11 @@ window.assignManager = {
} }
const tab = await browser.tabs.get(requestInfo.tabId); const tab = await browser.tabs.get(requestInfo.tabId);
const proxy = await proxifiedContainers.retrieveFromBackground(tab.cookieStoreId); const result = await proxifiedContainers.retrieve(tab.cookieStoreId);
return proxy; if (result) {
return result.proxy;
}
return Utils.DEFAULT_PROXY;
}, },
// Before a request is handled by the browser we decide if we should // Before a request is handled by the browser we decide if we should

View file

@ -1455,7 +1455,8 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
if (!deactivatedMozProxy) { if (!deactivatedMozProxy) {
return; return;
} }
proxifiedContainers.set(id.cookieStoreId, deactivatedMozProxy);
await proxifiedContainers.set(id.cookieStoreId, deactivatedMozProxy);
this.switch.checked = false; this.switch.checked = false;
return; return;
} }
@ -1485,7 +1486,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
} }
if (proxy) { if (proxy) {
proxifiedContainers.set(id.cookieStoreId, proxy); await proxifiedContainers.set(id.cookieStoreId, proxy);
this.switch.checked = true; this.switch.checked = true;
this.updateProxyDependentUi(proxy); this.updateProxyDependentUi(proxy);
@ -1810,30 +1811,21 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
const mozillaVpnUi = document.querySelector(".moz-vpn-controller-content"); const mozillaVpnUi = document.querySelector(".moz-vpn-controller-content");
mozillaVpnUi.updateMozVpnStatusDependentUi(); mozillaVpnUi.updateMozVpnStatusDependentUi();
if (userContextId) { if (!userContextId) {
proxifiedContainers.retrieve(identity.cookieStoreId).then((result) => { return;
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");
});
} }
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}`; advancedProxyInput.value = `${proxy.type}://${proxy.host}:${proxy.port}`;
}; };
try { const proxyData = await proxifiedContainers.retrieve(identity.cookieStoreId);
const { proxy } = await proxifiedContainers.retrieve(identity.cookieStoreId); if (proxyData) {
edit_proxy_dom(proxy); edit_proxy_dom(proxyData.proxy);
} catch (e) { } else {
advancedProxyInput.value = ""; advancedProxyInput.value = "";
} }
@ -1940,7 +1932,7 @@ Logic.registerPanel(P_MOZILLA_VPN_SERVER_LIST, {
mozillaVpnServers mozillaVpnServers
); );
proxifiedContainers.set(identity.cookieStoreId, proxy); await proxifiedContainers.set(identity.cookieStoreId, proxy);
Logic.showPanel(P_CONTAINER_EDIT, identity, false, false); Logic.showPanel(P_CONTAINER_EDIT, identity, false, false);
Logic.showPreviousPanel(); Logic.showPreviousPanel();
}); });
@ -2052,14 +2044,10 @@ Logic.registerPanel(P_MOZILLA_VPN_SERVER_LIST, {
this.makeServerList(mozillaVpnServers); this.makeServerList(mozillaVpnServers);
} }
try { const proxyData = await proxifiedContainers.retrieve(identity.cookieStoreId);
const {proxy} = await proxifiedContainers.retrieve(identity.cookieStoreId); if (proxyData) {
this.checkActiveServer(proxy); this.checkActiveServer(proxyData.proxy);
} catch(e) {
proxifiedContainers.report_proxy_error(e, "MozillaVPN server list");
} }
return Promise.resolve(null);
} }
}); });

View file

@ -1,98 +1,47 @@
// This object allows other scripts to access the list mapping containers to their proxies // This object allows other scripts to access the list mapping containers to their proxies
proxifiedContainers = { proxifiedContainers = {
// Slightly modified version of 'retrieve' which returns a direct proxy whenever an error is met. async retrieveAll() {
async retrieveFromBackground(cookieStoreId = null) { const result = await browser.storage.local.get("proxifiedContainersKey");
try { if(!result || !result["proxifiedContainersKey"]) {
const success = await proxifiedContainers.retrieve(cookieStoreId); return null;
return success.proxy;
} catch (e) {
return Utils.DEFAULT_PROXY;
} }
return result["proxifiedContainersKey"];
}, },
report_proxy_error(error, identifier = null) { async retrieve(cookieStoreId) {
// Currently I print to console but this is inefficient const result = await this.retrieveAll();
const relevant_id_str = identifier === null ? "" : ` call supplied with id: ${identifier.toString()}`; if(!result) {
browser.extension.getBackgroundPage().console.log(`proxifiedContainers error occured ${relevant_id_str}: ${JSON.stringify(error)}`); return null;
},
// 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;
} }
return result.find(o => o.cookieStoreId === cookieStoreId);
},
async set(cookieStoreId, proxy) {
// Assumes proxy is a properly formatted object // 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); let index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId);
if (index === -1) { if (index === -1) {
proxifiedContainersStore.push({ proxifiedContainersStore.push({
cookieStoreId: cookieStoreId, cookieStoreId: cookieStoreId,
proxy: proxy proxy: proxy
}); });
index = proxifiedContainersStore.length - 1;
} else { } else {
proxifiedContainersStore[index] = { proxifiedContainersStore[index] = {
cookieStoreId: cookieStoreId, cookieStoreId: cookieStoreId,
proxy: proxy proxy: proxy
}; };
} }
await browser.storage.local.set({ await browser.storage.local.set({
proxifiedContainersKey: proxifiedContainersStore 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) // 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) { parseProxy(proxy_str, mozillaVpnData = null) {
const proxyRegexp = /(?<type>(https?)|(socks4?)):\/\/(\b(?<username>\w+):(?<password>\w+)@)?(?<host>((?:\d{1,3}\.){3}\d{1,3}\b)|(\b([\w.-]+)+))(:(?<port>\d+))?/; const proxyRegexp = /(?<type>(https?)|(socks4?)):\/\/(\b(?<username>\w+):(?<password>\w+)@)?(?<host>((?:\d{1,3}\.){3}\d{1,3}\b)|(\b([\w.-]+)+))(:(?<port>\d+))?/;
@ -115,7 +64,7 @@ proxifiedContainers = {
// Deletes the proxy information object for a specified cookieStoreId [useful for cleaning] // Deletes the proxy information object for a specified cookieStoreId [useful for cleaning]
async delete(cookieStoreId) { async delete(cookieStoreId) {
// Assumes proxy is a properly formatted object // 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); const index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId);
if (index !== -1) { if (index !== -1) {
proxifiedContainersStore.splice(index, 1); proxifiedContainersStore.splice(index, 1);