2015-05-17 06:25:34 +00:00
|
|
|
// TODO: cleanup unused Peers when tracker doesn't respond with matches
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
module.exports = WebSocketTracker
|
|
|
|
|
2015-03-29 07:39:18 +00:00
|
|
|
var debug = require('debug')('bittorrent-tracker:websocket-tracker')
|
2015-03-24 08:52:21 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
var hat = require('hat')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var Peer = require('simple-peer')
|
|
|
|
var Socket = require('simple-websocket')
|
|
|
|
|
|
|
|
var common = require('./common')
|
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
// Use a socket pool, so tracker clients share WebSocket objects for the same server.
|
|
|
|
// In practice, WebSockets are pretty slow to establish, so this gives a nice performance
|
|
|
|
// boost, and saves browser resources.
|
|
|
|
var socketPool = {}
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
var RECONNECT_VARIANCE = 30 * 1000
|
|
|
|
var RECONNECT_MINIMUM = 5 * 1000
|
|
|
|
|
2015-03-24 08:52:21 +00:00
|
|
|
inherits(WebSocketTracker, EventEmitter)
|
|
|
|
|
|
|
|
function WebSocketTracker (client, announceUrl, opts) {
|
|
|
|
var self = this
|
|
|
|
EventEmitter.call(self)
|
|
|
|
debug('new websocket tracker %s', announceUrl)
|
|
|
|
|
|
|
|
self.client = client
|
2015-05-17 08:47:12 +00:00
|
|
|
self.destroyed = false
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-03-29 07:41:25 +00:00
|
|
|
self._opts = opts
|
2015-05-22 14:21:55 +00:00
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
if (announceUrl[announceUrl.length - 1] === '/') {
|
|
|
|
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
|
|
|
|
}
|
2015-03-24 08:52:21 +00:00
|
|
|
self._announceUrl = announceUrl
|
2015-05-22 14:21:55 +00:00
|
|
|
|
2015-03-24 08:52:21 +00:00
|
|
|
self._peers = {} // peers (offer id -> peer)
|
|
|
|
self._socket = null
|
|
|
|
self._intervalMs = self.client._intervalMs // use client interval initially
|
|
|
|
self._interval = null
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
self._openSocket()
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype.announce = function (opts) {
|
|
|
|
var self = this
|
2015-05-30 21:20:42 +00:00
|
|
|
if (self.destroyed) return
|
2015-04-10 23:58:21 +00:00
|
|
|
if (!self._socket.connected) {
|
2015-05-17 07:07:21 +00:00
|
|
|
return self._socket.once('connect', self.announce.bind(self, opts))
|
2015-04-10 23:58:21 +00:00
|
|
|
}
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-05-20 13:45:59 +00:00
|
|
|
opts.info_hash = self.client._infoHashBinary
|
|
|
|
opts.peer_id = self.client._peerIdBinary
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-05-19 11:32:17 +00:00
|
|
|
// Limit number of offers (temporarily)
|
2015-05-03 23:53:37 +00:00
|
|
|
// TODO: remove this when we cleanup old RTCPeerConnections cleanly
|
2015-05-19 11:32:17 +00:00
|
|
|
if (opts.numWant > 10) opts.numWant = 10
|
2015-05-03 23:53:37 +00:00
|
|
|
|
2015-03-24 08:52:21 +00:00
|
|
|
self._generateOffers(opts.numWant, function (offers) {
|
|
|
|
opts.offers = offers
|
|
|
|
|
|
|
|
if (self._trackerId) {
|
|
|
|
opts.trackerid = self._trackerId
|
|
|
|
}
|
|
|
|
self._send(opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype.scrape = function (opts) {
|
|
|
|
var self = this
|
2015-05-30 21:20:42 +00:00
|
|
|
if (self.destroyed) return
|
2015-04-10 23:58:21 +00:00
|
|
|
self._onSocketError(new Error('scrape not supported ' + self._announceUrl))
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Improve this interface
|
|
|
|
WebSocketTracker.prototype.setInterval = function (intervalMs) {
|
|
|
|
var self = this
|
|
|
|
clearInterval(self._interval)
|
|
|
|
|
|
|
|
self._intervalMs = intervalMs
|
|
|
|
if (intervalMs) {
|
|
|
|
// HACK
|
|
|
|
var update = self.announce.bind(self, self.client._defaultAnnounceOpts())
|
|
|
|
self._interval = setInterval(update, self._intervalMs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:33:50 +00:00
|
|
|
WebSocketTracker.prototype.destroy = function (onclose) {
|
2015-05-17 06:25:34 +00:00
|
|
|
var self = this
|
|
|
|
if (self.destroyed) return
|
|
|
|
self.destroyed = true
|
2015-05-17 07:07:21 +00:00
|
|
|
|
2015-05-17 06:25:34 +00:00
|
|
|
self._socket.removeListener('data', self._onSocketDataBound)
|
2015-05-17 07:07:21 +00:00
|
|
|
self._socket.removeListener('close', self._onSocketCloseBound)
|
|
|
|
self._socket.removeListener('error', self._onSocketErrorBound)
|
2015-07-17 01:33:50 +00:00
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
self._onSocketErrorBound = null
|
|
|
|
self._onSocketDataBound = null
|
|
|
|
self._onSocketCloseBound = null
|
|
|
|
|
2015-05-17 20:53:36 +00:00
|
|
|
self._socket.on('error', noop) // ignore all future errors
|
2015-07-17 01:33:50 +00:00
|
|
|
try {
|
|
|
|
self._socket.destroy(onclose)
|
|
|
|
} catch (err) {
|
|
|
|
if (onclose) onclose()
|
|
|
|
}
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
self._socket = null
|
2015-05-17 06:25:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
WebSocketTracker.prototype._openSocket = function () {
|
2015-04-10 23:58:21 +00:00
|
|
|
var self = this
|
2015-05-17 07:07:21 +00:00
|
|
|
self._onSocketErrorBound = self._onSocketError.bind(self)
|
|
|
|
self._onSocketDataBound = self._onSocketData.bind(self)
|
|
|
|
self._onSocketCloseBound = self._onSocketClose.bind(self)
|
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
self._socket = socketPool[self._announceUrl]
|
|
|
|
if (!self._socket) {
|
|
|
|
self._socket = socketPool[self._announceUrl] = new Socket(self._announceUrl)
|
|
|
|
}
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
self._socket.on('data', self._onSocketDataBound)
|
|
|
|
self._socket.on('close', self._onSocketCloseBound)
|
|
|
|
self._socket.on('error', self._onSocketErrorBound)
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 23:58:21 +00:00
|
|
|
WebSocketTracker.prototype._onSocketData = function (data) {
|
2015-03-24 08:52:21 +00:00
|
|
|
var self = this
|
2015-05-17 06:25:34 +00:00
|
|
|
if (self.destroyed) return
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
if (!(typeof data === 'object' && data !== null)) {
|
|
|
|
return self.client.emit('warning', new Error('Invalid tracker response'))
|
|
|
|
}
|
|
|
|
|
2015-05-20 13:45:59 +00:00
|
|
|
if (data.info_hash !== self.client._infoHashBinary) {
|
2015-05-27 11:09:15 +00:00
|
|
|
debug(
|
|
|
|
'ignoring websocket data from %s for %s (looking for %s: reused socket)',
|
|
|
|
self._announceUrl, common.binaryToHex(data.info_hash), self.client._infoHashHex
|
|
|
|
)
|
|
|
|
return
|
2015-03-29 07:40:21 +00:00
|
|
|
}
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
if (data.peer_id && data.peer_id === self.client._peerIdBinary) {
|
|
|
|
// ignore offers/answers from this client
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(
|
|
|
|
'received %s from %s for %s',
|
|
|
|
JSON.stringify(data), self._announceUrl, self.client._infoHashHex
|
|
|
|
)
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
var failure = data['failure reason']
|
|
|
|
if (failure) return self.client.emit('warning', new Error(failure))
|
|
|
|
|
|
|
|
var warning = data['warning message']
|
|
|
|
if (warning) self.client.emit('warning', new Error(warning))
|
|
|
|
|
|
|
|
var interval = data.interval || data['min interval']
|
|
|
|
if (interval && !self._opts.interval && self._intervalMs !== 0) {
|
|
|
|
// use the interval the tracker recommends, UNLESS the user manually specifies an
|
|
|
|
// interval they want to use
|
|
|
|
self.setInterval(interval * 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
var trackerId = data['tracker id']
|
|
|
|
if (trackerId) {
|
|
|
|
// If absent, do not discard previous trackerId value
|
|
|
|
self._trackerId = trackerId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.complete) {
|
|
|
|
self.client.emit('update', {
|
|
|
|
announce: self._announceUrl,
|
|
|
|
complete: data.complete,
|
|
|
|
incomplete: data.incomplete
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var peer
|
2015-05-27 11:09:15 +00:00
|
|
|
if (data.offer && data.peer_id) {
|
2015-05-04 00:21:08 +00:00
|
|
|
peer = new Peer({
|
|
|
|
trickle: false,
|
|
|
|
config: self.client._rtcConfig,
|
|
|
|
wrtc: self.client._wrtc
|
|
|
|
})
|
2015-03-24 08:52:21 +00:00
|
|
|
peer.id = common.binaryToHex(data.peer_id)
|
|
|
|
peer.once('signal', function (answer) {
|
2015-03-29 07:41:25 +00:00
|
|
|
var params = {
|
2015-05-20 13:45:59 +00:00
|
|
|
info_hash: self.client._infoHashBinary,
|
|
|
|
peer_id: self.client._peerIdBinary,
|
2015-03-24 08:52:21 +00:00
|
|
|
to_peer_id: data.peer_id,
|
|
|
|
answer: answer,
|
|
|
|
offer_id: data.offer_id
|
|
|
|
}
|
2015-03-29 07:41:25 +00:00
|
|
|
if (self._trackerId) params.trackerid = self._trackerId
|
|
|
|
self._send(params)
|
2015-03-24 08:52:21 +00:00
|
|
|
})
|
|
|
|
peer.signal(data.offer)
|
|
|
|
self.client.emit('peer', peer)
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
if (data.answer && data.peer_id) {
|
2015-03-29 08:08:26 +00:00
|
|
|
peer = self._peers[common.binaryToHex(data.offer_id)]
|
2015-03-24 08:52:21 +00:00
|
|
|
if (peer) {
|
|
|
|
peer.id = common.binaryToHex(data.peer_id)
|
|
|
|
peer.signal(data.answer)
|
|
|
|
self.client.emit('peer', peer)
|
|
|
|
} else {
|
|
|
|
debug('got unexpected answer: ' + JSON.stringify(data.answer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
WebSocketTracker.prototype._onSocketClose = function () {
|
|
|
|
var self = this
|
2015-05-27 11:09:15 +00:00
|
|
|
if (self.destroyed) return
|
2015-05-17 07:07:21 +00:00
|
|
|
self.destroy()
|
2015-05-27 11:09:15 +00:00
|
|
|
self._startReconnectTimer()
|
2015-05-17 07:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype._onSocketError = function (err) {
|
|
|
|
var self = this
|
|
|
|
if (self.destroyed) return
|
2015-05-27 11:09:15 +00:00
|
|
|
self.destroy()
|
|
|
|
// errors will often happen if a tracker is offline, so don't treat it as fatal
|
|
|
|
self.client.emit('warning', err)
|
|
|
|
self._startReconnectTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype._startReconnectTimer = function () {
|
|
|
|
var self = this
|
|
|
|
var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + RECONNECT_MINIMUM
|
|
|
|
setTimeout(function () {
|
|
|
|
self.destroyed = false
|
|
|
|
self._openSocket()
|
|
|
|
}, ms)
|
|
|
|
debug('reconnecting socket in %s ms', ms)
|
2015-05-17 07:07:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 07:41:25 +00:00
|
|
|
WebSocketTracker.prototype._send = function (params) {
|
2015-03-24 08:52:21 +00:00
|
|
|
var self = this
|
2015-05-17 06:25:34 +00:00
|
|
|
if (self.destroyed) return
|
|
|
|
|
2015-05-06 06:05:10 +00:00
|
|
|
var message = JSON.stringify(params)
|
|
|
|
debug('send %s', message)
|
|
|
|
self._socket.send(message)
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
|
|
|
|
var self = this
|
|
|
|
var offers = []
|
|
|
|
debug('generating %s offers', numWant)
|
|
|
|
|
|
|
|
// TODO: cleanup dead peers and peers that never get a return offer, from self._peers
|
|
|
|
for (var i = 0; i < numWant; ++i) {
|
|
|
|
generateOffer()
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateOffer () {
|
|
|
|
var offerId = hat(160)
|
|
|
|
var peer = self._peers[offerId] = new Peer({
|
|
|
|
initiator: true,
|
|
|
|
trickle: false,
|
2015-05-04 00:21:08 +00:00
|
|
|
config: self.client._rtcConfig,
|
|
|
|
wrtc: self.client._wrtc
|
2015-03-24 08:52:21 +00:00
|
|
|
})
|
|
|
|
peer.once('signal', function (offer) {
|
|
|
|
offers.push({
|
|
|
|
offer: offer,
|
2015-03-29 08:08:26 +00:00
|
|
|
offer_id: common.hexToBinary(offerId)
|
2015-03-24 08:52:21 +00:00
|
|
|
})
|
|
|
|
checkDone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkDone () {
|
|
|
|
if (offers.length === numWant) {
|
|
|
|
debug('generated %s offers', numWant)
|
|
|
|
cb(offers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-17 20:53:36 +00:00
|
|
|
|
|
|
|
function noop () {}
|