From 3eb9d836b9edc528fe739d811dd17843c7b21353 Mon Sep 17 00:00:00 2001 From: Juan Angel Date: Sat, 31 Dec 2022 01:39:06 +0100 Subject: [PATCH] fix: :bug: error retrying the connection If the first connection attempt failed. Later it was given as connected --- src/tor/client.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tor/client.ts b/src/tor/client.ts index 87acc99..5e5206e 100644 --- a/src/tor/client.ts +++ b/src/tor/client.ts @@ -16,7 +16,7 @@ const getPrivateKeyFile = () => { ) } -const createTorConfig = (): TorConfig => { +export const createTorConfig = (): TorConfig => { return { host: process.env.TOR_HOST, port: process.env.TOR_CONTROL_PORT ? Number(process.env.TOR_CONTROL_PORT) : 9051, @@ -31,16 +31,27 @@ export const getTorClient = async () => { const config = createTorConfig() debug('config: %o', config) - if (config.port) { + if (config.host !== undefined) { debug('connecting') client = new Tor(config) - await client.connect() + try{ + await client.connect() + }catch(error){ + client = undefined + } debug('connected') } } return client } +export const closeTorClient = async () => { + if (client) { + + await client.quit() + client = undefined + } +} export const addOnion = async ( port: number, @@ -66,6 +77,7 @@ export const addOnion = async ( debug('hidden service: %s:%d', hiddenService.ServiceID, port) if (hiddenService?.PrivateKey) { + console.log('saving private key to %s', path) debug('saving private key to %s', path) await writeFile(path, hiddenService.PrivateKey, 'utf8')