mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-02-22 13:19:04 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
50d3379255
17
README.md
17
README.md
@ -18,6 +18,7 @@ by [WebTorrent](http://webtorrent.io).
|
||||
|
||||
- Includes client & server implementations
|
||||
- Supports HTTP & UDP trackers ([BEP 15](http://www.bittorrent.org/beps/bep_0015.html))
|
||||
- Supports tracker scrape
|
||||
|
||||
## install
|
||||
|
||||
@ -53,9 +54,9 @@ client.on('error', function (err) {
|
||||
client.start()
|
||||
|
||||
client.on('update', function (data) {
|
||||
console.log('got a response from tracker: ' + data.announce)
|
||||
console.log('number of seeders on this tracker: ' + data.complete)
|
||||
console.log('number of leechers on this tracker: ' + data.incomplete)
|
||||
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) {
|
||||
@ -70,6 +71,16 @@ client.update()
|
||||
|
||||
// stop getting peers from the tracker, gracefully leave the swarm
|
||||
client.stop()
|
||||
|
||||
// scrape
|
||||
client.scape()
|
||||
|
||||
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.incomplete)
|
||||
})
|
||||
```
|
||||
|
||||
### server
|
||||
|
46
index.js
46
index.js
@ -29,25 +29,23 @@ inherits(Tracker, EventEmitter)
|
||||
*
|
||||
* @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) {
|
||||
function Tracker (client, announceUrl, opts) {
|
||||
var self = this
|
||||
if (!(self instanceof Tracker)) return new Tracker(client, announceUrl, interval, opts)
|
||||
EventEmitter.call(self)
|
||||
self._opts = opts || {}
|
||||
|
||||
self.client = client
|
||||
|
||||
self._announceUrl = announceUrl
|
||||
self._intervalMs = interval
|
||||
self._intervalMs = self.client._intervalMs // use client interval initially
|
||||
self._interval = null
|
||||
|
||||
if (self._announceUrl.indexOf('udp:') === 0) {
|
||||
self._requestImpl = self._requestUdp.bind(self)
|
||||
self._requestImpl = self._requestUdp
|
||||
} else {
|
||||
self._requestImpl = self._requestHttp.bind(self)
|
||||
self._requestImpl = self._requestHttp
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,7 +86,7 @@ Tracker.prototype.scrape = function (opts) {
|
||||
|
||||
if (!self._scrapeUrl) {
|
||||
var announce = 'announce'
|
||||
var i = self._announceUrl.lastIndexOf('\/') + 1
|
||||
var i = self._announceUrl.lastIndexOf('/') + 1
|
||||
|
||||
if (i >= 1 && self._announceUrl.slice(i, i + announce.length) === announce) {
|
||||
self._scrapeUrl = self._announceUrl.slice(0, i) + 'scrape' + self._announceUrl.slice(i + announce.length)
|
||||
@ -350,7 +348,7 @@ Tracker.prototype._handleResponse = function (requestUrl, data) {
|
||||
})
|
||||
}
|
||||
} else if (requestUrl === self._scrapeUrl) {
|
||||
// note: the unofficial spec says to use the 'files' key but i've seen 'host' in practice
|
||||
// NOTE: the unofficial spec says to use the 'files' key but i've seen 'host' in practice
|
||||
data = data.files || data.host || {}
|
||||
data = data[bytewiseEncodeURIComponent(self.client._infoHash)]
|
||||
|
||||
@ -402,7 +400,7 @@ function Client (peerId, port, torrent, opts) {
|
||||
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)
|
||||
return new Tracker(self, announceUrl, self._opts)
|
||||
})
|
||||
}
|
||||
|
||||
@ -523,24 +521,24 @@ Server.prototype._onHttpRequest = function (req, res) {
|
||||
|
||||
var warning
|
||||
var s = req.url.split('?')
|
||||
var params = querystring.parse(s[1])
|
||||
|
||||
// TODO: detect when required params are missing
|
||||
// TODO: support multiple info_hash parameters as a concatenation of individual requests
|
||||
var infoHash = bytewiseDecodeURIComponent(params.info_hash).toString('hex')
|
||||
|
||||
if (!infoHash) {
|
||||
return error('bittorrent-tracker server only supports announcing one torrent at a time')
|
||||
}
|
||||
|
||||
if (s[0] === '/announce') {
|
||||
var params = querystring.parse(s[1])
|
||||
|
||||
var ip = self._trustProxy
|
||||
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
||||
: req.connection.remoteAddress
|
||||
var port = Number(params.port)
|
||||
var addr = ip + ':' + port
|
||||
|
||||
// TODO: support multiple info_hash parameters as a concatenation of individual requests
|
||||
var infoHash = bytewiseDecodeURIComponent(params.info_hash).toString('hex')
|
||||
var peerId = bytewiseDecodeURIComponent(params.peer_id).toString('utf8')
|
||||
|
||||
if (!infoHash) {
|
||||
return error('bittorrent-tracker server only supports announcing one torrent at a time')
|
||||
}
|
||||
|
||||
var swarm = self._getSwarm(infoHash)
|
||||
var peer = swarm.peers[addr]
|
||||
|
||||
@ -627,14 +625,8 @@ Server.prototype._onHttpRequest = function (req, res) {
|
||||
}
|
||||
|
||||
res.end(bncode.encode(response))
|
||||
|
||||
} else if (s[0] === '/scrape') { // unofficial scrape message
|
||||
var params = querystring.parse(s[1])
|
||||
var infoHash = bytewiseDecodeURIComponent(params.info_hash).toString('hex')
|
||||
|
||||
if (!infoHash) {
|
||||
return error('bittorrent-tracker server only supports scraping one torrent at a time')
|
||||
}
|
||||
|
||||
var swarm = self._getSwarm(infoHash)
|
||||
var response = { files : { } }
|
||||
|
||||
@ -711,7 +703,9 @@ function toUInt32 (n) {
|
||||
function toUInt64 (n) {
|
||||
if (n > MAX_UINT || typeof n === 'string') {
|
||||
var bytes = bn(n).toArray()
|
||||
while (bytes.length < 8) bytes.unshift(0)
|
||||
while (bytes.length < 8) {
|
||||
bytes.unshift(0)
|
||||
}
|
||||
return new Buffer(bytes)
|
||||
}
|
||||
return Buffer.concat([toUInt32(0), toUInt32(n)])
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "bittorrent-tracker",
|
||||
"description": "Simple, robust, BitTorrent tracker (client & server) implementation",
|
||||
"version": "0.5.1",
|
||||
"version": "0.6.0",
|
||||
"author": {
|
||||
"name": "Feross Aboukhadijeh",
|
||||
"email": "feross@feross.org",
|
||||
|
@ -67,12 +67,15 @@ test('server', function (t) {
|
||||
t.equal(data.incomplete, 0)
|
||||
|
||||
client.scrape()
|
||||
|
||||
client.once('scrape', function (data) {
|
||||
t.equal(data.announce, announceUrl)
|
||||
t.equal(typeof data.complete, 'number')
|
||||
t.equal(typeof data.incomplete, 'number')
|
||||
t.equal(typeof data.downloaded, 'number')
|
||||
|
||||
client.stop()
|
||||
|
||||
client.once('update', function (data) {
|
||||
t.equal(data.announce, announceUrl)
|
||||
t.equal(data.complete, 0)
|
||||
@ -80,8 +83,6 @@ test('server', function (t) {
|
||||
|
||||
server.close()
|
||||
})
|
||||
|
||||
client.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user