Merge remote-tracking branch 'upstream/master'

This commit is contained in:
fisch0920 2014-05-15 06:30:11 -04:00
commit d7b567554f
6 changed files with 74 additions and 16 deletions

View File

@ -3,7 +3,7 @@
exports.Client = Client exports.Client = Client
exports.Server = Server exports.Server = Server
var bn = require('bn.js') var BN = require('bn.js')
var bncode = require('bncode') var bncode = require('bncode')
var compact2string = require('compact2string') var compact2string = require('compact2string')
var dgram = require('dgram') var dgram = require('dgram')
@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) {
var self = this var self = this
opts = opts || {} opts = opts || {}
opts.event = 'completed' opts.event = 'completed'
opts.downloaded = self._torrentLength opts.downloaded = opts.downloaded || self.torrentLength || 0
self._request(opts) self._request(opts)
} }
@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) {
info_hash: bytewiseEncodeURIComponent(self.client._infoHash), info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
peer_id: bytewiseEncodeURIComponent(self.client._peerId), peer_id: bytewiseEncodeURIComponent(self.client._peerId),
port: self.client._port, port: self.client._port,
left: self.client._torrentLength - (opts.downloaded || 0),
compact: 1, compact: 1,
numwant: self.client._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)
if (self.client.torrentLength !== undefined) {
opts.left = self.client.torrentLength - (opts.downloaded || 0)
}
if (self._trackerId) { if (self._trackerId) {
opts.trackerid = self._trackerId opts.trackerid = self._trackerId
} }
@ -267,7 +270,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
self.client._infoHash, self.client._infoHash,
self.client._peerId, self.client._peerId,
toUInt64(opts.downloaded || 0), toUInt64(opts.downloaded || 0),
toUInt64(opts.left || 0), opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
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)
@ -390,13 +393,17 @@ function Client (peerId, port, torrent, opts) {
self._infoHash = Buffer.isBuffer(torrent.infoHash) self._infoHash = Buffer.isBuffer(torrent.infoHash)
? torrent.infoHash ? torrent.infoHash
: new Buffer(torrent.infoHash, 'hex') : new Buffer(torrent.infoHash, 'hex')
self._torrentLength = torrent.length self.torrentLength = torrent.length
self._announce = torrent.announce self._announce = torrent.announce
// optional // optional
self._numWant = self._opts.numWant || 80 self._numWant = self._opts.numWant || 80
self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes
if (typeof torrent.announce === 'string') {
// magnet-uri returns a string if the magnet uri only contains one 'tr' parameter
torrent.announce = [torrent.announce]
}
self._trackers = torrent.announce.map(function (announceUrl) { self._trackers = torrent.announce.map(function (announceUrl) {
return new Tracker(self, announceUrl, self._opts) return new Tracker(self, announceUrl, self._opts)
}) })
@ -700,7 +707,7 @@ function toUInt32 (n) {
function toUInt64 (n) { function toUInt64 (n) {
if (n > MAX_UINT || typeof n === 'string') { if (n > MAX_UINT || typeof n === 'string') {
var bytes = bn(n).toArray() var bytes = new BN(n).toArray()
while (bytes.length < 8) { while (bytes.length < 8) {
bytes.unshift(0) bytes.unshift(0)
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "bittorrent-tracker", "name": "bittorrent-tracker",
"description": "Simple, robust, BitTorrent tracker (client & server) implementation", "description": "Simple, robust, BitTorrent tracker (client & server) implementation",
"version": "0.6.0", "version": "0.7.0",
"author": { "author": {
"name": "Feross Aboukhadijeh", "name": "Feross Aboukhadijeh",
"email": "feross@feross.org", "email": "feross@feross.org",
@ -19,9 +19,10 @@
"querystring": "^0.2.0", "querystring": "^0.2.0",
"run-parallel": "^1.0.0", "run-parallel": "^1.0.0",
"string2compact": "^1.1.0", "string2compact": "^1.1.0",
"bn.js": "^0.3.1" "bn.js": "^0.4.3"
}, },
"devDependencies": { "devDependencies": {
"magnet-uri": "^2.0.1",
"parse-torrent": "^1.1.0", "parse-torrent": "^1.1.0",
"portfinder": "^0.2.1", "portfinder": "^0.2.1",
"tape": "2.x" "tape": "2.x"

View File

@ -11,7 +11,7 @@ var port = 6881
// remove all tracker servers except a single UDP one, for now // remove all tracker servers except a single UDP one, for now
parsedTorrent.announce = [ 'udp://tracker.publicbt.com:80/announce' ] parsedTorrent.announce = [ 'udp://tracker.publicbt.com:80/announce' ]
test('client.start()', function (t) { test('large torrent: client.start()', function (t) {
t.plan(4) t.plan(4)
var client = new Client(peerId, port, parsedTorrent) var client = new Client(peerId, port, parsedTorrent)

50
test/client-magnet.js Normal file
View File

@ -0,0 +1,50 @@
var Client = require('../').Client
var fs = require('fs')
var magnet = require('magnet-uri')
var test = require('tape')
var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80'
var parsedTorrent = magnet(uri)
var peerId = new Buffer('01234567890123456789')
var announceUrl = 'udp://tracker.openbittorrent.com:80' // TODO: shouldn't rely on an external server!
var port = 6881
test('magnet + udp: client.start/update/stop()', function (t) {
t.plan(10)
var client = new Client(peerId, port, parsedTorrent)
client.on('error', function (err) {
t.error(err)
})
client.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
})
client.once('peer', function (addr) {
t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server!
client.once('update', function (data) {
// receive one final update after calling stop
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
client.once('update', function (data) {
// received an update!
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')
})
client.stop()
})
client.update()
})
client.start()
})

View File

@ -3,13 +3,15 @@ var fs = require('fs')
var parseTorrent = require('parse-torrent') var parseTorrent = require('parse-torrent')
var test = require('tape') var test = require('tape')
// TODO: add test where tracker doesn't support compact
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent') var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
var parsedTorrent = parseTorrent(torrent) var parsedTorrent = parseTorrent(torrent)
var peerId = new Buffer('01234567890123456789') var peerId = new Buffer('01234567890123456789')
var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server! var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server!
var port = 6881 var port = 6881
test('client.start()', function (t) { test('torrent: client.start()', function (t) {
t.plan(4) t.plan(4)
var client = new Client(peerId, port, parsedTorrent) var client = new Client(peerId, port, parsedTorrent)
@ -32,7 +34,7 @@ test('client.start()', function (t) {
client.start() client.start()
}) })
test('client.stop()', function (t) { test('torrent: client.stop()', function (t) {
t.plan(4) t.plan(4)
var client = new Client(peerId, port, parsedTorrent) var client = new Client(peerId, port, parsedTorrent)
@ -59,7 +61,7 @@ test('client.stop()', function (t) {
}, 1000) }, 1000)
}) })
test('client.update()', function (t) { test('torrent: client.update()', function (t) {
t.plan(3) t.plan(3)
var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) var client = new Client(peerId, port, parsedTorrent, { interval: 5000 })
@ -83,7 +85,7 @@ test('client.update()', function (t) {
}) })
}) })
test('client.scrape()', function (t) { test('torrent: client.scrape()', function (t) {
t.plan(4) t.plan(4)
var client = new Client(peerId, port, parsedTorrent) var client = new Client(peerId, port, parsedTorrent)
@ -101,5 +103,3 @@ test('client.scrape()', function (t) {
client.scrape() client.scrape()
}) })
// TODO: add test where tracker doesn't support compact

View File

@ -43,7 +43,7 @@ test('udp: client.start/update/stop()', function (t) {
t.equal(typeof data.complete, 'number') t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number') t.equal(typeof data.incomplete, 'number')
}) })
client.stop() client.stop()
}) })