Skip to content

Commit

Permalink
Refactor to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Feb 19, 2024
1 parent 8f7a8ab commit 52143d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
32 changes: 7 additions & 25 deletions src/composables/useSocksProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, computed } from 'vue';
import { computed } from 'vue';

import {
ProxyDetails,
Expand Down Expand Up @@ -34,40 +34,22 @@ const currentHostProxyEnabled = computed(
const globalProxyDNSEnabled = computed(() => globalProxy.value?.proxyDNS ?? false);
const currentHostProxyDNSEnabled = computed(() => currentHostProxyDetails.value?.proxyDNS ?? false);

const setProxyDetails = (proxyDetails: Ref<ProxyDetails>, enabled: boolean) => {
proxyDetails.value = {
...proxyDetails.value,
socksEnabled: enabled,
};
};

const toggleGlobalProxy = () => {
setProxyDetails(globalProxyDetails, !globalProxyDetails.value.socksEnabled);
globalProxyDetails.value.socksEnabled = !globalProxyDetails.value.socksEnabled;
};
const toggleCurrentHostProxy = () => {
hostProxiesDetails.value[activeTabHost.value] = {
...hostProxiesDetails.value[activeTabHost.value],
socksEnabled: !currentHostProxyEnabled.value,
};
hostProxiesDetails.value[activeTabHost.value].socksEnabled = !currentHostProxyEnabled.value;
};

const toggleGlobalProxyDNS = () => {
const updatedGlobalProxyDNS = !globalProxyDetails.value.proxyDNS;

globalProxyDetails.value = { ...globalProxyDetails.value, proxyDNS: updatedGlobalProxyDNS };
globalProxy.value = { ...globalProxy.value, proxyDNS: updatedGlobalProxyDNS };
globalProxyDetails.value.proxyDNS = updatedGlobalProxyDNS;
globalProxy.value.proxyDNS = updatedGlobalProxyDNS;
};
const toggleCurrentHostProxyDNS = () => {
const updatedCurrentHostProxyDNS = !currentHostProxyDetails.value.proxyDNS;

hostProxiesDetails.value[activeTabHost.value] = {
...hostProxiesDetails.value[activeTabHost.value],
proxyDNS: updatedCurrentHostProxyDNS,
};
hostProxies.value[activeTabHost.value] = {
...hostProxies.value[activeTabHost.value],
proxyDNS: updatedCurrentHostProxyDNS,
};
hostProxiesDetails.value[activeTabHost.value].proxyDNS = updatedCurrentHostProxyDNS;
hostProxies.value[activeTabHost.value].proxyDNS = updatedCurrentHostProxyDNS;
};

const setGlobalProxy = ({
Expand Down
14 changes: 2 additions & 12 deletions src/helpers/socksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const initProxyRequests = () => {
browser.proxy.onRequest.addListener(handleProxyRequest, { urls: ['<all_urls>'] });
};

// TODO decide what to do with fallback proxy (if proxy is invalid, it will fallback to Firefox proxy if configured)
// 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

const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
Expand All @@ -47,8 +47,6 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
const { excludedHosts } = await browser.storage.local.get('excludedHosts');
const { hostProxies } = await browser.storage.local.get('hostProxies');
const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails');

// TODO implement random proxy
const { randomProxyActive } = await browser.storage.local.get('randomProxyActive');

const globalConfigParsed = JSON.parse(globalProxy);
Expand All @@ -73,20 +71,13 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
proxiedHosts.includes(currentHost) &&
hostProxiesDetailsParsed[currentHost].socksEnabled
) {
// console.log(`host proxy "${currentHost}" used for :`, details.url);
// console.log('_____________________________');
return hostProxiesParsed[currentHost];
} else if (randomProxyActiveParsed) {
// console.log('random proxy');
// console.log('_____________________________');
// TODO implement random proxy
return { type: 'direct' };
} else if (globalProxyDetailsParsed.socksEnabled) {
// console.log('global proxy', details.url);
// console.log('_____________________________');
return globalConfigParsed;
}
// console.log('no proxy in use');
// console.log('_____________________________');
return { type: 'direct' };
};

Expand All @@ -110,7 +101,6 @@ const getCurrentHost = (details: RequestDetails) => {
// then the host is determined from the document URL
return new URL(details.documentUrl).host;
}

// When a request is initiated in the browser background,
// the host is derived from the request URL itself
return new URL(details.url).host;
Expand Down

0 comments on commit 52143d3

Please sign in to comment.