dht-rpc 6.0.2 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +11 -3
- package/package.json +1 -1
- package/test.js +19 -0
package/index.js
CHANGED
|
@@ -56,6 +56,7 @@ class DHT extends EventEmitter {
|
|
|
56
56
|
this._tickInterval = setInterval(this._ontick.bind(this), TICK_INTERVAL)
|
|
57
57
|
this._lastTick = Date.now()
|
|
58
58
|
this._lastHost = null
|
|
59
|
+
this._shouldAddNode = opts.addNode || null
|
|
59
60
|
this._onrow = (row) => row.on('full', (node) => this._onfullrow(node, row))
|
|
60
61
|
this._nonePersistentSamples = []
|
|
61
62
|
this._bootstrapping = this._bootstrap()
|
|
@@ -63,6 +64,7 @@ class DHT extends EventEmitter {
|
|
|
63
64
|
|
|
64
65
|
this.table.on('row', this._onrow)
|
|
65
66
|
|
|
67
|
+
if (this.ephemeral === false) this.io.ephemeral = false
|
|
66
68
|
this.io.networkInterfaces.on('change', (interfaces) => this._onnetworkchange(interfaces))
|
|
67
69
|
|
|
68
70
|
if (opts.nodes) {
|
|
@@ -240,6 +242,10 @@ class DHT extends EventEmitter {
|
|
|
240
242
|
}
|
|
241
243
|
|
|
242
244
|
_addNodeFromNetwork (sample, from, to) {
|
|
245
|
+
if (this._shouldAddNode !== null && !this._shouldAddNode(from)) {
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
|
|
243
249
|
if (from.id === null) {
|
|
244
250
|
this._sampleBootstrapMaybe(from, to)
|
|
245
251
|
return
|
|
@@ -502,9 +508,11 @@ class DHT extends EventEmitter {
|
|
|
502
508
|
|
|
503
509
|
const { host, port } = this._nat
|
|
504
510
|
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
511
|
+
if (!onlyFirewall) {
|
|
512
|
+
// remember what host we checked and reset the counter
|
|
513
|
+
this._stableTicks = MORE_STABLE_TICKS
|
|
514
|
+
this._lastHost = host
|
|
515
|
+
}
|
|
508
516
|
|
|
509
517
|
// check if we have a consistent host and port
|
|
510
518
|
if (host === null || port === 0) {
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -311,6 +311,25 @@ test('relay', async function (t) {
|
|
|
311
311
|
t.is(res.to.port, a.address().port)
|
|
312
312
|
})
|
|
313
313
|
|
|
314
|
+
test('filter nodes from routing table', async function (t) {
|
|
315
|
+
const [a, b, c] = await makeSwarm(3, t)
|
|
316
|
+
|
|
317
|
+
const node = new DHT({
|
|
318
|
+
ephemeral: false,
|
|
319
|
+
bootstrap: [a],
|
|
320
|
+
addNode (from) {
|
|
321
|
+
return from.port !== b.port
|
|
322
|
+
}
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
t.teardown(() => node.destroy())
|
|
326
|
+
|
|
327
|
+
const q = node.findNode(c.id)
|
|
328
|
+
await q.finished()
|
|
329
|
+
|
|
330
|
+
t.absent(node.table.has(b.id), 'should not have b')
|
|
331
|
+
})
|
|
332
|
+
|
|
314
333
|
function freePort () {
|
|
315
334
|
return new Promise(resolve => {
|
|
316
335
|
const socket = dgram.createSocket('udp4')
|