11.0.2 • Published 17 days ago

bittorrent-tracker v11.0.2

Weekly downloads
4,321
License
MIT
Repository
github
Last release
17 days ago

bittorrent-tracker ci npm downloads javascript style guide

Simple, robust, BitTorrent tracker (client & server) implementation

tracker visualization

Node.js implementation of a BitTorrent tracker, client and server.

A BitTorrent tracker is a web service which responds to requests from BitTorrent clients. The requests include metrics from clients that help the tracker keep overall statistics about the torrent. The response includes a peer list that helps the client participate in the torrent swarm.

This module is used by WebTorrent.

features

  • Includes client & server implementations
  • Supports all mainstream tracker types:
  • Supports ipv4 & ipv6
  • Supports tracker "scrape" extension
  • Robust and well-tested
  • Tracker statistics available via web interface at /stats or JSON data at /stats.json

Also see bittorrent-dht.

Tracker stats

Screenshot

install

npm install bittorrent-tracker

usage

client

To connect to a tracker, just do this:

import Client from 'bittorrent-tracker'

const requiredOpts = {
  infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
  peerId: new Buffer('01234567890123456789'), // hex string or Buffer
  announce: [], // list of tracker server urls
  port: 6881 // torrent client port, (in browser, optional)
}

const optionalOpts = {
  // RTCPeerConnection config object (only used in browser)
  rtcConfig: {},
  // User-Agent header for http requests
  userAgent: '',
  // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
  wrtc: {},
  getAnnounceOpts: function () {
    // Provide a callback that will be called whenever announce() is called
    // internally (on timer), or by the user
    return {
      uploaded: 0,
      downloaded: 0,
      left: 0,
      customParam: 'blah' // custom parameters supported
    }
  },
  // Proxy options (used to proxy requests in node)
  proxyOpts: {
      // For WSS trackers this is always a http.Agent
      // For UDP trackers this is an object of options for the Socks Connection
      // For HTTP trackers this is either an undici Agent if using Node16 or later, or http.Agent if using versions prior to Node 16, ex:
      // import Socks from 'socks'
      // proxyOpts.socksProxy = new Socks.Agent(optionsObject, isHttps)
      // or if using Node 16 or later
      // import { socksDispatcher } from 'fetch-socks'
      // proxyOpts.socksProxy = socksDispatcher(optionsObject)
      socksProxy: new SocksProxy(socksOptionsObject),
      // Populated with socksProxy if it's provided
      httpAgent: new http.Agent(agentOptionsObject),
      httpsAgent: new https.Agent(agentOptionsObject)
  },
}

const client = new Client(requiredOpts)

client.on('error', function (err) {
  // fatal client error!
  console.log(err.message)
})

client.on('warning', function (err) {
  // a tracker was unavailable or sent bad data to the client. you can probably ignore it
  console.log(err.message)
})

// start getting peers from the tracker
client.start()

client.on('update', function (data) {
  console.log('got an announce response from tracker: ' + data.announce)
  console.log('number of seeders in the swarm: ' + data.complete)
  console.log('number of leechers in the swarm: ' + data.incomplete)
})

client.once('peer', function (addr) {
  console.log('found a peer: ' + addr) // 85.10.239.191:48623
})

// announce that download has completed (and you are now a seeder)
client.complete()

// force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
client.update()

// provide parameters to the tracker
client.update({
  uploaded: 0,
  downloaded: 0,
  left: 0,
  customParam: 'blah' // custom parameters supported
})

// stop getting peers from the tracker, gracefully leave the swarm
client.stop()

// ungracefully leave the swarm (without sending final 'stop' message)
client.destroy()

// scrape
client.scrape()

client.on('scrape', function (data) {
  console.log('got a scrape response from tracker: ' + data.announce)
  console.log('number of seeders in the swarm: ' + data.complete)
  console.log('number of leechers in the swarm: ' + data.incomplete)
  console.log('number of total downloads of this torrent: ' + data.downloaded)
})

server

To start a BitTorrent tracker server to track swarms of peers:

import { Server } from 'bittorrent-tracker'

const server = new Server({
  udp: true, // enable udp server? [default=true]
  http: true, // enable http server? [default=true]
  ws: true, // enable websocket server? [default=true]
  stats: true, // enable web-based statistics? [default=true]
  trustProxy: false, // enable trusting x-forwarded-for header for remote IP [default=false]
  filter: function (infoHash, params, cb) {
    // Blacklist/whitelist function for allowing/disallowing torrents. If this option is
    // omitted, all torrents are allowed. It is possible to interface with a database or
    // external system before deciding to allow/deny, because this function is async.

    // It is possible to block by peer id (whitelisting torrent clients) or by secret
    // key (private trackers). Full access to the original HTTP/UDP request parameters
    // are available in `params`.

    // This example only allows one torrent.

    const allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
    if (allowed) {
      // If the callback is passed `null`, the torrent will be allowed.
      cb(null)
    } else {
      // If the callback is passed an `Error` object, the torrent will be disallowed
      // and the error's `message` property will be given as the reason.
      cb(new Error('disallowed torrent'))
    }
  }
})

