Skip to content

Commit 9db28c2

Browse files
committed
massive cleanup
1 parent 3746c05 commit 9db28c2

File tree

17 files changed

+132
-174
lines changed

17 files changed

+132
-174
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var Server = require('bittorrent-tracker').Server
111111
var server = new Server({
112112
udp: true, // enable udp server? [default=true]
113113
http: true, // enable http server? [default=true]
114-
ws: true, // enable websocket server? [default=false]
114+
ws: true, // enable websocket server? [default=true]
115115
filter: function (infoHash, params, cb) {
116116
// Blacklist/whitelist function for allowing/disallowing torrents. If this option is
117117
// omitted, all torrents are allowed. It is possible to interface with a database or

client.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module.exports = Client
22

3-
var debug = require('debug')('bittorrent-tracker')
43
var EventEmitter = require('events').EventEmitter
4+
var debug = require('debug')('bittorrent-tracker')
55
var inherits = require('inherits')
66
var once = require('once')
77
var parallel = require('run-parallel')
8+
var uniq = require('uniq')
89
var url = require('url')
910

1011
var common = require('./lib/common')
@@ -23,8 +24,6 @@ inherits(Client, EventEmitter)
2324
* @param {Number} port torrent client listening port
2425
* @param {Object} torrent parsed torrent
2526
* @param {Object} opts options object
26-
* @param {Number} opts.numwant number of peers to request
27-
* @param {Number} opts.interval announce interval (in ms)
2827
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
2928
* @param {Number} opts.wrtc custom webrtc implementation
3029
*/
@@ -55,13 +54,8 @@ function Client (peerId, port, torrent, opts) {
5554
self._rtcConfig = opts.rtcConfig
5655
self._wrtc = opts.wrtc
5756

58-
// optional
59-
self._numwant = opts.numwant || common.DEFAULT_ANNOUNCE_PEERS
60-
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
61-
6257
debug('new client %s', self._infoHashHex)
6358

64-
var trackerOpts = { interval: self._intervalMs }
6559
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'
6660

6761
var announce = (typeof torrent.announce === 'string')
@@ -70,18 +64,27 @@ function Client (peerId, port, torrent, opts) {
7064
? []
7165
: torrent.announce
7266

67+
announce = announce.map(function (announceUrl) {
68+
announceUrl = announceUrl.toString()
69+
if (announceUrl[announceUrl.length - 1] === '/') {
70+
// remove trailing slash from trackers to catch duplicates
71+
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
72+
}
73+
return announceUrl
74+
})
75+
76+
announce = uniq(announce)
77+
7378
self._trackers = announce
7479
.map(function (announceUrl) {
75-
announceUrl = announceUrl.toString()
7680
var protocol = url.parse(announceUrl).protocol
77-
7881
if ((protocol === 'http:' || protocol === 'https:') &&
7982
typeof HTTPTracker === 'function') {
80-
return new HTTPTracker(self, announceUrl, trackerOpts)
83+
return new HTTPTracker(self, announceUrl)
8184
} else if (protocol === 'udp:' && typeof UDPTracker === 'function') {
82-
return new UDPTracker(self, announceUrl, trackerOpts)
85+
return new UDPTracker(self, announceUrl)
8386
} else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) {
84-
return new WebSocketTracker(self, announceUrl, trackerOpts)
87+
return new WebSocketTracker(self, announceUrl)
8588
} else {
8689
process.nextTick(function () {
8790
var err = new Error('unsupported tracker protocol for ' + announceUrl)
@@ -151,7 +154,7 @@ Client.prototype.start = function (opts) {
151154

152155
// start announcing on intervals
153156
self._trackers.forEach(function (tracker) {
154-
tracker.setInterval(self._intervalMs)
157+
tracker.setInterval()
155158
})
156159
}
157160

@@ -231,9 +234,7 @@ Client.prototype.scrape = function (opts) {
231234

232235
Client.prototype.setInterval = function (intervalMs) {
233236
var self = this
234-
debug('setInterval')
235-
self._intervalMs = intervalMs
236-
237+
debug('setInterval %d', intervalMs)
237238
self._trackers.forEach(function (tracker) {
238239
tracker.setInterval(intervalMs)
239240
})
@@ -248,7 +249,6 @@ Client.prototype.destroy = function (cb) {
248249
var tasks = self._trackers.map(function (tracker) {
249250
return function (cb) {
250251
tracker.destroy(cb)
251-
tracker.setInterval(0) // stop announcing on intervals
252252
}
253253
})
254254

@@ -260,7 +260,7 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
260260
var self = this
261261
if (!opts) opts = {}
262262

263-
if (opts.numwant == null) opts.numwant = self._numwant
263+
if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS
264264

265265
if (opts.uploaded == null) opts.uploaded = 0
266266
if (opts.downloaded == null) opts.downloaded = 0

examples/express-embed/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var whitelist = {
1212
var server = new Server({
1313
http: false, // we do our own
1414
udp: false, // not interested
15+
ws: false, // not interested
1516
filter: function (params) {
1617
// black/whitelist for disallowing/allowing specific clients [default=allow all]
1718
// this example only allows the uTorrent client

lib/client/http-tracker.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ module.exports = HTTPTracker
33
var bencode = require('bencode')
44
var compact2string = require('compact2string')
55
var debug = require('debug')('bittorrent-tracker:http-tracker')
6-
var EventEmitter = require('events').EventEmitter
76
var get = require('simple-get')
87
var inherits = require('inherits')
98

109
var common = require('../common')
10+
var Tracker = require('./tracker')
1111

1212
var HTTP_SCRAPE_SUPPORT = /\/(announce)[^\/]*$/
1313

14-
inherits(HTTPTracker, EventEmitter)
14+
inherits(HTTPTracker, Tracker)
1515

1616
/**
1717
* HTTP torrent tracker client (for an individual tracker)
@@ -22,23 +22,15 @@ inherits(HTTPTracker, EventEmitter)
2222
*/
2323
function HTTPTracker (client, announceUrl, opts) {
2424
var self = this
25-
EventEmitter.call(self)
25+
Tracker.call(self, client, announceUrl)
2626
debug('new http tracker %s', announceUrl)
2727

28-
self.client = client
29-
self.destroyed = false
30-
31-
self._opts = opts
32-
self._announceUrl = announceUrl
33-
self._intervalMs = self.client._intervalMs // use client interval initially
34-
self._interval = null
35-
3628
// Determine scrape url (if http tracker supports it)
37-
self._scrapeUrl = null
29+
self.scrapeUrl = null
3830
var m
39-
if ((m = self._announceUrl.match(HTTP_SCRAPE_SUPPORT))) {
40-
self._scrapeUrl = self._announceUrl.slice(0, m.index) + '/scrape' +
41-
self._announceUrl.slice(m.index + 9)
31+
if ((m = self.announceUrl.match(HTTP_SCRAPE_SUPPORT))) {
32+
self.scrapeUrl = self.announceUrl.slice(0, m.index) + '/scrape' +
33+
self.announceUrl.slice(m.index + 9)
4234
}
4335
}
4436

@@ -58,15 +50,15 @@ HTTPTracker.prototype.announce = function (opts) {
5850
}
5951
if (self._trackerId) params.trackerid = self._trackerId
6052

61-
self._request(self._announceUrl, params, self._onAnnounceResponse.bind(self))
53+
self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self))
6254
}
6355

6456
HTTPTracker.prototype.scrape = function (opts) {
6557
var self = this
6658
if (self.destroyed) return
6759

68-
if (!self._scrapeUrl) {
69-
self.client.emit('error', new Error('scrape not supported ' + self._announceUrl))
60+
if (!self.scrapeUrl) {
61+
self.client.emit('error', new Error('scrape not supported ' + self.announceUrl))
7062
return
7163
}
7264

@@ -78,44 +70,33 @@ HTTPTracker.prototype.scrape = function (opts) {
7870
var params = {
7971
info_hash: infoHashes
8072
}
81-
self._request(self._scrapeUrl, params, self._onScrapeResponse.bind(self))
82-
}
83-
84-
// TODO: Improve this interface
85-
HTTPTracker.prototype.setInterval = function (intervalMs) {
86-
var self = this
87-
self._intervalMs = intervalMs
88-
clearInterval(self._interval)
89-
90-
if (intervalMs) {
91-
// HACK
92-
var update = self.announce.bind(self, self.client._defaultAnnounceOpts())
93-
self._interval = setInterval(update, self._intervalMs)
94-
}
73+
self._request(self.scrapeUrl, params, self._onScrapeResponse.bind(self))
9574
}
9675

9776
HTTPTracker.prototype.destroy = function (cb) {
9877
var self = this
9978
if (self.destroyed) return
10079
self.destroyed = true
80+
clearInterval(self.interval)
81+
10182
cb(null)
10283
}
10384

10485
HTTPTracker.prototype._request = function (requestUrl, params, cb) {
10586
var self = this
106-
10787
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
10888
common.querystringStringify(params)
89+
10990
get.concat(u, function (err, data, res) {
11091
if (self.destroyed) return
11192
if (err) return self.client.emit('warning', err)
11293
if (res.statusCode !== 200) {
11394
return self.client.emit('warning', new Error('Non-200 response code ' +
114-
res.statusCode + ' from ' + self._announceUrl))
95+
res.statusCode + ' from ' + self.announceUrl))
11596
}
11697
if (!data || data.length === 0) {
11798
return self.client.emit('warning', new Error('Invalid tracker response from' +
118-
self._announceUrl))
99+
self.announceUrl))
119100
}
120101

121102
try {
@@ -145,11 +126,7 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
145126
var self = this
146127

147128
var interval = data.interval || data['min interval']
148-
if (interval && !self._opts.interval && self._intervalMs !== 0) {
149-
// use the interval the tracker recommends, UNLESS the user manually specifies an
150-
// interval they want to use
151-
self.setInterval(interval * 1000)
152-
}
129+
if (interval) self.setInterval(interval * 1000)
153130

154131
var trackerId = data['tracker id']
155132
if (trackerId) {
@@ -158,7 +135,7 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
158135
}
159136

160137
self.client.emit('update', {
161-
announce: self._announceUrl,
138+
announce: self.announceUrl,
162139
complete: data.complete,
163140
incomplete: data.incomplete
164141
})
@@ -219,7 +196,7 @@ HTTPTracker.prototype._onScrapeResponse = function (data) {
219196
// TODO: optionally handle data.flags.min_request_interval
220197
// (separate from announce interval)
221198
self.client.emit('scrape', {
222-
announce: self._announceUrl,
199+
announce: self.announceUrl,
223200
infoHash: common.binaryToHex(infoHash),
224201
complete: response.complete,
225202
incomplete: response.incomplete,

lib/client/tracker.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = Tracker
2+
3+
var EventEmitter = require('events').EventEmitter
4+
var inherits = require('inherits')
5+
6+
var common = require('../common')
7+
8+
inherits(Tracker, EventEmitter)
9+
10+
function Tracker (client, announceUrl) {
11+
var self = this
12+
EventEmitter.call(self)
13+
14+
self.client = client
15+
self.announceUrl = announceUrl
16+
self.interval = null
17+
self.destroyed = false
18+
}
19+
20+
Tracker.prototype.setInterval = function (intervalMs) {
21+
var self = this
22+
if (self.interval) return
23+
if (intervalMs == null) intervalMs = common.DEFAULT_ANNOUNCE_INTERVAL
24+
25+
clearInterval(self.interval)
26+
27+
if (intervalMs) {
28+
var update = self.announce.bind(self, self.client._defaultAnnounceOpts())
29+
self.interval = setInterval(update, intervalMs)
30+
}
31+
}

0 commit comments

Comments
 (0)