/** * WebTorrent Client * @param {Object} opts */ function WebTorrent (opts) { var self = this if (!(self instanceof WebTorrent)) return new WebTorrent(opts) EventEmitter.call(self) if (!opts) opts = {} if (!debug.enabled) self.setMaxListeners(0) self.destroyed = false self.torrentPort = opts.torrentPort || 0 self.tracker = opts.tracker !== undefined ? opts.tracker : true self._rtcConfig = opts.rtcConfig self._wrtc = opts.wrtc || global.WRTC // to support `webtorrent-hybrid` package self.torrents = [] self.downloadSpeed = speedometer() self.uploadSpeed = speedometer() self.peerId = typeof opts.peerId === 'string' ? opts.peerId : (opts.peerId || new Buffer(VERSION_PREFIX + hat(48))).toString('hex') self.peerIdBuffer = new Buffer(self.peerId, 'hex') self.nodeId = typeof opts.nodeId === 'string' ? opts.nodeId : (opts.nodeId && opts.nodeId.toString('hex')) || hat(160) self.nodeIdBuffer = new Buffer(self.nodeId, 'hex') if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { // use a single DHT instance for all torrents, so the routing table can be reused self.dht = new DHT(extend({ nodeId: self.nodeId }, opts.dht)) self.dht.listen(opts.dhtPort) } debug('new webtorrent (peerId %s, nodeId %s)', self.peerId, self.nodeId) if (typeof loadIPSet === 'function') { loadIPSet(opts.blocklist, { headers: { 'user-agent': 'WebTorrent/' + VERSION + ' (http://webtorrent.io)' } }, function (err, ipSet) { if (err) return self.error('Failed to load blocklist: ' + err.message) self.blocked = ipSet ready() }) } else process.nextTick(ready) function ready () { if (self.destroyed) return self.ready = true self.emit('ready') } }
/** * WebTorrent Client * @param {Object} opts */ function WebTorrent (opts) { var self = this if (!(self instanceof WebTorrent)) return new WebTorrent(opts) if (!opts) opts = {} EventEmitter.call(self) self.listening = false self.torrentPort = opts.torrentPort || 0 self.tracker = (opts.tracker !== undefined) ? opts.tracker : true self.torrents = [] self.downloadSpeed = speedometer() self.uploadSpeed = speedometer() self.storage = typeof opts.storage === 'function' ? opts.storage : (opts.storage !== false && typeof FSStorage === 'function' /* browser exclude */) ? FSStorage : Storage self.peerId = opts.peerId === undefined ? new Buffer('-WW0001-' + hat(48), 'utf8') : typeof opts.peerId === 'string' ? new Buffer(opts.peerId, 'utf8') : opts.peerId self.peerIdHex = self.peerId.toString('hex') self.nodeId = opts.nodeId === undefined ? new Buffer(hat(160), 'hex') : typeof opts.nodeId === 'string' ? new Buffer(opts.nodeId, 'hex') : opts.nodeId self.nodeIdHex = self.nodeId.toString('hex') // TODO: implement webtorrent-dht if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { // use a single DHT instance for all torrents, so the routing table can be reused self.dht = new DHT(extend({ nodeId: self.nodeId }, opts.dht)) self.dht.listen(opts.dhtPort) } debug('new webtorrent (peerId %s, nodeId %s)', self.peerIdHex, self.nodeIdHex) if (opts.port !== false && typeof Server === 'function' /* browser exclude */) { self.server = new Server(self, opts.port) self.server.on('listening', function () { self.listening = true self.emit('listening') }) } if (typeof loadIPSet === 'function') { loadIPSet(opts.blocklist, function (err, ipSet) { self.blocked = ipSet ready() }) } else process.nextTick(ready) function ready () { self.ready = true self.emit('ready') } }
/** * WebTorrent Client * @param {Object=} opts */ function WebTorrent (opts) { var self = this if (!(self instanceof WebTorrent)) return new WebTorrent(opts) EventEmitter.call(self) if (!opts) opts = {} if (typeof opts.peerId === 'string') { self.peerId = opts.peerId } else if (Buffer.isBuffer(opts.peerId)) { self.peerId = opts.peerId.toString('hex') } else { self.peerId = Buffer.from(VERSION_PREFIX + hat(48)).toString('hex') } self.peerIdBuffer = Buffer.from(self.peerId, 'hex') if (typeof opts.nodeId === 'string') { self.nodeId = opts.nodeId } else if (Buffer.isBuffer(opts.nodeId)) { self.nodeId = opts.nodeId.toString('hex') } else { self.nodeId = hat(160) } self.nodeIdBuffer = Buffer.from(self.nodeId, 'hex') self.destroyed = false self.listening = false self.torrentPort = opts.torrentPort || 0 self.dhtPort = opts.dhtPort || 0 self.tracker = opts.tracker !== undefined ? opts.tracker : {} self.torrents = [] self.maxConns = Number(opts.maxConns) || 55 if (self.tracker) { if (typeof self.tracker !== 'object') self.tracker = {} if (opts.rtcConfig) { // TODO: remove in v1 console.warn('WebTorrent: opts.rtcConfig is deprecated. Use opts.tracker.rtcConfig instead') self.tracker.rtcConfig = opts.rtcConfig } if (opts.wrtc) { // TODO: remove in v1 console.warn('WebTorrent: opts.wrtc is deprecated. Use opts.tracker.wrtc instead') self.tracker.wrtc = opts.wrtc // to support `webtorrent-hybrid` package } if (global.WRTC && !self.tracker.wrtc) self.tracker.wrtc = global.WRTC } if (typeof TCPPool === 'function') { self._tcpPool = new TCPPool(self) } else { process.nextTick(function () { self._onListening() }) } // stats self._downloadSpeed = speedometer() self._uploadSpeed = speedometer() if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { // use a single DHT instance for all torrents, so the routing table can be reused self.dht = new DHT(extend({ nodeId: self.nodeId }, opts.dht)) self.dht.once('error', function (err) { self._destroy(err) }) self.dht.once('listening', function () { var address = self.dht.address() if (address) self.dhtPort = address.port }) // Ignore warning when there are > 10 torrents in the client self.dht.setMaxListeners(0) self.dht.listen(self.dhtPort) } else { self.dht = false } debug('new webtorrent (peerId %s, nodeId %s)', self.peerId, self.nodeId) if (typeof loadIPSet === 'function' && opts.blocklist != null) { loadIPSet(opts.blocklist, { headers: { 'user-agent': 'WebTorrent/' + VERSION + ' (https://webtorrent.io)' } }, function (err, ipSet) { if (err) return self.error('Failed to load blocklist: ' + err.message) self.blocked = ipSet ready() }) } else { process.nextTick(ready) } function ready () { if (self.destroyed) return self.ready = true self.emit('ready') } }
constructor (opts = {}) { super() if (typeof opts.peerId === 'string') { this.peerId = opts.peerId } else if (Buffer.isBuffer(opts.peerId)) { this.peerId = opts.peerId.toString('hex') } else { this.peerId = Buffer.from(VERSION_PREFIX + randombytes(9).toString('base64')).toString('hex') } this.peerIdBuffer = Buffer.from(this.peerId, 'hex') if (typeof opts.nodeId === 'string') { this.nodeId = opts.nodeId } else if (Buffer.isBuffer(opts.nodeId)) { this.nodeId = opts.nodeId.toString('hex') } else { this.nodeId = randombytes(20).toString('hex') } this.nodeIdBuffer = Buffer.from(this.nodeId, 'hex') this._debugId = this.peerId.toString('hex').substring(0, 7) this.destroyed = false this.listening = false this.torrentPort = opts.torrentPort || 0 this.dhtPort = opts.dhtPort || 0 this.tracker = opts.tracker !== undefined ? opts.tracker : {} this.torrents = [] this.maxConns = Number(opts.maxConns) || 55 this._debug( 'new webtorrent (peerId %s, nodeId %s, port %s)', this.peerId, this.nodeId, this.torrentPort ) if (this.tracker) { if (typeof this.tracker !== 'object') this.tracker = {} if (opts.rtcConfig) { // TODO: remove in v1 console.warn('WebTorrent: opts.rtcConfig is deprecated. Use opts.tracker.rtcConfig instead') this.tracker.rtcConfig = opts.rtcConfig } if (opts.wrtc) { // TODO: remove in v1 console.warn('WebTorrent: opts.wrtc is deprecated. Use opts.tracker.wrtc instead') this.tracker.wrtc = opts.wrtc } if (global.WRTC && !this.tracker.wrtc) { this.tracker.wrtc = global.WRTC } } if (typeof TCPPool === 'function') { this._tcpPool = new TCPPool(this) } else { process.nextTick(() => { this._onListening() }) } // stats this._downloadSpeed = speedometer() this._uploadSpeed = speedometer() if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { // use a single DHT instance for all torrents, so the routing table can be reused this.dht = new DHT(Object.assign({}, { nodeId: this.nodeId }, opts.dht)) this.dht.once('error', err => { this._destroy(err) }) this.dht.once('listening', () => { const address = this.dht.address() if (address) this.dhtPort = address.port }) // Ignore warning when there are > 10 torrents in the client this.dht.setMaxListeners(0) this.dht.listen(this.dhtPort) } else { this.dht = false } // Enable or disable BEP19 (Web Seeds). Enabled by default: this.enableWebSeeds = opts.webSeeds !== false const ready = () => { if (this.destroyed) return this.ready = true this.emit('ready') } if (typeof loadIPSet === 'function' && opts.blocklist != null) { loadIPSet(opts.blocklist, { headers: { 'user-agent': `WebTorrent/${VERSION} (https://webtorrent.io)` } }, (err, ipSet) => { if (err) return this.error(`Failed to load blocklist: ${err.message}`) this.blocked = ipSet ready() }) } else { process.nextTick(ready) } }
/** * WebTorrent Client * @param {Object} opts */ function WebTorrent (opts) { var self = this if (!(self instanceof WebTorrent)) return new WebTorrent(opts) if (!opts) opts = {} EventEmitter.call(self) if (!debug.enabled) self.setMaxListeners(0) self.destroyed = false self.torrentPort = opts.torrentPort || 0 self.tracker = opts.tracker !== undefined ? opts.tracker : true self.rtcConfig = opts.rtcConfig self.torrents = [] self.downloadSpeed = speedometer() self.uploadSpeed = speedometer() self.storage = typeof opts.storage === 'function' ? opts.storage : (opts.storage !== false && typeof FSStorage === 'function' /* browser exclude */) ? FSStorage : Storage self.peerId = opts.peerId === undefined ? new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8') : typeof opts.peerId === 'string' ? new Buffer(opts.peerId, 'hex') : opts.peerId self.peerIdHex = self.peerId.toString('hex') self.nodeId = opts.nodeId === undefined ? new Buffer(hat(160), 'hex') : typeof opts.nodeId === 'string' ? new Buffer(opts.nodeId, 'hex') : opts.nodeId self.nodeIdHex = self.nodeId.toString('hex') // TODO: implement webtorrent-dht if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) { // use a single DHT instance for all torrents, so the routing table can be reused self.dht = new DHT(extend({ nodeId: self.nodeId }, opts.dht)) self.dht.listen(opts.dhtPort) } debug('new webtorrent (peerId %s, nodeId %s)', self.peerIdHex, self.nodeIdHex) if (typeof loadIPSet === 'function') { loadIPSet(opts.blocklist, { headers: { 'user-agent': 'WebTorrent (http://webtorrent.io)' } }, function (err, ipSet) { if (err) return self.error('failed to load blocklist: ' + err.message) self.blocked = ipSet ready() }) } else process.nextTick(ready) function ready () { if (self.destroyed) return self.ready = true self.emit('ready') } }