2021-12-29 23:08:41 +00:00
import * as fs from 'fs' ;
import { join } from 'path' ;
import WebSocket from 'ws' ;
import { Logger } from '../../utils/logger.js' ;
import { Common } from '../../utils/common.js' ;
import { WSServer } from '../../utils/webSocketServer.js' ;
export class CLWebSocketClient {
constructor ( ) {
this . logger = Logger ;
this . common = Common ;
this . wsServer = WSServer ;
this . webSocketClients = [ ] ;
this . reconnectTimeOut = null ;
this . waitTime = 0.5 ;
this . reconnet = ( clWsClt ) => {
if ( this . reconnectTimeOut ) {
return ;
}
this . waitTime = ( this . waitTime >= 64 ) ? 64 : ( this . waitTime * 2 ) ;
this . reconnectTimeOut = setTimeout ( ( ) => {
if ( clWsClt . selectedNode ) {
2022-05-01 17:35:20 +00:00
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'INFO' , fileName : 'CLWebSocket' , msg : 'Reconnecting to the Core Lightning\'s Websocket Server..' } ) ;
2021-12-29 23:08:41 +00:00
this . connect ( clWsClt . selectedNode ) ;
}
this . reconnectTimeOut = null ;
} , this . waitTime * 1000 ) ;
} ;
this . connect = ( selectedNode ) => {
try {
const clientExists = this . webSocketClients . find ( ( wsc ) => wsc . selectedNode . index === selectedNode . index ) ;
if ( ! clientExists ) {
if ( selectedNode . ln _server _url ) {
const newWebSocketClient = { selectedNode : selectedNode , reConnect : true , webSocketClient : null } ;
this . connectWithClient ( newWebSocketClient ) ;
this . webSocketClients . push ( newWebSocketClient ) ;
}
}
else {
if ( ( ! clientExists . webSocketClient || clientExists . webSocketClient . readyState !== WebSocket . OPEN ) && selectedNode . ln _server _url ) {
clientExists . reConnect = true ;
this . connectWithClient ( clientExists ) ;
}
}
}
catch ( err ) {
throw new Error ( err ) ;
}
} ;
this . connectWithClient = ( clWsClt ) => {
2022-08-17 22:48:04 +00:00
var _a ;
2022-05-01 17:35:20 +00:00
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'INFO' , fileName : 'CLWebSocket' , msg : 'Connecting to the Core Lightning\'s Websocket Server..' } ) ;
2022-08-17 22:48:04 +00:00
const WS _LINK = ( ( _a = ( clWsClt . selectedNode . ln _server _url ) ) === null || _a === void 0 ? void 0 : _a . replace ( /^http/ , 'ws' ) ) + '/v1/ws' ;
2021-12-29 23:08:41 +00:00
const mcrnHexEncoded = Buffer . from ( fs . readFileSync ( join ( clWsClt . selectedNode . macaroon _path , 'access.macaroon' ) ) ) . toString ( 'hex' ) ;
clWsClt . webSocketClient = new WebSocket ( WS _LINK , [ mcrnHexEncoded , 'hex' ] , { rejectUnauthorized : false } ) ;
clWsClt . webSocketClient . onopen = ( ) => {
2022-05-01 17:35:20 +00:00
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'INFO' , fileName : 'CLWebSocket' , msg : 'Connected to the Core Lightning\'s Websocket Server..' } ) ;
2021-12-29 23:08:41 +00:00
this . waitTime = 0.5 ;
} ;
clWsClt . webSocketClient . onclose = ( e ) => {
2022-05-01 17:35:20 +00:00
if ( clWsClt && clWsClt . selectedNode && clWsClt . selectedNode . ln _implementation === 'CLN' ) {
2021-12-29 23:08:41 +00:00
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'INFO' , fileName : 'CLWebSocket' , msg : 'Web socket disconnected, will reconnect again...' } ) ;
clWsClt . webSocketClient . close ( ) ;
if ( clWsClt . reConnect ) {
this . reconnet ( clWsClt ) ;
}
}
} ;
clWsClt . webSocketClient . onmessage = ( msg ) => {
2022-01-16 20:55:50 +00:00
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'DEBUG' , fileName : 'CLWebSocket' , msg : 'Received message from the server..' , data : msg . data } ) ;
2021-12-29 23:08:41 +00:00
msg = ( typeof msg . data === 'string' ) ? JSON . parse ( msg . data ) : msg . data ;
2022-05-01 17:35:20 +00:00
msg [ 'source' ] = 'CLN' ;
2021-12-29 23:08:41 +00:00
const msgStr = JSON . stringify ( msg ) ;
this . wsServer . sendEventsToAllLNClients ( msgStr , clWsClt . selectedNode ) ;
} ;
clWsClt . webSocketClient . onerror = ( err ) => {
if ( clWsClt . selectedNode . api _version === '' || ! clWsClt . selectedNode . api _version || this . common . isVersionCompatible ( clWsClt . selectedNode . api _version , '0.6.0' ) ) {
this . logger . log ( { selectedNode : clWsClt . selectedNode , level : 'ERROR' , fileName : 'CLWebSocket' , msg : 'Web socket error' , error : err } ) ;
const errStr = ( ( typeof err === 'object' && err . message ) ? JSON . stringify ( { error : err . message } ) : ( typeof err === 'object' ) ? JSON . stringify ( { error : err } ) : ( '{ "error": ' + err + ' }' ) ) ;
this . wsServer . sendErrorToAllLNClients ( errStr , clWsClt . selectedNode ) ;
clWsClt . webSocketClient . close ( ) ;
if ( clWsClt . reConnect ) {
this . reconnet ( clWsClt ) ;
}
}
else {
clWsClt . reConnect = false ;
}
} ;
} ;
this . disconnect = ( selectedNode ) => {
const clientExists = this . webSocketClients . find ( ( wsc ) => wsc . selectedNode . index === selectedNode . index ) ;
if ( clientExists && clientExists . webSocketClient && clientExists . webSocketClient . readyState === WebSocket . OPEN ) {
2022-05-01 17:35:20 +00:00
this . logger . log ( { selectedNode : clientExists . selectedNode , level : 'INFO' , fileName : 'CLWebSocket' , msg : 'Disconnecting from the Core Lightning\'s Websocket Server..' } ) ;
2021-12-29 23:08:41 +00:00
clientExists . reConnect = false ;
clientExists . webSocketClient . close ( ) ;
const clientIdx = this . webSocketClients . findIndex ( ( wsc ) => wsc . selectedNode . index === selectedNode . index ) ;
this . webSocketClients . splice ( clientIdx , 1 ) ;
}
} ;
this . updateSelectedNode = ( newSelectedNode ) => {
const clientIdx = this . webSocketClients . findIndex ( ( wsc ) => + wsc . selectedNode . index === + newSelectedNode . index ) ;
2022-01-16 20:55:50 +00:00
let newClient = this . webSocketClients [ clientIdx ] ;
if ( ! newClient ) {
newClient = { selectedNode : null , reConnect : true , webSocketClient : null } ;
}
2021-12-29 23:08:41 +00:00
newClient . selectedNode = JSON . parse ( JSON . stringify ( newSelectedNode ) ) ;
this . webSocketClients [ clientIdx ] = newClient ;
} ;
2022-05-01 17:35:20 +00:00
this . wsServer . eventEmitterCLN . on ( 'CONNECT' , ( nodeIndex ) => {
2021-12-29 23:08:41 +00:00
this . connect ( this . common . findNode ( + nodeIndex ) ) ;
} ) ;
2022-05-01 17:35:20 +00:00
this . wsServer . eventEmitterCLN . on ( 'DISCONNECT' , ( nodeIndex ) => {
2021-12-29 23:08:41 +00:00
this . disconnect ( this . common . findNode ( + nodeIndex ) ) ;
} ) ;
}
}
export const CLWSClient = new CLWebSocketClient ( ) ;