Bug fix
This commit is contained in:
parent
5836ba0686
commit
c97c1f19f8
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
138
src/backup/values.js
Normal file
138
src/backup/values.js
Normal file
@ -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_,
|
||||
}
|
||||
@ -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))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user