refactored tracker tracker Client into a container for multiple Trackers each with their own, independent update interval and trackerIds

This commit is contained in:
fisch0920 2014-05-09 00:23:14 -04:00
parent 7102b9bc3e
commit 931f0a5a48

196
index.js
View File

@ -1,3 +1,5 @@
/* vim: set ts=2 sw=2 sts=2 et: */
exports.Client = Client exports.Client = Client
exports.Server = Server exports.Server = Server
@ -20,33 +22,36 @@ var ACTIONS = { CONNECT: 0, ANNOUNCE: 1 }
var EVENTS = { completed: 1, started: 2, stopped: 3 } var EVENTS = { completed: 1, started: 2, stopped: 3 }
var MAX_UINT = 4294967295 var MAX_UINT = 4294967295
inherits(Client, EventEmitter) inherits(Tracker, EventEmitter)
function Client (peerId, port, torrent, opts) { /**
* An individual torrent tracker
*
* @param {Client} client parent bittorrent tracker client
* @param {string} announceUrl announce url of tracker
* @param {Number} interval interval in ms to send announce requests to the tracker
* @param {Object} opts optional options
*/
function Tracker (client, announceUrl, interval, opts) {
var self = this var self = this
if (!(self instanceof Client)) return new Client(peerId, port, torrent, opts) if (!(self instanceof Tracker)) return new Tracker(client, announceUrl, interval, opts)
EventEmitter.call(self) EventEmitter.call(self)
self._opts = opts || {} self._opts = opts || {}
// required self.client = client
self._peerId = Buffer.isBuffer(peerId)
? peerId
: new Buffer(peerId, 'utf8')
self._port = port
self._infoHash = Buffer.isBuffer(torrent.infoHash)
? torrent.infoHash
: new Buffer(torrent.infoHash, 'hex')
self._torrentLength = torrent.length
self._announce = torrent.announce
// optional
self._numWant = self._opts.numWant || 80
self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes
self._announceUrl = announceUrl
self._intervalMs = interval
self._interval = null self._interval = null
if (self._announceUrl.indexOf('udp:') === 0) {
self._requestImpl = self._requestUdp.bind(self)
} else {
self._requestImpl = self._requestHttp.bind(self)
}
} }
Client.prototype.start = function (opts) { Tracker.prototype.start = function (opts) {
var self = this var self = this
opts = opts || {} opts = opts || {}
opts.event = 'started' opts.event = 'started'
@ -55,7 +60,7 @@ Client.prototype.start = function (opts) {
self.setInterval(self._intervalMs) // start announcing on intervals self.setInterval(self._intervalMs) // start announcing on intervals
} }
Client.prototype.stop = function (opts) { Tracker.prototype.stop = function (opts) {
var self = this var self = this
opts = opts || {} opts = opts || {}
opts.event = 'stopped' opts.event = 'stopped'
@ -64,7 +69,7 @@ Client.prototype.stop = function (opts) {
self.setInterval(0) // stop announcing on intervals self.setInterval(0) // stop announcing on intervals
} }
Client.prototype.complete = function (opts) { Tracker.prototype.complete = function (opts) {
var self = this var self = this
opts = opts || {} opts = opts || {}
opts.event = 'completed' opts.event = 'completed'
@ -72,13 +77,13 @@ Client.prototype.complete = function (opts) {
self._request(opts) self._request(opts)
} }
Client.prototype.update = function (opts) { Tracker.prototype.update = function (opts) {
var self = this var self = this
opts = opts || {} opts = opts || {}
self._request(opts) self._request(opts)
} }
Client.prototype.setInterval = function (intervalMs) { Tracker.prototype.setInterval = function (intervalMs) {
var self = this var self = this
if (self._interval) { if (self._interval) {
clearInterval(self._interval) clearInterval(self._interval)
@ -93,15 +98,15 @@ Client.prototype.setInterval = function (intervalMs) {
/** /**
* Send a request to the tracker * Send a request to the tracker
*/ */
Client.prototype._request = function (opts) { Tracker.prototype._request = function (opts) {
var self = this var self = this
opts = extend({ opts = extend({
info_hash: bytewiseEncodeURIComponent(self._infoHash), info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
peer_id: bytewiseEncodeURIComponent(self._peerId), peer_id: bytewiseEncodeURIComponent(self.client._peerId),
port: self._port, port: self.client._port,
left: self._torrentLength - (opts.downloaded || 0), left: self.client._torrentLength - (opts.downloaded || 0),
compact: 1, compact: 1,
numwant: self._numWant, numwant: self.client._numWant,
uploaded: 0, // default, user should provide real value uploaded: 0, // default, user should provide real value
downloaded: 0 // default, user should provide real value downloaded: 0 // default, user should provide real value
}, opts) }, opts)
@ -110,42 +115,36 @@ Client.prototype._request = function (opts) {
opts.trackerid = self._trackerId opts.trackerid = self._trackerId
} }
self._announce.forEach(function (announceUrl) { self._requestImpl(opts)
if (announceUrl.indexOf('udp:') === 0) {
self._requestUdp(announceUrl, opts)
} else {
self._requestHttp(announceUrl, opts)
}
})
} }
Client.prototype._requestHttp = function (announceUrl, opts) { Tracker.prototype._requestHttp = function (opts) {
var self = this var self = this
var fullUrl = announceUrl + '?' + querystring.stringify(opts) var fullUrl = self._announceUrl + '?' + querystring.stringify(opts)
var req = http.get(fullUrl, function (res) { var req = http.get(fullUrl, function (res) {
var data = '' var data = ''
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
res.resume() // consume the whole stream res.resume() // consume the whole stream
self.emit('error', new Error('Invalid response code ' + res.statusCode + ' from tracker')) self.client.emit('error', new Error('Invalid response code ' + res.statusCode + ' from tracker'))
return return
} }
res.on('data', function (chunk) { res.on('data', function (chunk) {
data += chunk data += chunk
}) })
res.on('end', function () { res.on('end', function () {
self._handleResponse(data, announceUrl) self._handleResponse(data)
}) })
}) })
req.on('error', function (err) { req.on('error', function (err) {
self.emit('error', err) self.client.emit('error', err)
}) })
} }
Client.prototype._requestUdp = function (announceUrl, opts) { Tracker.prototype._requestUdp = function (opts) {
var self = this var self = this
var parsedUrl = url.parse(announceUrl) var parsedUrl = url.parse(self._announceUrl)
var socket = dgram.createSocket('udp4') var socket = dgram.createSocket('udp4')
var transactionId = new Buffer(hat(32), 'hex') var transactionId = new Buffer(hat(32), 'hex')
@ -158,7 +157,7 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
} }
function error (message) { function error (message) {
self.emit('error', new Error(message)) self.client.emit('error', new Error(message))
socket.close() socket.close()
clearTimeout(timeout) clearTimeout(timeout)
} }
@ -187,7 +186,6 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
return error(new Error('invalid announce message')) return error(new Error('invalid announce message'))
} }
// TODO: this should be stored per tracker, not globally for all trackers
var interval = message.readUInt32BE(8) var interval = message.readUInt32BE(8)
if (interval && !self._opts.interval && self._intervalMs !== 0) { if (interval && !self._opts.interval && self._intervalMs !== 0) {
// use the interval the tracker recommends, UNLESS the user manually specifies an // use the interval the tracker recommends, UNLESS the user manually specifies an
@ -195,14 +193,14 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
self.setInterval(interval * 1000) self.setInterval(interval * 1000)
} }
self.emit('update', { self.client.emit('update', {
announce: announceUrl, announce: self._announceUrl,
complete: message.readUInt32BE(16), complete: message.readUInt32BE(16),
incomplete: message.readUInt32BE(12) incomplete: message.readUInt32BE(12)
}) })
compact2string.multi(message.slice(20)).forEach(function (addr) { compact2string.multi(message.slice(20)).forEach(function (addr) {
self.emit('peer', addr) self.client.emit('peer', addr)
}) })
clearTimeout(timeout) clearTimeout(timeout)
@ -225,16 +223,16 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
connectionId, connectionId,
toUInt32(ACTIONS.ANNOUNCE), toUInt32(ACTIONS.ANNOUNCE),
transactionId, transactionId,
self._infoHash, self.client._infoHash,
self._peerId, self.client._peerId,
toUInt64(opts.downloaded || 0), toUInt64(opts.downloaded || 0),
toUInt64(opts.left || 0), toUInt64(opts.left || 0),
toUInt64(opts.uploaded || 0), toUInt64(opts.uploaded || 0),
toUInt32(EVENTS[opts.event] || 0), toUInt32(EVENTS[opts.event] || 0),
toUInt32(0), // ip address (optional) toUInt32(0), // ip address (optional)
toUInt32(0), // key (optional) toUInt32(0), // key (optional)
toUInt32(self._numWant), toUInt32(self.client._numWant),
toUInt16(self._port || 0) toUInt16(self.client._port || 0)
])) ]))
} }
@ -245,25 +243,24 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
])) ]))
} }
Client.prototype._handleResponse = function (data, announceUrl) { Tracker.prototype._handleResponse = function (data) {
var self = this var self = this
try { try {
data = bncode.decode(data) data = bncode.decode(data)
} catch (err) { } catch (err) {
return self.emit('error', new Error('Error decoding tracker response: ' + err.message)) return self.client.emit('error', new Error('Error decoding tracker response: ' + err.message))
} }
var failure = data['failure reason'] var failure = data['failure reason']
if (failure) { if (failure) {
return self.emit('error', new Error(failure)) return self.client.emit('error', new Error(failure))
} }
var warning = data['warning message'] var warning = data['warning message']
if (warning) { if (warning) {
self.emit('warning', warning); self.client.emit('warning', warning);
} }
// TODO: this should be stored per tracker, not globally for all trackers
var interval = data.interval || data['min interval'] var interval = data.interval || data['min interval']
if (interval && !self._opts.interval && self._intervalMs !== 0) { if (interval && !self._opts.interval && self._intervalMs !== 0) {
// use the interval the tracker recommends, UNLESS the user manually specifies an // use the interval the tracker recommends, UNLESS the user manually specifies an
@ -271,15 +268,14 @@ Client.prototype._handleResponse = function (data, announceUrl) {
self.setInterval(interval * 1000) self.setInterval(interval * 1000)
} }
// TODO: this should be stored per tracker, not globally for all trackers
var trackerId = data['tracker id'] var trackerId = data['tracker id']
if (trackerId) { if (trackerId) {
// If absent, do not discard previous trackerId value // If absent, do not discard previous trackerId value
self._trackerId = trackerId self._trackerId = trackerId
} }
self.emit('update', { self.client.emit('update', {
announce: announceUrl, announce: self._announceUrl,
complete: data.complete, complete: data.complete,
incomplete: data.incomplete incomplete: data.incomplete
}) })
@ -287,17 +283,92 @@ Client.prototype._handleResponse = function (data, announceUrl) {
if (Buffer.isBuffer(data.peers)) { if (Buffer.isBuffer(data.peers)) {
// tracker returned compact response // tracker returned compact response
compact2string.multi(data.peers).forEach(function (addr) { compact2string.multi(data.peers).forEach(function (addr) {
self.emit('peer', addr) self.client.emit('peer', addr)
}) })
} else if (Array.isArray(data.peers)) { } else if (Array.isArray(data.peers)) {
// tracker returned normal response // tracker returned normal response
data.peers.forEach(function (peer) { data.peers.forEach(function (peer) {
var ip = peer.ip var ip = peer.ip
self.emit('peer', ip[0] + '.' + ip[1] + '.' + ip[2] + '.' + ip[3] + ':' + peer.port) self.client.emit('peer', ip[0] + '.' + ip[1] + '.' + ip[2] + '.' + ip[3] + ':' + peer.port)
}) })
} }
} }
inherits(Client, EventEmitter)
/**
* A Client manages tracker connections for a torrent.
*
* @param {string} peerId this peer's id
* @param {Number} port port number that the client is listening on
* @param {Object} torrent parsed torrent
* @param {Object} opts optional options
* @param {Number} opts.numWant number of peers to request
* @param {Number} opts.interval interval in ms to send announce requests to the tracker
*/
function Client (peerId, port, torrent, opts) {
var self = this
if (!(self instanceof Client)) return new Client(peerId, port, torrent, opts)
EventEmitter.call(self)
self._opts = opts || {}
// required
self._peerId = Buffer.isBuffer(peerId)
? peerId
: new Buffer(peerId, 'utf8')
self._port = port
self._infoHash = Buffer.isBuffer(torrent.infoHash)
? torrent.infoHash
: new Buffer(torrent.infoHash, 'hex')
self._torrentLength = torrent.length
self._announce = torrent.announce
// optional
self._numWant = self._opts.numWant || 80
self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes
self._trackers = torrent.announce.map(function (announceUrl) {
return Tracker(self, announceUrl, self._intervalMs, self._opts)
})
}
Client.prototype.start = function (opts) {
var self = this
self._trackers.forEach(function (tracker) {
tracker.start(opts)
})
}
Client.prototype.stop = function (opts) {
var self = this
self._trackers.forEach(function (tracker) {
tracker.stop(opts)
})
}
Client.prototype.complete = function (opts) {
var self = this
self._trackers.forEach(function (tracker) {
tracker.complete(opts)
})
}
Client.prototype.update = function (opts) {
var self = this
self._trackers.forEach(function (tracker) {
tracker.update(opts)
})
}
Client.prototype.setInterval = function (intervalMs) {
var self = this
self._intervalMs = intervalMs
self._trackers.forEach(function (tracker) {
tracker.setInterval(intervalMs)
})
}
inherits(Server, EventEmitter) inherits(Server, EventEmitter)
function Server (opts) { function Server (opts) {
@ -452,7 +523,7 @@ Server.prototype._onHttpRequest = function (req, res) {
return error('unexpected `update` event from peer that is not in swarm') return error('unexpected `update` event from peer that is not in swarm')
} }
self.emit('update', addr, params) self.client.emit('update', addr, params)
break break
default: default:
@ -476,6 +547,7 @@ Server.prototype._onHttpRequest = function (req, res) {
} }
res.end(bncode.encode(response)) res.end(bncode.encode(response))
} else { // TODO: handle unofficial scrape messages
} }
} }