From 7e0e7860cdea1f32363a047d049af122c143f1c0 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 4 Oct 2023 13:00:49 -0600 Subject: [PATCH] cancel old request and base interval on tor (#2439) --- .../ui/src/app/pages/login/login.page.ts | 52 ------------------- .../ui/src/app/services/api/api.types.ts | 2 +- .../services/api/embassy-live-api.service.ts | 6 ++- .../app/services/patch-db/patch-db.factory.ts | 10 +++- 4 files changed, 14 insertions(+), 56 deletions(-) diff --git a/frontend/projects/ui/src/app/pages/login/login.page.ts b/frontend/projects/ui/src/app/pages/login/login.page.ts index a0c564aad..4a61f2adc 100644 --- a/frontend/projects/ui/src/app/pages/login/login.page.ts +++ b/frontend/projects/ui/src/app/pages/login/login.page.ts @@ -4,7 +4,6 @@ import { ApiService } from 'src/app/services/api/embassy-api.service' import { AuthService } from 'src/app/services/auth.service' import { Router } from '@angular/router' import { ConfigService } from 'src/app/services/config.service' -import { pauseFor, RELATIVE_URL } from '@start9labs/shared' import { DOCUMENT } from '@angular/common' import { WINDOW } from '@ng-web-apis/common' @@ -18,58 +17,16 @@ export class LoginPage { unmasked = false error = '' - downloadClicked = false - instructionsClicked = false - polling = false - caTrusted = false - constructor( private readonly router: Router, private readonly authService: AuthService, private readonly loadingCtrl: LoadingController, private readonly api: ApiService, public readonly config: ConfigService, - @Inject(RELATIVE_URL) private readonly relativeUrl: string, @Inject(DOCUMENT) public readonly document: Document, @Inject(WINDOW) private readonly windowRef: Window, ) {} - async ngOnInit() { - if (!this.config.isSecure()) { - await this.testHttps().catch(e => - console.warn('Failed Https connection attempt'), - ) - } - } - - download() { - this.downloadClicked = true - this.document.getElementById('install-cert')?.click() - } - - instructions() { - this.windowRef.open( - 'https://docs.start9.com/getting-started/trust-ca/#trust-your-server-s-root-ca', - '_blank', - 'noreferrer', - ) - this.instructionsClicked = true - this.startDaemon() - } - - private async startDaemon(): Promise { - this.polling = true - while (this.polling) { - try { - await this.testHttps() - this.polling = false - } catch (e) { - console.warn('Failed Https connection attempt') - await pauseFor(2000) - } - } - } - launchHttps() { const host = this.config.getHost() this.windowRef.open(`https://${host}`, '_blank', 'noreferrer') @@ -104,13 +61,4 @@ export class LoginPage { loader.dismiss() } } - - private async testHttps() { - const url = `https://${this.document.location.host}${this.relativeUrl}` - await this.api.echo({ message: 'ping' }, url).then(() => { - this.downloadClicked = true - this.instructionsClicked = true - this.caTrusted = true - }) - } } diff --git a/frontend/projects/ui/src/app/services/api/api.types.ts b/frontend/projects/ui/src/app/services/api/api.types.ts index 472cb5ab9..d5c519e69 100644 --- a/frontend/projects/ui/src/app/services/api/api.types.ts +++ b/frontend/projects/ui/src/app/services/api/api.types.ts @@ -38,7 +38,7 @@ export module RR { // server - export type EchoReq = { message: string } // server.echo + export type EchoReq = { message: string; timeout?: number } // server.echo export type EchoRes = string export type GetSystemTimeReq = {} // server.time diff --git a/frontend/projects/ui/src/app/services/api/embassy-live-api.service.ts b/frontend/projects/ui/src/app/services/api/embassy-live-api.service.ts index 63b36aa93..775fabf28 100644 --- a/frontend/projects/ui/src/app/services/api/embassy-live-api.service.ts +++ b/frontend/projects/ui/src/app/services/api/embassy-live-api.service.ts @@ -91,7 +91,11 @@ export class LiveApiService extends ApiService { // server async echo(params: RR.EchoReq, urlOverride?: string): Promise { - return this.rpcRequest({ method: 'echo', params }, false, urlOverride) + return this.rpcRequest( + { method: 'echo', params, timeout: 4000 }, + false, + urlOverride, + ) } openPatchWebsocket$(): Observable> { diff --git a/frontend/projects/ui/src/app/services/patch-db/patch-db.factory.ts b/frontend/projects/ui/src/app/services/patch-db/patch-db.factory.ts index 78af9beec..dc4276f79 100644 --- a/frontend/projects/ui/src/app/services/patch-db/patch-db.factory.ts +++ b/frontend/projects/ui/src/app/services/patch-db/patch-db.factory.ts @@ -13,6 +13,7 @@ import { defer, EMPTY, from, interval, merge, Observable } from 'rxjs' import { AuthService } from '../auth.service' import { ConnectionService } from '../connection.service' import { ApiService } from '../api/embassy-api.service' +import { ConfigService } from '../config.service' export const PATCH_SOURCE = new InjectionToken[]>>( '', @@ -26,6 +27,9 @@ export function sourceFactory( const api = injector.get(ApiService) const authService = injector.get(AuthService) const connectionService = injector.get(ConnectionService) + const configService = injector.get(ConfigService) + const isTor = configService.isTor() + const timeout = isTor ? 16000 : 4000 const websocket$ = api.openPatchWebsocket$().pipe( bufferTime(250), @@ -33,9 +37,11 @@ export function sourceFactory( catchError((_, watch$) => { connectionService.websocketConnected$.next(false) - return interval(4000).pipe( + return interval(timeout).pipe( switchMap(() => - from(api.echo({ message: 'ping' })).pipe(catchError(() => EMPTY)), + from(api.echo({ message: 'ping', timeout })).pipe( + catchError(() => EMPTY), + ), ), take(1), switchMap(() => watch$),