Skip to content

Commit

Permalink
Merge pull request #1808 from TryQuiet/chore/ios_startup_improvement
Browse files Browse the repository at this point in the history
iOS: Do not restart all services on foreground-background switch
  • Loading branch information
vinkabuki authored Sep 14, 2023
2 parents ebfa5e6 + 7ea5a0b commit 98c8478
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
28 changes: 7 additions & 21 deletions packages/backend/src/backendManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -93,8 +94,7 @@ export const runBackendMobile = async (): Promise<any> => {

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,
Expand All @@ -113,27 +113,13 @@ export const runBackendMobile = async (): Promise<any> => {

rn_bridge.channel.on('close', async () => {
const connectionsManager = app.get<ConnectionsManagerService>(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>(ConnectionsManagerService)
const torControlParams = app.get<TorControl>(TorControl)
torControlParams.torControlParams.auth.value = msg.authCookie
await connectionsManager.openSocket()
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions packages/backend/src/nest/tor/tor-control.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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'
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)
Expand All @@ -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'
}
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 98c8478

Please sign in to comment.