2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-03 23:15:24 +00:00
RTL/controllers/peers.js

107 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-09-15 01:31:01 +00:00
var fs = require('fs');
var request = require('request-promise');
2018-09-15 01:31:01 +00:00
var options = require("../connect");
var common = require('../common');
2019-01-13 22:55:25 +00:00
var logger = require('./logger');
2018-09-15 01:31:01 +00:00
getAliasForPeers = (peer) => {
return new Promise(function(resolve, reject) {
options.url = common.lnd_server_url + '/graph/node/' + peer.pub_key;
request(options)
.then(function(aliasBody) {
2019-01-13 22:55:25 +00:00
logger.info('\r\nPeers: 11: ' + JSON.stringify(Date.now()) + ': INFO: Alias: ' + JSON.stringify(aliasBody.node.alias));
peer.alias = aliasBody.node.alias;
resolve(aliasBody.node.alias);
})
.catch(err => resolve(''));
});
}
2018-12-25 04:01:31 +00:00
exports.getPeers = (req, res, next) =>
{
options.url = common.lnd_server_url + '/peers';
request(options).then(function (body) {
let peers = (undefined === body.peers) ? [] : body.peers;
Promise.all(
peers.map(peer => {
return getAliasForPeers(peer);
}))
.then(function(values) {
2019-01-27 20:39:36 +00:00
if (undefined !== body.peers) {
body.peers = common.sortDescByKey(body.peers, 'alias');
}
2019-01-13 22:55:25 +00:00
logger.info('\r\nPeers: 29: ' + JSON.stringify(Date.now()) + ': INFO: Peers with Alias: ' + JSON.stringify(body));
2018-09-15 01:31:01 +00:00
res.status(200).json(body.peers);
});
2019-01-01 16:26:51 +00:00
})
.catch((err) => {
return res.status(500).json({
message: "Peers Fetched Failed!",
error: err.error
});
2018-09-15 01:31:01 +00:00
});
};
exports.postPeer = (req, res, next) => {
options.url = common.lnd_server_url + '/peers';
2018-09-15 01:31:01 +00:00
options.form = JSON.stringify({
addr: { host: req.body.host, pubkey: req.body.pubkey },
perm: req.body.perm
});
request.post(options, (error, response, body) => {
2019-01-13 22:55:25 +00:00
logger.info('\r\nPeers: 48: ' + JSON.stringify(Date.now()) + ': INFO: Peer Added: ' + JSON.stringify(body));
2018-09-15 01:31:01 +00:00
if(undefined === body || body.error) {
res.status(500).json({
2018-12-25 04:01:31 +00:00
message: "Adding peer failed!",
error: (undefined === body) ? 'Error From Server!' : body.error
2018-09-15 01:31:01 +00:00
});
} else {
options.url = common.lnd_server_url + '/peers';
request(options).then(function (body) {
let peers = (undefined === body.peers) ? [] : body.peers;
Promise.all(
peers.map(peer => {
return getAliasForPeers(peer);
}))
.then(function(values) {
if (undefined !== body.peers) {
body.peers = common.sortDescByKey(body.peers, 'alias');
logger.info('\r\nPeers: 69: ' + JSON.stringify(Date.now()) + ': INFO: Peer with Alias: ' + JSON.stringify(body));
body.peers = common.newestOnTop(body.peers, 'pub_key', req.body.pubkey);
logger.info('\r\nPeers: 71: ' + JSON.stringify(Date.now()) + ': INFO: Peer with Newest On Top: ' + JSON.stringify(body));
}
logger.info('\r\nPeers: 73: ' + JSON.stringify(Date.now()) + ': INFO: Peer Added Successfully');
res.status(201).json(body.peers);
2019-01-01 16:26:51 +00:00
})
.catch((err) => {
return res.status(500).json({
message: "Peer Add Failed!",
error: err.error
});
});
})
2018-09-15 01:31:01 +00:00
}
});
};
2018-12-25 04:01:31 +00:00
exports.deletePeer = (req, res, next) => {
options.url = common.lnd_server_url + '/peers/' + req.params.peerPubKey;
2019-01-01 16:26:51 +00:00
request.delete(options).then((body) => {
2019-01-13 22:55:25 +00:00
logger.info('\r\nPeers: 81: ' + JSON.stringify(Date.now()) + ': INFO: Detach Peer Response: ' + JSON.stringify(body));
2018-12-25 04:01:31 +00:00
if(undefined === body || body.error) {
res.status(500).json({
message: "Detach peer failed!",
error: (undefined === body) ? 'Error From Server!' : body.error
});
} else {
2019-01-13 22:55:25 +00:00
logger.info('\r\nPeers: 88: ' + JSON.stringify(Date.now()) + ': INFO: Peer Detached: ' + req.params.peerPubKey);
2019-01-01 16:26:51 +00:00
res.status(204).json({});
2018-12-25 04:01:31 +00:00
}
2019-01-01 16:26:51 +00:00
})
.catch((err) => {
return res.status(500).json({
message: "Detach Peer Failed!",
error: err.error
});
2018-12-25 04:01:31 +00:00
});
};