Skip to content

Commit

Permalink
Refactor active tab handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Dec 16, 2024
1 parent e8803bd commit 0606043
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/composables/useActiveTab.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/proxyBadge.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/proxyListeners.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 0 additions & 48 deletions src/helpers/socksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProxyDetails> => {
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

Expand Down
56 changes: 56 additions & 0 deletions src/helpers/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { checkDomain } from './domain';
import { ProxyDetails } from './socksProxy.types';

export const reloadMatchingTabs = async (url: string) => {
const urlPattern = `*://*.${url}/*`;

Expand Down Expand Up @@ -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<ProxyDetails> => {
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;
};

0 comments on commit 0606043

Please sign in to comment.