From ffc64a717f17244aee36ac7521bfa184e4a1cb20 Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Fri, 12 May 2023 02:28:32 +0200 Subject: [PATCH] rework PR-comments --- dht-node/src/config/devop.ts | 13 --------- dht-node/src/config/tools.ts | 53 ---------------------------------- dht-node/src/dht_node/index.ts | 24 +++------------ dht-node/src/index.ts | 25 +++++----------- 4 files changed, 11 insertions(+), 104 deletions(-) delete mode 100644 dht-node/src/config/devop.ts delete mode 100644 dht-node/src/config/tools.ts diff --git a/dht-node/src/config/devop.ts b/dht-node/src/config/devop.ts deleted file mode 100644 index 410953fbf..000000000 --- a/dht-node/src/config/devop.ts +++ /dev/null @@ -1,13 +0,0 @@ -// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env) -import dotenv from 'dotenv' -import { getDevOpEnvValue } from './tools' -dotenv.config() - -const DEVOP = { - FEDERATION_DHT_TOPIC: getDevOpEnvValue('FEDERATION_DHT_TOPIC') || null, - FEDERATION_DHT_SEED: getDevOpEnvValue('FEDERATION_DHT_SEED') || null, - HOME_COMMUNITY_PUBLICKEY: getDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY') || null, - HOME_COMMUNITY_PRIVATEKEY: getDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY') || null, -} - -export default DEVOP diff --git a/dht-node/src/config/tools.ts b/dht-node/src/config/tools.ts deleted file mode 100644 index bc8b05d68..000000000 --- a/dht-node/src/config/tools.ts +++ /dev/null @@ -1,53 +0,0 @@ -/** eslint-disable n/no-sync */ -import { logger } from '@/server/logger' -import fs from 'fs' -import os from 'os' -import path from 'path' - -const envFilePath = path.resolve(__dirname, './../../.env.devop') - -// read .env file & convert to array -const readEnvVars = () => { - if (!fs.existsSync(envFilePath)) { - logger.info(`devop config file ${envFilePath} will be created...`) - fs.writeFileSync(envFilePath, '', 'utf8') - } - return fs.readFileSync(envFilePath, 'utf-8').split(os.EOL) -} - -/** - * Finds the key in .env files and returns the corresponding value - * - * @param {string} key Key to find - * @returns {string|null} Value of the key - */ -export const getDevOpEnvValue = (key: string): string | null => { - // find the line that contains the key (exact match) - const matchedLine = readEnvVars().find((line) => line.split('=')[0] === key) - // split the line (delimiter is '=') and return the item at index 2 - return matchedLine !== undefined ? matchedLine.split('=')[1] : null -} - -/** - * Updates value for existing key or creates a new key=value line - * - * This function is a modified version of https://stackoverflow.com/a/65001580/3153583 - * - * @param {string} key Key to update/insert - * @param {string} value Value to update/insert - */ -export const setDevOpEnvValue = (key: string, value: string): void => { - const envVars = readEnvVars() - const targetLine = envVars.find((line) => line.split('=')[0] === key) - if (targetLine !== undefined) { - // update existing line - const targetLineIndex = envVars.indexOf(targetLine) - // replace the key/value with the new value - envVars.splice(targetLineIndex, 1, `${key}="${value}"`) - } else { - // create new key value - envVars.push(`${key}="${value}"`) - } - // write everything back to the file system - fs.writeFileSync(envFilePath, envVars.join(os.EOL)) -} diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index 6676d052b..dcb4db6be 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -5,19 +5,13 @@ import { logger } from '@/server/logger' import CONFIG from '@/config' import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { Community as DbCommunity } from '@entity/Community' -import DEVOP from '@/config/devop' -import { setDevOpEnvValue } from '@/config/tools' import { v4 as uuidv4 } from 'uuid' const KEY_SECRET_SEEDBYTES = 32 const getSeed = (): Buffer | null => { - let dhtseed = DEVOP.FEDERATION_DHT_SEED - logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED) - if (!dhtseed) { - dhtseed = CONFIG.FEDERATION_DHT_SEED - logger.debug(`dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`) - } - return dhtseed ? Buffer.alloc(KEY_SECRET_SEEDBYTES, dhtseed) : null + return CONFIG.FEDERATION_DHT_SEED + ? Buffer.alloc(KEY_SECRET_SEEDBYTES, CONFIG.FEDERATION_DHT_SEED) + : null } const POLLTIME = 20000 @@ -41,9 +35,6 @@ export const startDHT = async (topic: string): Promise => { const keyPair = DHT.keyPair(getSeed()) logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) - // insert or update keyPair in .env.devop file - setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex')) - setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex')) await writeHomeCommunityEntry(keyPair.publicKey.toString('hex')) const ownApiVersions = await writeFederatedHomeCommunityEntries( @@ -207,16 +198,13 @@ export async function writeFederatedHomeCommunityEntries(pubKey: string): Promis try { // first remove privious existing homeCommunity entries DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() - // homeApiVersions.forEach(async function (homeApi) { for (let i = 0; i < homeApiVersions.length; i++) { await createFederatedCommunityEntity(homeApiVersions[i], pubKey) - // console.log(`ApiVersion:${JSON.stringify(homeApiVersions[i])}`, JSON.stringify(result)) logger.info( `federation home-community inserted successfully: ${JSON.stringify(homeApiVersions[i])}`, ) } } catch (err) { - // console.log('Error1:', err) throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`) } return homeApiVersions @@ -226,7 +214,6 @@ async function createFederatedCommunityEntity( homeApi: CommunityApi, pubKey: string, ): Promise { - // let result: InsertResult try { const homeCom = DbFederatedCommunity.create() homeCom.foreign = false @@ -236,17 +223,14 @@ async function createFederatedCommunityEntity( // this will NOT update the updatedAt column, to distingue between a normal update and the last announcement await DbFederatedCommunity.insert(homeCom) - logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`) - // console.log(`result: ${JSON.stringify(result)}`) + logger.debug(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`) } catch (err) { - // console.log('Error2:', err) return false } return true } export async function writeHomeCommunityEntry(pubKey: string): Promise { - // console.log(`pubKey = `, pubKey) try { // check for existing homeCommunity entry let homeCom = await DbCommunity.findOne({ diff --git a/dht-node/src/index.ts b/dht-node/src/index.ts index 4f481d041..d5e5f700b 100644 --- a/dht-node/src/index.ts +++ b/dht-node/src/index.ts @@ -3,7 +3,6 @@ import { startDHT } from '@/dht_node/index' // config import CONFIG from './config' -import DEVOP from './config/devop' import { logger } from './server/logger' import connection from './typeorm/connection' import { checkDBVersion } from './typeorm/DBVersion' @@ -22,23 +21,13 @@ async function main() { logger.fatal('Fatal: Database Version incorrect') throw new Error('Fatal: Database Version incorrect') } - // first read from .env.devop if exist - let dhttopic = DEVOP.FEDERATION_DHT_TOPIC - logger.debug(`dhttopic set by DEVOP.FEDERATION_DHT_TOPIC=${DEVOP.FEDERATION_DHT_TOPIC}`) - if (!dhttopic) { - dhttopic = CONFIG.FEDERATION_DHT_TOPIC - logger.debug( - `dhttopic overwritten by CONFIG.FEDERATION_DHT_TOPIC=${CONFIG.FEDERATION_DHT_TOPIC}`, - ) - } - let dhtseed = DEVOP.FEDERATION_DHT_SEED - logger.debug(`dhtseed set by DEVOP.FEDERATION_DHT_SEED=${DEVOP.FEDERATION_DHT_SEED}`) - if (!dhtseed) { - dhtseed = CONFIG.FEDERATION_DHT_SEED - logger.debug(`dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`) - } - logger.info(`starting Federation on ${dhttopic} ${dhtseed ? 'with seed...' : 'without seed...'}`) - await startDHT(dhttopic) + logger.debug(`dhtseed set by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`) + logger.info( + `starting Federation on ${CONFIG.FEDERATION_DHT_TOPIC} ${ + CONFIG.FEDERATION_DHT_SEED ? 'with seed...' : 'without seed...' + }`, + ) + await startDHT(CONFIG.FEDERATION_DHT_TOPIC) } main().catch((e) => {