-
Notifications
You must be signed in to change notification settings - Fork 10
dWeb DHT Core
A DHT that supports peer revelation and distributed hole punching
npm install @dwdht/core
First run a dWeb Bootstrap Node
npm install -g @dwdht/bootstrap
dbootstrap --port=10000
var dWebDHT = require('@dwdht/core')
var a = dWebDHT({
bootstrap: ['localhost:10000']
})
var b = dWebDHT({
bootstrap: ['localhost:10000']
})
a.ready(function () {
// announce on a 32 byte key
var key = new Buffer('01234567012345670123456701234567')
b.announce(key, {port: 10000}, function (err) {
if (err) throw err
var stream = a.lookup(key)
stream.on('data', function (data) {
console.log('found peers:', data)
})
})
})
Create a new DHT. Options are passed to the @dwdht/rpc constructor
Announce that you are listening on a key. Options include
{
port: 10000, // port you are listening. If omitted the UDP sockets port is used
localAddress: {
host: '192.168.1.2', // announce that you are listening on a local address also
port: 8888
}
}
The returned stream will emit peers as they are discovered during the announce face. The data events look like this
{
node: {
host: '10.4.2.4', // DHT node's host
port: 42424 // DHT node's port
},
peers: [{
host: '4.41.3.4', // a peer host
port: 4244, // a peer port
}],
localPeers: [{
host: '192.168.3.4', // a local peer host
port: 4244, // a local peer port
}]
}
Local peers will only contain addresses that share the first two parts of your local address (192.168
in the example).
Both peers
and localPeers
will not contain your own address.
If you provide the callback the stream will be buffers and an array of results is passed. Note that you should keep announcing yourself at regular intervals (fx every 4-5min)
Find peers but do not announce. Accepts the same options as announce
and returns a similar stream.
Remove yourself from the DHT. Pass the same options as you used to announce yourself.
Ping a another DHT peer. Useful if you want to see if you can connect to a node without holepunching.
UDP holepunch to another peer. Pass the same node as was returned in the announce/lookup stream for the peer.
Wait for the dht to be fully bootstrapped. You do not need to call this before announcing / querying.
Destroy the dht and stop listening.
Re-bootstrap the DHT. Call this at regular intervals if you do not announce/lookup any keys.
Explicitly listen on a port. If you do not call this a random port will be chosen for you.
There is a command line tool available as well which is useful if you want to run a long lived DHT node on a server or similar.
npm install -g @dwdht/core
@dwdht/core --help
To run a node simply pass it the addresses of your bootstrap servers
ddht --bootstrap=localhost:10000 --bootstrap=localhost:10001