lebab lib/client

This commit is contained in:
Jimmy Wärting 2018-10-03 14:44:11 +02:00
parent 65b2bdc804
commit 386e0a5fbe
4 changed files with 934 additions and 938 deletions

View File

@ -1,18 +1,13 @@
module.exports = HTTPTracker
const arrayRemove = require('unordered-array-remove')
const bencode = require('bencode')
const compact2string = require('compact2string')
const debug = require('debug')('bittorrent-tracker:http-tracker')
const get = require('simple-get')
var arrayRemove = require('unordered-array-remove')
var bencode = require('bencode')
var compact2string = require('compact2string')
var debug = require('debug')('bittorrent-tracker:http-tracker')
var get = require('simple-get')
var inherits = require('inherits')
const common = require('../common')
const Tracker = require('./tracker')
var common = require('../common')
var Tracker = require('./tracker')
var HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/
inherits(HTTPTracker, Tracker)
const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/
/**
* HTTP torrent tracker client (for an individual tracker)
@ -21,32 +16,32 @@ inherits(HTTPTracker, Tracker)
* @param {string} announceUrl announce url of tracker
* @param {Object} opts options object
*/
function HTTPTracker (client, announceUrl, opts) {
var self = this
Tracker.call(self, client, announceUrl)
class HTTPTracker extends Tracker {
constructor (client, announceUrl, opts) {
super(client, announceUrl)
const self = this
debug('new http tracker %s', announceUrl)
// Determine scrape url (if http tracker supports it)
self.scrapeUrl = null
var match = self.announceUrl.match(HTTP_SCRAPE_SUPPORT)
const match = self.announceUrl.match(HTTP_SCRAPE_SUPPORT)
if (match) {
var pre = self.announceUrl.slice(0, match.index)
var post = self.announceUrl.slice(match.index + 9)
self.scrapeUrl = pre + '/scrape' + post
const pre = self.announceUrl.slice(0, match.index)
const post = self.announceUrl.slice(match.index + 9)
self.scrapeUrl = `${pre}/scrape${post}`
}
self.cleanupFns = []
self.maybeDestroyCleanup = null
}
}
HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
HTTPTracker.prototype.announce = function (opts) {
var self = this
announce (opts) {
const self = this
if (self.destroyed) return
var params = Object.assign({}, opts, {
const params = Object.assign({}, opts, {
compact: (opts.compact == null) ? 1 : opts.compact,
info_hash: self.client._infoHashBinary,
peer_id: self.client._peerIdBinary,
@ -54,37 +49,37 @@ HTTPTracker.prototype.announce = function (opts) {
})
if (self._trackerId) params.trackerid = self._trackerId
self._request(self.announceUrl, params, function (err, data) {
self._request(self.announceUrl, params, (err, data) => {
if (err) return self.client.emit('warning', err)
self._onAnnounceResponse(data)
})
}
}
HTTPTracker.prototype.scrape = function (opts) {
var self = this
scrape (opts) {
const self = this
if (self.destroyed) return
if (!self.scrapeUrl) {
self.client.emit('error', new Error('scrape not supported ' + self.announceUrl))
self.client.emit('error', new Error(`scrape not supported ${self.announceUrl}`))
return
}
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(function (infoHash) {
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => {
return infoHash.toString('binary')
})
: (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary
var params = {
const params = {
info_hash: infoHashes
}
self._request(self.scrapeUrl, params, function (err, data) {
self._request(self.scrapeUrl, params, (err, data) => {
if (err) return self.client.emit('warning', err)
self._onScrapeResponse(data)
})
}
}
HTTPTracker.prototype.destroy = function (cb) {
var self = this
destroy (cb) {
const self = this
if (self.destroyed) return cb(null)
self.destroyed = true
clearInterval(self.interval)
@ -98,7 +93,7 @@ HTTPTracker.prototype.destroy = function (cb) {
// But, if all pending requests complete before the timeout fires, do cleanup
// right away.
self.maybeDestroyCleanup = function () {
self.maybeDestroyCleanup = () => {
if (self.cleanupFns.length === 0) destroyCleanup()
}
@ -108,22 +103,22 @@ HTTPTracker.prototype.destroy = function (cb) {
timeout = null
}
self.maybeDestroyCleanup = null
self.cleanupFns.slice(0).forEach(function (cleanup) {
self.cleanupFns.slice(0).forEach(cleanup => {
cleanup()
})
self.cleanupFns = []
cb(null)
}
}
}
HTTPTracker.prototype._request = function (requestUrl, params, cb) {
var self = this
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
_request (requestUrl, params, cb) {
const self = this
const u = requestUrl + (!requestUrl.includes('?') ? '?' : '&') +
common.querystringStringify(params)
self.cleanupFns.push(cleanup)
var request = get.concat({
let request = get.concat({
url: u,
timeout: common.REQUEST_TIMEOUT,
headers: {
@ -146,56 +141,54 @@ HTTPTracker.prototype._request = function (requestUrl, params, cb) {
if (err) return cb(err)
if (res.statusCode !== 200) {
return cb(new Error('Non-200 response code ' +
res.statusCode + ' from ' + self.announceUrl))
return cb(new Error(`Non-200 response code ${res.statusCode} from ${self.announceUrl}`))
}
if (!data || data.length === 0) {
return cb(new Error('Invalid tracker response from' +
self.announceUrl))
return cb(new Error(`Invalid tracker response from${self.announceUrl}`))
}
try {
data = bencode.decode(data)
} catch (err) {
return cb(new Error('Error decoding tracker response: ' + err.message))
return cb(new Error(`Error decoding tracker response: ${err.message}`))
}
var failure = data['failure reason']
const failure = data['failure reason']
if (failure) {
debug('failure from ' + requestUrl + ' (' + failure + ')')
debug(`failure from ${requestUrl} (${failure})`)
return cb(new Error(failure))
}
var warning = data['warning message']
const warning = data['warning message']
if (warning) {
debug('warning from ' + requestUrl + ' (' + warning + ')')
debug(`warning from ${requestUrl} (${warning})`)
self.client.emit('warning', new Error(warning))
}
debug('response from ' + requestUrl)
debug(`response from ${requestUrl}`)
cb(null, data)
}
}
}
HTTPTracker.prototype._onAnnounceResponse = function (data) {
var self = this
_onAnnounceResponse (data) {
const self = this
var interval = data.interval || data['min interval']
const interval = data.interval || data['min interval']
if (interval) self.setInterval(interval * 1000)
var trackerId = data['tracker id']
const trackerId = data['tracker id']
if (trackerId) {
// If absent, do not discard previous trackerId value
self._trackerId = trackerId
}
var response = Object.assign({}, data, {
const response = Object.assign({}, data, {
announce: self.announceUrl,
infoHash: common.binaryToHex(data.info_hash)
})
self.client.emit('update', response)
var addrs
let addrs
if (Buffer.isBuffer(data.peers)) {
// tracker returned compact response
try {
@ -203,13 +196,13 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
} catch (err) {
return self.client.emit('warning', err)
}
addrs.forEach(function (addr) {
addrs.forEach(addr => {
self.client.emit('peer', addr)
})
} else if (Array.isArray(data.peers)) {
// tracker returned normal response
data.peers.forEach(function (peer) {
self.client.emit('peer', peer.ip + ':' + peer.port)
data.peers.forEach(peer => {
self.client.emit('peer', `${peer.ip}:${peer.port}`)
})
}
@ -220,39 +213,44 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
} catch (err) {
return self.client.emit('warning', err)
}
addrs.forEach(function (addr) {
addrs.forEach(addr => {
self.client.emit('peer', addr)
})
} else if (Array.isArray(data.peers6)) {
// tracker returned normal response
data.peers6.forEach(function (peer) {
var ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip)
data.peers6.forEach(peer => {
const ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip)
? peer.ip /* ipv6 w/ brackets or domain name */
: '[' + peer.ip + ']' /* ipv6 without brackets */
self.client.emit('peer', ip + ':' + peer.port)
: `[${peer.ip}]` /* ipv6 without brackets */
self.client.emit('peer', `${ip}:${peer.port}`)
})
}
}
}
HTTPTracker.prototype._onScrapeResponse = function (data) {
var self = this
_onScrapeResponse (data) {
const self = this
// NOTE: the unofficial spec says to use the 'files' key, 'host' has been
// seen in practice
data = data.files || data.host || {}
var keys = Object.keys(data)
const keys = Object.keys(data)
if (keys.length === 0) {
self.client.emit('warning', new Error('invalid scrape response'))
return
}
keys.forEach(function (infoHash) {
keys.forEach(infoHash => {
// TODO: optionally handle data.flags.min_request_interval
// (separate from announce interval)
var response = Object.assign(data[infoHash], {
const response = Object.assign(data[infoHash], {
announce: self.announceUrl,
infoHash: common.binaryToHex(infoHash)
})
self.client.emit('scrape', response)
})
}
}
HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
module.exports = HTTPTracker

View File

@ -1,30 +1,30 @@
module.exports = Tracker
const EventEmitter = require('events')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
class Tracker extends EventEmitter {
constructor (client, announceUrl) {
super()
inherits(Tracker, EventEmitter)
function Tracker (client, announceUrl) {
var self = this
EventEmitter.call(self)
const self = this
self.client = client
self.announceUrl = announceUrl
self.interval = null
self.destroyed = false
}
}
Tracker.prototype.setInterval = function (intervalMs) {
var self = this
setInterval (intervalMs) {
const self = this
if (intervalMs == null) intervalMs = self.DEFAULT_ANNOUNCE_INTERVAL
clearInterval(self.interval)
if (intervalMs) {
self.interval = setInterval(function () {
self.interval = setInterval(() => {
self.announce(self.client._defaultAnnounceOpts())
}, intervalMs)
if (self.interval.unref) self.interval.unref()
}
}
}
module.exports = Tracker

View File

@ -1,19 +1,14 @@
module.exports = UDPTracker
const arrayRemove = require('unordered-array-remove')
const BN = require('bn.js')
const Buffer = require('safe-buffer').Buffer
const compact2string = require('compact2string')
const debug = require('debug')('bittorrent-tracker:udp-tracker')
const dgram = require('dgram')
const randombytes = require('randombytes')
const url = require('url')
var arrayRemove = require('unordered-array-remove')
var BN = require('bn.js')
var Buffer = require('safe-buffer').Buffer
var compact2string = require('compact2string')
var debug = require('debug')('bittorrent-tracker:udp-tracker')
var dgram = require('dgram')
var inherits = require('inherits')
var randombytes = require('randombytes')
var url = require('url')
var common = require('../common')
var Tracker = require('./tracker')
inherits(UDPTracker, Tracker)
const common = require('../common')
const Tracker = require('./tracker')
/**
* UDP torrent tracker client (for an individual tracker)
@ -22,32 +17,31 @@ inherits(UDPTracker, Tracker)
* @param {string} announceUrl announce url of tracker
* @param {Object} opts options object
*/
function UDPTracker (client, announceUrl, opts) {
var self = this
Tracker.call(self, client, announceUrl)
class UDPTracker extends Tracker {
constructor (client, announceUrl, opts) {
super(client, announceUrl)
const self = this
debug('new udp tracker %s', announceUrl)
self.cleanupFns = []
self.maybeDestroyCleanup = null
}
}
UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
UDPTracker.prototype.announce = function (opts) {
var self = this
announce (opts) {
const self = this
if (self.destroyed) return
self._request(opts)
}
}
UDPTracker.prototype.scrape = function (opts) {
var self = this
scrape (opts) {
const self = this
if (self.destroyed) return
opts._scrape = true
self._request(opts) // udp scrape uses same announce url
}
}
UDPTracker.prototype.destroy = function (cb) {
var self = this
destroy (cb) {
const self = this
if (self.destroyed) return cb(null)
self.destroyed = true
clearInterval(self.interval)
@ -61,7 +55,7 @@ UDPTracker.prototype.destroy = function (cb) {
// But, if all pending requests complete before the timeout fires, do cleanup
// right away.
self.maybeDestroyCleanup = function () {
self.maybeDestroyCleanup = () => {
if (self.cleanupFns.length === 0) destroyCleanup()
}
@ -71,25 +65,25 @@ UDPTracker.prototype.destroy = function (cb) {
timeout = null
}
self.maybeDestroyCleanup = null
self.cleanupFns.slice(0).forEach(function (cleanup) {
self.cleanupFns.slice(0).forEach(cleanup => {
cleanup()
})
self.cleanupFns = []
cb(null)
}
}
}
UDPTracker.prototype._request = function (opts) {
var self = this
_request (opts) {
const self = this
if (!opts) opts = {}
var parsedUrl = url.parse(self.announceUrl)
var transactionId = genTransactionId()
var socket = dgram.createSocket('udp4')
const parsedUrl = url.parse(self.announceUrl)
let transactionId = genTransactionId()
let socket = dgram.createSocket('udp4')
var timeout = setTimeout(function () {
let timeout = setTimeout(() => {
// does not matter if `stopped` event arrives, so supress errors
if (opts.event === 'stopped') cleanup()
else onError(new Error('tracker request timed out (' + opts.event + ')'))
else onError(new Error(`tracker request timed out (${opts.event})`))
timeout = null
}, common.REQUEST_TIMEOUT)
if (timeout.unref) timeout.unref()
@ -125,7 +119,7 @@ UDPTracker.prototype._request = function (opts) {
cleanup()
if (self.destroyed) return
if (err.message) err.message += ' (' + self.announceUrl + ')'
if (err.message) err.message += ` (${self.announceUrl})`
// errors will often happen if a tracker is offline, so don't treat it as fatal
self.client.emit('warning', err)
}
@ -135,7 +129,7 @@ UDPTracker.prototype._request = function (opts) {
return onError(new Error('tracker sent invalid transaction id'))
}
var action = msg.readUInt32BE(0)
const action = msg.readUInt32BE(0)
debug('UDP response %s, action %s', self.announceUrl, action)
switch (action) {
case 0: // handshake
@ -155,7 +149,7 @@ UDPTracker.prototype._request = function (opts) {
if (msg.length < 20) return onError(new Error('invalid announce message'))
var interval = msg.readUInt32BE(8)
const interval = msg.readUInt32BE(8)
if (interval) self.setInterval(interval * 1000)
self.client.emit('update', {
@ -164,13 +158,13 @@ UDPTracker.prototype._request = function (opts) {
incomplete: msg.readUInt32BE(12)
})
var addrs
let addrs
try {
addrs = compact2string.multi(msg.slice(20))
} catch (err) {
return self.client.emit('warning', err)
}
addrs.forEach(function (addr) {
addrs.forEach(addr => {
self.client.emit('peer', addr)
})
@ -183,11 +177,11 @@ UDPTracker.prototype._request = function (opts) {
if (msg.length < 20 || (msg.length - 8) % 12 !== 0) {
return onError(new Error('invalid scrape message'))
}
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(function (infoHash) { return infoHash.toString('hex') })
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => { return infoHash.toString('hex') })
: [ (opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash ]
for (var i = 0, len = (msg.length - 8) / 12; i < len; i += 1) {
for (let i = 0, len = (msg.length - 8) / 12; i < len; i += 1) {
self.client.emit('scrape', {
announce: self.announceUrl,
infoHash: infoHashes[i],
@ -244,7 +238,7 @@ UDPTracker.prototype._request = function (opts) {
function scrape (connectionId) {
transactionId = genTransactionId()
var infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
const infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? Buffer.concat(opts.infoHash)
: (opts.infoHash || self.client._infoHashBuffer)
@ -255,23 +249,26 @@ UDPTracker.prototype._request = function (opts) {
infoHash
]))
}
}
}
UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
function genTransactionId () {
return randombytes(4)
}
function toUInt16 (n) {
var buf = Buffer.allocUnsafe(2)
const buf = Buffer.allocUnsafe(2)
buf.writeUInt16BE(n, 0)
return buf
}
var MAX_UINT = 4294967295
const MAX_UINT = 4294967295
function toUInt64 (n) {
if (n > MAX_UINT || typeof n === 'string') {
var bytes = new BN(n).toArray()
const bytes = new BN(n).toArray()
while (bytes.length < 8) {
bytes.unshift(0)
}
@ -281,3 +278,5 @@ function toUInt64 (n) {
}
function noop () {}
module.exports = UDPTracker

View File

@ -1,31 +1,25 @@
module.exports = WebSocketTracker
const debug = require('debug')('bittorrent-tracker:websocket-tracker')
const Peer = require('simple-peer')
const randombytes = require('randombytes')
const Socket = require('simple-websocket')
var debug = require('debug')('bittorrent-tracker:websocket-tracker')
var inherits = require('inherits')
var Peer = require('simple-peer')
var randombytes = require('randombytes')
var Socket = require('simple-websocket')
var common = require('../common')
var Tracker = require('./tracker')
const common = require('../common')
const Tracker = require('./tracker')
// 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 = {}
// Normally this shouldn't be accessed but is occasionally useful
WebSocketTracker._socketPool = socketPool
const socketPool = {}
var RECONNECT_MINIMUM = 15 * 1000
var RECONNECT_MAXIMUM = 30 * 60 * 1000
var RECONNECT_VARIANCE = 30 * 1000
var OFFER_TIMEOUT = 50 * 1000
const RECONNECT_MINIMUM = 15 * 1000
const RECONNECT_MAXIMUM = 30 * 60 * 1000
const RECONNECT_VARIANCE = 30 * 1000
const OFFER_TIMEOUT = 50 * 1000
inherits(WebSocketTracker, Tracker)
function WebSocketTracker (client, announceUrl, opts) {
var self = this
Tracker.call(self, client, announceUrl)
class WebSocketTracker extends Tracker {
constructor (client, announceUrl, opts) {
super(client, announceUrl)
const self = this
debug('new websocket tracker %s', announceUrl)
self.peers = {} // peers (offer id -> peer)
@ -40,21 +34,19 @@ function WebSocketTracker (client, announceUrl, opts) {
self.expectingResponse = false
self._openSocket()
}
}
WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds
WebSocketTracker.prototype.announce = function (opts) {
var self = this
announce (opts) {
const self = this
if (self.destroyed || self.reconnecting) return
if (!self.socket.connected) {
self.socket.once('connect', function () {
self.socket.once('connect', () => {
self.announce(opts)
})
return
}
var params = Object.assign({}, opts, {
const params = Object.assign({}, opts, {
action: 'announce',
info_hash: self.client._infoHashBinary,
peer_id: self.client._peerIdBinary
@ -66,41 +58,41 @@ WebSocketTracker.prototype.announce = function (opts) {
self._send(params)
} else {
// Limit the number of offers that are generated, since it can be slow
var numwant = Math.min(opts.numwant, 10)
const numwant = Math.min(opts.numwant, 10)
self._generateOffers(numwant, function (offers) {
self._generateOffers(numwant, offers => {
params.numwant = numwant
params.offers = offers
self._send(params)
})
}
}
}
WebSocketTracker.prototype.scrape = function (opts) {
var self = this
scrape (opts) {
const self = this
if (self.destroyed || self.reconnecting) return
if (!self.socket.connected) {
self.socket.once('connect', function () {
self.socket.once('connect', () => {
self.scrape(opts)
})
return
}
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(function (infoHash) {
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => {
return infoHash.toString('binary')
})
: (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary
var params = {
const params = {
action: 'scrape',
info_hash: infoHashes
}
self._send(params)
}
}
WebSocketTracker.prototype.destroy = function (cb) {
var self = this
destroy (cb) {
const self = this
if (!cb) cb = noop
if (self.destroyed) return cb(null)
@ -110,8 +102,8 @@ WebSocketTracker.prototype.destroy = function (cb) {
clearTimeout(self.reconnectTimer)
// Destroy peers
for (var peerId in self.peers) {
var peer = self.peers[peerId]
for (const peerId in self.peers) {
const peer = self.peers[peerId]
clearTimeout(peer.trackerTimeout)
peer.destroy()
}
@ -137,7 +129,7 @@ WebSocketTracker.prototype.destroy = function (cb) {
// Other instances are using the socket, so there's nothing left to do here
if (socketPool[self.announceUrl].consumers > 0) return cb()
var socket = socketPool[self.announceUrl]
let socket = socketPool[self.announceUrl]
delete socketPool[self.announceUrl]
socket.on('error', noop) // ignore all future errors
socket.once('close', cb)
@ -162,24 +154,24 @@ WebSocketTracker.prototype.destroy = function (cb) {
socket.destroy()
socket = null
}
}
}
WebSocketTracker.prototype._openSocket = function () {
var self = this
_openSocket () {
const self = this
self.destroyed = false
if (!self.peers) self.peers = {}
self._onSocketConnectBound = function () {
self._onSocketConnectBound = () => {
self._onSocketConnect()
}
self._onSocketErrorBound = function (err) {
self._onSocketErrorBound = err => {
self._onSocketError(err)
}
self._onSocketDataBound = function (data) {
self._onSocketDataBound = data => {
self._onSocketData(data)
}
self._onSocketCloseBound = function () {
self._onSocketCloseBound = () => {
self._onSocketClose()
}
@ -195,10 +187,10 @@ WebSocketTracker.prototype._openSocket = function () {
self.socket.on('data', self._onSocketDataBound)
self.socket.once('close', self._onSocketCloseBound)
self.socket.once('error', self._onSocketErrorBound)
}
}
WebSocketTracker.prototype._onSocketConnect = function () {
var self = this
_onSocketConnect () {
const self = this
if (self.destroyed) return
if (self.reconnecting) {
@ -206,10 +198,10 @@ WebSocketTracker.prototype._onSocketConnect = function () {
self.retries = 0
self.announce(self.client._defaultAnnounceOpts())
}
}
}
WebSocketTracker.prototype._onSocketData = function (data) {
var self = this
_onSocketData (data) {
const self = this
if (self.destroyed) return
self.expectingResponse = false
@ -226,12 +218,12 @@ WebSocketTracker.prototype._onSocketData = function (data) {
} else if (data.action === 'scrape') {
self._onScrapeResponse(data)
} else {
self._onSocketError(new Error('invalid action in WS response: ' + data.action))
self._onSocketError(new Error(`invalid action in WS response: ${data.action}`))
}
}
}
WebSocketTracker.prototype._onAnnounceResponse = function (data) {
var self = this
_onAnnounceResponse (data) {
const self = this
if (data.info_hash !== self.client._infoHashBinary) {
debug(
@ -251,41 +243,41 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
JSON.stringify(data), self.announceUrl, self.client.infoHash
)
var failure = data['failure reason']
const failure = data['failure reason']
if (failure) return self.client.emit('warning', new Error(failure))
var warning = data['warning message']
const warning = data['warning message']
if (warning) self.client.emit('warning', new Error(warning))
var interval = data.interval || data['min interval']
const interval = data.interval || data['min interval']
if (interval) self.setInterval(interval * 1000)
var trackerId = data['tracker id']
const trackerId = data['tracker id']
if (trackerId) {
// If absent, do not discard previous trackerId value
self._trackerId = trackerId
}
if (data.complete != null) {
var response = Object.assign({}, data, {
const response = Object.assign({}, data, {
announce: self.announceUrl,
infoHash: common.binaryToHex(data.info_hash)
})
self.client.emit('update', response)
}
var peer
let peer
if (data.offer && data.peer_id) {
debug('creating peer (from remote offer)')
peer = self._createPeer()
peer.id = common.binaryToHex(data.peer_id)
peer.once('signal', function (answer) {
var params = {
peer.once('signal', answer => {
const params = {
action: 'announce',
info_hash: self.client._infoHashBinary,
peer_id: self.client._peerIdBinary,
to_peer_id: data.peer_id,
answer: answer,
answer,
offer_id: data.offer_id
}
if (self._trackerId) params.trackerid = self._trackerId
@ -296,7 +288,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
}
if (data.answer && data.peer_id) {
var offerId = common.binaryToHex(data.offer_id)
const offerId = common.binaryToHex(data.offer_id)
peer = self.peers[offerId]
if (peer) {
peer.id = common.binaryToHex(data.peer_id)
@ -307,94 +299,94 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
peer.trackerTimeout = null
delete self.peers[offerId]
} else {
debug('got unexpected answer: ' + JSON.stringify(data.answer))
debug(`got unexpected answer: ${JSON.stringify(data.answer)}`)
}
}
}
}
WebSocketTracker.prototype._onScrapeResponse = function (data) {
var self = this
_onScrapeResponse (data) {
const self = this
data = data.files || {}
var keys = Object.keys(data)
const keys = Object.keys(data)
if (keys.length === 0) {
self.client.emit('warning', new Error('invalid scrape response'))
return
}
keys.forEach(function (infoHash) {
keys.forEach(infoHash => {
// TODO: optionally handle data.flags.min_request_interval
// (separate from announce interval)
var response = Object.assign(data[infoHash], {
const response = Object.assign(data[infoHash], {
announce: self.announceUrl,
infoHash: common.binaryToHex(infoHash)
})
self.client.emit('scrape', response)
})
}
}
WebSocketTracker.prototype._onSocketClose = function () {
var self = this
_onSocketClose () {
const self = this
if (self.destroyed) return
self.destroy()
self._startReconnectTimer()
}
}
WebSocketTracker.prototype._onSocketError = function (err) {
var self = this
_onSocketError (err) {
const self = this
if (self.destroyed) return
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) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM)
_startReconnectTimer () {
const self = this
const ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM)
self.reconnecting = true
clearTimeout(self.reconnectTimer)
self.reconnectTimer = setTimeout(function () {
self.reconnectTimer = setTimeout(() => {
self.retries++
self._openSocket()
}, ms)
if (self.reconnectTimer.unref) self.reconnectTimer.unref()
debug('reconnecting socket in %s ms', ms)
}
}
WebSocketTracker.prototype._send = function (params) {
var self = this
_send (params) {
const self = this
if (self.destroyed) return
self.expectingResponse = true
var message = JSON.stringify(params)
const message = JSON.stringify(params)
debug('send %s', message)
self.socket.send(message)
}
}
WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
var self = this
var offers = []
_generateOffers (numwant, cb) {
const self = this
const offers = []
debug('generating %s offers', numwant)
for (var i = 0; i < numwant; ++i) {
for (let i = 0; i < numwant; ++i) {
generateOffer()
}
checkDone()
function generateOffer () {
var offerId = randombytes(20).toString('hex')
const offerId = randombytes(20).toString('hex')
debug('creating peer (from _generateOffers)')
var peer = self.peers[offerId] = self._createPeer({ initiator: true })
peer.once('signal', function (offer) {
const peer = self.peers[offerId] = self._createPeer({ initiator: true })
peer.once('signal', offer => {
offers.push({
offer: offer,
offer,
offer_id: common.hexToBinary(offerId)
})
checkDone()
})
peer.trackerTimeout = setTimeout(function () {
peer.trackerTimeout = setTimeout(() => {
debug('tracker timeout: destroying peer')
peer.trackerTimeout = null
delete self.peers[offerId]
@ -409,10 +401,10 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
cb(offers)
}
}
}
}
WebSocketTracker.prototype._createPeer = function (opts) {
var self = this
_createPeer (opts) {
const self = this
opts = Object.assign({
trickle: false,
@ -420,7 +412,7 @@ WebSocketTracker.prototype._createPeer = function (opts) {
wrtc: self.client._wrtc
}, opts)
var peer = new Peer(opts)
const peer = new Peer(opts)
peer.once('error', onError)
peer.once('connect', onConnect)
@ -430,7 +422,7 @@ WebSocketTracker.prototype._createPeer = function (opts) {
// Handle peer 'error' events that are fired *before* the peer is emitted in
// a 'peer' event.
function onError (err) {
self.client.emit('warning', new Error('Connection error: ' + err.message))
self.client.emit('warning', new Error(`Connection error: ${err.message}`))
peer.destroy()
}
@ -440,6 +432,13 @@ WebSocketTracker.prototype._createPeer = function (opts) {
peer.removeListener('error', onError)
peer.removeListener('connect', onConnect)
}
}
}
WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds
// Normally this shouldn't be accessed but is occasionally useful
WebSocketTracker._socketPool = socketPool
function noop () {}
module.exports = WebSocketTracker