From 294d7c0d2d596bdc079410d0602c9ff3d70da963 Mon Sep 17 00:00:00 2001 From: Juan Angel Date: Mon, 26 Dec 2022 15:59:04 +0100 Subject: [PATCH] style: :rotating_light: fix linter warning --- src/@types/tor.ts | 5 ++ src/tor/client.ts | 130 +++++++++++++++++++++++----------------------- 2 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 src/@types/tor.ts diff --git a/src/@types/tor.ts b/src/@types/tor.ts new file mode 100644 index 0000000..8361c36 --- /dev/null +++ b/src/@types/tor.ts @@ -0,0 +1,5 @@ +export interface TorConfig { + host: string + port: number + password: string +} diff --git a/src/tor/client.ts b/src/tor/client.ts index 8fd46d9..8ab4df3 100644 --- a/src/tor/client.ts +++ b/src/tor/client.ts @@ -1,83 +1,83 @@ -import {Tor} from "tor-control-ts" +import { readFile, writeFile } from 'fs/promises' +import { homedir } from 'os' +import { join } from 'path' +import { Tor } from 'tor-control-ts' + import { createLogger } from '../factories/logger-factory' -import {readFile,writeFile} from 'fs/promises'; -import { homedir } from "os"; -import { join } from "path"; +import { TorConfig } from '../@types/tor' -interface torParams{ - host:string; - port:number; - password:string; -} - const debug = createLogger('tor-client') -const getPrivKeyFile = ()=>{ - return join(process.env.NOSTR_CONFIG_DIR ?? join(homedir(), '.nostr'),"v3_onion_private_key"); +const getPrivateKeyFile = () => { + return join( + process.env.NOSTR_CONFIG_DIR ?? join(homedir(), '.nostr'), + 'v3_onion_private_key' + ) } -const createTorConfig = ():torParams => { - return { - host:process.env.TOR_HOST, - port:Number(process.env.TOR_CONTROL_PORT), - password:process.env.TOR_PASSWORD - }; +const createTorConfig = (): TorConfig => { + return { + host: process.env.TOR_HOST, + port: process.env.TOR_CONTROL_PORT ? Number(process.env.TOR_CONTROL_PORT) : 9051, + password: process.env.TOR_PASSWORD, + } } -let client:any = null; +let client: Tor | undefined export const getTorClient = async () => { - if (!client) { - const config = createTorConfig(); - debug('config: %o', config); - //client = knex(config) - if(config.port){ - debug('connecting'); - client = new Tor(config); - await client.connect(); - debug('connected to tor'); - } - - } + if (!client) { + const config = createTorConfig() + debug('config: %o', config) - return client + if (config.port) { + debug('connecting') + client = new Tor(config) + await client.connect() + debug('connected') + } + } + + return client } -export const addOnion = async (port:number,host?:string):Promise=>{ - let privateKey = null; - try { - - let data = await readFile(getPrivKeyFile(),{ - encoding:"utf-8" - }); - if(data && data.length){ - privateKey = data; - } - debug('privateKey: %o', privateKey); - } catch (error) { - debug('addOnion catch: %o', error); +export const addOnion = async ( + port: number, + host?: string +): Promise => { + let privateKey = null + const path = getPrivateKeyFile() + + try { + debug('reading private key from %s', path) + const data = await readFile(path, 'utf8') + if (data?.length) { + privateKey = data + debug('privateKey: %o', privateKey) } + } catch (error) { + debug('error reading private key: %o', error) + } - try { - await getTorClient(); - if(client){ - let hs = await client.addOnion(port,host,privateKey); - if(hs && hs.PrivateKey){ - await writeFile(getPrivKeyFile(),hs.PrivateKey,{ - encoding:"utf-8" - }); - } - - debug('hs: %o', hs); - debug('hidden service: ', hs.ServiceID+":"+port); - return hs.ServiceID; - }else{ - return null; - } - } catch (error) { - debug('addOnion catch: %o', error); - return null; + try { + const client = await getTorClient() + if (!client) { + return } + if (client) { + const hiddenService = await client.addOnion(port, host, privateKey) + debug('hidden service: %s:%d', hiddenService.ServiceID, port) -} \ No newline at end of file + if (hiddenService?.PrivateKey) { + debug('saving private key to %s', path) + + await writeFile(path, hiddenService.PrivateKey, 'utf8') + } + + return hiddenService.ServiceID + } + } catch (error) { + console.error('error adding onion: %o', error) + } +}