Compare commits
2 Commits
993eff5398
...
9cd5e798d9
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 9cd5e798d9 | |
Derrick Hammer | 7c2e6d1df8 |
|
@ -8,6 +8,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
private lru;
|
private lru;
|
||||||
private swarm;
|
private swarm;
|
||||||
private protocol;
|
private protocol;
|
||||||
|
private topic;
|
||||||
private symbol;
|
private symbol;
|
||||||
private socketMap;
|
private socketMap;
|
||||||
constructor({ lruSize, ttl, messageNumber, id, swarm, protocol, }?: {
|
constructor({ lruSize, ttl, messageNumber, id, swarm, protocol, }?: {
|
||||||
|
|
|
@ -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"}
|
{"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"}
|
|
@ -14,6 +14,8 @@ const messages_js_1 = require("./messages.js");
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const compact_encoding_1 = __importDefault(require("compact-encoding"));
|
const compact_encoding_1 = __importDefault(require("compact-encoding"));
|
||||||
const b4a_1 = __importDefault(require("b4a"));
|
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 debug = (0, debug_1.default)("dht-flood");
|
||||||
const LRU_SIZE = 255;
|
const LRU_SIZE = 255;
|
||||||
const TTL = 255;
|
const TTL = 255;
|
||||||
|
@ -25,6 +27,7 @@ class DHTFlood extends events_1.default {
|
||||||
lru;
|
lru;
|
||||||
swarm;
|
swarm;
|
||||||
protocol;
|
protocol;
|
||||||
|
topic;
|
||||||
symbol;
|
symbol;
|
||||||
socketMap = new Set();
|
socketMap = new Set();
|
||||||
constructor({ lruSize = LRU_SIZE, ttl = TTL, messageNumber = 0, id = crypto_1.default.randomBytes(32), swarm = null, protocol = PROTOCOL, } = {}) {
|
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);
|
const mux = protomux_1.default.from(peer);
|
||||||
mux.pair({ protocol: this.protocol }, () => this.setupPeer(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);
|
this.symbol = Symbol.for(this.protocol);
|
||||||
}
|
}
|
||||||
handleMessage({ originId, messageNumber, ttl, data }, messenger) {
|
handleMessage({ originId, messageNumber, ttl, data }, messenger) {
|
||||||
|
@ -113,8 +121,16 @@ class DHTFlood extends events_1.default {
|
||||||
broadcast(data, ttl = this.ttl) {
|
broadcast(data, ttl = this.ttl) {
|
||||||
this.messageNumber++;
|
this.messageNumber++;
|
||||||
const { id, messageNumber } = this;
|
const { id, messageNumber } = this;
|
||||||
for (const peer of this.swarm.connections.values()) {
|
let topicString = this.topic.toString("hex");
|
||||||
const message = this.setupPeer(peer);
|
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({
|
message.send({
|
||||||
originId: id,
|
originId: id,
|
||||||
messageNumber,
|
messageNumber,
|
||||||
|
|
26
src/index.ts
26
src/index.ts
|
@ -9,6 +9,8 @@ import { Packet } from "./messages.js";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import c from "compact-encoding";
|
import c from "compact-encoding";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
// @ts-ignore
|
||||||
|
import sodium from "sodium-universal";
|
||||||
|
|
||||||
const debug = debug0("dht-flood");
|
const debug = debug0("dht-flood");
|
||||||
|
|
||||||
|
@ -23,6 +25,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
private lru: LRU;
|
private lru: LRU;
|
||||||
private swarm: any;
|
private swarm: any;
|
||||||
private protocol: string;
|
private protocol: string;
|
||||||
|
private topic: Buffer;
|
||||||
private symbol: Symbol;
|
private symbol: Symbol;
|
||||||
private socketMap: Set<Function> = new Set<Function>();
|
private socketMap: Set<Function> = new Set<Function>();
|
||||||
|
|
||||||
|
@ -51,6 +54,14 @@ export default class DHTFlood extends EventEmitter {
|
||||||
mux.pair({ protocol: this.protocol }, () => this.setupPeer(peer));
|
mux.pair({ protocol: this.protocol }, () => this.setupPeer(peer));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const topic = b4a.from(this.protocol);
|
||||||
|
const topicHash = b4a.allocUnsafe(32);
|
||||||
|
sodium.crypto_generichash(topicHash, topic);
|
||||||
|
|
||||||
|
this.topic = topicHash as Buffer;
|
||||||
|
|
||||||
|
this.swarm.join(topicHash);
|
||||||
|
|
||||||
this.symbol = Symbol.for(this.protocol);
|
this.symbol = Symbol.for(this.protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,8 +159,19 @@ export default class DHTFlood extends EventEmitter {
|
||||||
this.messageNumber++;
|
this.messageNumber++;
|
||||||
const { id, messageNumber } = this;
|
const { id, messageNumber } = this;
|
||||||
|
|
||||||
for (const peer of this.swarm.connections.values()) {
|
let topicString = this.topic.toString("hex");
|
||||||
const message = this.setupPeer(peer);
|
|
||||||
|
let peers: Buffer[] = [...this.swarm.peers.values()]
|
||||||
|
.filter((peerInfo: any) => 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({
|
message.send({
|
||||||
originId: id,
|
originId: id,
|
||||||
messageNumber,
|
messageNumber,
|
||||||
|
|
Loading…
Reference in New Issue