From a512e5c80837edf4ebbbfbc4a6abe02c4328abf8 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Fri, 21 Feb 2025 11:55:24 +0100 Subject: [PATCH] better proxy handling --- .../src/background/BackgroundMain.ts | 75 ++++++++++--------- .../container_proxy/src/store/Store.ts | 4 +- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/packages/flutter_mozilla_components/javascript/container_proxy/src/background/BackgroundMain.ts b/packages/flutter_mozilla_components/javascript/container_proxy/src/background/BackgroundMain.ts index 7a3a9e6e..c23ba77b 100644 --- a/packages/flutter_mozilla_components/javascript/container_proxy/src/background/BackgroundMain.ts +++ b/packages/flutter_mozilla_components/javascript/container_proxy/src/background/BackgroundMain.ts @@ -55,47 +55,52 @@ export default class BackgroundMain { ) } - // TODO: Fix in @types/firefox-webext-browser async onRequest(requestDetails: Pick<_OnRequestDetails, 'cookieStoreId' | 'url' | 'tabId'>): Promise { - const tab = (await browser.tabs.get(requestDetails.tabId)) + if (requestDetails.tabId > -1) { + const tab = (await browser.tabs.get(requestDetails.tabId)) - if (tab.cookieStoreId?.startsWith(containerIdentifier) === true) { - try { - const cookieStoreId = tab.cookieStoreId.substring(containerIdentifier.length) + if (tab.cookieStoreId?.startsWith(containerIdentifier) === true) { + try { + const cookieStoreId = tab.cookieStoreId.substring(containerIdentifier.length) - const proxies = await this.store.getProxiesForContainer(cookieStoreId) + const proxies = await this.store.getProxiesForContainer(cookieStoreId) - if (proxies.length > 0) { - proxies.forEach(p => { - if (p.type === ProxyType.Http || p.type === ProxyType.Https) { - this.initializeAuthListener(cookieStoreId, p) - } - }) - - const result: ProxyInfo[] = proxies.filter((p: ProxySettings) => { - try { - const documentUrl = new URL(requestDetails.url) - const isLocalhost = localhosts.has(documentUrl.hostname) - if (isLocalhost && p.doNotProxyLocal) { - return false - } - } catch (e) { - console.error(e) - } - - return true - }).map(p => p.asProxyInfo()) - - if (result.length === 0) { - return [emergencyBreak] + if (proxies === null) { + return doNotProxy } - return result - } - return [emergencyBreak] - } catch (e: unknown) { - console.error(`Error in onRequest listener: ${e as string}`) - return [emergencyBreak] + if (proxies.length > 0) { + proxies.forEach(p => { + if (p.type === ProxyType.Http || p.type === ProxyType.Https) { + this.initializeAuthListener(cookieStoreId, p) + } + }) + + const result: ProxyInfo[] = proxies.filter((p: ProxySettings) => { + try { + const documentUrl = new URL(requestDetails.url) + const isLocalhost = localhosts.has(documentUrl.hostname) + if (isLocalhost && p.doNotProxyLocal) { + return false + } + } catch (e) { + console.error(e) + } + + return true + }).map(p => p.asProxyInfo()) + + if (result.length === 0) { + return [emergencyBreak] + } + return result + } + + return [emergencyBreak] + } catch (e: unknown) { + console.error(`Error in onRequest listener: ${e as string}`) + return [emergencyBreak] + } } } diff --git a/packages/flutter_mozilla_components/javascript/container_proxy/src/store/Store.ts b/packages/flutter_mozilla_components/javascript/container_proxy/src/store/Store.ts index f8c5ec42..308ffaf0 100644 --- a/packages/flutter_mozilla_components/javascript/container_proxy/src/store/Store.ts +++ b/packages/flutter_mozilla_components/javascript/container_proxy/src/store/Store.ts @@ -119,13 +119,13 @@ export class Store { } } - async getProxiesForContainer(cookieStoreId: string): Promise { + async getProxiesForContainer(cookieStoreId: string): Promise { const relations = await this.getRelations() const proxyIds: string[] = relations[cookieStoreId] ?? [] if (proxyIds.length === 0) { - return [] + return null } const proxies = await this.getAllProxies()