Skip to content

Commit 272a867

Browse files
committed
Merge pull request webtorrent#47 from feross/hex
Use hex info_hash and peer_id throughout
2 parents 991363a + 79068a4 commit 272a867

File tree

6 files changed

+69
-81
lines changed

6 files changed

+69
-81
lines changed

client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Client (peerId, port, torrent, opts) {
3636
// required
3737
self._peerId = Buffer.isBuffer(peerId)
3838
? peerId
39-
: new Buffer(peerId, 'utf8')
39+
: new Buffer(peerId, 'hex')
4040
self._port = port
4141
self._infoHash = Buffer.isBuffer(torrent.infoHash)
4242
? torrent.infoHash

lib/common.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.EVENT_IDS = {
1515
1: 'completed',
1616
2: 'started',
1717
3: 'stopped'
18-
};
18+
}
1919

2020
function toUInt32 (n) {
2121
var buf = new Buffer(4)
@@ -24,8 +24,12 @@ function toUInt32 (n) {
2424
}
2525
exports.toUInt32 = toUInt32
2626

27-
exports.binaryToUtf8 = function (str) {
28-
return new Buffer(str, 'binary').toString('utf8')
27+
exports.binaryToHex = function (str) {
28+
return new Buffer(str, 'binary').toString('hex')
29+
}
30+
31+
exports.hexToBinary = function (str) {
32+
return new Buffer(str, 'hex').toString('binary')
2933
}
3034

3135
/**

lib/parse_http.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,44 @@ function parseHttpRequest (req, options) {
1111
if (s[0] === '/announce') {
1212
params.action = common.ACTIONS.ANNOUNCE
1313

14-
params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id)
15-
params.port = Number(params.port)
14+
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20)
15+
throw new Error('invalid info_hash')
16+
params.info_hash = common.binaryToHex(params.info_hash)
17+
if (typeof params.peer_id !== 'string' || params.peer_id.length !== 20)
18+
throw new Error('invalid peer_id')
19+
params.peer_id = common.binaryToHex(params.peer_id)
1620

17-
if (typeof params.info_hash !== 'string') throw new Error('invalid info_hash')
18-
if (params.info_hash.length !== 20) throw new Error('invalid info_hash length')
19-
if (typeof params.peer_id !== 'string') throw new Error('invalid peer_id')
20-
if (params.peer_id.length !== 20) throw new Error('invalid peer_id length')
21+
params.port = Number(params.port)
2122
if (!params.port) throw new Error('invalid port')
2223

2324
params.left = Number(params.left)
2425
params.compact = Number(params.compact)
26+
params.numwant = Math.min(
27+
Number(params.numwant) || common.NUM_ANNOUNCE_PEERS,
28+
common.MAX_ANNOUNCE_PEERS
29+
)
2530

2631
params.ip = options.trustProxy
2732
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
2833
: req.connection.remoteAddress.replace(REMOVE_IPV6_RE, '') // force ipv4
2934
params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets?
3035

31-
params.numwant = Math.min(
32-
Number(params.numwant) || common.NUM_ANNOUNCE_PEERS,
33-
common.MAX_ANNOUNCE_PEERS
34-
)
35-
36-
return params
3736
} else if (s[0] === '/scrape') { // unofficial scrape message
3837
params.action = common.ACTIONS.SCRAPE
39-
40-
if (typeof params.info_hash === 'string') {
38+
if (typeof params.info_hash === 'string')
4139
params.info_hash = [ params.info_hash ]
42-
}
43-
44-
if (params.info_hash) {
45-
if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash array')
4640

47-
params.info_hash.forEach(function (infoHash) {
48-
if (infoHash.length !== 20) {
41+
if (Array.isArray(params.info_hash)) {
42+
params.info_hash = params.info_hash.map(function (binaryInfoHash) {
43+
if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20)
4944
throw new Error('invalid info_hash')
50-
}
45+
return common.binaryToHex(binaryInfoHash)
5146
})
5247
}
5348

54-
return params
5549
} else {
56-
return null
50+
throw new Error('Invalid action in HTTP request: ' + params.action)
5751
}
52+
53+
return params
5854
}

lib/parse_udp.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ var ipLib = require('ip')
55
var common = require('./common')
66

77
function parseUdpRequest (msg, rinfo) {
8-
if (msg.length < 16) {
8+
if (msg.length < 16)
99
throw new Error('received packet is too short')
10-
}
1110

12-
if (rinfo.family !== 'IPv4') {
11+
if (rinfo.family !== 'IPv4')
1312
throw new Error('udp tracker does not support IPv6')
14-
}
1513

1614
var params = {
1715
connectionId: msg.slice(0, 8), // 64-bit
@@ -20,41 +18,46 @@ function parseUdpRequest (msg, rinfo) {
2018
}
2119

2220
// TODO: randomize
23-
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
21+
if (!bufferEqual(params.connectionId, common.CONNECTION_ID))
2422
throw new Error('received packet with invalid connection id')
25-
}
2623

2724
if (params.action === common.ACTIONS.CONNECT) {
2825
// No further params
26+
2927
} else if (params.action === common.ACTIONS.ANNOUNCE) {
30-
params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes
31-
params.peer_id = msg.slice(36, 56).toString('utf8') // 20 bytes
28+
params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes
29+
params.peer_id = msg.slice(36, 56).toString('hex') // 20 bytes
3230
params.downloaded = fromUInt64(msg.slice(56, 64)) // TODO: track this?
3331
params.left = fromUInt64(msg.slice(64, 72))
3432
params.uploaded = fromUInt64(msg.slice(72, 80)) // TODO: track this?
35-
params.event = msg.readUInt32BE(80)
36-
params.event = common.EVENT_IDS[params.event]
33+
34+
params.event = common.EVENT_IDS[msg.readUInt32BE(80)]
3735
if (!params.event) throw new Error('invalid event') // early return
38-
params.ip = msg.readUInt32BE(84) // optional
39-
params.ip = params.ip ?
40-
ipLib.toString(params.ip) :
41-
rinfo.address
36+
37+
var ip = msg.readUInt32BE(84) // optional
38+
params.ip = ip
39+
? ipLib.toString(ip)
40+
: rinfo.address
41+
4242
params.key = msg.readUInt32BE(88) // TODO: what is this for?
43-
params.numwant = msg.readUInt32BE(92) // optional
43+
4444
// never send more than MAX_ANNOUNCE_PEERS or else the UDP packet will get bigger than
4545
// 512 bytes which is not safe
46-
params.numwant = Math.min(params.numwant || common.NUM_ANNOUNCE_PEERS, common.MAX_ANNOUNCE_PEERS)
46+
params.numwant = Math.min(
47+
msg.readUInt32BE(92) || common.NUM_ANNOUNCE_PEERS, // optional
48+
common.MAX_ANNOUNCE_PEERS
49+
)
50+
4751
params.port = msg.readUInt16BE(96) || rinfo.port // optional
4852
params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets
4953
params.compact = 1 // udp is always compact
5054

5155
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
52-
params.info_hash = msg.slice(16, 36).toString('binary') // 20 bytes
53-
5456
// TODO: support multiple info_hash scrape
55-
if (msg.length > 36) {
56-
throw new Error('multiple info_hash scrape not supported')
57-
}
57+
if (msg.length > 36) throw new Error('multiple info_hash scrape not supported')
58+
59+
params.info_hash = [ msg.slice(16, 36).toString('hex') ] // 20 bytes
60+
5861
} else {
5962
throw new Error('Invalid action in UDP packet: ' + params.action)
6063
}

server.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ Server.prototype.close = function (cb) {
115115
}
116116
}
117117

118-
Server.prototype.getSwarm = function (binaryInfoHash) {
118+
Server.prototype.getSwarm = function (infoHash) {
119119
var self = this
120-
if (Buffer.isBuffer(binaryInfoHash)) binaryInfoHash = binaryInfoHash.toString('binary')
121-
var swarm = self.torrents[binaryInfoHash]
122-
if (!swarm) {
123-
swarm = self.torrents[binaryInfoHash] = new Swarm(binaryInfoHash, this)
124-
}
120+
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
121+
var swarm = self.torrents[infoHash]
122+
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this)
125123
return swarm
126124
}
127125

@@ -130,9 +128,7 @@ Server.prototype._onHttpRequest = function (req, res) {
130128

131129
var params
132130
try {
133-
params = parseHttpRequest(req, {
134-
trustProxy: self._trustProxy
135-
})
131+
params = parseHttpRequest(req, { trustProxy: self._trustProxy })
136132
} catch (err) {
137133
debug('sent error %s', err.message)
138134
res.end(bencode.encode({
@@ -142,7 +138,7 @@ Server.prototype._onHttpRequest = function (req, res) {
142138
// even though it's an error for the client, it's just a warning for the server.
143139
// don't crash the server because a client sent bad data :)
144140
self.emit('warning', err)
145-
141+
146142
return
147143
}
148144

@@ -225,28 +221,12 @@ Server.prototype._onAnnounce = function (params, cb) {
225221
Server.prototype._onScrape = function (params, cb) {
226222
var self = this
227223

228-
if (typeof params.info_hash === 'string') {
229-
params.info_hash = [ params.info_hash ]
230-
} else if (params.info_hash == null) {
224+
if (params.info_hash == null) {
231225
// if info_hash param is omitted, stats for all torrents are returned
232226
// TODO: make this configurable!
233227
params.info_hash = Object.keys(self.torrents)
234228
}
235229

236-
if (!Array.isArray(params.info_hash)) {
237-
var err = new Error('invalid info_hash')
238-
self.emit('warning', err)
239-
return cb(err)
240-
}
241-
242-
var response = {
243-
action: common.ACTIONS.SCRAPE,
244-
files: {},
245-
flags: {
246-
min_request_interval: self._intervalMs
247-
}
248-
}
249-
250230
series(params.info_hash.map(function (infoHash) {
251231
var swarm = self.getSwarm(infoHash)
252232
return function (cb) {
@@ -261,8 +241,14 @@ Server.prototype._onScrape = function (params, cb) {
261241
}), function (err, results) {
262242
if (err) return cb(err)
263243

244+
var response = {
245+
action: common.ACTIONS.SCRAPE,
246+
files: {},
247+
flags: { min_request_interval: self._intervalMs }
248+
}
249+
264250
results.forEach(function (result) {
265-
response.files[result.infoHash] = {
251+
response.files[common.hexToBinary(result.infoHash)] = {
266252
complete: result.complete,
267253
incomplete: result.incomplete,
268254
downloaded: result.complete // TODO: this only provides a lower-bound
@@ -273,7 +259,6 @@ Server.prototype._onScrape = function (params, cb) {
273259
})
274260
}
275261

276-
277262
function makeUdpPacket (params) {
278263
switch (params.action) {
279264
case common.ACTIONS.CONNECT:

test/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ var Client = require('../')
22
var Server = require('../').Server
33
var test = require('tape')
44

5-
var infoHash = new Buffer('4cb67059ed6bd08362da625b3ae77f6f4a075705', 'hex')
6-
var peerId = '01234567890123456789'
7-
var peerId2 = '12345678901234567890'
5+
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
6+
var peerId = new Buffer('01234567890123456789')
7+
var peerId2 = new Buffer('12345678901234567890')
88
var torrentLength = 50000
99

1010
function serverTest (t, serverType) {
@@ -52,7 +52,7 @@ function serverTest (t, serverType) {
5252
t.deepEqual(server.getSwarm(infoHash).peers['127.0.0.1:6881'], {
5353
ip: '127.0.0.1',
5454
port: 6881,
55-
peerId: peerId
55+
peerId: peerId.toString('hex')
5656
})
5757

5858
client.complete()

0 commit comments

Comments
 (0)