Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork326
🌊 Simple, robust, BitTorrent tracker (client & server) implementation
License
webtorrent/bittorrent-tracker
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Node.js implementation of aBitTorrent tracker, client and server.
ABitTorrent tracker is a web service which responds to requests from BitTorrentclients. The requests include metrics from clients that help the tracker keep overallstatistics about the torrent. The response includes a peer list that helps the clientparticipate in the torrent swarm.
This module is used byWebTorrent.
- Includes client & server implementations
- Supports all mainstream tracker types:
- HTTP trackers
- UDP trackers (BEP 15)
- WebTorrent trackers (BEP forthcoming)
- 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,peerflix, andplayback
- Tracker statistics available via web interface at
/stats
or JSON data at/stats.json
Also seebittorrent-dht.
npm install bittorrent-tracker
To connect to a tracker, just do this:
importClientfrom'bittorrent-tracker'constrequiredOpts={infoHash:newBuffer('012345678901234567890'),// hex string or BufferpeerId:newBuffer('01234567890123456789'),// hex string or Bufferannounce:[],// list of tracker server urlsport:6881// torrent client port, (in browser, optional)}constoptionalOpts={// RTCPeerConnection config object (only used in browser)rtcConfig:{},// User-Agent header for http requestsuserAgent:'',// Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)wrtc:{},getAnnounceOpts:function(){// Provide a callback that will be called whenever announce() is called// internally (on timer), or by the userreturn{uploaded:0,downloaded:0,left:0,customParam:'blah'// custom parameters supported}},// Proxy options (used to proxy requests in node)proxyOpts:{// For WSS trackers this is always a http.Agent// For UDP trackers this is an object of options for the Socks Connection// 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:// import Socks from 'socks'// proxyOpts.socksProxy = new Socks.Agent(optionsObject, isHttps)// or if using Node 16 or later// import { socksDispatcher } from 'fetch-socks'// proxyOpts.socksProxy = socksDispatcher(optionsObject)socksProxy:newSocksProxy(socksOptionsObject),// Populated with socksProxy if it's providedhttpAgent:newhttp.Agent(agentOptionsObject),httpsAgent:newhttps.Agent(agentOptionsObject)},}constclient=newClient(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 itconsole.log(err.message)})// start getting peers from the trackerclient.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' eventsclient.update()// provide parameters to the trackerclient.update({uploaded:0,downloaded:0,left:0,customParam:'blah'// custom parameters supported})// stop getting peers from the tracker, gracefully leave the swarmclient.stop()// ungracefully leave the swarm (without sending final 'stop' message)client.destroy()// scrapeclient.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)})
To start a BitTorrent tracker server to track swarms of peers:
import{Server}from'bittorrent-tracker'// Or import Server from 'bittorrent-tracker/server'constserver=newServer({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]trustProxy:false,// enable trusting x-forwarded-for header for remote IP [default=false]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.constallowed=(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(newError('disallowed torrent'))}}})// Internal http, udp, and websocket servers exposed as public properties.server.httpserver.udpserver.wsserver.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// HTTPconsthttpAddr=server.http.address()consthttpHost=httpAddr.address!=='::' ?httpAddr.address :'localhost'consthttpPort=httpAddr.portconsole.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)// UDPconstudpAddr=server.udp.address()constudpHost=udpAddr.addressconstudpPort=udpAddr.portconsole.log(`UDP tracker: udp://${udpHost}:${udpPort}`)// WSconstwsAddr=server.ws.address()constwsHost=wsAddr.address!=='::' ?wsAddr.address :'localhost'constwsPort=wsAddr.portconsole.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)})// start tracker server listening! Use 0 to listen on a random free port.constport=0consthostname="localhost"server.listen(port,hostname,()=>{// Do something on listening...})// 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 serverObject.keys(server.torrents)// get the number of seeders for a particular torrentserver.torrents[infoHash].complete// get the number of leechers for a particular torrentserver.torrents[infoHash].incomplete// get the peers who are in a particular torrent swarmserver.torrents[infoHash].peers
The http server will handle requests for the following paths:/announce
,/scrape
. Requests for other paths will not be handled.
Scraping multiple torrent info is possible with a staticClient.scrape
method:
importClientfrom'bittorrent-tracker'// Or import Client from 'bittorrent-tracker/client'Client.scrape({announce:announceUrl,infoHash:[infoHash1,infoHash2]},function(err,results){results[infoHash1].announceresults[infoHash1].infoHashresults[infoHash1].completeresults[infoHash1].incompleteresults[infoHash1].downloaded// ...})
Installbittorrent-tracker
globally:
$ npm install -g bittorrent-tracker
Easily start a tracker server:
$ bittorrent-trackerhttp server listening on 8000udp server listening on 8000ws 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] --httpenable http server --udpenable udp server --wsenable websocket server -q, --quiet only show error output -s, --silent show no output -v, --version print the current version
MIT. Copyright (c)Feross Aboukhadijeh andWebTorrent, LLC.
About
🌊 Simple, robust, BitTorrent tracker (client & server) implementation
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.