From 9cd5e798d91bfb5ffe9665894421e22372852f8f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 30 Dec 2022 16:22:51 -0500 Subject: [PATCH] *Update dist --- dist/index.d.ts | 1 + dist/index.d.ts.map | 2 +- dist/index.js | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index ba2bebe..c51051d 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -8,6 +8,7 @@ export default class DHTFlood extends EventEmitter { private lru; private swarm; private protocol; + private topic; private symbol; private socketMap; constructor({ lruSize, ttl, messageNumber, id, swarm, protocol, }?: { diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 61e7c8a..d3ce3e4 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAkBlC,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAChD,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAsC;gBAE3C,EACV,OAAkB,EAClB,GAAS,EACT,aAAiB,EACjB,EAA2B,EAC3B,KAAY,EACZ,QAAmB,GACpB;;;;;;;KAAK;IAqBN,OAAO,CAAC,aAAa;IAkCrB,OAAO,CAAC,SAAS;IAwDjB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;IAenC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;CAY1C"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAoBlC,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAChD,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAsC;gBAE3C,EACV,OAAkB,EAClB,GAAS,EACT,aAAiB,EACjB,EAA2B,EAC3B,KAAY,EACZ,QAAmB,GACpB;;;;;;;KAAK;IA6BN,OAAO,CAAC,aAAa;IAkCrB,OAAO,CAAC,SAAS;IAwDjB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;IA0BnC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;CAY1C"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index ec61a1a..adcd4b0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -14,6 +14,8 @@ const messages_js_1 = require("./messages.js"); // @ts-ignore const compact_encoding_1 = __importDefault(require("compact-encoding")); const b4a_1 = __importDefault(require("b4a")); +// @ts-ignore +const sodium_universal_1 = __importDefault(require("sodium-universal")); const debug = (0, debug_1.default)("dht-flood"); const LRU_SIZE = 255; const TTL = 255; @@ -25,6 +27,7 @@ class DHTFlood extends events_1.default { lru; swarm; protocol; + topic; symbol; socketMap = new Set(); constructor({ lruSize = LRU_SIZE, ttl = TTL, messageNumber = 0, id = crypto_1.default.randomBytes(32), swarm = null, protocol = PROTOCOL, } = {}) { @@ -42,6 +45,11 @@ class DHTFlood extends events_1.default { const mux = protomux_1.default.from(peer); mux.pair({ protocol: this.protocol }, () => this.setupPeer(peer)); }); + const topic = b4a_1.default.from(this.protocol); + const topicHash = b4a_1.default.allocUnsafe(32); + sodium_universal_1.default.crypto_generichash(topicHash, topic); + this.topic = topicHash; + this.swarm.join(topicHash); this.symbol = Symbol.for(this.protocol); } handleMessage({ originId, messageNumber, ttl, data }, messenger) { @@ -113,8 +121,16 @@ class DHTFlood extends events_1.default { broadcast(data, ttl = this.ttl) { this.messageNumber++; const { id, messageNumber } = this; - for (const peer of this.swarm.connections.values()) { - const message = this.setupPeer(peer); + let topicString = this.topic.toString("hex"); + let peers = [...this.swarm.peers.values()] + .filter((peerInfo) => peerInfo.topics.includes(topicString)) + .map((peerInfo) => peerInfo.publicKey); + for (const peer of peers) { + const conn = this.swarm._allConnections.get(peer); + if (conn) { + continue; + } + const message = this.setupPeer(conn); message.send({ originId: id, messageNumber,