项目作者: webtorrent

项目描述 :
高级语言: JavaScript
项目地址: git://github.com/webtorrent/bittorrent-tracker.git
创建时间: 2014-03-26T08:37:20Z
项目社区:https://github.com/webtorrent/bittorrent-tracker

开源协议:MIT License

下载


bittorrent-tracker ci npm downloads javascript style guide

Simple, robust, BitTorrent tracker (client & server) implementation

tracker visualization

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.

Tracker stats

Screenshot

install

  1. npm install bittorrent-tracker

usage

client

To connect to a tracker, just do this:

  1. import Client from 'bittorrent-tracker'
  2. const requiredOpts = {
  3. infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
  4. peerId: new Buffer('01234567890123456789'), // hex string or Buffer
  5. announce: [], // list of tracker server urls
  6. port: 6881 // torrent client port, (in browser, optional)
  7. }
  8. const optionalOpts = {
  9. // RTCPeerConnection config object (only used in browser)
  10. rtcConfig: {},
  11. // User-Agent header for http requests
  12. userAgent: '',
  13. // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
  14. wrtc: {},
  15. getAnnounceOpts: function () {
  16. // Provide a callback that will be called whenever announce() is called
  17. // internally (on timer), or by the user
  18. return {
  19. uploaded: 0,
  20. downloaded: 0,
  21. left: 0,
  22. customParam: 'blah' // custom parameters supported
  23. }
  24. },
  25. // Proxy options (used to proxy requests in node)
  26. proxyOpts: {
  27. // For WSS trackers this is always a http.Agent
  28. // For UDP trackers this is an object of options for the Socks Connection
  29. // For HTTP trackers this is either an undici Agent if using Node16 or later, or http.Agent if using versions prior to Node 16, ex:
  30. // import Socks from 'socks'
  31. // proxyOpts.socksProxy = new Socks.Agent(optionsObject, isHttps)
  32. // or if using Node 16 or later
  33. // import { socksDispatcher } from 'fetch-socks'
  34. // proxyOpts.socksProxy = socksDispatcher(optionsObject)
  35. socksProxy: new SocksProxy(socksOptionsObject),
  36. // Populated with socksProxy if it's provided
  37. httpAgent: new http.Agent(agentOptionsObject),
  38. httpsAgent: new https.Agent(agentOptionsObject)
  39. },
  40. }
  41. const client = new Client(requiredOpts)
  42. client.on('error', function (err) {
  43. // fatal client error!
  44. console.log(err.message)
  45. })
  46. client.on('warning', function (err) {
  47. // a tracker was unavailable or sent bad data to the client. you can probably ignore it
  48. console.log(err.message)
  49. })
  50. // start getting peers from the tracker
  51. client.start()
  52. client.on('update', function (data) {
  53. console.log('got an announce response from tracker: ' + data.announce)
  54. console.log('number of seeders in the swarm: ' + data.complete)
  55. console.log('number of leechers in the swarm: ' + data.incomplete)
  56. })
  57. client.once('peer', function (addr) {
  58. console.log('found a peer: ' + addr) // 85.10.239.191:48623
  59. })
  60. // announce that download has completed (and you are now a seeder)
  61. client.complete()
  62. // force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
  63. client.update()
  64. // provide parameters to the tracker
  65. client.update({
  66. uploaded: 0,
  67. downloaded: 0,
  68. left: 0,
  69. customParam: 'blah' // custom parameters supported
  70. })
  71. // stop getting peers from the tracker, gracefully leave the swarm
  72. client.stop()
  73. // ungracefully leave the swarm (without sending final 'stop' message)
  74. client.destroy()
  75. // scrape
  76. client.scrape()
  77. client.on('scrape', function (data) {
  78. console.log('got a scrape response from tracker: ' + data.announce)
  79. console.log('number of seeders in the swarm: ' + data.complete)
  80. console.log('number of leechers in the swarm: ' + data.incomplete)
  81. console.log('number of total downloads of this torrent: ' + data.downloaded)
  82. })

server

