mirror of
https://github.com/Cameri/nostream.git
synced 2025-03-17 21:31:48 +01:00
style: 🚨 fix linter warning
This commit is contained in:
parent
086b750528
commit
294d7c0d2d
5
src/@types/tor.ts
Normal file
5
src/@types/tor.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface TorConfig {
|
||||
host: string
|
||||
port: number
|
||||
password: string
|
||||
}
|
@ -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<string>=>{
|
||||
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<string> => {
|
||||
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)
|
||||
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user