-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
364 lines (295 loc) · 9 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
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
var socket = require('k-rpc-socket')
var KBucket = require('k-bucket')
var events = require('events')
var randombytes = require('randombytes')
var util = require('util')
var K = 20
var MAX_CONCURRENCY = 16
var BOOTSTRAP_NODES = [
{ host: 'router.bittorrent.com', port: 6881 },
{ host: 'router.utorrent.com', port: 6881 },
{ host: 'dht.transmissionbt.com', port: 6881 }
]
module.exports = RPC
function RPC (opts) {
if (!(this instanceof RPC)) return new RPC(opts)
if (!opts) opts = {}
var self = this
this._idLength = opts.idLength || 20
this.id = toBuffer(opts.id || opts.nodeId || randombytes(this._idLength))
this.socket = opts.krpcSocket || socket(opts)
this.bootstrap = toBootstrapArray(opts.nodes || opts.bootstrap)
this.concurrency = opts.concurrency || MAX_CONCURRENCY
this.backgroundConcurrency = opts.backgroundConcurrency || (this.concurrency / 4) | 0
this.k = opts.k || K
this.destroyed = false
this.pending = []
this.nodes = null
this.socket.setMaxListeners(0)
this.socket.on('query', onquery)
this.socket.on('response', onresponse)
this.socket.on('warning', onwarning)
this.socket.on('error', onerror)
this.socket.on('update', onupdate)
this.socket.on('listening', onlistening)
events.EventEmitter.call(this)
this.clear()
function onupdate () {
while (self.pending.length && self.socket.inflight < self.concurrency) {
var next = self.pending.shift()
self.query(next[0], next[1], next[2])
}
}
function onerror (err) {
self.emit('error', err)
}
function onlistening () {
self.emit('listening')
}
function onwarning (err) {
self.emit('warning', err)
}
function onquery (query, peer) {
addNode(query.a, peer)
self.emit('query', query, peer)
}
function onresponse (reply, peer) {
addNode(reply.r, peer)
}
function addNode (data, peer) {
if (data && isNodeId(data.id, self._idLength) && !data.id.equals(self.id)) {
var old = self.nodes.get(data.id)
if (old) {
old.seen = Date.now()
return
}
self._addNode({
id: data.id,
host: peer.address || peer.host,
port: peer.port,
distance: 0,
seen: Date.now()
})
}
}
}
util.inherits(RPC, events.EventEmitter)
RPC.prototype.response = function (node, query, response, nodes, cb) {
if (typeof nodes === 'function') {
cb = nodes
nodes = null
}
if (!response.id) response.id = this.id
if (nodes) response.nodes = encodeNodes(nodes, this._idLength)
this.socket.response(node, query, response, cb)
}
RPC.prototype.error = function (node, query, error, cb) {
this.socket.error(node, query, error, cb)
}
// bind([port], [address], [callback])
RPC.prototype.bind = function () {
this.socket.bind.apply(this.socket, arguments)
}
RPC.prototype.address = function () {
return this.socket.address()
}
RPC.prototype.queryAll = function (nodes, message, visit, cb) {
if (!message.a) message.a = {}
if (!message.a.id) message.a.id = this.id
var stop = false
var missing = nodes.length
var hits = 0
var error = null
if (!missing) return cb(new Error('No nodes to query'), 0)
for (var i = 0; i < nodes.length; i++) {
this.query(nodes[i], message, done)
}
function done (err, res, peer) {
if (!err) hits++
else if (err.code >= 300 && err.code < 400) error = err
if (!err && !stop) {
if (visit && visit(res, peer) === false) stop = true
}
if (!--missing) cb(hits ? null : error || new Error('All queries failed'), hits)
}
}
RPC.prototype.query = function (node, message, cb) {
if (this.socket.inflight >= this.concurrency) {
this.pending.push([node, message, cb])
} else {
if (!message.a) message.a = {}
if (!message.a.id) message.a.id = this.id
if (node.token) message.a.token = node.token
this.socket.query(node, message, cb)
}
}
RPC.prototype.destroy = function (cb) {
this.destroyed = true
this.socket.destroy(cb)
}
RPC.prototype.clear = function () {
var self = this
this.nodes = new KBucket({
localNodeId: this.id,
numberOfNodesPerKBucket: this.k,
numberOfNodesToPing: this.concurrency
})
this.nodes.on('ping', onping)
function onping (older, newer) {
self.emit('ping', older, function swap (deadNode) {
if (!deadNode) return
if (deadNode.id) self.nodes.remove(deadNode.id)
self._addNode(newer)
})
}
}
RPC.prototype.populate = function (target, message, cb) {
this._closest(target, message, true, null, cb)
}
RPC.prototype.closest = function (target, message, visit, cb) {
this._closest(target, message, false, visit, cb)
}
RPC.prototype._addNode = function (node) {
var old = this.nodes.get(node.id)
this.nodes.add(node)
if (!old) this.emit('node', node)
}
RPC.prototype._closest = function (target, message, background, visit, cb) {
if (!cb) cb = noop
var self = this
var count = 0
var queried = {}
var pending = 0
var once = true
var stop = false
if (!message.a) message.a = {}
if (!message.a.id) message.a.id = this.id
var table = new KBucket({
localNodeId: target,
numberOfNodesPerKBucket: this.k,
numberOfNodesToPing: this.concurrency
})
var evt = background ? 'postupdate' : 'update'
this.socket.on(evt, kick)
kick()
function kick () {
if (self.destroyed || self.socket.inflight >= self.concurrency) return
var otherInflight = self.pending.length + self.socket.inflight - pending
if (background && self.socket.inflight >= self.backgroundConcurrency && otherInflight) return
var closest = table.closest(target, self.k)
if (!closest.length || closest.length < self.bootstrap.length) {
closest = self.nodes.closest(target, self.k)
if (!closest.length || closest.length < self.bootstrap.length) bootstrap()
}
for (var i = 0; i < closest.length; i++) {
if (stop) break
if (self.socket.inflight >= self.concurrency) return
var peer = closest[i]
var id = peer.host + ':' + peer.port
if (queried[id]) continue
queried[id] = true
pending++
self.socket.query(peer, message, afterQuery)
}
if (!pending) {
self.socket.removeListener(evt, kick)
process.nextTick(done)
}
}
function done () {
cb(null, count)
}
function bootstrap () {
if (!once) return
once = false
self.bootstrap.forEach(function (peer) {
pending++
self.socket.query(peer, message, afterQuery)
})
}
function afterQuery (err, res, peer) {
pending--
if (peer) queried[(peer.address || peer.host) + ':' + peer.port] = true // need this for bootstrap nodes
if (peer && peer.id && self.nodes.get(peer.id)) {
if (err && (err.code === 'EUNEXPECTEDNODE' || err.code === 'ETIMEDOUT')) {
self.nodes.remove(peer.id)
}
}
var r = res && res.r
if (!r) return kick()
if (!err && isNodeId(r.id, self._idLength)) {
count++
add({
id: r.id,
port: peer.port,
host: peer.host || peer.address,
distance: 0
})
}
var nodes = r.nodes ? parseNodes(r.nodes, self._idLength) : []
for (var i = 0; i < nodes.length; i++) add(nodes[i])
if (visit && visit(res, peer) === false) stop = true
kick()
}
function add (node) {
if (node.id.equals(self.id)) return
table.add(node)
}
}
function toBootstrapArray (val) {
if (val === false) return []
if (val === true) return BOOTSTRAP_NODES
return [].concat(val || BOOTSTRAP_NODES).map(parsePeer)
}
function isNodeId (id, idLength) {
return id && Buffer.isBuffer(id) && id.length === idLength
}
function encodeNodes (nodes, idLength) {
var buf = Buffer.allocUnsafe(nodes.length * (idLength + 6))
var ptr = 0
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i]
if (!isNodeId(node.id, idLength)) continue
node.id.copy(buf, ptr)
ptr += idLength
var ip = (node.host || node.address).split('.')
for (var j = 0; j < 4; j++) buf[ptr++] = parseInt(ip[j] || 0, 10)
buf.writeUInt16BE(node.port, ptr)
ptr += 2
}
if (ptr === buf.length) return buf
return buf.slice(0, ptr)
}
function parseNodes (buf, idLength) {
var contacts = []
try {
for (var i = 0; i < buf.length; i += (idLength + 6)) {
var port = buf.readUInt16BE(i + (idLength + 4))
if (!port) continue
contacts.push({
id: buf.slice(i, i + idLength),
host: parseIp(buf, i + idLength),
port: port,
distance: 0,
token: null
})
}
} catch (err) {
// do nothing
}
return contacts
}
function parseIp (buf, offset) {
return buf[offset++] + '.' + buf[offset++] + '.' + buf[offset++] + '.' + buf[offset++]
}
function parsePeer (peer) {
if (typeof peer === 'string') return { host: peer.split(':')[0], port: Number(peer.split(':')[1]) }
return peer
}
function noop () {}
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')
}