-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
example.js
46 lines (36 loc) · 1.06 KB
/
example.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
var krpc = require('./')
var rpc = krpc()
var target = Buffer.from('aaaabbbbccccddddeeeeffffaaaabbbbccccdddd', 'hex')
rpc.on('query', function (query, peer) {
// console.log(query, peer)
})
var then = Date.now()
rpc.populate(rpc.id, { q: 'find_node', a: { id: rpc.id, target: rpc.id } }, function () {
console.log('(populated)', Date.now() - then)
})
rpc.closest(target, { q: 'get_peers', a: { info_hash: target } }, visit, function (_, n) {
console.log('(closest)', Date.now() - then, n)
})
function visit (res, peer) {
var peers = res.r.values ? parsePeers(res.r.values) : []
if (peers.length) console.log('count peers:', peers.length)
}
function parsePeers (buf) {
var peers = []
try {
for (var i = 0; i < buf.length; i++) {
var port = buf[i].readUInt16BE(4)
if (!port) continue
peers.push({
host: parseIp(buf[i], 0),
port: port
})
}
} catch (err) {
// do nothing
}
return peers
}
function parseIp (buf, offset) {
return buf[offset++] + '.' + buf[offset++] + '.' + buf[offset++] + '.' + buf[offset++]
}