From a60f9cdd594772bee04724f3a17fd16f6fcf1596 Mon Sep 17 00:00:00 2001 From: Oskar Nyberg Date: Mon, 18 Dec 2023 12:12:17 +0100 Subject: [PATCH] Remove location fetch in Electron app --- gui/src/main/daemon-rpc.ts | 11 +++---- gui/src/main/index.ts | 2 -- gui/src/main/tunnel-state.ts | 19 +++++++++-- gui/src/renderer/app.tsx | 51 ++++++------------------------ gui/src/shared/daemon-rpc-types.ts | 4 +-- gui/src/shared/ipc-schema.ts | 4 --- gui/test/e2e/setup/main.ts | 4 +-- 7 files changed, 33 insertions(+), 62 deletions(-) diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index 174b2aea77ca..ad7b60dec202 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -33,7 +33,6 @@ import { IDevice, IDeviceRemoval, IDnsOptions, - ILocation, IObfuscationEndpoint, IOpenVpnConstraints, IProxyEndpoint, @@ -434,11 +433,6 @@ export class DaemonRpc { await this.callEmpty(this.client.reconnectTunnel); } - public async getLocation(): Promise { - const response = await this.callEmpty(this.client.getCurrentLocation); - return response.toObject(); - } - public async getState(): Promise { const response = await this.callEmpty(this.client.getTunnelState); return convertFromTunnelState(response)!; @@ -851,7 +845,10 @@ function convertFromTunnelState(tunnelState: grpcTypes.TunnelState): TunnelState case grpcTypes.TunnelState.StateCase.STATE_NOT_SET: return undefined; case grpcTypes.TunnelState.StateCase.DISCONNECTED: - return { state: 'disconnected' }; + return { + state: 'disconnected', + location: tunnelStateObject.disconnected!.disconnectedLocation, + }; case grpcTypes.TunnelState.StateCase.DISCONNECTING: { const detailsMap: Record = { [grpcTypes.AfterDisconnect.NOTHING]: 'nothing', diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index da3d677cc0fc..6c3a6373fb04 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -728,8 +728,6 @@ class ApplicationMain navigationHistory: this.navigationHistory, })); - IpcMainEventChannel.location.handleGet(() => this.daemonRpc.getLocation()); - IpcMainEventChannel.tunnel.handleConnect(this.connectTunnel); IpcMainEventChannel.tunnel.handleReconnect(this.reconnectTunnel); IpcMainEventChannel.tunnel.handleDisconnect(this.disconnectTunnel); diff --git a/gui/src/main/tunnel-state.ts b/gui/src/main/tunnel-state.ts index 029386f3b792..682911881865 100644 --- a/gui/src/main/tunnel-state.ts +++ b/gui/src/main/tunnel-state.ts @@ -1,5 +1,5 @@ import { connectEnabled, disconnectEnabled, reconnectEnabled } from '../shared/connect-helper'; -import { TunnelState } from '../shared/daemon-rpc-types'; +import { ILocation, TunnelState } from '../shared/daemon-rpc-types'; import { Scheduler } from '../shared/scheduler'; export interface TunnelStateProvider { @@ -20,6 +20,8 @@ export default class TunnelStateHandler { // Scheduler for discarding the assumed next state. private tunnelStateFallbackScheduler = new Scheduler(); + private lastKnownDisconnectedLocation: Partial | undefined; + public constructor(private delegate: TunnelStateHandlerDelegate) {} public get tunnelState() { @@ -37,7 +39,9 @@ export default class TunnelStateHandler { this.tunnelStateFallback = this.tunnelState; this.setTunnelState( - state === 'disconnecting' ? { state, details: 'nothing' as const } : { state }, + state === 'disconnecting' + ? { state, details: 'nothing' as const, location: this.lastKnownDisconnectedLocation } + : { state }, ); this.tunnelStateFallbackScheduler.schedule(() => { @@ -67,6 +71,17 @@ export default class TunnelStateHandler { this.expectNextTunnelState('connecting'); this.tunnelStateFallback = newState; } else { + if (newState.state === 'disconnected' && newState.location !== undefined) { + this.lastKnownDisconnectedLocation = newState.location; + } + + if ( + newState.state === 'disconnecting' || + (newState.state === 'disconnected' && newState.location === undefined) + ) { + newState.location = this.lastKnownDisconnectedLocation; + } + this.setTunnelState(newState); } } diff --git a/gui/src/renderer/app.tsx b/gui/src/renderer/app.tsx index 584e80ed4f4b..b5eacf17b36b 100644 --- a/gui/src/renderer/app.tsx +++ b/gui/src/renderer/app.tsx @@ -94,7 +94,6 @@ export default class AppRenderer { }; private location?: Partial; - private lastDisconnectedLocation?: Partial; private relayList?: IRelayListWithEndpointData; private tunnelState!: TunnelState; private settings!: ISettings; @@ -103,7 +102,6 @@ export default class AppRenderer { private previousLoginState: LoginState = 'none'; private loginScheduler = new Scheduler(); private connectedToDaemon = false; - private getLocationPromise?: Promise; constructor() { log.addOutput(new ConsoleOutput(LogLevel.debug)); @@ -255,7 +253,7 @@ export default class AppRenderer { ); } - void this.updateLocation(); + this.updateLocation(); if (initialState.navigationHistory) { // Set last action to POP to trigger automatic scrolling to saved coordinates. @@ -765,7 +763,7 @@ export default class AppRenderer { } // Update the location when entering a new tunnel state since it's likely changed. - void this.updateLocation(); + this.updateLocation(); }); } @@ -944,22 +942,16 @@ export default class AppRenderer { this.reduxActions.userInterface.setForceShowChanges(forceShowChanges); } - private async updateLocation() { + private updateLocation() { switch (this.tunnelState.state) { - case 'disconnected': { - if (this.lastDisconnectedLocation) { - this.setLocation(this.lastDisconnectedLocation); - } - const location = await this.fetchLocation(); - if (location) { - this.setLocation(location); - this.lastDisconnectedLocation = location; + case 'disconnected': + if (this.tunnelState.location) { + this.setLocation(this.tunnelState.location); } break; - } case 'disconnecting': - if (this.lastDisconnectedLocation) { - this.setLocation(this.lastDisconnectedLocation); + if (this.tunnelState.location) { + this.setLocation(this.tunnelState.location); } else { // If there's no previous location while disconnecting we remove the location. We keep the // coordinates to prevent the map from jumping around. @@ -968,38 +960,13 @@ export default class AppRenderer { } break; case 'connecting': - this.setLocation(this.tunnelState.details?.location ?? this.getLocationFromConstraints()); - break; case 'connected': { - if (this.tunnelState.details?.location) { - this.setLocation(this.tunnelState.details.location); - } - const location = await this.fetchLocation(); - if (location) { - this.setLocation(location); - } + this.setLocation(this.tunnelState.details?.location ?? this.getLocationFromConstraints()); break; } } } - private async fetchLocation(): Promise { - try { - // Fetch the new user location - const getLocationPromise = IpcRendererEventChannel.location.get(); - this.getLocationPromise = getLocationPromise; - 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 && getLocationPromise === this.getLocationPromise) { - return location; - } - } catch (e) { - const error = e as Error; - log.error(`Failed to update the location: ${error.message}`); - } - } - private getLocationFromConstraints(): Partial { const state = this.reduxStore.getState(); const coordinates = { diff --git a/gui/src/shared/daemon-rpc-types.ts b/gui/src/shared/daemon-rpc-types.ts index 6977be437520..e83f9e5afc2a 100644 --- a/gui/src/shared/daemon-rpc-types.ts +++ b/gui/src/shared/daemon-rpc-types.ts @@ -181,10 +181,10 @@ export interface ITunnelStateRelayInfo { } export type TunnelState = - | { state: 'disconnected' } + | { state: 'disconnected'; location?: Partial } | { state: 'connecting'; details?: ITunnelStateRelayInfo } | { state: 'connected'; details: ITunnelStateRelayInfo } - | { state: 'disconnecting'; details: AfterDisconnect } + | { state: 'disconnecting'; details: AfterDisconnect; location?: Partial } | { state: 'error'; details: ErrorState }; export interface RelayLocationCountry extends Partial { diff --git a/gui/src/shared/ipc-schema.ts b/gui/src/shared/ipc-schema.ts index 994bacdcd66a..ecf34e93fb1e 100644 --- a/gui/src/shared/ipc-schema.ts +++ b/gui/src/shared/ipc-schema.ts @@ -15,7 +15,6 @@ import { IDevice, IDeviceRemoval, IDnsOptions, - ILocation, IRelayListWithEndpointData, ISettings, ObfuscationSettings, @@ -153,9 +152,6 @@ export const ipcSchema = { showOpenDialog: invoke(), showLaunchDaemonSettings: invoke(), }, - location: { - get: invoke(), - }, tunnel: { '': notifyRenderer(), connect: invoke(), diff --git a/gui/test/e2e/setup/main.ts b/gui/test/e2e/setup/main.ts index 430dfdb70d56..e645e8b7946f 100644 --- a/gui/test/e2e/setup/main.ts +++ b/gui/test/e2e/setup/main.ts @@ -153,7 +153,7 @@ class ApplicationMain { autoStart: false, accountData: this.accountData, accountHistory: undefined, - tunnelState: { state: 'disconnected' }, + tunnelState: { state: 'disconnected', location: this.location }, settings: this.settings, isPerformingPostUpgrade: false, deviceState: this.deviceState, @@ -174,8 +174,6 @@ class ApplicationMain { scrollPositions: {}, })); - IpcMainEventChannel.location.handleGet(() => Promise.resolve(this.location)); - IpcMainEventChannel.guiSettings.handleSetPreferredLocale((locale) => { this.updateCurrentLocale(locale); IpcMainEventChannel.guiSettings.notify?.(this.guiSettings);