Skip to content

Commit

Permalink
Store the proxies in storage, update on popup open
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Dec 7, 2023
1 parent b1b5a2b commit 885809a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 101 deletions.
20 changes: 1 addition & 19 deletions src/components/Location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,8 @@ jest.mock('@/composables/useListProxies', () => ({
default: jest.fn(),
}));

jest.mock('@/composables/useSocksProxy', () => ({
__esModule: true,
default: () => ({ connectToSocksProxy: jest.fn() }),
}));

// TODO Fix this test
describe('Location', () => {
it('should show a loading message', () => {
(useListProxies as jest.Mock).mockReturnValueOnce({ isLoading: true });
const wrapper = mount(Location);
expect(wrapper.text()).toMatch(/loading/i);
expect(wrapper.element).toMatchSnapshot();
});

it('should show an error message', () => {
(useListProxies as jest.Mock).mockReturnValueOnce({ isError: true, error: 'Network error' });
const wrapper = mount(Location);
expect(wrapper.text()).toMatch(/network error/i);
expect(wrapper.element).toMatchSnapshot();
});

it('should show two countries', () => {
(useListProxies as jest.Mock).mockReturnValueOnce({
data: [{ country: 'Australia' }, { country: 'Austria' }],
Expand Down
13 changes: 4 additions & 9 deletions src/components/Location.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { computed } from 'vue';
import { NButton, NCollapse, NCollapseItem, NSpace } from 'naive-ui';
import getRandomSocksProxy from '@/helpers/getRandomSocksProxy';
import IconLabel from '@/components/IconLabel.vue';
import LocationTabs from '@/components/LocationTabs.vue';
import useListProxies from '@/composables/useListProxies';
Expand All @@ -15,7 +14,7 @@ import type { HistoricConnection } from '@/composables/useHistoricConnections/Hi
const { activeTabHost } = useActiveTab();
const { toggleLocations } = useLocations();
const { data: socksProxies, isLoading, isError, error } = useListProxies();
const { proxiesList } = useListProxies();
const { setCustomProxy, setGlobalProxy } = useSocksProxy();
const { storeSocksProxyUsage } = useHistoricConnections();
Expand Down Expand Up @@ -59,7 +58,7 @@ const clickSocksProxy = (
const clickCountryOrCity = (country: string, city?: string) => {
const { ipv4_address, port, hostname } = getRandomSocksProxy({
socksProxies: socksProxies.value,
socksProxies: proxiesList.value,
country,
city,
});
Expand All @@ -81,14 +80,10 @@ const selectLocation = (connection: HistoricConnection) => {
<p class="mb-8">
Select the location where you want to have {{ currentOrAllWebsites }} routed through.
</p>
<p v-if="isLoading" class="text-lg flex items-center">
<IconLabel text="Loading proxy servers list" type="spinner" />
</p>
<p v-else-if="isError">{{ error }}</p>
<div v-else>
<div>
<LocationTabs :selectLocation="selectLocation" />
<n-collapse arrow-placement="right">
<n-collapse-item v-for="{ country, cities } in socksProxies" :key="country" :name="country">
<n-collapse-item v-for="{ country, cities } in proxiesList" :key="country" :name="country">
<template #header>
<n-button quaternary @click.prevent="clickCountryOrCity(country)">
{{ country }}
Expand Down
69 changes: 0 additions & 69 deletions src/components/__snapshots__/Location.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,74 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Location should show a loading message 1`] = `
<div
data-v-app=""
>
<p
class="mb-8"
>
Select the location where you want to have all your browser traffic routed through.
</p>
<p
class="text-lg flex items-center"
>
<span>
<i
class="mr-2 n-icon"
role="img"
style="--n-bezier: cubic-bezier(.4, 0, .2, 1); font-size: 25px;"
>
<!--v-if-->
<!--v-if-->
<!--v-if-->
<!--v-if-->
<svg
class="text-info"
height="1em"
stroke="#ffffff"
viewBox="0 0 24 24"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<g
class="spinner"
>
<circle
cx="12"
cy="12"
fill="none"
r="9.5"
stroke-width="2"
/>
</g>
</svg>
<!--v-if-->
</i>
Loading proxy servers list
</span>
</p>
</div>
`;

exports[`Location should show an error message 1`] = `
<div
data-v-app=""
>
<p
class="mb-8"
>
Select the location where you want to have all your browser traffic routed through.
</p>
<p>
Network error
</p>
</div>
`;

exports[`Location should show two countries 1`] = `
<div
data-v-app=""
Expand Down
10 changes: 6 additions & 4 deletions src/composables/useListProxies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios, { AxiosError } from 'axios';
import { useQuery } from 'vue-query';
import axios from 'axios';
import useStore from './useStore';

const { proxiesList } = useStore();

export type Location = {
city: string;
Expand Down Expand Up @@ -71,10 +73,10 @@ const useListProxies = () => {
'https://api.mullvad.net/network/v1-beta1/socks-proxies',
);
const grouped = groupByCountryAndCity(data);
return sortProxiesByCountryAndCity(grouped);
proxiesList.value = sortProxiesByCountryAndCity(grouped);
};

return useQuery<Country[], AxiosError>('socksProxies', getSocksProxies);
return { getSocksProxies, proxiesList };
};

export default useListProxies;
4 changes: 4 additions & 0 deletions src/composables/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Ref } from 'vue';
import { ProxyDetails, ProxyInfoMap, ProxyInfo, ProxyDetailsMap } from '@/helpers/socksProxy.types';
import useBrowserStorageLocal from '@/composables/useBrowserStorageLocal';
import type { HistoricConnectionsMap } from './useHistoricConnections/HistoricConnections.types';
import { Country } from './useListProxies';

export type Store = {
customProxies: Ref<ProxyInfoMap>;
proxiesList: Ref<Country[]>;
customProxiesDetails: Ref<ProxyDetailsMap>;
excludedHosts: Ref<string[]>;
globalProxy: Ref<ProxyInfo>;
Expand All @@ -23,6 +25,7 @@ const useStore = (): Store => {
const globalProxy = useBrowserStorageLocal('globalProxy', {} as ProxyInfo);
const globalProxyDetails = useBrowserStorageLocal('globalProxyDetails', { socksEnabled: false });
const historicConnections = useBrowserStorageLocal('connections', {});
const proxiesList = useBrowserStorageLocal('proxiesList', [] as Country[]);
const mullvadAccount = useBrowserStorageLocal('mullvadAccount', '');
const randomProxyActive = useBrowserStorageLocal('randomProxyActive', false);
const webRTCStatus = useBrowserStorageLocal('webRTCStatus', true);
Expand All @@ -33,6 +36,7 @@ const useStore = (): Store => {
globalProxy,
globalProxyDetails,
historicConnections,
proxiesList,
mullvadAccount,
randomProxyActive,
webRTCStatus,
Expand Down
4 changes: 4 additions & 0 deletions src/popup/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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 });
Expand All @@ -23,6 +25,8 @@ watch(connection, async () => {
},
connection.value.protocol!,
);
await getSocksProxies();
});
useQueryProvider();
Expand Down

0 comments on commit 885809a

Please sign in to comment.