2014-06-07 22:10:12 +00:00
|
|
|
module.exports = Client
|
|
|
|
|
2016-05-30 06:12:23 +00:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2017-01-21 02:34:33 +00:00
|
|
|
var debug = require('debug')('bittorrent-tracker:client')
|
2016-02-29 20:34:06 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
var extend = require('xtend')
|
2014-06-07 22:10:12 +00:00
|
|
|
var inherits = require('inherits')
|
2014-07-22 05:58:13 +00:00
|
|
|
var once = require('once')
|
2015-07-17 01:33:50 +00:00
|
|
|
var parallel = require('run-parallel')
|
2016-05-13 23:48:56 +00:00
|
|
|
var Peer = require('simple-peer')
|
2015-07-29 08:47:09 +00:00
|
|
|
var uniq = require('uniq')
|
2014-06-07 22:10:12 +00:00
|
|
|
var url = require('url')
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
var common = require('./lib/common')
|
2015-07-29 07:26:44 +00:00
|
|
|
var HTTPTracker = require('./lib/client/http-tracker') // empty object in browser
|
|
|
|
var UDPTracker = require('./lib/client/udp-tracker') // empty object in browser
|
|
|
|
var WebSocketTracker = require('./lib/client/websocket-tracker')
|
2015-03-24 08:01:49 +00:00
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
inherits(Client, EventEmitter)
|
|
|
|
|
|
|
|
/**
|
2015-03-27 03:19:06 +00:00
|
|
|
* BitTorrent tracker client.
|
|
|
|
*
|
|
|
|
* Find torrent peers, to help a torrent client participate in a torrent swarm.
|
2014-06-07 22:10:12 +00:00
|
|
|
*
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
* @param {Object} opts options object
|
|
|
|
* @param {string|Buffer} opts.infoHash torrent info hash
|
|
|
|
* @param {string|Buffer} opts.peerId peer id
|
|
|
|
* @param {string|Array.<string>} opts.announce announce
|
|
|
|
* @param {number} opts.port torrent client listening port
|
|
|
|
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
|
|
|
|
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
|
2017-01-16 23:43:47 +00:00
|
|
|
* @param {number} opts.userAgent User-Agent header for http requests
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
|
2014-06-07 22:10:12 +00:00
|
|
|
*/
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
function Client (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
if (!(self instanceof Client)) return new Client(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
EventEmitter.call(self)
|
2015-03-24 08:01:49 +00:00
|
|
|
if (!opts) opts = {}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
if (!opts.peerId) throw new Error('Option `peerId` is required')
|
|
|
|
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
|
|
|
|
if (!opts.announce) throw new Error('Option `announce` is required')
|
|
|
|
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
|
|
|
|
|
|
|
|
self.peerId = typeof opts.peerId === 'string'
|
|
|
|
? opts.peerId
|
|
|
|
: opts.peerId.toString('hex')
|
2016-05-30 06:12:23 +00:00
|
|
|
self._peerIdBuffer = Buffer.from(self.peerId, 'hex')
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
self._peerIdBinary = self._peerIdBuffer.toString('binary')
|
|
|
|
|
|
|
|
self.infoHash = typeof opts.infoHash === 'string'
|
|
|
|
? opts.infoHash
|
|
|
|
: opts.infoHash.toString('hex')
|
2016-05-30 06:12:23 +00:00
|
|
|
self._infoHashBuffer = Buffer.from(self.infoHash, 'hex')
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
self._infoHashBinary = self._infoHashBuffer.toString('binary')
|
2015-07-17 01:33:50 +00:00
|
|
|
|
2017-01-16 23:43:47 +00:00
|
|
|
debug('new client %s', self.infoHash)
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
|
|
|
|
self.destroyed = false
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2017-01-16 23:43:47 +00:00
|
|
|
self._port = opts.port
|
2015-11-04 17:01:35 +00:00
|
|
|
self._getAnnounceOpts = opts.getAnnounceOpts
|
2017-01-16 23:43:47 +00:00
|
|
|
self._rtcConfig = opts.rtcConfig
|
|
|
|
self._userAgent = opts.userAgent
|
2016-10-04 06:42:02 +00:00
|
|
|
|
|
|
|
// Support lazy 'wrtc' module initialization
|
|
|
|
// See: https://github.com/feross/webtorrent-hybrid/issues/46
|
2017-01-16 23:43:47 +00:00
|
|
|
self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
|
2015-05-04 00:21:08 +00:00
|
|
|
|
2017-01-16 23:43:47 +00:00
|
|
|
var announce = typeof opts.announce === 'string'
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
? [ opts.announce ]
|
2017-01-16 23:43:47 +00:00
|
|
|
: opts.announce == null ? [] : opts.announce
|
2015-05-17 05:54:45 +00:00
|
|
|
|
2017-01-16 23:43:47 +00:00
|
|
|
// Remove trailing slash from trackers to catch duplicates
|
2015-07-29 08:47:09 +00:00
|
|
|
announce = announce.map(function (announceUrl) {
|
|
|
|
announceUrl = announceUrl.toString()
|
|
|
|
if (announceUrl[announceUrl.length - 1] === '/') {
|
|
|
|
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
|
|
|
|
}
|
|
|
|
return announceUrl
|
|
|
|
})
|
|
|
|
announce = uniq(announce)
|
|
|
|
|
2017-01-16 23:43:47 +00:00
|
|
|
var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT)
|
|
|
|
|
2015-05-17 05:54:45 +00:00
|
|
|
self._trackers = announce
|
2014-08-02 21:09:44 +00:00
|
|
|
.map(function (announceUrl) {
|
2015-03-24 08:01:49 +00:00
|
|
|
var protocol = url.parse(announceUrl).protocol
|
2015-03-24 08:52:21 +00:00
|
|
|
if ((protocol === 'http:' || protocol === 'https:') &&
|
|
|
|
typeof HTTPTracker === 'function') {
|
2015-07-29 08:47:09 +00:00
|
|
|
return new HTTPTracker(self, announceUrl)
|
2015-03-24 08:52:21 +00:00
|
|
|
} else if (protocol === 'udp:' && typeof UDPTracker === 'function') {
|
2015-07-29 08:47:09 +00:00
|
|
|
return new UDPTracker(self, announceUrl)
|
2016-03-15 03:18:18 +00:00
|
|
|
} else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) {
|
2016-03-16 04:33:43 +00:00
|
|
|
// Skip ws:// trackers on https:// sites because they throw SecurityError
|
|
|
|
if (protocol === 'ws:' && typeof window !== 'undefined' &&
|
|
|
|
window.location.protocol === 'https:') {
|
|
|
|
nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl))
|
2016-03-15 03:16:25 +00:00
|
|
|
return null
|
|
|
|
}
|
2015-07-29 08:47:09 +00:00
|
|
|
return new WebSocketTracker(self, announceUrl)
|
2015-05-29 21:24:28 +00:00
|
|
|
} else {
|
2016-03-16 04:33:43 +00:00
|
|
|
nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl))
|
|
|
|
return null
|
2015-03-24 08:01:49 +00:00
|
|
|
}
|
2014-08-02 21:09:44 +00:00
|
|
|
})
|
2015-03-24 08:52:21 +00:00
|
|
|
.filter(Boolean)
|
2016-03-16 04:33:43 +00:00
|
|
|
|
|
|
|
function nextTickWarn (err) {
|
|
|
|
process.nextTick(function () {
|
|
|
|
self.emit('warning', err)
|
|
|
|
})
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 05:58:13 +00:00
|
|
|
/**
|
2015-05-02 00:36:07 +00:00
|
|
|
* Simple convenience function to scrape a tracker for an info hash without needing to
|
|
|
|
* create a Client, pass it a parsed torrent, etc. Support scraping a tracker for multiple
|
|
|
|
* torrents at the same time.
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
* @params {Object} opts
|
|
|
|
* @param {string|Array.<string>} opts.infoHash
|
|
|
|
* @param {string} opts.announce
|
2014-07-22 05:58:13 +00:00
|
|
|
* @param {function} cb
|
|
|
|
*/
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
Client.scrape = function (opts, cb) {
|
2014-07-22 05:58:13 +00:00
|
|
|
cb = once(cb)
|
2015-03-24 08:01:49 +00:00
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
|
|
|
|
if (!opts.announce) throw new Error('Option `announce` is required')
|
|
|
|
|
|
|
|
var clientOpts = extend(opts, {
|
|
|
|
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
|
2016-05-30 06:12:23 +00:00
|
|
|
peerId: Buffer.from('01234567890123456789'), // dummy value
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
port: 6881 // dummy value
|
|
|
|
})
|
|
|
|
|
|
|
|
var client = new Client(clientOpts)
|
2014-07-22 05:58:13 +00:00
|
|
|
client.once('error', cb)
|
2016-08-06 01:23:43 +00:00
|
|
|
client.once('warning', cb)
|
2015-05-02 00:36:07 +00:00
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
var len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1
|
2015-05-02 00:36:07 +00:00
|
|
|
var results = {}
|
|
|
|
client.on('scrape', function (data) {
|
|
|
|
len -= 1
|
|
|
|
results[data.infoHash] = data
|
|
|
|
if (len === 0) {
|
|
|
|
client.destroy()
|
|
|
|
var keys = Object.keys(results)
|
|
|
|
if (keys.length === 1) {
|
|
|
|
cb(null, results[keys[0]])
|
|
|
|
} else {
|
|
|
|
cb(null, results)
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 05:58:13 +00:00
|
|
|
})
|
2015-05-02 00:36:07 +00:00
|
|
|
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
opts.infoHash = Array.isArray(opts.infoHash)
|
2016-05-25 20:49:19 +00:00
|
|
|
? opts.infoHash.map(function (infoHash) {
|
2016-05-30 06:12:23 +00:00
|
|
|
return Buffer.from(infoHash, 'hex')
|
2016-05-25 20:49:19 +00:00
|
|
|
})
|
2016-05-30 06:12:23 +00:00
|
|
|
: Buffer.from(opts.infoHash, 'hex')
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
client.scrape({ infoHash: opts.infoHash })
|
|
|
|
return client
|
2014-07-22 05:58:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
/**
|
|
|
|
* Send a `start` announce to the trackers.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {number=} opts.uploaded
|
|
|
|
* @param {number=} opts.downloaded
|
|
|
|
* @param {number=} opts.left (if not set, calculated automatically)
|
|
|
|
*/
|
2014-06-07 22:10:12 +00:00
|
|
|
Client.prototype.start = function (opts) {
|
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('send `start`')
|
|
|
|
opts = self._defaultAnnounceOpts(opts)
|
|
|
|
opts.event = 'started'
|
|
|
|
self._announce(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
// start announcing on intervals
|
2015-01-29 22:59:08 +00:00
|
|
|
self._trackers.forEach(function (tracker) {
|
2015-07-29 08:47:09 +00:00
|
|
|
tracker.setInterval()
|
2015-01-29 22:59:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-07 22:10:12 +00:00
|
|
|
/**
|
2015-03-24 08:01:49 +00:00
|
|
|
* Send a `stop` announce to the trackers.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {number=} opts.uploaded
|
|
|
|
* @param {number=} opts.downloaded
|
2015-07-27 22:19:18 +00:00
|
|
|
* @param {number=} opts.numwant
|
2015-03-24 08:01:49 +00:00
|
|
|
* @param {number=} opts.left (if not set, calculated automatically)
|
2014-06-07 22:10:12 +00:00
|
|
|
*/
|
2015-03-24 08:01:49 +00:00
|
|
|
Client.prototype.stop = function (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('send `stop`')
|
|
|
|
opts = self._defaultAnnounceOpts(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
opts.event = 'stopped'
|
2014-07-13 01:44:41 +00:00
|
|
|
self._announce(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
/**
|
|
|
|
* Send a `complete` announce to the trackers.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {number=} opts.uploaded
|
|
|
|
* @param {number=} opts.downloaded
|
2015-07-27 22:19:18 +00:00
|
|
|
* @param {number=} opts.numwant
|
2015-03-24 08:01:49 +00:00
|
|
|
* @param {number=} opts.left (if not set, calculated automatically)
|
|
|
|
*/
|
|
|
|
Client.prototype.complete = function (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('send `complete`')
|
|
|
|
if (!opts) opts = {}
|
|
|
|
opts = self._defaultAnnounceOpts(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
opts.event = 'completed'
|
2014-07-13 01:44:41 +00:00
|
|
|
self._announce(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
/**
|
|
|
|
* Send a `update` announce to the trackers.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {number=} opts.uploaded
|
|
|
|
* @param {number=} opts.downloaded
|
2015-07-27 22:19:18 +00:00
|
|
|
* @param {number=} opts.numwant
|
2015-03-24 08:01:49 +00:00
|
|
|
* @param {number=} opts.left (if not set, calculated automatically)
|
|
|
|
*/
|
|
|
|
Client.prototype.update = function (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('send `update`')
|
|
|
|
opts = self._defaultAnnounceOpts(opts)
|
|
|
|
if (opts.event) delete opts.event
|
2014-07-13 01:44:41 +00:00
|
|
|
self._announce(opts)
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
Client.prototype._announce = function (opts) {
|
2015-01-29 22:59:08 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
self._trackers.forEach(function (tracker) {
|
2015-07-27 22:19:18 +00:00
|
|
|
// tracker should not modify `opts` object, it's passed to all trackers
|
2015-03-24 08:01:49 +00:00
|
|
|
tracker.announce(opts)
|
|
|
|
})
|
2015-01-29 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 01:44:41 +00:00
|
|
|
/**
|
2015-03-24 08:01:49 +00:00
|
|
|
* Send a scrape request to the trackers.
|
2014-07-13 01:44:41 +00:00
|
|
|
* @param {Object} opts
|
|
|
|
*/
|
2015-03-24 08:01:49 +00:00
|
|
|
Client.prototype.scrape = function (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('send `scrape`')
|
|
|
|
if (!opts) opts = {}
|
|
|
|
self._trackers.forEach(function (tracker) {
|
2015-07-27 22:19:18 +00:00
|
|
|
// tracker should not modify `opts` object, it's passed to all trackers
|
2015-03-24 08:01:49 +00:00
|
|
|
tracker.scrape(opts)
|
|
|
|
})
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
Client.prototype.setInterval = function (intervalMs) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-07-29 08:47:09 +00:00
|
|
|
debug('setInterval %d', intervalMs)
|
2015-03-24 08:01:49 +00:00
|
|
|
self._trackers.forEach(function (tracker) {
|
|
|
|
tracker.setInterval(intervalMs)
|
2014-06-07 22:10:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-08 17:14:11 +00:00
|
|
|
Client.prototype.destroy = function (cb) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-07-17 01:33:50 +00:00
|
|
|
if (self.destroyed) return
|
|
|
|
self.destroyed = true
|
2015-03-24 08:01:49 +00:00
|
|
|
debug('destroy')
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2015-07-17 01:33:50 +00:00
|
|
|
var tasks = self._trackers.map(function (tracker) {
|
|
|
|
return function (cb) {
|
|
|
|
tracker.destroy(cb)
|
|
|
|
}
|
2014-06-07 22:10:12 +00:00
|
|
|
})
|
2015-07-17 01:33:50 +00:00
|
|
|
|
|
|
|
parallel(tasks, cb)
|
2016-04-21 10:15:42 +00:00
|
|
|
|
2015-05-17 05:55:01 +00:00
|
|
|
self._trackers = []
|
2016-04-21 10:15:42 +00:00
|
|
|
self._getAnnounceOpts = null
|
2014-06-07 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
Client.prototype._defaultAnnounceOpts = function (opts) {
|
2014-06-07 22:10:12 +00:00
|
|
|
var self = this
|
2015-03-24 08:01:49 +00:00
|
|
|
if (!opts) opts = {}
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2015-07-29 08:47:09 +00:00
|
|
|
if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS
|
2014-06-07 22:10:12 +00:00
|
|
|
|
2015-03-24 08:01:49 +00:00
|
|
|
if (opts.uploaded == null) opts.uploaded = 0
|
|
|
|
if (opts.downloaded == null) opts.downloaded = 0
|
2014-07-20 11:34:32 +00:00
|
|
|
|
2016-02-29 20:34:06 +00:00
|
|
|
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
|
2015-03-24 08:01:49 +00:00
|
|
|
return opts
|
2014-07-20 11:34:32 +00:00
|
|
|
}
|