Skip to content

Commit d7b5675

Browse files
Merge remote-tracking branch 'upstream/master'
2 parents c4684c8 + c60fe18 commit d7b5675

File tree

6 files changed

+74
-16
lines changed

6 files changed

+74
-16
lines changed

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports.Client = Client
44
exports.Server = Server
55

6-
var bn = require('bn.js')
6+
var BN = require('bn.js')
77
var bncode = require('bncode')
88
var compact2string = require('compact2string')
99
var dgram = require('dgram')
@@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) {
7171
var self = this
7272
opts = opts || {}
7373
opts.event = 'completed'
74-
opts.downloaded = self._torrentLength
74+
opts.downloaded = opts.downloaded || self.torrentLength || 0
7575
self._request(opts)
7676
}
7777

@@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) {
126126
info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
127127
peer_id: bytewiseEncodeURIComponent(self.client._peerId),
128128
port: self.client._port,
129-
left: self.client._torrentLength - (opts.downloaded || 0),
130129
compact: 1,
131130
numwant: self.client._numWant,
132131
uploaded: 0, // default, user should provide real value
133132
downloaded: 0 // default, user should provide real value
134133
}, opts)
135134

135+
if (self.client.torrentLength !== undefined) {
136+
opts.left = self.client.torrentLength - (opts.downloaded || 0)
137+
}
138+
136139
if (self._trackerId) {
137140
opts.trackerid = self._trackerId
138141
}
@@ -267,7 +270,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
267270
self.client._infoHash,
268271
self.client._peerId,
269272
toUInt64(opts.downloaded || 0),
270-
toUInt64(opts.left || 0),
273+
opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
271274
toUInt64(opts.uploaded || 0),
272275
toUInt32(EVENTS[opts.event] || 0),
273276
toUInt32(0), // ip address (optional)
@@ -390,13 +393,17 @@ function Client (peerId, port, torrent, opts) {
390393
self._infoHash = Buffer.isBuffer(torrent.infoHash)
391394
? torrent.infoHash
392395
: new Buffer(torrent.infoHash, 'hex')
393-
self._torrentLength = torrent.length
396+
self.torrentLength = torrent.length
394397
self._announce = torrent.announce
395398

396399
// optional
397400
self._numWant = self._opts.numWant || 80
398401
self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes
399402

403+
if (typeof torrent.announce === 'string') {
404+
// magnet-uri returns a string if the magnet uri only contains one 'tr' parameter
405+
torrent.announce = [torrent.announce]
406+
}
400407
self._trackers = torrent.announce.map(function (announceUrl) {
401408
return new Tracker(self, announceUrl, self._opts)
402409
})
@@ -700,7 +707,7 @@ function toUInt32 (n) {
700707

701708
function toUInt64 (n) {
702709
if (n > MAX_UINT || typeof n === 'string') {
703-
var bytes = bn(n).toArray()
710+
var bytes = new BN(n).toArray()
704711
while (bytes.length < 8) {
705712
bytes.unshift(0)
706713
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bittorrent-tracker",
33
"description": "Simple, robust, BitTorrent tracker (client & server) implementation",
4-
"version": "0.6.0",
4+
"version": "0.7.0",
55
"author": {
66
"name": "Feross Aboukhadijeh",
77
"email": "[email protected]",
@@ -19,9 +19,10 @@
1919
"querystring": "^0.2.0",
2020
"run-parallel": "^1.0.0",
2121
"string2compact": "^1.1.0",
22-
"bn.js": "^0.3.1"
22+
"bn.js": "^0.4.3"
2323
},
2424
"devDependencies": {
25+
"magnet-uri": "^2.0.1",
2526
"parse-torrent": "^1.1.0",
2627
"portfinder": "^0.2.1",
2728
"tape": "2.x"

test/client-large-torrent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var port = 6881
1111
// remove all tracker servers except a single UDP one, for now
1212
parsedTorrent.announce = [ 'udp://tracker.publicbt.com:80/announce' ]
1313

14-
test('client.start()', function (t) {
14+
test('large torrent: client.start()', function (t) {
1515
t.plan(4)
1616

1717
var client = new Client(peerId, port, parsedTorrent)

test/client-magnet.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var Client = require('../').Client
2+
var fs = require('fs')
3+
var magnet = require('magnet-uri')
4+
var test = require('tape')
5+
6+
var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80'
7+
var parsedTorrent = magnet(uri)
8+
var peerId = new Buffer('01234567890123456789')
9+
var announceUrl = 'udp://tracker.openbittorrent.com:80' // TODO: shouldn't rely on an external server!
10+
var port = 6881
11+
12+
test('magnet + udp: client.start/update/stop()', function (t) {
13+
t.plan(10)
14+
15+
var client = new Client(peerId, port, parsedTorrent)
16+
17+
client.on('error', function (err) {
18+
t.error(err)
19+
})
20+
21+
client.once('update', function (data) {
22+
t.equal(data.announce, announceUrl)
23+
t.equal(typeof data.complete, 'number')
24+
t.equal(typeof data.incomplete, 'number')
25+
})
26+
27+
client.once('peer', function (addr) {
28+
t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server!
29+
30+
client.once('update', function (data) {
31+
// receive one final update after calling stop
32+
t.equal(data.announce, announceUrl)
33+
t.equal(typeof data.complete, 'number')
34+
t.equal(typeof data.incomplete, 'number')
35+
36+
client.once('update', function (data) {
37+
// received an update!
38+
t.equal(data.announce, announceUrl)
39+
t.equal(typeof data.complete, 'number')
40+
t.equal(typeof data.incomplete, 'number')
41+
})
42+
43+
client.stop()
44+
})
45+
46+
client.update()
47+
})
48+
49+
client.start()
50+
})

test/client.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ var fs = require('fs')
33
var parseTorrent = require('parse-torrent')
44
var test = require('tape')
55

6+
// TODO: add test where tracker doesn't support compact
7+
68
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
79
var parsedTorrent = parseTorrent(torrent)
810
var peerId = new Buffer('01234567890123456789')
911
var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server!
1012
var port = 6881
1113

12-
test('client.start()', function (t) {
14+
test('torrent: client.start()', function (t) {
1315
t.plan(4)
1416

1517
var client = new Client(peerId, port, parsedTorrent)
@@ -32,7 +34,7 @@ test('client.start()', function (t) {
3234
client.start()
3335
})
3436

35-
test('client.stop()', function (t) {
37+
test('torrent: client.stop()', function (t) {
3638
t.plan(4)
3739

3840
var client = new Client(peerId, port, parsedTorrent)
@@ -59,7 +61,7 @@ test('client.stop()', function (t) {
5961
}, 1000)
6062
})
6163

62-
test('client.update()', function (t) {
64+
test('torrent: client.update()', function (t) {
6365
t.plan(3)
6466

6567
var client = new Client(peerId, port, parsedTorrent, { interval: 5000 })
@@ -83,7 +85,7 @@ test('client.update()', function (t) {
8385
})
8486
})
8587

86-
test('client.scrape()', function (t) {
88+
test('torrent: client.scrape()', function (t) {
8789
t.plan(4)
8890

8991
var client = new Client(peerId, port, parsedTorrent)
@@ -101,5 +103,3 @@ test('client.scrape()', function (t) {
101103

102104
client.scrape()
103105
})
104-
105-
// TODO: add test where tracker doesn't support compact

test/udp-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('udp: client.start/update/stop()', function (t) {
4343
t.equal(typeof data.complete, 'number')
4444
t.equal(typeof data.incomplete, 'number')
4545
})
46-
46+
4747
client.stop()
4848
})
4949

0 commit comments

Comments
 (0)