From c97c1f19f8fb4c2890a0732e775611fb1796f7e4 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Tue, 20 Dec 2022 05:14:34 +0530 Subject: [PATCH] Bug fix --- src/backup/intra.js | 135 +----------------------------------------- src/backup/sync.js | 2 +- src/backup/values.js | 138 +++++++++++++++++++++++++++++++++++++++++++ src/client.js | 2 +- 4 files changed, 141 insertions(+), 136 deletions(-) create mode 100644 src/backup/values.js diff --git a/src/backup/intra.js b/src/backup/intra.js index df1979d..640b0dc 100644 --- a/src/backup/intra.js +++ b/src/backup/intra.js @@ -3,6 +3,7 @@ const WebSocket = require('ws'); const { DB } = require('../database'); const keys = require('../keys'); const TYPE_ = require('./message_types.json'); +const { _list, packet_, _nextNode, _prevNode } = require("./values"); const sync = require('./sync'); //CONSTANTS @@ -12,137 +13,6 @@ const SUPERNODE_INDICATOR = '$', var refresher; //container for and refresher -//List of node backups stored -const _list = {}; -Object.defineProperty(_list, 'delete', { - value: function (id) { - delete this[id]; - } -}); -Object.defineProperty(_list, 'get', { - value: function (keys = null) { - if (keys === null) keys = Object.keys(this); - if (Array.isArray(keys)) - return Object.fromEntries(keys.map(k => [k, this[k]])); - else - return this[keys]; - } -}); -Object.defineProperty(_list, 'stored', { - get: function () { - return Object.keys(this); - } -}); -Object.defineProperty(_list, 'serving', { - get: function () { - let serveList = []; - for (let id in this) - if (this[id] === 0) - serveList.push(id); - return serveList; - } -}); - -//Node container -function NodeContainer() { - var _ws, _id, _onmessage, _onclose; - Object.defineProperty(this, 'set', { - value: function (id, ws) { - if (_ws !== undefined) - this.close(); - _id = id; - _ws = ws; - if (_onmessage) - _ws.onmessage = _onmessage; - if (_onclose) - _ws.onclose = _onclose; - } - }); - Object.defineProperty(this, 'id', { - get: function () { - return _id; - } - }); - Object.defineProperty(this, 'readyState', { - get: function () { - if (_ws instanceof WebSocket) - return _ws.readyState; - else - return null; - } - }); - Object.defineProperty(this, 'send', { - value: function (packet) { - _ws.send(packet); - } - }); - Object.defineProperty(this, 'onmessage', { - set: function (fn) { - if (fn instanceof Function) - _onmessage = fn; - } - }); - Object.defineProperty(this, 'onclose', { - set: function (fn) { - if (fn instanceof Function) - _onclose = fn; - } - }); - Object.defineProperty(this, 'is', { - value: function (ws) { - return ws === _ws; - } - }); - Object.defineProperty(this, 'close', { - value: function () { - if (_ws.readyState === 1) { - _ws.onclose = () => console.warn('Closing: ' + _id); - _ws.close(); - }; - _ws = _id = undefined; - } - }); -}; - -//Container for next-node -const _nextNode = new NodeContainer(); -_nextNode.onmessage = evt => processTaskFromNextNode(evt.data); -_nextNode.onclose = evt => reconnectNextNode(); -//Container for prev-node -const _prevNode = new NodeContainer(); -_prevNode.onmessage = evt => processTaskFromPrevNode(evt.data); -_prevNode.onclose = evt => _prevNode.close(); - -//Packet processing -const packet_ = {}; -packet_.construct = function (message) { - const packet = { - from: keys.node_id, - message: message, - time: Date.now() - }; - packet.sign = floCrypto.signData(this.s(packet), keys.node_priv); - return SUPERNODE_INDICATOR + JSON.stringify(packet); -}; -packet_.s = d => [JSON.stringify(d.message), d.time].join("|"); -packet_.parse = function (str) { - try { - let packet = JSON.parse(str.substring(SUPERNODE_INDICATOR.length)); - let curTime = Date.now(); - if (packet.time > curTime - floGlobals.sn_config.delayDelta && - packet.from in floGlobals.supernodes && - floCrypto.verifySign(this.s(packet), packet.sign, floGlobals.supernodes[packet.from].pubKey)) { - if (!Array.isArray(packet.message)) - packet.message = [packet.message]; - return packet; - } else - return false; - } catch (error) { - console.error(str, error); - return false; - }; -}; - //-----NODE CONNECTORS (WEBSOCKET)----- //Connect to Node websocket @@ -672,9 +542,6 @@ module.exports = { logInterval, SUPERNODE_INDICATOR, _list, - _nextNode, - _prevNode, - packet_, set refresher(r) { refresher = r; } diff --git a/src/backup/sync.js b/src/backup/sync.js index 848d7a3..eeec76f 100644 --- a/src/backup/sync.js +++ b/src/backup/sync.js @@ -2,8 +2,8 @@ const Database = require("../database"); const DB = Database.DB; const floGlobals = require("../floGlobals"); const { H_struct } = require("../data_structure.json"); -const { packet_, _nextNode, _list } = require("./intra"); const TYPE = require('./message_types.json'); +const { _list, packet_, _nextNode } = require("./values"); const SYNC_WAIT_TIME = 5 * 60 * 1000 //5 mins diff --git a/src/backup/values.js b/src/backup/values.js new file mode 100644 index 0000000..e39e2bc --- /dev/null +++ b/src/backup/values.js @@ -0,0 +1,138 @@ +//List of node backups stored +const _list = {}; +Object.defineProperty(_list, 'delete', { + value: function (id) { + delete this[id]; + } +}); +Object.defineProperty(_list, 'get', { + value: function (keys = null) { + if (keys === null) keys = Object.keys(this); + if (Array.isArray(keys)) + return Object.fromEntries(keys.map(k => [k, this[k]])); + else + return this[keys]; + } +}); +Object.defineProperty(_list, 'stored', { + get: function () { + return Object.keys(this); + } +}); +Object.defineProperty(_list, 'serving', { + get: function () { + let serveList = []; + for (let id in this) + if (this[id] === 0) + serveList.push(id); + return serveList; + } +}); + +//Node container +function NodeContainer() { + var _ws, _id, _onmessage, _onclose; + Object.defineProperty(this, 'set', { + value: function (id, ws) { + if (_ws !== undefined) + this.close(); + _id = id; + _ws = ws; + if (_onmessage) + _ws.onmessage = _onmessage; + if (_onclose) + _ws.onclose = _onclose; + } + }); + Object.defineProperty(this, 'id', { + get: function () { + return _id; + } + }); + Object.defineProperty(this, 'readyState', { + get: function () { + if (_ws instanceof WebSocket) + return _ws.readyState; + else + return null; + } + }); + Object.defineProperty(this, 'send', { + value: function (packet) { + _ws.send(packet); + } + }); + Object.defineProperty(this, 'onmessage', { + set: function (fn) { + if (fn instanceof Function) + _onmessage = fn; + } + }); + Object.defineProperty(this, 'onclose', { + set: function (fn) { + if (fn instanceof Function) + _onclose = fn; + } + }); + Object.defineProperty(this, 'is', { + value: function (ws) { + return ws === _ws; + } + }); + Object.defineProperty(this, 'close', { + value: function () { + if (_ws.readyState === 1) { + _ws.onclose = () => console.warn('Closing: ' + _id); + _ws.close(); + }; + _ws = _id = undefined; + } + }); +}; + +//Container for next-node +const _nextNode = new NodeContainer(); +_nextNode.onmessage = evt => processTaskFromNextNode(evt.data); +_nextNode.onclose = evt => reconnectNextNode(); +//Container for prev-node +const _prevNode = new NodeContainer(); +_prevNode.onmessage = evt => processTaskFromPrevNode(evt.data); +_prevNode.onclose = evt => _prevNode.close(); + +//Packet processing +const packet_ = {}; +packet_.construct = function (message) { + const packet = { + from: keys.node_id, + message: message, + time: Date.now() + }; + packet.sign = floCrypto.signData(this.s(packet), keys.node_priv); + return SUPERNODE_INDICATOR + JSON.stringify(packet); +}; +packet_.s = d => [JSON.stringify(d.message), d.time].join("|"); +packet_.parse = function (str) { + try { + let packet = JSON.parse(str.substring(SUPERNODE_INDICATOR.length)); + let curTime = Date.now(); + if (packet.time > curTime - floGlobals.sn_config.delayDelta && + packet.from in floGlobals.supernodes && + floCrypto.verifySign(this.s(packet), packet.sign, floGlobals.supernodes[packet.from].pubKey)) { + if (!Array.isArray(packet.message)) + packet.message = [packet.message]; + return packet; + } else + return false; + } catch (error) { + console.error(str, error); + return false; + }; +}; + + +module.exports = { + _list, + _nextNode, + _prevNode, + packet_, +} \ No newline at end of file diff --git a/src/client.js b/src/client.js index ae8de06..92a3467 100644 --- a/src/client.js +++ b/src/client.js @@ -1,5 +1,5 @@ const { DB } = require("./database"); -const { _list } = require("./backup/intra"); +const { _list } = require("./backup/values"); global.INVALID = function (message) { if (!(this instanceof INVALID))