forked from webtorrent/torrent-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (135 loc) · 4.28 KB
/
Copy pathindex.js
File metadata and controls
163 lines (135 loc) · 4.28 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
module.exports = Discovery
var debug = require('debug')('torrent-discovery')
var DHT = require('bittorrent-dht/client') // empty object in browser
var EventEmitter = require('events').EventEmitter
var extend = require('xtend/mutable')
var inherits = require('inherits')
var parallel = require('run-parallel')
var reemit = require('re-emitter')
var Tracker = require('bittorrent-tracker/client')
inherits(Discovery, EventEmitter)
function Discovery (opts) {
var self = this
if (!(self instanceof Discovery)) return new Discovery(opts)
EventEmitter.call(self)
extend(self, {
announce: [],
dht: typeof DHT === 'function',
rtcConfig: null, // browser only
peerId: null,
port: 0, // torrent port
tracker: true,
wrtc: null
}, opts)
self.infoHash = null
self.infoHashHex = null
self.torrent = null
self._externalDHT = typeof self.dht === 'object'
self._performedDHTLookup = false
if (!self.peerId) throw new Error('peerId required')
if (!process.browser && !self.port) throw new Error('port required')
if (self.dht) self._createDHT(self.dhtPort)
}
Discovery.prototype.setTorrent = function (torrent) {
var self = this
if (!self.infoHash && Buffer.isBuffer(torrent) || typeof torrent === 'string') {
self.infoHash = typeof torrent === 'string'
? new Buffer(torrent, 'hex')
: torrent
} else if (!self.torrent && torrent && torrent.infoHash) {
self.torrent = torrent
self.infoHash = typeof torrent.infoHash === 'string'
? new Buffer(torrent.infoHash, 'hex')
: torrent.infoHash
} else {
return
}
self.infoHashHex = self.infoHash.toString('hex')
debug('setTorrent %s', self.infoHashHex)
// If tracker exists, then it was created with just infoHash. Set torrent length
// so client can report correct information about uploads.
if (self.tracker && self.tracker !== true) {
self.tracker.torrentLength = torrent.length
} else {
self._createTracker()
}
if (self.dht) {
if (self.dht.ready) self._dhtLookupAndAnnounce()
else self.dht.on('ready', self._dhtLookupAndAnnounce.bind(self))
}
}
Discovery.prototype.updatePort = function (port) {
var self = this
if (port === self.port) return
self.port = port
if (self.dht && self.infoHash) {
self._performedDHTLookup = false
self._dhtLookupAndAnnounce()
}
if (self.tracker && self.tracker !== true) {
self.tracker.stop()
self.tracker.destroy(function () {
self._createTracker()
})
}
}
Discovery.prototype.stop = function (cb) {
var self = this
var tasks = []
if (self.tracker && self.tracker !== true) {
self.tracker.stop()
tasks.push(function (cb) {
self.tracker.destroy(cb)
})
}
if (!self._externalDHT && self.dht && self.dht !== true) {
tasks.push(function (cb) {
self.dht.destroy(cb)
})
}
parallel(tasks, cb)
}
Discovery.prototype._createDHT = function (port) {
var self = this
if (!self._externalDHT) self.dht = new DHT()
reemit(self.dht, self, ['error', 'warning'])
self.dht.on('peer', function (addr, infoHash) {
if (infoHash === self.infoHashHex) self.emit('peer', addr)
})
if (!self._externalDHT) self.dht.listen(port)
}
Discovery.prototype._createTracker = function () {
var self = this
if (!self.tracker) return
var torrent = self.torrent
? extend({ announce: [] }, self.torrent)
: { infoHash: self.infoHashHex, announce: [] }
if (self.announce) torrent.announce = torrent.announce.concat(self.announce)
var trackerOpts = {
rtcConfig: self.rtcConfig,
wrtc: self.wrtc,
statsForAnnounce: self.statsForAnnounce,
key: self.trackerKey,
userAgent: self.userAgent
}
self.tracker = new Tracker(self.peerId, self.port, torrent, trackerOpts)
reemit(self.tracker, self, ['peer', 'warning', 'error'])
self.tracker.on('update', function (data) {
self.emit('trackerAnnounce', data)
})
self.tracker.start()
}
Discovery.prototype._dhtLookupAndAnnounce = function () {
var self = this
if (self._performedDHTLookup) return
self._performedDHTLookup = true
debug('dht lookup')
self.dht.lookup(self.infoHash, function (err) {
if (err || !self.port) return
debug('dht announce')
self.dht.announce(self.infoHash, self.port, function () {
debug('dht announce complete')
self.emit('dhtAnnounce')
})
})
}