// Internal http, udp, and websocket servers exposed as public properties.
server.http
server.udp
server.ws

server.on('error', function (err) {
  // fatal server error!
  console.log(err.message)
})

server.on('warning', function (err) {
  // client sent bad data. probably not a problem, just a buggy client.
  console.log(err.message)
})

server.on('listening', function () {
  // fired when all requested servers are listening

  // HTTP
  const httpAddr = server.http.address()
  const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
  const httpPort = httpAddr.port
  console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)

  // UDP
  const udpAddr = server.udp.address()
  const udpHost = udpAddr.address
  const udpPort = udpAddr.port
  console.log(`UDP tracker: udp://${udpHost}:${udpPort}`)

  // WS
  const wsAddr = server.ws.address()
  const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
  const wsPort = wsAddr.port
  console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)

})


// start tracker server listening! Use 0 to listen on a random free port.
const port = 0
const hostname = "localhost"
server.listen(port, hostname, () => {
  // Do something on listening...
})

// listen for individual tracker messages from peers:

server.on('start', function (addr) {
  console.log('got start message from ' + addr)
})

server.on('complete', function (addr) {})
server.on('update', function (addr) {})
server.on('stop', function (addr) {})

// get info hashes for all torrents in the tracker server
Object.keys(server.torrents)

// get the number of seeders for a particular torrent
server.torrents[infoHash].complete

// get the number of leechers for a particular torrent
server.torrents[infoHash].incomplete

// get the peers who are in a particular torrent swarm
server.torrents[infoHash].peers

The http server will handle requests for the following paths: /announce, /scrape. Requests for other paths will not be handled.

multi scrape

Scraping multiple torrent info is possible with a static Client.scrape method:

import Client from 'bittorrent-tracker'
Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) {
  results[infoHash1].announce
  results[infoHash1].infoHash
  results[infoHash1].complete
  results[infoHash1].incomplete
  results[infoHash1].downloaded

  // ...
})

command line

Install bittorrent-tracker globally:

$ npm install -g bittorrent-tracker

Easily start a tracker server:

$ bittorrent-tracker
http server listening on 8000
udp server listening on 8000
ws server listening on 8000

Lots of options:

$ bittorrent-tracker --help
  bittorrent-tracker - Start a bittorrent tracker server

  Usage:
    bittorrent-tracker [OPTIONS]

  If no --http, --udp, or --ws option is supplied, all tracker types will be started.

  Options:
    -p, --port [number]  change the port [default: 8000]
        --trust-proxy    trust 'x-forwarded-for' header from reverse proxy
        --interval       client announce interval (ms) [default: 600000]
        --http           enable http server
        --udp            enable udp server
        --ws             enable websocket server
    -q, --quiet          only show error output
    -s, --silent         show no output
    -v, --version        print the current version

license

MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.

11.0.2

17 days ago

11.0.1

2 months ago

10.0.12

8 months ago

10.0.11

8 months ago

11.0.0

5 months ago

10.0.9

10 months ago

10.0.10

10 months ago

10.0.5

10 months ago

10.0.6

10 months ago

10.0.7

10 months ago

10.0.8

10 months ago

10.0.3

10 months ago

10.0.4

10 months ago

10.0.0

1 year ago

10.0.1

1 year ago

10.0.2

1 year ago

9.19.0

2 years ago

9.18.6

2 years ago

9.18.4

2 years ago

9.18.5

2 years ago

9.18.3

2 years ago

9.18.1

3 years ago

9.18.2

3 years ago

9.18.0

3 years ago

9.17.4

3 years ago

9.17.3

3 years ago

9.17.1

3 years ago

9.17.2

3 years ago

9.17.0

3 years ago

9.16.1

3 years ago

9.16.0

3 years ago

9.15.0

4 years ago

9.14.5

4 years ago

9.14.4

5 years ago

9.14.3

5 years ago

9.14.2

5 years ago

9.14.1

5 years ago

9.14.0

5 years ago

9.13.0

5 years ago

9.12.1

5 years ago

9.11.0

5 years ago

9.10.1

6 years ago

9.10.0

6 years ago

9.9.1

6 years ago

9.9.0

6 years ago

9.8.1

6 years ago

9.8.0

6 years ago

9.7.1

6 years ago

9.7.0

6 years ago

9.6.0

6 years ago

9.5.0

6 years ago

9.4.0

6 years ago

9.3.0

6 years ago

9.2.4

6 years ago

9.2.3

7 years ago

9.2.2

7 years ago

9.2.1

7 years ago

9.2.0

7 years ago

9.1.0

7 years ago

9.0.3

7 years ago

9.0.2

