2018-09-15 03:31:01 +02:00
|
|
|
var fs = require('fs');
|
2019-02-24 14:16:41 +01:00
|
|
|
var crypto = require('crypto');
|
2018-09-15 03:31:01 +02:00
|
|
|
var clArgs = require('optimist').argv;
|
2019-01-01 17:26:51 +01:00
|
|
|
var ini = require('ini');
|
|
|
|
var common = require('./common');
|
2019-01-01 21:47:32 +01:00
|
|
|
var path = require('path');
|
2019-02-12 14:36:04 +01:00
|
|
|
var upperCase = require('upper-case');
|
2019-02-14 03:31:26 +01:00
|
|
|
var logger = require('./controllers/logger');
|
2019-03-02 18:55:19 +01:00
|
|
|
var connect = {};
|
2019-01-01 17:26:51 +01:00
|
|
|
|
|
|
|
var defaultConfig = {
|
2019-02-16 23:43:12 +01:00
|
|
|
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,
|
2019-02-22 04:20:07 +01:00
|
|
|
port: 3000
|
2019-02-16 23:43:12 +01:00
|
|
|
},
|
|
|
|
SSO: {
|
|
|
|
rtlSSO: 0,
|
|
|
|
rtlCookiePath: '',
|
|
|
|
logoutRedirectLink: '/login'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const normalizePort = val => {
|
|
|
|
var port = parseInt(val, 10);
|
|
|
|
if (isNaN(port)) {
|
|
|
|
return val;
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
2019-02-16 23:43:12 +01:00
|
|
|
if (port >= 0) {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
return false;
|
2019-01-01 17:26:51 +01:00
|
|
|
};
|
2018-09-15 03:31:01 +02:00
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const setMacaroonPath = (clArgs, config) => {
|
2019-01-01 17:26:51 +01:00
|
|
|
if(undefined !== clArgs.lndir) {
|
2019-02-09 21:37:36 +01:00
|
|
|
common.macaroon_path = clArgs.lndir;
|
2019-02-12 14:36:04 +01:00
|
|
|
} else if (undefined !== process.env.MACAROON_PATH) {
|
|
|
|
common.macaroon_path = process.env.MACAROON_PATH;
|
2019-01-01 17:26:51 +01:00
|
|
|
} else {
|
2019-01-19 04:33:50 +01:00
|
|
|
if(undefined !== config.Authentication.macroonPath && config.Authentication.macroonPath !== '') {
|
2019-02-09 21:37:36 +01:00
|
|
|
common.macaroon_path = config.Authentication.macroonPath;
|
2019-01-19 04:33:50 +01:00
|
|
|
} else if(undefined !== config.Authentication.macaroonPath && config.Authentication.macaroonPath !== '') {
|
2019-02-09 21:37:36 +01:00
|
|
|
common.macaroon_path = config.Authentication.macaroonPath;
|
2019-01-19 04:04:16 +01:00
|
|
|
}
|
2018-09-15 03:31:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const validateConfigFile = (config) => {
|
2019-02-09 21:37:36 +01:00
|
|
|
if(common.macaroon_path === '' || undefined === common.macaroon_path) {
|
|
|
|
errMsg = 'Please set macaroon path through environment/RTL.conf!';
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:36:04 +01:00
|
|
|
if(undefined !== process.env.LND_SERVER_URL) {
|
|
|
|
common.lnd_server_url = process.env.LND_SERVER_URL;
|
2019-01-01 17:26:51 +01:00
|
|
|
} else {
|
2019-02-16 23:43:12 +01:00
|
|
|
if((config.Authentication.lndServerUrl === '' || undefined === config.Authentication.lndServerUrl) && (config.Settings.lndServerUrl === '' || undefined === config.Settings.lndServerUrl)) {
|
2019-02-09 21:37:36 +01:00
|
|
|
errMsg = errMsg + '\nPlease set LND Server URL through environment/RTL.conf!';
|
|
|
|
} else {
|
2019-02-16 23:43:12 +01:00
|
|
|
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;
|
|
|
|
}
|
2019-02-09 21:37:36 +01:00
|
|
|
}
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
2019-02-09 21:37:36 +01:00
|
|
|
|
2019-02-12 14:36:04 +01:00
|
|
|
if(undefined !== process.env.NODE_AUTH_TYPE) {
|
|
|
|
common.node_auth_type = process.env.NODE_AUTH_TYPE;
|
2019-02-09 21:37:36 +01:00
|
|
|
} 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;
|
|
|
|
}
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
2019-02-09 21:37:36 +01:00
|
|
|
|
2019-02-12 14:36:04 +01:00
|
|
|
if(undefined !== process.env.LND_CONFIG_PATH) {
|
|
|
|
common.lnd_config_path = process.env.LND_CONFIG_PATH;
|
2019-01-19 04:04:16 +01:00
|
|
|
} else {
|
2019-02-09 21:37:36 +01:00
|
|
|
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!';
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 04:04:16 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 14:36:04 +01:00
|
|
|
if(undefined !== process.env.BITCOIND_CONFIG_PATH) {
|
|
|
|
common.bitcoind_config_path = process.env.BITCOIND_CONFIG_PATH;
|
2019-02-09 21:37:36 +01:00
|
|
|
} else {
|
2019-02-16 23:43:12 +01:00
|
|
|
if(config.Settings.bitcoindConfigPath !== '' || undefined !== config.Settings.bitcoindConfigPath) {
|
|
|
|
common.bitcoind_config_path = config.Settings.bitcoindConfigPath;
|
|
|
|
} else if(config.Authentication.bitcoindConfigPath !== '' || undefined !== config.Authentication.bitcoindConfigPath) {
|
2019-02-09 21:37:36 +01:00
|
|
|
common.bitcoind_config_path = config.Authentication.bitcoindConfigPath;
|
|
|
|
}
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
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;
|
2019-01-01 17:26:51 +01:00
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
}
|
2019-01-13 23:55:25 +01:00
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 01:31:38 +01:00
|
|
|
|
|
|
|
if (undefined !== process.env.PORT) {
|
2019-02-16 23:43:12 +01:00
|
|
|
common.port = normalizePort(process.env.PORT);
|
|
|
|
} else if (undefined !== config.Settings.port) {
|
|
|
|
common.port = normalizePort(config.Settings.port);
|
|
|
|
}
|
2019-02-21 01:31:38 +01:00
|
|
|
|
|
|
|
setSSOParams(config);
|
2019-02-16 23:43:12 +01:00
|
|
|
if (errMsg !== '') {
|
|
|
|
throw new Error(errMsg);
|
|
|
|
}
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const setSSOParams = (config) => {
|
|
|
|
if (undefined !== process.env.RTL_SSO) {
|
|
|
|
common.rtl_sso = process.env.RTL_SSO;
|
2019-02-17 03:07:18 +01:00
|
|
|
} else if (undefined !== config.SSO && undefined !== config.SSO.rtlSSO) {
|
2019-02-16 23:43:12 +01:00
|
|
|
common.rtl_sso = config.SSO.rtlSSO;
|
|
|
|
}
|
2019-02-12 14:36:04 +01:00
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
if (+common.rtl_sso) {
|
2019-02-26 03:16:55 +01:00
|
|
|
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';
|
|
|
|
}
|
2019-02-13 00:13:09 +01:00
|
|
|
readCookie(common.rtl_cookie_path);
|
2019-02-12 14:36:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const createDirectory = (dirname) => {
|
2019-02-12 14:36:04 +01:00
|
|
|
try {
|
|
|
|
fs.mkdirSync(dirname);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'EEXIST') {
|
|
|
|
return dirname;
|
|
|
|
}
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
throw new Error(`EACCES: permission denied, mkdir '${dirname}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const readCookie = (cookieFile) => {
|
2019-02-12 14:36:04 +01:00
|
|
|
let exists = fs.existsSync(cookieFile);
|
|
|
|
if (exists) {
|
|
|
|
common.cookie = fs.readFileSync(cookieFile, 'utf-8');
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
var dirname = path.dirname(cookieFile);
|
|
|
|
createDirectory(dirname);
|
2019-02-24 14:16:41 +01:00
|
|
|
fs.writeFileSync(cookieFile, crypto.randomBytes(64).toString('hex'));
|
2019-02-12 14:36:04 +01:00
|
|
|
common.cookie = fs.readFileSync(cookieFile, 'utf-8');
|
|
|
|
}
|
|
|
|
catch(err) {
|
2019-02-16 23:43:12 +01:00
|
|
|
console.error('Something went wrong: \n' + err);
|
2019-02-12 14:36:04 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:43:12 +01:00
|
|
|
const logEnvVariables = () => {
|
2019-02-14 03:31:26 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-01 17:26:51 +01:00
|
|
|
var errMsg = '';
|
2019-03-02 18:55:19 +01:00
|
|
|
connect.configFileExists = () => {
|
2019-02-13 00:13:09 +01:00
|
|
|
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);
|
2019-02-09 21:37:36 +01:00
|
|
|
RTLConfFile = common.rtl_conf_file_path + '/RTL.conf';
|
|
|
|
let exists = fs.existsSync(RTLConfFile);
|
2019-01-01 17:26:51 +01:00
|
|
|
if (exists) {
|
2019-02-09 21:37:36 +01:00
|
|
|
var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8'));
|
2019-01-01 17:26:51 +01:00
|
|
|
setMacaroonPath(clArgs, config)
|
2019-02-09 21:37:36 +01:00
|
|
|
validateConfigFile(config);
|
2019-02-14 03:31:26 +01:00
|
|
|
logEnvVariables();
|
2019-01-01 17:26:51 +01:00
|
|
|
} else {
|
|
|
|
try {
|
2019-02-09 21:37:36 +01:00
|
|
|
fs.writeFileSync(RTLConfFile, ini.stringify(defaultConfig));
|
|
|
|
var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8'));
|
2019-01-01 17:26:51 +01:00
|
|
|
setMacaroonPath(clArgs, config)
|
2019-02-09 21:37:36 +01:00
|
|
|
validateConfigFile(config);
|
2019-02-14 03:31:26 +01:00
|
|
|
logEnvVariables();
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2019-02-16 23:43:12 +01:00
|
|
|
console.error('Something went wrong: \n' + err);
|
2019-02-09 21:37:36 +01:00
|
|
|
throw new Error(err);
|
2019-01-01 17:26:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 18:55:19 +01:00
|
|
|
|
|
|
|
module.exports = connect.configFileExists();
|