forked from webtorrent/bittorrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-tracker.js
More file actions
220 lines (184 loc) · 6.4 KB
/
http-tracker.js
File metadata and controls
220 lines (184 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
module.exports = HTTPTracker
var bencode = require('bencode')
var compact2string = require('compact2string')
var debug = require('debug')('bittorrent-tracker:http-tracker')
var EventEmitter = require('events').EventEmitter
var get = require('simple-get')
var inherits = require('inherits')
var common = require('./common')
var HTTP_SCRAPE_SUPPORT = /\/(announce)[^\/]*$/
inherits(HTTPTracker, EventEmitter)
/**
* HTTP torrent tracker client (for an individual tracker)
*
* @param {Client} client parent bittorrent tracker client
* @param {string} announceUrl announce url of tracker
* @param {Object} opts options object
*/
function HTTPTracker (client, announceUrl, opts) {
var self = this
EventEmitter.call(self)
debug('new http tracker %s', announceUrl)
self.client = client
self.destroyed = false
self._opts = opts
self._announceUrl = announceUrl
self._intervalMs = self.client._intervalMs // use client interval initially
self._interval = null
// Determine scrape url (if http tracker supports it)
self._scrapeUrl = null
var m
if ((m = self._announceUrl.match(HTTP_SCRAPE_SUPPORT))) {
self._scrapeUrl = self._announceUrl.slice(0, m.index) + '/scrape' +
self._announceUrl.slice(m.index + 9)
}
}
HTTPTracker.prototype.announce = function (opts) {
var self = this
if (self._trackerId) opts.trackerid = self._trackerId
if (opts.compact == null) opts.compact = 1
if (opts.numwant == null) opts.numwant = self.client._numWant // spec says 'numwant'
opts.info_hash = self.client._infoHashBinary
opts.peer_id = self.client._peerIdBinary
opts.port = self.client._port
self._request(self._announceUrl, opts, self._onAnnounceResponse.bind(self))
}
HTTPTracker.prototype.scrape = function (opts) {
var self = this
if (!self._scrapeUrl) {
self.client.emit('error', new Error('scrape not supported ' + self._announceUrl))
return
}
opts.info_hash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(function (infoHash) { return infoHash.toString('binary') })
: (opts.infoHash || self.client._infoHash).toString('binary')
if (opts.infoHash) delete opts.infoHash
self._request(self._scrapeUrl, opts, self._onScrapeResponse.bind(self))
}
// TODO: Improve this interface
HTTPTracker.prototype.setInterval = function (intervalMs) {
var self = this
clearInterval(self._interval)
self._intervalMs = intervalMs
if (intervalMs) {
// HACK
var update = self.announce.bind(self, self.client._defaultAnnounceOpts())
self._interval = setInterval(update, self._intervalMs)
}
}
HTTPTracker.prototype.destroy = function () {
var self = this
self.destroyed = true
}
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {
var self = this
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
common.querystringStringify(opts)
get.concat(u, function (err, data, res) {
if (self.destroyed) return
if (err) return self.client.emit('warning', err)
if (res.statusCode !== 200) {
return self.client.emit('warning', new Error('Non-200 response code ' +
res.statusCode + ' from ' + self._announceUrl))
}
if (!data || data.length === 0) {
return self.client.emit('warning', new Error('Invalid tracker response from' +
self._announceUrl))
}
try {
data = bencode.decode(data)
} catch (err) {
return self.client.emit('warning', new Error('Error decoding tracker response: ' + err.message))
}
var failure = data['failure reason']
if (failure) {
debug('failure from ' + requestUrl + ' (' + failure + ')')
return self.client.emit('warning', new Error(failure))
}
var warning = data['warning message']
if (warning) {
debug('warning from ' + requestUrl + ' (' + warning + ')')
self.client.emit('warning', new Error(warning))
}
debug('response from ' + requestUrl)
cb(data)
})
}
HTTPTracker.prototype._onAnnounceResponse = function (data) {
var self = this
var interval = data.interval || data['min interval']
if (interval && !self._opts.interval && self._intervalMs !== 0) {
// use the interval the tracker recommends, UNLESS the user manually specifies an
// interval they want to use
self.setInterval(interval * 1000)
}
var trackerId = data['tracker id']
if (trackerId) {
// If absent, do not discard previous trackerId value
self._trackerId = trackerId
}
self.client.emit('update', {
announce: self._announceUrl,
complete: data.complete,
incomplete: data.incomplete
})
var addrs
if (Buffer.isBuffer(data.peers)) {
// tracker returned compact response
try {
addrs = compact2string.multi(data.peers)
} catch (err) {
return self.client.emit('warning', err)
}
addrs.forEach(function (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)
})
}
if (Buffer.isBuffer(data.peers6)) {
// tracker returned compact response
try {
addrs = compact2string.multi6(data.peers6)
} catch (err) {
return self.client.emit('warning', err)
}
addrs.forEach(function (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)
? peer.ip /* ipv6 w/ brackets or domain name */
: '[' + peer.ip + ']' /* ipv6 without brackets */
self.client.emit('peer', ip + ':' + peer.port)
})
}
}
HTTPTracker.prototype._onScrapeResponse = function (data) {
var 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)
if (keys.length === 0) {
self.client.emit('warning', new Error('invalid scrape response'))
return
}
keys.forEach(function (infoHash) {
var response = data[infoHash]
// TODO: optionally handle data.flags.min_request_interval
// (separate from announce interval)
self.client.emit('scrape', {
announce: self._announceUrl,
infoHash: common.binaryToHex(infoHash),
complete: response.complete,
incomplete: response.incomplete,
downloaded: response.downloaded
})
})
}