summaryrefslogtreecommitdiffhomepage
path: root/gui/src
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2019-09-10 14:18:30 +0200
committerAndrej Mihajlov <and@mullvad.net>2019-09-11 14:21:07 +0200
commit5f29340d148d7574d072766f2293f30edfa8e704 (patch)
treed631d48d52058e9cd7bc2472ff27a6db47dea5f6 /gui/src
parentdbf9925d8505de938107bfa5973dbed9d6a6dfab (diff)
downloadmullvadvpn-5f29340d148d7574d072766f2293f30edfa8e704.tar.xz
mullvadvpn-5f29340d148d7574d072766f2293f30edfa8e704.zip
Add Constraint<T> and LiftedConstraint<T> helper types
Diffstat (limited to 'gui/src')
-rw-r--r--gui/src/renderer/lib/relay-settings-builder.ts14
-rw-r--r--gui/src/renderer/redux/settings/reducers.ts11
-rw-r--r--gui/src/shared/daemon-rpc-types.ts34
3 files changed, 25 insertions, 34 deletions
diff --git a/gui/src/renderer/lib/relay-settings-builder.ts b/gui/src/renderer/lib/relay-settings-builder.ts
index 08ecef720e..344ddff267 100644
--- a/gui/src/renderer/lib/relay-settings-builder.ts
+++ b/gui/src/renderer/lib/relay-settings-builder.ts
@@ -1,6 +1,8 @@
import {
+ Constraint,
IOpenVpnConstraints,
IWireguardConstraints,
+ LiftedConstraint,
RelayLocation,
RelayProtocol,
RelaySettingsNormalUpdate,
@@ -13,7 +15,7 @@ interface ILocationBuilder<Self> {
city: (country: string, city: string) => Self;
hostname: (country: string, city: string, hostname: string) => Self;
any: () => Self;
- fromRaw: (location: 'any' | RelayLocation) => Self;
+ fromRaw: (location: LiftedConstraint<RelayLocation>) => Self;
}
interface IExactOrAny<T, Self> {
@@ -73,7 +75,7 @@ class NormalRelaySettingsBuilder {
this.payload.location = 'any';
return this;
},
- fromRaw(location: 'any' | RelayLocation) {
+ fromRaw(location: LiftedConstraint<RelayLocation>) {
if (location === 'any') {
return this.any();
} else if ('hostname' in location) {
@@ -118,7 +120,7 @@ class NormalRelaySettingsBuilder {
}
};
- const updateTunnelProtocol = (next: { only: TunnelProtocol } | 'any' | undefined) => {
+ const updateTunnelProtocol = (next?: Constraint<TunnelProtocol>) => {
this.payload.tunnelProtocol = next;
};
@@ -126,7 +128,7 @@ class NormalRelaySettingsBuilder {
openvpn: (configurator: (configurator: IOpenVPNConfigurator) => void) => {
const openvpnBuilder: IOpenVPNConfigurator = {
get port() {
- const apply = (port: 'any' | { only: number }) => {
+ const apply = (port: Constraint<number>) => {
updateOpenvpn({ port });
return this;
};
@@ -136,7 +138,7 @@ class NormalRelaySettingsBuilder {
};
},
get protocol() {
- const apply = (protocol: 'any' | { only: RelayProtocol }) => {
+ const apply = (protocol: Constraint<RelayProtocol>) => {
updateOpenvpn({ protocol });
return this;
};
@@ -155,7 +157,7 @@ class NormalRelaySettingsBuilder {
wireguard: (configurator: (configurator: IWireguardConfigurator) => void) => {
const wireguardBuilder: IWireguardConfigurator = {
get port() {
- const apply = (port: 'any' | { only: number }) => {
+ const apply = (port: Constraint<number>) => {
updateWireguard({ port });
return this;
};
diff --git a/gui/src/renderer/redux/settings/reducers.ts b/gui/src/renderer/redux/settings/reducers.ts
index 5d7f37fb50..ff7d531652 100644
--- a/gui/src/renderer/redux/settings/reducers.ts
+++ b/gui/src/renderer/redux/settings/reducers.ts
@@ -2,6 +2,7 @@ import log from 'electron-log';
import {
BridgeState,
KeygenEvent,
+ LiftedConstraint,
RelayLocation,
RelayProtocol,
TunnelProtocol,
@@ -12,14 +13,14 @@ import { ReduxAction } from '../store';
export type RelaySettingsRedux =
| {
normal: {
- tunnelProtocol: 'any' | TunnelProtocol;
- location: 'any' | RelayLocation;
+ tunnelProtocol: LiftedConstraint<TunnelProtocol>;
+ location: LiftedConstraint<RelayLocation>;
openvpn: {
- port: 'any' | number;
- protocol: 'any' | RelayProtocol;
+ port: LiftedConstraint<number>;
+ protocol: LiftedConstraint<RelayProtocol>;
};
wireguard: {
- port: 'any' | number;
+ port: LiftedConstraint<number>;
};
};
}
diff --git a/gui/src/shared/daemon-rpc-types.ts b/gui/src/shared/daemon-rpc-types.ts
index b16355af8f..c2368364ac 100644
--- a/gui/src/shared/daemon-rpc-types.ts
+++ b/gui/src/shared/daemon-rpc-types.ts
@@ -50,11 +50,11 @@ export function tunnelTypeToString(tunnel: TunnelType): string {
export type RelayProtocol = 'tcp' | 'udp';
-export function liftConstraint<T>(constraint: 'any' | { only: T }): 'any' | T {
- if (constraint === 'any') {
- return 'any';
- }
- return constraint.only;
+export type Constraint<T> = 'any' | { only: T };
+export type LiftedConstraint<T> = 'any' | T;
+
+export function liftConstraint<T>(constraint: Constraint<T>): LiftedConstraint<T> {
+ return constraint === 'any' ? constraint : constraint.only;
}
export type ProxyType = 'shadowsocks' | 'custom';
@@ -106,27 +106,19 @@ export type RelayLocation =
| { country: string };
export interface IOpenVpnConstraints {
- port: 'any' | { only: number };
- protocol: 'any' | { only: RelayProtocol };
+ port: Constraint<number>;
+ protocol: Constraint<RelayProtocol>;
}
export interface IWireguardConstraints {
- port: 'any' | { only: number };
+ port: Constraint<number>;
}
export type TunnelProtocol = 'wireguard' | 'openvpn';
interface IRelaySettingsNormal<OpenVpn, Wireguard> {
- location:
- | 'any'
- | {
- only: RelayLocation;
- };
- tunnelProtocol:
- | 'any'
- | {
- only: TunnelProtocol;
- };
+ location: Constraint<RelayLocation>;
+ tunnelProtocol: Constraint<TunnelProtocol>;
openvpnConstraints: OpenVpn;
wireguardConstraints: Wireguard;
}
@@ -311,11 +303,7 @@ export interface IWireguardPublicKey {
export type BridgeState = 'auto' | 'on' | 'off';
export interface IBridgeConstraints {
- location:
- | 'any'
- | {
- only: RelayLocation;
- };
+ location: Constraint<RelayLocation>;
}
export type BridgeSettings = { normal: IBridgeConstraints } | { custom: ProxySettings };