To start a BitTorrent tracker server to track swarms of peers:

  1. import { Server } from 'bittorrent-tracker'
  2. // Or import Server from 'bittorrent-tracker/server'
  3. const server = new Server({
  4. udp: true, // enable udp server? [default=true]
  5. http: true, // enable http server? [default=true]
  6. ws: true, // enable websocket server? [default=true]
  7. stats: true, // enable web-based statistics? [default=true]
  8. trustProxy: false, // enable trusting x-forwarded-for header for remote IP [default=false]
  9. filter: function (infoHash, params, cb) {
  10. // Blacklist/whitelist function for allowing/disallowing torrents. If this option is
  11. // omitted, all torrents are allowed. It is possible to interface with a database or
  12. // external system before deciding to allow/deny, because this function is async.
  13. // It is possible to block by peer id (whitelisting torrent clients) or by secret
  14. // key (private trackers). Full access to the original HTTP/UDP request parameters
  15. // are available in `params`.
  16. // This example only allows one torrent.
  17. const allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
  18. if (allowed) {
  19. // If the callback is passed `null`, the torrent will be allowed.
  20. cb(null)
  21. } else {
  22. // If the callback is passed an `Error` object, the torrent will be disallowed
  23. // and the error's `message` property will be given as the reason.
  24. cb(new Error('disallowed torrent'))
  25. }
  26. }
  27. })
  28. // Internal http, udp, and websocket servers exposed as public properties.
  29. server.http
  30. server.udp
  31. server.ws
  32. server.on('error', function (err) {
  33. // fatal server error!
  34. console.log(err.message)
  35. })
  36. server.on('warning', function (err) {
  37. // client sent bad data. probably not a problem, just a buggy client.
  38. console.log(err.message)
  39. })
  40. server.on('listening', function () {
  41. // fired when all requested servers are listening
  42. // HTTP
  43. const httpAddr = server.http.address()
  44. const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
  45. const httpPort = httpAddr.port
  46. console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)
  47. // UDP
  48. const udpAddr = server.udp.address()
  49. const udpHost = udpAddr.address
  50. const udpPort = udpAddr.port
  51. console.log(`UDP tracker: udp://${udpHost}:${udpPort}`)
  52. // WS
  53. const wsAddr = server.ws.address()
  54. const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
  55. const wsPort = wsAddr.port
  56. console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)
  57. })
  58. // start tracker server listening! Use 0 to listen on a random free port.
  59. const port = 0
  60. const hostname = "localhost"
  61. server.listen(port, hostname, () => {
  62. // Do something on listening...
  63. })
  64. // listen for individual tracker messages from peers:
  65. server.on('start', function (addr) {
  66. console.log('got start message from ' + addr)
  67. })
  68. server.on('complete', function (addr) {})
  69. server.on('update', function (addr) {})
  70. server.on('stop', function (addr) {})
  71. // get info hashes for all torrents in the tracker server
  72. Object.keys(server.torrents)
  73. // get the number of seeders for a particular torrent
  74. server.torrents[infoHash].complete
  75. // get the number of leechers for a particular torrent
  76. server.torrents[infoHash].incomplete
  77. // get the peers who are in a particular torrent swarm
  78. 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:

  1. import Client from 'bittorrent-tracker'
  2. // Or import Client from 'bittorrent-tracker/client'
  3. Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) {
  4. results[infoHash1].announce
  5. results[infoHash1].infoHash
  6. results[infoHash1].complete
  7. results[infoHash1].incomplete
  8. results[infoHash1].downloaded
  9. // ...
  10. })
  11. `

command line

Install bittorrent-tracker globally:

  1. $ npm install -g bittorrent-tracker

Easily start a tracker server:

  1. $ bittorrent-tracker
  2. http server listening on 8000
  3. udp server listening on 8000
  4. ws server listening on 8000

Lots of options:

  1. $ bittorrent-tracker --help
  2. bittorrent-tracker - Start a bittorrent tracker server
  3. Usage:
  4. bittorrent-tracker [OPTIONS]
  5. If no --http, --udp, or --ws option is supplied, all tracker types will be started.
  6. Options:
  7. -p, --port [number] change the port [default: 8000]
  8. --trust-proxy trust 'x-forwarded-for' header from reverse proxy
  9. --interval client announce interval (ms) [default: 600000]
  10. --http enable http server
  11. --udp enable udp server
  12. --ws enable websocket server
  13. -q, --quiet only show error output
  14. -s, --silent show no output
  15. -v, --version print the current version

license

MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.