2019-03-02 12:55:19 -05:00
|
|
|
var fs = require('fs');
|
2019-02-24 22:16:41 +09:00
|
|
|
var crypto = require('crypto');
|
2019-09-16 21:17:28 -04:00
|
|
|
var path = require('path');
|
2020-07-07 13:57:15 -04:00
|
|
|
var ini = require('ini');
|
2018-10-09 20:08:05 -04:00
|
|
|
var common = {};
|
|
|
|
|
2019-02-09 15:37:36 -05:00
|
|
|
common.rtl_conf_file_path = '';
|
2019-02-16 17:43:12 -05:00
|
|
|
common.rtl_pass = '';
|
2020-03-18 21:48:53 -04:00
|
|
|
common.rtl_secret2fa = '';
|
2019-02-12 08:36:04 -05:00
|
|
|
common.rtl_sso = 0;
|
2019-04-06 18:42:09 -04:00
|
|
|
common.port = 3000;
|
2020-05-09 13:15:10 -04:00
|
|
|
common.host = null;
|
2019-02-12 18:13:09 -05:00
|
|
|
common.rtl_cookie_path = '';
|
2019-02-12 08:36:04 -05:00
|
|
|
common.logout_redirect_link = '/login';
|
|
|
|
common.cookie = '';
|
2019-02-24 12:00:39 -05:00
|
|
|
common.secret_key = crypto.randomBytes(64).toString('hex');
|
2019-04-05 22:52:00 -04:00
|
|
|
common.nodes = [];
|
2019-04-06 20:20:40 -04:00
|
|
|
common.selectedNode = {};
|
2019-03-02 12:55:19 -05:00
|
|
|
|
2020-03-16 11:57:57 -04:00
|
|
|
common.getSelSwapServerUrl = () => {
|
|
|
|
return common.selectedNode.swap_server_url;
|
|
|
|
};
|
|
|
|
|
2019-09-02 00:11:37 -04:00
|
|
|
common.getSelLNServerUrl = () => {
|
2019-09-07 17:31:32 -04:00
|
|
|
return common.selectedNode.ln_server_url;
|
2019-04-06 20:20:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
common.getOptions = () => {
|
2020-07-07 13:57:15 -04:00
|
|
|
common.selectedNode.options.method = common.selectedNode.ln_implementation.toUpperCase() !== 'ECL' ? 'GET' : 'POST';
|
2020-02-03 12:09:13 -05:00
|
|
|
common.selectedNode.options.qs = {};
|
2019-09-07 17:31:32 -04:00
|
|
|
return common.selectedNode.options;
|
2019-04-05 22:52:00 -04:00
|
|
|
};
|
|
|
|
|
2019-11-06 16:37:04 -05:00
|
|
|
common.updateSelectedNodeOptions = () => {
|
2019-12-11 19:58:20 -05:00
|
|
|
if (!common.selectedNode) {
|
|
|
|
common.selectedNode = {};
|
|
|
|
}
|
2019-11-06 16:37:04 -05:00
|
|
|
common.selectedNode.options = {
|
|
|
|
url: '',
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
json: true,
|
|
|
|
form: ''
|
|
|
|
};
|
|
|
|
try {
|
2020-07-07 13:57:15 -04:00
|
|
|
if (common.selectedNode && common.selectedNode.ln_implementation) {
|
|
|
|
switch (common.selectedNode.ln_implementation.toUpperCase()) {
|
|
|
|
case 'CLT':
|
|
|
|
common.selectedNode.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(path.join(common.selectedNode.macaroon_path, 'access.macaroon'))).toString("base64") };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ECL':
|
2020-07-12 15:47:42 -04:00
|
|
|
common.selectedNode.options.headers = { 'authorization': 'Basic ' + Buffer.from(':' + common.selectedNode.ln_api_password).toString('base64') };
|
2020-07-07 13:57:15 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
common.selectedNode.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(path.join(common.selectedNode.macaroon_path, 'admin.macaroon')).toString('hex') };
|
|
|
|
break;
|
|
|
|
}
|
2019-11-06 16:37:04 -05:00
|
|
|
}
|
|
|
|
return { status: 200, message: 'Updated Successfully!' };
|
|
|
|
} catch(err) {
|
|
|
|
common.selectedNode.options = {
|
|
|
|
url: '',
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
json: true,
|
|
|
|
form: ''
|
|
|
|
};
|
|
|
|
console.error('Common Update Selected Node Options Error:' + JSON.stringify(err));
|
|
|
|
return { status: 502, message: err };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 18:42:09 -04:00
|
|
|
common.setOptions = () => {
|
2020-03-10 13:25:52 -04:00
|
|
|
if ( common.nodes[0].options && common.nodes[0].options.headers) { return; }
|
2019-11-05 19:26:40 -05:00
|
|
|
if (common.nodes && common.nodes.length > 0) {
|
|
|
|
common.nodes.forEach(node => {
|
|
|
|
node.options = {
|
|
|
|
url: '',
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
json: true,
|
|
|
|
form: ''
|
|
|
|
};
|
|
|
|
try {
|
2020-07-07 13:57:15 -04:00
|
|
|
if (node.ln_implementation) {
|
|
|
|
switch (node.ln_implementation.toUpperCase()) {
|
|
|
|
case 'CLT':
|
|
|
|
node.options.headers = { 'macaroon': Buffer.from(fs.readFileSync(path.join(node.macaroon_path, 'access.macaroon'))).toString("base64") };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ECL':
|
2020-07-12 15:47:42 -04:00
|
|
|
node.options.headers = { 'authorization': 'Basic ' + Buffer.from(':' + node.ln_api_password).toString('base64') };
|
2020-07-07 13:57:15 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
node.options.headers = { 'Grpc-Metadata-macaroon': fs.readFileSync(path.join(node.macaroon_path, 'admin.macaroon')).toString('hex') };
|
|
|
|
break;
|
|
|
|
}
|
2019-10-27 16:39:40 -04:00
|
|
|
}
|
2019-11-05 19:26:40 -05:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Common Set Options Error:' + JSON.stringify(err));
|
2019-10-27 16:39:40 -04:00
|
|
|
node.options = {
|
|
|
|
url: '',
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
json: true,
|
|
|
|
form: ''
|
|
|
|
};
|
2019-11-05 19:26:40 -05:00
|
|
|
}
|
|
|
|
});
|
2019-11-06 16:37:04 -05:00
|
|
|
common.updateSelectedNodeOptions();
|
2019-09-07 17:31:32 -04:00
|
|
|
}
|
2019-03-02 12:55:19 -05:00
|
|
|
}
|
|
|
|
|
2019-04-05 22:52:00 -04:00
|
|
|
common.findNode = (selNodeIndex) => {
|
2019-09-07 17:31:32 -04:00
|
|
|
return common.nodes.find(node => node.index == selNodeIndex);
|
2019-04-05 22:52:00 -04:00
|
|
|
}
|
2018-10-09 20:08:05 -04:00
|
|
|
|
2020-01-21 21:29:16 -05:00
|
|
|
common.replaceNode = (selNodeIndex, newNode) => {
|
|
|
|
common.nodes.splice(common.nodes.findIndex((node) => {node.index == selNodeIndex}), 1, newNode);
|
|
|
|
common.selectedNode = common.findNode(selNodeIndex);
|
|
|
|
}
|
|
|
|
|
2018-10-09 20:08:05 -04:00
|
|
|
common.convertToBTC = (num) => {
|
2019-09-07 17:31:32 -04:00
|
|
|
return (num / 100000000).toFixed(6);
|
2018-10-09 20:08:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
common.convertTimestampToDate = (num) => {
|
2019-11-15 22:34:05 -05:00
|
|
|
return new Date(+num * 1000).toUTCString().substring(5, 22).replace(' ', '/').replace(' ', '/').toUpperCase();
|
2018-10-09 20:08:05 -04:00
|
|
|
};
|
|
|
|
|
2019-01-19 18:47:56 -05:00
|
|
|
common.sortAscByKey = (array, key) => {
|
2019-09-07 17:31:32 -04:00
|
|
|
return array.sort(function (a, b) {
|
2019-12-05 15:16:41 -05:00
|
|
|
var x = +a[key]; var y = +b[key];
|
2019-09-07 17:31:32 -04:00
|
|
|
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
|
|
|
|
});
|
2019-01-19 18:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
common.sortDescByKey = (array, key) => {
|
2019-09-07 17:31:32 -04:00
|
|
|
const temp = array.sort(function (a, b) {
|
2019-12-05 15:16:41 -05:00
|
|
|
var x = +a[key]; var y = +b[key];
|
2020-05-03 15:52:38 -04:00
|
|
|
return (x > y) ? -1 : ((x < y) ? 1 : 0);
|
|
|
|
});
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
common.sortAscByStrKey = (array, key) => {
|
|
|
|
return array.sort(function (a, b) {
|
|
|
|
var x = a[key] ? a[key].toUpperCase() : ''; var y = b[key] ? b[key].toUpperCase() : '';
|
|
|
|
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
common.sortDescByStrKey = (array, key) => {
|
|
|
|
const temp = array.sort(function (a, b) {
|
|
|
|
var x = a[key] ? a[key].toUpperCase() : ''; var y = b[key] ? b[key].toUpperCase() : '';
|
|
|
|
return (x > y) ? -1 : ((x < y) ? 1 : 0);
|
2019-09-07 17:31:32 -04:00
|
|
|
});
|
|
|
|
return temp;
|
2019-01-19 18:47:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:49:19 -05:00
|
|
|
common.newestOnTop = (array, key, value) => {
|
2019-09-07 17:31:32 -04:00
|
|
|
var index = array.findIndex(function (item) {
|
|
|
|
return item[key] === value
|
|
|
|
});
|
|
|
|
var newlyAddedRecord = array.splice(index, 1);
|
|
|
|
array.unshift(newlyAddedRecord[0]);
|
|
|
|
return array;
|
2019-02-04 18:49:19 -05:00
|
|
|
}
|
|
|
|
|
2019-02-24 12:00:39 -05:00
|
|
|
module.exports = common;
|