From 7e7062d2ae8eea0a35a413fd92cf5d2ba10d931c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 29 Mar 2015 21:08:26 +1300 Subject: [PATCH] websocket tracker server/client: fix binary vs. hex bugs --- lib/parse_websocket.js | 8 +++++--- lib/swarm.js | 2 +- lib/websocket-tracker.js | 4 ++-- server.js | 17 ++++++++--------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/parse_websocket.js b/lib/parse_websocket.js index 9463e06..d876b71 100644 --- a/lib/parse_websocket.js +++ b/lib/parse_websocket.js @@ -18,9 +18,11 @@ function parseWebSocketRequest (socket, params) { } params.peer_id = common.binaryToHex(params.peer_id) - if (params.answer && - (typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20)) { - throw new Error('invalid `to_peer_id` (required with `answer`)') + if (params.answer) { + if (typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20)) { + 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 diff --git a/lib/swarm.js b/lib/swarm.js index 347b119..4029304 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -49,7 +49,7 @@ Swarm.prototype._onAnnounce_started = function (params, peer) { peer = this.peers[params.addr || params.peer_id] = { complete: false, ip: params.ip, // only http+udp - peerId: params.peer_id, + peerId: params.peer_id, // as hex port: params.port, // only http+udp socket: params.socket // only websocket } diff --git a/lib/websocket-tracker.js b/lib/websocket-tracker.js index cd97438..fc2af20 100644 --- a/lib/websocket-tracker.js +++ b/lib/websocket-tracker.js @@ -140,7 +140,7 @@ WebSocketTracker.prototype._onSocketMessage = function (data) { } if (data.answer) { - peer = self._peers[data.offer_id] + peer = self._peers[common.binaryToHex(data.offer_id)] if (peer) { peer.id = common.binaryToHex(data.peer_id) peer.signal(data.answer) @@ -177,7 +177,7 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) { peer.once('signal', function (offer) { offers.push({ offer: offer, - offer_id: offerId + offer_id: common.hexToBinary(offerId) }) checkDone() }) diff --git a/server.js b/server.js index 6a6f267..0ff6f4d 100644 --- a/server.js +++ b/server.js @@ -228,7 +228,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) { Server.prototype.onWebSocketConnection = function (socket) { var self = this - socket.peerId = null + socket.peerId = null // as hex socket.infoHashes = [] socket.onSend = self._onWebSocketSend.bind(self, socket) socket.on('message', self._onWebSocketRequest.bind(self, socket)) @@ -253,7 +253,7 @@ Server.prototype._onWebSocketRequest = function (socket, params) { 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) { if (err) { @@ -270,11 +270,10 @@ Server.prototype._onWebSocketRequest = function (socket, params) { var peers = response.peers delete response.peers - response.interval = self._intervalMs - response.info_hash = params.info_hash - socket.send(JSON.stringify(response), socket.onSend) + response.info_hash = params.info_hash // as hex + socket.send(JSON.stringify(response), socket.onSend) debug('sent response %s to %s', JSON.stringify(response), params.peer_id) if (params.numwant) { @@ -284,8 +283,8 @@ Server.prototype._onWebSocketRequest = function (socket, params) { peer.socket.send(JSON.stringify({ offer: params.offers[i].offer, offer_id: params.offers[i].offer_id, - peer_id: params.peer_id, - info_hash: params.info_hash + peer_id: common.hexToBinary(params.peer_id), + info_hash: common.hexToBinary(params.info_hash) })) 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({ answer: params.answer, offer_id: params.offer_id, - peer_id: params.peer_id, - info_hash: params.info_hash + peer_id: common.hexToBinary(params.peer_id), + info_hash: common.hexToBinary(params.info_hash) })) debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) }