2021-12-29 23:08:41 +00:00
|
|
|
import request from 'request-promise';
|
|
|
|
import { Logger, LoggerService } from '../../utils/logger.js';
|
|
|
|
import { Common, CommonService } from '../../utils/common.js';
|
2022-01-16 20:55:50 +00:00
|
|
|
import { CommonSelectedNode } from '../../models/config.model.js';
|
2021-12-29 23:08:41 +00:00
|
|
|
let options = null;
|
|
|
|
const logger: LoggerService = Logger;
|
|
|
|
const common: CommonService = Common;
|
|
|
|
|
2022-01-16 20:55:50 +00:00
|
|
|
export const getAliasForPeers = (selNode: CommonSelectedNode, 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) => {
|
|
|
|
peer.alias = peer.pub_key.slice(0, 10) + '...' + peer.pub_key.slice(-10);
|
|
|
|
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-08-17 22:48:04 +00:00
|
|
|
return Promise.all(peers?.map((peer) => getAliasForPeers(req.session.selectedNode, peer))).then((values) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peers with Alias before Sort', data: body });
|
|
|
|
if (body.peers) {
|
|
|
|
body.peers = common.sortDescByStrKey(body.peers, 'alias');
|
|
|
|
}
|
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) => {
|
|
|
|
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({
|
|
|
|
addr: { host: req.body.host, pubkey: req.body.pubkey },
|
|
|
|
perm: req.body.perm
|
|
|
|
});
|
|
|
|
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-08-17 22:48:04 +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) {
|
|
|
|
body.peers = common.sortDescByStrKey(body.peers, 'alias');
|
|
|
|
body.peers = common.newestOnTop(body.peers, 'pub_key', req.body.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 });
|
|
|
|
});
|
|
|
|
};
|