Skip to content

Commit

Permalink
Remove duplicate test and better tor init
Browse files Browse the repository at this point in the history
  • Loading branch information
ikoenigsknecht committed May 20, 2024
1 parent 97719b2 commit 9668b90
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 66 deletions.
4 changes: 0 additions & 4 deletions packages/backend/src/nest/tor/tor.service.tor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ describe('TorControl', () => {
expect(spyOnInit).toHaveBeenCalledTimes(2)
})

it('tor is initializing correctly with 40 seconds timeout', async () => {
await torService.init()
})

it('creates and destroys hidden service', async () => {
await torService.init()
const hiddenService = await torService.createNewHiddenService({ targetPort: 4343 })
Expand Down
137 changes: 75 additions & 62 deletions packages/backend/src/nest/tor/tor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,80 +77,89 @@ export class Tor extends EventEmitter implements OnModuleInit {
return false
}

public async init(timeout = 120_000): Promise<void> {
public async init(timeout: number = 120_000): Promise<void> {
this.isTorServiceUsed = true
if (!this.socksPort) this.socksPort = await getPort()
this.logger('Initializing tor...')
await this._init(timeout)
}

return await new Promise((resolve, reject) => {
if (!fs.existsSync(this.quietDir)) {
this.logger("Quiet dir doesn't exist, creating it now")
fs.mkdirSync(this.quietDir)
}

this.torDataDirectory = path.join.apply(null, [this.quietDir, 'TorDataDirectory'])
this.torPidPath = path.join.apply(null, [this.quietDir, 'torPid.json'])
let oldTorPid: number | null = null
if (fs.existsSync(this.torPidPath)) {
const file = fs.readFileSync(this.torPidPath)
oldTorPid = Number(file.toString())
this.logger(`${this.torPidPath} exists. Old tor pid: ${oldTorPid}`)
}

this.initTimeout = setTimeout(async () => {
this.logger('Checking init timeout')
const bootstrapDone = await this.isBootstrappingFinished()
if (!bootstrapDone) {
this.initializedHiddenServices = new Map()
clearInterval(this.interval)
await this.init()
private async _init(timeout: number) {
try {
return await new Promise<void>((resolve, reject) => {
if (!fs.existsSync(this.quietDir)) {
this.logger("Quiet dir doesn't exist, creating it now")
fs.mkdirSync(this.quietDir)
}
}, timeout)

const tryToSpawnTor = async () => {
if (oldTorPid != null) {
this.logger(`Clearing out old tor process with pid ${oldTorPid}`)
this.clearOldTorProcess(oldTorPid)
this.torDataDirectory = path.join.apply(null, [this.quietDir, 'TorDataDirectory'])
this.torPidPath = path.join.apply(null, [this.quietDir, 'torPid.json'])
let oldTorPid: number | null = null
if (fs.existsSync(this.torPidPath)) {
const file = fs.readFileSync(this.torPidPath)
oldTorPid = Number(file.toString())
this.logger(`${this.torPidPath} exists. Old tor pid: ${oldTorPid}`)
}

try {
this.logger('Clearing out hanging tor process(es)')
this.clearHangingTorProcess()
} catch (e) {
this.logger('Error occured while trying to clear hanging tor processes', e)
}
this.initTimeout = setTimeout(async () => {
this.logger('Checking init timeout')
const bootstrapDone = await this.isBootstrappingFinished()
if (!bootstrapDone) {
this.initializedHiddenServices = new Map()
clearInterval(this.interval)
reject(new Error(`Failed to initialize in timeout of ${timeout}ms`))
}
}, timeout)

try {
this.logger('Spawning new tor process(es)')
await this.spawnTor()

this.interval = setInterval(async () => {
this.logger('Checking bootstrap interval')
const bootstrapDone = await this.isBootstrappingFinished()
if (bootstrapDone) {
this.isTorInitialized = true
this.logger(`Sending ${SocketActionTypes.TOR_INITIALIZED}`)
this.serverIoProvider.io.emit(SocketActionTypes.TOR_INITIALIZED)
this.logger(`Sending ${SocketActionTypes.INITIAL_DIAL}`)
this.emit(SocketActionTypes.INITIAL_DIAL)
clearInterval(this.interval)
resolve()
}
}, 2500)
const tryToSpawnTor = async () => {
if (oldTorPid != null) {
this.logger(`Clearing out old tor process with pid ${oldTorPid}`)
this.clearOldTorProcess(oldTorPid)
}

try {
this.logger('Clearing out hanging tor process(es)')
this.clearHangingTorProcess()
} catch (e) {
this.logger('Error occured while trying to clear hanging tor processes', e)
}

this.logger(`Spawned tor with pid(s): ${this.getTorProcessIds()}`)
} catch (e) {
this.logger('Killing tor due to error', e)
this.clearHangingTorProcess()
removeFilesFromDir(this.torDataDirectory)
try {
this.logger('Spawning new tor process(es)')
await this.spawnTor()

this.interval = setInterval(async () => {
this.logger('Checking bootstrap interval')
const bootstrapDone = await this.isBootstrappingFinished()
if (bootstrapDone) {
this.isTorInitialized = true
this.logger(`Sending ${SocketActionTypes.TOR_INITIALIZED}`)
this.serverIoProvider.io.emit(SocketActionTypes.TOR_INITIALIZED)
this.logger(`Sending ${SocketActionTypes.INITIAL_DIAL}`)
this.emit(SocketActionTypes.INITIAL_DIAL)
clearInterval(this.interval)
resolve()
}
}, 2500)

this.logger(`Spawned tor with pid(s): ${this.getTorProcessIds()}`)
// resolve()
} catch (e) {
this.logger('Killing tor due to error', e)
this.clearHangingTorProcess()
removeFilesFromDir(this.torDataDirectory)

// eslint-disable-next-line
process.nextTick(tryToSpawnTor)
// eslint-disable-next-line
process.nextTick(tryToSpawnTor)
}
}
}

tryToSpawnTor()
})
tryToSpawnTor()
})
} catch (e) {
this.logger.error(`Initialization failed due to error: ${e.message}, retrying...`)
await this.init()
}
}

public resetHiddenServices() {
Expand Down Expand Up @@ -222,7 +231,11 @@ export class Tor extends EventEmitter implements OnModuleInit {
try {
process.kill(oldTorPid, 'SIGTERM')
} catch (e) {
this.logger.error(`Tried killing old tor process. Failed. Reason: ${e.message}`)
if ((e as Error).message.includes('ESRCH')) {
this.logger(`Tor process with PID ${oldTorPid} was already closed`)
} else {
this.logger.error(`Tried killing old tor process. Failed`, e)
}
}
} else {
this.logger(`Deleting ${this.torPidPath}`)
Expand Down

0 comments on commit 9668b90

Please sign in to comment.