Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Oct 26, 2023
1 parent 6539d72 commit 740b4bd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 78 deletions.
17 changes: 8 additions & 9 deletions gui/src/renderer/components/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import styled from 'styled-components';
import { colors } from '../../config.json';
import { Ownership } from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { useAppContext } from '../context';
import { toRawNormalRelaySettings } from '../lib/constraint-updater';
import { useRelaySettingsUpdater } from '../lib/constraint-updater';
import {
EndpointType,
filterLocations,
Expand Down Expand Up @@ -39,8 +38,7 @@ const StyledNavigationScrollbars = styled(NavigationScrollbars)({

export default function Filter() {
const history = useHistory();
const { updateRelaySettings } = useAppContext();
const relaySettings = useSelector((state) => state.settings.relaySettings);
const relaySettingsUpdater = useRelaySettingsUpdater();

const initialProviders = useSelector(providersSelector);
const [providers, setProviders] = useState<Record<string, boolean>>(initialProviders);
Expand Down Expand Up @@ -71,12 +69,13 @@ export default function Filter() {

// Applies the changes by sending them to the daemon.
const onApply = useCallback(async () => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.providers = formattedProviderList;
settings.ownership = ownership;
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => {
settings.providers = formattedProviderList;
settings.ownership = ownership;
return settings;
});
history.pop();
}, [formattedProviderList, ownership, history, updateRelaySettings, relaySettings]);
}, [formattedProviderList, ownership, history, relaySettingsUpdater]);

return (
<BackAction action={history.pop}>
Expand Down
19 changes: 10 additions & 9 deletions gui/src/renderer/components/OpenVpnSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { messages } from '../../shared/gettext';
import log from '../../shared/logging';
import { removeNonNumericCharacters } from '../../shared/string-helpers';
import { useAppContext } from '../context';
import { toRawNormalRelaySettings } from '../lib/constraint-updater';
import { useHistory } from '../lib/history';
import { formatHtml } from '../lib/html-formatter';
import { useBoolean } from '../lib/utilityHooks';
Expand All @@ -28,6 +27,7 @@ import {
TitleBarItem,
} from './NavigationBar';
import SettingsHeader, { HeaderTitle } from './SettingsHeader';
import { useRelaySettingsUpdater } from '../lib/constraint-updater';

const MIN_MSSFIX_VALUE = 1000;
const MAX_MSSFIX_VALUE = 1450;
Expand Down Expand Up @@ -178,23 +178,24 @@ function TransportProtocolSelector() {

function useProtocolAndPortUpdater() {
const { updateRelaySettings } = useAppContext();
const relaySettings = useSelector((state) => state.settings.relaySettings);
const relaySettingsUpdater = useRelaySettingsUpdater();

const updater = useCallback(
async (protocol: RelayProtocol | null, port?: number | null) => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.openvpnConstraints.protocol = wrapConstraint(protocol);
if (port) {
settings.openvpnConstraints.port = wrapConstraint(port);
}
try {
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => {
settings.openvpnConstraints.protocol = wrapConstraint(protocol);
if (port) {
settings.openvpnConstraints.port = wrapConstraint(port);
}
return settings;
});
} catch (e) {
const error = e as Error;
log.error('Failed to update relay settings', error.message);
}
},
[updateRelaySettings, relaySettings],
[updateRelaySettings],
);

return updater;
Expand Down
11 changes: 4 additions & 7 deletions gui/src/renderer/components/VpnSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { IDnsOptions, TunnelProtocol, wrapConstraint } from '../../shared/daemon
import { messages } from '../../shared/gettext';
import log from '../../shared/logging';
import { useAppContext } from '../context';
import { toRawNormalRelaySettings } from '../lib/constraint-updater';
import { useHistory } from '../lib/history';
import { formatHtml } from '../lib/html-formatter';
import { RoutePath } from '../lib/routes';
Expand All @@ -31,6 +30,7 @@ import {
TitleBarItem,
} from './NavigationBar';
import SettingsHeader, { HeaderTitle } from './SettingsHeader';
import { useRelaySettingsUpdater } from '../lib/constraint-updater';

const StyledContent = styled.div({
display: 'flex',
Expand Down Expand Up @@ -663,19 +663,16 @@ function TunnelProtocolSetting() {
const tunnelProtocol = useSelector((state) =>
mapRelaySettingsToProtocol(state.settings.relaySettings),
);
const relaySettings = useSelector((state) => state.settings.relaySettings);
const { updateRelaySettings } = useAppContext();
const relaySettingsUpdater = useRelaySettingsUpdater();

const setTunnelProtocol = useCallback(async (tunnelProtocol: TunnelProtocol | null) => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.tunnelProtocol = wrapConstraint(tunnelProtocol);
try {
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => ({ ...settings, tunnelProtocol: wrapConstraint(tunnelProtocol) }));
} catch (e) {
const error = e as Error;
log.error('Failed to update tunnel protocol constraints', error.message);
}
}, [relaySettings]);
}, []);

const tunnelProtocolItems: Array<SelectorItem<TunnelProtocol>> = useMemo(
() => [
Expand Down
34 changes: 19 additions & 15 deletions gui/src/renderer/components/WireguardSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { messages } from '../../shared/gettext';
import log from '../../shared/logging';
import { removeNonNumericCharacters } from '../../shared/string-helpers';
import { useAppContext } from '../context';
import { toRawNormalRelaySettings } from '../lib/constraint-updater';
import { useHistory } from '../lib/history';
import { useBoolean } from '../lib/utilityHooks';
import { useSelector } from '../redux/store';
Expand All @@ -34,6 +33,7 @@ import {
TitleBarItem,
} from './NavigationBar';
import SettingsHeader, { HeaderTitle } from './SettingsHeader';
import { useRelaySettingsUpdater } from '../lib/constraint-updater';

const MIN_WIREGUARD_MTU_VALUE = 1280;
const MAX_WIREGUARD_MTU_VALUE = 1420;
Expand Down Expand Up @@ -133,7 +133,7 @@ export default function WireguardSettings() {

function PortSelector() {
const relaySettings = useSelector((state) => state.settings.relaySettings);
const { updateRelaySettings } = useAppContext();
const relaySettingsUpdater = useRelaySettingsUpdater();
const allowedPortRanges = useSelector((state) => state.settings.wireguardEndpointData.portRanges);

const wireguardPortItems = useMemo<Array<SelectorItem<number>>>(
Expand All @@ -148,16 +148,17 @@ function PortSelector() {

const setWireguardPort = useCallback(
async (port: number | null) => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.wireguardConstraints.port = wrapConstraint(port);
try {
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => {
settings.wireguardConstraints.port = wrapConstraint(port);
return settings;
});
} catch (e) {
const error = e as Error;
log.error('Failed to update relay settings', error.message);
}
},
[relaySettings],
[relaySettingsUpdater],
);

const parseValue = useCallback((port: string) => parseInt(port), []);
Expand Down Expand Up @@ -318,6 +319,7 @@ function Udp2tcpPortSetting() {

function MultihopSetting() {
const relaySettings = useSelector((state) => state.settings.relaySettings);
const relaySettingsUpdater = useRelaySettingsUpdater();
const { updateRelaySettings } = useAppContext();

const multihop = 'normal' in relaySettings ? relaySettings.normal.wireguard.useMultihop : false;
Expand All @@ -326,16 +328,17 @@ function MultihopSetting() {

const setMultihopImpl = useCallback(
async (enabled: boolean) => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.wireguardConstraints.useMultihop = enabled;
try {
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => {
settings.wireguardConstraints.useMultihop = enabled;
return settings;
});
} catch (e) {
const error = e as Error;
log.error('Failed to update WireGuard multihop settings', error.message);
}
},
[relaySettings, updateRelaySettings],
[updateRelaySettings],
);

const setMultihop = useCallback(
Expand Down Expand Up @@ -409,7 +412,7 @@ function MultihopSetting() {
}

function IpVersionSetting() {
const { updateRelaySettings } = useAppContext();
const relaySettingsUpdater = useRelaySettingsUpdater();
const relaySettings = useSelector((state) => state.settings.relaySettings);
const ipVersion = useMemo(() => {
const ipVersion = 'normal' in relaySettings ? relaySettings.normal.wireguard.ipVersion : 'any';
Expand All @@ -432,16 +435,17 @@ function IpVersionSetting() {

const setIpVersion = useCallback(
async (ipVersion: IpVersion | null) => {
const settings = toRawNormalRelaySettings(relaySettings);
settings.wireguardConstraints.ipVersion = wrapConstraint(ipVersion);
try {
await updateRelaySettings({ normal: settings });
await relaySettingsUpdater((settings) => {
settings.wireguardConstraints.ipVersion = wrapConstraint(ipVersion);
return settings;
});
} catch (e) {
const error = e as Error;
log.error('Failed to update relay settings', error.message);
}
},
[relaySettings, updateRelaySettings],
[relaySettingsUpdater],
);

return (
Expand Down
17 changes: 6 additions & 11 deletions gui/src/renderer/components/select-location/SelectLocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { sprintf } from 'sprintf-js';
import { colors } from '../../../config.json';
import { Ownership } from '../../../shared/daemon-rpc-types';
import { messages } from '../../../shared/gettext';
import { useAppContext } from '../../context';
import { toRawNormalRelaySettings } from '../../lib/constraint-updater';
import { filterSpecialLocations } from '../../lib/filter-locations';
import { useHistory } from '../../lib/history';
import { formatHtml } from '../../lib/html-formatter';
Expand Down Expand Up @@ -55,10 +53,11 @@ import {
StyledSearchBar,
} from './SelectLocationStyles';
import { SpacePreAllocationView } from './SpacePreAllocationView';
import { useRelaySettingsUpdater } from '../../lib/constraint-updater';

export default function SelectLocation() {
const history = useHistory();
const { updateRelaySettings } = useAppContext();
const relaySettingsUpdater = useRelaySettingsUpdater();
const {
saveScrollPosition,
resetScrollPositions,
Expand Down Expand Up @@ -87,20 +86,16 @@ export default function SelectLocation() {
const onClearProviders = useCallback(async () => {
resetScrollPositions();
if (relaySettings) {
const newSettings = toRawNormalRelaySettings({ normal: relaySettings });
newSettings.providers = [];
await updateRelaySettings({ normal: newSettings });
await relaySettingsUpdater((settings) => ({ ...settings, providers: [] }));
}
}, [updateRelaySettings, resetScrollPositions, relaySettings]);
}, [relaySettingsUpdater, resetScrollPositions, relaySettings]);

const onClearOwnership = useCallback(async () => {
resetScrollPositions();
if (relaySettings) {
const newSettings = toRawNormalRelaySettings({ normal: relaySettings });
newSettings.ownership = Ownership.any;
await updateRelaySettings({ normal: newSettings });
await relaySettingsUpdater((settings) => ({ ...settings, ownership: Ownership.any }));
}
}, [updateRelaySettings, resetScrollPositions, relaySettings]);
}, [relaySettingsUpdater, resetScrollPositions, relaySettings]);

const changeLocationType = useCallback(
(locationType: LocationType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import {
BridgeSettings,
RelayLocation,
RelaySettings,
wrapConstraint,
} from '../../../shared/daemon-rpc-types';
import log from '../../../shared/logging';
import { useAppContext } from '../../context';
import { toRawNormalRelaySettings } from '../../lib/constraint-updater';
import { useHistory } from '../../lib/history';
import { useSelector } from '../../redux/store';
import { LocationType, SpecialBridgeLocationType } from './select-location-types';
import { useSelectLocationContext } from './SelectLocationContainer';
import { useRelaySettingsModifier } from '../../lib/constraint-updater';

export function useOnSelectExitLocation() {
const onSelectLocation = useOnSelectLocation();
const history = useHistory();
const relaySettings = useSelector((state) => state.settings.relaySettings);
const relaySettingsModifier = useRelaySettingsModifier();
const { connectTunnel } = useAppContext();

const onSelectRelay = useCallback(
async (relayLocation: RelayLocation) => {
const settings = await relaySettingsModifier((settings) => ({ ...settings, location: wrapConstraint(relayLocation) }));
history.pop();
const settings = toRawNormalRelaySettings(relaySettings);
settings.location = { only: relayLocation };
await onSelectLocation({ normal: settings });
await connectTunnel();
},
[history, relaySettings],
[history, relaySettingsModifier],
);

const onSelectSpecial = useCallback((_location: undefined) => {
Expand All @@ -41,21 +40,25 @@ export function useOnSelectExitLocation() {
export function useOnSelectEntryLocation() {
const onSelectLocation = useOnSelectLocation();
const { setLocationType } = useSelectLocationContext();
const baseRelaySettings = useSelector((state) => state.settings.relaySettings);
const relaySettingsModifier = useRelaySettingsModifier();

const onSelectRelay = useCallback(async (entryLocation: RelayLocation) => {
setLocationType(LocationType.exit);
const settings = toRawNormalRelaySettings(baseRelaySettings);
settings.wireguardConstraints.entryLocation = { only: entryLocation };
const settings = await relaySettingsModifier((settings) => {
settings.wireguardConstraints.entryLocation = wrapConstraint(entryLocation);
return settings;
});
await onSelectLocation({ normal: settings });
}, []);
}, [relaySettingsModifier]);

const onSelectSpecial = useCallback(async (_location: 'any') => {
setLocationType(LocationType.exit);
const settings = toRawNormalRelaySettings(baseRelaySettings);
settings.wireguardConstraints.entryLocation = 'any';
const settings = await relaySettingsModifier((settings) => {
settings.wireguardConstraints.entryLocation = 'any';
return settings;
});
await onSelectLocation({ normal: settings });
}, []);
}, [relaySettingsModifier]);

return [onSelectRelay, onSelectSpecial] as const;
}
Expand All @@ -68,7 +71,7 @@ function useOnSelectLocation() {
await updateRelaySettings(relaySettings);
} catch (e) {
const error = e as Error;
log.error(`Failed to select the exit location: ${error.message}`);
log.error(`Failed to select the location: ${error.message}`);
}
}, []);
}
Expand Down
Loading

0 comments on commit 740b4bd

Please sign in to comment.