diff --git a/common.js b/common.js index fc899bb2..880941fe 100644 --- a/common.js +++ b/common.js @@ -7,6 +7,7 @@ common.lnd_config_path = ''; common.node_auth_type = 'DEFAULT'; common.macaroon_path = ''; common.bitcoind_config_path = ''; +common.rtl_pass = ''; common.enable_logging = false; common.log_file = ''; common.rtl_sso = 0; diff --git a/connect.js b/connect.js index cc759d75..b5f8b190 100644 --- a/connect.js +++ b/connect.js @@ -8,26 +8,43 @@ var logger = require('./controllers/logger'); var options = {}; var defaultConfig = { - Authentication: { - lndServerUrl:'https://localhost:8080/v1', - macaroonPath:'', - nodeAuthType:'DEFAULT', - lndConfigPath:'', - bitcoindConfigPath: '', - rtlPass:'', - enableLogging: false - }, - Settings: { - flgSidenavOpened:true, - flgSidenavPinned:true, - menu:'Vertical', - menuType:'Regular', - theme:'dark-blue', - satsToBTC:false + Authentication: { + macaroonPath: '', + nodeAuthType: 'DEFAULT', + lndConfigPath: '', + rtlPass: '' + }, + Settings: { + flgSidenavOpened: true, + flgSidenavPinned: true, + menu: 'Vertical', + menuType: 'Regular', + theme: 'dark-blue', + satsToBTC: false, + lndServerUrl: 'https://localhost:8080/v1', + bitcoindConfigPath: '', + enableLogging: false, + port: 3000 + }, + SSO: { + rtlSSO: 0, + rtlCookiePath: '', + logoutRedirectLink: '/login' + } +}; + +const normalizePort = val => { + var port = parseInt(val, 10); + if (isNaN(port)) { + return val; } + if (port >= 0) { + return port; + } + return false; }; -var setMacaroonPath = (clArgs, config) => { +const setMacaroonPath = (clArgs, config) => { if(undefined !== clArgs.lndir) { common.macaroon_path = clArgs.lndir; } else if (undefined !== process.env.MACAROON_PATH) { @@ -41,7 +58,7 @@ var setMacaroonPath = (clArgs, config) => { } } -var validateConfigFile = (config) => { +const validateConfigFile = (config) => { if(common.macaroon_path === '' || undefined === common.macaroon_path) { errMsg = 'Please set macaroon path through environment/RTL.conf!'; } @@ -49,10 +66,14 @@ var validateConfigFile = (config) => { 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) { + if((config.Authentication.lndServerUrl === '' || undefined === config.Authentication.lndServerUrl) && (config.Settings.lndServerUrl === '' || undefined === config.Settings.lndServerUrl)) { errMsg = errMsg + '\nPlease set LND Server URL through environment/RTL.conf!'; } else { - common.lnd_server_url = config.Authentication.lndServerUrl; + 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; + } } } @@ -81,58 +102,87 @@ var validateConfigFile = (config) => { if(undefined !== process.env.BITCOIND_CONFIG_PATH) { common.bitcoind_config_path = process.env.BITCOIND_CONFIG_PATH; } else { - if(config.Authentication.bitcoindConfigPath !== '' || undefined !== config.Authentication.bitcoindConfigPath) { + 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(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 !== 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(undefined !== config.Authentication.enableLogging) { - common.enable_logging = config.Authentication.enableLogging; - 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) { - 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, unable to create log file!' + err); - } - } - } + } - if(errMsg !== '') { - throw new Error(errMsg); - } + 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) { + 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: \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); + } } -var setSSOParams = () => { - if(undefined !== process.env.RTL_SSO) { - common.rtl_sso = process.env.RTL_SSO; +const setSSOParams = (config) => { + if (undefined !== process.env.RTL_SSO) { + common.rtl_sso = process.env.RTL_SSO; + } else if (undefined !== config.SSO.rtlSSO) { + common.rtl_sso = config.SSO.rtlSSO; + } - if(undefined !== process.env.LOGOUT_REDIRECT_LINK) { - common.logout_redirect_link = process.env.LOGOUT_REDIRECT_LINK; - } + if (undefined !== process.env.LOGOUT_REDIRECT_LINK) { + common.logout_redirect_link = process.env.LOGOUT_REDIRECT_LINK; + } else if (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 { - common.rtl_cookie_path = common.rtl_conf_file_path + '/cookies/auth.cookie'; - } - + + if (undefined !== process.env.RTL_COOKIE_PATH) { + common.rtl_cookie_path = process.env.RTL_COOKIE_PATH; + } else if (undefined !== config.SSO.rtlCookiePath) { + common.rtl_cookie_path = config.SSO.rtlCookiePath; + } else { + common.rtl_cookie_path = common.rtl_conf_file_path + '/cookies/auth.cookie'; + } + + if (+common.rtl_sso) { readCookie(common.rtl_cookie_path); } }; -var createDirectory = (dirname) => { +const createDirectory = (dirname) => { try { fs.mkdirSync(dirname); } catch (err) { @@ -145,7 +195,7 @@ var createDirectory = (dirname) => { } } -var readCookie = (cookieFile) => { +const readCookie = (cookieFile) => { let exists = fs.existsSync(cookieFile); if (exists) { common.cookie = fs.readFileSync(cookieFile, 'utf-8'); @@ -157,7 +207,7 @@ var readCookie = (cookieFile) => { common.cookie = fs.readFileSync(cookieFile, 'utf-8'); } catch(err) { - console.error('Something went wrong, unable to create cookie file!\n' + err); + console.error('Something went wrong: \n' + err); throw new Error(err); } } @@ -173,7 +223,7 @@ String.random = function (length) { }, '').substring(-length); } -var setOptions = () => { +const setOptions = () => { var macaroon = fs.readFileSync(common.macaroon_path + '/admin.macaroon').toString('hex'); options = { url: '', @@ -186,7 +236,7 @@ var setOptions = () => { }; } -var logEnvVariables = () => { +const logEnvVariables = () => { if (!common.enable_logging) { return; } @@ -203,7 +253,7 @@ var logEnvVariables = () => { } var errMsg = ''; -var configFileExists = () => { +const 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); @@ -212,7 +262,6 @@ var configFileExists = () => { setMacaroonPath(clArgs, config) validateConfigFile(config); setOptions(); - setSSOParams(); logEnvVariables(); } else { try { @@ -221,11 +270,10 @@ var configFileExists = () => { setMacaroonPath(clArgs, config) validateConfigFile(config); setOptions(); - setSSOParams(); logEnvVariables(); } catch(err) { - console.error('Something went wrong, unable to create config file!\n' + err); + console.error('Something went wrong: \n' + err); throw new Error(err); } } diff --git a/controllers/RTLConf.js b/controllers/RTLConf.js index d20ff25c..844d7340 100644 --- a/controllers/RTLConf.js +++ b/controllers/RTLConf.js @@ -3,9 +3,9 @@ var path = require('path'); var fs = require('fs'); var logger = require('./logger'); var common = require('../common'); -var RTLConfFile = common.rtl_conf_file_path + '/RTL.conf'; exports.getRTLConfig = (req, res, next) => { + var RTLConfFile = common.rtl_conf_file_path + '/RTL.conf'; logger.info('\r\nConf: 7: ' + JSON.stringify(Date.now()) + ': INFO: Getting RTL Config'); fs.readFile(RTLConfFile, 'utf8', function(err, data) { if (err) { @@ -29,6 +29,7 @@ exports.getRTLConfig = (req, res, next) => { }; exports.updateUISettings = (req, res, next) => { + var RTLConfFile = common.rtl_conf_file_path + '/RTL.conf'; var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8')); delete config.Settings; fs.writeFileSync(RTLConfFile, ini.stringify(config)); diff --git a/controllers/authenticate.js b/controllers/authenticate.js index 9c1e5f9b..252ff2db 100644 --- a/controllers/authenticate.js +++ b/controllers/authenticate.js @@ -22,64 +22,52 @@ exports.authenticateUser = (req, res, next) => { }); } } else { - const RTLConfFilePath = common.rtl_conf_file_path + '/RTL.conf'; - 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({ - message: "RTL Config Reading Failed!", - error: err - }); + if(upperCase(common.node_auth_type) === 'CUSTOM') { + if (common.rtl_pass === password) { + var rpcUser = 'Custom_User'; + const token = jwt.sign( + { user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path }, + 'default_secret_key' + ); + res.status(200).json({ token: token }); } else { - if(upperCase(common.node_auth_type) === 'CUSTOM') { - const rtlPass = ini.parse(data).Authentication.rtlPass; - if (rtlPass === password) { - var rpcUser = 'Custom_User'; - const token = jwt.sign( - { user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path }, - 'default_secret_key' - ); - res.status(200).json({ token: token }); + res.status(401).json({ + message: "Authentication Failed!", + error: "Password Validation Failed!" + }); + } + } else { + fs.readFile(common.lnd_config_path, 'utf8', function (err, data) { + if (err) { + logger.error('\r\nAuthenticate: 45: ' + JSON.stringify(Date.now()) + ': ERROR: RTL Config Reading Failed!'); + res.status(500).json({ + message: "LND Config Reading Failed!", + error: err + }); + } else { + const jsonLNDConfig = ini.parse(data); + if (undefined !== jsonLNDConfig.Bitcoind && undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcpass']) { + if (jsonLNDConfig.Bitcoind['bitcoind.rpcpass'] === password) { + var rpcUser = (undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcuser']) ? jsonLNDConfig.Bitcoind['bitcoind.rpcuser'] : ''; + const token = jwt.sign( + { user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path }, + 'default_secret_key' + ); + res.status(200).json({ token: token }); + } else { + res.status(401).json({ + message: "Authentication Failed!", + error: "Password Validation Failed!" + }); + } } else { res.status(401).json({ message: "Authentication Failed!", - error: "Password Validation Failed!" + error: "Password Not Found In LND Config!" }); } - } else { - fs.readFile(common.lnd_config_path, 'utf8', function (err, data) { - if (err) { - logger.error('\r\nAuthenticate: 45: ' + JSON.stringify(Date.now()) + ': ERROR: RTL Config Reading Failed!'); - res.status(500).json({ - message: "LND Config Reading Failed!", - error: err - }); - } else { - const jsonLNDConfig = ini.parse(data); - if (undefined !== jsonLNDConfig.Bitcoind && undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcpass']) { - if (jsonLNDConfig.Bitcoind['bitcoind.rpcpass'] === password) { - var rpcUser = (undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcuser']) ? jsonLNDConfig.Bitcoind['bitcoind.rpcuser'] : ''; - const token = jwt.sign( - { user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path }, - 'default_secret_key' - ); - res.status(200).json({ token: token }); - } else { - res.status(401).json({ - message: "Authentication Failed!", - error: "Password Validation Failed!" - }); - } - } else { - res.status(401).json({ - message: "Authentication Failed!", - error: "Password Not Found In LND Config!" - }); - } - } - }); } - } - }) + }); + } } }; diff --git a/rtl.js b/rtl.js index f2cd8532..ead83dc6 100644 --- a/rtl.js +++ b/rtl.js @@ -3,18 +3,6 @@ const common = require("./common"); const debug = require("debug")("node-angular"); const http = require("http"); -const normalizePort = val => { - var port = parseInt(val, 10); - if (isNaN(port)) { - return val; - } - if (port >= 0) { - common.port = port; - return port; - } - return false; -}; - const onError = error => { if (error.syscall !== "listen") { throw error; @@ -40,15 +28,12 @@ const onError = error => { const onListening = () => { const addr = server.address(); - const bind = typeof addr === "string" ? "pipe " + addr : "port " + port; + const bind = typeof addr === "string" ? "pipe " + addr : "port " + common.port; debug("Listening on " + bind); - console.log('Server is up and running, please open the UI at http://localhost:' + port); + console.log('Server is up and running, please open the UI at http://localhost:' + common.port); }; -const port = normalizePort(process.env.PORT || common.port); - const server = http.createServer(app); server.on("error", onError); server.on("listening", onListening); -server.listen(port); - +server.listen(common.port); diff --git a/sample-RTL.conf b/sample-RTL.conf index f6a974f8..e2c9feaf 100644 --- a/sample-RTL.conf +++ b/sample-RTL.conf @@ -1,10 +1,9 @@ [Authentication] -lndServerUrl=https://localhost:8080/v1 -macroonPath= -nodeAuthType= -lndConfigPath= -rtlPass= -enableLogging=false +macaroonPath=C:\Users\suheb\AppData\Local\Lnd\data\chain\bitcoin\testnet +nodeAuthType=CUSTOM +lndConfigPath=C:\Users\Suheb\AppData\Local\Lnd\lnd.conf +rtlPass=xxx + [Settings] flgSidenavOpened=true flgSidenavPinned=true @@ -12,3 +11,12 @@ menu=Vertical menuType=Regular theme=dark-blue satsToBTC=false +bitcoindConfigPath= +enableLogging=true +port=3000 +lndServerUrl=https://192.168.1.8:8080/v1 + +[SSO] +rtlSSO=0 +rtlCookiePath= +logoutRedirectLink=/login