2018-12-10 02:26:11 +00:00
|
|
|
var request = require('request-promise');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
|
|
|
var logger = require('../logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-09-15 01:31:01 +00:00
|
|
|
|
2018-12-10 02:26:11 +00:00
|
|
|
getAliasForPeers = (peer) => {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/graph/node/' + peer.pub_key;
|
2018-12-10 02:26:11 +00:00
|
|
|
request(options)
|
|
|
|
.then(function(aliasBody) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Alias: ' + JSON.stringify(aliasBody.node.alias)});
|
2018-12-10 02:26:11 +00:00
|
|
|
peer.alias = aliasBody.node.alias;
|
|
|
|
resolve(aliasBody.node.alias);
|
|
|
|
})
|
2018-12-10 02:49:17 +00:00
|
|
|
.catch(err => resolve(''));
|
2018-12-10 02:26:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-06 02:52:00 +00:00
|
|
|
exports.getPeers = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/peers';
|
2018-12-10 02:26:11 +00:00
|
|
|
request(options).then(function (body) {
|
2020-01-29 23:36:28 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peers Received: ' + JSON.stringify(body)});
|
2018-12-10 02:26:11 +00:00
|
|
|
let peers = (undefined === body.peers) ? [] : body.peers;
|
|
|
|
Promise.all(
|
|
|
|
peers.map(peer => {
|
|
|
|
return getAliasForPeers(peer);
|
|
|
|
}))
|
|
|
|
.then(function(values) {
|
2020-01-29 23:36:28 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peers with Alias before Sort: ' + JSON.stringify(body)});
|
2019-01-27 20:39:36 +00:00
|
|
|
if (undefined !== body.peers) {
|
|
|
|
body.peers = common.sortDescByKey(body.peers, 'alias');
|
|
|
|
}
|
2020-01-29 23:36:28 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peers with Alias after Sort: ' + JSON.stringify(body)});
|
2018-09-15 01:31:01 +00:00
|
|
|
res.status(200).json(body.peers);
|
2018-12-10 02:26:11 +00:00
|
|
|
});
|
2019-01-01 16:26:51 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2020-02-03 17:09:13 +00:00
|
|
|
logger.error({fileName: 'Peers', lineNum: 39, msg: 'Peers List Failed: ' + JSON.stringify(err.error)});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
2020-02-03 17:09:13 +00:00
|
|
|
message: "Peers List Failed!",
|
2019-01-01 16:26:51 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postPeer = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/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-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: '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!",
|
2018-10-12 00:03:54 +00:00
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
|
|
|
} else {
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/peers';
|
2018-12-20 01:54:52 +00:00
|
|
|
request(options).then(function (body) {
|
|
|
|
let peers = (undefined === body.peers) ? [] : body.peers;
|
|
|
|
Promise.all(
|
|
|
|
peers.map(peer => {
|
|
|
|
return getAliasForPeers(peer);
|
|
|
|
}))
|
|
|
|
.then(function(values) {
|
2019-02-04 23:49:19 +00:00
|
|
|
if (undefined !== body.peers) {
|
|
|
|
body.peers = common.sortDescByKey(body.peers, 'alias');
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peer with Alias: ' + JSON.stringify(body)});
|
2019-02-04 23:49:19 +00:00
|
|
|
body.peers = common.newestOnTop(body.peers, 'pub_key', req.body.pubkey);
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peer with Newest On Top: ' + JSON.stringify(body)});
|
2019-02-04 23:49:19 +00:00
|
|
|
}
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: 'Peer Added Successfully'});
|
2018-12-20 01:54:52 +00:00
|
|
|
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-12-20 01:54:52 +00:00
|
|
|
});
|
|
|
|
})
|
2018-09-15 01:31:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-12-25 04:01:31 +00:00
|
|
|
|
|
|
|
exports.deletePeer = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/peers/' + req.params.peerPubKey;
|
2019-01-01 16:26:51 +00:00
|
|
|
request.delete(options).then((body) => {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: '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-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Peers', msg: '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
|
|
|
});
|
|
|
|
};
|