diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2022-08-08 10:41:06 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2022-08-22 08:34:37 +0200 |
| commit | ca0ac2495b2d6de906b8ea1ec112af908280fab3 (patch) | |
| tree | ea0a54a080d26625b242598a5bbb462290fe2167 /gui | |
| parent | c28b823651dbd4acf90f07f312d422ac91b73b97 (diff) | |
| download | mullvadvpn-ca0ac2495b2d6de906b8ea1ec112af908280fab3.tar.xz mullvadvpn-ca0ac2495b2d6de906b8ea1ec112af908280fab3.zip | |
Move relay related code to it's own class
Diffstat (limited to 'gui')
| -rw-r--r-- | gui/src/main/index.ts | 103 | ||||
| -rw-r--r-- | gui/src/main/relay-list.ts | 109 |
2 files changed, 119 insertions, 93 deletions
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 96c2665714..5463408673 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -19,12 +19,9 @@ import { IAccountData, IDeviceRemoval, IDnsOptions, - IRelayList, ISettings, - liftConstraint, ObfuscationType, Ownership, - RelaySettings, RelaySettingsUpdate, TunnelState, } from '../shared/daemon-rpc-types'; @@ -63,6 +60,7 @@ import { import NotificationController, { NotificationControllerDelegate } from './notification-controller'; import * as problemReport from './problem-report'; import ReconnectionBackoff from './reconnection-backoff'; +import RelayList from './relay-list'; import UserInterface, { UserInterfaceDelegate } from './user-interface'; import Version, { VersionDelegate } from './version'; @@ -99,6 +97,8 @@ class ApplicationMain private version = new Version(this, UPDATE_NOTIFICATION_DISABLED); + private relayList = new RelayList(); + // True while file pickers are displayed which is used to decide if the Browser window should be // hidden when losing focus. private browsingFiles = false; @@ -192,8 +192,6 @@ class ApplicationMain private guiSettings = new GuiSettings(); private tunnelStateExpectation?: Expectation; - private relays: IRelayList = { countries: [] }; - // The UI locale which is set once from onReady handler private locale = 'en'; @@ -628,7 +626,7 @@ class ApplicationMain // fetch relays try { - this.setRelays( + this.relayList.setRelays( await this.daemonRpc.getRelayLocations(), this.settings.relaySettings, this.settings.bridgeState, @@ -730,7 +728,7 @@ class ApplicationMain } else if ('settings' in daemonEvent) { this.setSettings(daemonEvent.settings); } else if ('relayList' in daemonEvent) { - this.setRelays( + this.relayList.setRelays( daemonEvent.relayList, this.settings.relaySettings, this.settings.bridgeState, @@ -846,7 +844,7 @@ class ApplicationMain // since settings can have the relay constraints changed, the relay // list should also be updated - this.setRelays(this.relays, newSettings.relaySettings, newSettings.bridgeState); + this.relayList.updateSettings(newSettings.relaySettings, newSettings.bridgeState); } private async updateSplitTunnelingApplications(appList: string[]): Promise<void> { @@ -858,87 +856,6 @@ class ApplicationMain IpcMainEventChannel.windowsSplitTunneling.notify?.(applications); } - private setRelays( - newRelayList: IRelayList, - relaySettings: RelaySettings, - bridgeState: BridgeState, - ) { - this.relays = newRelayList; - - const filteredRelays = this.processRelaysForPresentation(newRelayList, relaySettings); - const filteredBridges = this.processBridgesForPresentation(newRelayList, bridgeState); - - IpcMainEventChannel.relays.notify?.({ relays: filteredRelays, bridges: filteredBridges }); - } - - private processRelaysForPresentation( - relayList: IRelayList, - relaySettings: RelaySettings, - ): IRelayList { - const tunnelProtocol = - 'normal' in relaySettings ? liftConstraint(relaySettings.normal.tunnelProtocol) : undefined; - - const filteredCountries = relayList.countries - .map((country) => ({ - ...country, - cities: country.cities - .map((city) => ({ - ...city, - relays: city.relays.filter((relay) => { - if (relay.endpointType != 'bridge') { - switch (tunnelProtocol) { - case 'openvpn': - return relay.endpointType == 'openvpn'; - - case 'wireguard': - return relay.endpointType == 'wireguard'; - - case 'any': { - const useMultihop = - 'normal' in relaySettings && - relaySettings.normal.wireguardConstraints.useMultihop; - return !useMultihop || relay.endpointType == 'wireguard'; - } - default: - return false; - } - } else { - return false; - } - }), - })) - .filter((city) => city.relays.length > 0), - })) - .filter((country) => country.cities.length > 0); - - return { - countries: filteredCountries, - }; - } - - private processBridgesForPresentation( - relayList: IRelayList, - bridgeState: BridgeState, - ): IRelayList { - if (bridgeState === 'on') { - const filteredCountries = relayList.countries - .map((country) => ({ - ...country, - cities: country.cities - .map((city) => ({ - ...city, - relays: city.relays.filter((relay) => relay.endpointType == 'bridge'), - })) - .filter((city) => city.relays.length > 0), - })) - .filter((country) => country.cities.length > 0); - - return { countries: filteredCountries }; - } else { - return { countries: [] }; - } - } - private handleDeviceEvent(deviceEvent: DeviceEvent) { this.deviceState = deviceEvent.deviceState; @@ -978,10 +895,10 @@ class ApplicationMain settings: this.settings, isPerformingPostUpgrade: this.isPerformingPostUpgrade, deviceState: this.deviceState, - relayListPair: { - relays: this.processRelaysForPresentation(this.relays, this.settings.relaySettings), - bridges: this.processBridgesForPresentation(this.relays, this.settings.bridgeState), - }, + relayListPair: this.relayList.getProcessedRelays( + this.settings.relaySettings, + this.settings.bridgeState, + ), currentVersion: this.version.currentVersion, upgradeVersion: this.version.upgradeVersion, guiSettings: this.guiSettings.state, diff --git a/gui/src/main/relay-list.ts b/gui/src/main/relay-list.ts new file mode 100644 index 0000000000..8583f58776 --- /dev/null +++ b/gui/src/main/relay-list.ts @@ -0,0 +1,109 @@ +import { BridgeState, IRelayList, liftConstraint, RelaySettings } from '../shared/daemon-rpc-types'; +import { IpcMainEventChannel } from './ipc-event-channel'; + +interface RelayLists { + relays: IRelayList; + bridges: IRelayList; +} + +export default class RelayList { + private relays: IRelayList = { countries: [] }; + + public setRelays( + newRelayList: IRelayList, + relaySettings: RelaySettings, + bridgeState: BridgeState, + ) { + this.relays = newRelayList; + + const processedRelays = this.processRelays(newRelayList, relaySettings, bridgeState); + IpcMainEventChannel.relays.notify?.(processedRelays); + } + + public updateSettings(relaySettings: RelaySettings, bridgeState: BridgeState) { + this.setRelays(this.relays, relaySettings, bridgeState); + } + + public getProcessedRelays(relaySettings: RelaySettings, bridgeState: BridgeState) { + return this.processRelays(this.relays, relaySettings, bridgeState); + } + + private processRelays( + relayList: IRelayList, + relaySettings: RelaySettings, + bridgeState: BridgeState, + ): RelayLists { + const filteredRelays = this.processRelaysForPresentation(relayList, relaySettings); + const filteredBridges = this.processBridgesForPresentation(relayList, bridgeState); + + return { relays: filteredRelays, bridges: filteredBridges }; + } + + private processRelaysForPresentation( + relayList: IRelayList, + relaySettings: RelaySettings, + ): IRelayList { + const tunnelProtocol = + 'normal' in relaySettings ? liftConstraint(relaySettings.normal.tunnelProtocol) : undefined; + + const filteredCountries = relayList.countries + .map((country) => ({ + ...country, + cities: country.cities + .map((city) => ({ + ...city, + relays: city.relays.filter((relay) => { + if (relay.endpointType != 'bridge') { + switch (tunnelProtocol) { + case 'openvpn': + return relay.endpointType == 'openvpn'; + + case 'wireguard': + return relay.endpointType == 'wireguard'; + + case 'any': { + const useMultihop = + 'normal' in relaySettings && + relaySettings.normal.wireguardConstraints.useMultihop; + return !useMultihop || relay.endpointType == 'wireguard'; + } + default: + return false; + } + } else { + return false; + } + }), + })) + .filter((city) => city.relays.length > 0), + })) + .filter((country) => country.cities.length > 0); + + return { + countries: filteredCountries, + }; + } + + private processBridgesForPresentation( + relayList: IRelayList, + bridgeState: BridgeState, + ): IRelayList { + if (bridgeState === 'on') { + const filteredCountries = relayList.countries + .map((country) => ({ + ...country, + cities: country.cities + .map((city) => ({ + ...city, + relays: city.relays.filter((relay) => relay.endpointType == 'bridge'), + })) + .filter((city) => city.relays.length > 0), + })) + .filter((country) => country.cities.length > 0); + + return { countries: filteredCountries }; + } else { + return { countries: [] }; + } + } +} |
