diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2021-09-06 14:39:28 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2021-09-06 14:39:28 +0200 |
| commit | 29d599b9a54c20cbb8a9aeca75c1bef440d762eb (patch) | |
| tree | ca54c69232cefcbc33c50a6babe6c2aa0692e7a8 /gui/src/main | |
| parent | 4ba1552c25a84dcf4a7abb4ee109804b2722e3ec (diff) | |
| parent | 8659a3ec949485399155fb9bde4ceed7c79b6dd9 (diff) | |
| download | mullvadvpn-29d599b9a54c20cbb8a9aeca75c1bef440d762eb.tar.xz mullvadvpn-29d599b9a54c20cbb8a9aeca75c1bef440d762eb.zip | |
Merge branch 'fix-disconnecting-info'
Diffstat (limited to 'gui/src/main')
| -rw-r--r-- | gui/src/main/index.ts | 103 |
1 files changed, 42 insertions, 61 deletions
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 8845422e6b..8fa09576c7 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -26,7 +26,6 @@ import { IAccountData, IAppVersionInfo, IDnsOptions, - ILocation, IRelayList, ISettings, IWireguardPublicKey, @@ -125,6 +124,9 @@ class ApplicationMain { private accountData?: IAccountData = undefined; private accountHistory?: AccountToken = undefined; private tunnelState: TunnelState = { state: 'disconnected' }; + private lastIgnoredTunnelState?: TunnelState; + private ignoreTunnelStatesUntil?: TunnelState['state']; + private ignoreTunnelStateFallbackScheduler = new Scheduler(); private settings: ISettings = { accountToken: undefined, allowLan: false, @@ -179,10 +181,7 @@ class ApplicationMain { }, }; private guiSettings = new GuiSettings(); - private location?: ILocation; - private lastDisconnectedLocation?: ILocation; private tunnelStateExpectation?: Expectation; - private getLocationPromise?: Promise<ILocation>; private relays: IRelayList = { countries: [] }; @@ -624,6 +623,10 @@ class ApplicationMain { // Reset the daemon event listener since it's going to be invalidated on disconnect this.daemonEventListener = undefined; + this.ignoreTunnelStatesUntil = undefined; + this.lastIgnoredTunnelState = undefined; + this.autoConnectFallbackScheduler.cancel(); + if (wasConnected) { this.connectedToDaemon = false; @@ -729,10 +732,30 @@ class ApplicationMain { this.wireguardKeygenEventAutoConnect(); } + private setIgnoreTunnelStatesUntil(state: TunnelState['state']) { + this.ignoreTunnelStatesUntil = state; + this.ignoreTunnelStateFallbackScheduler.schedule(() => { + if (this.lastIgnoredTunnelState) { + this.ignoreTunnelStatesUntil = undefined; + this.setTunnelState(this.lastIgnoredTunnelState); + this.lastIgnoredTunnelState = undefined; + } + }, 3000); + } + private setTunnelState(newState: TunnelState) { + if (this.ignoreTunnelStatesUntil) { + if (this.ignoreTunnelStatesUntil === newState.state) { + this.ignoreTunnelStatesUntil = undefined; + this.lastIgnoredTunnelState = undefined; + } else { + this.lastIgnoredTunnelState = newState; + return; + } + } + this.tunnelState = newState; this.updateTrayIcon(newState, this.settings.blockWhenDisconnected); - void this.updateLocation(); if (process.platform === 'linux') { this.tray?.setContextMenu(this.createTrayContextMenu()); @@ -799,14 +822,6 @@ class ApplicationMain { } } - private setLocation(newLocation: ILocation) { - this.location = newLocation; - - if (this.windowController) { - IpcMainEventChannel.location.notify(this.windowController.webContents, newLocation); - } - } - private setRelays( newRelayList: IRelayList, relaySettings: RelaySettings, @@ -981,50 +996,6 @@ class ApplicationMain { } } - private async updateLocation() { - const tunnelState = this.tunnelState; - - if (tunnelState.state === 'connected' || tunnelState.state === 'connecting') { - // Location was broadcasted with the tunnel state, but it doesn't contain the relay out IP - // address, so it will have to be fetched afterwards - if (tunnelState.details && tunnelState.details.location) { - this.setLocation(tunnelState.details.location); - } - } else if (tunnelState.state === 'disconnected') { - // It may take some time to fetch the new user location. - // So take the user to the last known location when disconnected. - if (this.lastDisconnectedLocation) { - this.setLocation(this.lastDisconnectedLocation); - } - } - - if (tunnelState.state === 'connected' || tunnelState.state === 'disconnected') { - try { - // Fetch the new user location - const getLocationPromise = (this.getLocationPromise = this.daemonRpc.getLocation()); - const location = await getLocationPromise; - // If the location is currently unavailable, do nothing! This only ever - // happens when a custom relay is set or we are in a blocked state. - if (!location) { - return; - } - - // Cache the user location - // Note: hostname is only set for relay servers. - if (location.hostname === null) { - this.lastDisconnectedLocation = location; - } - - // Broadcast the new location if it is the result of the most recent call to getLocation. - if (getLocationPromise === this.getLocationPromise) { - this.setLocation(location); - } - } catch (error) { - log.error(`Failed to update the location: ${error.message}`); - } - } - } - private trayIconType(tunnelState: TunnelState, blockWhenDisconnected: boolean): TrayIconType { switch (tunnelState.state) { case 'connected': @@ -1102,7 +1073,6 @@ class ApplicationMain { accountHistory: this.accountHistory, tunnelState: this.tunnelState, settings: this.settings, - location: this.location, relayListPair: { relays: this.processRelaysForPresentation( this.relays, @@ -1158,9 +1128,20 @@ class ApplicationMain { return this.setAutoStart(autoStart); }); - IpcMainEventChannel.tunnel.handleConnect(() => this.daemonRpc.connectTunnel()); - IpcMainEventChannel.tunnel.handleDisconnect(() => this.daemonRpc.disconnectTunnel()); - IpcMainEventChannel.tunnel.handleReconnect(() => this.daemonRpc.reconnectTunnel()); + IpcMainEventChannel.location.handleGet(() => this.daemonRpc.getLocation()); + + IpcMainEventChannel.tunnel.handleConnect(() => { + this.setIgnoreTunnelStatesUntil('connecting'); + return this.daemonRpc.connectTunnel(); + }); + IpcMainEventChannel.tunnel.handleDisconnect(() => { + this.setIgnoreTunnelStatesUntil('disconnecting'); + return this.daemonRpc.disconnectTunnel(); + }); + IpcMainEventChannel.tunnel.handleReconnect(() => { + this.setIgnoreTunnelStatesUntil('connecting'); + return this.daemonRpc.reconnectTunnel(); + }); IpcMainEventChannel.guiSettings.handleSetEnableSystemNotifications((flag: boolean) => { this.guiSettings.enableSystemNotifications = flag; |
