diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index 66dd760..3dbeb50 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -149,9 +149,9 @@ WebSocketTracker.prototype._onSocketData = function (data) { return } - if (data.action === common.ACTIONS.ANNOUNCE || data.offer || data.answer) { + if (data.action === 'announce' || data.offer || data.answer) { self._onAnnounceResponse(data) - } else if (data.action === common.ACTIONS.SCRAPE) { + } else if (data.action === 'scrape') { self._onScrapeResponse(data) } else { throw new Error('invalid action in WS response: ' + data.action) @@ -245,9 +245,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) { WebSocketTracker.prototype._onScrapeResponse = function (data) { var self = this - // NOTE: the unofficial spec says to use the 'files' key, 'host' has been - // seen in practice - data = data.files || data.host || {} + data = data.files || {} var keys = Object.keys(data) if (keys.length === 0) { diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index 9139964..720639e 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -8,7 +8,7 @@ function parseWebSocketRequest (socket, opts, params) { params.type = 'ws' params.socket = socket - if (params.action === 'announce' || params.answer || params.offers) { + if (params.action === 'announce' || params.offers || params.answer) { params.action = common.ACTIONS.ANNOUNCE if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { @@ -45,8 +45,6 @@ function parseWebSocketRequest (socket, opts, params) { } return common.binaryToHex(binaryInfoHash) }) - } else { - params.info_hash = common.binaryToHex(params.info_hash) } } else { throw new Error('invalid action in WS request: ' + params.action) diff --git a/server.js b/server.js index 3ee3d5b..c33e834 100644 --- a/server.js +++ b/server.js @@ -327,7 +327,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) { self._onRequest(params, function (err, response) { if (err) { socket.send(JSON.stringify({ - action: params.action, + action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape', 'failure reason': err.message, info_hash: common.hexToBinary(params.info_hash) }), socket.onSend) @@ -337,22 +337,18 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) { } if (self.destroyed) return - var hashes - if (typeof params.info_hash === 'string') hashes = [ params.info_hash ] - else hashes = params.info_hash - hashes.forEach(function (info_hash) { - if (socket.infoHashes.indexOf(info_hash) === -1) { - socket.infoHashes.push(info_hash) - } - }) + response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape' - var peers = response.peers - delete response.peers + var peers + if (response.action === 'announce') { + peers = response.peers + delete response.peers - // WebSocket tracker should have a shorter interval – default: 2 minutes - response.interval = Math.ceil(self.intervalMs / 1000 / 5) + response.info_hash = common.hexToBinary(params.info_hash) - response.info_hash = common.hexToBinary(params.info_hash) + // WebSocket tracker should have a shorter interval – default: 2 minutes + response.interval = Math.ceil(self.intervalMs / 1000 / 5) + } socket.send(JSON.stringify(response), socket.onSend) debug('sent response %s to %s', JSON.stringify(response), params.peer_id)