Skip to content

Commit

Permalink
Fix selection of proxy server per host
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Feb 1, 2024
1 parent 5ed2ad2 commit 0a20200
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 104 deletions.
6 changes: 3 additions & 3 deletions src/components/ConnectionDetails/ConnectionDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import DnsLeakStatus from '@/components/ConnectionStatus/DnsLeakStatus.vue';
import { ConnectionKey, defaultConnection } from '@/composables/useConnection';
import useSocksProxy from '@/composables/useSocksProxy';
const { globalProxyEnabled, activeTabProxyEnabled } = useSocksProxy();
const { globalProxyEnabled } = useSocksProxy();
const { isLoading, isError, connection } = inject(ConnectionKey, defaultConnection);
const connected = computed(() => connection.value.isMullvad);
Expand All @@ -34,7 +34,7 @@ const connected = computed(() => connection.value.isMullvad);
<IconLabel text="Checking connection" type="spinner" />
</p>

<div v-if="globalProxyEnabled || activeTabProxyEnabled">
<div v-if="globalProxyEnabled">
<p>The connection is set to use a proxy server.</p>
<p>If you can't load any pages, try disabling the proxy to access the internet directly</p>
<router-link to="/proxy">
Expand All @@ -45,7 +45,7 @@ const connected = computed(() => connection.value.isMullvad);

