Coder Social home page Coder Social logo

simudream / bittorrent-dht Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webtorrent/bittorrent-dht

0.0 2.0 0.0 454 KB

Simple, robust, BitTorrent DHT implementation

Home Page: http://webtorrent.io

License: MIT License

JavaScript 100.00%

bittorrent-dht's Introduction

bittorrent-dht travis npm downloads

Simple, robust, BitTorrent DHT implementation

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.

features

  • complete implementation of the DHT protocol in JavaScript
  • follows the spec
  • robust and well-tested (comprehensive test suite, and used by WebTorrent and peerflix)
  • efficient recursive lookup algorithm minimizes UDP traffic
  • supports multiple, concurrent lookups using the same routing table

Also see bittorrent-tracker.

install

npm install bittorrent-dht

example

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, infoHash, from) {
  console.log('found potential peer ' + addr + ' through ' + from)
})

api

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)
}

To use dht_store, set opts.verify to an ed25519 supercop/ref10 implementation. opts.verify(signature, value, publicKey) should return a boolean whether the signature and value buffers were generated by the publicKey.

For example, for dht_store you can do:

var ed = require('ed25519-supercop')
var dht = new DHT({ verify: ed.verify })

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.put(opts, callback)

Write an arbitrary payload to the DHT. (BEP 44).

For all requests, you must specify:

  • opts.v - a buffer payload to write, no less than 1000 bytes

If you only specify opts.v, the content is considered immutable and the hash will just be the hash of the content.

Here is a simple example of creating some immutable content on the dht:

var DHT = require('bittorrent-dht')
var dht = new DHT()
var value = new Buffer(200).fill('abc')

dht.on('ready', function () {
  dht.put({ v: value }, function (errors, hash) {
    console.error('errors=', errors)
    console.log('hash=', hash)
  })
})

For mutable content, the hash will be the hash of the public key, opts.k. These options are available:

  • opts.k - ed25519 public key buffer (32 bytes) (REQUIRED)
  • opts.sign(buf) - function to generate an ed25519 signature buffer (64 bytes) corresponding to the opts.k public key (REQUIRED)
  • opts.seq - optional sequence (integer), must monotonically increase
  • opts.cas - optional previous sequence for compare-and-swap
  • opts.salt - optional salt buffer to include (< 64 bytes) when calculating the hash of the content. You can use a salt to have multiple mutable addresses for the same public key opts.k.

Note that bittorrent bep44 uses ed25519 supercop/ref10 keys, NOT nacl/sodium keys. You can use the ed25519-supercop package to generate the appropriate signatures or bittorrent-dht-store-keypair for a more convenient version.

To make a mutable update, you will need to create an elliptic key and pack values precisely according to the specification, like so:

var ed = require('ed25519-supercop')
var keypair = ed.createKeyPair(ed.createSeed())

var value = new Buffer(200).fill('whatever') // the payload you want to send
var opts = {
  k: keypair.publicKey,
  seq: 0,
  v: value,
  sign: function (buf) {
    return ed.sign(buf, keypair.publicKey, keypair.secretKey)
  }
}

var DHT = require('bittorrent-dht')
var dht = new DHT
dht.on('ready', function () {
  dht.put(opts, function (errors, hash) {
    console.error('errors=', errors)
    console.log('hash=', hash)
  })
})

In either mutable or immutable forms, callback(errors, hash) fires with an array errors of any errors encountered when announcing the content to peers and hash, the location where the mutable or immutable content can be retrieved (with dht.get(hash)).

Note that you should call .put() every hour for content that you want to keep alive, since nodes may discard data nodes older than 2 hours.

dht.get(hash, callback)

Read a data record (created with .put()) from the DHT. (BEP 44)

Given hash, a hex string or buffer, lookup data content from the DHT, sending the result in callback(err, res).

res objects are similar to the options objects written to the DHT with .put():

  • res.v - the value put in
  • res.id - the node that returned the content
  • res.k - the public key (only present for mutable data)
  • res.sig - the signature (only present for mutable data)
  • res.seq - the sequence (optional, only present for mutable data)
  • res.salt - the salt (optional, only present for mutable data)

events

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.

internal events

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.

further reading

license

MIT. Copyright (c) Feross Aboukhadijeh.

bittorrent-dht's People

Contributors

alexandergugel avatar arestov avatar bobrenjc93 avatar cjb avatar feross avatar hackerkid avatar ivantodorovich avatar luoyetx avatar mafintosh avatar mvayngrib avatar omgimalexis avatar rabidaudio avatar transitive-bullshit avatar x-oss-byte avatar xaiki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.