Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@tradle/bittorrent-dht
Advanced tools
Node.js implementation of the BitTorrent DHT protocol. BitTorrent DHT is the main peer discovery layer for BitTorrent, which allows for trackerless torrents. DHTs are awesome!
This module is used by WebTorrent.
Also see bittorrent-tracker.
npm install bittorrent-dht
npm install magnet-uri
var DHT = require('bittorrent-dht')
var magnet = require('magnet-uri')
var uri = 'magnet:?xt=urn:btih:e3811b9539cacff680e418124272177c47477157'
var parsed = magnet(uri)
console.log(parsed.infoHash) // 'e3811b9539cacff680e418124272177c47477157'
var dht = new DHT()
dht.listen(20000, function () {
console.log('now listening')
})
dht.on('ready', function () {
// DHT is ready to use (i.e. the routing table contains at least K nodes, discovered
// via the bootstrap nodes)
// find peers for the given torrent info hash
dht.lookup(parsed.infoHash)
})
dht.on('peer', function (addr, hash, from) {
console.log('found potential peer ' + addr + ' through ' + from)
})
dht = new DHT([opts])
Create a new dht
instance.
If opts
is specified, then the default options (shown below) will be overridden.
{
nodeId: '', // 160-bit DHT node ID (Buffer or hex string, default: randomly generated)
bootstrap: [] // bootstrap servers (default: router.bittorrent.com:6881, router.utorrent.com:6881, dht.transmissionbt.com:6881)
}
dht.lookup(infoHash, [callback])
Find peers for the given info hash.
This does a recursive lookup in the DHT. Potential peers that are discovered are emitted
as peer
events. See the peer
event below for more info.
infoHash
can be a string or Buffer. callback
is called when the recursive lookup has
terminated, and is called with two paramaters. The first is an Error
or null. The second
is an array of the K closest nodes. You usually don't need to use this info and can simply
listen for peer
events.
Note: dht.lookup()
should only be called after the ready event has fired, otherwise the
lookup may fail because the DHT routing table doesn't contain enough nodes.
dht.listen([port], [address], [onlistening])
Make the DHT listen on the given port
. If port
is undefined, an available port is
automatically picked.
If address
is undefined, the DHT will try to listen on all addresses.
If onlistening
is defined, it is attached to the listening
event.
dht.address()
Returns an object containing the address information for the listening socket of the DHT.
This object contains address
, family
and port
properties.
dht.announce(infoHash, port, [callback])
Announce that the peer, controlling the querying node, is downloading a torrent on a port.
If dht.announce
is called soon (< 5 minutes) after dht.lookup
, then the routing table
generated during the lookup can be re-used, because the "tokens" sent by each node will
still be valid.
If dht.announce
is called and there is no cached routing table, then a dht.lookup
will
first be performed to discover relevant nodes and get valid "tokens" from each of them.
This will take longer.
A "token" is an opaque value that must be presented for a node to announce that its controlling peer is downloading a torrent. It must present the token received from the same queried node in a recent query for peers. This is to prevent malicious hosts from signing up other hosts for torrents. All token management is handled internally by this module.
callback
will be called when the announce operation has completed, and is called with
a single parameter that is an Error
or null.
arr = dht.toArray()
Returns the nodes in the DHT as an array. This is useful for persisting the DHT
to disk between restarts of a BitTorrent client (as recommended by the spec). Each node in the array is an object with id
(hex string) and addr
(string) properties.
To restore the DHT nodes when instantiating a new DHT
object, simply pass in the array as the value of the bootstrap
option.
var dht1 = new DHT()
// some time passes ...
// destroy the dht
var arr = dht1.toArray()
dht1.destroy()
// some time passes ...
// initialize a new dht with the same routing table as the first
var dht2 = new DHT({ bootstrap: arr })
dht.addNode(addr, [nodeId])
Manually add a node to the DHT routing table. If there is space in the routing table (or
an unresponsive node can be evicted to make space), the node will be added. If not, the
node will not be added. This is useful to call when a peer wire sends a PORT
message to
share their DHT port.
If nodeId
is undefined, then the peer will be pinged to learn their node id. If the peer does not respond, the will not be added to the routing table.
dht.destroy([callback])
Destroy the DHT. Closes the socket and cleans up large data structure resources.
dht.on('ready', function () { ... })
Emitted when the DHT is ready to handle lookups (i.e. the routing table is sufficiently populated via the bootstrap nodes).
Note: If you initialize the DHT with the { bootstrap: false }
option, then the 'ready'
event will fire on the next tick even if there are not any nodes in the routing table.
It is assumed that you will manually populate the routing table with dht.addNode
if you
pass this option.
dht.on('listening', function () { ... })
Emitted when the DHT is listening.
dht.on('peer', function (addr, infoHash, from) { ... })
Emitted when a potential peer is found. addr
is of the form IP_ADDRESS:PORT
.
infoHash
is the torrent info hash of the swarm that the peer belongs to. Emitted
in response to a lookup(infoHash)
call.
dht.on('error', function (err) { ... })
Emitted when the DHT has a fatal error.
dht.on('node', function (addr, nodeId, from) { ... })
Emitted when the DHT finds a new node.
dht.on('announce', function (addr, infoHash) { ... })
Emitted when a peer announces itself in order to be stored in the DHT.
dht.on('warning', function (err) { ... })
Emitted when the DHT gets an unexpected message from another DHT node. This is purely informational.
MIT. Copyright (c) Feross Aboukhadijeh.
FAQs
Simple, robust, BitTorrent DHT implementation
We found that @tradle/bittorrent-dht demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.