# zeronet-tracker

> Simple, robust, ZeroNet tracker (client & server) implementation

Latest version **0.1.1-zn** (published 2018-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install zeronet-tracker
pnpm add zeronet-tracker
yarn add zeronet-tracker
bun add zeronet-tracker
```

Provides the command `zeronet-tracker`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1-zn |
| Published | 2018-01-26 |
| First published | 2017-09-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 27 |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| Author | Maciej Krüger |
| Maintainers | mkg20001 |
| Keywords | bittorrent, p2p, peer, peer-to-peer, stream, tracker, zeronet |

## Links

- npm: https://www.npmjs.com/package/zeronet-tracker
- Repository: https://github.com/ZeroNetJS/zeronet-tracker
- Homepage: https://github.com/ZeroNetJS/zeronet-tracker#readme
- Issues: https://github.com/ZeroNetJS/zeronet-tracker/issues
- npm.io page: https://npm.io/package/zeronet-tracker

## Dependencies (27)

- [ip](https://npm.io/package/ip.md) ^1.1.5
- [ws](https://npm.io/package/ws.md) ^4.0.0
- [lru](https://npm.io/package/lru.md) ^3.1.0
- [once](https://npm.io/package/once.md) ^1.4.0
- [uniq](https://npm.io/package/uniq.md) ^1.0.1
- [bn.js](https://npm.io/package/bn.js.md) ^4.11.8
- [clone](https://npm.io/package/clone.md) ^2.1.1
- [debug](https://npm.io/package/debug.md) ^3.1.0
- [xtend](https://npm.io/package/xtend.md) ^4.0.1
- [bencode](https://npm.io/package/bencode.md) ^1.0.0
- [inherits](https://npm.io/package/inherits.md) ^2.0.3
- [minimist](https://npm.io/package/minimist.md) ^1.2.0
- [libp2p-tcp](https://npm.io/package/libp2p-tcp.md) ^0.11.2
- [run-series](https://npm.io/package/run-series.md) ^1.1.4
- [simple-get](https://npm.io/package/simple-get.md) ^2.7.0
- [randombytes](https://npm.io/package/randombytes.md) ^2.0.6
- [safe-buffer](https://npm.io/package/safe-buffer.md) ^5.1.1
- [simple-peer](https://npm.io/package/simple-peer.md) ^8.2.0
- [run-parallel](https://npm.io/package/run-parallel.md) ^1.1.6
- [zeronet-swarm](https://npm.io/package/zeronet-swarm.md) 0.2.0
- [compact2string](https://npm.io/package/compact2string.md) ^1.4.0
- [random-iterate](https://npm.io/package/random-iterate.md) ^1.0.1
- [string2compact](https://npm.io/package/string2compact.md) ^1.2.2
- [simple-websocket](https://npm.io/package/simple-websocket.md) ^5.1.1
- [bittorrent-peerid](https://npm.io/package/bittorrent-peerid.md) ^1.2.0
- [zeronet-fileserver](https://npm.io/package/zeronet-fileserver.md) 0.0.7
- [unordered-array-remove](https://npm.io/package/unordered-array-remove.md) ^1.0.2

## Recent versions

- 0.1.1-zn (latest) — 2018-01-26
- 0.1.0 — 2017-12-27
- 0.0.0 — 2017-09-16

## README

# zeronet-tracker [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url]

[travis-image]: https://img.shields.io/travis/ZeroNetJS/zeronet-tracker/master.svg
[travis-url]: https://travis-ci.org/ZeroNetJS/zeronet-tracker
[npm-image]: https://img.shields.io/npm/v/zeronet-tracker.svg
[npm-url]: https://npmjs.org/package/zeronet-tracker
[downloads-image]: https://img.shields.io/npm/dm/zeronet-tracker.svg
[downloads-url]: https://npmjs.org/package/zeronet-tracker
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com
[greenkeeper-image]: https://badges.greenkeeper.io/zeronetjs/zeronet-tracker.svg
[greenkeeper-url]: https://greenkeeper.io/

#### Simple, robust, ZeroNet tracker (client & server) implementation

![tracker](https://raw.githubusercontent.com/ZeroNetJS/zeronet-tracker/master/img.png)

Node.js implementation of a ZeroNet Tracker (which is a [BitTorrent tracker](https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol) that has support for the [ZeroProtocol](https://zeronet.readthedocs.io/en/latest/help_zeronet/network_protocol/) and .onion addresses)

A **ZeroNet tracker** is a web service which responds to requests from ZeroNet
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 [ZeroNetJS](https://zeronetjs.github.io).

## features

- Includes client & server implementations
- Supports all mainstream tracker types:
  - HTTP trackers
  - UDP trackers ([BEP 15](http://www.bittorrent.org/beps/bep_0015.html))
  - WebTorrent trackers ([BEP forthcoming](http://webtorrent.io))
  - Zero trackers ([No spec, just code](https://github.com/HelloZeroNet/ZeroNet/blob/master/plugins/disabled-Bootstrapper/BootstrapperPlugin.py))
- Supports ipv4 & ipv6 & onion
- Supports tracker "scrape" extension
- Robust and well-tested
  - Comprehensive test suite (runs entirely offline, so it's reliable)
- Tracker statistics available via web interface at `/stats` or JSON data at `/stats.json`

Also see [zeronet-swarm](https://www.npmjs.com/package/zeronet-swarm).

### Tracker stats

![Screenshot](trackerStats.png)

## install

```
npm install zeronet-tracker
```

## usage

### client

To connect to a tracker, just do this:

```js
var Client = require('zeronet-tracker')

var requiredOpts = {
  infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
  peerId: new Buffer('01234567890123456789'), // hex string or Buffer
  announce: [], // list of tracker server urls
  // zswarm: swarm optional zeronet swarm (will be created if empty)
  port: 6881 // torrent client port, (in browser, optional)
}

var optionalOpts = {
  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
    }
  }
  // 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: {},
}

var 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:

```js
var Server = require('zeronet-tracker').Server

var 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]
  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.

    var 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
  console.log('listening on http port:' + server.http.address().port)
  console.log('listening on udp port:' + server.udp.address().port)
})

// start tracker server listening! Use 0 to listen on a random free port.
server.listen(port, hostname, onlistening)

// 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:

```js
var Client = require('zeronet-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 `zeronet-tracker` globally:

```sh
$ npm install -g zeronet-tracker
```

Easily start a tracker server:

```sh
$ zeronet-tracker
http server listening on 8000
udp server listening on 8000
ws server listening on 8000
```

Lots of options:

```sh
$ zeronet-tracker --help
  zeronet-tracker - Start a bittorrent tracker server

  Usage:
    zeronet-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](https://feross.org), [WebTorrent, LLC](https://webtorrent.io) and [Maciej Krüger](https://mkg20001.github.io).

---
_Source: https://npm.io/package/zeronet-tracker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
