-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
client.js
292 lines (252 loc) · 9.1 KB
/
client.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import Debug from 'debug'
import EventEmitter from 'events'
import once from 'once'
import parallel from 'run-parallel'
import Peer from '@thaunknown/simple-peer/lite.js'
import queueMicrotask from 'queue-microtask'
import { hex2arr, hex2bin, text2arr, arr2hex, arr2text } from 'uint8-util'
import common from './lib/common.js'
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
import UDPTracker from './lib/client/udp-tracker.js' // empty object in browser
import WebSocketTracker from './lib/client/websocket-tracker.js'
const debug = Debug('bittorrent-tracker:client')
/**
* BitTorrent tracker client.
*
* Find torrent peers, to help a torrent client participate in a torrent swarm.
*
* @param {Object} opts options object
* @param {string|Uint8Array} opts.infoHash torrent info hash
* @param {string|Uint8Array} 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
* @param {number} opts.userAgent User-Agent header for http requests
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
* @param {object} opts.proxyOpts proxy options (useful in node.js)
*/
class Client extends EventEmitter {
constructor (opts = {}) {
super()
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')
this.peerId = typeof opts.peerId === 'string'
? opts.peerId
: arr2hex(opts.peerId)
this._peerIdBuffer = hex2arr(this.peerId)
this._peerIdBinary = hex2bin(this.peerId)
this.infoHash = typeof opts.infoHash === 'string'
? opts.infoHash.toLowerCase()
: arr2hex(opts.infoHash)
this._infoHashBuffer = hex2arr(this.infoHash)
this._infoHashBinary = hex2bin(this.infoHash)
debug('new client %s', this.infoHash)
this.destroyed = false
this._port = opts.port
this._getAnnounceOpts = opts.getAnnounceOpts
this._rtcConfig = opts.rtcConfig
this._userAgent = opts.userAgent
this._proxyOpts = opts.proxyOpts
// Support lazy 'wrtc' module initialization
// See: https://github.com/webtorrent/webtorrent-hybrid/issues/46
this._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
let announce = typeof opts.announce === 'string'
? [opts.announce]
: opts.announce == null ? [] : opts.announce
// Remove trailing slash from trackers to catch duplicates
announce = announce.map(announceUrl => {
if (ArrayBuffer.isView(announceUrl)) announceUrl = arr2text(announceUrl)
if (announceUrl[announceUrl.length - 1] === '/') {
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
}
return announceUrl
})
// remove duplicates by converting to Set and back
announce = Array.from(new Set(announce))
const webrtcSupport = this._wrtc !== false && (!!this._wrtc || Peer.WEBRTC_SUPPORT)
const nextTickWarn = err => {
queueMicrotask(() => {
this.emit('warning', err)
})
}
this._trackers = announce
.map(announceUrl => {
let parsedUrl
try {
parsedUrl = common.parseUrl(announceUrl)
} catch (err) {
nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`))
return null
}
const port = parsedUrl.port
if (port < 0 || port > 65535) {
nextTickWarn(new Error(`Invalid tracker port: ${announceUrl}`))
return null
}
const protocol = parsedUrl.protocol
if ((protocol === 'http:' || protocol === 'https:') &&
typeof HTTPTracker === 'function') {
return new HTTPTracker(this, announceUrl)
} else if (protocol === 'udp:' && typeof UDPTracker === 'function') {
return new UDPTracker(this, announceUrl)
} else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) {
// 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}`))
return null
}
return new WebSocketTracker(this, announceUrl)
} else {
nextTickWarn(new Error(`Unsupported tracker protocol: ${announceUrl}`))
return null
}
})
.filter(Boolean)
}
/**
* 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)
*/
start (opts) {
opts = this._defaultAnnounceOpts(opts)
opts.event = 'started'
debug('send `start` %o', opts)
this._announce(opts)
// start announcing on intervals
this._trackers.forEach(tracker => {
tracker.setInterval()
})
}
/**
* Send a `stop` announce to the trackers.
* @param {Object} opts
* @param {number=} opts.uploaded
* @param {number=} opts.downloaded
* @param {number=} opts.numwant
* @param {number=} opts.left (if not set, calculated automatically)
*/
stop (opts) {
opts = this._defaultAnnounceOpts(opts)
opts.event = 'stopped'
debug('send `stop` %o', opts)
this._announce(opts)
}
/**
* Send a `complete` announce to the trackers.
* @param {Object} opts
* @param {number=} opts.uploaded
* @param {number=} opts.downloaded
* @param {number=} opts.numwant
* @param {number=} opts.left (if not set, calculated automatically)
*/
complete (opts) {
if (!opts) opts = {}
opts = this._defaultAnnounceOpts(opts)
opts.event = 'completed'
debug('send `complete` %o', opts)
this._announce(opts)
}
/**
* Send a `update` announce to the trackers.
* @param {Object} opts
* @param {number=} opts.uploaded
* @param {number=} opts.downloaded
* @param {number=} opts.numwant
* @param {number=} opts.left (if not set, calculated automatically)
*/
update (opts) {
opts = this._defaultAnnounceOpts(opts)
if (opts.event) delete opts.event
debug('send `update` %o', opts)
this._announce(opts)
}
_announce (opts) {
this._trackers.forEach(tracker => {
// tracker should not modify `opts` object, it's passed to all trackers
tracker.announce(opts)
})
}
/**
* Send a scrape request to the trackers.
* @param {Object} opts
*/
scrape (opts) {
debug('send `scrape`')
if (!opts) opts = {}
this._trackers.forEach(tracker => {
// tracker should not modify `opts` object, it's passed to all trackers
tracker.scrape(opts)
})
}
setInterval (intervalMs) {
debug('setInterval %d', intervalMs)
this._trackers.forEach(tracker => {
tracker.setInterval(intervalMs)
})
}
destroy (cb) {
if (this.destroyed) return
this.destroyed = true
debug('destroy')
const tasks = this._trackers.map(tracker => cb => {
tracker.destroy(cb)
})
parallel(tasks, cb)
this._trackers = []
this._getAnnounceOpts = null
}
_defaultAnnounceOpts (opts = {}) {
if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS
if (opts.uploaded == null) opts.uploaded = 0
if (opts.downloaded == null) opts.downloaded = 0
if (this._getAnnounceOpts) opts = Object.assign({}, opts, this._getAnnounceOpts())
return opts
}
}
/**
* 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.
* @params {Object} opts
* @param {string|Array.<string>} opts.infoHash
* @param {string} opts.announce
* @param {function} cb
*/
Client.scrape = (opts, cb) => {
cb = once(cb)
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
if (!opts.announce) throw new Error('Option `announce` is required')
const clientOpts = Object.assign({}, opts, {
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
peerId: text2arr('01234567890123456789'), // dummy value
port: 6881 // dummy value
})
const client = new Client(clientOpts)
client.once('error', cb)
client.once('warning', cb)
let len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1
const results = {}
client.on('scrape', data => {
len -= 1
results[data.infoHash] = data
if (len === 0) {
client.destroy()
const keys = Object.keys(results)
if (keys.length === 1) {
cb(null, results[keys[0]])
} else {
cb(null, results)
}
}
})
client.scrape({ infoHash: opts.infoHash })
return client
}
export default Client