diff --git a/common.js b/common.js index 76b736d6..cd3ac5f5 100644 --- a/common.js +++ b/common.js @@ -1,7 +1,10 @@ var common = {}; +common.rtl_conf_file_path = ''; common.lnd_server_url = ''; common.lnd_config_path = ''; +common.node_auth_type = ''; +common.macaroon_path = ''; common.bitcoind_config_path = ''; common.enable_logging = false; diff --git a/connect.js b/connect.js index a8b70883..c8db7059 100644 --- a/connect.js +++ b/connect.js @@ -4,10 +4,7 @@ var ini = require('ini'); var common = require('./common'); var upperCase = require('upper-case'); var path = require('path'); -var macaroonPath = ''; var options = {}; -var file_path = path.normalize(__dirname) + '/RTL.conf'; -var log_file_path = path.normalize(__dirname) + '/RTL.log'; var defaultConfig = { Authentication: { @@ -31,53 +28,76 @@ var defaultConfig = { var setMacaroonPath = (clArgs, config) => { if(undefined !== clArgs.lndir) { - macaroonPath = clArgs.lndir; + common.macaroon_path = clArgs.lndir; + } else if (undefined !== clArgs.macaroonPath) { + common.macaroon_path = clArgs.macaroonPath; } else { if(undefined !== config.Authentication.macroonPath && config.Authentication.macroonPath !== '') { - macaroonPath = config.Authentication.macroonPath; + common.macaroon_path = config.Authentication.macroonPath; } else if(undefined !== config.Authentication.macaroonPath && config.Authentication.macaroonPath !== '') { - macaroonPath = config.Authentication.macaroonPath; + common.macaroon_path = config.Authentication.macaroonPath; } } } -var validateConfigFile = (macaroonPath, config) => { - if(macaroonPath === '' || undefined === macaroonPath) { - errMsg = 'Please set macaroon path in RTL.conf'; +var validateConfigFile = (config) => { + if(common.macaroon_path === '' || undefined === common.macaroon_path) { + errMsg = 'Please set macaroon path through environment/RTL.conf!'; } - if(config.Authentication.lndServerUrl === '' || undefined === config.Authentication.lndServerUrl) { - errMsg = errMsg + '\nPlease set LND Server URL in RTL.conf'; + if(undefined !== clArgs.lndServerUrl) { + common.lnd_server_url = clArgs.lndServerUrl; } else { - common.lnd_server_url = config.Authentication.lndServerUrl; - } - - if(config.Authentication.nodeAuthType === '' || undefined === config.Authentication.nodeAuthType) { - errMsg = errMsg + '\nPlease set Node Auth Type in RTL.conf'; - } - - if(upperCase(config.Authentication.nodeAuthType) === 'DEFAULT' && (config.Authentication.lndConfigPath === '' || undefined === config.Authentication.lndConfigPath)) { - errMsg = errMsg + '\nDefault Node Authentication can be set with LND Config Path only. Please set LND Config Path in RTL.conf'; - } else { - common.lnd_config_path = config.Authentication.lndConfigPath; + if(config.Authentication.lndServerUrl === '' || undefined === config.Authentication.lndServerUrl) { + errMsg = errMsg + '\nPlease set LND Server URL through environment/RTL.conf!'; + } else { + common.lnd_server_url = config.Authentication.lndServerUrl; + } } - if(config.Authentication.bitcoindConfigPath !== '' || undefined !== config.Authentication.bitcoindConfigPath) { - common.bitcoind_config_path = config.Authentication.bitcoindConfigPath; + if(undefined !== clArgs.nodeAuthType) { + common.node_auth_type = clArgs.nodeAuthType; + } 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(upperCase(config.Authentication.nodeAuthType) === 'CUSTOM' && (config.Authentication.rtlPass === '' || undefined === config.Authentication.rtlPass)) { + if(undefined !== clArgs.lndConfigPath) { + common.lnd_config_path = clArgs.lndConfigPath; + } 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/RTL.conf!'; + } + } + } + + if(undefined !== clArgs.bitcoindConfigPath) { + common.bitcoind_config_path = clArgs.bitcoindConfigPath; + } else { + if(config.Authentication.bitcoindConfigPath !== '' || undefined !== config.Authentication.bitcoindConfigPath) { + common.bitcoind_config_path = config.Authentication.bitcoindConfigPath; + } + } + + if(upperCase(common.node_auth_type) === 'CUSTOM' && (config.Authentication.rtlPass === '' || undefined === config.Authentication.rtlPass)) { errMsg = errMsg + '\nCustom Node Authentication can be set with RTL password only. Please set RTL Password in RTL.conf'; } if(undefined !== config.Authentication.enableLogging) { common.enable_logging = config.Authentication.enableLogging; - let exists = fs.existsSync(log_file_path); + var logFile = common.rtl_conf_file_path + '/RTL.log'; + let exists = fs.existsSync(logFile); if(exists) { - fs.writeFile(log_file_path, '', () => {}); + fs.writeFile(logFile, '', () => {}); } else if (!exists && config.Authentication.enableLogging) { try { - var createStream = fs.createWriteStream(log_file_path); + var createStream = fs.createWriteStream(logFile); createStream.end(); } catch(err) { @@ -91,8 +111,8 @@ var validateConfigFile = (macaroonPath, config) => { } } -var setOptions = (macaroonPath) => { - var macaroon = fs.readFileSync(macaroonPath + '/admin.macaroon').toString('hex'); +var setOptions = () => { + var macaroon = fs.readFileSync(common.macaroon_path + '/admin.macaroon').toString('hex'); options = { url: '', rejectUnauthorized: false, @@ -106,22 +126,25 @@ var setOptions = (macaroonPath) => { var errMsg = ''; var configFileExists = () => { - let exists = fs.existsSync(file_path); + common.rtl_conf_file_path = (undefined !== clArgs.rtlConfFilePath) ? clArgs.rtlConfFilePath : path.normalize(__dirname); + RTLConfFile = common.rtl_conf_file_path + '/RTL.conf'; + let exists = fs.existsSync(RTLConfFile); if (exists) { - var config = ini.parse(fs.readFileSync(file_path, 'utf-8')); + var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8')); setMacaroonPath(clArgs, config) - validateConfigFile(macaroonPath, config); - setOptions(macaroonPath); + validateConfigFile(config); + setOptions(); } else { try { - fs.writeFileSync(file_path, ini.stringify(defaultConfig)); - var config = ini.parse(fs.readFileSync(file_path, 'utf-8')); + fs.writeFileSync(RTLConfFile, ini.stringify(defaultConfig)); + var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8')); setMacaroonPath(clArgs, config) - validateConfigFile(macaroonPath, config); - setOptions(macaroonPath); + validateConfigFile(config); + setOptions(); } catch(err) { - console.error('Something went wrong, unable to create config file!' + err); + console.error('Something went wrong, unable to create config file!\n' + err); + throw new Error(err); } } } diff --git a/controllers/RTLConf.js b/controllers/RTLConf.js index 3f3d0ff1..3122b19a 100644 --- a/controllers/RTLConf.js +++ b/controllers/RTLConf.js @@ -1,13 +1,13 @@ var ini = require('ini'); var path = require('path'); var fs = require('fs'); -var file_path = path.normalize(__dirname + '/..') + '/RTL.conf'; var logger = require('./logger'); var common = require('../common'); +var RTLConfFilePath = common.rtl_conf_file_path + '/RTL.conf'; exports.getRTLConfig = (req, res, next) => { logger.info('\r\nConf: 7: ' + JSON.stringify(Date.now()) + ': INFO: Getting RTL Config'); - fs.readFile(file_path, 'utf8', function(err, data) { + fs.readFile(RTLConfFilePath, 'utf8', function(err, data) { if (err) { logger.error('\r\nConf: 10: ' + JSON.stringify(Date.now()) + ': ERROR: Getting RTL Config Failed!'); res.status(500).json({ @@ -17,9 +17,9 @@ exports.getRTLConfig = (req, res, next) => { } else { const jsonConfig = ini.parse(data); authSettings = { - nodeAuthType: (jsonConfig.Authentication.nodeAuthType) ? jsonConfig.Authentication.nodeAuthType : 'DEFAULT', - lndConfigPath: (jsonConfig.Authentication.lndConfigPath) ? jsonConfig.Authentication.lndConfigPath : '', - bitcoindConfigPath: (jsonConfig.Authentication.bitcoindConfigPath) ? jsonConfig.Authentication.bitcoindConfigPath : '' + nodeAuthType: (common.node_auth_type) ? common.node_auth_type : 'DEFAULT', + lndConfigPath: (common.lnd_config_path) ? common.lnd_config_path : '', + bitcoindConfigPath: (common.bitcoind_config_path) ? common.bitcoind_config_path : '' }; res.status(200).json({settings: jsonConfig.Settings, authSettings: authSettings}); } @@ -27,10 +27,10 @@ exports.getRTLConfig = (req, res, next) => { }; exports.updateUISettings = (req, res, next) => { - var config = ini.parse(fs.readFileSync(file_path, 'utf-8')); + var config = ini.parse(fs.readFileSync(RTLConfFilePath, 'utf-8')); delete config.Settings; - fs.writeFileSync(file_path, ini.stringify(config)); - fs.appendFile(file_path, ini.stringify(req.body.updatedSettings, { section: 'Settings' }), function(err) { + fs.writeFileSync(RTLConfFilePath, ini.stringify(config)); + fs.appendFile(RTLConfFilePath, ini.stringify(req.body.updatedSettings, { section: 'Settings' }), function(err) { if (err) { logger.error('\r\nConf: 28: ' + JSON.stringify(Date.now()) + ': ERROR: Updating UI Settings Failed!'); res.status(500).json({ @@ -54,7 +54,7 @@ exports.getConfig = (req, res, next) => { confFilePath = common.bitcoind_config_path break; case 'rtl': - confFilePath = file_path; + confFilePath = RTLConfFilePath; break; default: confFilePath = ''; diff --git a/controllers/authenticate.js b/controllers/authenticate.js index 6f826269..4b3d5f7a 100644 --- a/controllers/authenticate.js +++ b/controllers/authenticate.js @@ -1,15 +1,15 @@ var ini = require('ini'); -var path = require('path'); var fs = require('fs'); -var rtl_config_path = path.normalize(__dirname + '/..') + '/RTL.conf'; +var common = require('../common'); const jwt = require("jsonwebtoken"); var upperCase = require('upper-case'); var atob = require('atob'); var logger = require('./logger'); exports.authenticateUser = (req, res, next) => { + const RTLConfFilePath = common.rtl_conf_file_path + '/RTL.conf'; password = atob(req.body.password); - fs.readFile(rtl_config_path, 'utf8', function (err, data) { + fs.readFile(RTLConfFilePath, 'utf8', function (err, data) { if (err) { logger.error('\r\nAuthenticate: 13: ' + JSON.stringify(Date.now()) + ': ERROR: RTL Config Reading Failed!'); res.status(500).json({ @@ -17,12 +17,11 @@ exports.authenticateUser = (req, res, next) => { error: err }); } else { - var jsonRTLConfig = ini.parse(data); - const nodeAuthType = jsonRTLConfig.Authentication.nodeAuthType; - const macaroonPath = jsonRTLConfig.Authentication.macroonPath; - const lndConfigPath = (undefined !== jsonRTLConfig.Authentication.lndConfigPath) ? jsonRTLConfig.Authentication.lndConfigPath : ''; + const nodeAuthType = common.node_auth_type; + const macaroonPath = common.macaroon_path; + const lndConfigPath = (undefined !== common.lnd_config_path) ? common.lnd_config_path : ''; if(upperCase(nodeAuthType) === 'CUSTOM') { - const rtlPass = jsonRTLConfig.Authentication.rtlPass; + const rtlPass = ini.parse(data).Authentication.rtlPass; if (rtlPass === password) { var rpcUser = 'Custom_User'; const token = jwt.sign(