bittorrent-tracker
![javascript style guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)
Simple, robust, ZeroNet-style BitTorrent tracker (client & server) implementation
Adding support for trackers on the ZeroNet Protocol and onion addresses
![tracker](https://raw.githubusercontent.com/webtorrent/bittorrent-tracker/master/img.png)
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.
install
npm install bittorrent-tracker
usage
client
To connect to a tracker, just do this:
var Client = require('bittorrent-tracker')
var requiredOpts = {
infoHash: new Buffer('012345678901234567890'),
peerId: new Buffer('01234567890123456789'),
announce: [],
port: 6881
}
var optionalOpts = {
getAnnounceOpts: function () {
return {
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
}
}
rtcConfig: {},
userAgent: '',
wrtc: {},
}
var client = new Client(requiredOpts)
client.on('error', function (err) {
console.log(err.message)
})
client.on('warning', function (err) {
console.log(err.message)
})
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)
})
client.complete()
client.update()
client.update({
uploaded: 0,
downloaded: 0,
left: 0,
customParam: 'blah'
})
client.stop()
client.destroy()
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:
var Server = require('bittorrent-tracker').Server
var server = new Server({
udp: true,
http: true,
ws: true,
stats: true,
filter: function (infoHash, params, cb) {
var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
if (allowed) {
cb(null)
} else {
cb(new Error('disallowed torrent'))
}
}
})
server.http
server.udp
server.ws
server.on('error', function (err) {
console.log(err.message)
})
server.on('warning', function (err) {
console.log(err.message)
})
server.on('listening', function () {
console.log('listening on http port:' + server.http.address().port)
console.log('listening on udp port:' + server.udp.address().port)
})
server.listen(port, hostname, onlistening)
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) {})
Object.keys(server.torrents)
server.torrents[infoHash].complete
server.torrents[infoHash].incomplete
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:
var Client = require('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.