mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 02:21:36 +00:00
Add fixes and tests for PR #179
This commit is contained in:
parent
91cb2d3439
commit
c4f4f012dd
12
README.md
12
README.md
@ -59,12 +59,8 @@ var requiredOpts = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var optionalOpts = {
|
var optionalOpts = {
|
||||||
// RTCPeerConnection config object (only used in browser)
|
|
||||||
rtcConfig: {},
|
|
||||||
// custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
|
|
||||||
wrtc: {},
|
|
||||||
getAnnounceOpts: function () {
|
getAnnounceOpts: function () {
|
||||||
// provide a callback that will be called whenever announce() is called
|
// Provide a callback that will be called whenever announce() is called
|
||||||
// internally (on timer), or by the user
|
// internally (on timer), or by the user
|
||||||
return {
|
return {
|
||||||
uploaded: 0,
|
uploaded: 0,
|
||||||
@ -73,6 +69,12 @@ var optionalOpts = {
|
|||||||
customParam: 'blah' // custom parameters supported
|
customParam: 'blah' // custom parameters supported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// RTCPeerConnection config object (only used in browser)
|
||||||
|
rtcConfig: {},
|
||||||
|
// User-Agent header for http requests
|
||||||
|
userAgent: '',
|
||||||
|
// Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
|
||||||
|
wrtc: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = new Client(requiredOpts)
|
var client = new Client(requiredOpts)
|
||||||
|
26
client.js
26
client.js
@ -30,6 +30,7 @@ inherits(Client, EventEmitter)
|
|||||||
* @param {number} opts.port torrent client listening port
|
* @param {number} opts.port torrent client listening port
|
||||||
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
|
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
|
||||||
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
|
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
|
||||||
|
* @param {number} opts.userAgent User-Agent header for http requests
|
||||||
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
|
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
|
||||||
*/
|
*/
|
||||||
function Client (opts) {
|
function Client (opts) {
|
||||||
@ -43,7 +44,6 @@ function Client (opts) {
|
|||||||
if (!opts.announce) throw new Error('Option `announce` is required')
|
if (!opts.announce) throw new Error('Option `announce` is required')
|
||||||
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
|
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
|
||||||
|
|
||||||
// required
|
|
||||||
self.peerId = typeof opts.peerId === 'string'
|
self.peerId = typeof opts.peerId === 'string'
|
||||||
? opts.peerId
|
? opts.peerId
|
||||||
: opts.peerId.toString('hex')
|
: opts.peerId.toString('hex')
|
||||||
@ -56,39 +56,35 @@ function Client (opts) {
|
|||||||
self._infoHashBuffer = Buffer.from(self.infoHash, 'hex')
|
self._infoHashBuffer = Buffer.from(self.infoHash, 'hex')
|
||||||
self._infoHashBinary = self._infoHashBuffer.toString('binary')
|
self._infoHashBinary = self._infoHashBuffer.toString('binary')
|
||||||
|
|
||||||
self._port = opts.port
|
debug('new client %s', self.infoHash)
|
||||||
|
|
||||||
self.destroyed = false
|
self.destroyed = false
|
||||||
|
|
||||||
self._rtcConfig = opts.rtcConfig
|
self._port = opts.port
|
||||||
self._getAnnounceOpts = opts.getAnnounceOpts
|
self._getAnnounceOpts = opts.getAnnounceOpts
|
||||||
self._wrtc = opts.wrtc
|
self._rtcConfig = opts.rtcConfig
|
||||||
|
self._userAgent = opts.userAgent
|
||||||
|
|
||||||
// Support lazy 'wrtc' module initialization
|
// Support lazy 'wrtc' module initialization
|
||||||
// See: https://github.com/feross/webtorrent-hybrid/issues/46
|
// See: https://github.com/feross/webtorrent-hybrid/issues/46
|
||||||
if (typeof self._wrtc === 'function') self._wrtc = self._wrtc()
|
self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
|
||||||
|
|
||||||
debug('new client %s', self.infoHash)
|
var announce = typeof opts.announce === 'string'
|
||||||
|
|
||||||
var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT)
|
|
||||||
|
|
||||||
var announce = (typeof opts.announce === 'string')
|
|
||||||
? [ opts.announce ]
|
? [ opts.announce ]
|
||||||
: opts.announce == null
|
: opts.announce == null ? [] : opts.announce
|
||||||
? []
|
|
||||||
: opts.announce
|
|
||||||
|
|
||||||
|
// Remove trailing slash from trackers to catch duplicates
|
||||||
announce = announce.map(function (announceUrl) {
|
announce = announce.map(function (announceUrl) {
|
||||||
announceUrl = announceUrl.toString()
|
announceUrl = announceUrl.toString()
|
||||||
if (announceUrl[announceUrl.length - 1] === '/') {
|
if (announceUrl[announceUrl.length - 1] === '/') {
|
||||||
// remove trailing slash from trackers to catch duplicates
|
|
||||||
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
|
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
|
||||||
}
|
}
|
||||||
return announceUrl
|
return announceUrl
|
||||||
})
|
})
|
||||||
|
|
||||||
announce = uniq(announce)
|
announce = uniq(announce)
|
||||||
|
|
||||||
|
var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT)
|
||||||
|
|
||||||
self._trackers = announce
|
self._trackers = announce
|
||||||
.map(function (announceUrl) {
|
.map(function (announceUrl) {
|
||||||
var protocol = url.parse(announceUrl).protocol
|
var protocol = url.parse(announceUrl).protocol
|
||||||
|
@ -91,7 +91,7 @@ HTTPTracker.prototype._request = function (requestUrl, params, cb) {
|
|||||||
common.querystringStringify(params)
|
common.querystringStringify(params)
|
||||||
var opts = {
|
var opts = {
|
||||||
url: u,
|
url: u,
|
||||||
header: {
|
headers: {
|
||||||
'user-agent': self.client._userAgent || ''
|
'user-agent': self.client._userAgent || ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -380,3 +380,35 @@ test('http: client announce with numwant', function (t) {
|
|||||||
test('udp: client announce with numwant', function (t) {
|
test('udp: client announce with numwant', function (t) {
|
||||||
testClientAnnounceWithNumWant(t, 'udp')
|
testClientAnnounceWithNumWant(t, 'udp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('http: userAgent', function (t) {
|
||||||
|
t.plan(2)
|
||||||
|
|
||||||
|
common.createServer(t, 'http', function (server, announceUrl) {
|
||||||
|
// Confirm that user-agent header is set
|
||||||
|
server.http.on('request', function (req, res) {
|
||||||
|
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
|
||||||
|
})
|
||||||
|
|
||||||
|
var client = new Client({
|
||||||
|
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||||
|
announce: announceUrl,
|
||||||
|
peerId: peerId1,
|
||||||
|
port: port,
|
||||||
|
userAgent: 'WebTorrent/0.98.0 (https://webtorrent.io)',
|
||||||
|
wrtc: {}
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on('error', function (err) { t.error(err) })
|
||||||
|
client.on('warning', function (err) { t.error(err) })
|
||||||
|
|
||||||
|
client.once('update', function (data) {
|
||||||
|
t.equal(data.announce, announceUrl)
|
||||||
|
|
||||||
|
server.close()
|
||||||
|
client.destroy()
|
||||||
|
})
|
||||||
|
|
||||||
|
client.start()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user