mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-10 03:06:06 +00:00
* refactor: add js-libp2p-connection-manager to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> * test(conn-mgr): only run in node * refactor: add js-libp2p-identify to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Yusef Napora <yusef@protocol.ai> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-pnet to repo Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> * refactor: add libp2p-ping to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-circuit to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com> Co-authored-by: Yusef Napora <yusef@napora.org> Co-authored-by: dirkmc <dirk@mccormick.cx> * test(switch): avoid using instanceof * chore(switch): update bignumber dep * refactor(circuit): clean up tests * refactor(switch): consolidate get peer utils * test(identify): do deep checks of addresses * test(identify): bump timeout for identify test * test(switch): tidy up limit dialer test * refactor(switch): remove redundant circuit tests * chore: add coverage script * refactor(circuit): consolidate get peer info * docs: reference original repositories in each sub readme * docs: fix comment * refactor: clean up sub package.json files and readmes
111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
'use strict'
|
|
|
|
const crypto = require('crypto')
|
|
const debug = require('debug')
|
|
const pair = require('pull-pair')
|
|
const Reader = require('pull-reader')
|
|
const cat = require('pull-cat')
|
|
const pull = require('pull-stream')
|
|
const deferred = require('pull-defer')
|
|
|
|
const cryptoStreams = require('./crypto')
|
|
const NONCE_LENGTH = require('./key-generator').NONCE_LENGTH
|
|
|
|
const log = debug('libp2p:pnet')
|
|
log.err = debug('libp2p:pnet:err')
|
|
log.trace = debug('libp2p:pnet:trace')
|
|
|
|
/**
|
|
* Keeps track of the state of a given connection, such as the local psk
|
|
* and local and remote nonces for encryption/decryption
|
|
*/
|
|
class State {
|
|
/**
|
|
* @param {Buffer} psk The key buffer used for encryption
|
|
* @constructor
|
|
*/
|
|
constructor (psk) {
|
|
this.local = {
|
|
nonce: Buffer.from(
|
|
crypto.randomBytes(NONCE_LENGTH)
|
|
),
|
|
psk: psk
|
|
}
|
|
this.remote = { nonce: null }
|
|
|
|
this.rawReader = Reader(60e3)
|
|
this.encryptedReader = Reader(60e3)
|
|
|
|
this.rawPairStream = pair()
|
|
this.encryptedPairStream = pair()
|
|
|
|
// The raw, pair stream
|
|
this.innerRawStream = null
|
|
this.outerRawStream = {
|
|
sink: this.rawReader,
|
|
source: cat([
|
|
pull.values([
|
|
this.local.nonce
|
|
]),
|
|
this.rawPairStream.source
|
|
])
|
|
}
|
|
|
|
// The encrypted, pair stream
|
|
this.innerEncryptedStream = {
|
|
sink: this.encryptedReader,
|
|
source: this.encryptedPairStream.source
|
|
}
|
|
this.outerEncryptedStream = null
|
|
}
|
|
|
|
/**
|
|
* Creates encryption streams for the given state
|
|
*
|
|
* @param {function(Error, Connection)} callback
|
|
* @returns {void}
|
|
*/
|
|
encrypt (callback) {
|
|
// The outer stream needs to be returned before we setup the
|
|
// rest of the streams, so we're delaying the execution
|
|
setTimeout(() => {
|
|
// Read the nonce first, once we have it resolve the
|
|
// deferred source, so we keep reading
|
|
const deferredSource = deferred.source()
|
|
this.rawReader.read(NONCE_LENGTH, (err, data) => {
|
|
if (err) {
|
|
log.err('There was an error attempting to read the nonce', err)
|
|
}
|
|
log.trace('remote nonce received')
|
|
this.remote.nonce = data
|
|
deferredSource.resolve(this.rawReader.read())
|
|
})
|
|
|
|
this.innerRawStream = {
|
|
sink: this.rawPairStream.sink,
|
|
source: deferredSource
|
|
}
|
|
|
|
// Create the pull exchange between the two inner streams
|
|
pull(
|
|
this.innerRawStream,
|
|
cryptoStreams.createUnboxStream(this.remote, this.local.psk),
|
|
this.innerEncryptedStream,
|
|
cryptoStreams.createBoxStream(this.local.nonce, this.local.psk),
|
|
this.innerRawStream
|
|
)
|
|
|
|
this.outerEncryptedStream = {
|
|
sink: this.encryptedPairStream.sink,
|
|
source: this.encryptedReader.read()
|
|
}
|
|
|
|
callback(null, this.outerEncryptedStream)
|
|
}, 0)
|
|
|
|
return this.outerRawStream
|
|
}
|
|
}
|
|
|
|
module.exports = State
|