forked from webtorrent/bittorrent-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
1230 lines (1067 loc) · 31.6 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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module.exports = DHT
var bencode = require('bencode')
var bufferEqual = require('buffer-equal')
var compact2string = require('compact2string')
var crypto = require('crypto')
var debug = require('debug')('bittorrent-dht')
var dgram = require('dgram')
var dns = require('dns')
var EventEmitter = require('events').EventEmitter
var hat = require('hat')
var inherits = require('inherits')
var KBucket = require('k-bucket')
var once = require('once')
var os = require('os')
var parallel = require('run-parallel')
var timers = require('timers')
var net = require('net')
var string2compact = require('string2compact')
var BOOTSTRAP_NODES = [
'router.bittorrent.com:6881',
'router.utorrent.com:6881',
'dht.transmissionbt.com:6881'
]
var BOOTSTRAP_TIMEOUT = 10000
var K = module.exports.K = 20 // number of nodes per bucket
var MAX_CONCURRENCY = 3 // α from Kademlia paper
var ROTATE_INTERVAL = 5 * 60 * 1000 // rotate secrets every 5 minutes
var SECRET_ENTROPY = 160 // entropy of token secrets
var SEND_TIMEOUT = 2000
var MESSAGE_TYPE = module.exports.MESSAGE_TYPE = {
QUERY: 'q',
RESPONSE: 'r',
ERROR: 'e'
}
var ERROR_TYPE = module.exports.ERROR_TYPE = {
GENERIC: 201,
SERVER: 202,
PROTOCOL: 203, // malformed packet, invalid arguments, or bad token
METHOD_UNKNOWN: 204
}
var LOCAL_HOSTS = { 4: [], 6: [] }
var interfaces = os.networkInterfaces()
for (var i in interfaces) {
for (var j = 0; j < interfaces[i].length; j++) {
var face = interfaces[i][j]
if (face.family === 'IPv4') LOCAL_HOSTS[4].push(face.address)
if (face.family === 'IPv6') LOCAL_HOSTS[6].push(face.address)
}
}
inherits(DHT, EventEmitter)
/**
* A DHT client implementation. The DHT is the main peer discovery layer for BitTorrent,
* which allows for trackerless torrents.
* @param {string|Buffer} opts
*/
function DHT (opts) {
var self = this
if (!(self instanceof DHT)) return new DHT(opts)
EventEmitter.call(self)
if (!opts) opts = {}
self.nodeId = idToBuffer(opts.nodeId || hat(160))
self.ipv = opts.ipv || 4
self._debug('new DHT %s', idToHexString(self.nodeId))
self.ready = false
self.listening = false
self._binding = false
self._destroyed = false
self.port = null
/**
* Query Handlers table
* @type {Object} string -> function
*/
self.queryHandler = {
ping: self._onPing,
find_node: self._onFindNode,
get_peers: self._onGetPeers,
announce_peer: self._onAnnouncePeer
}
/**
* Routing table
* @type {KBucket}
*/
self.nodes = new KBucket({
localNodeId: self.nodeId,
numberOfNodesPerKBucket: K,
numberOfNodesToPing: MAX_CONCURRENCY
})
/**
* Cache of routing tables used during a lookup. Saved in this object so we can access
* each node's unique token for announces later.
* TODO: Clean up tables after 5 minutes.
* @type {Object} infoHash:string -> KBucket
*/
self.tables = {}
/**
* Pending transactions (unresolved requests to peers)
* @type {Object} addr:string -> array of pending transactions
*/
self.transactions = {}
/**
* Peer address data (tracker storage)
* @type {Object} infoHash:string -> array of peers
*/
self.peers = {}
/**
* Lookup cache to prevent excessive GC.
* @type {Object} addr:string -> [host:string, port:number]
*/
self._addrData = {}
// Create socket and attach listeners
self.socket = dgram.createSocket('udp' + self.ipv)
self.socket.on('message', self._onData.bind(self))
self.socket.on('listening', self._onListening.bind(self))
self.socket.on('error', function () {}) // throw away errors
self._rotateSecrets()
self._rotateInterval = timers.setInterval(self._rotateSecrets.bind(self), ROTATE_INTERVAL)
self._rotateInterval.unref()
process.nextTick(function () {
if (opts.bootstrap === false) {
// Emit `ready` right away because the user does not want to bootstrap. Presumably,
// the user will call addNode() to populate the routing table manually.
self.ready = true
self.emit('ready')
} else if (typeof opts.bootstrap === 'string') {
self._bootstrap([ opts.bootstrap ])
} else if (Array.isArray(opts.bootstrap)) {
self._bootstrap(fromArray(opts.bootstrap))
} else {
// opts.bootstrap is undefined or true
self._bootstrap(BOOTSTRAP_NODES)
}
})
self.on('ready', function () {
self._debug('emit ready')
})
}
/**
* Start listening for UDP messages on given port.
* @param {number} port
* @param {function=} onlistening added as handler for listening event
*/
DHT.prototype.listen = function (port, onlistening) {
var self = this
if (typeof port === 'function') {
onlistening = port
port = undefined
}
if (onlistening)
self.once('listening', onlistening)
if (self._destroyed || self._binding || self.listening) return
self._binding = true
self._debug('listen %s', port)
self.socket.bind(port)
}
/**
* Called when DHT is listening for UDP messages.
*/
DHT.prototype._onListening = function () {
var self = this
self._binding = false
self.listening = true
self.port = self.socket.address().port
self._debug('emit listening %s', self.port)
self.emit('listening', self.port)
}
/**
* Announce that the peer, controlling the querying node, is downloading a torrent on a
* port.
* @param {string|Buffer} infoHash
* @param {number} port
* @param {function=} cb
*/
DHT.prototype.announce = function (infoHash, port, cb) {
var self = this
if (!cb) cb = function () {}
if (self._destroyed) return cb(new Error('dht is destroyed'))
self._debug('announce %s %s', infoHash, port)
var infoHashHex = idToHexString(infoHash)
// TODO: it would be nice to not use a table when a lookup is in progress
var table = self.tables[infoHashHex]
if (table) {
onClosest(null, table.closest({ id: infoHash }, K))
} else {
self.lookup(infoHash, onClosest)
}
function onClosest (err, closest) {
if (err) return cb(err)
closest.forEach(function (contact) {
self._sendAnnouncePeer(contact.addr, infoHash, port, contact.token)
})
self._debug('announce end %s %s', infoHash, port)
cb(null)
}
}
/**
* Destroy and cleanup the DHT.
* @param {function=} cb
*/
DHT.prototype.destroy = function (cb) {
var self = this
if (!cb) cb = function () {}
cb = once(cb)
if (self._destroyed) return cb(new Error('dht is destroyed'))
if (self._binding) return self.once('listening', self.destroy.bind(self, cb))
self._debug('destroy')
self._destroyed = true
self.listening = false
self.port = null
// garbage collect large data structures
self.nodes = null
self.tables = null
self.transactions = null
self.peers = null
self._addrData = null
timers.clearTimeout(self._bootstrapTimeout)
timers.clearInterval(self._rotateInterval)
self.socket.on('close', cb)
try {
self.socket.close()
} catch (err) {
// ignore error, socket was either already closed / not yet bound
cb(null)
}
}
/**
* Add a DHT node to the routing table.
* @param {string} addr
* @param {string|Buffer} nodeId
* @param {string=} from addr
*/
DHT.prototype.addNode = function (addr, nodeId, from) {
var self = this
if (self._destroyed) return
nodeId = idToBuffer(nodeId)
if (self._addrIsSelf(addr)) {
// self._debug('skipping adding %s since that is us!', addr)
return
}
var contact = {
id: nodeId,
addr: addr
}
self.nodes.add(contact)
// TODO: only emit this event for new nodes
self.emit('node', addr, nodeId, from)
self._debug('addNode %s %s discovered from %s', idToHexString(nodeId), addr, from)
}
/**
* Remove a DHT node from the routing table.
* @param {string|Buffer} nodeId
*/
DHT.prototype.removeNode = function (nodeId) {
var self = this
if (self._destroyed) return
var contact = self.nodes.get(idToBuffer(nodeId))
if (contact) {
self._debug('removeNode %s %s', contact.nodeId, contact.addr)
self.nodes.remove(contact)
}
}
/**
* Store a peer in the DHT. Called when a peer sends a `announce_peer` message.
* @param {string} addr
* @param {Buffer|string} infoHash
*/
DHT.prototype._addPeer = function (addr, infoHash) {
var self = this
if (self._destroyed) return
infoHash = idToHexString(infoHash)
var peers = self.peers[infoHash]
if (!peers) {
peers = self.peers[infoHash] = []
}
var compactPeerInfo = string2compact(addr)
// TODO: make this faster using a set
var exists = peers.some(function (peer) {
return bufferEqual(peer, compactPeerInfo)
})
if (!exists) {
peers.push(compactPeerInfo)
self._debug('addPeer %s %s', addr, infoHash)
self.emit('announce', addr, infoHash)
}
}
/**
* Remove a peer from the DHT.
* @param {string} addr
* @param {Buffer|string} infoHash
*/
DHT.prototype.removePeer = function (addr, infoHash) {
var self = this
if (self._destroyed) return
infoHash = idToHexString(infoHash)
var peers = self.peers[infoHash]
if (peers) {
var compactPeerInfo = string2compact(addr)
// TODO: make this faster using a set
peers.some(function (peer, index) {
if (bufferEqual(peer, compactPeerInfo)) {
peers.splice(index, 1)
self._debug('removePeer %s %s', addr, infoHash)
return true // abort early
}
})
}
}
/**
* Join the DHT network. To join initially, connect to known nodes (either public
* bootstrap nodes, or known nodes from a previous run of bittorrent-client).
* @param {Array.<string|Object>} nodes
*/
DHT.prototype._bootstrap = function (nodes) {
var self = this
self._debug('bootstrap with %s', JSON.stringify(nodes))
var contacts = nodes.map(function (obj) {
if (typeof obj === 'string') {
return { addr: obj }
} else {
return obj
}
})
self._resolveContacts(contacts, function (err, contacts) {
if (err) return self.emit('error', err)
// add all non-bootstrap nodes to routing table
contacts
.filter(function (contact) {
return !!contact.id
})
.forEach(function (contact) {
self.addNode(contact.addr, contact.id, contact.from)
})
// get addresses of bootstrap nodes
var addrs = contacts
.filter(function (contact) {
return !contact.id
})
.map(function (contact) {
return contact.addr
})
function lookup () {
self.lookup(self.nodeId, {
findNode: true,
addrs: addrs.length ? addrs : null
}, function (err) {
if (err) self._debug('lookup error %s during bootstrap', err.message)
// emit `ready` once the recursive lookup for our own node ID is finished
// (successful or not), so that later get_peer lookups will have a good shot at
// succeeding.
self.ready = true
self.emit('ready')
})
}
lookup()
// TODO: keep retrying after one failure
self._bootstrapTimeout = timers.setTimeout(function () {
// If 0 nodes are in the table after a timeout, retry with bootstrap nodes
if (self.nodes.count() === 0) {
self._debug('No DHT bootstrap nodes replied, retry')
lookup()
}
}, BOOTSTRAP_TIMEOUT)
self._bootstrapTimeout.unref()
})
}
/**
* Resolve the DNS for nodes whose hostname is a domain name (often the case for
* bootstrap nodes).
* @param {Array.<Object>} contacts array of contact objects with domain addresses
* @param {function} done
*/
DHT.prototype._resolveContacts = function (contacts, done) {
var self = this
var tasks = contacts.map(function (contact) {
return function (cb) {
var addrData = self._getAddrData(contact.addr)
if (net.isIP(addrData[0])) cb(null, contact)
else
dns.lookup(addrData[0], self.ipv, function (err, host) {
if (err) return cb(null, null)
contact.addr = host + ':' + addrData[1]
cb(null, contact)
})
}
})
parallel(tasks, function (err, contacts) {
if (err) return done(err)
// filter out hosts that don't resolve
contacts = contacts.filter(function (contact) { return !!contact })
done(null, contacts)
})
}
/**
* Perform a recurive node lookup for the given nodeId. If isFindNode is true, then
* `find_node` will be sent to each peer instead of `get_peers`.
* @param {Buffer|string} id node id or info hash
* @param {Object=} opts
* @param {boolean} opts.findNode
* @param {Array.<string>} opts.addrs
* @param {function} cb called with K closest nodes
*/
DHT.prototype.lookup = function (id, opts, cb) {
var self = this
if (typeof opts === 'function') {
cb = opts
opts = {}
}
id = idToBuffer(id)
if (!opts) opts = {}
if (!cb) cb = function () {}
cb = once(cb)
if (self._destroyed) return cb(new Error('dht is destroyed'))
if (!self.listening) return self.listen(self.lookup.bind(self, id, opts, cb))
var idHex = idToHexString(id)
self._debug('lookup %s %s', (opts.findNode ? '(find_node)' : '(get_peers)'), idHex)
var table = new KBucket({
localNodeId: id,
numberOfNodesPerKBucket: K,
numberOfNodesToPing: MAX_CONCURRENCY
})
if (!opts.findNode) {
self.tables[idHex] = table
}
function add (contact) {
if (!self._addrIsSelf(contact.addr)) table.add(contact)
}
var queried = {}
var pending = 0 // pending queries
if (opts.addrs) {
// kick off lookup with explicitly passed nodes (usually, bootstrap servers)
opts.addrs.forEach(query)
} else {
// kick off lookup with nodes in the main table
queryClosest()
}
function query (addr) {
pending += 1
queried[addr] = true
if (opts.findNode) {
self._sendFindNode(addr, id, onResponse.bind(null, addr))
} else {
self._sendGetPeers(addr, id, onResponse.bind(null, addr))
}
}
function queryClosest () {
self.nodes.closest({ id: id }, K).forEach(function (contact) {
query(contact.addr)
})
}
// Note: `_sendFindNode` and `_sendGetPeers` will insert newly discovered nodes into
// the routing table, so that's not done here.
function onResponse (addr, err, res) {
if (self._destroyed) return cb(new Error('dht is destroyed'))
pending -= 1
var nodeId = res && res.id
var nodeIdHex = idToHexString(nodeId)
// ignore errors - they are just timeouts
if (err) {
self._debug('got lookup error: %s', err.message)
} else {
self._debug('got lookup response: %s from %s', JSON.stringify(res), nodeIdHex)
// add node that sent this response
var contact = table.get(nodeId)
if (!contact) {
contact = { id: nodeId, addr: addr }
add(contact)
}
contact.token = res.token
// add nodes to this routing table for this lookup
if (res && res.nodes) {
res.nodes.forEach(function (contact) {
add(contact)
})
}
}
// find closest unqueried nodes
var candidates = table.closest({ id: id }, K)
.filter(function (contact) {
return !queried[contact.addr]
})
while (pending < MAX_CONCURRENCY && candidates.length) {
// query as many candidates as our concurrency limit will allow
query(candidates.pop().addr)
}
if (pending === 0 && candidates.length === 0) {
// recursive lookup should terminate because there are no closer nodes to find
self._debug('terminating lookup %s %s',
(opts.findNode ? '(find_node)' : '(get_peers)'), idHex)
var closest = table.closest({ id: id }, K)
self._debug('K closest nodes are:')
closest.forEach(function (contact) {
self._debug(' ' + contact.addr + ' ' + idToHexString(contact.id))
})
cb(null, closest)
}
}
}
/**
* Called when another node sends a UDP message
* @param {Buffer} data
* @param {Object} rinfo
*/
DHT.prototype._onData = function (data, rinfo) {
var self = this
var addr = rinfo.address + ':' + rinfo.port
var message, errMessage
try {
message = bencode.decode(data)
if (!message) throw new Error('message is empty')
} catch (err) {
errMessage = err.message + ' from ' + addr + ' (' + data + ')'
self._debug(errMessage)
self.emit('warning', new Error(errMessage))
return
}
var type = message.y && message.y.toString()
if (type !== MESSAGE_TYPE.QUERY && type !== MESSAGE_TYPE.RESPONSE &&
type !== MESSAGE_TYPE.ERROR) {
errMessage = 'unknown message type ' + type + ' from ' + addr
self._debug(errMessage)
self.emit('warning', new Error(errMessage))
return
}
self._debug('got data %s from %s', JSON.stringify(message), addr)
// Attempt to add every (valid) node that we see to the routing table.
// TODO: If they node is already in the table, just update the "last heard from" time
var nodeId = (message.r && message.r.id) || (message.a && message.a.id)
if (nodeId) {
// TODO: verify that this a valid length for a nodeId
// self._debug('adding (potentially) new node %s %s', idToHexString(nodeId), addr)
self.addNode(addr, nodeId, addr)
}
if (type === MESSAGE_TYPE.QUERY) {
self._onQuery(addr, message)
} else if (type === MESSAGE_TYPE.RESPONSE || type === MESSAGE_TYPE.ERROR) {
self._onResponseOrError(addr, type, message)
}
}
/**
* Called when another node sends a query.
* @param {string} addr
* @param {Object} message
*/
DHT.prototype._onQuery = function (addr, message) {
var self = this
var query = message.q.toString()
if (typeof self.queryHandler[query] === 'function') {
self.queryHandler[query].call(self, addr, message)
} else {
var errMessage = 'unexpected query type'
self._debug(errMessage)
self._sendError(addr, message.t, ERROR_TYPE.METHOD_UNKNOWN, errMessage)
}
}
/**
* Called when another node sends a response or error.
* @param {string} addr
* @param {string} type
* @param {Object} message
*/
DHT.prototype._onResponseOrError = function (addr, type, message) {
var self = this
var transactionId = Buffer.isBuffer(message.t) && message.t.length === 2
&& message.t.readUInt16BE(0)
var transaction = self.transactions && self.transactions[addr]
&& self.transactions[addr][transactionId]
var err = null
if (type === MESSAGE_TYPE.ERROR) {
err = new Error(Array.isArray(message.e) ? message.e.join(' ') : undefined)
}
if (!transaction || !transaction.cb) {
// unexpected message!
if (err) {
var errMessage = 'got unexpected error from ' + addr + ' ' + err.message
self._debug(errMessage)
self.emit('warning', new Error(errMessage))
} else {
self._debug('got unexpected message from ' + addr + ' ' + JSON.stringify(message))
self._sendError(addr, message.t, ERROR_TYPE.GENERIC, 'unexpected message')
}
return
}
transaction.cb(err, message.r)
}
/**
* Send a UDP message to the given addr.
* @param {string} addr
* @param {Object} message
* @param {function=} cb called once message has been sent
*/
DHT.prototype._send = function (addr, message, cb) {
var self = this
if (!self.listening) return self.listen(self._send.bind(self, addr, message, cb))
if (!cb) cb = function () {}
var addrData = self._getAddrData(addr)
var host = addrData[0]
var port = addrData[1]
if (!(port > 0 && port < 65535)) {
return
}
// self._debug('send %s to %s', JSON.stringify(message), addr)
message = bencode.encode(message)
self.socket.send(message, 0, message.length, port, host, cb)
}
DHT.prototype.query = function (data, addr, cb) {
var self = this
if (!data.a) data.a = {}
if (!data.a.id) data.a.id = self.nodeId
var transactionId = self._getTransactionId(addr, cb)
var message = {
t: transactionIdToBuffer(transactionId),
y: MESSAGE_TYPE.QUERY,
q: data.q,
a: data.a
}
self._debug('sent %s %s to %s', data.q, JSON.stringify(data.a), addr)
self._send(addr, message)
}
/**
* Send "ping" query to given addr.
* @param {string} addr
* @param {function} cb called with response
*/
DHT.prototype._sendPing = function (addr, cb) {
var self = this
self.query({ q: 'ping' }, addr, cb)
}
/**
* Called when another node sends a "ping" query.
* @param {string} addr
* @param {Object} message
*/
DHT.prototype._onPing = function (addr, message) {
var self = this
var res = {
t: message.t,
y: MESSAGE_TYPE.RESPONSE,
r: {
id: self.nodeId
}
}
self._debug('got ping from %s', addr)
self._send(addr, res)
}
/**
* Send "find_node" query to given addr.
* @param {string} addr
* @param {Buffer} nodeId
* @param {function} cb called with response
*/
DHT.prototype._sendFindNode = function (addr, nodeId, cb) {
var self = this
function onResponse (err, res) {
if (err) return cb(err)
if (res.nodes) {
res.nodes = parseNodeInfo(res.nodes)
res.nodes.forEach(function (node) {
self.addNode(node.addr, node.id, addr)
})
}
cb(null, res)
}
var data = {
q: 'find_node',
a: {
id: self.nodeId,
target: nodeId
}
}
self.query(data, addr, onResponse)
}
/**
* Called when another node sends a "find_node" query.
* @param {string} addr
* @param {Object} message
*/
DHT.prototype._onFindNode = function (addr, message) {
var self = this
var nodeId = message.a && message.a.target
if (!nodeId) {
var errMessage = '`find_node` missing required `a.target` field'
self._debug(errMessage)
self._sendError(addr, message.t, ERROR_TYPE.PROTOCOL, errMessage)
return
}
self._debug('got find_node %s from %s', idToHexString(nodeId), addr)
// Convert nodes to "compact node info" representation
var nodes = convertToNodeInfo(self.nodes.closest({ id: nodeId }, K))
var res = {
t: message.t,
y: MESSAGE_TYPE.RESPONSE,
r: {
id: self.nodeId,
nodes: nodes
}
}
self._send(addr, res)
}
/**
* Send "get_peers" query to given addr.
* @param {string} addr
* @param {Buffer|string} infoHash
* @param {function} cb called with response
*/
DHT.prototype._sendGetPeers = function (addr, infoHash, cb) {
var self = this
infoHash = idToBuffer(infoHash)
var infoHashHex = idToHexString(infoHash)
function onResponse (err, res) {
if (err) return cb(err)
if (res.nodes) {
res.nodes = parseNodeInfo(res.nodes)
res.nodes.forEach(function (node) {
self.addNode(node.addr, node.id, addr)
})
}
if (res.values) {
res.values = parsePeerInfo(res.values)
res.values.forEach(function (peerAddr) {
self._debug('emit peer %s %s from %s', infoHashHex, peerAddr, addr)
self.emit('peer', peerAddr, infoHashHex, addr)
})
}
cb(null, res)
}
var data = {
q: 'get_peers',
a: {
id: self.nodeId,
info_hash: infoHash
}
}
self.query(data, addr, onResponse)
}
/**
* Called when another node sends a "get_peers" query.
* @param {string} addr
* @param {Object} message
*/
DHT.prototype._onGetPeers = function (addr, message) {
var self = this
var addrData = self._getAddrData(addr)
var infoHash = message.a && message.a.info_hash
if (!infoHash) {
var errMessage = '`get_peers` missing required `a.info_hash` field'
self._debug(errMessage)
self._sendError(addr, message.t, ERROR_TYPE.PROTOCOL, errMessage)
return
}
var infoHashHex = idToHexString(infoHash)
self._debug('got get_peers %s from %s', infoHashHex, addr)
var res = {
t: message.t,
y: MESSAGE_TYPE.RESPONSE,
r: {
id: self.nodeId,
token: self._generateToken(addrData[0])
}
}
var peers = self.peers[infoHashHex]
if (peers) {
// We know of peers for the target info hash. Peers are stored as an array of
// compact peer info, so return it as-is.
res.r.values = peers
} else {
// No peers, so return the K closest nodes instead. Convert nodes to "compact node
// info" representation
res.r.nodes = convertToNodeInfo(self.nodes.closest({ id: infoHash }, K))
}
self._send(addr, res)
}
/**
* Send "announce_peer" query to given host and port.
* @param {string} addr
* @param {Buffer|string} infoHash
* @param {number} port
* @param {Buffer} token
* @param {function=} cb called with response
*/
DHT.prototype._sendAnnouncePeer = function (addr, infoHash, port, token, cb) {
var self = this
infoHash = idToBuffer(infoHash)
if (!cb) cb = function () {}
var data = {
q: 'announce_peer',
a: {
id: self.nodeId,
info_hash: infoHash,
port: port,
token: token,
implied_port: 0
}
}
self.query(data, addr, cb)
}
/**
* Called when another node sends a "announce_peer" query.
* @param {string} addr
* @param {Object} message
*/
DHT.prototype._onAnnouncePeer = function (addr, message) {
var self = this
var errMessage
var addrData = self._getAddrData(addr)
var infoHash = idToHexString(message.a && message.a.info_hash)
if (!infoHash) {
errMessage = '`announce_peer` missing required `a.info_hash` field'
self._debug(errMessage)
self._sendError(addr, message.t, ERROR_TYPE.PROTOCOL, errMessage)
return
}
var token = message.a && message.a.token
if (!self._isValidToken(token, addrData[0])) {
errMessage = 'cannot `announce_peer` with bad token'
self._sendError(addr, message.t, ERROR_TYPE.PROTOCOL, errMessage)
return
}
var port = message.a.implied_port !== 0
? addrData[1] // use port of udp packet
: message.a.port // use port in `announce_peer` message
self._debug('got announce_peer %s %s from %s with token %s', idToHexString(infoHash),
port, addr, idToHexString(token))
self._addPeer(addrData[0] + ':' + port, infoHash)
// send acknowledgement
var res = {
t: message.t,
y: MESSAGE_TYPE.RESPONSE,
r: {
id: self.nodeId
}
}
self._send(addr, res)
}
/**
* Send an error to given host and port.
* @param {string} addr
* @param {Buffer|number} transactionId
* @param {number} code
* @param {string} errMessage
*/
DHT.prototype._sendError = function (addr, transactionId, code, errMessage) {
var self = this
if (transactionId && !Buffer.isBuffer(transactionId)) {
transactionId = transactionIdToBuffer(transactionId)
}
var message = {
y: MESSAGE_TYPE.ERROR,
e: [code, errMessage]
}
if (transactionId) {
message.t = transactionId
}
self._debug('sent error %s to %s', JSON.stringify(message), addr)
self._send(addr, message)
}
/**
* Given an "address:port" string, return an array [address:string, port:number].
* Uses a cache to prevent excessive array allocations.
* @param {string} addr
* @return {Array.<*>}