2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-15 18:13:00 +00:00
RTL/connect.js

530 lines
22 KiB
JavaScript
Raw Normal View History

2019-04-06 02:52:00 +00:00
var os = require('os');
2018-09-15 01:31:01 +00:00
var fs = require('fs');
2019-03-31 15:39:41 +00:00
var platform = require('os').platform();
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
2018-09-15 01:31:01 +00:00
var clArgs = require('optimist').argv;
2019-01-01 16:26:51 +00:00
var ini = require('ini');
var common = require('./common');
var path = require('path');
2019-02-12 13:36:04 +00:00
var upperCase = require('upper-case');
var logger = require('./controllers/logger');
var connect = {};
2019-04-06 02:52:00 +00:00
var errMsg = '';
var request = require('request');
2019-01-01 16:26:51 +00:00
2019-04-06 02:52:00 +00:00
connect.setDefaultConfig = () => {
var homeDir = os.userInfo().homedir;
2019-03-17 21:53:25 +00:00
var macaroonPath = '';
var lndConfigPath = '';
2019-03-31 15:39:41 +00:00
switch (platform) {
case 'win32':
2019-04-06 02:52:00 +00:00
macaroonPath = homeDir + '\\AppData\\Local\\Lnd\\data\\chain\\bitcoin\\mainnet';
lndConfigPath = homeDir + '\\AppData\\Local\\Lnd\\lnd.conf';
2019-03-31 15:39:41 +00:00
break;
case 'darwin':
2019-04-06 02:52:00 +00:00
macaroonPath = homeDir + '/Library/Application Support/Lnd/data/chain/bitcoin/mainnet';
lndConfigPath = homeDir + '/Library/Application Support/Lnd/lnd.conf';
2019-03-31 15:39:41 +00:00
break;
case 'linux':
2019-04-06 02:52:00 +00:00
macaroonPath = homeDir + '/.lnd/data/chain/bitcoin/mainnet';
lndConfigPath = homeDir + '/.lnd/lnd.conf';
2019-03-31 15:39:41 +00:00
break;
default:
macaroonPath = '';
lndConfigPath = '';
break;
2019-04-06 02:52:00 +00:00
}
2019-03-17 21:53:25 +00:00
return {
Authentication: {
macaroonPath: macaroonPath,
nodeAuthType: 'DEFAULT',
lndConfigPath: lndConfigPath,
rtlPass: ''
},
Settings: {
flgSidenavOpened: true,
flgSidenavPinned: true,
menu: 'Vertical',
menuType: 'Regular',
theme: 'dark-blue',
satsToBTC: false,
channelBackupPath: homeDir + '/backup/node-0',
2019-03-17 21:53:25 +00:00
lndServerUrl: 'https://localhost:8080/v1',
enableLogging: false,
port: 3000
},
SSO: {
rtlSSO: 0,
rtlCookiePath: '',
logoutRedirectLink: '/login'
}
};
}
2019-02-16 22:43:12 +00:00
2019-04-06 02:52:00 +00:00
connect.normalizePort = val => {
2019-02-16 22:43:12 +00:00
var port = parseInt(val, 10);
if (isNaN(port)) {
return val;
2019-01-01 16:26:51 +00:00
}
2019-02-16 22:43:12 +00:00
if (port >= 0) {
return port;
}
return false;
2019-01-01 16:26:51 +00:00
};
2018-09-15 01:31:01 +00:00
2019-04-06 02:52:00 +00:00
connect.setMacaroonPath = (clArgs, config) => {
common.nodes[0] = {};
common.nodes[0].index = 1;
2019-01-01 16:26:51 +00:00
if(undefined !== clArgs.lndir) {
common.nodes[0].macaroon_path = clArgs.lndir;
2019-02-12 13:36:04 +00:00
} else if (undefined !== process.env.MACAROON_PATH) {
common.nodes[0].macaroon_path = process.env.MACAROON_PATH;
2019-01-01 16:26:51 +00:00
} else {
2019-01-19 03:33:50 +00:00
if(undefined !== config.Authentication.macroonPath && config.Authentication.macroonPath !== '') {
common.nodes[0].macaroon_path = config.Authentication.macroonPath;
2019-01-19 03:33:50 +00:00
} else if(undefined !== config.Authentication.macaroonPath && config.Authentication.macaroonPath !== '') {
common.nodes[0].macaroon_path = config.Authentication.macaroonPath;
}
2018-09-15 01:31:01 +00:00
}
}
connect.convertCustomToHash = (nodeSetupType) => {
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);
if(nodeSetupType === 'SINGLE') {
try {
RTLConfFile = common.rtl_conf_file_path + '/RTL.conf';
var config = ini.parse(fs.readFileSync(RTLConfFile, 'utf-8'));
const authTemp = config.Authentication;
authTemp.rtlPassHashed = hash.update(authTemp.rtlPass).digest('hex');
delete authTemp.rtlPass;
delete config.Authentication;
fs.writeFileSync(RTLConfFile, ini.stringify(config));
fs.appendFileSync(RTLConfFile, ini.stringify(authTemp, { section: 'Authentication' }));
console.log('Please note that RTL has encrypted the plaintext password into its corresponding hash.');
return authTemp.rtlPassHashed;
} catch (err) {
errMsg = errMsg + '\nrtlPass hash conversion failed!';
}
}
if(nodeSetupType === 'MULTI') {
try {
RTLConfFile = common.rtl_conf_file_path + '/RTL-Multi-Node-Conf.json';
var config = JSON.parse(fs.readFileSync(RTLConfFile, 'utf-8'));
config.multiPassHashed = hash.update(config.multiPass).digest('hex');
delete config.multiPass;
fs.writeFileSync(RTLConfFile, JSON.stringify(config, null, 2), 'utf-8');
console.log('Please note that RTL has encrypted the plaintext password into its corresponding hash.');
return config.multiPassHashed;
} catch (err) {
errMsg = errMsg + '\nmultiPass hash conversion failed!';
}
}
}
2019-04-06 02:52:00 +00:00
connect.validateSingleNodeConfig = (config) => {
if(common.nodes[0].macaroon_path === '' || undefined === common.nodes[0].macaroon_path) {
errMsg = 'Please set macaroon path through environment or RTL.conf!';
2019-01-01 16:26:51 +00:00
}
2019-02-12 13:36:04 +00:00
if(undefined !== process.env.LND_SERVER_URL) {
common.nodes[0].lnd_server_url = process.env.LND_SERVER_URL;
2019-01-01 16:26:51 +00:00
} else {
2019-02-16 22:43:12 +00:00
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!';
2019-02-09 20:37:36 +00:00
} else {
2019-02-16 22:43:12 +00:00
if (config.Settings.lndServerUrl !== '' && undefined !== config.Settings.lndServerUrl) {
common.nodes[0].lnd_server_url = config.Settings.lndServerUrl;
2019-02-16 22:43:12 +00:00
} else if (config.Authentication.lndServerUrl !== '' && undefined !== config.Authentication.lndServerUrl) {
common.nodes[0].lnd_server_url = config.Authentication.lndServerUrl;
2019-02-16 22:43:12 +00:00
}
2019-02-09 20:37:36 +00:00
}
2019-01-01 16:26:51 +00:00
}
2019-02-09 20:37:36 +00:00
2019-02-12 13:36:04 +00:00
if(undefined !== process.env.NODE_AUTH_TYPE) {
common.node_auth_type = process.env.NODE_AUTH_TYPE;
2019-02-09 20:37:36 +00:00
} else {
if(config.Authentication.nodeAuthType === '' || undefined === config.Authentication.nodeAuthType) {
errMsg = errMsg + '\nPlease set Node Auth Type through environment or RTL.conf!';
2019-02-09 20:37:36 +00:00
} else {
common.node_auth_type = config.Authentication.nodeAuthType;
}
2019-01-01 16:26:51 +00:00
}
2019-02-09 20:37:36 +00:00
2019-02-12 13:36:04 +00:00
if(undefined !== process.env.LND_CONFIG_PATH) {
common.nodes[0].lnd_config_path = process.env.LND_CONFIG_PATH;
} else {
2019-02-09 20:37:36 +00:00
if(config.Authentication.lndConfigPath !== '' && undefined !== config.Authentication.lndConfigPath) {
common.nodes[0].lnd_config_path = config.Authentication.lndConfigPath;
2019-02-09 20:37:36 +00:00
} 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!';
2019-02-09 20:37:36 +00:00
}
}
}
2019-02-12 13:36:04 +00:00
if(undefined !== process.env.BITCOIND_CONFIG_PATH) {
common.nodes[0].bitcoind_config_path = process.env.BITCOIND_CONFIG_PATH;
2019-02-09 20:37:36 +00:00
} else {
if(config.Settings.bitcoindConfigPath !== '' && undefined !== config.Settings.bitcoindConfigPath) {
common.nodes[0].bitcoind_config_path = config.Settings.bitcoindConfigPath;
} else if(config.Authentication.bitcoindConfigPath !== '' && undefined !== config.Authentication.bitcoindConfigPath) {
common.nodes[0].bitcoind_config_path = config.Authentication.bitcoindConfigPath;
2019-02-09 20:37:36 +00:00
}
2019-01-01 16:26:51 +00:00
}
if(undefined !== process.env.CHANNEL_BACKUP_PATH) {
common.nodes[0].channel_backup_path = process.env.CHANNEL_BACKUP_PATH;
} else {
if(config.Settings.channelBackupPath !== '' && undefined !== config.Settings.channelBackupPath) {
common.nodes[0].channel_backup_path = config.Settings.channelBackupPath;
} else {
common.nodes[0].channel_backup_path = common.rtl_conf_file_path + '/backup';
}
try {
connect.createDirectory(common.nodes[0].channel_backup_path);
let exists = fs.existsSync(common.nodes[0].channel_backup_path + '/channel-all.bak');
if (!exists) {
try {
var createStream = fs.createWriteStream(common.nodes[0].channel_backup_path + '/channel-all.bak');
createStream.end();
} catch (err) {
console.error('Something went wrong while creating backup file: \n' + err);
}
}
} catch (err) {
console.error('Something went wrong while creating backup file: \n' + err);
}
}
if (undefined !== process.env.RTL_PASS) {
2019-02-16 22:43:12 +00:00
common.rtl_pass = process.env.RTL_PASS;
} else if (config.Authentication.rtlPassHashed !== '' && undefined !== config.Authentication.rtlPassHashed) {
common.rtl_pass = config.Authentication.rtlPassHashed;
} else if (config.Authentication.rtlPass !== '' && undefined !== config.Authentication.rtlPass) {
common.rtl_pass = connect.convertCustomToHash('SINGLE');
2019-02-16 22:43:12 +00:00
}
2019-01-13 22:55:25 +00:00
2019-02-16 22:43:12 +00: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 or RTL.conf';
2019-02-16 22:43:12 +00:00
}
if (undefined !== process.env.ENABLE_LOGGING) {
common.nodes[0].enable_logging = process.env.ENABLE_LOGGING;
2019-02-16 22:43:12 +00:00
} else if (undefined !== config.Settings.enableLogging) {
common.nodes[0].enable_logging = config.Settings.enableLogging;
2019-02-16 22:43:12 +00:00
} else if (undefined !== config.Authentication.enableLogging) {
common.nodes[0].enable_logging = config.Authentication.enableLogging;
2019-04-06 02:52:00 +00:00
}
if (common.nodes[0].enable_logging) {
common.nodes[0].log_file = common.rtl_conf_file_path + '/logs/RTL.log';
let exists = fs.existsSync(common.nodes[0].log_file);
2019-02-16 22:43:12 +00:00
if (exists) {
fs.writeFile(common.nodes[0].log_file, '', () => { });
2019-04-06 02:52:00 +00:00
} else {
2019-02-16 22:43:12 +00:00
try {
var dirname = path.dirname(common.nodes[0].log_file);
2019-04-06 02:52:00 +00:00
connect.createDirectory(dirname);
var createStream = fs.createWriteStream(common.nodes[0].log_file);
2019-02-16 22:43:12 +00:00
createStream.end();
}
catch (err) {
2019-03-31 15:39:41 +00:00
console.error('Something went wrong while creating log file: \n' + err);
2019-02-16 22:43:12 +00:00
}
}
}
2019-02-21 00:31:38 +00:00
if (undefined !== process.env.PORT) {
2019-04-06 02:52:00 +00:00
common.port = connect.normalizePort(process.env.PORT);
2019-02-16 22:43:12 +00:00
} else if (undefined !== config.Settings.port) {
2019-04-06 02:52:00 +00:00
common.port = connect.normalizePort(config.Settings.port);
2019-02-16 22:43:12 +00:00
}
2019-02-21 00:31:38 +00:00
2019-04-06 02:52:00 +00:00
connect.setSSOParams(config);
2019-02-16 22:43:12 +00:00
if (errMsg !== '') {
throw new Error(errMsg);
}
2019-01-01 16:26:51 +00:00
}
2019-04-06 02:52:00 +00:00
connect.validateMultiNodeConfig = (config) => {
common.node_auth_type = 'CUSTOM';
if (config.multiPassHashed !== '' && undefined !== config.multiPassHashed) {
common.rtl_pass = config.multiPassHashed;
} else if (config.multiPass !== '' && undefined !== config.multiPass) {
common.rtl_pass = connect.convertCustomToHash('MULTI');
} else {
errMsg = errMsg + '\nMulti Node Authentication can be set with multiPass only. Please set MultiPass in RTL-Multi-Node-Conf.json';
}
common.port = (undefined !== config.port) ? connect.normalizePort(config.port) : 3000;
2019-04-06 02:52:00 +00:00
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].channel_backup_path = (undefined !== node.Settings.channelBackupPath) ? node.Settings.channelBackupPath : common.rtl_conf_file_path + '/backup/node-' + node.index;
try {
connect.createDirectory(common.nodes[idx].channel_backup_path);
let exists = fs.existsSync(common.nodes[idx].channel_backup_path + '/channel-all.bak');
if (!exists) {
try {
var createStream = fs.createWriteStream(common.nodes[idx].channel_backup_path + '/channel-all.bak');
createStream.end();
} catch (err) {
console.error('Something went wrong while creating backup file: \n' + err);
}
}
} catch (err) {
console.error('Something went wrong while creating backup file: \n' + err);
}
2019-04-06 02:52:00 +00:00
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) => {
2019-02-16 22:43:12 +00:00
if (undefined !== process.env.RTL_SSO) {
common.rtl_sso = process.env.RTL_SSO;
2019-02-17 02:07:18 +00:00
} else if (undefined !== config.SSO && undefined !== config.SSO.rtlSSO) {
2019-02-16 22:43:12 +00:00
common.rtl_sso = config.SSO.rtlSSO;
}
2019-02-12 13:36:04 +00:00
2019-02-16 22:43:12 +00:00
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';
}
2019-04-06 02:52:00 +00:00
connect.readCookie(common.rtl_cookie_path);
2019-02-12 13:36:04 +00:00
}
};
2019-04-06 02:52:00 +00:00
connect.createDirectory = (dirname) => {
2019-02-12 13:36:04 +00:00
try {
const sep = path.sep;
const initDir = path.isAbsolute(dirname) ? sep : '';
dirname.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir);
if (!fs.existsSync(curDir)) {
fs.mkdirSync(curDir);
}
return curDir;
}, initDir);
2019-02-12 13:36:04 +00:00
} catch (err) {
if (err.code === 'EEXIST') {
return dirname;
}
if (err.code === 'ENOENT') {
throw new Error(`EACCES: permission denied, mkdir '${dirname}'`);
}
}
}
2019-04-06 02:52:00 +00:00
connect.readCookie = (cookieFile) => {
2019-02-12 13:36:04 +00:00
let exists = fs.existsSync(cookieFile);
if (exists) {
common.cookie = fs.readFileSync(cookieFile, 'utf-8');
} else {
try {
var dirname = path.dirname(cookieFile);
2019-04-06 02:52:00 +00:00
connect.createDirectory(dirname);
fs.writeFileSync(cookieFile, crypto.randomBytes(64).toString('hex'));
2019-02-12 13:36:04 +00:00
common.cookie = fs.readFileSync(cookieFile, 'utf-8');
}
catch(err) {
2019-04-06 02:52:00 +00:00
console.error('Something went wrong while reading cookie: \n' + err);
2019-02-12 13:36:04 +00:00
throw new Error(err);
}
}
}
2019-04-06 02:52:00 +00:00
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);
}
2019-02-12 13:36:04 +00:00
}
2019-04-06 02:52:00 +00:00
connect.logEnvVariables = () => {
if (common.multi_node_setup) {
common.nodes.forEach((node, idx) => {
if (!node.enable_logging) { return; }
logger.info('\r\nConfig Setup Variable DEFAULT_NODE_INDEX: ' + common.selectedNode.index);
2019-04-06 02:52:00 +00:00
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: ' + common.port, node);
2019-04-06 02:52:00 +00:00
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);
logger.info('\r\nConfig Setup Variable CHANNEL_BACKUP_PATH: ' + node.channel_backup_path, node);
2019-04-06 02:52:00 +00:00
});
} else {
if (!common.nodes[0].enable_logging) { return; }
2019-04-06 02:52:00 +00:00
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.nodes[0].lnd_server_url);
logger.info('\r\nConfig Setup Variable MACAROON_PATH: ' + common.nodes[0].macaroon_path);
2019-04-06 02:52:00 +00:00
logger.info('\r\nConfig Setup Variable NODE_AUTH_TYPE: ' + common.node_auth_type);
logger.info('\r\nConfig Setup Variable LND_CONFIG_PATH: ' + common.nodes[0].lnd_config_path);
2019-04-06 02:52:00 +00:00
logger.info('\r\nConfig Setup Variable RTL_CONFIG_PATH: ' + common.rtl_conf_file_path);
logger.info('\r\nConfig Setup Variable BITCOIND_CONFIG_PATH: ' + common.nodes[0].bitcoind_config_path);
logger.info('\r\nConfig Setup Variable CHANNEL_BACKUP_PATH: ' + common.nodes[0].channel_backup_path);
2019-04-06 02:52:00 +00:00
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);
}
}
connect.getAllNodeAllChannelBackup = (node) => {
let channel_backup_file = node.channel_backup_path + '/channel-all.bak';
let options = {
url: node.lnd_server_url + '/channels/backup',
rejectUnauthorized: false,
json: true,
headers: {'Grpc-Metadata-macaroon': fs.readFileSync(node.macaroon_path + '/admin.macaroon').toString('hex')}
};
request(options, function (err, res, body) {
if (err) {
logger.error('\r\nConnect: ' + new Date().toJSON().slice(0,19) + ': ERROR: Channel Backup Response Failed: ' + JSON.stringify(err));
} else {
fs.writeFile(channel_backup_file, JSON.stringify(body), function(err) {
if (err) {
if (node.ln_node) {
logger.error('\r\nConnect: ' + new Date().toJSON().slice(0,19) + ': ERROR: Channel Backup Failed for Node ' + node.ln_node + ' with error response: ' + JSON.stringify(err));
} else {
logger.error('\r\nConnect: ' + new Date().toJSON().slice(0,19) + ': ERROR: Channel Backup Failed: ' + JSON.stringify(err));
}
} else {
if (node.ln_node) {
logger.info('\r\nConnect: ' + new Date().toJSON().slice(0,19) + ': INFO: Channel Backup Successful for Node: ' + node.ln_node);
} else {
logger.info('\r\nConnect: ' + new Date().toJSON().slice(0,19) + ': INFO: Channel Backup Successful');
}
}
});
}
})
};
2019-04-06 02:52:00 +00:00
connect.setSingleNodeConfiguration = (singleNodeFilePath) => {
const exists = fs.existsSync(singleNodeFilePath);
2019-01-01 16:26:51 +00:00
if (exists) {
2019-04-06 02:52:00 +00:00
var config = ini.parse(fs.readFileSync(singleNodeFilePath, 'utf-8'));
connect.setMacaroonPath(clArgs, config);
connect.validateSingleNodeConfig(config);
connect.setSelectedNode(config);
2019-04-06 02:52:00 +00:00
connect.logEnvVariables();
2019-01-01 16:26:51 +00:00
} else {
try {
2019-04-06 02:52:00 +00:00
fs.writeFileSync(singleNodeFilePath, ini.stringify(connect.setDefaultConfig()));
var config = ini.parse(fs.readFileSync(singleNodeFilePath, 'utf-8'));
connect.setMacaroonPath(clArgs, config);
connect.validateSingleNodeConfig(config);
connect.setSelectedNode(config);
2019-04-06 02:52:00 +00:00
connect.logEnvVariables();
2019-01-01 16:26:51 +00:00
}
catch(err) {
2019-04-06 02:52:00 +00:00
console.error('Something went wrong while configuring the single node server: \n' + err);
2019-02-09 20:37:36 +00:00
throw new Error(err);
2019-01-01 16:26:51 +00:00
}
}
}
2019-04-06 02:52:00 +00:00
connect.setMultiNodeConfiguration = (multiNodeFilePath) => {
try {
var config = JSON.parse(fs.readFileSync(multiNodeFilePath, 'utf-8'));
connect.validateMultiNodeConfig(config);
connect.setSelectedNode(config);
2019-04-06 02:52:00 +00:00
connect.logEnvVariables();
}
catch(err) {
console.error('Something went wrong while configuring the multi node server: \n' + err);
throw new Error(err);
}
2019-03-31 15:39:41 +00:00
}
connect.setSelectedNode = (config) => {
if(undefined !== process.env.DEFAULT_NODE_INDEX) {
common.selectedNode = common.findNode(process.env.DEFAULT_NODE_INDEX);
} else {
if(undefined !== config.defaultNodeIndex) {
common.selectedNode = common.findNode(config.defaultNodeIndex);
} else {
common.selectedNode = common.findNode(common.nodes[0].index);
}
}
}
2019-04-06 02:52:00 +00:00
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);
}
common.nodes.map(node => { connect.getAllNodeAllChannelBackup(node); });
2019-04-06 02:52:00 +00:00
}
2019-03-31 15:39:41 +00:00
module.exports = connect;