From 4aae30bdbf5ffda66f226c53689d9866655de955 Mon Sep 17 00:00:00 2001 From: Uros Kukic <33048701+Kuki145@users.noreply.github.com> Date: Fri, 18 Sep 2020 13:38:12 +0200 Subject: [PATCH 1/2] Improve node filtering based on network id (#1342) --- modules/network/kademlia/kademlia.js | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/modules/network/kademlia/kademlia.js b/modules/network/kademlia/kademlia.js index 96f036ae8..860744bd5 100644 --- a/modules/network/kademlia/kademlia.js +++ b/modules/network/kademlia/kademlia.js @@ -213,6 +213,8 @@ class Kademlia { if (!fs.existsSync(peerCacheFilePath)) { fs.writeFileSync(peerCacheFilePath, '{}'); + } else { + this._filterPeerCache(peerCacheFilePath); } this.node.rolodex = this.node.plugin(kadence.rolodex(peerCacheFilePath)); @@ -242,6 +244,23 @@ class Kademlia { this._registerRoutes(); + // Override node's _updateContact method to filter contacts. + this.node._updateContact = (identity, contact) => { + try { + if (!this.validateContact(identity, contact)) { + this.log.debug(`Ignored contact ${identity}. Hostname ${contact.hostname}. Network ID ${contact.network_id}.`); + return; + } + } catch (err) { + this.log.debug(`Failed to filter contact(${identity}, ${contact}). ${err}.`); + return; + } + + // Simulate node's "super._updateContact(identity, contact)". + this.node.constructor.prototype.constructor.prototype + ._updateContact.call(this.node, identity, contact); + }; + this.node.listen(this.config.node_port, () => { this.log.notify(`OT Node listening at https://${this.node.contact.hostname}:${this.node.contact.port}`); this.kademliaUtilities.registerControlInterface(this.config, this.node); @@ -258,6 +277,7 @@ class Kademlia { if (entry) { this.log.info(`Connected to network via ${entry}`); this.log.info(`Discovered ${this.node.router.size} peers from seed`); + this._filterPeerCache(peerCacheFilePath); } resolve(); }); @@ -871,6 +891,55 @@ class Kademlia { } return null; } + + + _filterContacts() { + const nodesToRemove = []; + this.node.router.forEach((value, key, map) => { + if (value.length > 0) { + value.forEach((bValue, bKey, bMap) => { + if (bValue.network_id !== this.config.network.id) { + nodesToRemove.push(bKey); + } + }); + } + }); + + for (const nod of nodesToRemove) { + this.node.router.removeContactByNodeId(nod); + } + + const message = {}; + this.node.router.forEach((value, key, map) => { + if (value.length > 0) { + value.forEach((bValue, bKey, bMap) => { + if (bValue.network_id !== this.config.network.id) { + nodesToRemove.push(bKey); + } + message[bKey] = bValue; + }); + } + }); + + return message; + } + + _filterPeerCache(peerCacheFilePath) { + const peerCacheFile = fs.readFileSync(peerCacheFilePath); + + const peerCache = JSON.parse(peerCacheFile); + + for (const id in peerCache) { + const elem = peerCache[id]; + if (elem.network_id !== this.config.network.id) { + delete peerCache[id]; + } + } + + this._filterContacts(); + + fs.writeFileSync(peerCacheFilePath, JSON.stringify(peerCache)); + } } module.exports = Kademlia; From fad5a7a10cfa636432f923313cc8e4737393e3f4 Mon Sep 17 00:00:00 2001 From: Uros Kukic <33048701+Kuki145@users.noreply.github.com> Date: Fri, 18 Sep 2020 14:07:32 +0200 Subject: [PATCH 2/2] Improve network validation formatting (#1344) * Improve node filtering based on network id * Improve filter function format --- modules/network/kademlia/kademlia.js | 32 +++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/modules/network/kademlia/kademlia.js b/modules/network/kademlia/kademlia.js index 860744bd5..3daee85bf 100644 --- a/modules/network/kademlia/kademlia.js +++ b/modules/network/kademlia/kademlia.js @@ -214,7 +214,7 @@ class Kademlia { if (!fs.existsSync(peerCacheFilePath)) { fs.writeFileSync(peerCacheFilePath, '{}'); } else { - this._filterPeerCache(peerCacheFilePath); + this._filterContacts(peerCacheFilePath); } this.node.rolodex = this.node.plugin(kadence.rolodex(peerCacheFilePath)); @@ -277,7 +277,7 @@ class Kademlia { if (entry) { this.log.info(`Connected to network via ${entry}`); this.log.info(`Discovered ${this.node.router.size} peers from seed`); - this._filterPeerCache(peerCacheFilePath); + this._filterContacts(peerCacheFilePath); } resolve(); }); @@ -893,13 +893,18 @@ class Kademlia { } - _filterContacts() { + + _filterRoutingTable() { + const message = {}; const nodesToRemove = []; + this.node.router.forEach((value, key, map) => { if (value.length > 0) { value.forEach((bValue, bKey, bMap) => { if (bValue.network_id !== this.config.network.id) { nodesToRemove.push(bKey); + } else { + message[bKey] = bValue; } }); } @@ -909,18 +914,6 @@ class Kademlia { this.node.router.removeContactByNodeId(nod); } - const message = {}; - this.node.router.forEach((value, key, map) => { - if (value.length > 0) { - value.forEach((bValue, bKey, bMap) => { - if (bValue.network_id !== this.config.network.id) { - nodesToRemove.push(bKey); - } - message[bKey] = bValue; - }); - } - }); - return message; } @@ -936,9 +929,14 @@ class Kademlia { } } - this._filterContacts(); - fs.writeFileSync(peerCacheFilePath, JSON.stringify(peerCache)); + + return peerCache; + } + + _filterContacts(peerCacheFilePath) { + this._filterPeerCache(peerCacheFilePath); + this._filterRoutingTable(); } }