2015-01-22 22:28:06 +00:00
# bittorrent-tracker [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]
[travis-image]: https://img.shields.io/travis/feross/bittorrent-tracker.svg?style=flat
[travis-url]: https://travis-ci.org/feross/bittorrent-tracker
[npm-image]: https://img.shields.io/npm/v/bittorrent-tracker.svg?style=flat
[npm-url]: https://npmjs.org/package/bittorrent-tracker
[downloads-image]: https://img.shields.io/npm/dm/bittorrent-tracker.svg?style=flat
[downloads-url]: https://npmjs.org/package/bittorrent-tracker
2014-03-26 08:37:13 +00:00
2014-03-27 07:39:14 +00:00
#### Simple, robust, BitTorrent tracker (client & server) implementation
2014-03-26 08:37:13 +00:00
2014-03-27 07:45:25 +00:00
![tracker ](https://raw.githubusercontent.com/feross/bittorrent-tracker/master/img.png )
2014-03-26 08:37:13 +00:00
Node.js implementation of a [BitTorrent tracker ](https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol ), client and server.
A **BitTorrent tracker** is an HTTP service which responds to GET 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.
2015-01-26 03:17:51 +00:00
This module is used by [WebTorrent ](http://webtorrent.io ).
2014-03-26 08:37:13 +00:00
2014-03-28 04:08:20 +00:00
## features
2015-01-26 03:16:46 +00:00
- includes client & server implementations
- supports HTTP & UDP trackers ([BEP 15](http://www.bittorrent.org/beps/bep_0015.html))
- supports tracker "scrape" extension
- robust and well-tested (comprehensive test suite, and used by [WebTorrent ](http://webtorrent.io ) and [peerflix ](https://github.com/mafintosh/peerflix ))
- supports ipv4 & ipv6
2014-03-28 04:08:20 +00:00
2015-01-26 03:17:51 +00:00
Also see [bittorrent-dht ](https://github.com/feross/bittorrent-dht ).
2014-03-27 07:45:25 +00:00
## install
2014-03-26 08:37:13 +00:00
```
npm install bittorrent-tracker
```
2014-03-27 07:45:25 +00:00
## usage
2014-03-27 07:39:14 +00:00
2014-03-27 07:45:25 +00:00
### client
2014-03-26 08:37:13 +00:00
To connect to a tracker, just do this:
```js
2014-06-07 22:15:00 +00:00
var Client = require('bittorrent-tracker')
2014-03-26 08:37:13 +00:00
var parseTorrent = require('parse-torrent')
2014-06-06 08:21:01 +00:00
var fs = require('fs')
2014-03-26 08:37:13 +00:00
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] }
var peerId = new Buffer('01234567890123456789')
var port = 6881
var client = new Client(peerId, port, parsedTorrent)
client.on('error', function (err) {
2014-08-18 08:40:30 +00:00
// fatal client error!
2014-03-26 08:37:13 +00:00
console.log(err.message)
2014-08-18 08:40:30 +00:00
})
client.on('warning', function (err) {
2014-03-26 08:37:13 +00:00
// a tracker was unavailable or sent bad data to the client. you can probably ignore it
2014-08-18 08:40:30 +00:00
console.log(err.message)
2014-03-26 08:37:13 +00:00
})
2014-03-26 09:11:27 +00:00
// start getting peers from the tracker
client.start()
2014-03-26 08:37:13 +00:00
client.on('update', function (data) {
2014-05-12 00:29:12 +00:00
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)
2014-03-26 08:37:13 +00:00
})
client.once('peer', function (addr) {
console.log('found a peer: ' + addr) // 85.10.239.191:48623
})
2014-03-26 09:11:27 +00:00
// announce that download has completed (and you are now a seeder)
client.complete()
2014-03-26 08:37:13 +00:00
2014-03-26 09:11:27 +00:00
// force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
client.update()
2014-03-26 08:37:13 +00:00
2014-03-26 09:11:27 +00:00
// stop getting peers from the tracker, gracefully leave the swarm
client.stop()
2014-05-12 00:29:12 +00:00
2015-01-29 22:59:08 +00:00
// ungracefully leave the swarm (without sending final 'stop' message)
client.destroy()
2014-05-12 00:29:12 +00:00
// scrape
2014-06-06 08:21:01 +00:00
client.scrape()
2014-05-12 00:29:12 +00:00
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.incomplete)
})
2014-03-26 08:37:13 +00:00
```
2014-03-27 07:45:25 +00:00
### server
2014-03-27 07:39:14 +00:00
To start a BitTorrent tracker server to track swarms of peers:
2014-03-26 08:37:13 +00:00
```js
var Server = require('bittorrent-tracker').Server
2014-05-24 01:06:29 +00:00
var server = new Server({
udp: true, // enable udp server? [default=true]
2015-01-29 20:24:17 +00:00
http: true, // enable http server? [default=true]
2015-04-09 10:53:03 +00:00
ws: true, // enable websocket server? [default=false]
2015-02-19 20:56:09 +00:00
filter: function (infoHash, params) {
2015-01-29 22:59:08 +00:00
// black/whitelist for disallowing/allowing torrents [default=allow all]
// this example only allows this one torrent
2015-02-19 20:56:09 +00:00
return infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa'
2015-02-19 17:01:46 +00:00
// you can also block by peer id (whitelisting torrent clients) or by
2015-02-19 20:56:09 +00:00
// secret key, as you get full access to the original http GET
// request parameters in `params`
2015-01-29 20:24:17 +00:00
})
2014-05-24 01:06:29 +00:00
})
2014-03-27 07:36:46 +00:00
2015-04-09 10:53:03 +00:00
// Internal http, udp, and websocket servers exposed as public properties.
2015-01-29 22:59:08 +00:00
server.http
server.udp
2015-04-09 10:53:03 +00:00
server.ws
2015-01-29 22:59:08 +00:00
2014-03-27 07:36:46 +00:00
server.on('error', function (err) {
2014-05-24 01:05:18 +00:00
// fatal server error!
console.log(err.message)
})
server.on('warning', function (err) {
// client sent bad data. probably not a problem, just a buggy client.
2014-03-27 07:36:46 +00:00
console.log(err.message)
})
2015-01-29 22:59:08 +00:00
server.on('listening', function () {
// fired when all requested servers are listening
console.log('listening on http port:' + server.http.address().port)
console.log('listening on udp port:' + server.udp.address().port)
2014-03-27 07:36:46 +00:00
})
2015-01-29 22:59:08 +00:00
// start tracker server listening! Use 0 to listen on a random free port.
2015-04-01 22:42:13 +00:00
server.listen(port, hostname, onlistening)
2014-03-27 07:47:12 +00:00
2014-03-27 07:36:46 +00:00
// listen for individual tracker messages from peers:
2014-07-13 01:41:08 +00:00
server.on('start', function (addr) {
2014-03-27 07:36:46 +00:00
console.log('got start message from ' + addr)
})
2014-07-13 01:41:08 +00:00
server.on('complete', function (addr) {})
server.on('update', function (addr) {})
server.on('stop', function (addr) {})
2014-03-27 07:36:46 +00:00
// 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
2014-03-26 08:37:13 +00:00
```
2014-07-04 03:05:02 +00:00
The http server will handle requests for the following paths: `/announce` , `/scrape` . Requests for other paths will not be handled.
2015-03-20 10:01:44 +00:00
## command line
2015-03-20 10:02:43 +00:00
Easily start a tracker server:
2015-03-20 10:01:44 +00:00
```sh
$ bittorrent-tracker
http server listening on 8000
udp server listening on 8000
2015-04-09 10:53:03 +00:00
ws server listening on 8000
2015-03-20 10:01:44 +00:00
```
Lots of options:
```sh
2015-03-20 10:02:43 +00:00
$ bittorrent-tracker --help
2015-03-20 10:01:44 +00:00
bittorrent-tracker - Start a bittorrent tracker server
Usage:
bittorrent-tracker
Options:
-p, --port [number] change the port [default: 8000]
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
--interval tell clients to announce on this interval (ms)
--http enable http server [default: true]
--udp enable udp server [default: true]
2015-04-09 10:53:03 +00:00
--ws enable ws server [default: false]
2015-03-20 10:01:44 +00:00
-q, --quiet only show error output
-s, --silent show no output
-v, --version print the current version
Please report bugs! https://github.com/feross/bittorrent-tracker/issues
```
2014-03-26 08:37:13 +00:00
## license
MIT. Copyright (c) [Feross Aboukhadijeh ](http://feross.org ).