diff options
Diffstat (limited to 'gui/src/main')
| -rw-r--r-- | gui/src/main/daemon-rpc.ts | 54 | ||||
| -rw-r--r-- | gui/src/main/index.ts | 44 |
2 files changed, 79 insertions, 19 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index eab5559ad3..f17505bd80 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -46,7 +46,9 @@ import { VoucherResponse, TunnelProtocol, IDnsOptions, - IDeviceConfig, + DeviceConfig, + IDevice, + IDeviceRemoval, } from '../shared/daemon-rpc-types'; import log from '../shared/logging'; @@ -484,6 +486,37 @@ export class DaemonRpc { await this.callEmpty(this.client.checkVolumes); } + public async getDevice(): Promise<DeviceConfig> { + try { + const response = await this.callEmpty<grpcTypes.DeviceConfig>(this.client.getDevice); + return convertFromDeviceConfig(response); + } catch (e) { + const error = e as grpc.ServiceError; + if (error.code === grpc.status.NOT_FOUND) { + return undefined; + } else { + throw error; + } + } + } + + public async listDevices(accountToken: AccountToken): Promise<Array<IDevice>> { + const response = await this.callString<grpcTypes.DeviceList>( + this.client.listDevices, + accountToken, + ); + + return response.toObject().devicesList; + } + + public async removeDevice(deviceRemoval: IDeviceRemoval): Promise<void> { + const grpcDeviceRemoval = new grpcTypes.DeviceRemoval(); + grpcDeviceRemoval.setAccountToken(deviceRemoval.accountToken); + grpcDeviceRemoval.setDeviceId(deviceRemoval.deviceId); + + await this.call<grpcTypes.DeviceRemoval, Empty>(this.client.removeDevice, grpcDeviceRemoval); + } + private subscriptionId(): number { const current = this.nextSubscriptionId; this.nextSubscriptionId += 1; @@ -1110,7 +1143,7 @@ function convertFromDaemonEvent(data: grpcTypes.DaemonEvent): DaemonEvent { const deviceConfig = data.getDevice(); if (deviceConfig !== undefined) { - return { deviceConfig: convertFromDeviceConfig(deviceConfig) }; + return { deviceConfig: convertFromDeviceEvent(deviceConfig) }; } return { @@ -1313,12 +1346,17 @@ function convertToTransportProtocol(protocol: RelayProtocol): grpcTypes.Transpor } } -function convertFromDeviceConfig(deviceEvent: grpcTypes.DeviceEvent): IDeviceConfig { - const deviceConfig = deviceEvent.getDevice(); - return { - accountToken: deviceConfig?.getAccountToken(), - device: deviceConfig?.getDevice()?.toObject(), - }; +function convertFromDeviceEvent(deviceEvent: grpcTypes.DeviceEvent): DeviceConfig { + return convertFromDeviceConfig(deviceEvent.getDevice()); +} + +function convertFromDeviceConfig(deviceConfig?: grpcTypes.DeviceConfig): DeviceConfig { + return ( + deviceConfig && { + accountToken: deviceConfig.getAccountToken(), + device: deviceConfig.getDevice()?.toObject(), + } + ); } function ensureExists<T>(value: T | undefined, errorMessage: string): T { diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 07afda535c..f8a9b2497f 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -27,7 +27,8 @@ import { DaemonEvent, IAccountData, IAppVersionInfo, - IDeviceConfig, + DeviceConfig, + IDeviceRemoval, IDnsOptions, IRelayList, ISettings, @@ -197,7 +198,7 @@ class ApplicationMain { }, }, }; - private deviceConfig: IDeviceConfig = { accountToken: undefined, device: undefined }; + private deviceConfig: DeviceConfig = undefined; private guiSettings = new GuiSettings(); private tunnelStateExpectation?: Expectation; @@ -640,6 +641,16 @@ class ApplicationMain { return this.handleBootstrapError(error); } + // fetch device + try { + this.setDeviceConfig(await this.daemonRpc.getDevice()); + } catch (e) { + const error = e as Error; + log.error(`Failed to fetch device: ${error.message}`); + + return this.handleBootstrapError(error); + } + // fetch settings try { this.setSettings(await this.daemonRpc.getSettings()); @@ -697,7 +708,7 @@ class ApplicationMain { } // show window when account is not set - if (!this.deviceConfig.accountToken) { + if (!this.deviceConfig) { this.windowController?.show(); } }; @@ -1095,14 +1106,21 @@ class ApplicationMain { } } - private setDeviceConfig(deviceConfig: IDeviceConfig) { + private setDeviceConfig(deviceConfig: DeviceConfig) { const oldDeviceConfig = this.deviceConfig; this.deviceConfig = deviceConfig; // make sure to invalidate the account data cache when account tokens change - this.updateAccountDataOnAccountChange(oldDeviceConfig.accountToken, deviceConfig.accountToken); + this.updateAccountDataOnAccountChange( + oldDeviceConfig?.accountToken, + deviceConfig?.accountToken, + ); void this.updateAccountHistory(); + + if (this.windowController) { + IpcMainEventChannel.account.notifyDevice(this.windowController.webContents, deviceConfig); + } } private trayIconType(tunnelState: TunnelState, blockWhenDisconnected: boolean): TrayIconType { @@ -1276,7 +1294,7 @@ class ApplicationMain { IpcMainEventChannel.account.handleLogout(() => this.logout()); IpcMainEventChannel.account.handleGetWwwAuthToken(() => this.daemonRpc.getWwwAuthToken()); IpcMainEventChannel.account.handleSubmitVoucher(async (voucherCode: string) => { - const currentAccountToken = this.deviceConfig.accountToken; + const currentAccountToken = this.deviceConfig?.accountToken; const response = await this.daemonRpc.submitVoucher(voucherCode); if (currentAccountToken) { @@ -1287,6 +1305,13 @@ class ApplicationMain { }); IpcMainEventChannel.account.handleUpdateData(() => this.updateAccountData()); + IpcMainEventChannel.account.handleListDevices((accountToken: AccountToken) => { + return this.daemonRpc.listDevices(accountToken); + }); + IpcMainEventChannel.account.handleRemoveDevice((deviceRemoval: IDeviceRemoval) => { + return this.daemonRpc.removeDevice(deviceRemoval); + }); + IpcMainEventChannel.accountHistory.handleClear(async () => { await this.daemonRpc.clearAccountHistory(); void this.updateAccountHistory(); @@ -1467,10 +1492,7 @@ class ApplicationMain { private async autoConnect() { if (process.env.NODE_ENV === 'development') { log.info('Skip autoconnect in development'); - } else if ( - this.deviceConfig.accountToken && - (!this.accountData || !hasExpired(this.accountData.expiry)) - ) { + } else if (this.deviceConfig && (!this.accountData || !hasExpired(this.accountData.expiry))) { if (this.guiSettings.autoConnect) { try { log.info('Autoconnect the tunnel'); @@ -1530,7 +1552,7 @@ class ApplicationMain { } private updateAccountData() { - if (this.connectedToDaemon && this.deviceConfig.accountToken) { + if (this.connectedToDaemon && this.deviceConfig) { this.accountDataCache.fetch(this.deviceConfig.accountToken); } } |
