From 6c08670dc0ac175e781600138a1b68df1073739a Mon Sep 17 00:00:00 2001 From: vinkabuki Date: Thu, 14 Sep 2023 12:13:41 +0200 Subject: [PATCH 1/2] iOS: Do not restart all services on foreground-background switch --- packages/backend/src/backendManager.ts | 26 +++++-------------- .../connections-manager.service.ts | 8 ++++++ .../src/nest/tor/tor-control.service.ts | 2 ++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/backendManager.ts b/packages/backend/src/backendManager.ts index 47fc4fcb84..fe951d4cc3 100644 --- a/packages/backend/src/backendManager.ts +++ b/packages/backend/src/backendManager.ts @@ -5,6 +5,7 @@ import path from 'path' import getPort from 'get-port' import { AppModule } from './nest/app.module' import { ConnectionsManagerService } from './nest/connections-manager/connections-manager.service' +import { TorControl } from './nest/tor/tor-control.service' import { torBinForPlatform, torDirForPlatform } from './nest/common/utils' import initRnBridge from './rn-bridge' @@ -113,27 +114,14 @@ export const runBackendMobile = async (): Promise => { rn_bridge.channel.on('close', async () => { const connectionsManager = app.get(ConnectionsManagerService) - await connectionsManager.closeAllServices() - await app.close() + connectionsManager.closeSocket() }) rn_bridge.channel.on('open', async (msg: OpenServices) => { - app = await NestFactory.createApplicationContext( - AppModule.forOptions({ - socketIOPort: msg.socketIOPort, - httpTunnelPort: msg.httpTunnelPort ? msg.httpTunnelPort : null, - torAuthCookie: msg.authCookie ? msg.authCookie : null, - torControlPort: msg.torControlPort ? msg.torControlPort : await getPort(), - torBinaryPath: options.torBinary ? options.torBinary : null, - options: { - env: { - appDataPath: options.dataPath, - }, - createPaths: false, - }, - }), - { logger: ['warn', 'error', 'log', 'debug', 'verbose'] } - ) - console.log('started backend wiktor little bastard ') + const connectionsManager = app.get(ConnectionsManagerService) + // BARTEK: Ugly temporary solution - we should update provider instead directly replacing value! + const torControlParams = app.get(TorControl) + torControlParams.torControlParams.auth.value = msg.authCookie + await connectionsManager.openSocket() }) } diff --git a/packages/backend/src/nest/connections-manager/connections-manager.service.ts b/packages/backend/src/nest/connections-manager/connections-manager.service.ts index 4baec55254..cc3d078bc1 100644 --- a/packages/backend/src/nest/connections-manager/connections-manager.service.ts +++ b/packages/backend/src/nest/connections-manager/connections-manager.service.ts @@ -203,6 +203,14 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI } } + public closeSocket() { + this.serverIoProvider.io.close() + } + + public async openSocket() { + await this.socketService.init() + } + public async leaveCommunity() { this.tor.resetHiddenServices() this.serverIoProvider.io.close() diff --git a/packages/backend/src/nest/tor/tor-control.service.ts b/packages/backend/src/nest/tor/tor-control.service.ts index f833154e87..755ecfdcd9 100644 --- a/packages/backend/src/nest/tor/tor-control.service.ts +++ b/packages/backend/src/nest/tor/tor-control.service.ts @@ -46,6 +46,8 @@ export class TorControl implements OnModuleInit { reject(new Error(`TOR: Control port error: ${data.toString() as string}`)) } }) + // BARTEK - Add support for dynamic authCookie + this.onModuleInit() this.connection.write(this.authString) }) } From 7ea5a0b07a5da20248a89edebf7d76ed8766b66b Mon Sep 17 00:00:00 2001 From: Vin Kabuki Date: Thu, 14 Sep 2023 14:15:05 +0200 Subject: [PATCH 2/2] Add method for dynamic auth cookie --- packages/backend/src/backendManager.ts | 4 +--- packages/backend/src/nest/tor/tor-control.service.ts | 9 ++++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/backendManager.ts b/packages/backend/src/backendManager.ts index fe951d4cc3..9f3e9e21c6 100644 --- a/packages/backend/src/backendManager.ts +++ b/packages/backend/src/backendManager.ts @@ -94,8 +94,7 @@ export const runBackendMobile = async (): Promise => { const rn_bridge = initRnBridge() - let app: INestApplicationContext - app = await NestFactory.createApplicationContext( + const app: INestApplicationContext = await NestFactory.createApplicationContext( AppModule.forOptions({ socketIOPort: options.dataPort, httpTunnelPort: options.httpTunnelPort ? options.httpTunnelPort : null, @@ -118,7 +117,6 @@ export const runBackendMobile = async (): Promise => { }) rn_bridge.channel.on('open', async (msg: OpenServices) => { const connectionsManager = app.get(ConnectionsManagerService) - // BARTEK: Ugly temporary solution - we should update provider instead directly replacing value! const torControlParams = app.get(TorControl) torControlParams.torControlParams.auth.value = msg.authCookie await connectionsManager.openSocket() diff --git a/packages/backend/src/nest/tor/tor-control.service.ts b/packages/backend/src/nest/tor/tor-control.service.ts index 755ecfdcd9..5fa1426bea 100644 --- a/packages/backend/src/nest/tor/tor-control.service.ts +++ b/packages/backend/src/nest/tor/tor-control.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, OnModuleInit } from '@nestjs/common' +import { Inject, Injectable } from '@nestjs/common' import net from 'net' import { CONFIG_OPTIONS, TOR_CONTROL_PARAMS } from '../const' import { ConfigOptions } from '../types' @@ -6,7 +6,7 @@ import { TorControlAuthType, TorControlParams } from './tor.types' import Logger from '../common/logger' @Injectable() -export class TorControl implements OnModuleInit { +export class TorControl { connection: net.Socket | null authString: string private readonly logger = Logger(TorControl.name) @@ -15,7 +15,7 @@ export class TorControl implements OnModuleInit { @Inject(CONFIG_OPTIONS) public configOptions: ConfigOptions ) {} - onModuleInit() { + private updateAuthString() { if (this.torControlParams.auth.type === TorControlAuthType.PASSWORD) { this.authString = 'AUTHENTICATE "' + this.torControlParams.auth.value + '"\r\n' } @@ -46,8 +46,7 @@ export class TorControl implements OnModuleInit { reject(new Error(`TOR: Control port error: ${data.toString() as string}`)) } }) - // BARTEK - Add support for dynamic authCookie - this.onModuleInit() + this.updateAuthString() this.connection.write(this.authString) }) }