From ccc1e9b6ea85be945128396e4826c721e59942b9 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Wed, 12 Feb 2025 12:24:21 +0100 Subject: [PATCH 1/7] Rename city or country random proxy helper --- src/components/Location.vue | 4 ++-- ...est.ts => getCityCountrySocksProxy.test.ts} | 18 +++++++++--------- ...cksProxy.ts => getCityCountrySocksProxy.ts} | 4 ++-- src/helpers/proxyListeners.ts | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) rename src/helpers/{getRandomSocksProxy.test.ts => getCityCountrySocksProxy.test.ts} (83%) rename src/helpers/{getRandomSocksProxy.ts => getCityCountrySocksProxy.ts} (88%) diff --git a/src/components/Location.vue b/src/components/Location.vue index 1203f9c2..5f5792f1 100644 --- a/src/components/Location.vue +++ b/src/components/Location.vue @@ -6,7 +6,7 @@ import LocationTabs from '@/components/LocationTabs.vue'; import SearchLocation from '@/components/SearchLocation.vue'; import IconLabel from '@/components/IconLabel.vue'; -import getRandomSocksProxy from '@/helpers/getRandomSocksProxy'; +import getCityCountrySocksProxy from '@/helpers/getCityCountrySocksProxy'; import { updateCurrentTabProxyBadge } from '@/helpers/proxyBadge'; import useSocksProxies from '@/composables/useSocksProxies/useSocksProxies'; @@ -61,7 +61,7 @@ const clickServer = ( }; const clickCountryOrCity = (selectedCountry: string, selectedCity?: string) => { - const { country, countryCode, city, hostname, ipv4_address, port } = getRandomSocksProxy({ + const { country, countryCode, city, hostname, ipv4_address, port } = getCityCountrySocksProxy({ socksProxies: filteredProxies.value, country: selectedCountry, city: selectedCity, diff --git a/src/helpers/getRandomSocksProxy.test.ts b/src/helpers/getCityCountrySocksProxy.test.ts similarity index 83% rename from src/helpers/getRandomSocksProxy.test.ts rename to src/helpers/getCityCountrySocksProxy.test.ts index b8900e8f..92265b9c 100644 --- a/src/helpers/getRandomSocksProxy.test.ts +++ b/src/helpers/getCityCountrySocksProxy.test.ts @@ -1,4 +1,4 @@ -import getRandomSocksProxy from '@/helpers/getRandomSocksProxy'; +import getCityCountrySocksProxy from '@/helpers/getCityCountrySocksProxy'; import { Country } from '@/composables/useSocksProxies/socksProxies.types'; const gothenburgProxies = [ @@ -39,19 +39,19 @@ const mockSocksProxies = [ }, ] as Country[]; -describe('getRandomSocksProxy', () => { +describe('getCityCountrySocksProxy', () => { it('should throw error if no sockproxies list is available', () => { expect(() => - getRandomSocksProxy({ socksProxies: undefined, country: 'Mongolia' }), - ).toThrowError('No proxies to choose from'); + getCityCountrySocksProxy({ socksProxies: undefined, country: 'Mongolia' }), + ).toThrow('No proxies to choose from'); - expect(() => getRandomSocksProxy({ socksProxies: [], country: 'Mongolia' })).toThrowError( + expect(() => getCityCountrySocksProxy({ socksProxies: [], country: 'Mongolia' })).toThrow( 'No proxies to choose from', ); }); it('should return a proxy in Sweden', () => { - const { hostname, port } = getRandomSocksProxy({ + const { hostname, port } = getCityCountrySocksProxy({ socksProxies: mockSocksProxies, country: 'Sweden', }); @@ -62,7 +62,7 @@ describe('getRandomSocksProxy', () => { }); it('should return a proxy in Gothenburg', () => { - const { hostname, port } = getRandomSocksProxy({ + const { hostname, port } = getCityCountrySocksProxy({ socksProxies: mockSocksProxies, country: 'Sweden', city: 'Gothenburg', @@ -76,7 +76,7 @@ describe('getRandomSocksProxy', () => { }); it('should return a proxy in Malmö', () => { - const { hostname, port } = getRandomSocksProxy({ + const { hostname, port } = getCityCountrySocksProxy({ socksProxies: mockSocksProxies, country: 'Sweden', city: 'Malmo', @@ -86,7 +86,7 @@ describe('getRandomSocksProxy', () => { }); it('should return a proxy in Stockholm', () => { - const { hostname, port } = getRandomSocksProxy({ + const { hostname, port } = getCityCountrySocksProxy({ socksProxies: mockSocksProxies, country: 'Sweden', city: 'Stockholm', diff --git a/src/helpers/getRandomSocksProxy.ts b/src/helpers/getCityCountrySocksProxy.ts similarity index 88% rename from src/helpers/getRandomSocksProxy.ts rename to src/helpers/getCityCountrySocksProxy.ts index 85784a61..f09c21e3 100644 --- a/src/helpers/getRandomSocksProxy.ts +++ b/src/helpers/getCityCountrySocksProxy.ts @@ -6,7 +6,7 @@ type Props = { city?: string; }; -const getRandomSocksProxy = ({ socksProxies, country, city }: Props) => { +const getCityCountrySocksProxy = ({ socksProxies, country, city }: Props) => { if (!socksProxies || !socksProxies.length) { throw new Error('No proxies to choose from'); } @@ -36,4 +36,4 @@ const getRandomSocksProxy = ({ socksProxies, country, city }: Props) => { return randomSocks; }; -export default getRandomSocksProxy; +export default getCityCountrySocksProxy; diff --git a/src/helpers/proxyListeners.ts b/src/helpers/proxyListeners.ts index a23c2e78..ef109e0c 100644 --- a/src/helpers/proxyListeners.ts +++ b/src/helpers/proxyListeners.ts @@ -1,7 +1,7 @@ import { getProxyPermissions } from '@/helpers/permissions'; import { updateCurrentTabProxyBadge, updateTabProxyBadge } from '@/helpers/proxyBadge'; import { handleProxyRequest } from '@/helpers/socksProxy'; -import { getActiveProxyDetails } from './tabs'; +import { getActiveProxyDetails } from '@/helpers/tabs'; export const initProxyListeners = () => { // Will init listeners on extension start if permissions are granted From e0d13b727443c110ff086049d8f2c6c7c58d8f09 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Wed, 12 Feb 2025 13:27:23 +0100 Subject: [PATCH 2/7] Add toggle to enable random mode --- src/components/OptionsTabs/ProxyTab.vue | 2 ++ src/components/Proxy/RandomProxyMode.vue | 36 ++++++++++++++++++++++++ src/composables/useStore.ts | 3 ++ src/helpers/socksProxy.ts | 10 ++++++- 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/components/Proxy/RandomProxyMode.vue diff --git a/src/components/OptionsTabs/ProxyTab.vue b/src/components/OptionsTabs/ProxyTab.vue index 05e43d32..6bf356a5 100644 --- a/src/components/OptionsTabs/ProxyTab.vue +++ b/src/components/OptionsTabs/ProxyTab.vue @@ -6,6 +6,7 @@ import CustomProxies from '@/components/Proxy/CustomProxies.vue'; import IconLabel from '@/components/IconLabel.vue'; import LocationDrawer from '@/components/ConnectionDetails/LocationDrawer.vue'; import ProxyAutoReload from '@/components/Proxy/ProxyAutoReload.vue'; +import RandomProxyMode from '@/components/Proxy/RandomProxyMode.vue'; import TitleCategory from '@/components/TitleCategory.vue'; import useProxyPermissions from '@/composables/useProxyPermissions'; @@ -16,6 +17,7 @@ const { isGranted, requestPermissions } = useProxyPermissions(); diff --git a/src/components/Proxy/RandomProxyMode.vue b/src/components/Proxy/RandomProxyMode.vue new file mode 100644 index 00000000..e14f93b2 --- /dev/null +++ b/src/components/Proxy/RandomProxyMode.vue @@ -0,0 +1,36 @@ + + + diff --git a/src/composables/useStore.ts b/src/composables/useStore.ts index 41c5c1c4..a107cf13 100644 --- a/src/composables/useStore.ts +++ b/src/composables/useStore.ts @@ -22,6 +22,7 @@ export type Store = { hostProxiesDetails: Ref; optionsActiveTab: Ref; proxyAutoReload: Ref; + randomProxyMode: Ref; webRTCStatus: Ref; }; @@ -36,6 +37,7 @@ const useStore = (): Store => { const optionsActiveTab = useBrowserStorageLocal('optionsActiveTab', Tab.SETTINGS); const proxyAutoReload = useBrowserStorageLocal('proxyAutoReload', false); const webRTCStatus = useBrowserStorageLocal('webRTCStatus', true); + const randomProxyMode = useBrowserStorageLocal('randomProxyMode', true); return { excludedHosts, @@ -47,6 +49,7 @@ const useStore = (): Store => { hostProxiesDetails, optionsActiveTab, proxyAutoReload, + randomProxyMode, webRTCStatus, }; }; diff --git a/src/helpers/socksProxy.ts b/src/helpers/socksProxy.ts index 7b2e64a0..5e404e54 100644 --- a/src/helpers/socksProxy.ts +++ b/src/helpers/socksProxy.ts @@ -9,12 +9,14 @@ import { checkDomain } from './domain'; export const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => { try { const { globalProxy } = await browser.storage.local.get('globalProxy'); + const { randomProxyMode } = await browser.storage.local.get('randomProxyMode'); const { globalProxyDetails } = await browser.storage.local.get('globalProxyDetails'); const { excludedHosts } = await browser.storage.local.get('excludedHosts'); const { hostProxies } = await browser.storage.local.get('hostProxies'); const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails'); const globalConfigParsed = JSON.parse(globalProxy); + const randomProxyModeParsed = JSON.parse(randomProxyMode); const globalProxyDetailsParsed: ProxyDetails = JSON.parse(globalProxyDetails); const excludedHostsParsed: string[] = JSON.parse(excludedHosts); const hostProxiesParsed: ProxyInfoMap = JSON.parse(hostProxies); @@ -23,7 +25,7 @@ export const handleProxyRequest = async (details: browser.proxy._OnRequestDetail const currentHost = getCurrentHost(details); const { hasSubdomain, domain, subDomain } = checkDomain(currentHost); - // Block speculative requests + // Block speculative requests, since we can't identify their origins if (details.type === 'speculative') { return { cancel: true }; } @@ -33,6 +35,12 @@ export const handleProxyRequest = async (details: browser.proxy._OnRequestDetail return { type: 'direct' }; } + // 0. If random proxy is enabled, return a random proxy for the session + if (randomProxyModeParsed) { + console.log({ randomProxyModeParsed }); + return { cancel: true }; + } + // 1. Check subdomain level if (hasSubdomain) { if (excludedHostsParsed.includes(subDomain)) { From 5180a7d5c3cd17baa92ad182d1382c20e4016eb1 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Wed, 12 Feb 2025 13:28:20 +0100 Subject: [PATCH 3/7] Add initial proxy randomization support --- src/components/Proxy/HomeProxyStatus.vue | 7 +++ src/composables/useSocksProxy.ts | 4 +- src/composables/useStore.ts | 2 +- src/helpers/getRandomSessionProxy.ts | 57 ++++++++++++++++++++++++ src/helpers/socksProxy.ts | 7 +-- 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/helpers/getRandomSessionProxy.ts diff --git a/src/components/Proxy/HomeProxyStatus.vue b/src/components/Proxy/HomeProxyStatus.vue index d97d50ed..830ff021 100644 --- a/src/components/Proxy/HomeProxyStatus.vue +++ b/src/components/Proxy/HomeProxyStatus.vue @@ -128,6 +128,13 @@ const handleRemoveProxy = (host: string) => { watch([currentHostProxyEnabled, subDomainProxyEnabled, domainProxyDetails, excludedHosts], () => { lastClickedTab.value = null; }); + +watch(isGranted, () => { + // This is to make sure there's always a proxy list when the user starts using the proxy feature + if (isGranted.value) { + getSocksProxies(); + } +});