diff --git a/src/composables/useActiveTab.ts b/src/composables/useActiveTab.ts index e478902b..878ff2a1 100644 --- a/src/composables/useActiveTab.ts +++ b/src/composables/useActiveTab.ts @@ -1,5 +1,5 @@ import { ref } from 'vue'; -import { getActiveTabDetails } from '@/helpers/socksProxy'; +import { getActiveTabDetails } from '@/helpers/tabs'; const activeTabHost = ref(''); const isBrowserPage = ref(false); diff --git a/src/helpers/proxyBadge.ts b/src/helpers/proxyBadge.ts index e9ef4e2f..7de1a1ae 100644 --- a/src/helpers/proxyBadge.ts +++ b/src/helpers/proxyBadge.ts @@ -1,9 +1,10 @@ -import { getActiveProxyDetails, isLocalOrReservedIP } from '@/helpers/socksProxy'; +import { isLocalOrReservedIP } from '@/helpers/socksProxy'; import { ProxyDetails } from '@/helpers/socksProxy.types'; import { checkDomain } from './domain'; +import { getActiveProxyDetails, getActiveTab } from './tabs'; export const updateCurrentTabProxyBadge = async () => { - const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true }); + const activeTab = await getActiveTab(); if (activeTab) { await updateTabProxyBadge(activeTab, await getActiveProxyDetails()); diff --git a/src/helpers/proxyListeners.ts b/src/helpers/proxyListeners.ts index b8d760f6..a23c2e78 100644 --- a/src/helpers/proxyListeners.ts +++ b/src/helpers/proxyListeners.ts @@ -1,6 +1,7 @@ import { getProxyPermissions } from '@/helpers/permissions'; import { updateCurrentTabProxyBadge, updateTabProxyBadge } from '@/helpers/proxyBadge'; -import { getActiveProxyDetails, handleProxyRequest } from '@/helpers/socksProxy'; +import { handleProxyRequest } from '@/helpers/socksProxy'; +import { getActiveProxyDetails } from './tabs'; export const initProxyListeners = () => { // Will init listeners on extension start if permissions are granted diff --git a/src/helpers/socksProxy.ts b/src/helpers/socksProxy.ts index 1adef4ad..7b2e64a0 100644 --- a/src/helpers/socksProxy.ts +++ b/src/helpers/socksProxy.ts @@ -3,54 +3,6 @@ import ipaddr from 'ipaddr.js'; import { RequestDetails, ProxyDetails, ProxyInfoMap } from '@/helpers/socksProxy.types'; import { checkDomain } from './domain'; -const getGlobalProxyDetails = async (): Promise => { - const response = await browser.storage.local.get('globalProxyDetails'); - - if ('globalProxyDetails' in response) { - return JSON.parse(response.globalProxyDetails); - } - return { socksEnabled: false }; -}; - -export const getActiveTabDetails = async () => { - const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true }); - - // activeTab will be null if tabs permission has not been granted - if (!activeTab?.url) { - return { host: '', protocol: '' }; - } - - const activeTabURL = new URL(activeTab.url); - return { - host: activeTabURL.hostname, - protocol: activeTabURL.protocol, - }; -}; - -export const getActiveProxyDetails = async () => { - const globalProxyDetails = await getGlobalProxyDetails(); - const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails'); - - if (hostProxiesDetails) { - const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails); - const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true }); - const tabHost = new URL(activeTab.url!).hostname; - const { domain } = checkDomain(tabHost); - - // Check subdomain proxy first - if (hostProxiesDetailsParsed[tabHost]?.socksEnabled) { - return hostProxiesDetailsParsed[tabHost]; - } - - // Then check domain proxy - if (hostProxiesDetailsParsed[domain]?.socksEnabled) { - return hostProxiesDetailsParsed[domain]; - } - } - - return globalProxyDetails; -}; - // TODO decide what how to handle fallback proxy (if proxy is invalid, it will fallback to Firefox proxy if configured) // https://bugzilla.mozilla.org/show_bug.cgi?id=1750561 diff --git a/src/helpers/tabs.ts b/src/helpers/tabs.ts index b6b75c5d..9eea562c 100644 --- a/src/helpers/tabs.ts +++ b/src/helpers/tabs.ts @@ -1,3 +1,6 @@ +import { checkDomain } from './domain'; +import { ProxyDetails } from './socksProxy.types'; + export const reloadMatchingTabs = async (url: string) => { const urlPattern = `*://*.${url}/*`; @@ -30,3 +33,56 @@ const isExcludedURL = (url: string, excludedURLs: string[]): boolean => { const { hostname } = new URL(url); return excludedURLs.some((excludedUrl) => hostname === excludedUrl); }; + +export const getActiveTabDetails = async () => { + const activeTab = await getActiveTab(); + + // activeTab will be null if tabs permission has not been granted + if (!activeTab?.url) { + return { host: '', protocol: '' }; + } + + const activeTabURL = new URL(activeTab.url); + return { + host: activeTabURL.hostname, + protocol: activeTabURL.protocol, + }; +}; + +export const getActiveProxyDetails = async () => { + const globalProxyDetails = await getGlobalProxyDetails(); + const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails'); + + if (hostProxiesDetails) { + const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails); + const activeTab = await getActiveTab(); + const tabHost = new URL(activeTab.url!).hostname; + const { domain } = checkDomain(tabHost); + + // Check subdomain proxy first + if (hostProxiesDetailsParsed[tabHost]?.socksEnabled) { + return hostProxiesDetailsParsed[tabHost]; + } + + // Then check domain proxy + if (hostProxiesDetailsParsed[domain]?.socksEnabled) { + return hostProxiesDetailsParsed[domain]; + } + } + + return globalProxyDetails; +}; + +const getGlobalProxyDetails = async (): Promise => { + const response = await browser.storage.local.get('globalProxyDetails'); + + if ('globalProxyDetails' in response) { + return JSON.parse(response.globalProxyDetails); + } + return { socksEnabled: false }; +}; + +export const getActiveTab = async () => { + const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true }); + return activeTab; +};