2015-01-22 14:28:06 -08:00
# bittorrent-tracker [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]
2016-01-13 23:34:49 +01:00
[travis-image]: https://img.shields.io/travis/feross/bittorrent-tracker/master.svg
2015-01-22 14:28:06 -08:00
[travis-url]: https://travis-ci.org/feross/bittorrent-tracker
2016-01-13 23:34:49 +01:00
[npm-image]: https://img.shields.io/npm/v/bittorrent-tracker.svg
2015-01-22 14:28:06 -08:00
[npm-url]: https://npmjs.org/package/bittorrent-tracker
2016-01-13 23:34:49 +01:00
[downloads-image]: https://img.shields.io/npm/dm/bittorrent-tracker.svg
2015-01-22 14:28:06 -08:00
[downloads-url]: https://npmjs.org/package/bittorrent-tracker
2014-03-26 01:37:13 -07:00
2014-03-27 00:39:14 -07:00
#### Simple, robust, BitTorrent tracker (client & server) implementation
2014-03-26 01:37:13 -07:00
2014-03-27 00:45:25 -07:00

2014-03-26 01:37:13 -07:00
Node.js implementation of a [BitTorrent tracker ](https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol ), client and server.
2015-11-21 18:22:48 -08:00
A **BitTorrent tracker** is an web service which responds to requests from BitTorrent
2014-03-26 01:37:13 -07:00
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
2015-11-21 18:22:48 -08:00
participate in the torrent swarm.
2014-03-26 01:37:13 -07:00
2015-01-25 19:17:51 -08:00
This module is used by [WebTorrent ](http://webtorrent.io ).
2014-03-26 01:37:13 -07:00
2014-03-27 21:08:20 -07:00
## features
2015-11-21 18:23:45 -08:00
- Includes client & server implementations
- Supports all mainstream tracker types:
2015-11-21 18:22:48 -08:00
- HTTP trackers
- UDP trackers ([BEP 15 ](http://www.bittorrent.org/beps/bep_0015.html ))
- WebTorrent trackers ([BEP forthcoming ](http://webtorrent.io ))
2015-11-21 18:23:45 -08:00
- Supports ipv4 & ipv6
- Supports tracker "scrape" extension
- Robust and well-tested
- Comprehensive test suite (runs entirely offline, so it's reliable)
- Used by popular clients: [WebTorrent ](http://webtorrent.io ), [peerflix ](https://github.com/mafintosh/peerflix ), and [playback ](https://mafintosh.github.io/playback/ )
2016-06-07 10:49:27 +02:00
- Tracker statistics available via web interface at `/stats` or JSON data at `/stats.json`
2014-03-27 21:08:20 -07:00
2015-01-25 19:17:51 -08:00
Also see [bittorrent-dht ](https://github.com/feross/bittorrent-dht ).
2014-03-27 00:45:25 -07:00
## install
2014-03-26 01:37:13 -07:00
```
npm install bittorrent-tracker
```
2014-03-27 00:45:25 -07:00
## usage
2014-03-27 00:39:14 -07:00
2014-03-27 00:45:25 -07:00
### client
2014-03-26 01:37:13 -07:00
To connect to a tracker, just do this:
```js
2014-06-07 15:15:00 -07:00
var Client = require('bittorrent-tracker')
2014-03-26 01:37:13 -07:00
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-03-31 21:37:51 -07:00
var 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)
}
2014-03-26 01:37:13 -07:00
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-03-31 21:37:51 -07:00
var optionalOpts = {
2016-02-29 20:17:51 -08:00
// RTCPeerConnection config object (only used in browser)
rtcConfig: {},
// custom webrtc impl, useful in node to specify [wrtc ](https://npmjs.com/package/wrtc )
wrtc: {},
2016-02-29 18:16:08 -08:00
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
}
}
}
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-03-31 21:37:51 -07:00
var client = new Client(requiredOpts)
2014-03-26 01:37:13 -07:00
client.on('error', function (err) {
2014-08-18 01:40:30 -07:00
// fatal client error!
2014-03-26 01:37:13 -07:00
console.log(err.message)
2014-08-18 01:40:30 -07:00
})
client.on('warning', function (err) {
2014-03-26 01:37:13 -07:00
// a tracker was unavailable or sent bad data to the client. you can probably ignore it
2014-08-18 01:40:30 -07:00
console.log(err.message)
2014-03-26 01:37:13 -07:00
})
2014-03-26 02:11:27 -07:00
// start getting peers from the tracker
client.start()
2014-03-26 01:37:13 -07:00
client.on('update', function (data) {
2014-05-11 17:29:12 -07: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 01:37:13 -07:00
})
client.once('peer', function (addr) {
console.log('found a peer: ' + addr) // 85.10.239.191:48623
})
2014-03-26 02:11:27 -07:00
// announce that download has completed (and you are now a seeder)
client.complete()
2014-03-26 01:37:13 -07:00
2014-03-26 02:11:27 -07:00
// force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
client.update()
2014-03-26 01:37:13 -07:00
2016-02-29 18:16:08 -08:00
// provide parameters to the tracker
client.update({
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah' // custom parameters supported
})
2014-03-26 02:11:27 -07:00
// stop getting peers from the tracker, gracefully leave the swarm
client.stop()
2014-05-11 17:29:12 -07:00
2015-01-29 14:59:08 -08:00
// ungracefully leave the swarm (without sending final 'stop' message)
client.destroy()
2014-05-11 17:29:12 -07:00
// scrape
2014-06-06 10:21:01 +02:00
client.scrape()
2014-05-11 17:29:12 -07: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)
2015-12-18 13:02:52 -02:00
console.log('number of total downloads of this torrent: ' + data.downloaded)
2014-05-11 17:29:12 -07:00
})
2014-03-26 01:37:13 -07:00
```
2014-03-27 00:45:25 -07:00
### server
2014-03-27 00:39:14 -07:00
To start a BitTorrent tracker server to track swarms of peers:
2014-03-26 01:37:13 -07:00
```js
var Server = require('bittorrent-tracker').Server
2014-05-23 18:06:29 -07:00
var server = new Server({
udp: true, // enable udp server? [default=true]
2015-01-29 12:24:17 -08:00
http: true, // enable http server? [default=true]
2015-07-29 01:47:09 -07:00
ws: true, // enable websocket server? [default=true]
2016-03-30 00:31:51 -07:00
stats: true, // enable web-based statistics? [default=true]
2015-07-04 17:15:07 -07:00
filter: function (infoHash, params, cb) {
2015-07-08 10:13:52 -07:00
// 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.
2015-02-19 12:56:09 -08:00
2015-07-08 10:13:52 -07:00
// 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
2015-11-30 22:27:59 -08:00
// are available in `params` .
2015-07-04 17:15:07 -07:00
2015-07-08 10:13:52 -07:00
// This example only allows one torrent.
var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
cb(allowed)
2015-07-08 10:32:41 -07:00
// In addition to returning a boolean (`true` for allowed, `false` for disallowed),
// you can return an `Error` object to disallow and provide a custom reason.
2016-02-12 16:07:22 +05:30
}
2014-05-23 18:06:29 -07:00
})
2014-03-27 00:36:46 -07:00
2015-04-09 22:53:03 +12:00
// Internal http, udp, and websocket servers exposed as public properties.
2015-01-29 14:59:08 -08:00
server.http
server.udp
2015-04-09 22:53:03 +12:00
server.ws
2015-01-29 14:59:08 -08:00
2014-03-27 00:36:46 -07:00
server.on('error', function (err) {
2014-05-23 18:05:18 -07: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 00:36:46 -07:00
console.log(err.message)
})
2015-01-29 14:59:08 -08: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 00:36:46 -07:00
})
2015-01-29 14:59:08 -08:00
// start tracker server listening! Use 0 to listen on a random free port.
2015-04-02 11:42:13 +13:00
server.listen(port, hostname, onlistening)
2014-03-27 00:47:12 -07:00
2014-03-27 00:36:46 -07:00
// listen for individual tracker messages from peers:
2014-07-12 18:41:08 -07:00
server.on('start', function (addr) {
2014-03-27 00:36:46 -07:00
console.log('got start message from ' + addr)
})
2014-07-12 18:41:08 -07:00
server.on('complete', function (addr) {})
server.on('update', function (addr) {})
server.on('stop', function (addr) {})
2014-03-27 00:36:46 -07: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 01:37:13 -07:00
```
2014-07-03 20:05:02 -07:00
The http server will handle requests for the following paths: `/announce` , `/scrape` . Requests for other paths will not be handled.
2016-02-10 18:47:05 -08:00
## multi scrape
Scraping multiple torrent info is possible with a static `Client.scrape` method:
```js
var Client = require('bittorrent-tracker')
Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) {
results[infoHash1].announce
results[infoHash1].infoHash
results[infoHash1].complete
results[infoHash1].incomplete
results[infoHash1].downloaded
// ...
})
````
2015-03-20 03:01:44 -07:00
## command line
2015-03-20 03:02:43 -07:00
Easily start a tracker server:
2015-03-20 03:01:44 -07:00
```sh
$ bittorrent-tracker
http server listening on 8000
udp server listening on 8000
2015-04-09 22:53:03 +12:00
ws server listening on 8000
2015-03-20 03:01:44 -07:00
```
Lots of options:
```sh
2015-03-20 03:02:43 -07:00
$ bittorrent-tracker --help
2015-03-20 03:01:44 -07:00
bittorrent-tracker - Start a bittorrent tracker server
Usage:
2015-11-21 18:00:55 -08:00
bittorrent-tracker [OPTIONS]
If no --http, --udp, or --ws option is supplied, all tracker types will be started.
2015-03-20 03:01:44 -07:00
Options:
2015-11-21 18:00:55 -08:00
-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
2015-03-20 03:01:44 -07:00
Please report bugs! https://github.com/feross/bittorrent-tracker/issues
```
2014-03-26 01:37:13 -07:00
## license
MIT. Copyright (c) [Feross Aboukhadijeh ](http://feross.org ).