diff --git a/packages/backend/src/backendManager.ts b/packages/backend/src/backendManager.ts index 47fc4fcb84..9f3e9e21c6 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' @@ -93,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, @@ -113,27 +113,13 @@ 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) + 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 f6ac46284e..b967de73fc 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..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,6 +46,7 @@ export class TorControl implements OnModuleInit { reject(new Error(`TOR: Control port error: ${data.toString() as string}`)) } }) + this.updateAuthString() this.connection.write(this.authString) }) }