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

Websocket client rune fix

This commit is contained in:
ShahanaFarooqui 2023-10-26 21:29:04 -07:00
parent 91cf05cee6
commit 1141dad0a5
6 changed files with 82 additions and 17 deletions

View File

@ -1,4 +1,3 @@
import * as fs from 'fs';
import WebSocket from 'ws';
import { Logger } from '../../utils/logger.js';
import { Common } from '../../utils/common.js';
@ -47,11 +46,18 @@ export class CLWebSocketClient {
};
this.connectWithClient = (clWsClt) => {
this.logger.log({ selectedNode: clWsClt.selectedNode, level: 'INFO', fileName: 'CLWebSocket', msg: 'Connecting to the Core Lightning\'s Websocket Server..' });
const mcrnHexEncoded = Buffer.from(fs.readFileSync(clWsClt.selectedNode.macaroon_path)).toString().replace('\n', '');
clWsClt.webSocketClient = new WebSocket(clWsClt.selectedNode.ln_server_url, {
headers: { rune: mcrnHexEncoded },
rejectUnauthorized: false
});
try {
if (!clWsClt.selectedNode.macaroon_value) {
clWsClt.selectedNode.macaroon_value = this.common.getMacaroonValue(clWsClt.selectedNode.macaroon_path);
}
clWsClt.webSocketClient = new WebSocket(clWsClt.selectedNode.ln_server_url, {
headers: { rune: clWsClt.selectedNode.macaroon_value },
rejectUnauthorized: false
});
}
catch (err) {
throw new Error(err);
}
clWsClt.webSocketClient.onopen = () => {
this.logger.log({ selectedNode: clWsClt.selectedNode, level: 'INFO', fileName: 'CLWebSocket', msg: 'Connected to the Core Lightning\'s Websocket Server..' });
this.waitTime = 0.5;

View File

@ -1,8 +1,9 @@
export class CommonSelectedNode {
constructor(options, ln_server_url, macaroon_path, ln_api_password, swap_server_url, boltz_server_url, config_path, rtl_conf_file_path, swap_macaroon_path, boltz_macaroon_path, bitcoind_config_path, channel_backup_path, log_level, log_file, index, ln_node, ln_implementation, user_persona, theme_mode, theme_color, unannounced_channels, fiat_conversion, currency_unit, ln_version, api_version, enable_offers, enable_peerswap) {
constructor(options, ln_server_url, macaroon_path, macaroon_value, ln_api_password, swap_server_url, boltz_server_url, config_path, rtl_conf_file_path, swap_macaroon_path, boltz_macaroon_path, bitcoind_config_path, channel_backup_path, log_level, log_file, index, ln_node, ln_implementation, user_persona, theme_mode, theme_color, unannounced_channels, fiat_conversion, currency_unit, ln_version, api_version, enable_offers, enable_peerswap) {
this.options = options;
this.ln_server_url = ln_server_url;
this.macaroon_path = macaroon_path;
this.macaroon_value = macaroon_value;
this.ln_api_password = ln_api_password;
this.swap_server_url = swap_server_url;
this.boltz_server_url = boltz_server_url;

View File

@ -90,7 +90,15 @@ export class CommonService {
if (req.session.selectedNode && req.session.selectedNode.ln_implementation) {
switch (req.session.selectedNode.ln_implementation.toUpperCase()) {
case 'CLN':
req.session.selectedNode.options.headers = { rune: Buffer.from(fs.readFileSync(req.session.selectedNode.macaroon_path)).toString().replace('\n', '') };
try {
if (!req.session.selectedNode.macaroon_value) {
req.session.selectedNode.macaroon_value = this.getMacaroonValue(req.session.selectedNode.macaroon_path);
}
req.session.selectedNode.options.headers = { rune: req.session.selectedNode.macaroon_value };
}
catch (err) {
throw new Error(err);
}
break;
case 'ECL':
req.session.selectedNode.options.headers = { authorization: 'Basic ' + Buffer.from(':' + req.session.selectedNode.ln_api_password).toString('base64') };
@ -116,6 +124,17 @@ export class CommonService {
return { status: 502, message: err };
}
};
this.getMacaroonValue = (macaroon_path) => {
const data = fs.readFileSync(macaroon_path, 'utf8');
const pattern = /LIGHTNING_RUNE="(?<runeValue>[^"]+)"/;
const match = data.match(pattern);
if (match.groups.runeValue) {
return match.groups.runeValue;
}
else {
throw new Error('Rune not found in the file.');
}
};
this.setOptions = (req) => {
if (this.nodes[0].options && this.nodes[0].options.headers) {
return;
@ -132,7 +151,15 @@ export class CommonService {
if (node.ln_implementation) {
switch (node.ln_implementation.toUpperCase()) {
case 'CLN':
node.options.headers = { rune: Buffer.from(fs.readFileSync(node.macaroon_path)).toString().replace('\n', '') };
try {
if (!node.macaroon_value) {
node.macaroon_value = this.getMacaroonValue(node.macaroon_path);
}
node.options.headers = { rune: node.macaroon_value };
}
catch (err) {
throw new Error(err);
}
break;
case 'ECL':
node.options.headers = { authorization: 'Basic ' + Buffer.from(':' + node.ln_api_password).toString('base64') };

View File

@ -58,12 +58,17 @@ export class CLWebSocketClient {
public connectWithClient = (clWsClt) => {
this.logger.log({ selectedNode: clWsClt.selectedNode, level: 'INFO', fileName: 'CLWebSocket', msg: 'Connecting to the Core Lightning\'s Websocket Server..' });
const mcrnHexEncoded = Buffer.from(fs.readFileSync(clWsClt.selectedNode.macaroon_path)).toString().replace('\n', '');
clWsClt.webSocketClient = new WebSocket(clWsClt.selectedNode.ln_server_url, {
headers: { rune: mcrnHexEncoded },
rejectUnauthorized: false
});
try {
if (!clWsClt.selectedNode.macaroon_value) {
clWsClt.selectedNode.macaroon_value = this.common.getMacaroonValue(clWsClt.selectedNode.macaroon_path);
}
clWsClt.webSocketClient = new WebSocket(clWsClt.selectedNode.ln_server_url, {
headers: { rune: clWsClt.selectedNode.macaroon_value },
rejectUnauthorized: false
});
} catch (err) {
throw new Error(err);
}
clWsClt.webSocketClient.onopen = () => {
this.logger.log({ selectedNode: clWsClt.selectedNode, level: 'INFO', fileName: 'CLWebSocket', msg: 'Connected to the Core Lightning\'s Websocket Server..' });
this.waitTime = 0.5;

View File

@ -4,6 +4,7 @@ export class CommonSelectedNode {
public options?: any,
public ln_server_url?: string,
public macaroon_path?: string,
public macaroon_value?: string,
public ln_api_password?: string,
public swap_server_url?: string,
public boltz_server_url?: string,

View File

@ -96,7 +96,14 @@ export class CommonService {
if (req.session.selectedNode && req.session.selectedNode.ln_implementation) {
switch (req.session.selectedNode.ln_implementation.toUpperCase()) {
case 'CLN':
req.session.selectedNode.options.headers = { rune: Buffer.from(fs.readFileSync(req.session.selectedNode.macaroon_path)).toString().replace('\n', '') };
try {
if (!req.session.selectedNode.macaroon_value) {
req.session.selectedNode.macaroon_value = this.getMacaroonValue(req.session.selectedNode.macaroon_path);
}
req.session.selectedNode.options.headers = { rune: req.session.selectedNode.macaroon_value };
} catch (err) {
throw new Error(err);
}
break;
case 'ECL':
@ -124,6 +131,17 @@ export class CommonService {
}
};
public getMacaroonValue = (macaroon_path) => {
const data = fs.readFileSync(macaroon_path, 'utf8');
const pattern = /LIGHTNING_RUNE="(?<runeValue>[^"]+)"/;
const match = data.match(pattern);
if (match.groups.runeValue) {
return match.groups.runeValue;
} else {
throw new Error('Rune not found in the file.');
}
};
public setOptions = (req) => {
if (this.nodes[0].options && this.nodes[0].options.headers) { return; }
if (this.nodes && this.nodes.length > 0) {
@ -138,7 +156,14 @@ export class CommonService {
if (node.ln_implementation) {
switch (node.ln_implementation.toUpperCase()) {
case 'CLN':
node.options.headers = { rune: Buffer.from(fs.readFileSync(node.macaroon_path)).toString().replace('\n', '') };
try {
if (!node.macaroon_value) {
node.macaroon_value = this.getMacaroonValue(node.macaroon_path);
}
node.options.headers = { rune: node.macaroon_value };
} catch (err) {
throw new Error(err);
}
break;
case 'ECL':