2014-06-07 22:10:12 +00:00
|
|
|
module.exports = Server
|
|
|
|
|
2014-06-12 00:57:19 +00:00
|
|
|
var bencode = require('bencode')
|
2014-08-02 20:43:06 +00:00
|
|
|
var debug = require('debug')('bittorrent-tracker')
|
2014-06-07 22:10:12 +00:00
|
|
|
var dgram = require('dgram')
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
var http = require('http')
|
|
|
|
var inherits = require('inherits')
|
2014-12-10 15:47:41 +00:00
|
|
|
var series = require('run-series')
|
2014-12-10 15:51:35 +00:00
|
|
|
var string2compact = require('string2compact')
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
var common = require('./lib/common')
|
|
|
|
var Swarm = require('./lib/swarm')
|
2014-12-09 23:44:45 +00:00
|
|
|
var parseHttpRequest = require('./lib/parse_http')
|
|
|
|
var parseUdpRequest = require('./lib/parse_udp')
|
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
inherits(Server, EventEmitter)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A BitTorrent tracker 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.
|
|
|
|
*
|
|
|
|
* @param {Object} opts options
|
|
|
|
* @param {Number} opts.interval interval in ms that clients should announce on
|
|
|
|
* @param {Number} opts.trustProxy Trust 'x-forwarded-for' header from reverse proxy
|
|
|
|
* @param {boolean} opts.http Start an http server? (default: true)
|
|
|
|
* @param {boolean} opts.udp Start a udp server? (default: true)
|
|
|
|
*/
|
|
|
|
function Server (opts) {
|
|
|
|
var self = this
|
|
|
|
if (!(self instanceof Server)) return new Server(opts)
|
|
|
|
EventEmitter.call(self)
|
|
|
|
opts = opts || {}
|
|
|
|
|
|
|
|
self._intervalMs = opts.interval
|
2015-01-05 18:55:58 +00:00
|
|
|
? opts.interval
|
|
|
|
: 10 * 60 * 1000 // 10 min
|
2014-06-07 22:10:12 +00:00
|
|
|
|
|
|
|
self._trustProxy = !!opts.trustProxy
|
2015-01-29 22:59:08 +00:00
|
|
|
if (typeof opts.filter === 'function') self._filter = opts.filter
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2014-08-17 02:05:56 +00:00
|
|
|
self.listening = false
|
2014-06-07 22:10:12 +00:00
|
|
|
self.torrents = {}
|
|
|
|
|
|
|
|
// default to starting an http server unless the user explictly says no
|
|
|
|
if (opts.http !== false) {
|
2015-01-29 22:59:08 +00:00
|
|
|
self.http = http.createServer()
|
|
|
|
self.http.on('request', self.onHttpRequest.bind(self))
|
|
|
|
self.http.on('error', self._onError.bind(self))
|
|
|
|
self.http.on('listening', onListening)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default to starting a udp server unless the user explicitly says no
|
|
|
|
if (opts.udp !== false) {
|
2015-01-29 22:59:08 +00:00
|
|
|
self.udp = dgram.createSocket('udp4')
|
|
|
|
self.udp.on('message', self.onUdpRequest.bind(self))
|
|
|
|
self.udp.on('error', self._onError.bind(self))
|
|
|
|
self.udp.on('listening', onListening)
|
2014-08-07 05:35:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 22:59:08 +00:00
|
|
|
var num = !!self.http + !!self.udp
|
2014-08-07 05:35:43 +00:00
|
|
|
function onListening () {
|
|
|
|
num -= 1
|
2014-08-17 02:05:56 +00:00
|
|
|
if (num === 0) {
|
|
|
|
self.listening = true
|
2015-01-29 22:59:08 +00:00
|
|
|
self.emit('listening')
|
2014-08-17 02:05:56 +00:00
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 22:28:23 +00:00
|
|
|
Server.prototype._onError = function (err) {
|
|
|
|
var self = this
|
|
|
|
self.emit('error', err)
|
|
|
|
}
|
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
Server.prototype.listen = function (port, onlistening) {
|
|
|
|
var self = this
|
2014-08-17 02:05:56 +00:00
|
|
|
if (typeof port === 'function') {
|
|
|
|
onlistening = port
|
|
|
|
port = undefined
|
|
|
|
}
|
|
|
|
if (self.listening) throw new Error('server already listening')
|
2014-08-07 05:35:43 +00:00
|
|
|
if (onlistening) self.once('listening', onlistening)
|
2014-08-17 02:05:56 +00:00
|
|
|
|
2015-01-29 22:59:08 +00:00
|
|
|
if (!port) port = 0
|
2014-08-17 02:05:56 +00:00
|
|
|
|
2015-01-29 22:59:08 +00:00
|
|
|
// ATTENTION:
|
|
|
|
// binding to :: only receives IPv4 connections if the bindv6only
|
|
|
|
// sysctl is set 0, which is the default on many operating systems.
|
|
|
|
self.http && self.http.listen(port.http || port, '::')
|
|
|
|
self.udp && self.udp.bind(port.udp || port)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.close = function (cb) {
|
|
|
|
var self = this
|
2015-01-29 22:59:08 +00:00
|
|
|
self.listening = false
|
2014-06-07 22:10:12 +00:00
|
|
|
cb = cb || function () {}
|
2015-01-29 22:59:08 +00:00
|
|
|
if (self.udp) {
|
|
|
|
self.udp.close()
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
2015-01-29 22:59:08 +00:00
|
|
|
if (self.http) {
|
|
|
|
self.http.close(cb)
|
2014-06-07 22:10:12 +00:00
|
|
|
} else {
|
|
|
|
cb(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
Server.prototype.getSwarm = function (infoHash) {
|
2014-07-11 05:05:56 +00:00
|
|
|
var self = this
|
2014-12-12 10:02:11 +00:00
|
|
|
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
2015-01-29 22:59:08 +00:00
|
|
|
if (self._filter && !self._filter(infoHash)) return null
|
2014-12-12 10:02:11 +00:00
|
|
|
var swarm = self.torrents[infoHash]
|
|
|
|
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this)
|
2014-06-07 22:10:12 +00:00
|
|
|
return swarm
|
|
|
|
}
|
|
|
|
|
2015-02-04 19:35:52 +00:00
|
|
|
Server.prototype.onHttpRequest = function (req, res, options) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-02-04 19:35:52 +00:00
|
|
|
options = options || {}
|
|
|
|
options.trustProxy = options.trustProxy || self._trustProxy
|
2014-12-12 01:55:40 +00:00
|
|
|
|
2014-12-08 22:42:05 +00:00
|
|
|
var params
|
|
|
|
try {
|
2015-02-04 19:35:52 +00:00
|
|
|
params = parseHttpRequest(req, options)
|
2015-02-04 19:34:46 +00:00
|
|
|
params.httpReq = req
|
|
|
|
params.httpRes = res
|
2014-12-08 22:42:05 +00:00
|
|
|
} catch (err) {
|
2014-12-12 01:55:40 +00:00
|
|
|
debug('sent error %s', err.message)
|
2014-12-08 22:42:05 +00:00
|
|
|
res.end(bencode.encode({
|
2014-12-12 01:55:40 +00:00
|
|
|
'failure reason': err.message
|
2014-12-08 22:42:05 +00:00
|
|
|
}))
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2014-12-08 22:42:05 +00:00
|
|
|
// even though it's an error for the client, it's just a warning for the server.
|
|
|
|
// don't crash the server because a client sent bad data :)
|
2014-12-12 01:55:40 +00:00
|
|
|
self.emit('warning', err)
|
2014-12-12 10:02:11 +00:00
|
|
|
|
2014-12-08 22:42:05 +00:00
|
|
|
return
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
this._onRequest(params, function (err, response) {
|
|
|
|
if (err) {
|
2014-12-11 15:24:33 +00:00
|
|
|
self.emit('warning', err)
|
2014-12-09 01:35:05 +00:00
|
|
|
response = {
|
|
|
|
'failure reason': err.message
|
2014-11-26 07:27:02 +00:00
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 22:18:20 +00:00
|
|
|
delete response.action // only needed for UDP encoding
|
2014-06-12 00:57:19 +00:00
|
|
|
res.end(bencode.encode(response))
|
2015-01-29 22:59:08 +00:00
|
|
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
self.emit(common.EVENT_NAMES[params.event], params.addr)
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 23:11:59 +00:00
|
|
|
Server.prototype.onUdpRequest = function (msg, rinfo) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
|
|
|
|
2014-12-08 23:22:36 +00:00
|
|
|
var params
|
|
|
|
try {
|
|
|
|
params = parseUdpRequest(msg, rinfo)
|
|
|
|
} catch (err) {
|
2014-12-09 01:35:05 +00:00
|
|
|
self.emit('warning', err)
|
|
|
|
// Do not reply for parsing errors
|
|
|
|
return
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
// Handle
|
|
|
|
this._onRequest(params, function (err, response) {
|
|
|
|
if (err) {
|
2014-12-11 15:24:33 +00:00
|
|
|
self.emit('warning', err)
|
2014-12-09 01:35:05 +00:00
|
|
|
response = {
|
2015-01-29 22:59:08 +00:00
|
|
|
action: common.ACTIONS.ERROR,
|
2014-12-09 01:35:05 +00:00
|
|
|
'failure reason': err.message
|
2014-11-26 07:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 22:59:08 +00:00
|
|
|
if (!self.listening) return
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
response.transactionId = params.transactionId
|
|
|
|
response.connectionId = params.connectionId
|
2015-01-29 22:59:08 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
var buf = makeUdpPacket(response)
|
2015-01-29 22:59:08 +00:00
|
|
|
self.udp.send(buf, 0, buf.length, rinfo.port, rinfo.address)
|
|
|
|
|
|
|
|
if (params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
self.emit(common.EVENT_NAMES[params.event], params.addr)
|
|
|
|
}
|
2014-12-09 01:35:05 +00:00
|
|
|
})
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
Server.prototype._onRequest = function (params, cb) {
|
|
|
|
if (params && params.action === common.ACTIONS.CONNECT) {
|
|
|
|
cb(null, { action: common.ACTIONS.CONNECT })
|
|
|
|
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
|
|
|
this._onAnnounce(params, cb)
|
|
|
|
} else if (params && params.action === common.ACTIONS.SCRAPE) {
|
|
|
|
this._onScrape(params, cb)
|
|
|
|
} else {
|
|
|
|
cb(new Error('Invalid action'))
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 07:27:02 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
Server.prototype._onAnnounce = function (params, cb) {
|
|
|
|
var self = this
|
2014-12-09 22:18:47 +00:00
|
|
|
var swarm = self.getSwarm(params.info_hash)
|
2015-01-29 22:59:08 +00:00
|
|
|
if (swarm === null) return cb(new Error('disallowed info_hash'))
|
|
|
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
2014-12-10 15:47:41 +00:00
|
|
|
swarm.announce(params, function (err, response) {
|
|
|
|
if (response) {
|
|
|
|
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
2015-01-05 18:55:58 +00:00
|
|
|
if (!response.interval) response.interval = Math.ceil(self._intervalMs / 1000)
|
2014-12-13 22:35:29 +00:00
|
|
|
|
2014-12-10 15:51:35 +00:00
|
|
|
if (params.compact === 1) {
|
2014-12-12 23:09:40 +00:00
|
|
|
var peers = response.peers
|
|
|
|
// Find IPv4 peers
|
|
|
|
response.peers = string2compact(peers.filter(function (peer) {
|
|
|
|
return common.IPV4_RE.test(peer.ip)
|
|
|
|
}).map(function (peer) {
|
|
|
|
return peer.ip + ':' + peer.port
|
|
|
|
}))
|
|
|
|
// Find IPv6 peers
|
|
|
|
response.peers6 = string2compact(peers.filter(function (peer) {
|
|
|
|
return common.IPV6_RE.test(peer.ip)
|
|
|
|
}).map(function (peer) {
|
|
|
|
return '[' + peer.ip + ']:' + peer.port
|
2014-12-10 15:51:35 +00:00
|
|
|
}))
|
|
|
|
}
|
2014-12-12 23:09:40 +00:00
|
|
|
// IPv6 peers are not separate for non-compact responses
|
2014-11-26 07:27:02 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
cb(err, response)
|
2014-12-09 01:35:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype._onScrape = function (params, cb) {
|
|
|
|
var self = this
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
if (params.info_hash == null) {
|
2014-12-09 01:35:05 +00:00
|
|
|
// if info_hash param is omitted, stats for all torrents are returned
|
|
|
|
// TODO: make this configurable!
|
|
|
|
params.info_hash = Object.keys(self.torrents)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
series(params.info_hash.map(function (infoHash) {
|
2014-12-09 22:18:47 +00:00
|
|
|
var swarm = self.getSwarm(infoHash)
|
2014-12-10 15:47:41 +00:00
|
|
|
return function (cb) {
|
2014-12-11 20:53:39 +00:00
|
|
|
swarm.scrape(params, function (err, scrapeInfo) {
|
2014-12-10 15:47:41 +00:00
|
|
|
cb(err, scrapeInfo && {
|
|
|
|
infoHash: infoHash,
|
|
|
|
complete: scrapeInfo.complete || 0,
|
|
|
|
incomplete: scrapeInfo.incomplete || 0
|
|
|
|
})
|
|
|
|
})
|
2014-12-09 01:35:05 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
}), function (err, results) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
var response = {
|
|
|
|
action: common.ACTIONS.SCRAPE,
|
|
|
|
files: {},
|
2015-01-05 18:55:58 +00:00
|
|
|
flags: { min_request_interval: Math.ceil(self._intervalMs / 1000) }
|
2014-12-12 10:02:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
results.forEach(function (result) {
|
2014-12-12 10:02:11 +00:00
|
|
|
response.files[common.hexToBinary(result.infoHash)] = {
|
2014-12-10 15:47:41 +00:00
|
|
|
complete: result.complete,
|
|
|
|
incomplete: result.incomplete,
|
|
|
|
downloaded: result.complete // TODO: this only provides a lower-bound
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
})
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
cb(null, response)
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
function makeUdpPacket (params) {
|
2015-01-27 02:16:01 +00:00
|
|
|
var packet
|
2014-12-09 01:35:05 +00:00
|
|
|
switch (params.action) {
|
|
|
|
case common.ACTIONS.CONNECT:
|
2015-01-27 02:16:01 +00:00
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
common.toUInt32(common.ACTIONS.CONNECT),
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
params.connectionId
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
case common.ACTIONS.ANNOUNCE:
|
2015-01-27 02:16:01 +00:00
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
|
|
|
common.toUInt32(params.transactionId),
|
2015-01-05 18:55:58 +00:00
|
|
|
common.toUInt32(params.interval),
|
2014-12-09 01:35:05 +00:00
|
|
|
common.toUInt32(params.incomplete),
|
|
|
|
common.toUInt32(params.complete),
|
|
|
|
params.peers
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
case common.ACTIONS.SCRAPE:
|
|
|
|
var firstInfoHash = Object.keys(params.files)[0]
|
|
|
|
var scrapeInfo = firstInfoHash ? {
|
|
|
|
complete: params.files[firstInfoHash].complete,
|
|
|
|
incomplete: params.files[firstInfoHash].incomplete,
|
|
|
|
completed: params.files[firstInfoHash].complete // TODO: this only provides a lower-bound
|
|
|
|
} : {}
|
2015-01-27 02:16:01 +00:00
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
common.toUInt32(common.ACTIONS.SCRAPE),
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
common.toUInt32(scrapeInfo.complete),
|
|
|
|
common.toUInt32(scrapeInfo.completed),
|
|
|
|
common.toUInt32(scrapeInfo.incomplete)
|
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
break
|
2014-12-09 01:35:05 +00:00
|
|
|
case common.ACTIONS.ERROR:
|
2015-01-27 02:16:01 +00:00
|
|
|
packet = Buffer.concat([
|
2014-12-09 01:35:05 +00:00
|
|
|
common.toUInt32(common.ACTIONS.ERROR),
|
|
|
|
common.toUInt32(params.transactionId || 0),
|
2015-01-29 22:59:08 +00:00
|
|
|
new Buffer(params['failure reason'], 'utf8')
|
2014-12-09 01:35:05 +00:00
|
|
|
])
|
2015-01-27 02:16:01 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error('Action not implemented: ' + params.action)
|
2015-01-29 02:18:47 +00:00
|
|
|
}
|
2015-01-27 02:16:01 +00:00
|
|
|
return packet
|
2014-12-09 01:35:05 +00:00
|
|
|
}
|