2021-12-29 23:08:41 +00:00
|
|
|
import request from 'request-promise';
|
|
|
|
import { Logger } from '../../utils/logger.js';
|
|
|
|
import { Common } from '../../utils/common.js';
|
|
|
|
let options = null;
|
|
|
|
const logger = Logger;
|
|
|
|
const common = Common;
|
2022-01-16 20:55:50 +00:00
|
|
|
export const getAliasForPeers = (selNode, peer) => {
|
|
|
|
options.url = selNode.ln_server_url + '/v1/graph/node/' + peer.pub_key;
|
2021-12-29 23:08:41 +00:00
|
|
|
return request(options).then((aliasBody) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: selNode, level: 'DEBUG', fileName: 'Peers', msg: 'Alias Received', data: aliasBody.node.alias });
|
2021-12-29 23:08:41 +00:00
|
|
|
peer.alias = aliasBody.node.alias;
|
|
|
|
return aliasBody.node.alias;
|
|
|
|
}).catch((err) => {
|
2022-11-21 21:44:43 +00:00
|
|
|
peer.alias = peer.pub_key.slice(0, 20);
|
2021-12-29 23:08:41 +00:00
|
|
|
return peer.pub_key;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const getPeers = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Getting Peers..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/peers';
|
|
|
|
request(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peers List Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
const peers = !body.peers ? [] : body.peers;
|
2022-11-24 02:20:23 +00:00
|
|
|
return Promise.all(peers?.map((peer) => getAliasForPeers(req.session.selectedNode, peer))).then((values) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Sorted Peers List Received', data: body.peers });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body.peers);
|
|
|
|
});
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Peers', 'List Peers Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const postPeer = (req, res, next) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
const { host, pubkey, perm } = req.body;
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Connecting Peer..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/peers';
|
|
|
|
options.form = JSON.stringify({
|
2024-03-14 22:07:06 +00:00
|
|
|
addr: { host: host, pubkey: pubkey },
|
|
|
|
perm: perm
|
2021-12-29 23:08:41 +00:00
|
|
|
});
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peer Connected', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/peers';
|
|
|
|
request(options).then((body) => {
|
|
|
|
const peers = (!body.peers) ? [] : body.peers;
|
2022-11-24 02:20:23 +00:00
|
|
|
return Promise.all(peers?.map((peer) => getAliasForPeers(req.session.selectedNode, peer))).then((values) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
if (body.peers) {
|
2024-03-14 22:07:06 +00:00
|
|
|
body.peers = common.newestOnTop(body.peers, 'pub_key', pubkey);
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Peers List after Connect Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
res.status(201).json(body.peers);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Peers', 'Connect Peer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const deletePeer = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Disconnecting Peer..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/peers/' + req.params.peerPubKey;
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Peer Disconnect Pubkey', data: req.params.peerPubKey });
|
2021-12-29 23:08:41 +00:00
|
|
|
request.delete(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Peer Disconneted', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(204).json({});
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Peers', 'Disconnect Peer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|