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-08-17 02:05:56 +00:00
|
|
|
var portfinder = require('portfinder')
|
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-08-17 02:05:56 +00:00
|
|
|
// Use random port above 1024
|
|
|
|
portfinder.basePort = Math.floor(Math.random() * 60000) + 1025
|
|
|
|
|
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
|
|
|
|
? opts.interval / 1000
|
|
|
|
: 10 * 60 // 10 min (in secs)
|
|
|
|
|
|
|
|
self._trustProxy = !!opts.trustProxy
|
|
|
|
|
2014-08-17 02:05:56 +00:00
|
|
|
self.listening = false
|
2014-08-07 05:35:43 +00:00
|
|
|
self.port = null
|
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) {
|
|
|
|
self._httpServer = http.createServer()
|
|
|
|
self._httpServer.on('request', self._onHttpRequest.bind(self))
|
2014-07-13 22:28:23 +00:00
|
|
|
self._httpServer.on('error', self._onError.bind(self))
|
2014-08-07 05:35:43 +00:00
|
|
|
self._httpServer.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) {
|
|
|
|
self._udpServer = dgram.createSocket('udp4')
|
|
|
|
self._udpServer.on('message', self._onUdpRequest.bind(self))
|
2014-07-13 22:28:23 +00:00
|
|
|
self._udpServer.on('error', self._onError.bind(self))
|
2014-08-07 05:35:43 +00:00
|
|
|
self._udpServer.on('listening', onListening)
|
|
|
|
}
|
|
|
|
|
|
|
|
var num = !!self._httpServer + !!self._udpServer
|
|
|
|
function onListening () {
|
|
|
|
num -= 1
|
2014-08-17 02:05:56 +00:00
|
|
|
if (num === 0) {
|
|
|
|
self.listening = true
|
|
|
|
self.emit('listening', self.port)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
function onPort (err, port) {
|
|
|
|
if (err) return self.emit('error', err)
|
|
|
|
self.port = port
|
|
|
|
self._httpServer && self._httpServer.listen(port.http || port)
|
|
|
|
self._udpServer && self._udpServer.bind(port.udp || port)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port) onPort(null, port)
|
|
|
|
else portfinder.getPort(onPort)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.close = function (cb) {
|
|
|
|
var self = this
|
|
|
|
cb = cb || function () {}
|
|
|
|
if (self._udpServer) {
|
|
|
|
self._udpServer.close()
|
|
|
|
}
|
|
|
|
if (self._httpServer) {
|
|
|
|
self._httpServer.close(cb)
|
|
|
|
} else {
|
|
|
|
cb(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:18:47 +00:00
|
|
|
Server.prototype.getSwarm = function (binaryInfoHash) {
|
2014-07-11 05:05:56 +00:00
|
|
|
var self = this
|
2014-12-09 22:18:47 +00:00
|
|
|
if (Buffer.isBuffer(binaryInfoHash)) binaryInfoHash = binaryInfoHash.toString('binary')
|
2014-07-11 05:05:56 +00:00
|
|
|
var swarm = self.torrents[binaryInfoHash]
|
2014-06-07 22:10:12 +00:00
|
|
|
if (!swarm) {
|
2014-12-10 15:47:41 +00:00
|
|
|
swarm = self.torrents[binaryInfoHash] = new Swarm(binaryInfoHash, this)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
return swarm
|
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype._onHttpRequest = function (req, res) {
|
|
|
|
var self = this
|
2014-12-11 15:22:17 +00:00
|
|
|
var error
|
2014-12-08 22:42:05 +00:00
|
|
|
var params
|
|
|
|
try {
|
|
|
|
params = parseHttpRequest(req, {
|
|
|
|
trustProxy: self._trustProxy
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2014-12-11 15:22:17 +00:00
|
|
|
error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error && !params) error = new Error('Empty HTTP parameters')
|
|
|
|
if (error) {
|
|
|
|
debug('sent error %s', error.message)
|
2014-12-08 22:42:05 +00:00
|
|
|
res.end(bencode.encode({
|
2014-12-11 15:22:17 +00:00
|
|
|
'failure reason': error.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-11 15:22:17 +00:00
|
|
|
self.emit('warning', error)
|
2014-12-11 20:53:39 +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))
|
2014-12-09 01:35:05 +00:00
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype._onUdpRequest = function (msg, rinfo) {
|
|
|
|
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 = {
|
|
|
|
action: common.ACTIONS.ERRROR,
|
|
|
|
'failure reason': err.message
|
2014-11-26 07:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
var socket = dgram.createSocket('udp4')
|
|
|
|
response.transactionId = params.transactionId
|
|
|
|
response.connectionId = params.connectionId
|
|
|
|
var buf = makeUdpPacket(response)
|
|
|
|
socket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () {
|
|
|
|
try {
|
|
|
|
socket.close()
|
|
|
|
} catch (err) {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
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)
|
2014-12-10 15:47:41 +00:00
|
|
|
swarm.announce(params, function (err, response) {
|
|
|
|
if (response) {
|
|
|
|
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
|
|
|
if (!response.intervalMs) response.intervalMs = self._intervalMs
|
2014-12-10 15:51:35 +00:00
|
|
|
if (params.compact === 1) {
|
|
|
|
response.peers = string2compact(response.peers.map(function (peer) {
|
|
|
|
return peer.ip + ':' + peer.port // TODO: ipv6 brackets
|
|
|
|
}))
|
|
|
|
}
|
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-09 01:35:05 +00:00
|
|
|
if (typeof params.info_hash === 'string') {
|
|
|
|
params.info_hash = [ params.info_hash ]
|
|
|
|
} else if (params.info_hash == null) {
|
|
|
|
// 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-09 01:35:05 +00:00
|
|
|
if (!Array.isArray(params.info_hash)) {
|
|
|
|
var err = new Error('invalid info_hash')
|
|
|
|
self.emit('warning', err)
|
|
|
|
return cb(err)
|
|
|
|
}
|
2014-12-11 20:53:39 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
var response = {
|
|
|
|
action: common.ACTIONS.SCRAPE,
|
|
|
|
files: {},
|
|
|
|
flags: {
|
|
|
|
min_request_interval: self._intervalMs
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
results.forEach(function (result) {
|
|
|
|
response.files[result.infoHash] = {
|
|
|
|
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-08 22:42:05 +00:00
|
|
|
|
2014-12-09 01:35:05 +00:00
|
|
|
function makeUdpPacket (params) {
|
|
|
|
switch (params.action) {
|
|
|
|
case common.ACTIONS.CONNECT:
|
|
|
|
return Buffer.concat([
|
|
|
|
common.toUInt32(common.ACTIONS.CONNECT),
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
params.connectionId
|
|
|
|
])
|
|
|
|
case common.ACTIONS.ANNOUNCE:
|
|
|
|
return Buffer.concat([
|
|
|
|
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
common.toUInt32(params.intervalMs),
|
|
|
|
common.toUInt32(params.incomplete),
|
|
|
|
common.toUInt32(params.complete),
|
|
|
|
params.peers
|
|
|
|
])
|
|
|
|
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
|
|
|
|
} : {}
|
|
|
|
return Buffer.concat([
|
|
|
|
common.toUInt32(common.ACTIONS.SCRAPE),
|
|
|
|
common.toUInt32(params.transactionId),
|
|
|
|
common.toUInt32(scrapeInfo.complete),
|
|
|
|
common.toUInt32(scrapeInfo.completed),
|
|
|
|
common.toUInt32(scrapeInfo.incomplete)
|
|
|
|
])
|
|
|
|
case common.ACTIONS.ERROR:
|
|
|
|
return Buffer.concat([
|
|
|
|
common.toUInt32(common.ACTIONS.ERROR),
|
|
|
|
common.toUInt32(params.transactionId || 0),
|
|
|
|
new Buffer(params.message, 'utf8')
|
|
|
|
])
|
|
|
|
default:
|
|
|
|
throw new Error('Action not implemented: ' + params.action)
|
|
|
|
}
|
|
|
|
}
|