<div v-else-if="isError">
<IconLabel text="Couldn't get connection details" type="warning" />
<div v-if="globalProxyEnabled || activeTabProxyEnabled">
<div v-if="globalProxyEnabled">
<p>The connection is set to use a proxy server.</p>
<p>If you can't load any pages, try disabling the proxy to access the internet directly</p>
<router-link to="/proxy">
Expand Down
4 changes: 2 additions & 2 deletions src/components/Location.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { HistoricConnection } from '@/composables/useHistoricConnections/Hi
const { activeTabHost } = useActiveTab();
const { hostProxySelect, toggleLocations } = useLocations();
const { proxiesList } = useListProxies();
const { setHostProxy, setGlobalProxy } = useSocksProxy();
const { setCurrentHostProxy, setGlobalProxy } = useSocksProxy();
const { storeSocksProxyUsage } = useHistoricConnections();
const currentOrAllWebsites = computed(() =>
Expand All @@ -33,7 +33,7 @@ const setProxy = (
toggleLocations();
if (hostProxySelect.value) {
setHostProxy({ country, city, hostname, ipv4_address, port }, activeTabHost.value);
setCurrentHostProxy({ country, city, hostname, ipv4_address, port }, activeTabHost.value);
} else {
setGlobalProxy({ country, city, hostname, ipv4_address, port });
}
Expand Down
57 changes: 15 additions & 42 deletions src/components/ProxyStatus/ProxyHost.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<script lang="ts" setup>
import { computed } from 'vue';
import { NButtonGroup, NCard, NTag } from 'naive-ui';
import { NButtonGroup, NCard, NSwitch, NTooltip } from 'naive-ui';
import Button from '@/components/Buttons/Button.vue';
import CurrentProxyDetails from '@/components/CurrentProxyDetails.vue';
import FeCheck from '@/components/Icons/FeCheck.vue';
import TitleCategory from '@/components/TitleCategory.vue';
import useActiveTab from '@/composables/useActiveTab';
Expand All @@ -13,26 +12,18 @@ import useLocations from '@/composables/useLocations';
import useSocksProxy from '@/composables/useSocksProxy';
import useStore from '@/composables/useStore';
const { activeTabHost, activeTab } = useActiveTab();
const { connection } = useConnection();
const { hostProxySelect, toggleLocations } = useLocations();
const { activeTabProxyEnabled } = useSocksProxy();
const { hostProxies, hostProxiesDetails, excludedHosts } = useStore();
const { currentHostProxyDetails, currentHostProxyEnabled, toggleCurrentHostProxy } =
useSocksProxy();
const { excludedHosts } = useStore();
const { activeTabHost, activeTab } = useActiveTab();
const title = computed(() => `For this website (${activeTabHost.value})`);
const customTitle = computed(() => `For this website (${activeTabHost.value})`);
const currentProxyDetails = computed(() => {
return hostProxiesDetails.value[activeTabHost.value];
});
const isExcluded = computed(() => excludedHosts.value.includes(activeTabHost.value));
const isWireGuard = computed(() => connection.value.protocol?.includes('WireGuard'));
const removeHostProxy = () => {
const updatedHostProxies = { ...hostProxies.value };
delete updatedHostProxies[activeTabHost.value];
hostProxies.value = updatedHostProxies;
};
const neverProxyHost = () => {
excludedHosts.value = [...excludedHosts.value, activeTabHost.value];
};
Expand All @@ -51,7 +42,7 @@ const handleHostProxySelect = () => {
// watchEffect(() => {
// let message = '';
// if (activeTabProxyEnabled.value) {
// if (currentHostProxyEnabled.value) {
// message = `Only this website is configured to use the following proxy.`;
// } else if (isExcluded.value) {
// message = 'The website is configured to never use a proxy.';
Expand All @@ -66,20 +57,16 @@ const handleHostProxySelect = () => {
<template>
<n-card v-if="activeTab" :bordered="false" class="mb-4">
<div class="flex justify-between">
<TitleCategory :title="customTitle" />
<n-tag v-if="activeTabProxyEnabled" round :bordered="false" type="success">
active
<template #icon>
<FeCheck />
<TitleCategory :title="title" />
<n-tooltip>
<template #trigger>
<n-switch :value="currentHostProxyEnabled" @update-value="toggleCurrentHostProxy" />
</template>
</n-tag>
<div v-else />
<span> {{ currentHostProxyEnabled ? 'enabled' : 'disabled' }}</span>
</n-tooltip>
</div>

<CurrentProxyDetails
v-if="activeTabProxyEnabled && currentProxyDetails"
:proxy-details="currentProxyDetails"
/>
<CurrentProxyDetails v-if="currentHostProxyDetails" :proxy-details="currentHostProxyDetails" />

<!-- <IconLabel :text="instructions" type="info" class="mb-8 mt-6" /> -->

Expand All @@ -98,21 +85,7 @@ const handleHostProxySelect = () => {
Switch location
</Button>

<Button
v-if="activeTabProxyEnabled"
color="error"
class="flex items-center justify-center"
@click="removeHostProxy"
>
Disable proxy
</Button>

<Button
v-else
color="error"
class="flex items-center justify-center"
@click="neverProxyHost"
>
<Button color="error" class="flex items-center justify-center" @click="neverProxyHost">
Never proxy
</Button>
</n-button-group>
Expand Down
75 changes: 44 additions & 31 deletions src/composables/useSocksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const { activeTabHost } = useActiveTab();
const { connection, updateConnection } = useConnection();
const { globalProxy, globalProxyDetails, hostProxies, hostProxiesDetails } = useStore();

const currentHostProxyDetails = computed(() => hostProxiesDetails.value[activeTabHost.value]);

const globalProxyEnabled = computed(() => globalProxyDetails.value.socksEnabled);
const activeTabProxyEnabled = computed(() =>
Object.keys(hostProxies.value).includes(activeTabHost.value),
);
const currentHostProxyEnabled = computed(() => currentHostProxyDetails.value.socksEnabled);

const baseConfig: Partial<ProxyInfo> = {
port: 1080,
Expand All @@ -37,33 +37,45 @@ const toggleGlobalProxy = () => {
setProxyDetails(globalProxyDetails, !globalProxyDetails.value.socksEnabled);
};

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

const createDefaultProxy = (protocol: string) => {
const socksIp = getSocksIpForProtocol(protocol);

return {
host: socksIp,
port: baseConfig.port!,
proxyDNS: baseConfig.proxyDNS,
type: ProxyInfoType.socks,
};
};

const initProxies = async (
{ hostname, country, city }: Partial<ProxyOperationArgs>,
protocol: string,
) => {
// Create a default globalProxy if there's none yet
// We don't do it it in the background script, because we don't want to make a request on extension start
const newProxyDetails = {
city,
country,
protocol,
proxyDNS: baseConfig.proxyDNS,
server: hostname,
socksEnabled: false,
};

if (Object.keys(globalProxy.value).length !== 4) {
const socksIp = getSocksIpForProtocol(protocol);

const defaultGlobalProxy: ProxyInfo = {
host: socksIp,
port: baseConfig.port!,
proxyDNS: baseConfig.proxyDNS,
type: ProxyInfoType.socks,
};

const newGlobalProxyDetails: ProxyDetails = {
socksEnabled: false,
protocol: connection.value.protocol,
server: hostname,
country: country,
city: city,
proxyDNS: baseConfig.proxyDNS,
};

globalProxy.value = defaultGlobalProxy;
globalProxyDetails.value = newGlobalProxyDetails;
globalProxy.value = createDefaultProxy(protocol);
globalProxyDetails.value = newProxyDetails;
}

if (!hostProxiesDetails.value[activeTabHost.value]) {
hostProxies.value[activeTabHost.value] = createDefaultProxy(protocol);
hostProxiesDetails.value[activeTabHost.value] = newProxyDetails;
}
};

Expand Down Expand Up @@ -96,7 +108,7 @@ const setGlobalProxy = ({
updateConnection();
};

const setHostProxy = (
const setCurrentHostProxy = (
{ country, city, hostname, ipv4_address, port = baseConfig.port }: Partial<ProxyOperationArgs>,
host: string,
) => {
Expand Down Expand Up @@ -124,13 +136,14 @@ const setHostProxy = (

const useSocksProxy = () => {
return {
activeTabProxyEnabled,
hostProxiesDetails,
currentHostProxyDetails,
currentHostProxyEnabled,
globalProxyDetails,
globalProxyEnabled,
initGlobalProxy,
setHostProxy,
initProxies,
setCurrentHostProxy,
setGlobalProxy,
toggleCurrentHostProxy,
toggleGlobalProxy,
};
};
Expand Down
23 changes: 14 additions & 9 deletions src/helpers/socksProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
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');

// TODO implement random proxy
const { randomProxyActive } = await browser.storage.local.get('randomProxyActive');
Expand All @@ -29,6 +30,7 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
const globalProxyDetailsParsed: ProxyDetails = JSON.parse(globalProxyDetails);
const excludedHostsParsed: string[] = JSON.parse(excludedHosts);
const hostProxiesParsed = JSON.parse(hostProxies);
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
const randomProxyActiveParsed = JSON.parse(randomProxyActive);

const proxiedHosts = Object.keys(hostProxiesParsed);
Expand All @@ -42,24 +44,27 @@ const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
console.log('_____________________________');
}
return { type: 'direct' };
} else if (proxiedHosts.includes(currentHost)) {
} else if (
proxiedHosts.includes(currentHost) &&
hostProxiesDetailsParsed[currentHost].socksEnabled
) {
// TODO Warn about ubO or other extension creation DNS leaks
// Maybe check automatically?

console.log(`host proxy "${currentHost}" used for :`, details.url);
console.log('_____________________________');
// console.log(`host proxy "${currentHost}" used for :`, details.url);
// console.log('_____________________________');
return hostProxiesParsed[currentHost];
} else if (randomProxyActiveParsed) {
console.log('random proxy');
console.log('_____________________________');
// console.log('random proxy');
// console.log('_____________________________');
return { type: 'direct' };
} else if (globalProxyDetailsParsed.socksEnabled) {
console.log('global proxy', details.url);
console.log('_____________________________');
// console.log('global proxy', details.url);
// console.log('_____________________________');
return globalConfigParsed;
}
console.log('no proxy in use');
console.log('_____________________________');
// console.log('no proxy in use');
// console.log('_____________________________');
return { type: 'direct' };
};

Expand Down
16 changes: 1 addition & 15 deletions src/popup/App.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
<script lang="ts" setup>
import { provide, watch, onMounted } from 'vue';
import { provide, onMounted } from 'vue';
import { useQueryProvider } from 'vue-query';
import { NConfigProvider, GlobalThemeOverrides, darkTheme } from 'naive-ui';
import Popup from '@/popup/Popup.vue';
import useConnection, { ConnectionKey } from '@/composables/useConnection';
import useSocksProxy from '@/composables/useSocksProxy';
import useListProxies from '@/composables/useListProxies';
const { isLoading, connection, isError } = useConnection();
const { initGlobalProxy } = useSocksProxy();
const { getSocksProxies } = useListProxies();
provide(ConnectionKey, { connection, isLoading, isError });
watch(connection, async () => {
// Make sure the proxy always has some value
await initGlobalProxy(
{
hostname: connection.value.server,
country: connection.value.country,
city: connection.value.city,
},
connection.value.protocol!,
);
});
const loadProxies = async () => {
await getSocksProxies();
};
Expand Down
20 changes: 18 additions & 2 deletions src/popup/views/Home.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
<script lang="ts" setup>
import { inject } from 'vue';
import { inject, watch } from 'vue';
import NotificationsCarousel from '@/components/NotificationsCarousel.vue';
import ConnectionDetails from '@/components/ConnectionDetails/ConnectionDetails.vue';
import { ConnectionKey, defaultConnection } from '@/composables/useConnection';
import useConnection, { ConnectionKey, defaultConnection } from '@/composables/useConnection';
import useSocksProxy from '@/composables/useSocksProxy';
const { initProxies } = useSocksProxy();
const { connection } = useConnection();
const { isLoading } = inject(ConnectionKey, defaultConnection);
watch(connection, async () => {
// Make sure the proxies always have some value on popup start
await initProxies(
{
hostname: connection.value.server,
country: connection.value.country,
city: connection.value.city,
},
connection.value.protocol!,
);
});
</script>

<template>
Expand Down

0 comments on commit 0a20200

Please sign in to comment.