2014-03-26 08:37:13 +00:00
|
|
|
# bittorrent-tracker [![build](http://img.shields.io/travis/feross/bittorrent-tracker.svg)](https://travis-ci.org/feross/bittorrent-tracker) [![npm](http://img.shields.io/npm/v/bittorrent-tracker.svg)](https://npmjs.org/package/bittorrent-tracker) [![gittip](http://img.shields.io/gittip/feross.svg)](https://www.gittip.com/feross/)
|
|
|
|
|
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.
|
|
|
|
|
2014-03-27 07:17:49 +00:00
|
|
|
Also see [bittorrent-dht](https://github.com/feross/bittorrent-dht). This module is used
|
2014-03-26 08:37:13 +00:00
|
|
|
by [WebTorrent](http://webtorrent.io).
|
|
|
|
|
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
|
|
|
|
var Client = require('bittorrent-tracker').Client
|
|
|
|
var parseTorrent = require('parse-torrent')
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
// you must add an 'error' event handler!
|
|
|
|
client.on('error', function (err) {
|
|
|
|
console.log(err.message)
|
|
|
|
// a tracker was unavailable or sent bad data to the client. you can probably ignore it
|
|
|
|
})
|
|
|
|
|
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) {
|
|
|
|
console.log('got a response from tracker: ' + data.announce)
|
|
|
|
console.log('number of seeders on this tracker: ' + data.complete)
|
|
|
|
console.log('number of leechers on this tracker: ' + data.incomplete)
|
|
|
|
})
|
|
|
|
|
|
|
|
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-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-03-27 07:36:46 +00:00
|
|
|
var server = new Server()
|
|
|
|
|
|
|
|
// you must add an 'error' event handler!
|
|
|
|
server.on('error', function (err) {
|
|
|
|
console.log(err.message)
|
|
|
|
})
|
|
|
|
|
|
|
|
server.on('listening', function () {
|
|
|
|
console.log('tracker server is listening!')
|
|
|
|
})
|
|
|
|
|
2014-03-27 07:47:12 +00:00
|
|
|
// start tracker server listening!
|
|
|
|
server.listen(port)
|
|
|
|
|
2014-03-27 07:36:46 +00:00
|
|
|
// listen for individual tracker messages from peers:
|
|
|
|
|
|
|
|
server.on('start', function (addr, params) {
|
|
|
|
console.log('got start message from ' + addr)
|
|
|
|
console.log('params in the message: ' + JSON.stringify(params))
|
|
|
|
})
|
|
|
|
|
|
|
|
server.on('complete', function (addr, params) {})
|
|
|
|
server.on('update', function (addr, params) {})
|
|
|
|
server.on('stop', function (addr, params) {})
|
|
|
|
|
|
|
|
// 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
|
|
|
```
|
|
|
|
|
|
|
|
## license
|
|
|
|
|
|
|
|
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).
|