-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdaemon.js
More file actions
578 lines (526 loc) · 15.5 KB
/
daemon.js
File metadata and controls
578 lines (526 loc) · 15.5 KB
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
/* eslint max-depth: ["error", 6] */
'use strict'
const TCP = require('libp2p-tcp')
const Libp2p = require('./libp2p')
const createIntrospection = require('./introspection')
const PeerId = require('peer-id')
const ma = require('multiaddr')
const CID = require('cids')
const lp = require('it-length-prefixed')
const pipe = require('it-pipe')
const pushable = require('it-pushable')
const StreamHandler = require('./stream-handler')
const { concat } = require('streaming-iterables')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const { passThroughUpgrader } = require('./util')
const {
Request,
DHTRequest,
PeerstoreRequest,
PSRequest,
Response,
DHTResponse,
PSMessage,
StreamInfo
} = require('./protocol')
const LIMIT = 1 << 22 // 4MB
const log = require('debug')('libp2p:daemon')
class Daemon {
/**
* @class
* @param {object} options
* @param {string} options.multiaddr
* @param {Libp2p} options.libp2pNode
*/
constructor ({
multiaddr,
libp2pNode
}) {
this.multiaddr = ma(multiaddr)
this.libp2p = libp2pNode
// TODO: Add custom port + enabled (y/n)
this.introspection = createIntrospection({ libp2p: this.libp2p })
this.tcp = new TCP({ upgrader: passThroughUpgrader })
this.listener = this.tcp.createListener((maConn) => {
this.handleConnection(maConn)
})
this.streamHandlers = {}
this._onExit = this._onExit.bind(this)
}
/**
* Connects the daemons libp2p node to the peer provided
* in the ConnectRequest
*
* @param {ConnectRequest} connectRequest
* @returns {Promise<Connection>}
*/
connect (connectRequest) {
const peer = connectRequest.connect.peer
const addrs = connectRequest.connect.addrs.map((a) => ma(a))
const peerId = PeerId.createFromBytes(peer)
this.libp2p.peerStore.addressBook.set(peerId, addrs)
return this.libp2p.dial(peerId)
}
/**
* A number, or a string containing a number.
*
* @typedef {Object} OpenStream
* @property {StreamInfo} streamInfo
* @property {Stream} connection
*/
/**
* Opens a stream on one of the given protocols to the given peer
*
* @param {StreamOpenRequest} request
* @throws {Error}
* @returns {OpenStream}
*/
async openStream (request) {
const { peer, proto } = request.streamOpen
const peerId = PeerId.createFromB58String(uint8ArrayToString(peer, 'base58btc'))
const connection = this.libp2p.connectionManager.get(peerId)
const { stream, protocol } = await connection.newStream(proto)
return {
streamInfo: {
peer: peerId.toBytes(),
addr: connection.remoteAddr.bytes,
proto: protocol
},
connection: stream
}
}
/**
* Sends inbound requests for the given protocol
* to the unix socket path provided. If an existing handler
* is registered at the path, it will be overridden.
*
* @param {StreamHandlerRequest} request
* @returns {Promise<void>}
*/
async registerStreamHandler (request) {
const protocols = request.streamHandler.proto
const addr = ma(request.streamHandler.addr)
const addrString = addr.toString()
// If we have a handler, end it
if (this.streamHandlers[addrString]) {
this.streamHandlers[addrString].end()
delete this.streamHandlers[addrString]
}
protocols.forEach((proto) => {
// Connect the client socket with the libp2p connection
this.libp2p.handle(proto, ({ connection, stream, protocol }) => {
const message = StreamInfo.encode({
peer: connection.remotePeer.toBytes(),
addr: connection.remoteAddr.bytes,
proto: protocol
})
const encodedMessage = lp.encode.single(message)
// Tell the client about the new connection
// And then begin piping the client and peer connection
pipe(
concat([encodedMessage], stream.source),
clientConnection,
stream.sink
)
})
})
const clientConnection = await this.tcp.dial(addr)
}
/**
* Listens for process exit to handle cleanup
*
* @private
* @returns {void}
*/
_listen () {
// listen for graceful termination
process.on('SIGTERM', this._onExit)
process.on('SIGINT', this._onExit)
process.on('SIGHUP', this._onExit)
}
_onExit () {
this.stop({ exit: true })
}
/**
* Starts the daemon
*
* @returns {Promise<void>}
*/
async start () {
this._listen()
await this.introspection.start()
await this.libp2p.start()
await this.listener.listen(this.multiaddr)
this._start()
}
// TMP for demo
async _start () {
await new Promise(resolve => setTimeout(resolve, 20e3))
// Find providers
const cid = new CID('QmZKcfhEUbY6GumNmL7rga58qnRdMqMtv64smChiHJyfJf')
console.log('Searching for %s', cid.toString())
for await (const provider of this.libp2p.contentRouting.findProviders(cid, { maxNumProviders: 1 })) {
console.log('Found provider %j', provider)
}
}
/**
* Stops the daemon
*
* @param {object} options
* @param {boolean} options.exit - If the daemon process should exit
* @returns {Promise<void>}
*/
async stop (options = { exit: false }) {
await this.introspection.stop()
await this.libp2p.stop()
await this.listener.close()
if (options.exit) {
log('server closed, exiting')
}
process.removeListener('SIGTERM', this._onExit)
process.removeListener('SIGINT', this._onExit)
process.removeListener('SIGHUP', this._onExit)
}
handlePeerStoreRequest ({ peerStore }) {
const peerStoreAction = {
[PeerstoreRequest.Type.GET_PROTOCOLS]: function * (daemon) {
try {
const peerId = PeerId.createFromBytes(peerStore.id)
const peer = daemon.libp2p.peerStore.get(peerId)
const protos = peer.protocols
yield OkResponse({ peerStore: { protos } })
} catch (err) {
throw new Error('ERR_INVALID_PEERSTORE_REQUEST')
}
},
[PeerstoreRequest.Type.GET_PEER_INFO]: function * () {
yield ErrorResponse('ERR_NOT_IMPLEMENTED')
},
invalid: function * () {
yield ErrorResponse('ERR_INVALID_REQUEST_TYPE')
}
}
return peerStoreAction[peerStore.type](this)
}
/**
* Parses and responds to PSRequests. An async generator will
* be returned.
*
* @private
* @param {Request} request
* @returns {*} Returns an async generator
*/
handlePubsubRequest ({ pubsub }) {
const pubsubAction = {
[PSRequest.Type.GET_TOPICS]: async function * (daemon) {
const topics = await daemon.libp2p.pubsub.getTopics()
yield OkResponse({ pubsub: { topics } })
},
[PSRequest.Type.SUBSCRIBE]: async function * (daemon) {
const topic = pubsub.topic
const onMessage = pushable()
await daemon.libp2p.pubsub.subscribe(topic, (msg) => {
onMessage.push(PSMessage.encode({
from: msg.from && uint8ArrayFromString(msg.from),
data: msg.data,
seqno: msg.seqno,
topicIDs: msg.topicIDs,
signature: msg.signature,
key: msg.key
}))
})
yield OkResponse()
yield * onMessage
},
[PSRequest.Type.PUBLISH]: async function * (daemon) {
const topic = pubsub.topic
const data = pubsub.data
await daemon.libp2p.pubsub.publish(topic, data)
yield OkResponse()
},
invalid: function * () {
yield ErrorResponse('ERR_INVALID_REQUEST_TYPE')
}
}
if (!pubsubAction[pubsub.type]) {
return pubsubAction.invalid()
}
return pubsubAction[pubsub.type](this)
}
/**
* Parses and responds to DHTRequests
*
* @private
* @param {Request} request
* @returns {DHTResponse[]}
*/
handleDHTRequest ({ dht }) {
const dhtAction = {
[DHTRequest.Type.FIND_PEER]: async function * (daemon) {
const peerId = PeerId.createFromBytes(dht.peer)
try {
const peer = await daemon.libp2p.peerRouting.findPeer(peerId)
yield OkResponse({
dht: {
type: DHTResponse.Type.VALUE,
peer: {
id: peer.id.toBytes(),
addrs: peer.multiaddrs.map(m => m.bytes)
}
}
})
} catch (err) {
yield ErrorResponse(err.message)
}
},
[DHTRequest.Type.FIND_PROVIDERS]: async function * (daemon) {
const cid = new CID(dht.cid)
const maxNumProviders = dht.count
let okSent = false
try {
for await (const provider of daemon.libp2p.contentRouting.findProviders(cid, {
maxNumProviders
})) {
if (!okSent) {
okSent = true
yield OkResponse({
dht: {
type: DHTResponse.Type.BEGIN
}
})
}
yield DHTResponse.encode({
type: DHTResponse.Type.VALUE,
peer: {
id: provider.id.toBytes(),
addrs: (provider.multiaddrs || []).map(m => m.bytes)
}
})
}
} catch (err) {
yield ErrorResponse(err.message)
return
}
yield DHTResponse.encode({
type: DHTResponse.Type.END
})
},
[DHTRequest.Type.PROVIDE]: async function * (daemon) {
const cid = new CID(dht.cid)
await daemon.libp2p.contentRouting.provide(cid)
yield OkResponse()
},
[DHTRequest.Type.GET_CLOSEST_PEERS]: async function * (daemon) {
yield OkResponse({
dht: {
type: DHTResponse.Type.BEGIN
}
})
for await (const peerId of daemon.libp2p._dht.getClosestPeers(dht.key)) {
yield DHTResponse.encode({
type: DHTResponse.Type.VALUE,
value: peerId.toBytes()
})
}
yield DHTResponse.encode({
type: DHTResponse.Type.END
})
},
[DHTRequest.Type.GET_PUBLIC_KEY]: async function * (daemon) {
const peerId = PeerId.createFromBytes(dht.peer)
const pubKey = await daemon.libp2p._dht.getPublicKey(peerId)
yield OkResponse({
dht: {
type: DHTResponse.Type.VALUE,
value: pubKey.bytes
}
})
},
[DHTRequest.Type.GET_VALUE]: async function * (daemon) {
try {
const value = await daemon.libp2p.contentRouting.get(dht.key)
yield OkResponse({
dht: {
type: DHTResponse.Type.VALUE,
value: value
}
})
} catch (err) {
yield ErrorResponse(err.message)
}
},
[DHTRequest.Type.PUT_VALUE]: async function * (daemon) {
await daemon.libp2p.contentRouting.put(
dht.key,
dht.value
)
yield OkResponse()
},
invalid: function * () {
yield ErrorResponse('ERR_INVALID_REQUEST_TYPE')
}
}
if (!dhtAction[dht.type]) {
return dhtAction.invalid()
}
return dhtAction[dht.type](this)
}
/**
* Handles requests for the given connection
*
* @private
* @param {MultiaddrConnection} maConn
* @returns {void}
*/
async handleConnection (maConn) {
const daemon = this
const streamHandler = new StreamHandler({ stream: maConn, maxLength: LIMIT })
await pipe(
streamHandler.decoder,
source => (async function * () {
for await (let request of source) {
try {
request = Request.decode(request.slice())
} catch (err) {
yield ErrorResponse('ERR_INVALID_MESSAGE')
}
switch (request.type) {
// Connect to another peer
case Request.Type.CONNECT: {
try {
await daemon.connect(request)
} catch (err) {
yield ErrorResponse(err.message)
break
}
yield OkResponse()
break
}
// Get the daemon peer id and addresses
case Request.Type.IDENTIFY: {
yield OkResponse({
identify: {
id: daemon.libp2p.peerId.toBytes(),
addrs: daemon.libp2p.multiaddrs.map(m => m.bytes)
}
})
break
}
// Get a list of our current peers
case Request.Type.LIST_PEERS: {
const peers = Array.from(daemon.libp2p.peerStore.peers.values()).map((peer) => {
// TODO: conn mgr
const conn = daemon.libp2p.registrar.getConnection(peer.id)
const addr = conn.remoteAddr
return {
id: peer.id.toBytes(),
addrs: [addr ? addr.bytes : null]
}
})
yield OkResponse({ peers })
break
}
case Request.Type.STREAM_OPEN: {
let response
try {
response = await daemon.openStream(request)
} catch (err) {
yield ErrorResponse(err.message)
break
}
// write the response
yield OkResponse({
streamInfo: response.streamInfo
})
const stream = streamHandler.rest()
// then pipe the connection to the client
await pipe(
stream,
response.connection,
stream
)
// Exit the iterator, no more requests can come through
return
}
case Request.Type.STREAM_HANDLER: {
try {
await daemon.registerStreamHandler(request)
} catch (err) {
yield ErrorResponse(err.message)
break
}
// write the response
yield OkResponse()
break
}
case Request.Type.PEERSTORE: {
yield * daemon.handlePeerStoreRequest(request)
break
}
case Request.Type.PUBSUB: {
yield * daemon.handlePubsubRequest(request)
break
}
case Request.Type.DHT: {
yield * daemon.handleDHTRequest(request)
break
}
// Not yet supported or doesn't exist
default:
yield ErrorResponse('ERR_INVALID_REQUEST_TYPE')
break
}
}
})(),
async function (source) {
for await (const result of source) {
streamHandler.write(result)
}
}
)
}
}
/**
* Creates and encodes an OK response
*
* @private
* @param {Object} data - an optional map of values to be assigned to the response
* @returns {Response}
*/
function OkResponse (data) {
return Response.encode({
type: Response.Type.OK,
...data
})
}
/**
* Creates and encodes an ErrorResponse
*
* @private
* @param {string} message
* @returns {ErrorResponse}
*/
function ErrorResponse (message) {
return Response.encode({
type: Response.Type.ERROR,
error: {
msg: message
}
})
}
/**
* Creates a daemon from the provided Daemon Options
*
* @param {object} options
* @returns {Daemon}
*/
const createDaemon = async (options) => {
const libp2pNode = await Libp2p.createLibp2p(options)
const daemon = new Daemon({
multiaddr: options.listen,
libp2pNode: libp2pNode
})
return daemon
}
module.exports.createDaemon = createDaemon