diff --git a/index.js b/index.js index 229e2c8..e08b543 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,7 @@ class DHT extends EventEmitter { this._tickInterval = setInterval(this._ontick.bind(this), TICK_INTERVAL) this._lastTick = Date.now() this._lastHost = null + this._shouldAddNode = opts.addNode || null this._onrow = (row) => row.on('full', (node) => this._onfullrow(node, row)) this._nonePersistentSamples = [] this._bootstrapping = this._bootstrap() @@ -241,6 +242,10 @@ class DHT extends EventEmitter { } _addNodeFromNetwork (sample, from, to) { + if (this._shouldAddNode !== null && !this._shouldAddNode(from)) { + return + } + if (from.id === null) { this._sampleBootstrapMaybe(from, to) return diff --git a/test.js b/test.js index cd76329..710500b 100644 --- a/test.js +++ b/test.js @@ -311,6 +311,25 @@ test('relay', async function (t) { 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 () { return new Promise(resolve => { const socket = dgram.createSocket('udp4')