Browse Source

add allowNode that can filter which nodes are added automatically (#46)

* add allowNode that can filter which nodes are added automatically

* Add test and rename option

Co-authored-by: Kasper Isager Dalsgarð <kasperisager@hey.com>
session-estimator
Mathias Buus 2 years ago
committed by GitHub
parent
commit
80724872f5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      index.js
  2. 19
      test.js

5
index.js

@ -56,6 +56,7 @@ class DHT extends EventEmitter {
this._tickInterval = setInterval(this._ontick.bind(this), TICK_INTERVAL) this._tickInterval = setInterval(this._ontick.bind(this), TICK_INTERVAL)
this._lastTick = Date.now() this._lastTick = Date.now()
this._lastHost = null this._lastHost = null
this._shouldAddNode = opts.addNode || null
this._onrow = (row) => row.on('full', (node) => this._onfullrow(node, row)) this._onrow = (row) => row.on('full', (node) => this._onfullrow(node, row))
this._nonePersistentSamples = [] this._nonePersistentSamples = []
this._bootstrapping = this._bootstrap() this._bootstrapping = this._bootstrap()
@ -241,6 +242,10 @@ class DHT extends EventEmitter {
} }
_addNodeFromNetwork (sample, from, to) { _addNodeFromNetwork (sample, from, to) {
if (this._shouldAddNode !== null && !this._shouldAddNode(from)) {
return
}
if (from.id === null) { if (from.id === null) {
this._sampleBootstrapMaybe(from, to) this._sampleBootstrapMaybe(from, to)
return return

19
test.js

@ -311,6 +311,25 @@ test('relay', async function (t) {
t.is(res.to.port, a.address().port) t.is(res.to.port, a.address().port)
}) })
test('filter nodes from routing table', async function (t) {
const [a, b, c] = await makeSwarm(3, t)
const node = new DHT({
ephemeral: false,
bootstrap: [a],
addNode (from) {
return from.port !== b.port
}
})
t.teardown(() => node.destroy())
const q = node.findNode(c.id)
await q.finished()
t.absent(node.table.has(b.id), 'should not have b')
})
function freePort () { function freePort () {
return new Promise(resolve => { return new Promise(resolve => {
const socket = dgram.createSocket('udp4') const socket = dgram.createSocket('udp4')

Loading…
Cancel
Save