-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
327 lines (271 loc) · 7.42 KB
/
index.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
module.exports = UDPTracker
var dgram = require('dgram')
var url = require('url')
var EventEmitter = require('events').EventEmitter
var hat = require('hat')
var toUInt64 = require('to-unsigned-int64')
var toUInt32Sync = require('to-unsigned-int32').toUInt32Sync
var fromUInt32Sync = require('from-unsigned-int32').fromUInt32Sync
var compact2string = require('compact2string')
var inherits = require('inherits')
var bufferEqual = require('buffer-equal')
inherits(UDPTracker, EventEmitter)
var eventMap = {
0: toUInt32Sync(0),
1: toUInt32Sync(1),
2: toUInt32Sync(2),
3: toUInt32Sync(3)
}
/*
things are remaining
1. handle announce intervals (done)
2. add emitter (done)
3. handle destory api (done)
4. test module
5. readme
*/
/**
info Hash
peerId
downloaded
uploaded
left
event
ip_address
key
num_want
port
announce_url
**/
function UDPTracker (peerId, infoHash, announceUrl, opts) {
var self = this
EventEmitter.call(self)
self.infoHash = Buffer.isBuffer(infoHash) ?
infoHash : new Buffer(infoHash, 'hex')
self.peerId = Buffer.isBuffer(peerId) ?
peerId : new Buffer(peerId)
self.announceUrl = announceUrl
self.opts = opts || {}
self.port = self.opts['port'] ? toUInt16(self.opts['port']) : toUInt16(0)
self._timeout = self.opts['_timeout'] || 15000
self._in_interval = false
self._last_opts = null
self.destoryed = false
self._announce_intervals_timeout = []
}
/*
opts must
*/
UDPTracker.prototype.announce = function (event, opts) {
var self = this
self._last_opts = opts || {}
self._last_opts['event'] = event
if (!self._in_interval) {
_request.bind(self, self._last_opts['event'], self._last_opts)()
} else {
return self.emit('error', 'not calling announce')
}
}
UDPTracker.prototype.destory = function () {
var self = this
self.destoryed = true
_destory(self)
return
}
function _cleanSocket (socket) {
if (!socket) {
return null
}
socket.removeListener('error', console.error)
socket.removeListener('message', console.log)
socket.on('error', console.error)
try {
socket.close()
} catch (e) {}
return null
}
function _clearTimmer (udpTimmers) {
if (udpTimmers) {
for (var i = 0; i < udpTimmers.length; i++) {
clearTimeout(udpTimmers[i])
}
}
return []
}
function _destory (self) {
self._announce_intervals_timeout.forEach(function (timmer) {
clearTimeout(timmer)
})
self._announce_intervals_timeout = []
self._udpTimmers = _clearTimmer(self._udpTimmers)
self._socket = _cleanSocket(self._socket)
}
function _request (event, opts) {
var self = this
if (self.destoryed) {
return
}
event = eventMap[event]
if (!event) {
self.emit('error', 'unknown event')
}
_destory(self)
self._socket = dgram.createSocket('udp4')
self._socket.on('message', onMessage)
self._socket.on('error', console.error)
self._udpTimmers = []
var connectionId = Buffer.concat([ toUInt32Sync(0x417), toUInt32Sync(0x27101980) ])
var transactionId = getTransactionId()
var parsedAnnounceUrl = url.parse(self.announceUrl)
// make connect request
makeConnectRequest()
// schedule next connect request base on timeout specification
timmer(makeConnectRequest, 'connect')()
function onMessage (msg) {
if (self.destoryed) {
return
}
var action = fromUInt32Sync(msg, 0)
_clearTimmer(self._udpTimmers)
switch (action) {
case 0 :
if (!handleConnectResponce(msg)) {
_destory(self)
return
}
makeAnnounceRequest()
// schedule next announce call
timmer(makeAnnounceRequest, 'announce')()
break
case 1 :
_destory(self)
handleAnnounceResponse(msg)
break
case 3 :
_destory(self)
return onAnnounceError(msg)
default :
_destory(self)
self.emit('error', 'unsupported event:' + action)
return
}
}
function onAnnounceError (msg) {
if (msg.length <= 8) {
self.emit('error', 'error action response is not 9 bytes long')
return false
}
var _trasactionId = msg.slice(4, 8)
if (!bufferEqual(transactionId, _trasactionId)) {
self.emit('error', 'transactionId is not same as in connect request')
return false
}
var err_message = msg.slice(8).toString()
return self.emit('error', err_message)
}
function handleConnectResponce (msg) {
if (msg.length !== 16) {
self.emit('error', 'connect response is not 16 bytes long')
return false
}
var _trasactionId = msg.slice(4, 8)
if (!bufferEqual(transactionId, _trasactionId)) {
self.emit('error', 'transactionId is not same as in connect request')
return false
}
connectionId = msg.slice(8, 16)
return true
}
function handleAnnounceResponse (msg) {
if (msg.length < 20) {
return self.emit('error', 'announce response should be 20 bytes')
}
var _trasactionId = msg.slice(4, 8)
if (!bufferEqual(transactionId, _trasactionId)) {
return self.emit('error', 'transactionId is not same as in announce request')
}
// interval is in seconds
var interval = fromUInt32Sync(msg, 8)
self._in_interval = true
var intervalTimout = setTimeout(function () {
self._in_interval = false
return self.announce(self._last_opts['event'], self._last_opts)
}, interval * 1000)
if (intervalTimout.unref) {
intervalTimout.unref()
}
self._announce_intervals_timeout.push(intervalTimout)
self.emit('update', {
leechers: fromUInt32Sync(msg, 12),
seeds: fromUInt32Sync(msg, 16),
peers: compact2string.multi(msg.slice(20))
})
}
function makeConnectRequest () {
var action = new Buffer(4)
action.fill(0)
var data = Buffer.concat([connectionId, toUInt32Sync(0), transactionId])
_write(data, 0)
}
function makeAnnounceRequest () {
var downloaded = opts['downloaded'] ? toUInt64(opts['downloaded']) : toUInt64(0)
var left = opts['left'] ? toUInt64(opts['left']) : toUInt64(0)
var uploaded = opts['uploaded'] ? toUInt64(opts['uploaded']) : toUInt64(0)
transactionId = getTransactionId()
var data = Buffer.concat([
connectionId,
toUInt32Sync(1),
transactionId,
self.infoHash,
self.peerId,
downloaded,
left,
uploaded,
event,
toUInt32Sync(0),
toUInt32Sync(0),
toUInt32Sync(50),
self.port
])
_write(data, 0)
}
function timmer (fn, message, timeout) {
var n = 1
var _timeout = timeout || incrementTimeout(n)
_clearTimmer(self._udpTimmers)
return function _timmer () {
if (n >= 8) {
_destory(self)
return self.emit('error', 'tracker is not responding after:' + _timeout)
}
var udpTimmer = setTimeout(function () {
n++
_timeout = incrementTimeout(n)
fn()
_timmer()
}, _timeout)
self._udpTimmers.push(udpTimmer)
if (udpTimmer.unref) {
udpTimmer.unref()
}
}
}
function _write (data, offset) {
if (self.destoryed) {
return
}
parsedAnnounceUrl.port = parsedAnnounceUrl.port || 80
self._socket.send(data, offset, data.length, parsedAnnounceUrl.port, parsedAnnounceUrl.hostname)
}
function incrementTimeout (time) {
return self._timeout * (Math.pow(2, time))
}
}
function getTransactionId () {
return new Buffer(hat(32), 'hex')
}
function toUInt16 (n) {
var buf = new Buffer(2)
buf.writeUInt16BE(n, 0)
return buf
}