From 1141dad0a577c6e13d09ebed3718e4fe0e18b4c6 Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Thu, 26 Oct 2023 21:29:04 -0700 Subject: [PATCH] Websocket client rune fix --- backend/controllers/cln/webSocketClient.js | 18 ++++++++----- backend/models/config.model.js | 3 ++- backend/utils/common.js | 31 ++++++++++++++++++++-- server/controllers/cln/webSocketClient.ts | 17 +++++++----- server/models/config.model.ts | 1 + server/utils/common.ts | 29 ++++++++++++++++++-- 6 files changed, 82 insertions(+), 17 deletions(-) diff --git a/backend/controllers/cln/webSocketClient.js b/backend/controllers/cln/webSocketClient.js index a8c1bebd..46894b06 100644 --- a/backend/controllers/cln/webSocketClient.js +++ b/backend/controllers/cln/webSocketClient.js @@ -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; diff --git a/backend/models/config.model.js b/backend/models/config.model.js index 78cb9cfb..8ee944f2 100644 --- a/backend/models/config.model.js +++ b/backend/models/config.model.js @@ -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; diff --git a/backend/utils/common.js b/backend/utils/common.js index f3a010ea..a7bdddd2 100644 --- a/backend/utils/common.js +++ b/backend/utils/common.js @@ -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="(?[^"]+)"/; + 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') }; diff --git a/server/controllers/cln/webSocketClient.ts b/server/controllers/cln/webSocketClient.ts index b032b002..da1d255e 100644 --- a/server/controllers/cln/webSocketClient.ts +++ b/server/controllers/cln/webSocketClient.ts @@ -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; diff --git a/server/models/config.model.ts b/server/models/config.model.ts index ab0f00f0..997f0841 100644 --- a/server/models/config.model.ts +++ b/server/models/config.model.ts @@ -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, diff --git a/server/utils/common.ts b/server/utils/common.ts index 02725704..efc22dd8 100644 --- a/server/utils/common.ts +++ b/server/utils/common.ts @@ -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="(?[^"]+)"/; + 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':