7 years ago

9.0.1

7 years ago

9.0.0

7 years ago

8.6.1

7 years ago

8.6.0

7 years ago

8.5.2

7 years ago

8.5.1

7 years ago

8.5.0

7 years ago

8.4.0

7 years ago

8.3.1

7 years ago

8.3.0

7 years ago

8.2.0

7 years ago

8.1.0

7 years ago

8.0.14

7 years ago

8.0.13

7 years ago

8.0.12

7 years ago

8.0.11

8 years ago

8.0.10

8 years ago

8.0.9

8 years ago

8.0.8

8 years ago

8.0.7

8 years ago

8.0.6

8 years ago

8.0.5

8 years ago

8.0.4

8 years ago

8.0.3

8 years ago

8.0.2

8 years ago

8.0.1

8 years ago

8.0.0

8 years ago

7.7.0

8 years ago

7.6.2

8 years ago

7.6.1

8 years ago

7.6.0

8 years ago

7.5.9

8 years ago

7.5.8

8 years ago

7.5.7

8 years ago

7.5.6

8 years ago

7.5.5

8 years ago

7.5.4

8 years ago

7.5.3

8 years ago

7.5.2

8 years ago

7.5.1

8 years ago

7.5.0

8 years ago

7.4.1

8 years ago

7.4.0

8 years ago

7.3.4

8 years ago

7.3.3

8 years ago

7.3.2

8 years ago

7.3.1

8 years ago

7.3.0

8 years ago

7.2.0

8 years ago

7.1.0

8 years ago

7.0.3

8 years ago

7.0.2

8 years ago

7.0.1

8 years ago

7.0.0

8 years ago

6.3.6

8 years ago

6.3.5

8 years ago

6.3.4

8 years ago

6.3.3

8 years ago

6.3.2

8 years ago

6.3.1

8 years ago

6.3.0

8 years ago

6.2.0

8 years ago

6.1.0

8 years ago

6.0.8

8 years ago

6.0.7

8 years ago

6.0.6

8 years ago

6.0.5

9 years ago

6.0.4

9 years ago

6.0.3

9 years ago

6.0.2

9 years ago

6.0.1

9 years ago

6.0.0

9 years ago

5.3.2

9 years ago

5.3.1

9 years ago

5.3.0

9 years ago

5.2.1

9 years ago

5.2.0

9 years ago

5.1.0

9 years ago

5.0.1

9 years ago

5.0.0

9 years ago

4.5.1

9 years ago

4.5.0

9 years ago

4.4.5

9 years ago

4.4.4

9 years ago

4.4.3

9 years ago

4.4.2

9 years ago

4.4.1

9 years ago

4.4.0

9 years ago

4.3.7

9 years ago

4.3.6

9 years ago

4.3.5

9 years ago

4.3.4

9 years ago

4.3.3

9 years ago

4.3.2

9 years ago

4.3.1

9 years ago

4.3.0

9 years ago

4.2.0

9 years ago

4.1.3

9 years ago

4.1.2

9 years ago

4.1.1

9 years ago

4.1.0

9 years ago

4.0.0

9 years ago

3.6.3

9 years ago

3.6.1

9 years ago

3.6.0

9 years ago

3.5.1

9 years ago

3.5.0

9 years ago

3.4.9

9 years ago

3.4.8

9 years ago

3.4.7

9 years ago

3.4.6

9 years ago

3.4.5

9 years ago

3.4.4

9 years ago

3.4.3

9 years ago

3.4.2

9 years ago

3.4.1

9 years ago

3.4.0

9 years ago

3.3.1

9 years ago

3.3.0

9 years ago

3.2.1

9 years ago

3.2.0

9 years ago

3.1.4

9 years ago

3.1.3

9 years ago

3.1.2

9 years ago

3.1.1

9 years ago

3.1.0

9 years ago

3.0.3

9 years ago

3.0.2

9 years ago

3.0.1

9 years ago

3.0.0

9 years ago

2.12.1

9 years ago

2.12.0

9 years ago

2.11.0

9 years ago

2.10.3

9 years ago

2.10.2

9 years ago

2.10.1

9 years ago

2.10.0

9 years ago

2.9.1

9 years ago

2.9.0

9 years ago

2.8.2

9 years ago

2.8.1

9 years ago

2.8.0

9 years ago

2.7.0

9 years ago

2.6.1

10 years ago

2.6.0

10 years ago

2.5.1

10 years ago

2.5.0

10 years ago

2.4.0

10 years ago

2.2.4

10 years ago

2.2.3

10 years ago

2.2.2

10 years ago

2.2.1

10 years ago

2.2.0

10 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.0

10 years ago

1.11.0

10 years ago

1.10.0

10 years ago

1.9.0

10 years ago

1.8.0

10 years ago

1.7.0

10 years ago

1.6.0

10 years ago

1.5.0

10 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.7.3

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago