mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-02-22 13:19:04 +00:00
fixes for PR #107
This commit is contained in:
parent
1db2cb8736
commit
b34fc100a1
20
client.js
20
client.js
@ -1,7 +1,8 @@
|
|||||||
module.exports = Client
|
module.exports = Client
|
||||||
|
|
||||||
var EventEmitter = require('events').EventEmitter
|
|
||||||
var debug = require('debug')('bittorrent-tracker')
|
var debug = require('debug')('bittorrent-tracker')
|
||||||
|
var EventEmitter = require('events').EventEmitter
|
||||||
|
var extend = require('xtend')
|
||||||
var inherits = require('inherits')
|
var inherits = require('inherits')
|
||||||
var once = require('once')
|
var once = require('once')
|
||||||
var parallel = require('run-parallel')
|
var parallel = require('run-parallel')
|
||||||
@ -20,12 +21,13 @@ inherits(Client, EventEmitter)
|
|||||||
*
|
*
|
||||||
* Find torrent peers, to help a torrent client participate in a torrent swarm.
|
* Find torrent peers, to help a torrent client participate in a torrent swarm.
|
||||||
*
|
*
|
||||||
* @param {string|Buffer} peerId peer id
|
* @param {string|Buffer} peerId peer id
|
||||||
* @param {Number} port torrent client listening port
|
* @param {Number} port torrent client listening port
|
||||||
* @param {Object} torrent parsed torrent
|
* @param {Object} torrent parsed torrent
|
||||||
* @param {Object} opts options object
|
* @param {Object} opts options object
|
||||||
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
|
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
|
||||||
* @param {Number} opts.wrtc custom webrtc implementation
|
* @param {Number} opts.wrtc custom webrtc implementation
|
||||||
|
* @param {Object} opts.getAnnounceOpts callback to provide data to tracker
|
||||||
*/
|
*/
|
||||||
function Client (peerId, port, torrent, opts) {
|
function Client (peerId, port, torrent, opts) {
|
||||||
var self = this
|
var self = this
|
||||||
@ -270,9 +272,7 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
|
|||||||
opts.left = self.torrentLength - opts.downloaded
|
opts.left = self.torrentLength - opts.downloaded
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.getAnnounceOpts == null) {
|
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
|
||||||
opts.getAnnounceOpts = self._getAnnounceOpts
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ module.exports = HTTPTracker
|
|||||||
var bencode = require('bencode')
|
var bencode = require('bencode')
|
||||||
var compact2string = require('compact2string')
|
var compact2string = require('compact2string')
|
||||||
var debug = require('debug')('bittorrent-tracker:http-tracker')
|
var debug = require('debug')('bittorrent-tracker:http-tracker')
|
||||||
|
var extend = require('xtend')
|
||||||
var get = require('simple-get')
|
var get = require('simple-get')
|
||||||
var inherits = require('inherits')
|
var inherits = require('inherits')
|
||||||
|
|
||||||
@ -40,27 +41,12 @@ HTTPTracker.prototype.announce = function (opts) {
|
|||||||
var self = this
|
var self = this
|
||||||
if (self.destroyed) return
|
if (self.destroyed) return
|
||||||
|
|
||||||
// Refresh opts if the callback is provided
|
var params = extend(opts, {
|
||||||
var cbopts
|
|
||||||
if (opts.getAnnounceOpts) {
|
|
||||||
cbopts = opts.getAnnounceOpts()
|
|
||||||
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
|
|
||||||
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
|
|
||||||
if (cbopts.left) opts.left = cbopts.left
|
|
||||||
}
|
|
||||||
|
|
||||||
var params = {
|
|
||||||
numwant: opts.numwant,
|
|
||||||
uploaded: opts.uploaded,
|
|
||||||
downloaded: opts.downloaded,
|
|
||||||
left: opts.left,
|
|
||||||
event: opts.event,
|
|
||||||
compact: (opts.compact == null) ? 1 : opts.compact,
|
compact: (opts.compact == null) ? 1 : opts.compact,
|
||||||
info_hash: self.client._infoHashBinary,
|
info_hash: self.client._infoHashBinary,
|
||||||
peer_id: self.client._peerIdBinary,
|
peer_id: self.client._peerIdBinary,
|
||||||
port: self.client._port,
|
port: self.client._port
|
||||||
extras: cbopts && cbopts.extraAnnounceOpts
|
})
|
||||||
}
|
|
||||||
if (self._trackerId) params.trackerid = self._trackerId
|
if (self._trackerId) params.trackerid = self._trackerId
|
||||||
|
|
||||||
self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self))
|
self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self))
|
||||||
|
@ -189,15 +189,6 @@ UDPTracker.prototype._request = function (opts) {
|
|||||||
function announce (connectionId, opts) {
|
function announce (connectionId, opts) {
|
||||||
transactionId = genTransactionId()
|
transactionId = genTransactionId()
|
||||||
|
|
||||||
// Refresh opts if the callback is provided
|
|
||||||
var cbopts
|
|
||||||
if (opts.getAnnounceOpts) {
|
|
||||||
cbopts = opts.getAnnounceOpts()
|
|
||||||
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
|
|
||||||
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
|
|
||||||
if (cbopts.left) opts.left = cbopts.left
|
|
||||||
}
|
|
||||||
|
|
||||||
send(Buffer.concat([
|
send(Buffer.concat([
|
||||||
connectionId,
|
connectionId,
|
||||||
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
module.exports = WebSocketTracker
|
module.exports = WebSocketTracker
|
||||||
|
|
||||||
var debug = require('debug')('bittorrent-tracker:websocket-tracker')
|
var debug = require('debug')('bittorrent-tracker:websocket-tracker')
|
||||||
|
var extend = require('xtend')
|
||||||
var hat = require('hat')
|
var hat = require('hat')
|
||||||
var inherits = require('inherits')
|
var inherits = require('inherits')
|
||||||
var Peer = require('simple-peer')
|
var Peer = require('simple-peer')
|
||||||
@ -44,27 +45,13 @@ WebSocketTracker.prototype.announce = function (opts) {
|
|||||||
// Limit the number of offers that are generated, since it can be slow
|
// Limit the number of offers that are generated, since it can be slow
|
||||||
var numwant = Math.min(opts.numwant, 10)
|
var numwant = Math.min(opts.numwant, 10)
|
||||||
|
|
||||||
// Refresh opts if the callback is provided
|
|
||||||
var cbopts
|
|
||||||
if (opts.getAnnounceOpts) {
|
|
||||||
cbopts = opts.getAnnounceOpts()
|
|
||||||
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
|
|
||||||
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
|
|
||||||
if (cbopts.left) opts.left = cbopts.left
|
|
||||||
}
|
|
||||||
|
|
||||||
self._generateOffers(numwant, function (offers) {
|
self._generateOffers(numwant, function (offers) {
|
||||||
var params = {
|
var params = extend(opts, {
|
||||||
numwant: numwant,
|
numwant: numwant,
|
||||||
uploaded: opts.uploaded || 0,
|
|
||||||
downloaded: opts.downloaded,
|
|
||||||
left: opts.left,
|
|
||||||
event: opts.event,
|
|
||||||
info_hash: self.client._infoHashBinary,
|
info_hash: self.client._infoHashBinary,
|
||||||
peer_id: self.client._peerIdBinary,
|
peer_id: self.client._peerIdBinary,
|
||||||
offers: offers,
|
offers: offers
|
||||||
extras: cbopts && cbopts.extraAnnounceOpts
|
})
|
||||||
}
|
|
||||||
if (self._trackerId) params.trackerid = self._trackerId
|
if (self._trackerId) params.trackerid = self._trackerId
|
||||||
|
|
||||||
self._send(params)
|
self._send(params)
|
||||||
|
Loading…
Reference in New Issue
Block a user