2015-03-24 08:01:49 +00:00
|
|
|
module.exports = HTTPTracker
|
|
|
|
|
|
|
|
var bencode = require('bencode')
|
|
|
|
var compact2string = require('compact2string')
|
|
|
|
var debug = require('debug')('bittorrent-tracker:http-tracker')
|
2016-02-29 20:34:06 +00:00
|
|
|
var extend = require('xtend')
|
2015-03-24 08:01:49 +00:00
|
|
|
var get = require('simple-get')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
|
2015-07-29 07:26:44 +00:00
|
|
|
var common = require('../common')
|
2015-07-29 08:47:09 +00:00
|
|
|
var Tracker = require('./tracker')
|
2015-03-24 08:01:49 +00:00
|
|
|
|
|
|
|
var HTTP_SCRAPE_SUPPORT = /\/(announce)[^\/]*$/
|
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
inherits(HTTPTracker, Tracker)
|
2015-03-24 08:01:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP torrent tracker client (for an individual tracker)
|
|
|
|
*
|
|
|
|
* @param {Client} client parent bittorrent tracker client
|
|
|
|
* @param {string} announceUrl announce url of tracker
|
|
|
|
* @param {Object} opts options object
|
|
|
|
*/
|
|
|
|
function HTTPTracker (client, announceUrl, opts) {
|
|
|
|
var self = this
|
2015-07-29 08:47:09 +00:00
|
|
|
Tracker.call(self, client, announceUrl)
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('new http tracker %s', announceUrl)
|
|
|
|
|
|
|
|
// Determine scrape url (if http tracker supports it)
|
2015-07-29 08:47:09 +00:00
|
|
|
self.scrapeUrl = null
|
2015-03-24 08:01:49 +00:00
|
|
|
var m
|
2015-07-29 08:47:09 +00:00
|
|
|
if ((m = self.announceUrl.match(HTTP_SCRAPE_SUPPORT))) {
|
|
|
|
self.scrapeUrl = self.announceUrl.slice(0, m.index) + '/scrape' +
|
|
|
|
self.announceUrl.slice(m.index + 9)
|
2015-03-24 08:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 09:12:14 +00:00
|
|
|
HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
HTTPTracker.prototype.announce = function (opts) {
|
|
|
|
var self = this
|
2015-05-30 21:20:42 +00:00
|
|
|
if (self.destroyed) return
|
2015-03-24 08:01:49 +00:00
|
|
|
|
2016-02-29 20:34:06 +00:00
|
|
|
var params = extend(opts, {
|
2015-07-27 22:19:18 +00:00
|
|
|
compact: (opts.compact == null) ? 1 : opts.compact,
|
|
|
|
info_hash: self.client._infoHashBinary,
|
|
|
|
peer_id: self.client._peerIdBinary,
|
2016-02-29 20:34:06 +00:00
|
|
|
port: self.client._port
|
|
|
|
})
|
2015-07-27 22:19:18 +00:00
|
|
|
if (self._trackerId) params.trackerid = self._trackerId
|
2015-03-24 08:01:49 +00:00
|
|
|
|
2016-03-29 06:38:23 +00:00
|
|
|
self._request(self.announceUrl, params, function (data) {
|
|
|
|
self._onAnnounceResponse(data)
|
|
|
|
})
|
2015-03-24 08:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPTracker.prototype.scrape = function (opts) {
|
|
|
|
var self = this
|
2015-05-30 21:20:42 +00:00
|
|
|
if (self.destroyed) return
|
2015-03-24 08:01:49 +00:00
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
if (!self.scrapeUrl) {
|
|
|
|
self.client.emit('error', new Error('scrape not supported ' + self.announceUrl))
|
2015-03-24 08:01:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
|
|
|
|
? opts.infoHash.map(function (infoHash) {
|
|
|
|
return infoHash.toString('binary')
|
|
|
|
})
|
2015-12-02 23:31:20 +00:00
|
|
|
: (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary
|
2015-07-27 22:19:18 +00:00
|
|
|
var params = {
|
|
|
|
info_hash: infoHashes
|
|
|
|
}
|
2016-03-29 06:38:23 +00:00
|
|
|
self._request(self.scrapeUrl, params, function (data) {
|
|
|
|
self._onScrapeResponse(data)
|
|
|
|
})
|
2015-03-24 08:01:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 01:33:50 +00:00
|
|
|
HTTPTracker.prototype.destroy = function (cb) {
|
2015-05-17 05:55:41 +00:00
|
|
|
var self = this
|
2016-03-15 02:16:00 +00:00
|
|
|
if (self.destroyed) return cb(null)
|
2015-05-17 05:55:41 +00:00
|
|
|
self.destroyed = true
|
2015-07-29 08:47:09 +00:00
|
|
|
clearInterval(self.interval)
|
|
|
|
|
2015-07-17 01:33:50 +00:00
|
|
|
cb(null)
|
2015-05-17 05:55:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
HTTPTracker.prototype._request = function (requestUrl, params, cb) {
|
2015-03-24 08:01:49 +00:00
|
|
|
var self = this
|
|
|
|
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
|
2015-07-27 22:19:18 +00:00
|
|
|
common.querystringStringify(params)
|
2016-10-28 18:51:37 +00:00
|
|
|
var opts = {
|
|
|
|
url: u,
|
|
|
|
header: {
|
2016-10-28 20:25:50 +00:00
|
|
|
'user-agent': self.client._userAgent || ''
|
2016-10-28 18:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 08:47:09 +00:00
|
|
|
|
2016-10-28 18:51:37 +00:00
|
|
|
get.concat(opts, function (err, res, data) {
|
2015-05-17 05:55:41 +00:00
|
|
|
if (self.destroyed) return
|
2015-03-24 08:01:49 +00:00
|
|
|
if (err) return self.client.emit('warning', err)
|
2015-03-24 08:52:21 +00:00
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
return self.client.emit('warning', new Error('Non-200 response code ' +
|
2015-07-29 08:47:09 +00:00
|
|
|
res.statusCode + ' from ' + self.announceUrl))
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
if (!data || data.length === 0) {
|
|
|
|
return self.client.emit('warning', new Error('Invalid tracker response from' +
|
2015-07-29 08:47:09 +00:00
|
|
|
self.announceUrl))
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
2015-03-24 08:01:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
data = bencode.decode(data)
|
|
|
|
} catch (err) {
|
|
|
|
return self.client.emit('warning', new Error('Error decoding tracker response: ' + err.message))
|
|
|
|
}
|
|
|
|
var failure = data['failure reason']
|
|
|
|
if (failure) {
|
|
|
|
debug('failure from ' + requestUrl + ' (' + failure + ')')
|
|
|
|
return self.client.emit('warning', new Error(failure))
|
|
|
|
}
|
|
|
|
|
|
|
|
var warning = data['warning message']
|
|
|
|
if (warning) {
|
|
|
|
debug('warning from ' + requestUrl + ' (' + warning + ')')
|
|
|
|
self.client.emit('warning', new Error(warning))
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('response from ' + requestUrl)
|
|
|
|
|
|
|
|
cb(data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTPTracker.prototype._onAnnounceResponse = function (data) {
|
|
|
|
var self = this
|
|
|
|
|
|
|
|
var interval = data.interval || data['min interval']
|
2015-07-29 08:47:09 +00:00
|
|
|
if (interval) self.setInterval(interval * 1000)
|
2015-03-24 08:01:49 +00:00
|
|
|
|
|
|
|
var trackerId = data['tracker id']
|
|
|
|
if (trackerId) {
|
|
|
|
// If absent, do not discard previous trackerId value
|
|
|
|
self._trackerId = trackerId
|
|
|
|
}
|
|
|
|
|
|
|
|
self.client.emit('update', {
|
2015-07-29 08:47:09 +00:00
|
|
|
announce: self.announceUrl,
|
2015-03-24 08:01:49 +00:00
|
|
|
complete: data.complete,
|
|
|
|
incomplete: data.incomplete
|
|
|
|
})
|
|
|
|
|
|
|
|
var addrs
|
|
|
|
if (Buffer.isBuffer(data.peers)) {
|
|
|
|
// tracker returned compact response
|
|
|
|
try {
|
|
|
|
addrs = compact2string.multi(data.peers)
|
|
|
|
} catch (err) {
|
|
|
|
return self.client.emit('warning', err)
|
|
|
|
}
|
|
|
|
addrs.forEach(function (addr) {
|
|
|
|
self.client.emit('peer', addr)
|
|
|
|
})
|
|
|
|
} else if (Array.isArray(data.peers)) {
|
|
|
|
// tracker returned normal response
|
|
|
|
data.peers.forEach(function (peer) {
|
|
|
|
self.client.emit('peer', peer.ip + ':' + peer.port)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Buffer.isBuffer(data.peers6)) {
|
|
|
|
// tracker returned compact response
|
|
|
|
try {
|
|
|
|
addrs = compact2string.multi6(data.peers6)
|
|
|
|
} catch (err) {
|
|
|
|
return self.client.emit('warning', err)
|
|
|
|
}
|
|
|
|
addrs.forEach(function (addr) {
|
|
|
|
self.client.emit('peer', addr)
|
|
|
|
})
|
|
|
|
} else if (Array.isArray(data.peers6)) {
|
|
|
|
// tracker returned normal response
|
|
|
|
data.peers6.forEach(function (peer) {
|
|
|
|
var ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip)
|
|
|
|
? peer.ip /* ipv6 w/ brackets or domain name */
|
|
|
|
: '[' + peer.ip + ']' /* ipv6 without brackets */
|
|
|
|
self.client.emit('peer', ip + ':' + peer.port)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTPTracker.prototype._onScrapeResponse = function (data) {
|
|
|
|
var self = this
|
|
|
|
// NOTE: the unofficial spec says to use the 'files' key, 'host' has been
|
|
|
|
// seen in practice
|
|
|
|
data = data.files || data.host || {}
|
|
|
|
|
2015-05-02 00:36:07 +00:00
|
|
|
var keys = Object.keys(data)
|
|
|
|
if (keys.length === 0) {
|
2015-03-24 08:01:49 +00:00
|
|
|
self.client.emit('warning', new Error('invalid scrape response'))
|
2015-05-02 00:36:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.forEach(function (infoHash) {
|
|
|
|
var response = data[infoHash]
|
2015-03-24 08:01:49 +00:00
|
|
|
// TODO: optionally handle data.flags.min_request_interval
|
|
|
|
// (separate from announce interval)
|
|
|
|
self.client.emit('scrape', {
|
2015-07-29 08:47:09 +00:00
|
|
|
announce: self.announceUrl,
|
2015-05-02 00:36:07 +00:00
|
|
|
infoHash: common.binaryToHex(infoHash),
|
|
|
|
complete: response.complete,
|
|
|
|
incomplete: response.incomplete,
|
|
|
|
downloaded: response.downloaded
|
2015-03-24 08:01:49 +00:00
|
|
|
})
|
2015-05-02 00:36:07 +00:00
|
|
|
})
|
2015-03-24 08:01:49 +00:00
|
|
|
}
|