@ -1,3 +1,4 @@
var os = require ( 'os' ) ;
var fs = require ( 'fs' ) ;
var platform = require ( 'os' ) . platform ( ) ;
var crypto = require ( 'crypto' ) ;
@ -8,35 +9,30 @@ var path = require('path');
var upperCase = require ( 'upper-case' ) ;
var logger = require ( './controllers/logger' ) ;
var connect = { } ;
var errMsg = '' ;
const setDefaultConfig = ( ) => {
connect . setDefaultConfig = ( ) => {
var homeDir = os . userInfo ( ) . homedir ;
var macaroonPath = '' ;
var lndConfigPath = '' ;
var bitcoindConfigPath = '' ;
switch ( platform ) {
case 'win32' :
macaroonPath = 'C:\\Users\\<user>\\AppData\\Local\\Lnd\\data\\chain\\bitcoin\\testnet' ;
lndConfigPath = 'C:\\Users\\<user>\\AppData\\Local\\Lnd' ;
bitcoindConfigPath = 'C:\\Users\\<user>\\AppData\\Local\\bitcoin\\bitcoin_testnet' ;
macaroonPath = homeDir + '\\AppData\\Local\\Lnd\\data\\chain\\bitcoin\\mainnet' ;
lndConfigPath = homeDir + '\\AppData\\Local\\Lnd\\lnd.conf' ;
break ;
case 'darwin' :
macaroonPath = '/Users/<user>/Library/Application Support/Lnd/data/chain/bitcoin/testnet' ;
lndConfigPath = '/Users/<user>/Library/Application Support/Lnd/' ;
bitcoindConfigPath = '/Users/<user>/Library/Application Support/Bitcoin/' ;
macaroonPath = homeDir + '/Library/Application Support/Lnd/data/chain/bitcoin/mainnet' ;
lndConfigPath = homeDir + '/Library/Application Support/Lnd/lnd.conf' ;
break ;
case 'linux' :
macaroonPath = '/home/admin/.lnd/data/chain/bitcoin/testnet' ;
lndConfigPath = '/home/admin/.lnd/' ;
bitcoindConfigPath = '/home/admin/.lnd/' ;
macaroonPath = homeDir + '/.lnd/data/chain/bitcoin/mainnet' ;
lndConfigPath = homeDir + '/.lnd/lnd.conf' ;
break ;
default :
macaroonPath = '' ;
lndConfigPath = '' ;
bitcoindConfigPath = '' ;
break ;
}
}
return {
Authentication : {
macaroonPath : macaroonPath ,
@ -52,7 +48,6 @@ const setDefaultConfig = () => {
theme : 'dark-blue' ,
satsToBTC : false ,
lndServerUrl : 'https://localhost:8080/v1' ,
bitcoindConfigPath : bitcoindConfigPath ,
enableLogging : false ,
port : 3000
} ,
@ -64,7 +59,7 @@ const setDefaultConfig = () => {
} ;
}
const normalizePort = val => {
connect . normalizePort = val => {
var port = parseInt ( val , 10 ) ;
if ( isNaN ( port ) ) {
return val ;
@ -75,7 +70,7 @@ const normalizePort = val => {
return false ;
} ;
const setMacaroonPath = ( clArgs , config ) => {
connect . setMacaroonPath = ( clArgs , config ) => {
if ( undefined !== clArgs . lndir ) {
common . macaroon _path = clArgs . lndir ;
} else if ( undefined !== process . env . MACAROON _PATH ) {
@ -89,7 +84,7 @@ const setMacaroonPath = (clArgs, config) => {
}
}
const validateConfigFile = ( config ) => {
connect . validateSingleNodeConfig = ( config ) => {
if ( common . macaroon _path === '' || undefined === common . macaroon _path ) {
errMsg = 'Please set macaroon path through environment or RTL.conf!' ;
}
@ -144,7 +139,6 @@ const validateConfigFile = (config) => {
common . rtl _pass = process . env . RTL _PASS ;
} else if ( config . Authentication . rtlPass !== '' || undefined !== config . Authentication . rtlPass ) {
common . rtl _pass = config . Authentication . rtlPass ;
}
if ( upperCase ( common . node _auth _type ) === 'CUSTOM' && ( common . rtl _pass === '' || undefined === common . rtl _pass ) ) {
@ -157,16 +151,16 @@ const validateConfigFile = (config) => {
common . enable _logging = config . Settings . enableLogging ;
} else if ( undefined !== config . Authentication . enableLogging ) {
common . enable _logging = config . Authentication . enableLogging ;
}
}
if ( common . enable _logging ) {
common . log _file = common . rtl _conf _file _path + '/logs/RTL.log' ;
let exists = fs . existsSync ( common . log _file ) ;
let exists = fs . existsSync ( common . log _file ) ;
if ( exists ) {
fs . writeFile ( common . log _file , '' , ( ) => { } ) ;
} else if ( ( ! exists && config . Authentication . enableLogging ) || ( ! exists && config . Settings . enableLogging ) ) {
} else {
try {
var dirname = path . dirname ( common . log _file ) ;
createDirectory ( dirname ) ;
connect . createDirectory ( dirname ) ;
var createStream = fs . createWriteStream ( common . log _file ) ;
createStream . end ( ) ;
}
@ -177,18 +171,67 @@ const validateConfigFile = (config) => {
}
if ( undefined !== process . env . PORT ) {
common . port = normalizePort( process . env . PORT ) ;
common . port = connect. normalizePort( process . env . PORT ) ;
} else if ( undefined !== config . Settings . port ) {
common . port = normalizePort( config . Settings . port ) ;
common . port = connect. normalizePort( config . Settings . port ) ;
}
setSSOParams( config ) ;
connect. setSSOParams( config ) ;
if ( errMsg !== '' ) {
throw new Error ( errMsg ) ;
}
}
const setSSOParams = ( config ) => {
connect . validateMultiNodeConfig = ( config ) => {
common . node _auth _type = 'CUSTOM' ;
common . rtl _pass = config . multiPass ;
config . nodes . forEach ( ( node , idx ) => {
common . nodes [ idx ] = { } ;
if ( node . Authentication . macaroonPath === '' || undefined === node . Authentication . macaroonPath ) {
errMsg = 'Please set macaroon path for node index ' + node . index + ' in RTL-Multi-Node-Conf.json!' ;
} else {
common . nodes [ idx ] . macaroon _path = node . Authentication . macaroonPath ;
}
if ( ( node . Settings . lndServerUrl === '' || undefined === node . Settings . lndServerUrl ) ) {
errMsg = errMsg + '\nPlease set LND server URL for node index ' + node . index + ' in RTL-Multi-Node-Conf.json!' ;
} else {
common . nodes [ idx ] . lnd _server _url = node . Settings . lndServerUrl ;
}
common . nodes [ idx ] . index = node . index ;
common . nodes [ idx ] . ln _node = node . lnNode ;
common . nodes [ idx ] . ln _implementation = node . lnImplementation ;
common . nodes [ idx ] . lnd _config _path = ( undefined !== node . Authentication . lndConfigPath ) ? node . Authentication . lndConfigPath : '' ;
common . nodes [ idx ] . bitcoind _config _path = ( undefined !== node . Settings . bitcoindConfigPath ) ? node . Settings . bitcoindConfigPath : '' ;
common . nodes [ idx ] . enable _logging = ( undefined !== node . Settings . enableLogging ) ? node . Settings . enableLogging : false ;
common . nodes [ idx ] . port = ( undefined !== node . Settings . port ) ? connect . normalizePort ( node . Settings . port ) : 3000 ;
if ( common . nodes [ idx ] . enable _logging ) {
common . nodes [ idx ] . log _file = common . rtl _conf _file _path + '/logs/RTL-Node-' + node . index + '.log' ;
const log _file = common . nodes [ idx ] . log _file ;
if ( fs . existsSync ( log _file ) ) {
fs . writeFile ( log _file , '' , ( ) => { } ) ;
} else {
try {
var dirname = path . dirname ( log _file ) ;
connect . createDirectory ( dirname ) ;
var createStream = fs . createWriteStream ( log _file ) ;
createStream . end ( ) ;
}
catch ( err ) {
console . error ( 'Something went wrong while creating log file ' + log _file + ': \n' + err ) ;
}
}
}
} ) ;
connect . setSSOParams ( config ) ;
if ( errMsg !== '' ) { throw new Error ( errMsg ) ; }
}
connect . setSSOParams = ( config ) => {
if ( undefined !== process . env . RTL _SSO ) {
common . rtl _sso = process . env . RTL _SSO ;
} else if ( undefined !== config . SSO && undefined !== config . SSO . rtlSSO ) {
@ -210,11 +253,11 @@ const setSSOParams = (config) => {
} else {
common . rtl _cookie _path = common . rtl _conf _file _path + '/cookies/auth.cookie' ;
}
readCookie( common . rtl _cookie _path ) ;
connect. readCookie( common . rtl _cookie _path ) ;
}
} ;
const createDirectory = ( dirname ) => {
connect . createDirectory = ( dirname ) => {
try {
fs . mkdirSync ( dirname ) ;
} catch ( err ) {
@ -227,300 +270,116 @@ const createDirectory = (dirname) => {
}
}
const readCookie = ( cookieFile ) => {
connect . readCookie = ( cookieFile ) => {
let exists = fs . existsSync ( cookieFile ) ;
if ( exists ) {
common . cookie = fs . readFileSync ( cookieFile , 'utf-8' ) ;
} else {
try {
var dirname = path . dirname ( cookieFile ) ;
c reateDirectory( dirname ) ;
c onnect. c reateDirectory( dirname ) ;
fs . writeFileSync ( cookieFile , crypto . randomBytes ( 64 ) . toString ( 'hex' ) ) ;
common . cookie = fs . readFileSync ( cookieFile , 'utf-8' ) ;
}
catch ( err ) {
console . error ( 'Something went wrong while creating cookie fil e: \n' + err ) ;
console . error ( 'Something went wrong while reading cooki e: \n' + err ) ;
throw new Error ( err ) ;
}
}
}
String . random = function ( length ) {
let radom13chars = function ( ) {
return Math . random ( ) . toString ( 16 ) . substring ( 2 , 15 ) . toUpperCase ( ) ;
}
let loops = Math . ceil ( length / 13 ) ;
return new Array ( loops ) . fill ( radom13chars ) . reduce ( ( string , func ) => {
return string + func ( ) ;
} , '' ) . substring ( - length ) ;
connect . refreshCookie = ( cookieFile ) => {
try {
fs . writeFileSync ( cookieFile , crypto . randomBytes ( 64 ) . toString ( 'hex' ) ) ;
common . cookie = fs . readFileSync ( cookieFile , 'utf-8' ) ;
}
catch ( err ) {
console . error ( 'Something went wrong while refreshing cookie: \n' + err ) ;
throw new Error ( err ) ;
}
}
const logEnvVariables = ( ) => {
if ( ! common . enable _logging ) {
return ;
connect . logEnvVariables = ( ) => {
if ( common . multi _node _setup ) {
common . nodes . forEach ( ( node , idx ) => {
if ( ! node . enable _logging ) { return ; }
logger . info ( '\r\nConfig Setup Variable NODE_SETUP: MULTI' , node ) ;
logger . info ( '\r\nConfig Setup Variable RTL_SSO: ' + common . rtl _sso , node ) ;
logger . info ( '\r\nConfig Setup Variable RTL_COOKIE_PATH: ' + common . rtl _cookie _path , node ) ;
logger . info ( '\r\nConfig Setup Variable LOGOUT_REDIRECT_LINK: ' + common . logout _redirect _link + '\r\n' , node ) ;
logger . info ( '\r\nConfig Setup Variable INDEX: ' + node . index , node ) ;
logger . info ( '\r\nConfig Setup Variable LN NODE: ' + node . ln _node , node ) ;
logger . info ( '\r\nConfig Setup Variable LN IMPLEMENTATION: ' + node . ln _implementation , node ) ;
logger . info ( '\r\nConfig Setup Variable PORT: ' + node . port , node ) ;
logger . info ( '\r\nConfig Setup Variable MACAROON_PATH: ' + node . macaroon _path , node ) ;
logger . info ( '\r\nConfig Setup Variable LND_SERVER_URL: ' + node . lnd _server _url , node ) ;
logger . info ( '\r\nConfig Setup Variable RTL_CONFIG_PATH: ' + node . rtl _conf _file _path , node ) ;
logger . info ( '\r\nConfig Setup Variable LND_CONFIG_PATH: ' + node . lnd _config _path , node ) ;
logger . info ( '\r\nConfig Setup Variable BITCOIND_CONFIG_PATH: ' + node . bitcoind _config _path , node ) ;
} ) ;
} else {
if ( ! common . enable _logging ) { return ; }
logger . info ( '\r\nConfig Setup Variable NODE_SETUP: SINGLE' ) ;
logger . info ( '\r\nConfig Setup Variable PORT: ' + common . port ) ;
logger . info ( '\r\nConfig Setup Variable LND_SERVER_URL: ' + common . lnd _server _url ) ;
logger . info ( '\r\nConfig Setup Variable MACAROON_PATH: ' + common . macaroon _path ) ;
logger . info ( '\r\nConfig Setup Variable NODE_AUTH_TYPE: ' + common . node _auth _type ) ;
logger . info ( '\r\nConfig Setup Variable LND_CONFIG_PATH: ' + common . lnd _config _path ) ;
logger . info ( '\r\nConfig Setup Variable RTL_CONFIG_PATH: ' + common . rtl _conf _file _path ) ;
logger . info ( '\r\nConfig Setup Variable BITCOIND_CONFIG_PATH: ' + common . bitcoind _config _path ) ;
logger . info ( '\r\nConfig Setup Variable RTL_SSO: ' + common . rtl _sso ) ;
logger . info ( '\r\nConfig Setup Variable RTL_COOKIE_PATH: ' + common . rtl _cookie _path ) ;
logger . info ( '\r\nConfig Setup Variable LOGOUT_REDIRECT_LINK: ' + common . logout _redirect _link ) ;
}
logger . info ( '\r\nConfig Setup Variable PORT: ' + common . port ) ;
logger . info ( '\r\nConfig Setup Variable LND_SERVER_URL: ' + common . lnd _server _url ) ;
logger . info ( '\r\nConfig Setup Variable MACAROON_PATH: ' + common . macaroon _path ) ;
logger . info ( '\r\nConfig Setup Variable NODE_AUTH_TYPE: ' + common . node _auth _type ) ;
logger . info ( '\r\nConfig Setup Variable LND_CONFIG_PATH: ' + common . lnd _config _path ) ;
logger . info ( '\r\nConfig Setup Variable RTL_CONFIG_PATH: ' + common . rtl _conf _file _path ) ;
logger . info ( '\r\nConfig Setup Variable BITCOIND_CONFIG_PATH: ' + common . bitcoind _config _path ) ;
logger . info ( '\r\nConfig Setup Variable RTL_SSO: ' + common . rtl _sso ) ;
logger . info ( '\r\nConfig Setup Variable RTL_COOKIE_PATH: ' + common . rtl _cookie _path ) ;
logger . info ( '\r\nConfig Setup Variable LOGOUT_REDIRECT_LINK: ' + common . logout _redirect _link ) ;
}
var errMsg = '' ;
connect . setSingleNodeConfiguration = ( ) => {
common . rtl _conf _file _path = ( undefined !== process . env . RTL _CONFIG _PATH ) ? process . env . RTL _CONFIG _PATH . substring ( 0 , process . env . RTL _CONFIG _PATH . length - 9 ) : path . normalize ( _ _dirname ) ;
RTLConfFile = common . rtl _conf _file _path + '/RTL.conf' ;
let exists = fs . existsSync ( RTLConfFile ) ;
connect . setSingleNodeConfiguration = ( singleNodeFilePath ) => {
const exists = fs . existsSync ( singleNodeFilePath ) ;
if ( exists ) {
var config = ini . parse ( fs . readFileSync ( RTLConfFile , 'utf-8' ) ) ;
setMacaroonPath ( clArgs , config )
validateConfigFile ( config ) ;
logEnvVariables ( ) ;
var config = ini . parse ( fs . readFileSync ( singleNodeFilePath , 'utf-8' ) ) ;
connect . setMacaroonPath ( clArgs , config ) ;
connect . validateSingleNodeConfig ( config ) ;
connect . logEnvVariables ( ) ;
} else {
try {
fs . writeFileSync ( RTLConfFile , ini . stringify ( setDefaultConfig ( ) ) ) ;
throw new Error ( 'Please change default settings of macaroonPath, lndConfigPath and bitcoindConfigPath in RTL.conf and restart the server' ) ;
fs . writeFileSync ( singleNodeFilePath , ini . stringify ( connect . setDefaultConfig ( ) ) ) ;
var config = ini . parse ( fs . readFileSync ( singleNodeFilePath , 'utf-8' ) ) ;
connect . setMacaroonPath ( clArgs , config ) ;
connect . validateSingleNodeConfig ( config ) ;
connect . logEnvVariables ( ) ;
}
catch ( err ) {
console . error ( 'Something went wrong while creating config file: \n' + err ) ;
console . error ( 'Something went wrong while c onfiguring the single node server : \n' + err ) ;
throw new Error ( err ) ;
}
}
}
connect . setNodeConfiguration = ( ) => {
common . rtl _conf _file _path = ( undefined !== process . env . RTL _CONFIG _PATH ) ? process . env . RTL _CONFIG _PATH . substring ( 0 , process . env . RTL _CONFIG _PATH . length - 9 ) : path . normalize ( _ _dirname ) ;
RTLConfFile = common . rtl _conf _file _path + '/RTL.conf' ;
common . rtl _multi _node _conf _file _path = ( undefined !== process . env . RTL _MULTI _NODE _CONFIG _PATH ) ? process . env . RTL _MULTI _NODE _CONFIG _PATH . substring ( 0 , process . env . RTL _MULTI _NODE _CONFIG _PATH . length - 25 ) : path . normalize ( _ _dirname ) ;
RTLMultiNodeConfFile = common . rtl _multi _node _conf _file _path + '/RTL-Multi-Node-Conf.json' ;
connect . setMultiNodeConfiguration = ( multiNodeFilePath ) => {
try {
var config = JSON . parse ( fs . readFileSync ( multiNodeFilePath , 'utf-8' ) ) ;
connect . validateMultiNodeConfig ( config ) ;
connect . logEnvVariables ( ) ;
}
catch ( err ) {
console . error ( 'Something went wrong while configuring the multi node server: \n' + err ) ;
throw new Error ( err ) ;
}
}
// MULTI NODE CODE
// const setMacaroonPath = (clArgs, config) => {
// if(undefined !== clArgs.lndir) {
// common.macaroon_path = clArgs.lndir;
// } else if (undefined !== process.env.MACAROON_PATH) {
// common.macaroon_path = process.env.MACAROON_PATH;
// } else {
// if(undefined !== config.Authentication.macroonPath && config.Authentication.macroonPath !== '') {
// common.macaroon_path = config.Authentication.macroonPath;
// } else if(undefined !== config.Authentication.macaroonPath && config.Authentication.macaroonPath !== '') {
// common.macaroon_path = config.Authentication.macaroonPath;
// }
// }
// }
// const validateConfigFile = (config) => {
// if(common.macaroon_path === '' || undefined === common.macaroon_path) {
// errMsg = 'Please set macaroon path through environment or RTL.conf!';
// }
// if(undefined !== process.env.LND_SERVER_URL) {
// common.lnd_server_url = process.env.LND_SERVER_URL;
// } else {
// if((config.Authentication.lndServerUrl === '' || undefined === config.Authentication.lndServerUrl) && (config.Settings.lndServerUrl === '' || undefined === config.Settings.lndServerUrl)) {
// errMsg = errMsg + '\nPlease set LND Server URL through environment or RTL.conf!';
// } else {
// if (config.Settings.lndServerUrl !== '' && undefined !== config.Settings.lndServerUrl) {
// common.lnd_server_url = config.Settings.lndServerUrl;
// } else if (config.Authentication.lndServerUrl !== '' && undefined !== config.Authentication.lndServerUrl) {
// common.lnd_server_url = config.Authentication.lndServerUrl;
// }
// }
// }
// if(undefined !== process.env.NODE_AUTH_TYPE) {
// common.node_auth_type = process.env.NODE_AUTH_TYPE;
// } else {
// if(config.Authentication.nodeAuthType === '' || undefined === config.Authentication.nodeAuthType) {
// errMsg = errMsg + '\nPlease set Node Auth Type through environment/RTL.conf!';
// } else {
// common.node_auth_type = config.Authentication.nodeAuthType;
// }
// }
// if(undefined !== process.env.LND_CONFIG_PATH) {
// common.lnd_config_path = process.env.LND_CONFIG_PATH;
// } else {
// if(config.Authentication.lndConfigPath !== '' && undefined !== config.Authentication.lndConfigPath) {
// common.lnd_config_path = config.Authentication.lndConfigPath;
// } else {
// if(upperCase(common.node_auth_type) === 'DEFAULT') {
// errMsg = errMsg + '\nDefault Node Authentication can be set with LND Config Path only. Please set LND Config Path through environment or RTL.conf!';
// }
// }
// }
// if(undefined !== process.env.BITCOIND_CONFIG_PATH) {
// common.bitcoind_config_path = process.env.BITCOIND_CONFIG_PATH;
// } else {
// if(config.Settings.bitcoindConfigPath !== '' || undefined !== config.Settings.bitcoindConfigPath) {
// common.bitcoind_config_path = config.Settings.bitcoindConfigPath;
// } else if(config.Authentication.bitcoindConfigPath !== '' || undefined !== config.Authentication.bitcoindConfigPath) {
// common.bitcoind_config_path = config.Authentication.bitcoindConfigPath;
// }
// }
// if (undefined !== process.env.RTL_PASS) {
// common.rtl_pass = process.env.RTL_PASS;
// } else if (config.Authentication.rtlPass !== '' || undefined !== config.Authentication.rtlPass) {
// common.rtl_pass = config.Authentication.rtlPass;
// }
// if (upperCase(common.node_auth_type) === 'CUSTOM' && (common.rtl_pass === '' || undefined === common.rtl_pass)) {
// errMsg = errMsg + '\nCustom Node Authentication can be set with RTL password only. Please set RTL Password through environment/RTL.conf';
// }
// if (undefined !== process.env.ENABLE_LOGGING) {
// common.enable_logging = process.env.ENABLE_LOGGING;
// } else if (undefined !== config.Settings.enableLogging) {
// common.enable_logging = config.Settings.enableLogging;
// } else if (undefined !== config.Authentication.enableLogging) {
// common.enable_logging = config.Authentication.enableLogging;
// }
// if (common.enable_logging) {
// common.log_file = common.rtl_conf_file_path + '/logs/RTL.log';
// let exists = fs.existsSync(common.log_file);
// if (exists) {
// fs.writeFile(common.log_file, '', () => { });
// } else if ((!exists && config.Authentication.enableLogging) || (!exists && config.Settings.enableLogging)) {
// try {
// var dirname = path.dirname(common.log_file);
// createDirectory(dirname);
// var createStream = fs.createWriteStream(common.log_file);
// createStream.end();
// }
// catch (err) {
// console.error('Something went wrong while creating log file: \n' + err);
// }
// }
// }
// if (undefined !== process.env.PORT) {
// common.port = normalizePort(process.env.PORT);
// } else if (undefined !== config.Settings.port) {
// common.port = normalizePort(config.Settings.port);
// }
// setSSOParams(config);
// if (errMsg !== '') {
// throw new Error(errMsg);
// }
// }
// const setSSOParams = (config) => {
// if (undefined !== process.env.RTL_SSO) {
// common.rtl_sso = process.env.RTL_SSO;
// } else if (undefined !== config.SSO && undefined !== config.SSO.rtlSSO) {
// common.rtl_sso = config.SSO.rtlSSO;
// }
// if (+common.rtl_sso) {
// if (undefined !== process.env.LOGOUT_REDIRECT_LINK) {
// common.logout_redirect_link = process.env.LOGOUT_REDIRECT_LINK;
// } else if (undefined !== config.SSO && undefined !== config.SSO.logoutRedirectLink) {
// common.logout_redirect_link = config.SSO.logoutRedirectLink;
// }
// if (undefined !== process.env.RTL_COOKIE_PATH) {
// common.rtl_cookie_path = process.env.RTL_COOKIE_PATH;
// } else if (undefined !== config.SSO && undefined !== config.SSO.rtlCookiePath) {
// common.rtl_cookie_path = config.SSO.rtlCookiePath;
// } else {
// common.rtl_cookie_path = common.rtl_conf_file_path + '/cookies/auth.cookie';
// }
// readCookie(common.rtl_cookie_path);
// }
// };
// const createDirectory = (dirname) => {
// try {
// fs.mkdirSync(dirname);
// } catch (err) {
// if (err.code === 'EEXIST') {
// return dirname;
// }
// if (err.code === 'ENOENT') {
// throw new Error(`EACCES: permission denied, mkdir '${dirname}'`);
// }
// }
// }
// const readCookie = (cookieFile) => {
// let exists = fs.existsSync(cookieFile);
// if (exists) {
// common.cookie = fs.readFileSync(cookieFile, 'utf-8');
// } else {
// try {
// var dirname = path.dirname(cookieFile);
// createDirectory(dirname);
// fs.writeFileSync(cookieFile, crypto.randomBytes(64).toString('hex'));
// common.cookie = fs.readFileSync(cookieFile, 'utf-8');
// }
// catch(err) {
// console.error('Something went wrong while creating cookie file: \n' + err);
// throw new Error(err);
// }
// }
// }
// String.random = function (length) {
// let radom13chars = function () {
// return Math.random().toString(16).substring(2, 15).toUpperCase();
// }
// let loops = Math.ceil(length / 13);
// return new Array(loops).fill(radom13chars).reduce((string, func) => {
// return string + func();
// }, '').substring(-length);
// }
// const logEnvVariables = () => {
// if (!common.enable_logging) {
// return;
// }
// logger.info('\r\nConfig Setup Variable PORT: ' + common.port);
// logger.info('\r\nConfig Setup Variable LND_SERVER_URL: ' + common.lnd_server_url);
// logger.info('\r\nConfig Setup Variable MACAROON_PATH: ' + common.macaroon_path);
// logger.info('\r\nConfig Setup Variable NODE_AUTH_TYPE: ' + common.node_auth_type);
// logger.info('\r\nConfig Setup Variable LND_CONFIG_PATH: ' + common.lnd_config_path);
// logger.info('\r\nConfig Setup Variable RTL_CONFIG_PATH: ' + common.rtl_conf_file_path);
// logger.info('\r\nConfig Setup Variable BITCOIND_CONFIG_PATH: ' + common.bitcoind_config_path);
// logger.info('\r\nConfig Setup Variable RTL_SSO: ' + common.rtl_sso);
// logger.info('\r\nConfig Setup Variable RTL_COOKIE_PATH: ' + common.rtl_cookie_path);
// logger.info('\r\nConfig Setup Variable LOGOUT_REDIRECT_LINK: ' + common.logout_redirect_link);
// }
// var errMsg = '';
// connect.configFileExists = () => {
// common.rtl_conf_file_path = (undefined !== process.env.RTL_CONFIG_PATH) ? process.env.RTL_CONFIG_PATH.substring(0, process.env.RTL_CONFIG_PATH.length - 9) : path.normalize(__dirname);
// RTLConfFile = common.rtl_conf_file_path + '/RTL.conf';
// let exists = fs.existsSync(RTLConfFile);
// if (exists) {
// var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8'));
// setMacaroonPath(clArgs, config)
// validateConfigFile(config);
// logEnvVariables();
// } else {
// try {
// fs.writeFileSync(RTLConfFile, ini.stringify(setDefaultConfig()));
// throw new Error('Please change default settings of macaroonPath, lndConfigPath and bitcoindConfigPath in RTL.conf and restart the server');
// }
// catch(err) {
// console.error('Something went wrong while creating config file: \n' + err);
// throw new Error(err);
// }
// }
// }
connect . setServerConfiguration = ( ) => {
common . rtl _conf _file _path = ( undefined !== process . env . RTL _CONFIG _PATH ) ? process . env . RTL _CONFIG _PATH . substring ( 0 , process . env . RTL _CONFIG _PATH . length - 9 ) : path . normalize ( _ _dirname ) ;
singleNodeConfFile = common . rtl _conf _file _path + '/RTL.conf' ;
multiNodeConfFile = common . rtl _conf _file _path + '/RTL-Multi-Node-Conf.json' ;
const singleNodeExists = fs . existsSync ( singleNodeConfFile ) ;
const multiNodeExists = fs . existsSync ( multiNodeConfFile ) ;
if ( ( ! multiNodeExists && singleNodeExists ) || ( ! multiNodeExists && ! singleNodeExists ) ) {
common . multi _node _setup = false ;
connect . setSingleNodeConfiguration ( singleNodeConfFile ) ;
} else if ( ( multiNodeExists && singleNodeExists ) || ( multiNodeExists && ! singleNodeExists ) ) {
common . multi _node _setup = true ;
connect . setMultiNodeConfiguration ( multiNodeConfFile ) ;
}
}
module . exports = connect ;