websocket tracker server/client: fix binary vs. hex bugs

This commit is contained in:
Feross Aboukhadijeh 2015-03-29 21:08:26 +13:00
parent 13e7ff7057
commit 7e7062d2ae
4 changed files with 16 additions and 15 deletions

View File

@ -18,9 +18,11 @@ function parseWebSocketRequest (socket, params) {
} }
params.peer_id = common.binaryToHex(params.peer_id) params.peer_id = common.binaryToHex(params.peer_id)
if (params.answer && if (params.answer) {
(typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20)) { if (typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20)) {
throw new Error('invalid `to_peer_id` (required with `answer`)') throw new Error('invalid `to_peer_id` (required with `answer`)')
}
params.to_peer_id = common.binaryToHex(params.to_peer_id)
} }
params.left = Number(params.left) || Infinity params.left = Number(params.left) || Infinity

View File

@ -49,7 +49,7 @@ Swarm.prototype._onAnnounce_started = function (params, peer) {
peer = this.peers[params.addr || params.peer_id] = { peer = this.peers[params.addr || params.peer_id] = {
complete: false, complete: false,
ip: params.ip, // only http+udp ip: params.ip, // only http+udp
peerId: params.peer_id, peerId: params.peer_id, // as hex
port: params.port, // only http+udp port: params.port, // only http+udp
socket: params.socket // only websocket socket: params.socket // only websocket
} }

View File

@ -140,7 +140,7 @@ WebSocketTracker.prototype._onSocketMessage = function (data) {
} }
if (data.answer) { if (data.answer) {
peer = self._peers[data.offer_id] peer = self._peers[common.binaryToHex(data.offer_id)]
if (peer) { if (peer) {
peer.id = common.binaryToHex(data.peer_id) peer.id = common.binaryToHex(data.peer_id)
peer.signal(data.answer) peer.signal(data.answer)
@ -177,7 +177,7 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
peer.once('signal', function (offer) { peer.once('signal', function (offer) {
offers.push({ offers.push({
offer: offer, offer: offer,
offer_id: offerId offer_id: common.hexToBinary(offerId)
}) })
checkDone() checkDone()
}) })

View File

@ -228,7 +228,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) {
Server.prototype.onWebSocketConnection = function (socket) { Server.prototype.onWebSocketConnection = function (socket) {
var self = this var self = this
socket.peerId = null socket.peerId = null // as hex
socket.infoHashes = [] socket.infoHashes = []
socket.onSend = self._onWebSocketSend.bind(self, socket) socket.onSend = self._onWebSocketSend.bind(self, socket)
socket.on('message', self._onWebSocketRequest.bind(self, socket)) socket.on('message', self._onWebSocketRequest.bind(self, socket))
@ -253,7 +253,7 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
return return
} }
if (!socket.peerId) socket.peerId = params.peer_id if (!socket.peerId) socket.peerId = params.peer_id // as hex
self._onRequest(params, function (err, response) { self._onRequest(params, function (err, response) {
if (err) { if (err) {
@ -270,11 +270,10 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
var peers = response.peers var peers = response.peers
delete response.peers delete response.peers
response.interval = self._intervalMs response.interval = self._intervalMs
response.info_hash = params.info_hash response.info_hash = params.info_hash // as hex
socket.send(JSON.stringify(response), socket.onSend)
socket.send(JSON.stringify(response), socket.onSend)
debug('sent response %s to %s', JSON.stringify(response), params.peer_id) debug('sent response %s to %s', JSON.stringify(response), params.peer_id)
if (params.numwant) { if (params.numwant) {
@ -284,8 +283,8 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
peer.socket.send(JSON.stringify({ peer.socket.send(JSON.stringify({
offer: params.offers[i].offer, offer: params.offers[i].offer,
offer_id: params.offers[i].offer_id, offer_id: params.offers[i].offer_id,
peer_id: params.peer_id, peer_id: common.hexToBinary(params.peer_id),
info_hash: params.info_hash info_hash: common.hexToBinary(params.info_hash)
})) }))
debug('sent offer to %s from %s', peer.peerId, params.peer_id) debug('sent offer to %s from %s', peer.peerId, params.peer_id)
}) })
@ -303,8 +302,8 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
toPeer.socket.send(JSON.stringify({ toPeer.socket.send(JSON.stringify({
answer: params.answer, answer: params.answer,
offer_id: params.offer_id, offer_id: params.offer_id,
peer_id: params.peer_id, peer_id: common.hexToBinary(params.peer_id),
info_hash: params.info_hash info_hash: common.hexToBinary(params.info_hash)
})) }))
debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) debug('sent answer to %s from %s', toPeer.peerId, params.peer_id)
} }