diff --git a/index.js b/index.js index e230b9e6..ad717af8 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const Persistent = require('./lib/persistent') const Router = require('./lib/router') const Server = require('./lib/server') const connect = require('./lib/connect') -const { FIREWALL, BOOTSTRAP_NODES, COMMANDS } = require('./lib/constants') +const { FIREWALL, BOOTSTRAP_NODES, KNOWN_NODES, COMMANDS } = require('./lib/constants') const { hash, createKeyPair } = require('./lib/crypto') const { decode } = require('hypercore-id-encoding') const RawStreamSet = require('./lib/raw-stream-set') @@ -21,8 +21,11 @@ class HyperDHT extends DHT { constructor (opts = {}) { const port = opts.port || 49737 const bootstrap = opts.bootstrap || BOOTSTRAP_NODES + const knownNodes = opts.knownNodes || KNOWN_NODES - super({ ...opts, port, bootstrap, addNode }) + super({ ...opts, port, bootstrap, filterNode }) + + for (const node of knownNodes) this.addNode(node) const { router, persistent } = defaultCacheOpts(opts) @@ -543,7 +546,7 @@ function toRange (n) { return typeof n === 'number' ? [n, n] : n } -function addNode (node) { +function filterNode (node) { // always skip these testnet nodes that got mixed in by accident, until they get updated return !(node.port === 49738 && (node.host === '134.209.28.98' || node.host === '167.99.142.185')) && !(node.port === 9400 && node.host === '35.233.47.252') && !(node.host === '150.136.142.116') diff --git a/lib/constants.js b/lib/constants.js index 1981801d..d1948371 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -19,6 +19,8 @@ exports.BOOTSTRAP_NODES = [ '138.68.147.8@node3.hyperdht.org:49737' ] +exports.KNOWN_NODES = global.Pear?.config.dht || [] + exports.FIREWALL = { UNKNOWN: 0, OPEN: 1, diff --git a/test/connections.js b/test/connections.js index adb7c6f4..1adb25a1 100644 --- a/test/connections.js +++ b/test/connections.js @@ -788,3 +788,18 @@ test('fail to bootstrap completely', async function (t) { await a.destroy() }) + +test('Populate DHT with options.knownNodes', async function (t) { + const a = new DHT({ bootstrap: [] }) + await a.ready() + const knownNodes = [{ host: '127.0.0.1', port: a.address().port }] + + const b = new DHT({ knownNodes, bootstrap: [] }) + await b.ready() + + t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }]) + + a.destroy() + b.destroy() + delete global.Pear +})