-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
client.js
798 lines (657 loc) · 20.7 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import { EventEmitter } from 'events'
import bencode from 'bencode'
import Debug from 'debug'
import KBucket from 'k-bucket'
import krpc from 'k-rpc'
import low from 'last-one-wins'
import LRU from 'lru'
import randombytes from 'randombytes'
import records from 'record-cache'
import crypto from 'crypto'
const debug = Debug('bittorrent-dht')
const ROTATE_INTERVAL = 5 * 60 * 1000 // rotate secrets every 5 minutes
const BUCKET_OUTDATED_TIMESPAN = 15 * 60 * 1000 // check nodes in bucket in 15 minutes old buckets
class DHT extends EventEmitter {
constructor (opts = {}) {
super()
this._tables = new LRU({ maxAge: ROTATE_INTERVAL, max: opts.maxTables || 1000 })
this._values = new LRU(opts.maxValues || 1000)
this._peers = records({
maxAge: opts.maxAge || 0,
maxSize: opts.maxPeers || 10000
})
this._secrets = null
this._hash = opts.hash || sha1
this._hashLength = this._hash(Buffer.from('')).length
this._rpc = opts.krpc || krpc(Object.assign({ idLength: this._hashLength }, opts))
this._rpc.on('query', onquery)
this._rpc.on('node', onnode)
this._rpc.on('warning', onwarning)
this._rpc.on('error', onerror)
this._rpc.on('listening', onlistening)
this._rotateSecrets()
this._verify = opts.verify || null
this._host = opts.host || null
this._interval = setInterval(rotateSecrets, ROTATE_INTERVAL)
this._runningBucketCheck = false
this._bucketCheckTimeout = null
this._bucketOutdatedTimeSpan = opts.timeBucketOutdated || BUCKET_OUTDATED_TIMESPAN
this.listening = false
this.destroyed = false
this.nodeId = this._rpc.id
this.nodes = this._rpc.nodes
// ensure only *one* ping it running at the time to avoid infinite async
// ping recursion, and make the latest one is always ran, but inbetween ones
// are disregarded
const onping = low(ping)
this._rpc.on('ping', (older, swap) => {
onping({ older, swap })
})
process.nextTick(bootstrap)
this._debug('new DHT %s', this.nodeId)
const self = this
function ping (opts, cb) {
const older = opts.older
const swap = opts.swap
self._debug('received ping', older)
self._checkNodes(older, false, (_, deadNode) => {
if (deadNode) {
self._debug('swaping dead node with newer', deadNode)
swap(deadNode)
return cb()
}
self._debug('no node added, all other nodes ok')
cb()
})
}
function onlistening () {
self.listening = true
self._debug('listening %d', self.address().port)
self.updateBucketTimestamp()
self._setBucketCheckInterval()
self.emit('listening')
}
function onquery (query, peer) {
self._onquery(query, peer)
}
function rotateSecrets () {
self._rotateSecrets()
}
function bootstrap () {
if (!self.destroyed) self._bootstrap(opts.bootstrap !== false)
}
function onwarning (err) {
self.emit('warning', err)
}
function onerror (err) {
self.emit('error', err)
}
function onnode (node) {
self.emit('node', node)
}
}
_setBucketCheckInterval () {
const self = this
const interval = 1 * 60 * 1000 // check age of bucket every minute
this._runningBucketCheck = true
queueNext()
function checkBucket () {
const diff = Date.now() - self._rpc.nodes.metadata.lastChange
if (diff < self._bucketOutdatedTimeSpan) return queueNext()
self._pingAll(() => {
if (self.destroyed) return
if (self.nodes.toArray().length < 1) {
// node is currently isolated,
// retry with initial bootstrap nodes
self._bootstrap(true)
}
queueNext()
})
}
function queueNext () {
if (!self._runningBucketCheck || self.destroyed) return
const nextTimeout = Math.floor(Math.random() * interval + interval / 2)
self._bucketCheckTimeout = setTimeout(checkBucket, nextTimeout)
}
}
_pingAll (cb) {
this._checkAndRemoveNodes(this.nodes.toArray(), cb)
}
removeBucketCheckInterval () {
this._runningBucketCheck = false
clearTimeout(this._bucketCheckTimeout)
}
updateBucketTimestamp () {
this._rpc.nodes.metadata.lastChange = Date.now()
}
_checkAndRemoveNodes (nodes, cb) {
const self = this
this._checkNodes(nodes, true, (_, node) => {
if (node) self.removeNode(node.id)
cb(null, node)
})
}
_checkNodes (nodes, force, cb) {
const self = this
test(nodes)
function test (acc) {
let current = null
while (acc.length) {
current = acc.pop()
if (!current.id || force) break
if (Date.now() - (current.seen || 0) > 10000) break // not pinged within 10s
current = null
}
if (!current) return cb(null)
self._sendPing(current, err => {
if (!err) {
self.updateBucketTimestamp()
return test(acc)
}
cb(null, current)
})
}
}
addNode (node) {
const self = this
if (node.id) {
node.id = toBuffer(node.id)
const old = !!this._rpc.nodes.get(node.id)
this._rpc.nodes.add(node)
if (!old) {
this.emit('node', node)
this.updateBucketTimestamp()
}
return
}
this._sendPing(node, (_, node) => {
if (node) self.addNode(node)
})
}
removeNode (id) {
this._rpc.nodes.remove(toBuffer(id))
}
_sendPing (node, cb) {
const self = this
const expectedId = node.id
this._rpc.query(node, { q: 'ping' }, (err, pong, node) => {
if (err) return cb(err)
if (!pong.r || !pong.r.id || !Buffer.isBuffer(pong.r.id) || pong.r.id.length !== self._hashLength) {
return cb(new Error('Bad reply'))
}
if (Buffer.isBuffer(expectedId) && !expectedId.equals(pong.r.id)) {
return cb(new Error('Unexpected node id'))
}
self.updateBucketTimestamp()
cb(null, {
id: pong.r.id,
host: node.host || node.address,
port: node.port
})
})
}
toJSON () {
const self = this
const values = {}
Object.keys(this._values.cache).forEach(key => {
const value = self._values.cache[key].value
values[key] = {
v: value.v.toString('hex'),
id: value.id.toString('hex')
}
if (value.seq != null) values[key].seq = value.seq
if (value.sig != null) values[key].sig = value.sig.toString('hex')
if (value.k != null) values[key].k = value.k.toString('hex')
})
return {
nodes: this._rpc.nodes.toArray().map(toNode),
values
}
}
put (opts, cb) {
if (Buffer.isBuffer(opts) || typeof opts === 'string') opts = { v: opts }
const isMutable = !!opts.k
if (opts.v === undefined) {
throw new Error('opts.v not given')
}
if (opts.v.length >= 1000) {
throw new Error('v must be less than 1000 bytes in put()')
}
if (isMutable && opts.cas !== undefined && typeof opts.cas !== 'number') {
throw new Error('opts.cas must be an integer if provided')
}
if (isMutable && opts.k.length !== 32) {
throw new Error('opts.k ed25519 public key must be 32 bytes')
}
if (isMutable && typeof opts.sign !== 'function' && !Buffer.isBuffer(opts.sig)) {
throw new Error('opts.sign function or options.sig signature is required for mutable put')
}
if (isMutable && opts.salt && opts.salt.length > 64) {
throw new Error('opts.salt is > 64 bytes long')
}
if (isMutable && opts.seq === undefined) {
throw new Error('opts.seq not provided for a mutable update')
}
if (isMutable && typeof opts.seq !== 'number') {
throw new Error('opts.seq not an integer')
}
return this._put(opts, cb)
}
_put (opts, cb) {
if (!cb) cb = noop
const isMutable = !!opts.k
const v = typeof opts.v === 'string' ? Buffer.from(opts.v) : opts.v
const key = isMutable
? this._hash(opts.salt ? Buffer.concat([opts.k, opts.salt]) : opts.k)
: this._hash(bencode.encode(v))
const table = this._tables.get(key.toString('hex'))
if (!table) return this._preput(key, opts, cb)
const message = {
q: 'put',
a: {
id: this._rpc.id,
token: null, // queryAll sets this
v
}
}
if (isMutable) {
if (typeof opts.cas === 'number') message.a.cas = opts.cas
if (opts.salt) message.a.salt = opts.salt
message.a.k = opts.k
message.a.seq = opts.seq
if (typeof opts.sign === 'function') message.a.sig = opts.sign(encodeSigData(message.a))
else if (Buffer.isBuffer(opts.sig)) message.a.sig = opts.sig
} else {
this._values.set(key.toString('hex'), message.a)
}
this._rpc.queryAll(table.closest(key), message, null, (err, n) => {
if (err) return cb(err, key, n)
cb(null, key, n)
})
return key
}
_preput (key, opts, cb) {
const self = this
this._closest(key, {
q: 'get',
a: {
id: this._rpc.id,
target: key
}
}, null, (err, n) => {
if (err) return cb(err)
self.put(opts, cb)
})
return key
}
get (key, opts, cb) {
key = toBuffer(key)
if (typeof opts === 'function') {
cb = opts
opts = null
}
if (!opts) opts = {}
const verify = opts.verify || this._verify
const hash = this._hash
let value = this._values.get(key.toString('hex')) || null
if (value && (opts.cache !== false)) {
value = createGetResponse(this._rpc.id, null, value)
return process.nextTick(done)
}
this._closest(key, {
q: 'get',
a: {
id: this._rpc.id,
target: key
}
}, onreply, done)
function done (err) {
if (err) return cb(err)
cb(null, value)
}
function onreply (message) {
const r = message.r
if (!r || !r.v) return true
const isMutable = r.k || r.sig
if (opts.salt) r.salt = Buffer.from(opts.salt)
if (isMutable) {
if (!verify || !r.sig || !r.k) return true
if (!verify(r.sig, encodeSigData(r), r.k)) return true
if (hash(r.salt ? Buffer.concat([r.k, r.salt]) : r.k).equals(key)) {
if (!value || r.seq > value.seq) value = r
}
} else {
if (hash(bencode.encode(r.v)).equals(key)) {
value = r
return false
}
}
return true
}
}
announce (infoHash, port, cb) {
if (typeof port === 'function') return this.announce(infoHash, 0, port)
infoHash = toBuffer(infoHash)
if (!cb) cb = noop
const table = this._tables.get(infoHash.toString('hex'))
if (!table) return this._preannounce(infoHash, port, cb)
if (this._host) {
const dhtPort = this.listening ? this.address().port : 0
this._addPeer(
{ host: this._host, port: port || dhtPort },
infoHash,
{ host: this._host, port: dhtPort }
)
}
const message = {
q: 'announce_peer',
a: {
id: this._rpc.id,
token: null, // queryAll sets this
info_hash: infoHash,
port,
implied_port: port ? 0 : 1
}
}
this._debug('announce %s %d', infoHash, port)
this._rpc.queryAll(table.closest(infoHash), message, null, cb)
}
_preannounce (infoHash, port, cb) {
const self = this
this.lookup(infoHash, err => {
if (self.destroyed) return cb(new Error('dht is destroyed'))
if (err) return cb(err)
self.announce(infoHash, port, cb)
})
}
lookup (infoHash, cb) {
infoHash = toBuffer(infoHash)
if (!cb) cb = noop
const self = this
let aborted = false
this._debug('lookup %s', infoHash)
process.nextTick(emit)
this._closest(infoHash, {
q: 'get_peers',
a: {
id: this._rpc.id,
info_hash: infoHash
}
}, onreply, cb)
function emit (values, from) {
if (!values) values = self._peers.get(infoHash.toString('hex'), 100)
const peers = decodePeers(values)
for (let i = 0; i < peers.length; i++) {
self.emit('peer', peers[i], infoHash, from || null)
}
}
function onreply (message, node) {
if (aborted) return false
if (message.r.values) emit(message.r.values, node)
}
return function abort () { aborted = true }
}
address () {
return this._rpc.address()
}
// listen([port], [address], [onlistening])
listen (...args) {
this._rpc.bind(...args)
}
destroy (cb) {
if (this.destroyed) {
if (cb) process.nextTick(cb)
return
}
this.destroyed = true
const self = this
clearInterval(this._interval)
this.removeBucketCheckInterval()
this._peers.destroy()
this._debug('destroying')
this._rpc.destroy(() => {
self.emit('close')
if (cb) cb()
})
}
_onquery (query, peer) {
if (query.q === undefined || query.q === null) return
const q = query.q.toString()
this._debug('received %s query from %s:%d', q, peer.address, peer.port)
if (!query.a) return
switch (q) {
case 'ping':
return this._rpc.response(peer, query, { id: this._rpc.id })
case 'find_node':
return this._onfindnode(query, peer)
case 'get_peers':
return this._ongetpeers(query, peer)
case 'announce_peer':
return this._onannouncepeer(query, peer)
case 'get':
return this._onget(query, peer)
case 'put':
return this._onput(query, peer)
}
}
_onfindnode (query, peer) {
const target = query.a.target
if (!target) return this._rpc.error(peer, query, [203, '`find_node` missing required `a.target` field'])
this.emit('find_node', target)
const nodes = this._rpc.nodes.closest(target)
this._rpc.response(peer, query, { id: this._rpc.id }, nodes)
}
_ongetpeers (query, peer) {
const host = peer.address || peer.host
const infoHash = query.a.info_hash
if (!infoHash) return this._rpc.error(peer, query, [203, '`get_peers` missing required `a.info_hash` field'])
this.emit('get_peers', infoHash)
const r = { id: this._rpc.id, token: this._generateToken(host) }
const peers = this._peers.get(infoHash.toString('hex'))
if (peers.length) {
r.values = peers
this._rpc.response(peer, query, r)
} else {
this._rpc.response(peer, query, r, this._rpc.nodes.closest(infoHash))
}
}
_onannouncepeer (query, peer) {
const host = peer.address || peer.host
const port = query.a.implied_port ? peer.port : query.a.port
if (!port || typeof port !== 'number' || port <= 0 || port > 65535) return
const infoHash = query.a.info_hash
const token = query.a.token
if (!infoHash || !token) return
if (!this._validateToken(host, token)) {
return this._rpc.error(peer, query, [203, 'cannot `announce_peer` with bad token'])
}
this.emit('announce_peer', infoHash, { host, port: peer.port })
this._addPeer({ host, port }, infoHash, { host, port: peer.port })
this._rpc.response(peer, query, { id: this._rpc.id })
}
_addPeer (peer, infoHash, from) {
this._peers.add(infoHash.toString('hex'), encodePeer(peer.host, peer.port))
this.emit('announce', peer, infoHash, from)
}
_onget (query, peer) {
const host = peer.address || peer.host
const target = query.a.target
if (!target) return
const token = this._generateToken(host)
const value = this._values.get(target.toString('hex'))
this.emit('get', target, value)
if (!value) {
const nodes = this._rpc.nodes.closest(target)
this._rpc.response(peer, query, { id: this._rpc.id, token }, nodes)
} else {
this._rpc.response(peer, query, createGetResponse(this._rpc.id, token, value))
}
}
_onput (query, peer) {
const host = peer.address || peer.host
const a = query.a
if (!a) return
const v = query.a.v
if (!v) return
const id = query.a.id
if (!id) return
const token = a.token
if (!token) return
if (!this._validateToken(host, token)) {
return this._rpc.error(peer, query, [203, 'cannot `put` with bad token'])
}
if (v.length > 1000) {
return this._rpc.error(peer, query, [205, 'data payload too large'])
}
const isMutable = !!(a.k || a.sig)
if (isMutable && !a.k && !a.sig) return
const key = isMutable
? this._hash(a.salt ? Buffer.concat([a.k, a.salt]) : a.k)
: this._hash(bencode.encode(v))
const keyHex = key.toString('hex')
this.emit('put', key, v)
if (isMutable) {
if (!this._verify) return this._rpc.error(peer, query, [400, 'verification not supported'])
if (!this._verify(a.sig, encodeSigData(a), a.k)) return
const prev = this._values.get(keyHex)
if (prev && typeof a.cas === 'number' && prev.seq !== a.cas) {
return this._rpc.error(peer, query, [301, 'CAS mismatch, re-read and try again'])
}
if (prev && typeof prev.seq === 'number' && !(a.seq > prev.seq)) {
return this._rpc.error(peer, query, [302, 'sequence number less than current'])
}
this._values.set(keyHex, { v, k: a.k, salt: a.salt, sig: a.sig, seq: a.seq, id })
} else {
this._values.set(keyHex, { v, id })
}
this._rpc.response(peer, query, { id: this._rpc.id })
}
_bootstrap (populate) {
const self = this
if (!populate) return process.nextTick(ready)
this._rpc.populate(self._rpc.id, {
q: 'find_node',
a: {
id: self._rpc.id,
target: self._rpc.id
}
}, ready)
function ready () {
if (self.ready) return
self._debug('emit ready')
self.ready = true
self.emit('ready')
}
}
_closest (target, message, onmessage, cb) {
const self = this
const table = new KBucket({
localNodeId: target,
numberOfNodesPerKBucket: this._rpc.k
})
this._rpc.closest(target, message, onreply, done)
function done (err, n) {
if (err) return cb(err)
self._tables.set(target.toString('hex'), table)
self._debug('visited %d nodes', n)
cb(null, n)
}
function onreply (message, node) {
if (!message.r) return true
if (message.r.token && message.r.id && Buffer.isBuffer(message.r.id) && message.r.id.length === self._hashLength) {
self._debug('found node %s (target: %s)', message.r.id, target)
table.add({
id: message.r.id,
host: node.host || node.address,
port: node.port,
token: message.r.token
})
}
if (!onmessage) return true
return onmessage(message, node)
}
}
_debug () {
if (!debug.enabled) return
const args = [].slice.call(arguments)
args[0] = `[${this.nodeId.toString('hex').substring(0, 7)}] ${args[0]}`
for (let i = 1; i < args.length; i++) {
if (Buffer.isBuffer(args[i])) args[i] = args[i].toString('hex')
}
debug(...args)
}
_validateToken (host, token) {
const tokenA = this._generateToken(host, this._secrets[0])
const tokenB = this._generateToken(host, this._secrets[1])
return token.equals(tokenA) || token.equals(tokenB)
}
_generateToken (host, secret) {
if (!secret) secret = this._secrets[0]
return this._hash(Buffer.concat([Buffer.from(host), secret]))
}
_rotateSecrets () {
if (!this._secrets) {
this._secrets = [randombytes(this._hashLength), randombytes(this._hashLength)]
} else {
this._secrets[1] = this._secrets[0]
this._secrets[0] = randombytes(this._hashLength)
}
}
}
function noop () {}
function sha1 (buf) {
return crypto.createHash('sha1').update(buf).digest()
}
function createGetResponse (id, token, value) {
const r = { id, token, v: value.v }
if (value.sig) {
r.sig = value.sig
r.k = value.k
if (typeof value.seq === 'number') r.seq = value.seq
}
return r
}
function encodePeer (host, port) {
const buf = Buffer.allocUnsafe(6)
const ip = host.split('.')
for (let i = 0; i < 4; i++) buf[i] = parseInt(ip[i] || 0, 10)
buf.writeUInt16BE(port, 4)
return buf
}
function decodePeers (buf) {
const peers = []
try {
for (let i = 0; i < buf.length; i++) {
const port = buf[i].readUInt16BE(4)
if (!port) continue
peers.push({
host: parseIp(buf[i], 0),
port
})
}
} catch (err) {
// do nothing
}
return peers
}
function parseIp (buf, offset) {
return `${buf[offset++]}.${buf[offset++]}.${buf[offset++]}.${buf[offset++]}`
}
function encodeSigData (msg) {
const ref = { seq: msg.seq || 0, v: msg.v }
if (msg.salt) ref.salt = msg.salt
return bencode.encode(ref).slice(1, -1)
}
function toNode (node) {
return {
host: node.host,
port: node.port
}
}
function toBuffer (str) {
if (Buffer.isBuffer(str)) return str
if (ArrayBuffer.isView(str)) return Buffer.from(str.buffer, str.byteOffset, str.byteLength)
if (typeof str === 'string') return Buffer.from(str, 'hex')
throw new Error('Pass a buffer or a string')
}
export default DHT