2021-12-29 18:08:41 -05:00
import * as os from 'os' ;
import * as fs from 'fs' ;
import { join , dirname , sep } from 'path' ;
import { fileURLToPath } from 'url' ;
import * as crypto from 'crypto' ;
import ini from 'ini' ;
import parseHocon from 'hocon-parser' ;
import { Common , CommonService } from './common.js' ;
import { Logger , LoggerService } from './logger.js' ;
export class ConfigService {
private platform = os . platform ( ) ;
private hash = crypto . createHash ( 'sha256' ) ;
private errMsg = '' ;
private directoryName = dirname ( fileURLToPath ( import . meta . url ) ) ;
private common : CommonService = Common ;
private logger : LoggerService = Logger ;
2023-02-17 17:33:33 -08:00
constructor ( ) {
this . setServerConfiguration ( ) ;
}
2021-12-29 18:08:41 -05:00
private setDefaultConfig = ( ) = > {
const homeDir = os . userInfo ( ) . homedir ;
let macaroonPath = '' ;
let configPath = '' ;
let channelBackupPath = '' ;
2023-02-17 17:33:33 -08:00
let dbPath = '' ;
2021-12-29 18:08:41 -05:00
switch ( this . platform ) {
case 'win32' :
macaroonPath = homeDir + '\\AppData\\Local\\Lnd\\data\\chain\\bitcoin\\mainnet' ;
configPath = homeDir + '\\AppData\\Local\\Lnd\\lnd.conf' ;
channelBackupPath = homeDir + '\\backup\\node-1' ;
2023-02-17 17:33:33 -08:00
dbPath = homeDir + '\\database\\node-1' ;
2021-12-29 18:08:41 -05:00
break ;
case 'darwin' :
macaroonPath = homeDir + '/Library/Application Support/Lnd/data/chain/bitcoin/mainnet' ;
configPath = homeDir + '/Library/Application Support/Lnd/lnd.conf' ;
channelBackupPath = homeDir + '/backup/node-1' ;
2023-02-17 17:33:33 -08:00
dbPath = homeDir + '/database/node-1' ;
2021-12-29 18:08:41 -05:00
break ;
case 'linux' :
macaroonPath = homeDir + '/.lnd/data/chain/bitcoin/mainnet' ;
configPath = homeDir + '/.lnd/lnd.conf' ;
channelBackupPath = homeDir + '/backup/node-1' ;
2023-02-17 17:33:33 -08:00
dbPath = homeDir + '/database/node-1' ;
2021-12-29 18:08:41 -05:00
break ;
default :
macaroonPath = '' ;
configPath = '' ;
channelBackupPath = '' ;
2023-02-17 17:33:33 -08:00
dbPath = '' ;
2021-12-29 18:08:41 -05:00
break ;
}
2022-03-05 10:58:15 -05:00
const configData = {
2021-12-29 18:08:41 -05:00
port : '3000' ,
defaultNodeIndex : 1 ,
2023-02-17 17:33:33 -08:00
dbDirectoryPath : dbPath ,
2021-12-29 18:08:41 -05:00
SSO : {
rtlSSO : 0 ,
rtlCookiePath : '' ,
logoutRedirectLink : ''
} ,
nodes : [
{
index : 1 ,
lnNode : 'Node 1' ,
lnImplementation : 'LND' ,
2024-06-10 12:40:37 -07:00
authentication : {
2021-12-29 18:08:41 -05:00
macaroonPath : macaroonPath ,
configPath : configPath
} ,
2024-06-10 12:40:37 -07:00
settings : {
2021-12-29 18:08:41 -05:00
userPersona : 'MERCHANT' ,
themeMode : 'DAY' ,
themeColor : 'PURPLE' ,
channelBackupPath : channelBackupPath ,
logLevel : 'ERROR' ,
2022-12-15 15:09:08 -08:00
lnServerUrl : 'https://127.0.0.1:8080' ,
2022-11-15 19:17:23 -08:00
fiatConversion : false ,
2024-06-10 12:40:37 -07:00
unannouncedChannels : false ,
blockExplorerUrl : 'https://mempool.space'
2021-12-29 18:08:41 -05:00
}
}
]
} ;
2022-12-27 18:05:42 -08:00
if ( ( process ? . env ? . RTL_SSO && + process ? . env ? . RTL_SSO === 0 ) || configData . SSO . rtlSSO === 0 ) {
2022-03-05 10:58:15 -05:00
configData [ 'multiPass' ] = 'password' ;
}
return configData ;
2021-12-29 18:08:41 -05:00
} ;
private normalizePort = ( val ) = > {
const port = parseInt ( val , 10 ) ;
if ( isNaN ( port ) ) {
return val ;
}
if ( port >= 0 ) {
return port ;
}
return false ;
} ;
private updateLogByLevel = ( ) = > {
let updateLogFlag = false ;
2024-06-10 12:40:37 -07:00
this . common . appConfig . rtlConfFilePath = process ? . env ? . RTL_CONFIG_PATH ? process?.env?.RTL_CONFIG_PATH : join ( this . directoryName , '../..' ) ;
2021-12-29 18:08:41 -05:00
try {
2024-06-10 12:40:37 -07:00
const RTLConfFile = this . common . appConfig . rtlConfFilePath + sep + 'RTL-Config.json' ;
2021-12-29 18:08:41 -05:00
const config = JSON . parse ( fs . readFileSync ( RTLConfFile , 'utf-8' ) ) ;
config . nodes . forEach ( ( node ) = > {
2024-06-10 12:40:37 -07:00
if ( node . settings . hasOwnProperty ( 'enableLogging' ) ) {
2021-12-29 18:08:41 -05:00
updateLogFlag = true ;
2024-06-10 12:40:37 -07:00
node . settings . logLevel = node . settings . enableLogging ? 'INFO' : 'ERROR' ;
delete node . settings . enableLogging ;
2021-12-29 18:08:41 -05:00
}
} ) ;
if ( updateLogFlag ) {
fs . writeFileSync ( RTLConfFile , JSON . stringify ( config , null , 2 ) , 'utf-8' ) ;
}
} catch ( err ) {
this . errMsg = this . errMsg + '\nLog level update failed!' ;
}
} ;
private validateNodeConfig = ( config ) = > {
2024-06-10 12:40:37 -07:00
config . allowPasswordUpdate = true ;
2022-12-27 18:05:42 -08:00
if ( ( process ? . env ? . RTL_SSO && + process ? . env ? . RTL_SSO === 0 ) || ( typeof process ? . env ? . RTL_SSO === 'undefined' && + config . SSO . rtlSSO === 0 ) ) {
if ( process ? . env ? . APP_PASSWORD && process ? . env ? . APP_PASSWORD . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
config . rtlPass = this . hash . update ( process ? . env ? . APP_PASSWORD ) . digest ( 'hex' ) ;
config . allowPasswordUpdate = false ;
2021-12-29 18:08:41 -05:00
} else if ( config . multiPassHashed && config . multiPassHashed !== '' ) {
2024-06-10 12:40:37 -07:00
config . rtlPass = config . multiPassHashed ;
2021-12-29 18:08:41 -05:00
} else if ( config . multiPass && config . multiPass !== '' ) {
2024-06-10 12:40:37 -07:00
config . rtlPass = this . common . replacePasswordWithHash ( this . hash . update ( config . multiPass ) . digest ( 'hex' ) ) ;
2021-12-29 18:08:41 -05:00
} else {
this . errMsg = this . errMsg + '\nNode Authentication can be set with multiPass only. Please set multiPass in RTL-Config.json' ;
}
2024-06-10 12:40:37 -07:00
config . enable2FA = ! ! config . secret2FA ;
2021-12-29 18:08:41 -05:00
} else {
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . APP_PASSWORD && process ? . env ? . APP_PASSWORD . trim ( ) !== '' ) {
2021-12-29 18:08:41 -05:00
this . errMsg = this . errMsg + '\nRTL Password cannot be set with SSO. Please set SSO as 0 or remove password.' ;
}
}
2022-12-27 18:05:42 -08:00
this . common . port = ( process ? . env ? . PORT ) ? this . normalizePort ( process ? . env ? . PORT ) : ( config . port ) ? this . normalizePort ( config . port ) : 3000 ;
this . common . host = ( process ? . env ? . HOST ) ? process ? . env ? . HOST : ( config . host ) ? config.host : null ;
2024-06-10 12:40:37 -07:00
config . dbDirectoryPath = ( process ? . env ? . DB_DIRECTORY_PATH ) ? process ? . env ? . DB_DIRECTORY_PATH : ( config . dbDirectoryPath ) ? config.dbDirectoryPath : join ( dirname ( fileURLToPath ( import . meta . url ) ) , '..' , '..' ) ;
2021-12-29 18:08:41 -05:00
if ( config . nodes && config . nodes . length > 0 ) {
config . nodes . forEach ( ( node , idx ) = > {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] = { settings : { blockExplorerUrl : '' } , authentication : { } } ;
2021-12-29 18:08:41 -05:00
this . common . nodes [ idx ] . index = node . index ;
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . lnNode = node . lnNode ;
this . common . nodes [ idx ] . lnImplementation = ( process ? . env ? . lnImplementation ) ? process?.env?.lnImplementation : node.lnImplementation ? node . lnImplementation : 'LND' ;
if ( this . common . nodes [ idx ] . lnImplementation === 'CLT' ) { this . common . nodes [ idx ] . lnImplementation = 'CLN' ; }
switch ( this . common . nodes [ idx ] . lnImplementation ) {
2023-12-05 20:32:05 -08:00
case 'CLN' :
if ( process ? . env ? . RUNE_PATH && process ? . env ? . RUNE_PATH . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . runePath = process ? . env ? . RUNE_PATH ;
} else if ( node . authentication && node . authentication . runePath && node . authentication . runePath . trim ( ) !== '' ) {
this . common . nodes [ idx ] . authentication . runePath = node . authentication . runePath ;
2023-12-05 20:32:05 -08:00
} else {
this . errMsg = 'Please set rune path for node index ' + node . index + ' in RTL-Config.json!' ;
}
break ;
2021-12-29 18:08:41 -05:00
2023-12-05 20:32:05 -08:00
case 'ECL' :
if ( process ? . env ? . LN_API_PASSWORD ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . lnApiPassword = process ? . env ? . LN_API_PASSWORD ;
} else if ( node . authentication && node . authentication . lnApiPassword ) {
this . common . nodes [ idx ] . authentication . lnApiPassword = node . authentication . lnApiPassword ;
2023-12-05 20:32:05 -08:00
} else {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . lnApiPassword = '' ;
2023-12-05 20:32:05 -08:00
}
break ;
default :
if ( process ? . env ? . MACAROON_PATH && process ? . env ? . MACAROON_PATH . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . macaroonPath = process ? . env ? . MACAROON_PATH ;
} else if ( node . authentication && node . authentication . macaroonPath && node . authentication . macaroonPath . trim ( ) !== '' ) {
this . common . nodes [ idx ] . authentication . macaroonPath = node . authentication . macaroonPath ;
2023-12-05 20:32:05 -08:00
} else {
this . errMsg = 'Please set macaroon path for node index ' + node . index + ' in RTL-Config.json!' ;
}
break ;
2021-12-29 18:08:41 -05:00
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . CONFIG_PATH ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . configPath = process ? . env ? . CONFIG_PATH ;
} else if ( node . authentication && node . authentication . configPath ) {
this . common . nodes [ idx ] . authentication . configPath = node . authentication . configPath ;
2021-12-29 18:08:41 -05:00
} else {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . configPath = '' ;
2021-12-29 18:08:41 -05:00
}
2024-06-10 12:40:37 -07:00
if ( this . common . nodes [ idx ] . lnImplementation === 'ECL' && this . common . nodes [ idx ] . authentication . lnApiPassword === '' && this . common . nodes [ idx ] . authentication . configPath !== '' ) {
2021-12-29 18:08:41 -05:00
try {
2024-06-10 12:40:37 -07:00
const exists = fs . existsSync ( this . common . nodes [ idx ] . authentication . configPath || '' ) ;
2021-12-29 18:08:41 -05:00
if ( exists ) {
try {
2024-06-10 12:40:37 -07:00
const configFile = fs . readFileSync ( ( this . common . nodes [ idx ] . authentication . configPath || '' ) , 'utf-8' ) ;
2021-12-29 18:08:41 -05:00
const iniParsed = ini . parse ( configFile ) ;
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . authentication . lnApiPassword = iniParsed [ 'eclair.api.password' ] ? iniParsed [ 'eclair.api.password' ] : parseHocon ( configFile ) . eclair . api . password ;
2021-12-29 18:08:41 -05:00
} catch ( err ) {
this . errMsg = this . errMsg + '\nSomething went wrong while reading config file: \n' + err ;
}
} else {
2024-06-10 12:40:37 -07:00
this . errMsg = this . errMsg + '\nInvalid config path: ' + this . common . nodes [ idx ] . authentication . configPath ;
2021-12-29 18:08:41 -05:00
}
} catch ( err ) {
this . errMsg = this . errMsg + '\nUnable to read config file: \n' + err ;
}
}
2024-06-10 12:40:37 -07:00
if ( this . common . nodes [ idx ] . lnImplementation === 'ECL' && this . common . nodes [ idx ] . authentication . lnApiPassword === '' ) {
2021-12-29 18:08:41 -05:00
this . errMsg = this . errMsg + '\nPlease set config path Or api password for node index ' + node . index + ' in RTL-Config.json! It is mandatory for Eclair authentication!' ;
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . LN_SERVER_URL && process ? . env ? . LN_SERVER_URL . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . lnServerUrl = process ? . env ? . LN_SERVER_URL . endsWith ( '/v1' ) ? process ? . env ? . LN_SERVER_URL . slice ( 0 , - 3 ) : process ? . env ? . LN_SERVER_URL ;
2022-12-27 18:05:42 -08:00
} else if ( process ? . env ? . LND_SERVER_URL && process ? . env ? . LND_SERVER_URL . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . lnServerUrl = process ? . env ? . LND_SERVER_URL . endsWith ( '/v1' ) ? process ? . env ? . LND_SERVER_URL . slice ( 0 , - 3 ) : process ? . env ? . LND_SERVER_URL ;
} else if ( node . settings . lnServerUrl && node . settings . lnServerUrl . trim ( ) !== '' ) {
this . common . nodes [ idx ] . settings . lnServerUrl = node . settings . lnServerUrl . endsWith ( '/v1' ) ? node . settings . lnServerUrl . slice ( 0 , - 3 ) : node . settings . lnServerUrl ;
} else if ( node . settings . lndServerUrl && node . settings . lndServerUrl . trim ( ) !== '' ) {
this . common . nodes [ idx ] . settings . lnServerUrl = node . settings . lndServerUrl . endsWith ( '/v1' ) ? node . settings . lndServerUrl . slice ( 0 , - 3 ) : node . settings . lndServerUrl ;
2021-12-29 18:08:41 -05:00
} else {
this . errMsg = this . errMsg + '\nPlease set LN Server URL for node index ' + node . index + ' in RTL-Config.json!' ;
}
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . userPersona = node . settings . userPersona ? node . settings . userPersona : 'MERCHANT' ;
this . common . nodes [ idx ] . settings . themeMode = node . settings . themeMode ? node . settings . themeMode : 'DAY' ;
this . common . nodes [ idx ] . settings . themeColor = node . settings . themeColor ? node . settings . themeColor : 'PURPLE' ;
this . common . nodes [ idx ] . settings . unannouncedChannels = node . settings . unannouncedChannels ? ! ! node.settings.unannouncedChannels : false ;
this . common . nodes [ idx ] . settings . logLevel = node . settings . logLevel ? node . settings . logLevel : 'ERROR' ;
this . common . nodes [ idx ] . settings . fiatConversion = node . settings . fiatConversion ? ! ! node.settings.fiatConversion : false ;
if ( this . common . nodes [ idx ] . settings . fiatConversion ) {
this . common . nodes [ idx ] . settings . currencyUnit = node . settings . currencyUnit ? node . settings . currencyUnit : 'USD' ;
2021-12-29 18:08:41 -05:00
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . SWAP_SERVER_URL && process ? . env ? . SWAP_SERVER_URL . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . swapServerUrl = process ? . env ? . SWAP_SERVER_URL . endsWith ( '/v1' ) ? process ? . env ? . SWAP_SERVER_URL . slice ( 0 , - 3 ) : process ? . env ? . SWAP_SERVER_URL ;
this . common . nodes [ idx ] . authentication . swapMacaroonPath = process ? . env ? . SWAP_MACAROON_PATH ;
} else if ( node . settings . swapServerUrl && node . settings . swapServerUrl . trim ( ) !== '' ) {
this . common . nodes [ idx ] . settings . swapServerUrl = node . settings . swapServerUrl . endsWith ( '/v1' ) ? node . settings . swapServerUrl . slice ( 0 , - 3 ) : node . settings . swapServerUrl ;
this . common . nodes [ idx ] . authentication . swapMacaroonPath = node . authentication . swapMacaroonPath ? node . authentication . swapMacaroonPath : '' ;
2021-12-29 18:08:41 -05:00
} else {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . swapServerUrl = '' ;
this . common . nodes [ idx ] . authentication . swapMacaroonPath = '' ;
2021-12-29 18:08:41 -05:00
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . BOLTZ_SERVER_URL && process ? . env ? . BOLTZ_SERVER_URL . trim ( ) !== '' ) {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . boltzServerUrl = process ? . env ? . BOLTZ_SERVER_URL . endsWith ( '/v1' ) ? process ? . env ? . BOLTZ_SERVER_URL . slice ( 0 , - 3 ) : process ? . env ? . BOLTZ_SERVER_URL ;
this . common . nodes [ idx ] . authentication . boltzMacaroonPath = process ? . env ? . BOLTZ_MACAROON_PATH ;
} else if ( node . settings . boltzServerUrl && node . settings . boltzServerUrl . trim ( ) !== '' ) {
this . common . nodes [ idx ] . settings . boltzServerUrl = node . settings . boltzServerUrl . endsWith ( '/v1' ) ? node . settings . boltzServerUrl . slice ( 0 , - 3 ) : node . settings . boltzServerUrl ;
this . common . nodes [ idx ] . authentication . boltzMacaroonPath = node . authentication . boltzMacaroonPath ? node . authentication . boltzMacaroonPath : '' ;
2021-12-29 18:08:41 -05:00
} else {
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . boltzServerUrl = '' ;
this . common . nodes [ idx ] . authentication . boltzMacaroonPath = '' ;
2021-12-29 18:08:41 -05:00
}
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . enableOffers = process ? . env ? . ENABLE_OFFERS ? process ? . env ? . ENABLE_OFFERS : ( node . settings . enableOffers ) ? node.settings.enableOffers : false ;
this . common . nodes [ idx ] . settings . enablePeerswap = process ? . env ? . ENABLE_PEERSWAP ? process ? . env ? . ENABLE_PEERSWAP : ( node . settings . enablePeerswap ) ? node.settings.enablePeerswap : false ;
this . common . nodes [ idx ] . settings . bitcoindConfigPath = process ? . env ? . BITCOIND_CONFIG_PATH ? process ? . env ? . BITCOIND_CONFIG_PATH : ( node . settings . bitcoindConfigPath ) ? node . settings . bitcoindConfigPath : '' ;
this . common . nodes [ idx ] . settings . channelBackupPath = process ? . env ? . CHANNEL_BACKUP_PATH ? process ? . env ? . CHANNEL_BACKUP_PATH : ( node . settings . channelBackupPath ) ? node.settings.channelBackupPath : this.common.appConfig.rtlConfFilePath + sep + 'channels-backup' + sep + 'node-' + node . index ;
this . common . nodes [ idx ] . settings . blockExplorerUrl = process ? . env ? . BLOCK_EXPLORER_URL ? process . env . BLOCK_EXPLORER_URL : ( node . settings . blockExplorerUrl ) ? node . settings . blockExplorerUrl : 'https://mempool.space' ;
2021-12-29 18:08:41 -05:00
try {
2024-06-10 12:40:37 -07:00
this . common . createDirectory ( this . common . nodes [ idx ] . settings . channelBackupPath ) ;
const exists = fs . existsSync ( this . common . nodes [ idx ] . settings . channelBackupPath + sep + 'channel-all.bak' ) ;
2021-12-29 18:08:41 -05:00
if ( ! exists ) {
try {
2024-06-10 12:40:37 -07:00
if ( this . common . nodes [ idx ] . lnImplementation === 'LND' ) {
2021-12-29 18:08:41 -05:00
this . common . getAllNodeAllChannelBackup ( this . common . nodes [ idx ] ) ;
} else {
2024-06-10 12:40:37 -07:00
const createStream = fs . createWriteStream ( this . common . nodes [ idx ] . settings . channelBackupPath + sep + 'channel-all.bak' ) ;
2021-12-29 18:08:41 -05:00
createStream . end ( ) ;
}
} catch ( err ) {
2024-06-10 12:40:37 -07:00
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while creating backup file: \n' + err } ) ;
2021-12-29 18:08:41 -05:00
}
}
} catch ( err ) {
2024-06-10 12:40:37 -07:00
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while creating the backup directory: \n' + err } ) ;
2021-12-29 18:08:41 -05:00
}
2024-06-10 12:40:37 -07:00
this . common . nodes [ idx ] . settings . logFile = config . rtlConfFilePath + '/logs/RTL-Node-' + node . index + '.log' ;
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'INFO' , fileName : 'Config' , msg : 'Node Config: ' + JSON . stringify ( this . common . nodes [ idx ] ) } ) ;
const log_file = this . common . nodes [ idx ] . settings . logFile ;
2022-12-27 18:05:42 -08:00
if ( fs . existsSync ( log_file || '' ) ) {
fs . writeFile ( ( log_file || '' ) , '' , ( ) = > { } ) ;
2021-12-29 18:08:41 -05:00
} else {
try {
2022-12-27 18:05:42 -08:00
const directoryName = dirname ( log_file || '' ) ;
2021-12-29 18:08:41 -05:00
this . common . createDirectory ( directoryName ) ;
2022-12-27 18:05:42 -08:00
const createStream = fs . createWriteStream ( log_file || '' ) ;
2021-12-29 18:08:41 -05:00
createStream . end ( ) ;
} catch ( err ) {
2024-06-10 12:40:37 -07:00
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while creating log file ' + log_file + ': \n' + err } ) ;
2021-12-29 18:08:41 -05:00
}
}
} ) ;
}
this . setSSOParams ( config ) ;
if ( this . errMsg && this . errMsg . trim ( ) !== '' ) { throw new Error ( this . errMsg ) ; }
} ;
private setSSOParams = ( config ) = > {
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . RTL_SSO ) {
2024-06-10 12:40:37 -07:00
config . SSO . rtlSso = + process ? . env ? . RTL_SSO ;
2021-12-29 18:08:41 -05:00
} else if ( config . SSO && config . SSO . rtlSSO ) {
2024-06-10 12:40:37 -07:00
config . SSO . rtlSso = config . SSO . rtlSSO ;
2021-12-29 18:08:41 -05:00
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . RTL_COOKIE_PATH ) {
2024-06-10 12:40:37 -07:00
config . SSO . rtlCookiePath = process ? . env ? . RTL_COOKIE_PATH ;
2021-12-29 18:08:41 -05:00
} else if ( config . SSO && config . SSO . rtlCookiePath ) {
2024-06-10 12:40:37 -07:00
config . SSO . rtlCookiePath = config . SSO . rtlCookiePath ;
2021-12-29 18:08:41 -05:00
} else {
2024-06-10 12:40:37 -07:00
config . SSO . rtlCookiePath = '' ;
2021-12-29 18:08:41 -05:00
}
2022-12-27 18:05:42 -08:00
if ( process ? . env ? . LOGOUT_REDIRECT_LINK ) {
2024-06-10 12:40:37 -07:00
config . SSO . logoutRedirectLink = process ? . env ? . LOGOUT_REDIRECT_LINK ;
2021-12-29 18:08:41 -05:00
} else if ( config . SSO && config . SSO . logoutRedirectLink ) {
2024-06-10 12:40:37 -07:00
config . SSO . logoutRedirectLink = config . SSO . logoutRedirectLink ;
2021-12-29 18:08:41 -05:00
}
2024-06-10 12:40:37 -07:00
if ( + config . SSO . rtlSso ) {
if ( ! config . SSO . rtlCookiePath || config . SSO . rtlCookiePath . trim ( ) === '' ) {
2022-05-01 13:35:20 -04:00
this . errMsg = 'Please set rtlCookiePath value for single sign on option!' ;
} else {
this . common . readCookie ( ) ;
}
2021-12-29 18:08:41 -05:00
}
} ;
private setSelectedNode = ( config ) = > {
if ( config . defaultNodeIndex ) {
2024-06-10 12:40:37 -07:00
this . common . selectedNode = this . common . findNode ( config . defaultNodeIndex ) || { } ;
2021-12-29 18:08:41 -05:00
} else {
2024-06-10 12:40:37 -07:00
this . common . selectedNode = this . common . findNode ( this . common . nodes [ 0 ] . index ) || { } ;
2021-12-29 18:08:41 -05:00
}
} ;
2024-06-10 12:40:37 -07:00
private updateConfig = ( confFileFullPath , config ) = > {
// Update Config file to change Settings to settings and Authentication to authentication
// Added in v0.15.1, remove in a year?
if ( ! config . nodes ) { return ; }
config . nodes . map ( ( node ) = > {
if ( node . Authentication ) {
node . authentication = JSON . parse ( JSON . stringify ( node . Authentication ) ) ;
delete node . Authentication ;
}
if ( node . Settings ) {
node . settings = JSON . parse ( JSON . stringify ( node . Settings ) ) ;
delete node . Settings ;
}
return node ;
} ) ;
fs . writeFileSync ( confFileFullPath , JSON . stringify ( config , null , 2 ) , 'utf-8' ) ;
} ;
2021-12-29 18:08:41 -05:00
public setServerConfiguration = ( ) = > {
2024-06-10 12:40:37 -07:00
const rtlConfFilePath = ( process ? . env ? . RTL_CONFIG_PATH ) ? process?.env?.RTL_CONFIG_PATH : join ( this . directoryName , '../..' ) ;
const confFileFullPath = rtlConfFilePath + sep + 'RTL-Config.json' ;
2021-12-29 18:08:41 -05:00
try {
if ( ! fs . existsSync ( confFileFullPath ) ) {
2024-06-10 12:40:37 -07:00
fs . writeFileSync ( confFileFullPath , JSON . stringify ( this . setDefaultConfig ( ) , null , 2 ) , 'utf-8' ) ;
2021-12-29 18:08:41 -05:00
}
const config = JSON . parse ( fs . readFileSync ( confFileFullPath , 'utf-8' ) ) ;
2024-06-10 12:40:37 -07:00
this . updateConfig ( confFileFullPath , config ) ;
config . rtlConfFilePath = rtlConfFilePath ;
2021-12-29 18:08:41 -05:00
this . updateLogByLevel ( ) ;
this . validateNodeConfig ( config ) ;
this . setSelectedNode ( config ) ;
2024-06-10 12:40:37 -07:00
this . common . appConfig = config ;
2022-12-27 18:05:42 -08:00
} catch ( err : any ) {
2024-06-10 12:40:37 -07:00
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Config file path: ' + confFileFullPath } ) ;
this . logger . log ( { selectedNode : this.common.selectedNode , level : 'ERROR' , fileName : 'Config' , msg : 'Something went wrong while configuring the node server: \n' + err } ) ;
2021-12-29 18:08:41 -05:00
throw new Error ( err ) ;
}
} ;
}
export const Config = new ConfigService ( ) ;