Skip to content

Commit

Permalink
Remove location fetch in Electron app
Browse files Browse the repository at this point in the history
  • Loading branch information
raksooo authored and Serock3 committed Dec 15, 2023
1 parent e4952b5 commit dbf73c1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 62 deletions.
11 changes: 4 additions & 7 deletions gui/src/main/daemon-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
IDevice,
IDeviceRemoval,
IDnsOptions,
ILocation,
IObfuscationEndpoint,
IOpenVpnConstraints,
IProxyEndpoint,
Expand Down Expand Up @@ -434,11 +433,6 @@ export class DaemonRpc {
await this.callEmpty(this.client.reconnectTunnel);
}

public async getLocation(): Promise<ILocation> {
const response = await this.callEmpty<grpcTypes.GeoIpLocation>(this.client.getCurrentLocation);
return response.toObject();
}

public async getState(): Promise<TunnelState> {
const response = await this.callEmpty<grpcTypes.TunnelState>(this.client.getTunnelState);
return convertFromTunnelState(response)!;
Expand Down Expand Up @@ -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, AfterDisconnect> = {
[grpcTypes.AfterDisconnect.NOTHING]: 'nothing',
Expand Down
2 changes: 0 additions & 2 deletions gui/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 17 additions & 2 deletions gui/src/main/tunnel-state.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -20,6 +20,8 @@ export default class TunnelStateHandler {
// Scheduler for discarding the assumed next state.
private tunnelStateFallbackScheduler = new Scheduler();

private lastKnownDisconnectedLocation: Partial<ILocation> | undefined;

public constructor(private delegate: TunnelStateHandlerDelegate) {}

public get tunnelState() {
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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);
}
}
Expand Down
51 changes: 9 additions & 42 deletions gui/src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export default class AppRenderer {
};

private location?: Partial<ILocation>;
private lastDisconnectedLocation?: Partial<ILocation>;
private relayList?: IRelayListWithEndpointData;
private tunnelState!: TunnelState;
private settings!: ISettings;
Expand All @@ -103,7 +102,6 @@ export default class AppRenderer {
private previousLoginState: LoginState = 'none';
private loginScheduler = new Scheduler();
private connectedToDaemon = false;
private getLocationPromise?: Promise<ILocation>;

constructor() {
log.addOutput(new ConsoleOutput(LogLevel.debug));
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
});
}

Expand Down Expand Up @@ -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.
Expand All @@ -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<ILocation | void> {
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<ILocation> {
const state = this.reduxStore.getState();
const coordinates = {
Expand Down
4 changes: 2 additions & 2 deletions gui/src/shared/daemon-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ export interface ITunnelStateRelayInfo {
}

export type TunnelState =
| { state: 'disconnected' }
| { state: 'disconnected'; location?: Partial<ILocation> }
| { state: 'connecting'; details?: ITunnelStateRelayInfo }
| { state: 'connected'; details: ITunnelStateRelayInfo }
| { state: 'disconnecting'; details: AfterDisconnect }
| { state: 'disconnecting'; details: AfterDisconnect; location?: Partial<ILocation> }
| { state: 'error'; details: ErrorState };

export interface RelayLocationCountry extends Partial<RelayLocationCustomList> {
Expand Down
4 changes: 0 additions & 4 deletions gui/src/shared/ipc-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
IDevice,
IDeviceRemoval,
IDnsOptions,
ILocation,
IRelayListWithEndpointData,
ISettings,
ObfuscationSettings,
Expand Down Expand Up @@ -153,9 +152,6 @@ export const ipcSchema = {
showOpenDialog: invoke<Electron.OpenDialogOptions, Electron.OpenDialogReturnValue>(),
showLaunchDaemonSettings: invoke<void, void>(),
},
location: {
get: invoke<void, ILocation>(),
},
tunnel: {
'': notifyRenderer<TunnelState>(),
connect: invoke<void, void>(),
Expand Down
4 changes: 1 addition & 3 deletions gui/test/e2e/setup/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit dbf73c1

Please sign in to comment.