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')
|
2016-02-29 20:34:06 +00:00
|
|
|
var extend = require('xtend')
|
2015-03-24 08:52:21 +00:00
|
|
|
var hat = require('hat')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var Peer = require('simple-peer')
|
|
|
|
var Socket = require('simple-websocket')
|
|
|
|
|
2015-07-29 07:26:44 +00:00
|
|
|
var common = require('../common')
|
2015-07-29 08:47:09 +00:00
|
|
|
var Tracker = require('./tracker')
|
2015-03-24 08:52:21 +00:00
|
|
|
|
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 = {}
|
|
|
|
|
2016-03-11 07:12:09 +00:00
|
|
|
var RECONNECT_MINIMUM = 15 * 1000
|
2016-03-16 04:14:24 +00:00
|
|
|
var RECONNECT_MAXIMUM = 30 * 60 * 1000
|
2015-05-17 07:07:21 +00:00
|
|
|
var RECONNECT_VARIANCE = 30 * 1000
|
2015-12-05 11:06:46 +00:00
|
|
|
var OFFER_TIMEOUT = 50 * 1000
|
2015-05-17 07:07:21 +00:00
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
inherits(WebSocketTracker, Tracker)
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
function WebSocketTracker (client, announceUrl, opts) {
|
|
|
|
var self = this
|
2015-07-29 08:47:09 +00:00
|
|
|
Tracker.call(self, client, announceUrl)
|
2015-03-24 08:52:21 +00:00
|
|
|
debug('new websocket tracker %s', announceUrl)
|
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
self.peers = {} // peers (offer id -> peer)
|
|
|
|
self.socket = null
|
2015-07-29 10:56:22 +00:00
|
|
|
self.reconnecting = false
|
2016-03-11 07:12:09 +00:00
|
|
|
self.retries = 0
|
2016-03-17 00:58:47 +00:00
|
|
|
self.reconnectTimer = null
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-05-17 07:07:21 +00:00
|
|
|
self._openSocket()
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 09:12:14 +00:00
|
|
|
WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds
|
|
|
|
|
2015-03-24 08:52:21 +00:00
|
|
|
WebSocketTracker.prototype.announce = function (opts) {
|
|
|
|
var self = this
|
2015-07-29 10:56:22 +00:00
|
|
|
if (self.destroyed || self.reconnecting) return
|
2015-07-29 08:47:09 +00:00
|
|
|
if (!self.socket.connected) {
|
|
|
|
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
|
|
|
|
2016-03-17 01:04:56 +00:00
|
|
|
var params = extend(opts, {
|
|
|
|
action: 'announce',
|
|
|
|
info_hash: self.client._infoHashBinary,
|
|
|
|
peer_id: self.client._peerIdBinary
|
|
|
|
})
|
|
|
|
if (self._trackerId) params.trackerid = self._trackerId
|
2015-07-27 22:19:18 +00:00
|
|
|
|
2016-03-17 01:04:56 +00:00
|
|
|
if (opts.event === 'stopped') {
|
|
|
|
// Don't include offers with 'stopped' event
|
2015-07-27 22:19:18 +00:00
|
|
|
self._send(params)
|
2016-03-17 01:04:56 +00:00
|
|
|
} else {
|
|
|
|
// Limit the number of offers that are generated, since it can be slow
|
|
|
|
var numwant = Math.min(opts.numwant, 10)
|
|
|
|
|
|
|
|
self._generateOffers(numwant, function (offers) {
|
|
|
|
params.numwant = numwant
|
|
|
|
params.offers = offers
|
|
|
|
self._send(params)
|
|
|
|
})
|
|
|
|
}
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype.scrape = function (opts) {
|
|
|
|
var self = this
|
2015-07-29 10:56:22 +00:00
|
|
|
if (self.destroyed || self.reconnecting) return
|
2016-03-13 23:15:41 +00:00
|
|
|
if (!self.socket.connected) {
|
|
|
|
return self.socket.once('connect', self.scrape.bind(self, opts))
|
|
|
|
}
|
|
|
|
|
|
|
|
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
|
|
|
|
? opts.infoHash.map(function (infoHash) {
|
|
|
|
return infoHash.toString('binary')
|
|
|
|
})
|
|
|
|
: (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary
|
|
|
|
var params = {
|
|
|
|
action: 'scrape',
|
|
|
|
info_hash: infoHashes
|
|
|
|
}
|
|
|
|
|
|
|
|
self._send(params)
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 02:16:00 +00:00
|
|
|
WebSocketTracker.prototype.destroy = function (cb) {
|
2015-05-17 06:25:34 +00:00
|
|
|
var self = this
|
2016-03-15 02:16:00 +00:00
|
|
|
if (!cb) cb = noop
|
|
|
|
if (self.destroyed) return cb(null)
|
2015-05-17 06:25:34 +00:00
|
|
|
self.destroyed = true
|
2015-07-29 08:47:09 +00:00
|
|
|
clearInterval(self.interval)
|
2016-03-16 08:22:33 +00:00
|
|
|
clearTimeout(self.reconnectTimer)
|
2015-05-17 07:07:21 +00:00
|
|
|
|
2015-07-29 10:56:22 +00:00
|
|
|
self.socket.removeListener('connect', self._onSocketConnectBound)
|
2015-07-29 08:47:09 +00:00
|
|
|
self.socket.removeListener('data', self._onSocketDataBound)
|
|
|
|
self.socket.removeListener('close', self._onSocketCloseBound)
|
|
|
|
self.socket.removeListener('error', self._onSocketErrorBound)
|
2015-07-17 01:33:50 +00:00
|
|
|
|
2015-07-29 10:56:22 +00:00
|
|
|
self._onSocketConnectBound = null
|
2015-05-17 07:07:21 +00:00
|
|
|
self._onSocketErrorBound = null
|
|
|
|
self._onSocketDataBound = null
|
|
|
|
self._onSocketCloseBound = null
|
|
|
|
|
2016-03-17 00:58:47 +00:00
|
|
|
// Destroy peers
|
|
|
|
for (var peerId in self.peers) {
|
|
|
|
var peer = self.peers[peerId]
|
|
|
|
clearTimeout(peer.trackerTimeout)
|
|
|
|
peer.destroy()
|
|
|
|
}
|
|
|
|
self.peers = null
|
|
|
|
|
|
|
|
if (socketPool[self.announceUrl]) {
|
|
|
|
socketPool[self.announceUrl].consumers -= 1
|
|
|
|
}
|
|
|
|
|
2016-03-14 15:29:51 +00:00
|
|
|
if (socketPool[self.announceUrl].consumers === 0) {
|
|
|
|
delete socketPool[self.announceUrl]
|
|
|
|
|
|
|
|
self.socket.on('error', noop) // ignore all future errors
|
|
|
|
|
|
|
|
try {
|
|
|
|
self.socket.destroy(cb)
|
|
|
|
} catch (err) {
|
2016-03-17 00:58:47 +00:00
|
|
|
cb(null)
|
2016-03-14 15:29:51 +00:00
|
|
|
}
|
2015-07-17 01:33:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:47:09 +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-07-29 10:56:22 +00:00
|
|
|
self.destroyed = false
|
|
|
|
|
|
|
|
self._onSocketConnectBound = self._onSocketConnect.bind(self)
|
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-07-29 08:47:09 +00:00
|
|
|
self.socket = socketPool[self.announceUrl]
|
2016-03-17 00:58:47 +00:00
|
|
|
if (self.socket) {
|
|
|
|
socketPool[self.announceUrl].consumers += 1
|
|
|
|
} else {
|
2015-07-29 08:47:09 +00:00
|
|
|
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
|
2016-03-14 15:29:51 +00:00
|
|
|
self.socket.consumers = 1
|
2015-07-29 10:56:22 +00:00
|
|
|
self.socket.on('connect', self._onSocketConnectBound)
|
2015-05-27 11:09:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:47:09 +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-07-29 10:56:22 +00:00
|
|
|
WebSocketTracker.prototype._onSocketConnect = function () {
|
|
|
|
var self = this
|
|
|
|
if (self.destroyed) return
|
|
|
|
|
|
|
|
if (self.reconnecting) {
|
|
|
|
self.reconnecting = false
|
2016-03-11 07:12:09 +00:00
|
|
|
self.retries = 0
|
2015-07-29 10:56:22 +00:00
|
|
|
self.announce(self.client._defaultAnnounceOpts())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-02-16 03:36:47 +00:00
|
|
|
try {
|
|
|
|
data = JSON.parse(data)
|
|
|
|
} catch (err) {
|
|
|
|
self.client.emit('warning', new Error('Invalid tracker response'))
|
|
|
|
return
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 03:06:39 +00:00
|
|
|
if (data.action === 'announce' || data.offer || data.answer) {
|
2016-03-13 23:15:41 +00:00
|
|
|
self._onAnnounceResponse(data)
|
2016-03-16 03:06:39 +00:00
|
|
|
} else if (data.action === 'scrape') {
|
2016-03-13 23:15:41 +00:00
|
|
|
self._onScrapeResponse(data)
|
|
|
|
} else {
|
2016-03-16 03:59:04 +00:00
|
|
|
self._onSocketError(new Error('invalid action in WS response: ' + data.action))
|
2016-03-13 23:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketTracker.prototype._onAnnounceResponse = function (data) {
|
|
|
|
var self = this
|
|
|
|
|
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)',
|
2015-12-02 23:46:41 +00:00
|
|
|
self.announceUrl, common.binaryToHex(data.info_hash), self.client.infoHash
|
2015-05-27 11:09:15 +00:00
|
|
|
)
|
|
|
|
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',
|
2015-12-02 23:46:41 +00:00
|
|
|
JSON.stringify(data), self.announceUrl, self.client.infoHash
|
2015-05-27 11:09:15 +00:00
|
|
|
)
|
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']
|
2015-07-29 08:47:09 +00:00
|
|
|
if (interval) self.setInterval(interval * 1000)
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
var trackerId = data['tracker id']
|
|
|
|
if (trackerId) {
|
|
|
|
// If absent, do not discard previous trackerId value
|
|
|
|
self._trackerId = trackerId
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:48:59 +00:00
|
|
|
if (data.complete != null) {
|
2015-03-24 08:52:21 +00:00
|
|
|
self.client.emit('update', {
|
2015-07-29 08:47:09 +00:00
|
|
|
announce: self.announceUrl,
|
2015-03-24 08:52:21 +00:00
|
|
|
complete: data.complete,
|
|
|
|
incomplete: data.incomplete
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var peer
|
2015-05-27 11:09:15 +00:00
|
|
|
if (data.offer && data.peer_id) {
|
2015-12-05 07:38:50 +00:00
|
|
|
debug('creating peer (from remote offer)')
|
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-12-05 09:01:04 +00:00
|
|
|
var offerId = common.binaryToHex(data.offer_id)
|
|
|
|
peer = self.peers[offerId]
|
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)
|
2015-12-05 09:01:04 +00:00
|
|
|
|
|
|
|
clearTimeout(peer.trackerTimeout)
|
2015-12-05 11:06:54 +00:00
|
|
|
peer.trackerTimeout = null
|
2016-03-11 05:26:28 +00:00
|
|
|
delete self.peers[offerId]
|
2015-03-24 08:52:21 +00:00
|
|
|
} else {
|
|
|
|
debug('got unexpected answer: ' + JSON.stringify(data.answer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 23:15:41 +00:00
|
|
|
WebSocketTracker.prototype._onScrapeResponse = function (data) {
|
|
|
|
var self = this
|
2016-03-16 03:06:39 +00:00
|
|
|
data = data.files || {}
|
2016-03-13 23:15:41 +00:00
|
|
|
|
|
|
|
var keys = Object.keys(data)
|
|
|
|
if (keys.length === 0) {
|
|
|
|
self.client.emit('warning', new Error('invalid scrape response'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.forEach(function (infoHash) {
|
|
|
|
var response = data[infoHash]
|
|
|
|
// TODO: optionally handle data.flags.min_request_interval
|
|
|
|
// (separate from announce interval)
|
|
|
|
self.client.emit('scrape', {
|
|
|
|
announce: self.announceUrl,
|
|
|
|
infoHash: common.binaryToHex(infoHash),
|
|
|
|
complete: response.complete,
|
|
|
|
incomplete: response.incomplete,
|
|
|
|
downloaded: response.downloaded
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2016-03-16 04:14:24 +00:00
|
|
|
var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM)
|
2015-07-17 01:33:54 +00:00
|
|
|
|
2015-07-29 10:56:22 +00:00
|
|
|
self.reconnecting = true
|
2016-03-16 08:22:33 +00:00
|
|
|
self.reconnectTimer = setTimeout(function () {
|
2016-03-11 07:12:09 +00:00
|
|
|
self.retries++
|
2015-05-27 11:09:15 +00:00
|
|
|
self._openSocket()
|
|
|
|
}, ms)
|
2016-03-16 08:22:33 +00:00
|
|
|
if (self.reconnectTimer.unref) self.reconnectTimer.unref()
|
2015-07-17 01:33:54 +00:00
|
|
|
|
2015-05-27 11:09:15 +00:00
|
|
|
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)
|
2015-07-29 08:47:09 +00:00
|
|
|
self.socket.send(message)
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
|
2015-03-24 08:52:21 +00:00
|
|
|
var self = this
|
|
|
|
var offers = []
|
2015-07-27 22:19:18 +00:00
|
|
|
debug('generating %s offers', numwant)
|
2015-03-24 08:52:21 +00:00
|
|
|
|
2015-07-27 22:19:18 +00:00
|
|
|
for (var i = 0; i < numwant; ++i) {
|
2015-03-24 08:52:21 +00:00
|
|
|
generateOffer()
|
|
|
|
}
|
2016-03-17 01:04:56 +00:00
|
|
|
checkDone()
|
2015-03-24 08:52:21 +00:00
|
|
|
|
|
|
|
function generateOffer () {
|
|
|
|
var offerId = hat(160)
|
2015-12-05 07:38:50 +00:00
|
|
|
debug('creating peer (from _generateOffers)')
|
2015-07-29 08:47:09 +00:00
|
|
|
var peer = self.peers[offerId] = new Peer({
|
2015-03-24 08:52:21 +00:00
|
|
|
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()
|
|
|
|
})
|
2015-12-05 09:01:04 +00:00
|
|
|
peer.trackerTimeout = setTimeout(function () {
|
2015-12-05 11:06:54 +00:00
|
|
|
debug('tracker timeout: destroying peer')
|
|
|
|
peer.trackerTimeout = null
|
2016-03-11 05:26:28 +00:00
|
|
|
delete self.peers[offerId]
|
2015-12-05 11:06:54 +00:00
|
|
|
peer.destroy()
|
2015-12-05 09:01:04 +00:00
|
|
|
}, OFFER_TIMEOUT)
|
2016-03-17 00:59:01 +00:00
|
|
|
if (peer.trackerTimeout.unref) peer.trackerTimeout.unref()
|
2015-03-24 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkDone () {
|
2015-07-27 22:19:18 +00:00
|
|
|
if (offers.length === numwant) {
|
|
|
|
debug('generated %s offers', numwant)
|
2015-03-24 08:52:21 +00:00
|
|
|
cb(offers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-17 20:53:36 +00:00
|
|
|
|
|
|
|
function noop () {}
|