fix: 🐛 error retrying the connection

If the first connection attempt failed. Later it was given as connected
This commit is contained in:
Juan Angel
2022-12-31 01:39:06 +01:00
committed by juanAngel
parent d955a7c653
commit 3eb9d836b9

View File

@@ -16,7 +16,7 @@ const getPrivateKeyFile = () => {
) )
} }
const createTorConfig = (): TorConfig => { export const createTorConfig = (): TorConfig => {
return { return {
host: process.env.TOR_HOST, host: process.env.TOR_HOST,
port: process.env.TOR_CONTROL_PORT ? Number(process.env.TOR_CONTROL_PORT) : 9051, port: process.env.TOR_CONTROL_PORT ? Number(process.env.TOR_CONTROL_PORT) : 9051,
@@ -31,16 +31,27 @@ export const getTorClient = async () => {
const config = createTorConfig() const config = createTorConfig()
debug('config: %o', config) debug('config: %o', config)
if (config.port) { if (config.host !== undefined) {
debug('connecting') debug('connecting')
client = new Tor(config) client = new Tor(config)
await client.connect() try{
await client.connect()
}catch(error){
client = undefined
}
debug('connected') debug('connected')
} }
} }
return client return client
} }
export const closeTorClient = async () => {
if (client) {
await client.quit()
client = undefined
}
}
export const addOnion = async ( export const addOnion = async (
port: number, port: number,
@@ -66,6 +77,7 @@ export const addOnion = async (
debug('hidden service: %s:%d', hiddenService.ServiceID, port) debug('hidden service: %s:%d', hiddenService.ServiceID, port)
if (hiddenService?.PrivateKey) { if (hiddenService?.PrivateKey) {
console.log('saving private key to %s', path)
debug('saving private key to %s', path) debug('saving private key to %s', path)
await writeFile(path, hiddenService.PrivateKey, 'utf8') await writeFile(path, hiddenService.PrivateKey, 'utf8')