refactor: add core modules to libp2p (#400)

* 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
This commit is contained in:
Jacob Heun 2019-08-16 17:30:03 +02:00 committed by GitHub
parent d92306f222
commit b294301456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
87 changed files with 5399 additions and 750 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ logs
*.log
coverage
.nyc_output
# Runtime data
pids

1
examples/pnet-ipfs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp/

View File

@ -0,0 +1,29 @@
# Private Networking with IPFS
This example shows how to set up a private network of IPFS nodes.
## Setup
Install dependencies:
```
npm install
```
## Run
Running the example will cause two nodes with the same swarm key to be started and exchange basic information.
```
node index.js
```
### Using different keys
This example includes `TASK` comments that can be used to try the example with different swarm keys. This will
allow you to see how nodes will fail to connect if they are on different private networks and try to connect to
one another.
To change the swarm key of one of the nodes, look through `index.js` for comments starting with `TASK` to indicate
where lines are that pertain to changing the swarm key of node 2.
### Exploring the repos
Once you've run the example you can take a look at the repos in the `./tmp` directory to see how they differ, including
the swarm keys. You should see a `swarm.key` file in each of the repos and when the nodes are on the same private network
this contents of the `swarm.key` files should be the same.

145
examples/pnet-ipfs/index.js Normal file
View File

@ -0,0 +1,145 @@
/* eslint no-console: ["off"] */
'use strict'
const IPFS = require('ipfs')
const assert = require('assert').strict
const writeKey = require('libp2p-pnet').generate
const path = require('path')
const fs = require('fs')
const privateLibp2pBundle = require('./libp2p-bundle')
const { mkdirp } = require('./utils')
// Create two separate repo paths so we can run two nodes and check their output
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
mkdirp(repo1)
mkdirp(repo2)
// Create a buffer and write the swarm key to it
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)
// This key is for the `TASK` mentioned in the writeFileSync calls below
const otherSwarmKey = Buffer.alloc(95)
writeKey(otherSwarmKey)
// Add the swarm key to both repos
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
fs.writeFileSync(swarmKey1Path, swarmKey)
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
fs.writeFileSync(swarmKey2Path, swarmKey)
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)
// Create the first ipfs node
const node1 = new IPFS({
repo: repo1,
libp2p: privateLibp2pBundle(swarmKey1Path),
config: {
Addresses: {
// Set the swarm address so we dont get port collision on the nodes
Swarm: ['/ip4/0.0.0.0/tcp/9101']
}
}
})
// Create the second ipfs node
const node2 = new IPFS({
repo: repo2,
libp2p: privateLibp2pBundle(swarmKey2Path),
config: {
Addresses: {
// Set the swarm address so we dont get port collision on the nodes
Swarm: ['/ip4/0.0.0.0/tcp/9102']
}
}
})
console.log('auto starting the nodes...')
// `nodesStarted` keeps track of how many of our nodes have started
let nodesStarted = 0
/**
* Calls `connectAndTalk` when both nodes have started
* @returns {void}
*/
const didStartHandler = () => {
if (++nodesStarted === 2) {
// If both nodes are up, start talking
connectAndTalk()
}
}
/**
* Exits the process when all started nodes have stopped
* @returns {void}
*/
const didStopHandler = () => {
if (--nodesStarted < 1) {
console.log('all nodes stopped, exiting.')
process.exit(0)
}
}
/**
* Stops the running nodes
* @param {Error} err An optional error to log to the console
* @returns {void}
*/
const doStop = (err) => {
if (err) {
console.error(err)
}
console.log('Shutting down...')
node1.stop()
node2.stop()
}
/**
* Connects the IPFS nodes and transfers data between them
* @returns {void}
*/
const connectAndTalk = async () => {
console.log('connecting the nodes...')
const node2Id = await node2.id()
const dataToAdd = Buffer.from('Hello, private friend!')
// Connect the nodes
// This will error when different private keys are used
try {
await node1.swarm.connect(node2Id.addresses[0])
} catch (err) {
return doStop(err)
}
console.log('the nodes are connected, let\'s add some data')
// Add some data to node 1
let addedCID
try {
addedCID = await node1.files.add(dataToAdd)
} catch (err) {
return doStop(err)
}
console.log(`added ${addedCID[0].path} to the node1`)
// Retrieve the data from node 2
let cattedData
try {
cattedData = await node2.files.cat(addedCID[0].path)
} catch (err) {
return doStop(err)
}
assert.deepEqual(cattedData.toString(), dataToAdd.toString(), 'Should have equal data')
console.log(`successfully retrieved "${dataToAdd.toString()}" from node2`)
doStop()
}
// Wait for the nodes to boot
node1.once('start', didStartHandler)
node2.once('start', didStartHandler)
// Listen for the nodes stopping so we can cleanup
node1.once('stop', didStopHandler)
node2.once('stop', didStopHandler)

View File

@ -0,0 +1,60 @@
'use strict'
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const fs = require('fs')
const Protector = require('libp2p-pnet')
/**
* Options for the libp2p bundle
* @typedef {Object} libp2pBundle~options
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
* @property {Object} config - The config of the IPFS node
* @property {Object} options - The options given to the IPFS node
*/
/**
* privateLibp2pBundle returns a libp2p bundle function that will use the swarm
* key at the given `swarmKeyPath` to create the Protector
*
* @param {string} swarmKeyPath The path to our swarm key
* @returns {libp2pBundle} Returns a libp2pBundle function for use in IPFS creation
*/
const privateLibp2pBundle = (swarmKeyPath) => {
/**
* This is the bundle we will use to create our fully customized libp2p bundle.
*
* @param {libp2pBundle~options} opts The options to use when generating the libp2p node
* @returns {Libp2p} Our new libp2p node
*/
const libp2pBundle = (opts) => {
// Set convenience variables to clearly showcase some of the useful things that are available
const peerInfo = opts.peerInfo
const peerBook = opts.peerBook
// Build and return our libp2p node
return new Libp2p({
peerInfo,
peerBook,
modules: {
transport: [TCP], // We're only using the TCP transport for this example
streamMuxer: [MPLEX], // We're only using mplex muxing
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
// care about node identity, and only the presence of private keys
connEncryption: [SECIO],
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
// being left in for explicit readability.
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
peerDiscovery: [],
connProtector: new Protector(fs.readFileSync(swarmKeyPath))
}
})
}
return libp2pBundle
}
module.exports = privateLibp2pBundle

View File

@ -0,0 +1,21 @@
{
"name": "pnet-ipfs-example",
"version": "1.0.0",
"description": "An example of private networking with IPFS",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ipfs": "~0.32.3",
"libp2p": "~0.23.1",
"libp2p-mplex": "~0.8.2",
"libp2p-pnet": "../../",
"libp2p-secio": "~0.10.0",
"libp2p-tcp": "~0.13.0"
}
}

View File

@ -0,0 +1,28 @@
'use strict'
const fs = require('fs')
const path = require('path')
/**
* mkdirp recursively creates needed folders for the given dir path
* @param {string} dir
* @returns {string} The path that was created
*/
module.exports.mkdirp = (dir) => {
return path
.resolve(dir)
.split(path.sep)
.reduce((acc, cur) => {
const currentPath = path.normalize(acc + path.sep + cur)
try {
fs.statSync(currentPath)
} catch (e) {
if (e.code === 'ENOENT') {
fs.mkdirSync(currentPath)
} else {
throw e
}
}
return currentPath
}, '')
}

View File

@ -16,7 +16,8 @@
"test:browser": "aegir test -t browser",
"release": "aegir release -t node -t browser",
"release-minor": "aegir release --type minor -t node -t browser",
"release-major": "aegir release --type major -t node -t browser"
"release-major": "aegir release --type major -t node -t browser",
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
},
"repository": {
"type": "git",
@ -44,17 +45,20 @@
},
"dependencies": {
"async": "^2.6.2",
"bignumber.js": "^8.1.1",
"bignumber.js": "^9.0.0",
"class-is": "^1.1.0",
"debug": "^4.1.1",
"err-code": "^1.1.2",
"fsm-event": "^2.1.0",
"hashlru": "^2.3.0",
"interface-connection": "~0.3.3",
"libp2p-circuit": "~0.3.6",
"libp2p-connection-manager": "^0.1.0",
"libp2p-identify": "~0.7.6",
"libp2p-ping": "^0.8.5",
"latency-monitor": "~0.2.1",
"libp2p-circuit": "./src/circuit",
"libp2p-connection-manager": "./src/connection-manager",
"libp2p-crypto": "~0.16.1",
"libp2p-identify": "./src/identify",
"libp2p-ping": "./src/ping",
"libp2p-pnet": "./src/pnet",
"libp2p-switch": "./src/switch",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
@ -65,10 +69,16 @@
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "~0.15.1",
"pull-stream": "^3.6.13",
"pull-cat": "^1.1.11",
"pull-defer": "~0.2.3",
"pull-handshake": "^1.1.4",
"pull-reader": "^1.3.1",
"pull-stream": "^3.6.9",
"promisify-es6": "^1.0.3",
"protons": "^1.0.1",
"retimer": "^2.0.0",
"superstruct": "^0.6.0"
"superstruct": "^0.6.0",
"xsalsa20": "^1.0.2"
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
@ -80,7 +90,6 @@
"electron-webrtc": "^0.3.0",
"interface-datastore": "^0.6.0",
"libp2p-bootstrap": "^0.9.7",
"libp2p-circuit": "^0.3.7",
"libp2p-delegated-content-routing": "^0.2.2",
"libp2p-delegated-peer-routing": "^0.2.2",
"libp2p-floodsub": "~0.17.0",
@ -103,8 +112,8 @@
"pull-length-prefixed": "^1.3.3",
"pull-mplex": "^0.1.2",
"pull-pair": "^1.1.0",
"pull-protocol-buffers": "~0.1.2",
"pull-serializer": "^0.3.2",
"pull-stream": "^3.6.12",
"sinon": "^7.2.7",
"webrtcsupport": "^2.2.0",
"wrtc": "^0.4.1"

View File

@ -0,0 +1,128 @@
EDIT: This document is outdated and here only for historical purposes
NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation
Example:
- if there are apples
- eat them
- if not, check for pears
- then eat them
- if not, check for cherries
- then eat them
Or,
- if there are apples
- eat them
- if not
- check for pears
- then eat them
- if not
- check for cherries
- then eat them
In order to minimize nesting, the first example is preferred
# Relay flow
## Relay transport (dialer/listener)
- ### Dial over a relay
- See if there is a relay that's already connected to the destination peer, if not
- Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful.
- If successful
- Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`.
- If no relays could connect, fail the same way a regular transport would
- Once the connection has been established, the swarm should treat it as a regular connection,
- i.e. muxing, encrypt, etc should all be performed on the relayed connection
- ### Listen for relayed connections
- Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections
- A connection arrives
- read the address of the source peer from the incoming connection stream
- if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list
- pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow
- ### Relay discovery and static relay addresses in swarm config
- #### Relay address in swarm config
- A peer has relay addresses in its swarm config section
- On node startup, connect to the relays in swarm config
- if successful add address to swarms PeerInfo's multiaddresses
- `identify` should take care of announcing that the peer is reachable over the listed relays
- #### Passive relay discovery
- A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.)
- *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible*
- *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list*
- *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.*
- *Should it be possible to disable passive relay discovery?*
- This could be useful when the peer wants to be reachable **only** over the listed relays
- If the incoming peer is a relay, send an `ls` and record its peers
## Relay Nodes
- ### Passive relay node
- *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.*
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
- check its swarm's peerbook(?) see if its a known peer, if it is
- use the swarms existing connection and
- send the multistream header and the source peer address to the dest peer
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
- circuit the source and dest connections
- if couldn't dial, or the connection/stream to the dest peer closed prematurelly
- close the src stream
- ### Active relay node
- *An active relay node can dial other peers even if its swarm doesnt know about those peers*
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
- use the swarm to dial to the dest node
- send the multistream header and the source peer address to the dest peer
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
- circuit the source and dest connections
- if couldn't dial, or the connection/stream to the dest peer closed prematurely
- close the src stream
- ### `ls` command
- *A relay node can allow the peers known to it's swarm to be listed*
- *this should be possible to enable/disable from the config*
- when a relay gets the `ls` request
- if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs
- e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]`
- if disabled, respond with `na`
## Relay Implementation notes
- ### Relay transport
- Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution.
- ### `circuit-relay`
- This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay.
## Relay swarm integration
- The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p
- Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods
- ### Listener
- the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto
- if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate
- when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed
- ### Dialer
- When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on
- *The relay will be used in two cases*
- If the peer has an explicit relay address that it can be reached on
- no other transport is available
- The relay will attempt to dial the peer over that relay
- If no explicit relay address is provided
- no other transport is available
- A generic circuit address will be added to the peers multiaddr list
- i.e. `/p2p-circuit/ipfs/QmDest`
- If another transport is available, then use that instead of the relay

156
src/circuit/README.md Normal file
View File

@ -0,0 +1,156 @@
# js-libp2p-circuit
> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit.
`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node.
This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
### Why?
`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
- routing traffic between private nets and circumventing NAT layers
- route mangling for better privacy (matreshka/shallot dialing).
It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
### libp2p-circuit and IPFS
Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
## Table of Contents
- [Install](#install)
- [npm](#npm)
- [Usage](#usage)
- [Example](#example)
- [This module uses `pull-streams`](#this-module-uses-pull-streams)
- [Converting `pull-streams` to Node.js Streams](#converting-pull-streams-to-nodejs-streams)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)
## Usage
### Example
#### Create dialer/listener
```js
const Circuit = require('libp2p-circuit')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh1 = multiaddr('/p2p-circuit/ipfs/QmHash') // dial /ipfs/QmHash over any circuit
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
const listener = circuit.createListener(mh1, (connection) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})
listener.listen(() => {
console.log('listening')
pull(
circuit.dial(mh1),
pull.log,
pull.onEnd(() => {
circuit.close()
})
)
})
```
Outputs:
```sh
listening
new connection opened
hello
```
#### Create `relay`
```js
const Relay = require('libp2p-circuit').Relay
const relay = new Relay(options)
relay.mount(swarmInstance) // start relaying traffic
```
### This module uses `pull-streams`
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
You can learn more about pull-streams at:
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
- [pull-streams documentation](https://pull-stream.github.io/)
#### Converting `pull-streams` to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
```js
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
## API
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
`libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
`/p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
Both for dialing and listening.
### Implementation rational
This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p-swarm`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p-swarm` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
```
libp2p libp2p-circuit (transport)
+-------------------------------------------------+ +--------------------------+
| +---------------------------------+ | | |
| | | | | +------------------+ |
| | | | circuit-relay listens for the HOP | | | |
| | libp2p-swarm <------------------------------------------------| circuit-relay | |
| | | | message to handle incomming relay | | | |
| | | | requests from other nodes | +------------------+ |
| +---------------------------------+ | | |
| ^ ^ ^ ^ ^ ^ | | +------------------+ |
| | | | | | | | | | +-------------+ | |
| | | | | | | | dialer uses libp2p-swarm to dial | | | | | |
| | | | +----------------------------------------------------------------------> dialer | | |
| | | transports | | to a circuit-relay node using the | | | | | |
| | | | | | | HOP message | | +-------------+ | |
| | | | | | | | | | |
| v v | v v | | | | |
|+------------------|----------------------------+| | | +-------------+ | |
|| | | | | || | | | | | |
||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | |
|| | +--------------------------------------------------------------------------> | | |
|| | | |plugs in just || circuit-relay nodes | | +-------------+ | |
|| | | |as any other || | | | |
|| | | |transport || | +------------------+ |
|+-----------------------------------------------+| | |
+-------------------------------------------------+ +--------------------------+
```

126
src/circuit/circuit.js Normal file
View File

@ -0,0 +1,126 @@
'use strict'
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
const CircuitDialer = require('./circuit/dialer')
const utilsFactory = require('./circuit/utils')
const debug = require('debug')
const log = debug('libp2p:circuit:transportdialer')
log.err = debug('libp2p:circuit:error:transportdialer')
const createListener = require('./listener')
class Circuit {
static get tag () {
return 'Circuit'
}
/**
* Creates an instance of Dialer.
*
* @param {Swarm} swarm - the swarm
* @param {any} options - config options
*
* @memberOf Dialer
*/
constructor (swarm, options) {
this.options = options || {}
this.swarm = swarm
this.dialer = null
this.utils = utilsFactory(swarm)
this.peerInfo = this.swarm._peerInfo
this.relays = this.filter(this.peerInfo.multiaddrs.toArray())
// if no explicit relays, add a default relay addr
if (this.relays.length === 0) {
this.peerInfo
.multiaddrs
.add(`/p2p-circuit/ipfs/${this.peerInfo.id.toB58String()}`)
}
this.dialer = new CircuitDialer(swarm, options)
this.swarm.on('peer-mux-established', (peerInfo) => {
this.dialer.canHop(peerInfo)
})
this.swarm.on('peer-mux-closed', (peerInfo) => {
this.dialer.relayPeers.delete(peerInfo.id.toB58String())
})
}
/**
* Dial the relays in the Addresses.Swarm config
*
* @param {Array} relays
* @return {void}
*/
_dialSwarmRelays () {
// if we have relay addresses in swarm config, then dial those relays
this.relays.forEach((relay) => {
const relaySegments = relay
.toString()
.split('/p2p-circuit')
.filter(segment => segment.length)
relaySegments.forEach((relaySegment) => {
const ma = this.utils.peerInfoFromMa(multiaddr(relaySegment))
this.dialer._dialRelay(ma)
})
})
}
/**
* Dial a peer over a relay
*
* @param {multiaddr} ma - the multiaddr of the peer to dial
* @param {Object} options - dial options
* @param {Function} cb - a callback called once dialed
* @returns {Connection} - the connection
*
* @memberOf Dialer
*/
dial (ma, options, cb) {
return this.dialer.dial(ma, options, cb)
}
/**
* Create a listener
*
* @param {any} options
* @param {Function} handler
* @return {listener}
*/
createListener (options, handler) {
if (typeof options === 'function') {
handler = options
options = this.options || {}
}
const listener = createListener(this.swarm, options, handler)
listener.on('listen', this._dialSwarmRelays.bind(this))
return listener
}
/**
* Filter check for all multiaddresses
* that this transport can dial on
*
* @param {any} multiaddrs
* @returns {Array<multiaddr>}
*
* @memberOf Dialer
*/
filter (multiaddrs) {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
return multiaddrs.filter((ma) => {
return mafmt.Circuit.matches(ma)
})
}
}
module.exports = Circuit

View File

@ -0,0 +1,275 @@
'use strict'
const once = require('once')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const multiaddr = require('multiaddr')
const Connection = require('interface-connection').Connection
const utilsFactory = require('./utils')
const StreamHandler = require('./stream-handler')
const debug = require('debug')
const log = debug('libp2p:circuit:dialer')
log.err = debug('libp2p:circuit:error:dialer')
const multicodec = require('../multicodec')
const proto = require('../protocol')
class Dialer {
/**
* Creates an instance of Dialer.
* @param {Swarm} swarm - the swarm
* @param {any} options - config options
*
* @memberOf Dialer
*/
constructor (swarm, options) {
this.swarm = swarm
this.relayPeers = new Map()
this.relayConns = new Map()
this.options = options
this.utils = utilsFactory(swarm)
}
/**
* Helper that returns a relay connection
*
* @param {*} relay
* @param {*} callback
* @returns {Function} - callback
*/
_dialRelayHelper (relay, callback) {
if (this.relayConns.has(relay.id.toB58String())) {
return callback(null, this.relayConns.get(relay.id.toB58String()))
}
return this._dialRelay(relay, callback)
}
/**
* Dial a peer over a relay
*
* @param {multiaddr} ma - the multiaddr of the peer to dial
* @param {Function} cb - a callback called once dialed
* @returns {Connection} - the connection
*
*/
dial (ma, cb) {
cb = cb || (() => { })
const strMa = ma.toString()
if (!strMa.includes('/p2p-circuit')) {
log.err('invalid circuit address')
return cb(new Error('invalid circuit address'))
}
const addr = strMa.split('p2p-circuit') // extract relay address if any
const relay = addr[0] === '/' ? null : multiaddr(addr[0])
const peer = multiaddr(addr[1] || addr[0])
const dstConn = new Connection()
setImmediate(
this._dialPeer.bind(this),
peer,
relay,
(err, conn) => {
if (err) {
log.err(err)
return cb(err)
}
dstConn.setInnerConn(conn)
cb(null, dstConn)
})
return dstConn
}
/**
* Does the peer support the HOP protocol
*
* @param {PeerInfo} peer
* @param {Function} callback
* @returns {void}
*/
canHop (peer, callback) {
callback = once(callback || (() => { }))
this._dialRelayHelper(peer, (err, conn) => {
if (err) {
return callback(err)
}
const sh = new StreamHandler(conn)
waterfall([
(cb) => sh.write(proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.CAN_HOP
}), cb),
(cb) => sh.read(cb)
], (err, msg) => {
if (err) {
return callback(err)
}
const response = proto.CircuitRelay.decode(msg)
if (response.code !== proto.CircuitRelay.Status.SUCCESS) {
const err = new Error(`HOP not supported, skipping - ${this.utils.getB58String(peer)}`)
log(err)
return callback(err)
}
log('HOP supported adding as relay - %s', this.utils.getB58String(peer))
this.relayPeers.set(this.utils.getB58String(peer), peer)
sh.close()
callback()
})
})
}
/**
* Dial the destination peer over a relay
*
* @param {multiaddr} dstMa
* @param {Connection|PeerInfo} relay
* @param {Function} cb
* @return {Function|void}
* @private
*/
_dialPeer (dstMa, relay, cb) {
if (typeof relay === 'function') {
cb = relay
relay = null
}
if (!cb) {
cb = () => {}
}
dstMa = multiaddr(dstMa)
// if no relay provided, dial on all available relays until one succeeds
if (!relay) {
const relays = Array.from(this.relayPeers.values())
const next = (nextRelay) => {
if (!nextRelay) {
const err = `no relay peers were found or all relays failed to dial`
log.err(err)
return cb(err)
}
return this._negotiateRelay(
nextRelay,
dstMa,
(err, conn) => {
if (err) {
log.err(err)
return next(relays.shift())
}
cb(null, conn)
})
}
next(relays.shift())
} else {
return this._negotiateRelay(
relay,
dstMa,
(err, conn) => {
if (err) {
log.err('An error has occurred negotiating the relay connection', err)
return cb(err)
}
return cb(null, conn)
})
}
}
/**
* Negotiate the relay connection
*
* @param {Multiaddr|PeerInfo|Connection} relay - the Connection or PeerInfo of the relay
* @param {multiaddr} dstMa - the multiaddr of the peer to relay the connection for
* @param {Function} callback - a callback which gets the negotiated relay connection
* @returns {void}
* @private
*
* @memberOf Dialer
*/
_negotiateRelay (relay, dstMa, callback) {
dstMa = multiaddr(dstMa)
relay = this.utils.peerInfoFromMa(relay)
const srcMas = this.swarm._peerInfo.multiaddrs.toArray()
this._dialRelayHelper(relay, (err, conn) => {
if (err) {
log.err(err)
return callback(err)
}
const sh = new StreamHandler(conn)
waterfall([
(cb) => {
log('negotiating relay for peer %s', dstMa.getPeerId())
let dstPeerId
try {
dstPeerId = PeerId.createFromB58String(dstMa.getPeerId()).id
} catch (err) {
return cb(err)
}
sh.write(
proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: this.swarm._peerInfo.id.id,
addrs: srcMas.map((addr) => addr.buffer)
},
dstPeer: {
id: dstPeerId,
addrs: [dstMa.buffer]
}
}), cb)
},
(cb) => sh.read(cb)
], (err, msg) => {
if (err) {
return callback(err)
}
const message = proto.CircuitRelay.decode(msg)
if (message.type !== proto.CircuitRelay.Type.STATUS) {
return callback(new Error(`Got invalid message type - ` +
`expected ${proto.CircuitRelay.Type.STATUS} got ${message.type}`))
}
if (message.code !== proto.CircuitRelay.Status.SUCCESS) {
return callback(new Error(`Got ${message.code} error code trying to dial over relay`))
}
callback(null, new Connection(sh.rest()))
})
})
}
/**
* Dial a relay peer by its PeerInfo
*
* @param {PeerInfo} peer - the PeerInfo of the relay peer
* @param {Function} cb - a callback with the connection to the relay peer
* @returns {void}
* @private
*/
_dialRelay (peer, cb) {
cb = once(cb || (() => { }))
this.swarm.dial(
peer,
multicodec.relay,
once((err, conn) => {
if (err) {
log.err(err)
return cb(err)
}
cb(null, conn)
}))
}
}
module.exports = Dialer

283
src/circuit/circuit/hop.js Normal file
View File

@ -0,0 +1,283 @@
'use strict'
const pull = require('pull-stream/pull')
const debug = require('debug')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const EE = require('events').EventEmitter
const once = require('once')
const utilsFactory = require('./utils')
const StreamHandler = require('./stream-handler')
const proto = require('../protocol').CircuitRelay
const multiaddr = require('multiaddr')
const series = require('async/series')
const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const multicodec = require('./../multicodec')
const log = debug('libp2p:circuit:relay')
log.err = debug('libp2p:circuit:error:relay')
class Hop extends EE {
/**
* Construct a Circuit object
*
* This class will handle incoming circuit connections and
* either start a relay or hand the relayed connection to
* the swarm
*
* @param {Swarm} swarm
* @param {Object} options
*/
constructor (swarm, options) {
super()
this.swarm = swarm
this.peerInfo = this.swarm._peerInfo
this.utils = utilsFactory(swarm)
this.config = options || { active: false, enabled: false }
this.active = this.config.active
}
/**
* Handle the relay message
*
* @param {CircuitRelay} message
* @param {StreamHandler} sh
* @returns {*}
*/
handle (message, sh) {
if (!this.config.enabled) {
this.utils.writeResponse(
sh,
proto.Status.HOP_CANT_SPEAK_RELAY)
return sh.close()
}
// check if message is `CAN_HOP`
if (message.type === proto.Type.CAN_HOP) {
this.utils.writeResponse(
sh,
proto.Status.SUCCESS)
return sh.close()
}
// This is a relay request - validate and create a circuit
let srcPeerId = null
let dstPeerId = null
try {
srcPeerId = PeerId.createFromBytes(message.srcPeer.id).toB58String()
dstPeerId = PeerId.createFromBytes(message.dstPeer.id).toB58String()
} catch (err) {
log.err(err)
if (!srcPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_SRC_MULTIADDR_INVALID)
return sh.close()
}
if (!dstPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_DST_MULTIADDR_INVALID)
return sh.close()
}
}
if (srcPeerId === dstPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_CANT_RELAY_TO_SELF)
return sh.close()
}
if (!message.dstPeer.addrs.length) {
// TODO: use encapsulate here
const addr = multiaddr(`/p2p-circuit/ipfs/${dstPeerId}`).buffer
message.dstPeer.addrs.push(addr)
}
log('trying to establish a circuit: %s <-> %s', srcPeerId, dstPeerId)
const noPeer = () => {
// log.err(err)
this.utils.writeResponse(
sh,
proto.Status.HOP_NO_CONN_TO_DST)
return sh.close()
}
const isConnected = (cb) => {
let dstPeer
try {
dstPeer = this.swarm._peerBook.get(dstPeerId)
if (!dstPeer.isConnected() && !this.active) {
const err = new Error(`No Connection to peer ${dstPeerId}`)
noPeer(err)
return cb(err)
}
} catch (err) {
if (!this.active) {
noPeer(err)
return cb(err)
}
}
cb()
}
series([
(cb) => this.utils.validateAddrs(message, sh, proto.Type.HOP, cb),
(cb) => isConnected(cb),
(cb) => this._circuit(sh, message, cb)
], (err) => {
if (err) {
log.err(err)
sh.close()
return setImmediate(() => this.emit('circuit:error', err))
}
setImmediate(() => this.emit('circuit:success'))
})
}
/**
* Connect to STOP
*
* @param {PeerInfo} peer
* @param {StreamHandler} srcSh
* @param {function} callback
* @returns {void}
*/
_connectToStop (peer, srcSh, callback) {
this._dialPeer(peer, (err, dstConn) => {
if (err) {
this.utils.writeResponse(
srcSh,
proto.Status.HOP_CANT_DIAL_DST)
log.err(err)
return callback(err)
}
return this.utils.writeResponse(
srcSh,
proto.Status.SUCCESS,
(err) => {
if (err) {
log.err(err)
return callback(err)
}
return callback(null, dstConn)
})
})
}
/**
* Negotiate STOP
*
* @param {StreamHandler} dstSh
* @param {StreamHandler} srcSh
* @param {CircuitRelay} message
* @param {function} callback
* @returns {void}
*/
_negotiateStop (dstSh, srcSh, message, callback) {
const stopMsg = Object.assign({}, message, {
type: proto.Type.STOP // change the message type
})
dstSh.write(proto.encode(stopMsg),
(err) => {
if (err) {
this.utils.writeResponse(
srcSh,
proto.Status.HOP_CANT_OPEN_DST_STREAM)
log.err(err)
return callback(err)
}
// read response from STOP
dstSh.read((err, msg) => {
if (err) {
log.err(err)
return callback(err)
}
const message = proto.decode(msg)
if (message.code !== proto.Status.SUCCESS) {
return callback(new Error(`Unable to create circuit!`))
}
return callback(null, msg)
})
})
}
/**
* Attempt to make a circuit from A <-> R <-> B where R is this relay
*
* @param {StreamHandler} srcSh - the source stream handler
* @param {CircuitRelay} message - the message with the src and dst entries
* @param {Function} callback - callback to signal success or failure
* @returns {void}
* @private
*/
_circuit (srcSh, message, callback) {
let dstSh = null
waterfall([
(cb) => this._connectToStop(message.dstPeer, srcSh, cb),
(_dstConn, cb) => {
dstSh = new StreamHandler(_dstConn)
this._negotiateStop(dstSh, srcSh, message, cb)
}
], (err) => {
if (err) {
// close/end the source stream if there was an error
if (srcSh) {
srcSh.close()
}
if (dstSh) {
dstSh.close()
}
return callback(err)
}
const src = srcSh.rest()
const dst = dstSh.rest()
const srcIdStr = PeerId.createFromBytes(message.srcPeer.id).toB58String()
const dstIdStr = PeerId.createFromBytes(message.dstPeer.id).toB58String()
// circuit the src and dst streams
pull(
src,
dst,
src
)
log('circuit %s <-> %s established', srcIdStr, dstIdStr)
callback()
})
}
/**
* Dial the dest peer and create a circuit
*
* @param {Multiaddr} dstPeer
* @param {Function} callback
* @returns {void}
* @private
*/
_dialPeer (dstPeer, callback) {
const peerInfo = new PeerInfo(PeerId.createFromBytes(dstPeer.id))
dstPeer.addrs.forEach((a) => peerInfo.multiaddrs.add(a))
this.swarm.dial(peerInfo, multicodec.relay, once((err, conn) => {
if (err) {
log.err(err)
return callback(err)
}
callback(null, conn)
}))
}
}
module.exports = Hop

View File

@ -0,0 +1,56 @@
'use strict'
const setImmediate = require('async/setImmediate')
const EE = require('events').EventEmitter
const Connection = require('interface-connection').Connection
const utilsFactory = require('./utils')
const PeerInfo = require('peer-info')
const proto = require('../protocol').CircuitRelay
const series = require('async/series')
const debug = require('debug')
const log = debug('libp2p:circuit:stop')
log.err = debug('libp2p:circuit:error:stop')
class Stop extends EE {
constructor (swarm) {
super()
this.swarm = swarm
this.utils = utilsFactory(swarm)
}
/**
* Handle the incoming STOP message
*
* @param {{}} msg - the parsed protobuf message
* @param {StreamHandler} sh - the stream handler wrapped connection
* @param {Function} callback - callback
* @returns {undefined}
*/
handle (msg, sh, callback) {
callback = callback || (() => {})
series([
(cb) => this.utils.validateAddrs(msg, sh, proto.Type.STOP, cb),
(cb) => this.utils.writeResponse(sh, proto.Status.Success, cb)
], (err) => {
if (err) {
// we don't return the error here,
// since multistream select don't expect one
callback()
return log(err)
}
const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
const newConn = new Connection(sh.rest())
newConn.setPeerInfo(peerInfo)
setImmediate(() => this.emit('connection', newConn))
callback(newConn)
})
}
}
module.exports = Stop

View File

@ -0,0 +1,140 @@
'use strict'
const values = require('pull-stream/sources/values')
const collect = require('pull-stream/sinks/collect')
const empty = require('pull-stream/sources/empty')
const pull = require('pull-stream/pull')
const lp = require('pull-length-prefixed')
const handshake = require('pull-handshake')
const debug = require('debug')
const log = debug('libp2p:circuit:stream-handler')
log.err = debug('libp2p:circuit:error:stream-handler')
class StreamHandler {
/**
* Create a stream handler for connection
*
* @param {Connection} conn - connection to read/write
* @param {Function|undefined} cb - handshake callback called on error
* @param {Number} timeout - handshake timeout
* @param {Number} maxLength - max bytes length of message
*/
constructor (conn, cb, timeout, maxLength) {
this.conn = conn
this.stream = null
this.shake = null
this.timeout = cb || 1000 * 60
this.maxLength = maxLength || 4096
if (typeof cb === 'function') {
this.timeout = timeout || 1000 * 60
}
this.stream = handshake({ timeout: this.timeout }, cb)
this.shake = this.stream.handshake
pull(this.stream, conn, this.stream)
}
isValid () {
return this.conn && this.shake && this.stream
}
/**
* Read and decode message
*
* @param {Function} cb
* @returns {void|Function}
*/
read (cb) {
if (!this.isValid()) {
return cb(new Error(`handler is not in a valid state`))
}
lp.decodeFromReader(
this.shake,
{ maxLength: this.maxLength },
(err, msg) => {
if (err) {
log.err(err)
// this.shake.abort(err)
return cb(err)
}
return cb(null, msg)
})
}
/**
* Encode and write array of buffers
*
* @param {Buffer[]} msg
* @param {Function} [cb]
* @returns {Function}
*/
write (msg, cb) {
cb = cb || (() => {})
if (!this.isValid()) {
return cb(new Error(`handler is not in a valid state`))
}
pull(
values([msg]),
lp.encode(),
collect((err, encoded) => {
if (err) {
log.err(err)
this.shake.abort(err)
return cb(err)
}
encoded.forEach((e) => this.shake.write(e))
cb()
})
)
}
/**
* Get the raw Connection
*
* @returns {null|Connection|*}
*/
getRawConn () {
return this.conn
}
/**
* Return the handshake rest stream and invalidate handler
*
* @return {*|{source, sink}}
*/
rest () {
const rest = this.shake.rest()
this.conn = null
this.stream = null
this.shake = null
return rest
}
/**
* Close the stream
*
* @returns {undefined}
*/
close () {
if (!this.isValid()) {
return
}
// close stream
pull(
empty(),
this.rest()
)
}
}
module.exports = StreamHandler

View File

@ -0,0 +1,118 @@
'use strict'
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const proto = require('../protocol')
const { getPeerInfo } = require('../../get-peer-info')
module.exports = function (swarm) {
/**
* Get b58 string from multiaddr or peerinfo
*
* @param {Multiaddr|PeerInfo} peer
* @return {*}
*/
function getB58String (peer) {
let b58Id = null
if (multiaddr.isMultiaddr(peer)) {
const relayMa = multiaddr(peer)
b58Id = relayMa.getPeerId()
} else if (PeerInfo.isPeerInfo(peer)) {
b58Id = peer.id.toB58String()
}
return b58Id
}
/**
* Helper to make a peer info from a multiaddrs
*
* @param {Multiaddr|PeerInfo|PeerId} peer
* @return {PeerInfo}
* @private
*/
function peerInfoFromMa (peer) {
return getPeerInfo(peer, swarm._peerBook)
}
/**
* Checks if peer has an existing connection
*
* @param {String} peerId
* @param {Swarm} swarm
* @return {Boolean}
*/
function isPeerConnected (peerId) {
return swarm.muxedConns[peerId] || swarm.conns[peerId]
}
/**
* Write a response
*
* @param {StreamHandler} streamHandler
* @param {CircuitRelay.Status} status
* @param {Function} cb
* @returns {*}
*/
function writeResponse (streamHandler, status, cb) {
cb = cb || (() => {})
streamHandler.write(proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.STATUS,
code: status
}))
return cb()
}
/**
* Validate incomming HOP/STOP message
*
* @param {CircuitRelay} msg
* @param {StreamHandler} streamHandler
* @param {CircuitRelay.Type} type
* @returns {*}
* @param {Function} cb
*/
function validateAddrs (msg, streamHandler, type, cb) {
try {
msg.dstPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
} catch (err) {
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
? proto.CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
: proto.CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
return cb(err)
}
try {
msg.srcPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
} catch (err) {
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
? proto.CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
: proto.CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
return cb(err)
}
return cb(null)
}
function peerIdFromId (id) {
if (typeof id === 'string') {
return PeerId.createFromB58String(id)
}
return PeerId.createFromBytes(id)
}
return {
getB58String,
peerInfoFromMa,
isPeerConnected,
validateAddrs,
writeResponse,
peerIdFromId
}
}

3
src/circuit/index.js Normal file
View File

@ -0,0 +1,3 @@
'use strict'
module.exports = require('./circuit')

149
src/circuit/listener.js Normal file
View File

@ -0,0 +1,149 @@
'use strict'
const setImmediate = require('async/setImmediate')
const multicodec = require('./multicodec')
const EE = require('events').EventEmitter
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const Stop = require('./circuit/stop')
const Hop = require('./circuit/hop')
const proto = require('./protocol')
const utilsFactory = require('./circuit/utils')
const StreamHandler = require('./circuit/stream-handler')
const debug = require('debug')
const log = debug('libp2p:circuit:listener')
log.err = debug('libp2p:circuit:error:listener')
module.exports = (swarm, options, connHandler) => {
const listener = new EE()
const utils = utilsFactory(swarm)
listener.stopHandler = new Stop(swarm)
listener.stopHandler.on('connection', (conn) => listener.emit('connection', conn))
listener.hopHandler = new Hop(swarm, options.hop)
/**
* Add swarm handler and listen for incoming connections
*
* @param {Multiaddr} ma
* @param {Function} callback
* @return {void}
*/
listener.listen = (ma, callback) => {
callback = callback || (() => {})
swarm.handle(multicodec.relay, (_, conn) => {
const sh = new StreamHandler(conn)
sh.read((err, msg) => {
if (err) {
log.err(err)
return
}
let request = null
try {
request = proto.CircuitRelay.decode(msg)
} catch (err) {
return utils.writeResponse(
sh,
proto.CircuitRelay.Status.MALFORMED_MESSAGE)
}
switch (request.type) {
case proto.CircuitRelay.Type.CAN_HOP:
case proto.CircuitRelay.Type.HOP: {
return listener.hopHandler.handle(request, sh)
}
case proto.CircuitRelay.Type.STOP: {
return listener.stopHandler.handle(request, sh, connHandler)
}
default: {
utils.writeResponse(
sh,
proto.CircuitRelay.Status.INVALID_MSG_TYPE)
return sh.close()
}
}
})
})
setImmediate(() => listener.emit('listen'))
callback()
}
/**
* Remove swarm listener
*
* @param {Function} cb
* @return {void}
*/
listener.close = (cb) => {
swarm.unhandle(multicodec.relay)
setImmediate(() => listener.emit('close'))
cb()
}
/**
* Get fixed up multiaddrs
*
* NOTE: This method will grab the peers multiaddrs and expand them such that:
*
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
* `QmPeer` is this peers id
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
* addr, such when dialing over a relay with this address, it will create the circuit using
* the encapsulated transport address. This is useful when for example, a peer should only
* be dialed over TCP rather than any other transport
*
* @param {Function} callback
* @return {void}
*/
listener.getAddrs = (callback) => {
let addrs = swarm._peerInfo.multiaddrs.toArray()
// get all the explicit relay addrs excluding self
const p2pAddrs = addrs.filter((addr) => {
return mafmt.Circuit.matches(addr) &&
!addr.toString().includes(swarm._peerInfo.id.toB58String())
})
// use the explicit relays instead of any relay
if (p2pAddrs.length) {
addrs = p2pAddrs
}
const listenAddrs = []
addrs.forEach((addr) => {
const peerMa = `/p2p-circuit/ipfs/${swarm._peerInfo.id.toB58String()}`
if (addr.toString() === peerMa) {
listenAddrs.push(multiaddr(peerMa))
return
}
if (!mafmt.Circuit.matches(addr)) {
if (addr.getPeerId()) {
// by default we're reachable over any relay
listenAddrs.push(multiaddr(`/p2p-circuit`).encapsulate(addr))
} else {
const ma = `${addr}/ipfs/${swarm._peerInfo.id.toB58String()}`
listenAddrs.push(multiaddr(`/p2p-circuit`).encapsulate(ma))
}
} else {
listenAddrs.push(addr.encapsulate(`/ipfs/${swarm._peerInfo.id.toB58String()}`))
}
})
callback(null, listenAddrs)
}
return listener
}

View File

@ -0,0 +1,5 @@
'use strict'
module.exports = {
relay: '/libp2p/circuit/relay/0.1.0'
}

5
src/circuit/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "libp2p-circuit",
"description": "JavaScript implementation of circuit/switch relaying",
"main": "./index.js"
}

View File

@ -0,0 +1,44 @@
'use strict'
const protobuf = require('protons')
module.exports = protobuf(`
message CircuitRelay {
enum Status {
SUCCESS = 100;
HOP_SRC_ADDR_TOO_LONG = 220;
HOP_DST_ADDR_TOO_LONG = 221;
HOP_SRC_MULTIADDR_INVALID = 250;
HOP_DST_MULTIADDR_INVALID = 251;
HOP_NO_CONN_TO_DST = 260;
HOP_CANT_DIAL_DST = 261;
HOP_CANT_OPEN_DST_STREAM = 262;
HOP_CANT_SPEAK_RELAY = 270;
HOP_CANT_RELAY_TO_SELF = 280;
STOP_SRC_ADDR_TOO_LONG = 320;
STOP_DST_ADDR_TOO_LONG = 321;
STOP_SRC_MULTIADDR_INVALID = 350;
STOP_DST_MULTIADDR_INVALID = 351;
STOP_RELAY_REFUSED = 390;
MALFORMED_MESSAGE = 400;
}
enum Type { // RPC identifier, either HOP, STOP or STATUS
HOP = 1;
STOP = 2;
STATUS = 3;
CAN_HOP = 4;
}
message Peer {
required bytes id = 1; // peer id
repeated bytes addrs = 2; // peer's known addresses
}
optional Type type = 1; // Type of the message
optional Peer srcPeer = 2; // srcPeer and dstPeer are used when Type is HOP or STATUS
optional Peer dstPeer = 3;
optional Status code = 4; // Status code, used when Type is STATUS
}
`)

View File

@ -0,0 +1,99 @@
# libp2p-connection-manager
> JavaScript connection manager for libp2p
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-connection-manager.
## Table of Contents
- [Install](#install)
- [npm](#npm)
- [Use in Node.js, a browser with browserify, webpack or any other bundler](##use-in-nodejs-or-in-the-browser-with-browserify-webpack-or-any-other-bundler)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)
## API
A connection manager manages the peers you're connected to. The application provides one or more limits that will trigger the disconnection of peers. These limits can be any of the following:
* number of connected peers
* maximum bandwidth (sent / received or both)
* maximum event loop delay
The connection manager will disconnect peers (starting from the less important peers) until all the measures are withing the stated limits.
A connection manager first disconnects the peers with the least value. By default all peers have the same importance (1), but the application can define otherwise. Once a peer disconnects the connection manager discards the peer importance. (If necessary, the application should redefine the peer state if the peer is again connected).
### Create a ConnectionManager
```js
const libp2p = // …
const options = {…}
const connManager = new ConnManager(libp2p, options)
```
Options is an optional object with the following key-value pairs:
* **`maxPeers`**: number identifying the maximum number of peers the current peer is willing to be connected to before is starts disconnecting. Defaults to `Infinity`
* **`maxPeersPerProtocol`**: Object with key-value pairs, where a key is the protocol tag (case-insensitive) and the value is a number, representing the maximum number of peers allowing to connect for each protocol. Defaults to `{}`.
* **`minPeers`**: number identifying the number of peers below which this node will not activate preemptive disconnections. Defaults to `0`.
* **`maxData`**: sets the maximum data — in bytes per second - (sent and received) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxSentData`**: sets the maximum sent data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxReceivedData`**: sets the maximum received data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxEventLoopDelay`**: sets the maximum event loop delay (measured in miliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`pollInterval`**: sets the poll interval (in miliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
* **`movingAverageInterval`**: the interval used to calculate moving averages (in miliseconds). Defaults to `60000` (1 minute).
* **`defaultPeerValue`**: number between 0 and 1. Defaults to 1.
### `connManager.start()`
Starts the connection manager.
### `connManager.stop()`
Stops the connection manager.
### `connManager.setPeerValue(peerId, value)`
Sets the peer value for a given peer id. This is used to sort peers (in reverse order of value) to determine which to disconnect from first.
Arguments:
* peerId: B58-encoded string or [`peer-id`](https://github.com/libp2p/js-peer-id) instance.
* value: a number between 0 and 1, which represents a scale of how valuable this given peer id is to the application.
### `connManager.peers()`
Returns the peers this connection manager is connected to.
Returns an array of [PeerInfo](https://github.com/libp2p/js-peer-info).
### `connManager.emit('limit:exceeded', limitName, measured)`
Emitted when a limit is exceeded. Limit names can be:
* `maxPeers`
* `minPeers`
* `maxData`
* `maxSentData`
* `maxReceivedData`
* `maxEventLoopDelay`
* a protocol tag string (lower-cased)
### `connManager.emit('disconnect:preemptive', peerId)`
Emitted when a peer is about to be preemptively disconnected.
### `connManager.emit('disconnected', peerId)`
Emitted when a peer is disconnected (preemptively or note). If this peer reconnects, you will need to reset it's value, since the connection manager does not remember it.
### `connManager.emit('connected', peerId: String)`
Emitted when a peer connects. This is a good event to set the peer value, so you can get some control over who gets banned once a maximum number of peers is reached.

View File

@ -0,0 +1,218 @@
'use strict'
const EventEmitter = require('events')
const LatencyMonitor = require('latency-monitor').default
const debug = require('debug')('libp2p:connection-manager')
const defaultOptions = {
maxPeers: Infinity,
minPeers: 0,
maxData: Infinity,
maxSentData: Infinity,
maxReceivedData: Infinity,
maxEventLoopDelay: Infinity,
pollInterval: 2000,
movingAverageInterval: 60000,
defaultPeerValue: 1
}
class ConnectionManager extends EventEmitter {
constructor (libp2p, options) {
super()
this._libp2p = libp2p
this._options = Object.assign({}, defaultOptions, options)
this._options.maxPeersPerProtocol = fixMaxPeersPerProtocol(this._options.maxPeersPerProtocol)
debug('options: %j', this._options)
this._stats = libp2p.stats
if (options && !this._stats) {
throw new Error('No libp2p.stats')
}
this._peerValues = new Map()
this._peers = new Map()
this._peerProtocols = new Map()
this._peerCountPerProtocol = new Map()
this._onStatsUpdate = this._onStatsUpdate.bind(this)
this._onPeerConnect = this._onPeerConnect.bind(this)
this._onPeerDisconnect = this._onPeerDisconnect.bind(this)
if (this._libp2p.isStarted()) {
this._onceStarted()
} else {
this._libp2p.once('start', this._onceStarted.bind(this))
}
}
start () {
this._stats.on('update', this._onStatsUpdate)
this._libp2p.on('connection:start', this._onPeerConnect)
this._libp2p.on('connection:end', this._onPeerDisconnect)
// latency monitor
this._latencyMonitor = new LatencyMonitor({
dataEmitIntervalMs: this._options.pollInterval
})
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
this._latencyMonitor.on('data', this._onLatencyMeasure)
}
stop () {
this._stats.removeListener('update', this._onStatsUpdate)
this._libp2p.removeListener('connection:start', this._onPeerConnect)
this._libp2p.removeListener('connection:end', this._onPeerDisconnect)
this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
}
setPeerValue (peerId, value) {
if (value < 0 || value > 1) {
throw new Error('value should be a number between 0 and 1')
}
if (peerId.toB58String) {
peerId = peerId.toB58String()
}
this._peerValues.set(peerId, value)
}
_onceStarted () {
this._peerId = this._libp2p.peerInfo.id.toB58String()
}
_onStatsUpdate () {
const movingAvgs = this._stats.global.movingAverages
const received = movingAvgs.dataReceived[this._options.movingAverageInterval].movingAverage()
this._checkLimit('maxReceivedData', received)
const sent = movingAvgs.dataSent[this._options.movingAverageInterval].movingAverage()
this._checkLimit('maxSentData', sent)
const total = received + sent
this._checkLimit('maxData', total)
debug('stats update', total)
}
_onPeerConnect (peerInfo) {
const peerId = peerInfo.id.toB58String()
debug('%s: connected to %s', this._peerId, peerId)
this._peerValues.set(peerId, this._options.defaultPeerValue)
this._peers.set(peerId, peerInfo)
this.emit('connected', peerId)
this._checkLimit('maxPeers', this._peers.size)
protocolsFromPeerInfo(peerInfo).forEach((protocolTag) => {
const protocol = this._peerCountPerProtocol[protocolTag]
if (!protocol) {
this._peerCountPerProtocol[protocolTag] = 0
}
this._peerCountPerProtocol[protocolTag]++
let peerProtocols = this._peerProtocols[peerId]
if (!peerProtocols) {
peerProtocols = this._peerProtocols[peerId] = new Set()
}
peerProtocols.add(protocolTag)
this._checkProtocolMaxPeersLimit(protocolTag, this._peerCountPerProtocol[protocolTag])
})
}
_onPeerDisconnect (peerInfo) {
const peerId = peerInfo.id.toB58String()
debug('%s: disconnected from %s', this._peerId, peerId)
this._peerValues.delete(peerId)
this._peers.delete(peerId)
const peerProtocols = this._peerProtocols[peerId]
if (peerProtocols) {
Array.from(peerProtocols).forEach((protocolTag) => {
const peerCountForProtocol = this._peerCountPerProtocol[protocolTag]
if (peerCountForProtocol) {
this._peerCountPerProtocol[protocolTag]--
}
})
}
this.emit('disconnected', peerId)
}
_onLatencyMeasure (summary) {
this._checkLimit('maxEventLoopDelay', summary.avgMs)
}
_checkLimit (name, value) {
const limit = this._options[name]
debug('checking limit of %s. current value: %d of %d', name, value, limit)
if (value > limit) {
debug('%s: limit exceeded: %s, %d', this._peerId, name, value)
this.emit('limit:exceeded', name, value)
this._maybeDisconnectOne()
}
}
_checkProtocolMaxPeersLimit (protocolTag, value) {
debug('checking protocol limit. current value of %s is %d', protocolTag, value)
const limit = this._options.maxPeersPerProtocol[protocolTag]
if (value > limit) {
debug('%s: protocol max peers limit exceeded: %s, %d', this._peerId, protocolTag, value)
this.emit('limit:exceeded', protocolTag, value)
this._maybeDisconnectOne()
}
}
_maybeDisconnectOne () {
if (this._options.minPeers < this._peerValues.size) {
const peerValues = Array.from(this._peerValues).sort(byPeerValue)
debug('%s: sorted peer values: %j', this._peerId, peerValues)
const disconnectPeer = peerValues[0]
if (disconnectPeer) {
const peerId = disconnectPeer[0]
debug('%s: lowest value peer is %s', this._peerId, peerId)
debug('%s: forcing disconnection from %j', this._peerId, peerId)
this._disconnectPeer(peerId)
}
}
}
_disconnectPeer (peerId) {
debug('preemptively disconnecting peer', peerId)
this.emit('%s: disconnect:preemptive', this._peerId, peerId)
const peer = this._peers.get(peerId)
this._libp2p.hangUp(peer, (err) => {
if (err) {
this.emit('error', err)
}
})
}
}
module.exports = ConnectionManager
function byPeerValue (peerValueEntryA, peerValueEntryB) {
return peerValueEntryA[1] - peerValueEntryB[1]
}
function fixMaxPeersPerProtocol (maxPeersPerProtocol) {
if (!maxPeersPerProtocol) {
maxPeersPerProtocol = {}
}
Object.keys(maxPeersPerProtocol).forEach((transportTag) => {
const max = maxPeersPerProtocol[transportTag]
delete maxPeersPerProtocol[transportTag]
maxPeersPerProtocol[transportTag.toLowerCase()] = max
})
return maxPeersPerProtocol
}
function protocolsFromPeerInfo (peerInfo) {
const protocolTags = new Set()
peerInfo.multiaddrs.forEach((multiaddr) => {
multiaddr.protos().map(protocolToProtocolTag).forEach((protocolTag) => {
protocolTags.add(protocolTag)
})
})
return Array.from(protocolTags)
}
function protocolToProtocolTag (protocol) {
return protocol.name.toLowerCase()
}

View File

@ -0,0 +1,5 @@
{
"name": "libp2p-connection-manager",
"description": "JS Libp2p Connection Manager",
"main": "./index.js"
}

View File

@ -4,64 +4,73 @@ const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
return promisify(function (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance or Multiaddr String
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
if (typeof peer === 'string') {
try {
peer = multiaddr(peer)
} catch (err) {
return callback(
errCode(err, 'ERR_INVALID_MULTIADDR')
)
}
}
/**
* Converts the given `peer` to a `PeerInfo` instance.
* The `PeerBook` will be checked for the resulting peer, and
* the peer will be updated in the `PeerBook`.
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer
* @param {PeerBook} peerBook
* @returns {PeerInfo}
*/
function getPeerInfo (peer, peerBook) {
if (typeof peer === 'string') {
peer = multiaddr(peer)
}
const peerIdB58Str = peer.getPeerId()
if (!peerIdB58Str) {
return callback(
errCode(
new Error('peer multiaddr instance or string must include peerId'),
'ERR_INVALID_MULTIADDR'
)
)
}
try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)
// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
return node.peerRouting.findPeer(peer, callback)
}
} else {
return callback(
errCode(
new Error(`${p} is not a valid peer type`),
'ERR_INVALID_PEER_TYPE'
)
let addr
if (multiaddr.isMultiaddr(peer)) {
addr = peer
try {
peer = PeerId.createFromB58String(peer.getPeerId())
} catch (err) {
throw errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_MULTIADDR'
)
}
}
callback(null, p)
})
if (PeerId.isPeerId(peer)) {
peer = new PeerInfo(peer)
}
addr && peer.multiaddrs.add(addr)
return peerBook ? peerBook.put(peer) : peer
}
/**
* If `getPeerInfo` does not return a peer with multiaddrs,
* the `libp2p` PeerRouter will be used to attempt to find the peer.
*
* @async
* @param {PeerInfo|PeerId|Multiaddr|string} peer
* @param {Libp2p} libp2p
* @returns {Promise<PeerInfo>}
*/
function getPeerInfoRemote (peer, libp2p) {
let peerInfo
try {
peerInfo = getPeerInfo(peer, libp2p.peerBook)
} catch (err) {
throw errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_PEER_TYPE'
)
}
// If we don't have an address for the peer, attempt to find it
if (peerInfo.multiaddrs.size < 1) {
return libp2p.peerRouting.findPeer(peerInfo.id)
}
return Promise.resolve(peerInfo)
}
module.exports = {
getPeerInfoRemote,
getPeerInfo
}

37
src/identify/README.md Normal file
View File

@ -0,0 +1,37 @@
# js-libp2p-identify
> libp2p Identify Protocol
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-identify.
## Description
Identify is a STUN protocol, used by libp2p-swarm in order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
## How does it work
Best way to understand the current design is through this issue: https://github.com/libp2p/js-libp2p-swarm/issues/78
### This module uses `pull-streams`
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
You can learn more about pull-streams at:
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
- [pull-streams documentation](https://pull-stream.github.io/)
#### Converting `pull-streams` to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
```js
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.

87
src/identify/dialer.js Normal file
View File

@ -0,0 +1,87 @@
'use strict'
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const pull = require('pull-stream/pull')
const take = require('pull-stream/throughs/take')
const collect = require('pull-stream/sinks/collect')
const lp = require('pull-length-prefixed')
const msg = require('./message')
module.exports = (conn, expectedPeerInfo, callback) => {
if (typeof expectedPeerInfo === 'function') {
callback = expectedPeerInfo
expectedPeerInfo = null
// eslint-disable-next-line no-console
console.warn('WARNING: no expected peer info was given, identify will not be able to verify peer integrity')
}
pull(
conn,
lp.decode(),
take(1),
collect((err, data) => {
if (err) {
return callback(err)
}
// connection got closed graciously
if (data.length === 0) {
return callback(new Error('conn was closed, did not receive data'))
}
const input = msg.decode(data[0])
PeerId.createFromPubKey(input.publicKey, (err, id) => {
if (err) {
return callback(err)
}
const peerInfo = new PeerInfo(id)
if (expectedPeerInfo && expectedPeerInfo.id.toB58String() !== id.toB58String()) {
return callback(new Error('invalid peer'))
}
try {
input.listenAddrs
.map(multiaddr)
.forEach((ma) => peerInfo.multiaddrs.add(ma))
} catch (err) {
return callback(err)
}
let observedAddr
try {
observedAddr = getObservedAddrs(input)
} catch (err) {
return callback(err)
}
// Copy the protocols
peerInfo.protocols = new Set(input.protocols)
callback(null, peerInfo, observedAddr)
})
})
)
}
function getObservedAddrs (input) {
if (!hasObservedAddr(input)) {
return []
}
let addrs = input.observedAddr
if (!Array.isArray(addrs)) {
addrs = [addrs]
}
return addrs.map((oa) => multiaddr(oa))
}
function hasObservedAddr (input) {
return input.observedAddr && input.observedAddr.length > 0
}

7
src/identify/index.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
exports = module.exports
exports.multicodec = '/ipfs/id/1.0.0'
exports.listener = require('./listener')
exports.dialer = require('./dialer')
exports.message = require('./message')

35
src/identify/listener.js Normal file
View File

@ -0,0 +1,35 @@
'use strict'
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const lp = require('pull-length-prefixed')
const msg = require('./message')
module.exports = (conn, pInfoSelf) => {
// send what I see from the other + my Info
conn.getObservedAddrs((err, observedAddrs) => {
if (err) { return }
observedAddrs = observedAddrs[0]
let publicKey = Buffer.alloc(0)
if (pInfoSelf.id.pubKey) {
publicKey = pInfoSelf.id.pubKey.bytes
}
const msgSend = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: publicKey,
listenAddrs: pInfoSelf.multiaddrs.toArray().map((ma) => ma.buffer),
observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from(''),
protocols: Array.from(pInfoSelf.protocols)
})
pull(
values([msgSend]),
lp.encode(),
conn
)
})
}

30
src/identify/message.js Normal file
View File

@ -0,0 +1,30 @@
'use strict'
const protons = require('protons')
const schema = `
message Identify {
// protocolVersion determines compatibility between peers
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
// agentVersion is like a UserAgent string in browsers, or client version in bittorrent
// includes the client name and client.
optional string agentVersion = 6; // e.g. go-ipfs/0.1.0
// publicKey is this node's public key (which also gives its node.ID)
// - may not need to be sent, as secure channel implies it has been sent.
// - then again, if we change / disable secure channel, may still want it.
optional bytes publicKey = 1;
// listenAddrs are the multiaddrs the sender node listens for open connections on
repeated bytes listenAddrs = 2;
// oservedAddr is the multiaddr of the remote endpoint that the sender node perceives
// this is useful information to convey to the other side, as it helps the remote endpoint
// determine whether its connection to the local peer goes through NAT.
optional bytes observedAddr = 4;
repeated string protocols = 3;
}
`
module.exports = protons(schema).Identify

View File

@ -0,0 +1,5 @@
{
"name": "libp2p-identify",
"description": "libp2p Identify Protocol",
"main": "./index.js"
}

View File

@ -25,7 +25,7 @@ const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing')
const dht = require('./dht')
const pubsub = require('./pubsub')
const getPeerInfo = require('./get-peer-info')
const { getPeerInfoRemote } = require('./get-peer-info')
const validateConfig = require('./config').validate
const { codes } = require('./errors')
@ -133,8 +133,6 @@ class Libp2p extends EventEmitter {
this.contentRouting = contentRouting(this)
this.dht = dht(this)
this._getPeerInfo = getPeerInfo(this)
// Mount default protocols
Ping.mount(this._switch)
@ -267,11 +265,10 @@ class Libp2p extends EventEmitter {
protocol = undefined
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this._switch.dial(peerInfo, protocol, callback)
})
getPeerInfoRemote(peer, this)
.then(peerInfo => {
this._switch.dial(peerInfo, protocol, callback)
}, callback)
}
/**
@ -293,11 +290,10 @@ class Libp2p extends EventEmitter {
protocol = undefined
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this._switch.dialFSM(peerInfo, protocol, callback)
})
getPeerInfoRemote(peer, this)
.then(peerInfo => {
this._switch.dialFSM(peerInfo, protocol, callback)
}, callback)
}
/**
@ -308,11 +304,10 @@ class Libp2p extends EventEmitter {
* @returns {void}
*/
hangUp (peer, callback) {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this._switch.hangUp(peerInfo, callback)
})
getPeerInfoRemote(peer, this)
.then(peerInfo => {
this._switch.hangUp(peerInfo, callback)
}, callback)
}
/**
@ -327,11 +322,10 @@ class Libp2p extends EventEmitter {
return callback(notStarted('ping', this.state._state))
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
callback(null, new Ping(this._switch, peerInfo))
})
getPeerInfoRemote(peer, this)
.then(peerInfo => {
callback(null, new Ping(this._switch, peerInfo))
}, callback)
}
handle (protocol, handlerFunc, matchFunc) {

23
src/ping/README.md Normal file
View File

@ -0,0 +1,23 @@
libp2p-ping JavaScript Implementation
=====================================
> IPFS ping protocol JavaScript implementation
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-ping.
## Usage
```javascript
var Ping = require('libp2p-ping')
Ping.mount(swarm) // Enable this peer to echo Ping requests
var p = new Ping(swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object
p.on('ping', function (time) {
console.log(time + 'ms')
p.stop() // stop sending pings
})
p.start()
```

6
src/ping/constants.js Normal file
View File

@ -0,0 +1,6 @@
'use strict'
module.exports = {
PROTOCOL: '/ipfs/ping/1.0.0',
PING_LENGTH: 32
}

50
src/ping/handler.js Normal file
View File

@ -0,0 +1,50 @@
'use strict'
const pull = require('pull-stream/pull')
const handshake = require('pull-handshake')
const constants = require('./constants')
const PROTOCOL = constants.PROTOCOL
const PING_LENGTH = constants.PING_LENGTH
const debug = require('debug')
const log = debug('libp2p-ping')
log.error = debug('libp2p-ping:error')
function mount (swarm) {
swarm.handle(PROTOCOL, (protocol, conn) => {
const stream = handshake({ timeout: 0 })
const shake = stream.handshake
// receive and echo back
function next () {
shake.read(PING_LENGTH, (err, buf) => {
if (err === true) {
// stream closed
return
}
if (err) {
return log.error(err)
}
shake.write(buf)
return next()
})
}
pull(
conn,
stream,
conn
)
next()
})
}
function unmount (swarm) {
swarm.unhandle(PROTOCOL)
}
exports = module.exports
exports.mount = mount
exports.unmount = unmount

7
src/ping/index.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
const handler = require('./handler')
exports = module.exports = require('./ping')
exports.mount = handler.mount
exports.unmount = handler.unmount

5
src/ping/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "libp2p-ping",
"description": "libp2p Ping protocol implementation",
"main": "./index.js"
}

83
src/ping/ping.js Normal file
View File

@ -0,0 +1,83 @@
'use strict'
const EventEmitter = require('events').EventEmitter
const pull = require('pull-stream/pull')
const empty = require('pull-stream/sources/empty')
const handshake = require('pull-handshake')
const constants = require('./constants')
const util = require('./util')
const rnd = util.rnd
const debug = require('debug')
const log = debug('libp2p-ping')
log.error = debug('libp2p-ping:error')
const PROTOCOL = constants.PROTOCOL
const PING_LENGTH = constants.PING_LENGTH
class Ping extends EventEmitter {
constructor (swarm, peer) {
super()
this._stopped = false
this.peer = peer
this.swarm = swarm
}
start () {
log('dialing %s to %s', PROTOCOL, this.peer.id.toB58String())
this.swarm.dial(this.peer, PROTOCOL, (err, conn) => {
if (err) {
return this.emit('error', err)
}
const stream = handshake({ timeout: 0 })
this.shake = stream.handshake
pull(
stream,
conn,
stream
)
// write and wait to see ping back
const self = this
function next () {
const start = new Date()
const buf = rnd(PING_LENGTH)
self.shake.write(buf)
self.shake.read(PING_LENGTH, (err, bufBack) => {
const end = new Date()
if (err || !buf.equals(bufBack)) {
const err = new Error('Received wrong ping ack')
return self.emit('error', err)
}
self.emit('ping', end - start)
if (self._stopped) {
return
}
next()
})
}
next()
})
}
stop () {
if (this._stopped || !this.shake) {
return
}
this._stopped = true
pull(
empty(),
this.shake.rest()
)
}
}
module.exports = Ping

13
src/ping/util.js Normal file
View File

@ -0,0 +1,13 @@
'use strict'
const crypto = require('libp2p-crypto')
const constants = require('./constants')
exports = module.exports
exports.rnd = (length) => {
if (!length) {
length = constants.PING_LENGTH
}
return crypto.randomBytes(length)
}

67
src/pnet/README.md Normal file
View File

@ -0,0 +1,67 @@
js-libp2p-pnet
==================
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-pnet.
## Table of Contents
- [Usage](#usage)
- [Examples](#examples)
- [Private Shared Keys (PSK)](#private-shared-keys)
- [PSK Generation](#psk-generation)
- [Contribute](#contribute)
- [License](#license)
## Usage
```js
const Protector = require('libp2p-pnet')
const protector = new Protector(swarmKeyBuffer)
const privateConnection = protector.protect(myPublicConnection, (err) => { })
```
### Examples
[Private Networks with IPFS](./examples/pnet-ipfs)
### Private Shared Keys
Private Shared Keys are expected to be in the following format:
```
/key/swarm/psk/1.0.0/
/base16/
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
```
### PSK Generation
A utility method has been created to generate a key for your private network. You can
use one of the methods below to generate your key.
#### From libp2p-pnet
If you have libp2p-pnet locally, you can run the following from the projects root.
```sh
node ./key-generator.js > swarm.key
```
#### From a module using libp2p
If you have a module locally that depends on libp2p-pnet, you can run the following from
that project, assuming the node_modules are installed.
```sh
node -e "require('libp2p-pnet').generate(process.stdout)" > swarm.key
```
#### Programmatically
```js
const writeKey = require('libp2p-pnet').generate
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)
fs.writeFileSync('swarm.key', swarmKey)
```

98
src/pnet/crypto.js Normal file
View File

@ -0,0 +1,98 @@
'use strict'
const pull = require('pull-stream')
const debug = require('debug')
const Errors = require('./errors')
const xsalsa20 = require('xsalsa20')
const KEY_LENGTH = require('./key-generator').KEY_LENGTH
const log = debug('libp2p:pnet')
log.trace = debug('libp2p:pnet:trace')
log.err = debug('libp2p:pnet:err')
/**
* Creates a pull stream to encrypt messages in a private network
*
* @param {Buffer} nonce The nonce to use in encryption
* @param {Buffer} psk The private shared key to use in encryption
* @returns {PullStream} a through stream
*/
module.exports.createBoxStream = (nonce, psk) => {
const xor = xsalsa20(nonce, psk)
return pull(
ensureBuffer(),
pull.map((chunk) => {
return xor.update(chunk, chunk)
})
)
}
/**
* Creates a pull stream to decrypt messages in a private network
*
* @param {Object} remote Holds the nonce of the peer
* @param {Buffer} psk The private shared key to use in decryption
* @returns {PullStream} a through stream
*/
module.exports.createUnboxStream = (remote, psk) => {
let xor
return pull(
ensureBuffer(),
pull.map((chunk) => {
if (!xor) {
xor = xsalsa20(remote.nonce, psk)
log.trace('Decryption enabled')
}
return xor.update(chunk, chunk)
})
)
}
/**
* Decode the version 1 psk from the given Buffer
*
* @param {Buffer} pskBuffer
* @throws {INVALID_PSK}
* @returns {Object} The PSK metadata (tag, codecName, psk)
*/
module.exports.decodeV1PSK = (pskBuffer) => {
try {
// This should pull from multibase/multicodec to allow for
// more encoding flexibility. Ideally we'd consume the codecs
// from the buffer line by line to evaluate the next line
// programatically instead of making assumptions about the
// encodings of each line.
const metadata = pskBuffer.toString().split(/(?:\r\n|\r|\n)/g)
const pskTag = metadata.shift()
const codec = metadata.shift()
const psk = Buffer.from(metadata.shift(), 'hex')
if (psk.byteLength !== KEY_LENGTH) {
throw new Error(Errors.INVALID_PSK)
}
return {
tag: pskTag,
codecName: codec,
psk: psk
}
} catch (err) {
throw new Error(Errors.INVALID_PSK)
}
}
/**
* Returns a through pull-stream that ensures the passed chunks
* are buffers instead of strings
* @returns {PullStream} a through stream
*/
function ensureBuffer () {
return pull.map((chunk) => {
if (typeof chunk === 'string') {
return Buffer.from(chunk, 'utf-8')
}
return chunk
})
}

7
src/pnet/errors.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
module.exports.INVALID_PEER = 'Not a valid peer connection'
module.exports.INVALID_PSK = 'Your private shared key is invalid'
module.exports.NO_LOCAL_ID = 'No local private key provided'
module.exports.NO_HANDSHAKE_CONNECTION = 'No connection for the handshake provided'
module.exports.STREAM_ENDED = 'Stream ended prematurely'

70
src/pnet/index.js Normal file
View File

@ -0,0 +1,70 @@
'use strict'
const pull = require('pull-stream')
const Connection = require('interface-connection').Connection
const assert = require('assert')
const Errors = require('./errors')
const State = require('./state')
const decodeV1PSK = require('./crypto').decodeV1PSK
const debug = require('debug')
const log = debug('libp2p:pnet')
log.err = debug('libp2p:pnet:err')
/**
* Takes a Private Shared Key (psk) and provides a `protect` method
* for wrapping existing connections in a private encryption stream
*/
class Protector {
/**
* @param {Buffer} keyBuffer The private shared key buffer
* @constructor
*/
constructor (keyBuffer) {
const decodedPSK = decodeV1PSK(keyBuffer)
this.psk = decodedPSK.psk
this.tag = decodedPSK.tag
}
/**
* Takes a given Connection and creates a privaste encryption stream
* between its two peers from the PSK the Protector instance was
* created with.
*
* @param {Connection} connection The connection to protect
* @param {function(Error)} callback
* @returns {Connection} The protected connection
*/
protect (connection, callback) {
assert(connection, Errors.NO_HANDSHAKE_CONNECTION)
const protectedConnection = new Connection(undefined, connection)
const state = new State(this.psk)
log('protecting the connection')
// Run the connection through an encryptor
pull(
connection,
state.encrypt((err, encryptedOuterStream) => {
if (err) {
log.err('There was an error attempting to protect the connection', err)
return callback(err)
}
connection.getPeerInfo(() => {
protectedConnection.setInnerConn(new Connection(encryptedOuterStream, connection))
log('the connection has been successfully wrapped by the protector')
callback()
})
}),
connection
)
return protectedConnection
}
}
module.exports = Protector
module.exports.errors = Errors
module.exports.generate = require('./key-generator')

22
src/pnet/key-generator.js Normal file
View File

@ -0,0 +1,22 @@
'use strict'
const crypto = require('crypto')
const KEY_LENGTH = 32
/**
* Generates a PSK that can be used in a libp2p-pnet private network
* @param {Writer} writer An object containing a `write` method
* @returns {void}
*/
function generate (writer) {
const psk = crypto.randomBytes(KEY_LENGTH).toString('hex')
writer.write('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
}
module.exports = generate
module.exports.NONCE_LENGTH = 24
module.exports.KEY_LENGTH = KEY_LENGTH
if (require.main === module) {
generate(process.stdout)
}

5
src/pnet/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "libp2p-pnet",
"description": "Private Network protection implementation",
"main": "./index.js"
}

110
src/pnet/state.js Normal file
View File

@ -0,0 +1,110 @@
'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

View File

@ -1,24 +1,9 @@
libp2p-switch JavaScript implementation
======================================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-switch)](https://travis-ci.com/libp2p/js-libp2p-switch)
[![codecov](https://codecov.io/gh/libp2p/js-libp2p-switch/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-switch)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-switch.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-switch)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square)
> libp2p-switch is a dialer machine, it leverages the multiple libp2p transports, stream muxers, crypto channels and other connection upgrades to dial to peers in the libp2p network. It also supports Protocol Multiplexing through a multicodec and multistream-select handshake.
libp2p-switch is used by [libp2p](https://github.com/libp2p/js-libp2p) but it can be also used as a standalone module.
## Lead Maintainer
[Jacob Heun](https://github.com/jacobheun)
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-switch.
## Table of Contents
@ -436,12 +421,3 @@ const nodeStreamInstance = pullToStream(pullStreamInstance)
```
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
## Contribute
This module is actively under development. Please check out the issues and submit PRs!
## License
MIT © Protocol Labs

View File

@ -1,7 +1,7 @@
'use strict'
const DialQueueManager = require('./queueManager')
const getPeerInfo = require('../get-peer-info')
const { getPeerInfo } = require('../../get-peer-info')
const {
DENY_ATTEMPTS,
DENY_TTL,

View File

@ -1,49 +0,0 @@
'use strict'
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
/**
* Helper method to check the data type of peer and convert it to PeerInfo
*
* @param {PeerInfo|Multiaddr|PeerId} peer
* @param {PeerBook} peerBook
* @throws {InvalidPeerType}
* @returns {PeerInfo}
*/
function getPeerInfo (peer, peerBook) {
let peerInfo
// Already a PeerInfo instance,
// add to the peer book and return the latest value
if (PeerInfo.isPeerInfo(peer)) {
return peerBook.put(peer)
}
// Attempt to convert from Multiaddr instance (not string)
if (multiaddr.isMultiaddr(peer)) {
const peerIdB58Str = peer.getPeerId()
try {
peerInfo = peerBook.get(peerIdB58Str)
} catch (err) {
peerInfo = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
peerInfo.multiaddrs.add(peer)
return peerInfo
}
// Attempt to convert from PeerId
if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
return peerBook.get(peerIdB58Str)
} catch (err) {
throw new Error(`Couldnt get PeerInfo for ${peerIdB58Str}`)
}
}
throw new Error('peer type not recognized')
}
module.exports = getPeerInfo

View File

@ -8,7 +8,7 @@ const series = require('async/series')
const Circuit = require('libp2p-circuit')
const TransportManager = require('./transport')
const ConnectionManager = require('./connection/manager')
const getPeerInfo = require('./get-peer-info')
const { getPeerInfo } = require('../get-peer-info')
const getDialer = require('./dialer')
const connectionHandler = require('./connection/handler')
const ProtocolMuxer = require('./protocol-muxer')

View File

@ -1,71 +1,5 @@
{
"name": "libp2p-switch",
"version": "0.43.0",
"description": "libp2p switch implementation in JavaScript",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
"files": [
"src",
"dist"
],
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test -t node -t browser",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"release": "aegir release -t node -t browser",
"release-minor": "aegir release --type minor -t node -t browser",
"release-major": "aegir release --type major -t node -t browser",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider coveralls"
},
"repository": {
"type": "git",
"url": "https://github.com/libp2p/js-libp2p-switch.git"
},
"keywords": [
"IPFS"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-switch/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p-switch",
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
"Alan Shaw <alan@tableflip.io>",
"Arnaud <arnaud.valensi@gmail.com>",
"David Dias <daviddias.p@gmail.com>",
"David Dias <mail@daviddias.me>",
"Dmitriy Ryajov <dryajov@gmail.com>",
"Francisco Baio Dias <xicombd@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"Haad <haadcode@users.noreply.github.com>",
"Hugo Dias <mail@hugodias.me>",
"Hugo Dias <hugomrdias@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Jacob Heun <jake@andyet.net>",
"Kevin Kwok <antimatter15@gmail.com>",
"Kobi Gurkan <kobigurk@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Matteo Collina <matteo.collina@gmail.com>",
"Michael Fakhry <fakhrimichael@live.com>",
"Oli Evans <oli@tableflip.io>",
"Pau Ramon Revilla <masylum@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"Pius Nyakoojo <piusnyakoojo@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Sid Harder <sideharder@gmail.com>",
"Vasco Santos <vasco.santos@ua.pt>",
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"harrshasri <35241544+harrshasri@users.noreply.github.com>",
"kumavis <kumavis@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
]
"main": "./index.js"
}

303
test/circuit/dialer.spec.js Normal file
View File

@ -0,0 +1,303 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 5] */
'use strict'
const Dialer = require('../../src/circuit/circuit/dialer')
const nodes = require('./fixtures/nodes')
const Connection = require('interface-connection').Connection
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const asyncMap = require('pull-stream/throughs/async-map')
const pair = require('pull-pair/duplex')
const pb = require('pull-protocol-buffers')
const proto = require('../../src/circuit/protocol')
const utilsFactory = require('../../src/circuit/circuit/utils')
const sinon = require('sinon')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
describe(`dialer tests`, function () {
let dialer
beforeEach(() => {
dialer = sinon.createStubInstance(Dialer)
})
afterEach(() => {
sinon.restore()
})
describe(`.dial`, function () {
beforeEach(function () {
dialer.relayPeers = new Map()
dialer.relayPeers.set(nodes.node2.id, new Connection())
dialer.relayPeers.set(nodes.node3.id, new Connection())
dialer.dial.callThrough()
})
it(`fail on non circuit addr`, function () {
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
expect(() => dialer.dial(dstMa, (err) => {
err.to.match(/invalid circuit address/)
}))
})
it(`dial a peer`, function (done) {
const dstMa = multiaddr(`/p2p-circuit/ipfs/${nodes.node3.id}`)
dialer._dialPeer.callsFake(function (dstMa, relay, callback) {
return callback(null, dialer.relayPeers.get(nodes.node3.id))
})
dialer.dial(dstMa, (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.be.an.instanceOf(Connection)
done()
})
})
it(`dial a peer over the specified relay`, function (done) {
const dstMa = multiaddr(`/ipfs/${nodes.node3.id}/p2p-circuit/ipfs/${nodes.node4.id}`)
dialer._dialPeer.callsFake(function (dstMa, relay, callback) {
expect(relay.toString()).to.equal(`/ipfs/${nodes.node3.id}`)
return callback(null, new Connection())
})
dialer.dial(dstMa, (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.be.an.instanceOf(Connection)
done()
})
})
})
describe(`.canHop`, function () {
let fromConn = null
const peer = new PeerInfo(PeerId.createFromB58String('QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA'))
let p = null
beforeEach(function () {
p = pair()
fromConn = new Connection(p[0])
dialer.relayPeers = new Map()
dialer.relayConns = new Map()
dialer.utils = utilsFactory({})
dialer.canHop.callThrough()
dialer._dialRelayHelper.callThrough()
})
it(`should handle successful CAN_HOP`, (done) => {
dialer._dialRelay.callsFake((_, cb) => {
pull(
values([{
type: proto.CircuitRelay.type.HOP,
code: proto.CircuitRelay.Status.SUCCESS
}]),
pb.encode(proto.CircuitRelay),
p[1]
)
cb(null, fromConn)
})
dialer.canHop(peer, (err) => {
expect(err).to.not.exist()
expect(dialer.relayPeers.has(peer.id.toB58String())).to.be.ok()
done()
})
})
it(`should handle failed CAN_HOP`, function (done) {
dialer._dialRelay.callsFake((_, cb) => {
pull(
values([{
type: proto.CircuitRelay.type.HOP,
code: proto.CircuitRelay.Status.HOP_CANT_SPEAK_RELAY
}]),
pb.encode(proto.CircuitRelay),
p[1]
)
cb(null, fromConn)
})
dialer.canHop(peer, (err) => {
expect(err).to.exist()
expect(dialer.relayPeers.has(peer.id.toB58String())).not.to.be.ok()
done()
})
})
})
describe(`._dialPeer`, function () {
beforeEach(function () {
dialer.relayPeers = new Map()
dialer.relayPeers.set(nodes.node1.id, new Connection())
dialer.relayPeers.set(nodes.node2.id, new Connection())
dialer.relayPeers.set(nodes.node3.id, new Connection())
dialer._dialPeer.callThrough()
})
it(`should dial a peer over any relay`, function (done) {
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
dialer._negotiateRelay.callsFake(function (conn, dstMa, callback) {
if (conn === dialer.relayPeers.get(nodes.node3.id)) {
return callback(null, dialer.relayPeers.get(nodes.node3.id))
}
callback(new Error(`error`))
})
dialer._dialPeer(dstMa, (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.be.an.instanceOf(Connection)
expect(conn).to.deep.equal(dialer.relayPeers.get(nodes.node3.id))
done()
})
})
it(`should fail dialing a peer over any relay`, function (done) {
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
dialer._negotiateRelay.callsFake(function (conn, dstMa, callback) {
callback(new Error(`error`))
})
dialer._dialPeer(dstMa, (err, conn) => {
expect(conn).to.be.undefined()
expect(err).to.not.be.null()
expect(err).to.equal(`no relay peers were found or all relays failed to dial`)
done()
})
})
})
describe(`._negotiateRelay`, function () {
const dstMa = multiaddr(`/ipfs/${nodes.node4.id}`)
let conn = null
let peer = null
let p = null
before((done) => {
PeerId.createFromJSON(nodes.node4, (_, peerId) => {
PeerInfo.create(peerId, (err, peerInfo) => {
peer = peerInfo
peer.multiaddrs.add(`/p2p-circuit/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`)
done(err)
})
})
})
beforeEach(() => {
dialer.swarm = {
_peerInfo: peer
}
dialer.utils = utilsFactory({})
dialer.relayConns = new Map()
dialer._negotiateRelay.callThrough()
dialer._dialRelayHelper.callThrough()
peer = new PeerInfo(PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`))
p = pair()
conn = new Connection(p[1])
})
it(`should write the correct dst addr`, function (done) {
dialer._dialRelay.callsFake((_, cb) => {
pull(
p[0],
pb.decode(proto.CircuitRelay),
asyncMap((msg, cb) => {
expect(msg.dstPeer.addrs[0]).to.deep.equal(dstMa.buffer)
cb(null, {
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.SUCCESS
})
}),
pb.encode(proto.CircuitRelay),
p[0]
)
cb(null, conn)
})
dialer._negotiateRelay(peer, dstMa, done)
})
it(`should negotiate relay`, function (done) {
dialer._dialRelay.callsFake((_, cb) => {
pull(
p[0],
pb.decode(proto.CircuitRelay),
asyncMap((msg, cb) => {
expect(msg.dstPeer.addrs[0]).to.deep.equal(dstMa.buffer)
cb(null, {
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.SUCCESS
})
}),
pb.encode(proto.CircuitRelay),
p[0]
)
cb(null, conn)
})
dialer._negotiateRelay(peer, dstMa, (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.be.instanceOf(Connection)
done()
})
})
it(`should fail with an invalid peer id`, function (done) {
const dstMa = multiaddr('/ip4/127.0.0.1/tcp/4001')
dialer._dialRelay.callsFake((_, cb) => {
pull(
p[0],
pb.decode(proto.CircuitRelay),
asyncMap((msg, cb) => {
expect(msg.dstPeer.addrs[0]).to.deep.equal(dstMa.buffer)
cb(null, {
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.SUCCESS
})
}),
pb.encode(proto.CircuitRelay),
p[0]
)
cb(null, conn)
})
dialer._negotiateRelay(peer, dstMa, (err, conn) => {
expect(err).to.exist()
expect(conn).to.not.exist()
done()
})
})
it(`should handle failed relay negotiation`, function (done) {
dialer._dialRelay.callsFake((_, cb) => {
cb(null, conn)
pull(
values([{
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.MALFORMED_MESSAGE
}]),
pb.encode(proto.CircuitRelay),
p[0]
)
})
dialer._negotiateRelay(peer, dstMa, (err, conn) => {
expect(err).to.not.be.null()
expect(err).to.be.an.instanceOf(Error)
expect(err.message).to.be.equal(`Got 400 error code trying to dial over relay`)
done()
})
})
})
})

View File

@ -0,0 +1,25 @@
'use strict'
exports.node1 = {
id: 'QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE',
privKey: 'CAASpwkwggSjAgEAAoIBAQDJwzJPar4nylKY71Mm5q2BOED8uPf1ILvIi15VwVZWqter6flnlii/RKEcBypPbFqJHHa56MvybgQgrFmHKwDjnJvq4jyOZfR+o/D/99Ft1p2FAEBjImSXAgNpK4YsbyV5r0Q1+Avcj++aWWlLu6enUrL9WGzeUkf0U5L6XwXEPRUQdEojAIQi241P1hyqXX5gKAZVGqcPtKb6p1db3fcXodkS1G6JR90TopJHCqTCECp3SB9c6LlG7KXU92sIHJBlhOEEzGkEI1pM1SWnNnW5VLEypU7P56ifzzp4QxPNiJeC+cmE5SrgR3cXP44iKOuNVRJwBpCh5oNYqECzgqJ9AgMBAAECggEBAJpCdqXHrAmKJCqv2HiGqCODGhTfax1s4IYNIJwaTOPIjUrwgfKUGSVb2H4wcEX3RyVLsO6lMcFyIg/FFlJFK9HavE8SmFAbXZqxx6I9HE+JZjf5IEFrW1Mlg+wWDejNNe7adSF6O79wATaWo+32VNGWZilTQTGd4UvJ1jc9DZCh8zZeNhm4C6exXD45gMB0HI1t2ZNl47scsBEE4rV+s7F7y8Yk/tIsf0wSI/H8KSXS5I9aFxr3Z9c3HOfbVwhnIfNUDqcFTeU5BnhByYNLJ4v9xGj7puidcabVXkt2zLmm/LHbKVeGzec9LW5D+KkuB/pKaslsCXN6bVlu+SbVr9UCgYEA7MXfzZw36vDyfn4LPCN0wgzz11uh3cm31QzOPlWpA7hIsL/eInpvc8wa9yBRC1sRk41CedPHn913MR6EJi0Ne6/B1QOmRYBUjr60VPRNdTXCAiLykjXg6+TZ+AKnxlUGK1hjTo8krhpWq7iD/JchVlLoqDAXGFHvSxN0H3WEUm8CgYEA2iWC9w1v+YHfT2PXcLxYde9EuLVkIS4TM7Kb0N3wr/4+K4xWjVXuaJJLJoAbihNAZw0Y+2s1PswDUEpSG0jXeNXLs6XcQxYSEAu/pFdvHFeg2BfwVQoeEFlWyTJR29uti9/APaXMo8FSVAPPR5lKZLStJDM9hEfAPfUaHyic39MCgYAKQbwjNQw7Ejr+/cjQzxxkt5jskFyftfhPs2FP0/ghYB9OANHHnpQraQEWCYFZQ5WsVac2jdUM+NQL/a1t1e/Klt+HscPHKPsAwAQh1f9w/2YrH4ZwjQL0VRKYKs1HyzEcOZT7tzm4jQ2KHNEi5Q0dpzPK7WJivFHoZ6xVHIsh4wKBgAQq20mk9BKsLHvzyFXbA0WdgI6WyIbpvmwqaVegJcz26nEiiTTCA3/z64OcxunoXD6bvXJwJeBBPX73LIJg7dzdGLsh3AdcEJRF5S9ajEDaW7RFIM4/FzvwuPu2/mFY3QPjDmUfGb23H7+DIx6XCxjJatVaNT6lsEJ+wDUALZ8JAoGAO0YJSEziA7y0dXPK5azkJUMJ5yaN+zRDmoBnEggza34rQW0s16NnIR0EBzKGwbpNyePlProv4dQEaLF1kboKsSYvV2rW2ftLVdNqBHEUYFRC9ofPctCxwM1YU21TI2/k1squ+swApg2EHMev2+WKd+jpVPIbCIvJ3AjiAKZtiGQ=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDJwzJPar4nylKY71Mm5q2BOED8uPf1ILvIi15VwVZWqter6flnlii/RKEcBypPbFqJHHa56MvybgQgrFmHKwDjnJvq4jyOZfR+o/D/99Ft1p2FAEBjImSXAgNpK4YsbyV5r0Q1+Avcj++aWWlLu6enUrL9WGzeUkf0U5L6XwXEPRUQdEojAIQi241P1hyqXX5gKAZVGqcPtKb6p1db3fcXodkS1G6JR90TopJHCqTCECp3SB9c6LlG7KXU92sIHJBlhOEEzGkEI1pM1SWnNnW5VLEypU7P56ifzzp4QxPNiJeC+cmE5SrgR3cXP44iKOuNVRJwBpCh5oNYqECzgqJ9AgMBAAE='
}
exports.node2 = {
id: 'QmYJjAri5soV8RbeQcHaYYcTAYTET17QTvcoFMyKvRDTXe',
privKey: 'CAASpgkwggSiAgEAAoIBAQDt7YgUeBQsoN/lrgo690mB7yEh8G9iXhZiDecgZCLRRSl3v2cH9w4WjhoW9erfnVbdoTqkCK+se8uK01ySi/ubQQDPcrjacXTa6wAuRTbCG/0bUR9RxKtxZZBS1HaY7L923ulgGDTiVaRQ3JQqhzmQkaU0ikNcluSGaw0kmhXP6JmcL+wndKgW5VD9etcp2Qlk8uUFC/GAO90cOAuER3wnI3ocHGm9on9zyb97g4TDzIfjSaTW4Wanmx2yVbURQxmCba16X3LT9IMPqQaGOzq3+EewMLeCESbUm/uJaJLdqWrWRK4oNzxcMgmUkzav+s476HdA9CRo72am+g3Vdq+lAgMBAAECggEAcByKD6MZVoIjnlVo6qoVUA1+3kAuK/rLrz5/1wp4QYXGaW+eO+mVENm6v3D3UJESGnLbb+nL5Ymbunmn2EHvuBNkL1wOcJgfiPxM5ICmscaAeHu8N0plwpQp8m28yIheG8Qj0az2VmQmfhfCFVwMquuGHgC8hwdu/Uu6MLIObx1xjtaGbY9kk7nzAeXHeJ4RDeuNN0QrYuQVKwrIz1NtPNDR/cli298ZXJcm+HEhBCIHVIYpAq6BHSuiXVqPGEOYWYXo+yVhEtDJ8BmNqlN1Y1s6bnfu/tFkKUN6iQQ46vYnQEGTGR9lg7J/c6tqfRs9FcywWb9J1SX6HxPO8184zQKBgQD6vDYl20UT4ZtrzhFfMyV/1QUqFM/TdwNuiOsIewHBol9o7aOjrxrrbYVa1HOxETyBjmFsW+iIfOVl61SG2HcU4CG+O2s9WBo4JdRlOm4YQ8/83xO3YfbXzuTx8BMCyP/i1uPIZTKQFFAN0HiL96r4L60xHoWB7tQsbZiEbIO/2wKBgQDy7HnkgVeTld6o0+sT84FYRUotjDB00oUWiSeGtj0pFC4yIxhMhD8QjKiWoJyJItcoCsQ/EncuuwwRtuXi83793lJQR1DBYd+TSPg0M8J1pw97fUIPi/FU+jHtrsx7Vn/7Bk9voictsYVLAfbi68tYdsZpAaYOWYMY9NUfVuAmfwKBgCYZDwk1hgt9TkZVK2KRvPLthTldrC5veQAEoeHJ/vxTFbg105V9d9Op8odYnLOc8NqmrbrvRCfpAlo4JcHPhliPrdDf6m2Jw4IgjWNMO4pIU4QSyUYmBoHIGBWC6wCTVf47tKSwa7xkub0/nfF2km3foKtD/fk+NtMBXBlS+7ndAoGAJo6GIlCtN82X07AfJcGGjB4jUetoXYJ0gUkvruAKARUk5+xOFQcAg33v3EiNz+5pu/9JesFRjWc+2Sjwf/8p7t10ry1Ckg8Yz2XLj22PteDYQj91VsZdfaFgf1s5NXJbSdqMjSltkoEUqP0c1JOcaOQhRdVvJ+PpPPLPSPQfC70CgYBvJE1I06s7BEM1DOli3VyfNaJDI4k9W2dCJOU6Bh2MNmbdRjM3xnpOKH5SqRlCz/oI9pn4dxgbX6WPg331MD9CNYy2tt5KBQRrSuDj8p4jlzMIpX36hsyTTrzYU6WWSIPz6jXW8IexXKvXEmr8TVb78ZPiQfbG012cdUhAJniNgg==',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDt7YgUeBQsoN/lrgo690mB7yEh8G9iXhZiDecgZCLRRSl3v2cH9w4WjhoW9erfnVbdoTqkCK+se8uK01ySi/ubQQDPcrjacXTa6wAuRTbCG/0bUR9RxKtxZZBS1HaY7L923ulgGDTiVaRQ3JQqhzmQkaU0ikNcluSGaw0kmhXP6JmcL+wndKgW5VD9etcp2Qlk8uUFC/GAO90cOAuER3wnI3ocHGm9on9zyb97g4TDzIfjSaTW4Wanmx2yVbURQxmCba16X3LT9IMPqQaGOzq3+EewMLeCESbUm/uJaJLdqWrWRK4oNzxcMgmUkzav+s476HdA9CRo72am+g3Vdq+lAgMBAAE='
}
exports.node3 = {
id: 'QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA',
privKey: 'CAASpwkwggSjAgEAAoIBAQDdnGp0X7Pix5dIawfyuffVryRDRS5JXdyjayKUkgikJLYoiijB5TakrFKhx1SDKpmVLxxqAGz8m5iA2cHwetIQXTZvdYx7XXxv332En3ji8TiGRUiEFM8KQ5WCJ5G7yw8R2pv/pYdnMrPd04QbtSCn0cFVCiiA2Zkl5KnwBo/lf+sVI/TEeiwmVD9nxi13qWgBTmCysqH8Ppyu8fq+bQgqRZSlalVDswyIhgWlepPkD0uYakJJhhOxY+2RlbNhGY0qjRyMTYou2uR/hfd6j8uR++WdB0v3+DYWG2Kc3sWa4BLYb5r4trvQGO1Iagnwuk3AVoi7PldsaInekzWEVljDAgMBAAECggEAXx0jE49/xXWkmJBXePYYSL5C8hxfIV4HtJvm251R2CFpjTy/AXk/Wq4bSRQkUaeXA1CVAWntXP3rFmJfurb8McnP80agZNJa9ikV1jYbzEt71yUlWosT0XPwV0xkYBVnAmKxUafZ1ZENYcfGi53RxjVgpP8XIzZBZOIfjcVDPVw9NAOzQmq4i3DJEz5xZAkaeSM8mn5ZFl1JMBUOgyOHB7d4BWd3zuLyvnn0/08HlsaSUl0mZa3f2Lm2NlsjOiNfMCJTOIT+xDEP9THm5n2cqieSjvtpAZzV4kcoD0rB8OsyHQlFAEXzkgELDr5dVXji0rrIdVz8stYAKGfi996OAQKBgQDuviV1sc+ClJQA59vqbBiKxWqcuCKMzvmL4Yk1e/AkQeRt+JX9kALWzBx65fFmHTj4Lus8AIQoiruPxa0thtqh/m3SlucWnrdaW410xbz3KqQWS7bx+0sFWZIEi4N+PESrIYhtVbFuRiabYgliqdSU9shxtXXnvfhjl+9quZltiwKBgQDtoUCKqrZbm0bmzLvpnKdNodg1lUHaKGgEvWgza2N1t3b/GE07iha2KO3hBDta3bdfIEEOagY8o13217D0VIGsYNKpiEGLEeNIjfcXBEqAKiTfa/sXUfTprpWBZQ/7ZS+eZIYtQjq14EHa7ifAby1v3yDrMIuxphz5JfKdXFgYqQKBgHr47FikPwu2tkmFJCyqgzWvnEufOQSoc7eOc1tePIKggiX2/mM+M4gqWJ0hJeeAM+D6YeZlKa2sUBItMxeZN7JrWGw5mEx5cl4TfFhipgP2LdDiLRiVZL4bte+rYQ67wm8XdatDkYIIlkhBBi6Q5dPZDcQsQNAedPvvvb2OXi4jAoGBAKp06FpP+L2fle2LYSRDlhNvDCvrpDA8mdkEkRGJb/AKKdb09LnH5WDH3VNy+KzGrHoVJfWUAmNPAOFHeYzabaZcUeEAd5utui7afytIjbSABrEpwRTKWneiH2aROzSnMdBZ5ZHjlz/N3Q+RlHxKg/piwTdUPHCzasch/HX6vsr5AoGAGvhCNPKyCwpu8Gg5GQdx0yN6ZPar9wieD345cLanDZWKkLRQbo4SfkfoS+PDfOLzDbWFdPRnWQ0qhdPm3D/N1YD/nudHqbeDlx0dj/6lEHmmPKFFO2kiNFEhn8DycNGbvWyVBKksacuRXav21+LvW+TatUkRMhi8fgRoypnbJjg=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDdnGp0X7Pix5dIawfyuffVryRDRS5JXdyjayKUkgikJLYoiijB5TakrFKhx1SDKpmVLxxqAGz8m5iA2cHwetIQXTZvdYx7XXxv332En3ji8TiGRUiEFM8KQ5WCJ5G7yw8R2pv/pYdnMrPd04QbtSCn0cFVCiiA2Zkl5KnwBo/lf+sVI/TEeiwmVD9nxi13qWgBTmCysqH8Ppyu8fq+bQgqRZSlalVDswyIhgWlepPkD0uYakJJhhOxY+2RlbNhGY0qjRyMTYou2uR/hfd6j8uR++WdB0v3+DYWG2Kc3sWa4BLYb5r4trvQGO1Iagnwuk3AVoi7PldsaInekzWEVljDAgMBAAE='
}
exports.node4 = {
id: 'QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy',
privKey: 'CAASqAkwggSkAgEAAoIBAQC6pg6LYWbY+49SOYdYap6RPqKqZxg80IXeo3hiTUbiGtTruxVYZpnz3UbernL9J9mwlXJGRUQJUKmXmi1yePTQiyclpH0KyPefaWLbpxJQdCBI1TPZpDWo2hutWSPqhKBU1QyH2FLKQPWLdxuIX1cNFPtIlSl5gCxN6oiDIwh7++kxNM1G+d5XgJX6iHLlLoNv3Wn6XYX+VtYdyZRFk8gYyT2BrISbxmsrSjSOodwUUzF8TYTjsqW6ksL2x0mrRm2cMM9evULktqwU+I8l9ulASDbFWBXUToXaZSL9M+Oq5JvZO0WIjPeYVpAgWladtayhdxg5dBv8aTbDaM5DZvyRAgMBAAECggEAR65YbZz1k6Vg0HI5kXI4/YzxicHYJBrtHqjnJdGJxHILjZCmzPFydJ5phkG29ZRlXRS381bMn0s0Jn3WsFzVoHWgjitSvl6aAsXFapgKR42hjHcc15vh47wH3xYZ3gobTRkZG96vRO+XnX0bvM7orqR9MM3gRMI9wZqt3LcKnhpiqSlyEZ3Zehu7ZZ8B+XcUw42H6ZTXgmg5mCFEjS/1rVt+EsdZl7Ll7jHigahPA6qMjyRiZB6T20qQ0FFYfmaNuRuuC6cWUXf8DOgnEjMB/Mi/Feoip9bTqNBrVYn2XeDxdMv5pDznNKXpalsMkZwx5FpNOMKnIMdQFyAGtkeQ9QKBgQD3rjTiulitpbbQBzF8VXeymtMJAbR1TAqNv2yXoowhL3JZaWICM7nXHjjsJa3UzJygbi8bO0KWrw7tY0nUbPy5SmHtNYhmUsEjiTjqEnNRrYN68tEKr0HlgX+9rArsjOcwucl2svFSfk+rTYDHU5neZkDDhu1QmnZm/pQI92Lo4wKBgQDA6wpMd53fmX9DhWegs3xelRStcqBTw1ucWVRyPgY1hO1cJ0oReYIXKEw9CHNLW0RHvnVM26kRnqCl+dTcg7dhLuqrckuyQyY1KcRYG1ryJnz3euucaSF2UCsZCHvFNV7Vz8dszUMUVCogWmroVP6HE/BoazUCNh25s/dNwE+i+wKBgEfa1WL1luaBzgCaJaQhk4FQY2sYgIcLEYDACTwQn0C9aBpCdXmYEhEzpmX0JHM5DTOJ48atsYrPrK/3/yJOoB8NUk2kGzc8SOYLWGSoB6aphRx1N2o3IBH6ONoJAH5R/nxnWehCz7oUBP74lCS/v0MDPUS8bzrUJQeKUd4sDxjrAoGBAIRO7rJA+1qF+J1DWi4ByxNHJXZLfh/UhPj23w628SU1dGDWZVsUvZ7KOXdGW2RcRLj7q5E5uXtnEoCillViVJtnRPSun7Gzkfm2Gn3ezQH0WZKVkA+mnpd5JgW2JsS69L6pEPnS0OWZT4b+3AFZgXL8vs2ucR2CJeLdxYdilHuPAoGBAPLCzBkAboXZZtvEWqzqtVNqdMrjLHihFrpg4TXSsk8+ZQZCVN+sRyTGTvBX8+Jvx4at6ClaSgT3eJ/412fEH6CHvrFXjUE9W9y6X0axxaT63y1OXmFiB/hU3vjLWZKZWSDGNS7St02fYri4tWmGtJDjYG1maLRhMSzcoj4fP1xz',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC6pg6LYWbY+49SOYdYap6RPqKqZxg80IXeo3hiTUbiGtTruxVYZpnz3UbernL9J9mwlXJGRUQJUKmXmi1yePTQiyclpH0KyPefaWLbpxJQdCBI1TPZpDWo2hutWSPqhKBU1QyH2FLKQPWLdxuIX1cNFPtIlSl5gCxN6oiDIwh7++kxNM1G+d5XgJX6iHLlLoNv3Wn6XYX+VtYdyZRFk8gYyT2BrISbxmsrSjSOodwUUzF8TYTjsqW6ksL2x0mrRm2cMM9evULktqwU+I8l9ulASDbFWBXUToXaZSL9M+Oq5JvZO0WIjPeYVpAgWladtayhdxg5dBv8aTbDaM5DZvyRAgMBAAE='
}

View File

@ -0,0 +1,22 @@
'use strict'
const Libp2p = require('../../../src')
const secio = require('libp2p-secio')
class TestNode extends Libp2p {
constructor (peerInfo, transports, muxer, options) {
options = options || {}
const modules = {
transport: transports,
connection: {
muxer: [muxer],
crypto: options.isCrypto ? [secio] : null
},
discovery: []
}
super(modules, peerInfo, null, options)
}
}
module.exports = TestNode

View File

@ -0,0 +1,78 @@
'use strict'
const TestNode = require('./test-node')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const eachAsync = require('async/each')
exports.createNodes = function createNodes (configNodes, callback) {
const nodes = {}
eachAsync(Object.keys(configNodes), (key, cb1) => {
const config = configNodes[key]
const setup = (err, peer) => {
if (err) {
callback(err)
}
eachAsync(config.addrs, (addr, cb2) => {
peer.multiaddrs.add(addr)
cb2()
}, (err) => {
if (err) {
return callback(err)
}
nodes[key] = new TestNode(peer, config.transports, config.muxer, config.config)
cb1()
})
}
if (config.id) {
PeerId.createFromJSON(config.id, (err, peerId) => {
if (err) return callback(err)
PeerInfo.create(peerId, setup)
})
} else {
PeerInfo.create(setup)
}
}, (err) => {
if (err) {
return callback(err)
}
startNodes(nodes, (err) => {
if (err) {
callback(err)
}
callback(null, nodes)
})
})
}
function startNodes (nodes, callback) {
eachAsync(Object.keys(nodes),
(key, cb) => {
nodes[key].start(cb)
},
(err) => {
if (err) {
return callback(err)
}
callback(null)
})
}
exports.stopNodes = function stopNodes (nodes, callback) {
eachAsync(Object.keys(nodes),
(key, cb) => {
nodes[key].stop(cb)
},
(err) => {
if (err) {
return callback(err)
}
callback()
})
}

433
test/circuit/hop.spec.js Normal file
View File

@ -0,0 +1,433 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 5] */
'use strict'
const Hop = require('../../src/circuit/circuit/hop')
const nodes = require('./fixtures/nodes')
const Connection = require('interface-connection').Connection
const handshake = require('pull-handshake')
const waterfall = require('async/waterfall')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const collect = require('pull-stream/sinks/collect')
const lp = require('pull-length-prefixed')
const proto = require('../../src/circuit/protocol')
const StreamHandler = require('../../src/circuit/circuit/stream-handler')
const sinon = require('sinon')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
describe('relay', () => {
describe(`.handle`, () => {
let relay
let swarm
let fromConn
let stream
let shake
beforeEach((done) => {
stream = handshake({ timeout: 1000 * 60 })
shake = stream.handshake
fromConn = new Connection(stream)
const peerInfo = new PeerInfo(PeerId.createFromB58String('QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA'))
fromConn.setPeerInfo(peerInfo)
const peers = {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE:
new PeerInfo(PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`)),
QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA:
new PeerInfo(PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`)),
QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy:
new PeerInfo(PeerId.createFromB58String(`QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`))
}
Object.keys(peers).forEach((key) => { peers[key]._connectedMultiaddr = true }) // make it truthy
waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
peer.multiaddrs.add('/p2p-circuit/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE')
swarm = {
_peerInfo: peer,
conns: {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection(),
QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA: new Connection(),
QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy: new Connection()
},
_peerBook: {
get: (peer) => {
if (!peers[peer]) {
throw new Error()
}
return peers[peer]
}
}
}
cb()
}
], () => {
relay = new Hop(swarm, { enabled: true })
relay._circuit = sinon.stub()
relay._circuit.callsArgWith(2, null, new Connection())
done()
})
})
afterEach(() => {
relay._circuit.reset()
})
it(`should handle a valid circuit request`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).id,
addrs: [multiaddr(`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).buffer]
},
dstPeer: {
id: PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).id,
addrs: [multiaddr(`/ipfs/QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).buffer]
}
}
relay.on('circuit:success', () => {
expect(relay._circuit.calledWith(sinon.match.any, relayMsg)).to.be.ok()
done()
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
it(`should handle a request to passive circuit`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).id,
addrs: [multiaddr(`/ipfs/QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).buffer]
},
dstPeer: {
id: PeerId.createFromB58String(`QmYJjAri5soV8RbeQcHaYYcTAYTET17QTvcoFMyKvRDTXe`).id,
addrs: [multiaddr(`/ipfs/QmYJjAri5soV8RbeQcHaYYcTAYTET17QTvcoFMyKvRDTXe`).buffer]
}
}
relay.active = false
lp.decodeFromReader(
shake,
(err, msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(msg)
expect(response.code).to.equal(proto.CircuitRelay.Status.HOP_NO_CONN_TO_DST)
expect(response.type).to.equal(proto.CircuitRelay.Type.STATUS)
done()
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
it(`should handle a request to active circuit`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).id,
addrs: [multiaddr(`/ipfs/QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).buffer]
},
dstPeer: {
id: PeerId.createFromB58String(`QmYJjAri5soV8RbeQcHaYYcTAYTET17QTvcoFMyKvRDTXe`).id,
addrs: [multiaddr(`/ipfs/QmYJjAri5soV8RbeQcHaYYcTAYTET17QTvcoFMyKvRDTXe`).buffer]
}
}
relay.active = true
relay.on('circuit:success', () => {
expect(relay._circuit.calledWith(sinon.match.any, relayMsg)).to.be.ok()
done()
})
relay.on('circuit:error', (err) => {
done(err)
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
it(`not dial to self`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).id,
addrs: [multiaddr(`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).buffer]
},
dstPeer: {
id: PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).id,
addrs: [multiaddr(`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).buffer]
}
}
lp.decodeFromReader(
shake,
(err, msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(msg)
expect(response.code).to.equal(proto.CircuitRelay.Status.HOP_CANT_RELAY_TO_SELF)
expect(response.type).to.equal(proto.CircuitRelay.Type.STATUS)
done()
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
it(`fail on invalid src address`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: `sdfkjsdnfkjdsb`,
addrs: [`sdfkjsdnfkjdsb`]
},
dstPeer: {
id: PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).id,
addrs: [multiaddr(`/ipfs/QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).buffer]
}
}
lp.decodeFromReader(
shake,
(err, msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(msg)
expect(response.code).to.equal(proto.CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID)
expect(response.type).to.equal(proto.CircuitRelay.Type.STATUS)
done()
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
it(`fail on invalid dst address`, (done) => {
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).id,
addrs: [multiaddr(`/ipfs/QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`).buffer]
},
dstPeer: {
id: PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).id,
addrs: [`sdfkjsdnfkjdsb`]
}
}
lp.decodeFromReader(
shake,
(err, msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(msg)
expect(response.code).to.equal(proto.CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID)
expect(response.type).to.equal(proto.CircuitRelay.Type.STATUS)
done()
})
relay.handle(relayMsg, new StreamHandler(fromConn))
})
})
describe(`._circuit`, () => {
let relay
let swarm
let srcConn
let dstConn
let srcStream
let dstStream
let srcShake
let dstShake
before((done) => {
srcStream = handshake({ timeout: 1000 * 60 })
srcShake = srcStream.handshake
srcConn = new Connection(srcStream)
dstStream = handshake({ timeout: 1000 * 60 })
dstShake = dstStream.handshake
dstConn = new Connection(dstStream)
const peerInfo = new PeerInfo(PeerId.createFromB58String('QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA'))
srcConn.setPeerInfo(peerInfo)
const peers = {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE:
new PeerInfo(PeerId.createFromB58String(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`)),
QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA:
new PeerInfo(PeerId.createFromB58String(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`)),
QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy:
new PeerInfo(PeerId.createFromB58String(`QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`))
}
Object.keys(peers).forEach((key) => { peers[key]._connectedMultiaddr = true }) // make it truthy
waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
peer.multiaddrs.add('/p2p-circuit/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE')
swarm = {
_peerInfo: peer,
conns: {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection(),
QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA: new Connection(),
QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy: new Connection()
},
_peerBook: {
get: (peer) => {
if (!peers[peer]) {
throw new Error()
}
return peers[peer]
}
}
}
cb()
}
], () => {
relay = new Hop(swarm, { enabled: true })
relay._dialPeer = sinon.stub()
relay._dialPeer.callsArgWith(1, null, dstConn)
done()
})
})
after(() => relay._dialPeer.reset())
describe('should correctly dial destination node', () => {
const msg = {
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: Buffer.from(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`),
addrs: [Buffer.from(`dsfsdfsdf`)]
},
dstPeer: {
id: Buffer.from(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`),
addrs: [Buffer.from(`sdflksdfndsklfnlkdf`)]
}
}
before(() => {
relay._circuit(
new StreamHandler(srcConn),
msg,
(err) => {
expect(err).to.not.exist()
})
})
it('should respond with SUCCESS to source node', (done) => {
lp.decodeFromReader(
srcShake,
(err, msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(msg)
expect(response.type).to.equal(proto.CircuitRelay.Type.STATUS)
expect(response.code).to.equal(proto.CircuitRelay.Status.SUCCESS)
done()
})
})
it('should send STOP message to destination node', (done) => {
lp.decodeFromReader(
dstShake,
(err, _msg) => {
expect(err).to.not.exist()
const response = proto.CircuitRelay.decode(_msg)
expect(response.type).to.deep.equal(msg.type)
expect(response.srcPeer).to.deep.equal(msg.srcPeer)
expect(response.dstPeer).to.deep.equal(msg.dstPeer)
done()
})
})
it('should create circuit', (done) => {
pull(
values([proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.SUCCESS
})]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => dstShake.write(e))
pull(
values([Buffer.from('hello')]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => srcShake.write(e))
lp.decodeFromReader(
dstShake,
(err, _msg) => {
expect(err).to.not.exist()
expect(_msg.toString()).to.equal('hello')
done()
})
})
)
})
)
})
})
describe('should fail creating circuit', () => {
const msg = {
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: Buffer.from(`QmQWqGdndSpAkxfk8iyiJyz3XXGkrDNujvc8vEst3baubA`),
addrs: [Buffer.from(`dsfsdfsdf`)]
},
dstPeer: {
id: Buffer.from(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`),
addrs: [Buffer.from(`sdflksdfndsklfnlkdf`)]
}
}
it('should not create circuit', (done) => {
relay._circuit(
new StreamHandler(srcConn),
msg,
(err) => {
expect(err).to.exist()
expect(err).to.match(/Unable to create circuit!/)
done()
})
pull(
values([proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.STATUS,
code: proto.CircuitRelay.Status.STOP_RELAY_REFUSED
})]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => dstShake.write(e))
})
)
})
})
})
})

View File

@ -0,0 +1,292 @@
/* eslint-env mocha */
'use strict'
const Listener = require('../../src/circuit/listener')
const nodes = require('./fixtures/nodes')
const waterfall = require('async/waterfall')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const handshake = require('pull-handshake')
const Connection = require('interface-connection').Connection
const proto = require('../../src/circuit/protocol')
const lp = require('pull-length-prefixed')
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const collect = require('pull-stream/sinks/collect')
const multicodec = require('../../src/circuit/multicodec')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const sinon = require('sinon')
describe('listener', function () {
describe(`listen`, function () {
let swarm = null
let handlerSpy = null
let listener = null
let stream = null
let shake = null
let conn = null
beforeEach(function (done) {
stream = handshake({ timeout: 1000 * 60 })
shake = stream.handshake
conn = new Connection(stream)
conn.setPeerInfo(new PeerInfo(PeerId
.createFromB58String('QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE')))
waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
swarm = {
_peerInfo: peer,
handle: sinon.spy((proto, h) => {
handlerSpy = sinon.spy(h)
}),
conns: {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection()
}
}
listener = Listener(swarm, {}, () => {})
listener.listen()
cb()
}
], done)
})
afterEach(() => {
listener = null
})
it(`should handle HOP`, function (done) {
handlerSpy(multicodec.relay, conn)
const relayMsg = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`]
}
}
listener.hopHandler.handle = (message, conn) => {
expect(message.type).to.equal(proto.CircuitRelay.Type.HOP)
expect(message.srcPeer.id.toString()).to.equal(relayMsg.srcPeer.id)
expect(message.srcPeer.addrs[0].toString()).to.equal(relayMsg.srcPeer.addrs[0])
expect(message.dstPeer.id.toString()).to.equal(relayMsg.dstPeer.id)
expect(message.dstPeer.addrs[0].toString()).to.equal(relayMsg.dstPeer.addrs[0])
done()
}
pull(
values([proto.CircuitRelay.encode(relayMsg)]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => shake.write(e))
})
)
})
it(`should handle STOP`, function (done) {
handlerSpy(multicodec.relay, conn)
const relayMsg = {
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`]
}
}
listener.stopHandler.handle = (message, conn) => {
expect(message.type).to.equal(proto.CircuitRelay.Type.STOP)
expect(message.srcPeer.id.toString()).to.equal(relayMsg.srcPeer.id)
expect(message.srcPeer.addrs[0].toString()).to.equal(relayMsg.srcPeer.addrs[0])
expect(message.dstPeer.id.toString()).to.equal(relayMsg.dstPeer.id)
expect(message.dstPeer.addrs[0].toString()).to.equal(relayMsg.dstPeer.addrs[0])
done()
}
pull(
values([proto.CircuitRelay.encode(relayMsg)]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => shake.write(e))
})
)
})
it(`should emit 'connection'`, function (done) {
handlerSpy(multicodec.relay, conn)
const relayMsg = {
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`]
}
}
listener.stopHandler.handle = (message, sh) => {
const newConn = new Connection(sh.rest())
listener.stopHandler.emit('connection', newConn)
}
listener.on('connection', (conn) => {
expect(conn).to.be.instanceof(Connection)
done()
})
pull(
values([proto.CircuitRelay.encode(relayMsg)]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => shake.write(e))
})
)
})
it(`should handle CAN_HOP`, function (done) {
handlerSpy(multicodec.relay, conn)
const relayMsg = {
type: proto.CircuitRelay.Type.CAN_HOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`]
}
}
listener.hopHandler.handle = (message, conn) => {
expect(message.type).to.equal(proto.CircuitRelay.Type.CAN_HOP)
expect(message.srcPeer.id.toString()).to.equal(relayMsg.srcPeer.id)
expect(message.srcPeer.addrs[0].toString()).to.equal(relayMsg.srcPeer.addrs[0])
expect(message.dstPeer.id.toString()).to.equal(relayMsg.dstPeer.id)
expect(message.dstPeer.addrs[0].toString()).to.equal(relayMsg.dstPeer.addrs[0])
done()
}
pull(
values([proto.CircuitRelay.encode(relayMsg)]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => shake.write(e))
})
)
})
it(`should handle invalid message correctly`, function (done) {
handlerSpy(multicodec.relay, conn)
const relayMsg = {
type: 100000,
srcPeer: {
id: Buffer.from(`QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`),
addrs: [multiaddr(`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`).buffer]
},
dstPeer: {
id: Buffer.from(`QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`),
addrs: [multiaddr(`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`).buffer]
}
}
pull(
values([Buffer.from([relayMsg])]),
lp.encode(),
collect((err, encoded) => {
expect(err).to.not.exist()
encoded.forEach((e) => shake.write(e))
}),
lp.decodeFromReader(shake, { maxLength: this.maxLength }, (err, msg) => {
expect(err).to.not.exist()
expect(proto.CircuitRelay.decode(msg).type).to.equal(proto.CircuitRelay.Type.STATUS)
expect(proto.CircuitRelay.decode(msg).code).to.equal(proto.CircuitRelay.Status.MALFORMED_MESSAGE)
done()
})
)
})
})
describe(`getAddrs`, function () {
let swarm = null
let listener = null
let peerInfo = null
beforeEach(function (done) {
waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
swarm = {
_peerInfo: peer
}
peerInfo = peer
listener = Listener(swarm, {}, () => {})
cb()
}
], done)
})
afterEach(() => {
peerInfo = null
})
it(`should return correct addrs`, function () {
peerInfo.multiaddrs.add(`/ip4/0.0.0.0/tcp/4002`)
peerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/4003/ws`)
listener.getAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(addrs).to.deep.equal([
multiaddr(`/p2p-circuit/ip4/0.0.0.0/tcp/4002/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`),
multiaddr(`/p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`)])
})
})
it(`don't return default addrs in an explicit p2p-circuit addres`, function () {
peerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/4003/ws`)
peerInfo.multiaddrs.add(`/p2p-circuit/ip4/0.0.0.0/tcp/4002`)
listener.getAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(addrs[0]
.toString())
.to.equal(`/p2p-circuit/ip4/0.0.0.0/tcp/4002/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`)
})
})
})
})

View File

@ -0,0 +1,50 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const proto = require('../../src/circuit/protocol')
describe('protocol', function () {
let msgObject = null
let message = null
before(() => {
msgObject = {
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: Buffer.from('QmSource'),
addrs: [
multiaddr('/p2p-circuit/ipfs/QmSource').buffer,
multiaddr('/p2p-circuit/ip4/0.0.0.0/tcp/9000/ipfs/QmSource').buffer,
multiaddr('/ip4/0.0.0.0/tcp/9000/ipfs/QmSource').buffer
]
},
dstPeer: {
id: Buffer.from('QmDest'),
addrs: [
multiaddr('/p2p-circuit/ipfs/QmDest').buffer,
multiaddr('/p2p-circuit/ip4/1.1.1.1/tcp/9000/ipfs/QmDest').buffer,
multiaddr('/ip4/1.1.1.1/tcp/9000/ipfs/QmDest').buffer
]
}
}
const buff = proto.CircuitRelay.encode(msgObject)
message = proto.CircuitRelay.decode(buff)
})
it(`should source and dest`, () => {
expect(message.srcPeer).to.deep.equal(msgObject.srcPeer)
expect(message.dstPeer).to.deep.equal(msgObject.dstPeer)
})
it(`should encode message`, () => {
expect(message.message).to.deep.equal(msgObject.message)
})
})

85
test/circuit/stop.spec.js Normal file
View File

@ -0,0 +1,85 @@
/* eslint-env mocha */
'use strict'
const Stop = require('../../src/circuit/circuit/stop')
const nodes = require('./fixtures/nodes')
const Connection = require('interface-connection').Connection
const handshake = require('pull-handshake')
const waterfall = require('async/waterfall')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const StreamHandler = require('../../src/circuit/circuit/stream-handler')
const proto = require('../../src/circuit/protocol')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
describe('stop', function () {
describe(`handle relayed connections`, function () {
let stopHandler
let swarm
let conn
let stream
beforeEach(function (done) {
stream = handshake({ timeout: 1000 * 60 })
conn = new Connection(stream)
const peerId = PeerId.createFromB58String('QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE')
conn.setPeerInfo(new PeerInfo(peerId))
waterfall([
(cb) => PeerId.createFromJSON(nodes.node4, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(peer, cb) => {
peer.multiaddrs.add('/p2p-circuit/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE')
swarm = {
_peerInfo: peer,
conns: {
QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE: new Connection()
}
}
stopHandler = new Stop(swarm)
cb()
}
], done)
})
it(`handle request with a valid multiaddr`, function (done) {
stopHandler.handle({
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`/ipfs/QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`/ipfs/QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`]
}
}, new StreamHandler(conn), (conn) => { // multistream handler doesn't expect errors...
expect(conn).to.be.instanceOf(Connection)
done()
})
})
it(`handle request with invalid multiaddr`, function (done) {
stopHandler.handle({
type: proto.CircuitRelay.Type.STOP,
srcPeer: {
id: `QmSswe1dCFRepmhjAMR5VfHeokGLcvVggkuDJm7RMfJSrE`,
addrs: [`dsfsdfsdf`]
},
dstPeer: {
id: `QmQvM2mpqkjyXWbTHSUidUAWN26GgdMphTh9iGDdjgVXCy`,
addrs: [`sdflksdfndsklfnlkdf`]
}
}, new StreamHandler(conn), (conn) => {
expect(conn).to.not.exist()
done()
})
})
})
})

View File

@ -0,0 +1,19 @@
/* eslint-env mocha */
'use strict'
const Prepare = require('./utils/prepare')
describe('default', function () {
const prepare = Prepare(3, { pollInterval: 1000 })
before(prepare.before)
after(prepare.after)
it('does not kick out any peer', (done) => {
prepare.connManagers().forEach((connManager) => {
connManager.on('disconnected', () => {
throw new Error('should not have disconnected')
})
})
setTimeout(done, 1900)
})
})

View File

@ -0,0 +1,36 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxData', function () {
const prepare = Prepare(PEER_COUNT, {
maxData: 100,
minPeers: 1
})
before(prepare.create)
after(prepare.after)
it('kicks out peer after maxData reached', function (done) {
this.timeout(10000)
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,59 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxEventLoopDelay', function () {
const prepare = Prepare(PEER_COUNT, [{
pollInterval: 1000,
maxEventLoopDelay: 5,
minPeers: 1
}])
before(prepare.create)
after(prepare.after)
it('kicks out peer after maxEventLoopDelay reached', function (done) {
this.timeout(10000)
let stopped = false
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
stopped = true
done()
})
prepare.tryConnectAll((err) => {
expect(err).to.not.exist()
makeDelay()
})
function makeDelay () {
let sum = 0
for (let i = 0; i < 1000000; i++) {
sum += Math.random()
}
debug(sum)
if (!stopped) {
setTimeout(makeDelay, 0)
}
}
})
})
function debug (what) {
if (what === 0) {
// never true but the compiler doesn't know that
throw new Error('something went wrong')
}
}

View File

@ -0,0 +1,37 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxPeers', function () {
const prepare = Prepare(PEER_COUNT, [{
maxPeersPerProtocol: {
tcp: 1
}
}])
before(prepare.create)
after(prepare.after)
it('kicks out peers in excess', function (done) {
this.timeout(10000)
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,35 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxPeers', function () {
const prepare = Prepare(PEER_COUNT, [{
maxPeers: 1
}])
before(prepare.create)
after(prepare.after)
it('kicks out peers in excess', function (done) {
this.timeout(10000)
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err, eachNodeConnections) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,36 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxReceivedData', function () {
const prepare = Prepare(PEER_COUNT, {
maxReceivedData: 50,
minPeers: 1
})
before(prepare.create)
after(prepare.after)
it('kicks out peer after maxReceivedData reached', function (done) {
this.timeout(10000)
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err, eachNodeConnections) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,36 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('maxSentData', function () {
const prepare = Prepare(PEER_COUNT, [{
maxSentData: 50,
minPeers: 1
}])
before(prepare.create)
after(prepare.after)
it('kicks out peer after maxSentData reached', function (done) {
this.timeout(10000)
let disconnects = 0
const manager = prepare.connManagers()[0]
manager.on('disconnected', () => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err, eachNodeConnections) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,10 @@
'use strict'
require('./default')
require('./max-data')
require('./max-event-loop-delay')
require('./max-peer-per-protocol')
require('./max-peers')
require('./max-received-data')
require('./max-sent-data')
require('./set-peer-value')

View File

@ -0,0 +1,44 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Prepare = require('./utils/prepare')
const PEER_COUNT = 3
describe('setPeerValue', function () {
const prepare = Prepare(PEER_COUNT, [{
maxPeers: 1,
defaultPeerValue: 0
}])
before(prepare.create)
after(prepare.after)
it('kicks out lower valued peer first', function (done) {
let disconnects = 0
let firstConnectedPeer
const manager = prepare.connManagers()[0]
manager.once('connected', (peerId) => {
if (!firstConnectedPeer) {
firstConnectedPeer = peerId
manager.setPeerValue(peerId, 1)
}
})
manager.on('disconnected', (peerId) => {
disconnects++
expect(disconnects).to.be.most(PEER_COUNT - 2)
expect(peerId).to.not.be.equal(firstConnectedPeer)
manager.removeAllListeners('disconnected')
done()
})
prepare.tryConnectAll((err) => {
expect(err).to.not.exist()
})
})
})

View File

@ -0,0 +1,17 @@
'use strict'
const eachSeries = require('async/eachSeries')
module.exports = (nodes, callback) => {
eachSeries(
nodes,
(node, cb) => {
eachSeries(
nodes.filter(n => node !== n),
(otherNode, cb) => node.dial(otherNode.peerInfo, cb),
cb
)
},
callback
)
}

View File

@ -0,0 +1,50 @@
'use strict'
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const libp2p = require('../../../src')
const waterfall = require('async/waterfall')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const ConnManager = require('libp2p-connection-manager')
class Node extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [TCP],
streamMuxer: [Multiplex],
connEncryption: [SECIO]
}
super({
peerInfo,
modules,
config: {
peerDiscovery: {
autoDial: false
}
}
})
}
}
function createLibp2pNode (options, callback) {
let node
waterfall([
(cb) => PeerId.create({ bits: 1024 }, cb),
(id, cb) => PeerInfo.create(id, cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
node = new Node(peerInfo)
// Replace the connection manager so we use source code instead of dep code
node.connectionManager = new ConnManager(node, options)
node.start(cb)
}
], (err) => callback(err, node))
}
exports = module.exports = createLibp2pNode
exports.bundle = Node

View File

@ -0,0 +1,83 @@
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const series = require('async/series')
const each = require('async/each')
const createLibp2pNode = require('./create-libp2p-node')
const connectAll = require('./connect-all')
const tryConnectAll = require('./try-connect-all')
module.exports = (count, options) => {
let nodes
if (!Array.isArray(options)) {
const opts = options
options = []
for (let n = 0; n < count; n++) {
options[n] = opts
}
}
const create = (done) => {
const tasks = []
for (let i = 0; i < count; i++) {
tasks.push((cb) => createLibp2pNode(options.shift() || {}, cb))
}
series(tasks, (err, things) => {
if (!err) {
nodes = things
expect(things.length).to.equal(count)
}
done(err)
})
}
const connect = function (done) {
if (this && this.timeout) {
this.timeout(10000)
}
connectAll(nodes, done)
}
const tryConnectAllFn = function (done) {
if (this && this.timeout) {
this.timeout(10000)
}
tryConnectAll(nodes, done)
}
const before = (done) => {
if (this && this.timeout) {
this.timeout(10000)
}
series([create, connect], done)
}
const after = function (done) {
if (this && this.timeout) {
this.timeout(10000)
}
if (!nodes) { return done() }
each(nodes, (node, cb) => {
series([
(cb) => node.stop(cb)
], cb)
}, done)
}
return {
create,
connect,
tryConnectAll: tryConnectAllFn,
before,
after,
things: () => nodes,
connManagers: () => nodes.map((node) => node.connectionManager)
}
}

View File

@ -0,0 +1,27 @@
'use strict'
const mapSeries = require('async/mapSeries')
const eachSeries = require('async/eachSeries')
module.exports = (nodes, callback) => {
mapSeries(
nodes,
(node, cb) => {
const connectedTo = []
eachSeries(
nodes.filter(n => node !== n),
(otherNode, cb) => {
const otherNodePeerInfo = otherNode.peerInfo
node.dial(otherNodePeerInfo, (err) => {
if (!err) {
connectedTo.push(otherNodePeerInfo.id.toB58String())
}
cb()
})
},
(err) => cb(err, connectedTo)
)
},
callback
)
}

View File

@ -4,31 +4,124 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerBook = require('peer-book')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const MultiAddr = require('multiaddr')
const TestPeerInfos = require('./switch/test-data/ids.json').infos
const getPeerInfo = require('../src/get-peer-info')
const { getPeerInfo, getPeerInfoRemote } = require('../src/get-peer-info')
describe('getPeerInfo', () => {
it('should callback with error for invalid string multiaddr', (done) => {
getPeerInfo(null)('INVALID MULTIADDR', (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_INVALID_MULTIADDR')
done()
describe('Get Peer Info', () => {
describe('getPeerInfo', () => {
let peerBook
let peerInfoA
let multiaddrA
let peerIdA
before((done) => {
peerBook = new PeerBook()
PeerId.createFromJSON(TestPeerInfos[0].id, (err, id) => {
peerIdA = id
peerInfoA = new PeerInfo(peerIdA)
multiaddrA = MultiAddr('/ipfs/QmdWYwTywvXBeLKWthrVNjkq9SafEDn1PbAZdz4xZW7Jd9')
peerInfoA.multiaddrs.add(multiaddrA)
peerBook.put(peerInfoA)
done(err)
})
})
it('should be able get peer info from multiaddr', () => {
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
expect(peerBook.has(_peerInfo)).to.equal(true)
expect(peerInfoA).to.deep.equal(_peerInfo)
})
it('should return a new PeerInfo with a multiAddr not in the PeerBook', () => {
const wrongMultiAddr = MultiAddr('/ipfs/QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
const _peerInfo = getPeerInfo(wrongMultiAddr, peerBook)
expect(PeerInfo.isPeerInfo(_peerInfo)).to.equal(true)
})
it('should be able get peer info from peer id', () => {
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
expect(peerBook.has(_peerInfo)).to.equal(true)
expect(peerInfoA).to.deep.equal(_peerInfo)
})
it('should add a peerInfo to the book', (done) => {
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
const peerInfo = new PeerInfo(id)
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(false)
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(true)
done(err)
})
})
it('should return the most up to date version of the peer', (done) => {
const ma1 = MultiAddr('/ip4/0.0.0.0/tcp/8080')
const ma2 = MultiAddr('/ip6/::/tcp/8080')
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
const peerInfo = new PeerInfo(id)
peerInfo.multiaddrs.add(ma1)
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
const peerInfo2 = new PeerInfo(id)
peerInfo2.multiaddrs.add(ma2)
const returnedPeerInfo = getPeerInfo(peerInfo2, peerBook)
expect(returnedPeerInfo.multiaddrs.toArray()).to.contain.members([
ma1, ma2
])
done(err)
})
})
it('an invalid peer type should throw an error', () => {
let error
try {
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_MULTIADDR')
})
})
it('should callback with error for invalid non-peer multiaddr', (done) => {
getPeerInfo(null)('/ip4/8.8.8.8/tcp/1080', (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_INVALID_MULTIADDR')
done()
})
})
describe('getPeerInfoRemote', () => {
it('should callback with error for invalid string multiaddr', async () => {
let error
try {
await getPeerInfoRemote('INVALID MULTIADDR')
} catch (err) {
error = err
}
it('should callback with error for invalid non-peer multiaddr', (done) => {
getPeerInfo(null)(undefined, (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_INVALID_PEER_TYPE')
done()
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
it('should callback with error for invalid non-peer multiaddr', async () => {
let error
try {
await getPeerInfoRemote('/ip4/8.8.8.8/tcp/1080')
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
it('should callback with error for invalid non-peer multiaddr', async () => {
let error
try {
await getPeerInfoRemote(undefined)
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
})
})

View File

@ -0,0 +1,15 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const identify = require('libp2p-identify')
describe('basic', () => {
it('multicodec', () => {
expect(identify.multicodec).to.eql('/ipfs/id/1.0.0')
})
})

View File

@ -0,0 +1,192 @@
/* eslint-env mocha */
'use strict'
const pull = require('pull-stream/pull')
const values = require('pull-stream/sources/values')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const pair = require('pull-pair/duplex')
const PeerInfo = require('peer-info')
const lp = require('pull-length-prefixed')
const multiaddr = require('multiaddr')
const msg = require('libp2p-identify').message
const identify = require('libp2p-identify')
describe('identify.dialer', () => {
let original
before(function (done) {
this.timeout(20 * 1000)
PeerInfo.create((err, info) => {
if (err) {
return done(err)
}
original = info
done()
})
})
afterEach(() => {
original.multiaddrs.clear()
original.protocols.clear()
})
it('works', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
original.protocols.add('/echo/1.0.0')
original.protocols.add('/ping/1.0.0')
const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: Array.from(original.protocols)
})
pull(
values([input]),
lp.encode(),
p[0]
)
identify.dialer(p[1], (err, info, observedAddrs) => {
expect(err).to.not.exist()
expect(info.id.pubKey.bytes)
.to.eql(original.id.pubKey.bytes)
expect(info.multiaddrs.has(original.multiaddrs.toArray()[0]))
.to.eql(true)
expect(multiaddr('/ip4/127.0.0.1/tcp/5001').equals(observedAddrs[0]))
.to.eql(true)
expect(info.protocols).to.eql(original.protocols)
done()
})
})
it('should handle missing protocols', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: Array.from(original.protocols)
})
pull(
values([input]),
lp.encode(),
p[0]
)
identify.dialer(p[1], (err, info, observedAddrs) => {
expect(err).to.not.exist()
expect(info.id.pubKey.bytes)
.to.eql(original.id.pubKey.bytes)
expect(info.multiaddrs.has(original.multiaddrs.toArray()[0]))
.to.eql(true)
expect(multiaddr('/ip4/127.0.0.1/tcp/5001').equals(observedAddrs[0]))
.to.eql(true)
expect(Array.from(info.protocols)).to.eql([])
done()
})
})
it('does not crash with invalid listen addresses', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [Buffer.from('ffac010203')],
observedAddr: Buffer.from('ffac010203')
})
pull(
values([input]),
lp.encode(),
p[0]
)
identify.dialer(p[1], (err, info, observedAddrs) => {
expect(err).to.exist()
done()
})
})
it('does not crash with invalid observed address', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: Buffer.from('ffac010203')
})
pull(
values([input]),
lp.encode(),
p[0]
)
identify.dialer(p[1], (err, info, observedAddrs) => {
expect(err).to.exist()
done()
})
})
it('should return an error with mismatched peerInfo data', function (done) {
this.timeout(10e3)
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer
})
PeerInfo.create((err, info) => {
if (err) {
return done(err)
}
pull(
values([input]),
lp.encode(),
p[0]
)
identify.dialer(p[1], info, (err, peerInfo) => {
expect(err).to.exist()
expect(peerInfo).to.not.exist()
done()
})
})
})
})

View File

@ -0,0 +1,70 @@
/* eslint-env mocha */
'use strict'
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const pair = require('pull-pair/duplex')
const PeerInfo = require('peer-info')
const lp = require('pull-length-prefixed')
const multiaddr = require('multiaddr')
const msg = require('libp2p-identify').message
const identify = require('libp2p-identify')
describe('identify.listener', () => {
let info
beforeEach(function (done) {
this.timeout(20 * 1000)
PeerInfo.create((err, _info) => {
if (err) {
return done(err)
}
_info.protocols.add('/echo/1.0.0')
_info.protocols.add('/chat/1.0.0')
info = _info
done()
})
})
it('works', (done) => {
const p = pair()
info.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
pull(
p[1],
lp.decode(),
collect((err, result) => {
expect(err).to.not.exist()
const input = msg.decode(result[0])
expect(
input
).to.be.eql({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: info.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: ['/echo/1.0.0', '/chat/1.0.0']
})
done()
})
)
const conn = p[0]
conn.getObservedAddrs = (cb) => {
cb(null, [multiaddr('/ip4/127.0.0.1/tcp/5001')])
}
identify.listener(conn, info)
})
})

View File

@ -14,4 +14,5 @@ require('./multiaddr-trim.node')
require('./stats')
require('./dht.node')
require('./ping/node')
require('./switch/node')

3
test/ping/node.js Normal file
View File

@ -0,0 +1,3 @@
'use strict'
require('./test-ping.js')

118
test/ping/test-ping.js Normal file
View File

@ -0,0 +1,118 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const Swarm = require('libp2p-switch')
const TCP = require('libp2p-tcp')
const series = require('async/series')
const parallel = require('async/parallel')
const Ping = require('libp2p-ping')
describe('libp2p ping', () => {
let swarmA
let swarmB
let peerA
let peerB
before(function (done) {
this.timeout(20 * 1000)
series([
(cb) => PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
peerA = peerInfo
peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
cb()
}),
(cb) => PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
peerB = peerInfo
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
cb()
}),
(cb) => {
swarmA = new Swarm(peerA, new PeerBook())
swarmB = new Swarm(peerB, new PeerBook())
swarmA.transport.add('tcp', new TCP())
swarmB.transport.add('tcp', new TCP())
cb()
},
(cb) => swarmA.start(cb),
(cb) => swarmB.start(cb),
(cb) => {
Ping.mount(swarmA)
Ping.mount(swarmB)
cb()
}
], done)
})
after((done) => {
parallel([
(cb) => swarmA.stop(cb),
(cb) => swarmB.stop(cb)
], done)
})
it('ping once from peerA to peerB', (done) => {
const p = new Ping(swarmA, peerB)
p.on('error', (err) => {
expect(err).to.not.exist()
})
p.on('ping', (time) => {
expect(time).to.be.a('Number')
p.stop()
done()
})
p.start()
})
it('ping 5 times from peerB to peerA', (done) => {
const p = new Ping(swarmB, peerA)
p.on('error', (err) => {
expect(err).to.not.exist()
})
let counter = 0
p.on('ping', (time) => {
expect(time).to.be.a('Number')
if (++counter === 5) {
p.stop()
done()
}
})
p.start()
})
it('cannot ping itself', (done) => {
const p = new Ping(swarmA, peerA)
p.on('error', (err) => {
expect(err).to.exist()
done()
})
p.on('ping', () => {
expect.fail('should not be called')
})
p.start()
})
it('unmount PING protocol', () => {
Ping.unmount(swarmA)
Ping.unmount(swarmB)
})
})

View File

@ -0,0 +1,5 @@
{
"id": "QmeS1ou3mrjCFGoFtRx3MwrGDzqKD6xbuYJU1CKtMrtFFu",
"privKey": "CAASqAkwggSkAgEAAoIBAQChwzYwCNIyUkzEK3sILqq9ChAKZ9eU+ribY+B/xwAwDKPfvuqHq0hjauJBMcjiQyKAWz9xEBR3WupOM7h9M8oU+/e0xJUTt/CDOrtJ0PCgUXeBLkqsApbBoXW3yomHEDHxYttKzrtoTimiP1bhrxurcpVNC4CUYD+q8gw3sRZlsrqpeYYAfU04kS0BM75W/sUT90znnHvOxFXrEdMMdenEFhZOsDyEK9ENzwhkKgOGb18MBY4kN5DoW4bVd4ItfZnNwdkQtpP/X99tMWJxO4yqpngbywZGnkfirLeuRwt/xRGFVbLOigjBpTVpcbBqe1t2Flhuf/bfWYX4FbyElA5FAgMBAAECggEAJnDTcbrG6LpyD7QdeqZMYLwBb9eZfYfPUu37LaJGwyRd1Q/zf+YOP8HonoGMMWiuzD3i56Vgl7R9NbRIxUgHX9E43jZRDuyJNUZBt5r1c8OoWIR9rj63QLBz3wc8g2Iv3CMX5cEW/ASHFE1lAiCwvJ9wJ2zyU1BEEQWQLbPhlKzw7SLhr4fee45/7pnrKZMllt5vwC9pM6lrpIkICO5gUu0OWu5wfzzlTvfmCgfTb11VqKESEPbDBMUtpJibRqegE4xvipLklJ8VV8jz7NFs9bhgCpNM74Ngt5vGHcddeqtj//86UsClEw5YgWAdRe29ZjMApWvKIkginLjZEO8eiQKBgQDoDWii0rmlgBl1/8fENUSWxYvknGmWO7eWjVqMjDvA+waWUVDpTE+eHT1QAaPofM+nFz5PG+SpB55o4rXdxDesq+DqnaRAI9WtSHdgRtjgETyqoBAiahQ0zGWmSEYHGDB+xGctTMr8GxdhZxqZjjfyptp6oXXqZkmxgcogrx+WTwKBgQCydNDmCDpeH0kSvhAPxaNx5c9WkFEFSA0OCZOx57Y+Mt0MVamRILFrUrcMz095w8BQZkjlHjSHfsRgKa/b2eOd+3BhoMLZVtxRqBdpdqq1KTAcRRG4yA2KA39rttpVzaTV5SPfdDf3tsVlBtV784W63gVpN9gNfajyyrpeffiBKwKBgDnDrLprbl8uZigjhdznza0ie9JqxTXqo6bMhS/bcLx3QIqGr3eD0YXwjWSvI9gpyZ80gAQ9U0xoYxyE4vTTdXB8UL7Wgx6cTQKXuW+z8yTD5bArrBiFA4apItyjvRrjAJ9t0KlMJnNfYxCSE+MJrg+vTU+dhbbVw552SpScQ2atAoGBAKMu3rb6XyUiRpe05MsHVuYX1vi5Dt1dfVKQv1W3JJbLvAZDbsMeuh4BjRFRoMMflQPwBEg+zpn3+WpVtFG9dL5J5gHgF0zWeLDSnFX8BS2TdELlhccKaBcEC8hbdFtxqIFO/vaeN2902hv/m8e0b1zpGNmWDyKG/a7GYpV1a3/xAoGBAJtgGANDVk6qqcWGEVk56FH1ZksvgF3SPXWaXpzbZ5KLCcV5ooRyhowylKUZBBPowMeZ46tem2xwJbraB5kDg6WiSjBsXcbN95ivb8AuoRa6gDqAszjokQUSdpY7FTgMaL046AuihrKsQSly1jrQqbQu8JBgmnnBzus3s77inL/j",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQChwzYwCNIyUkzEK3sILqq9ChAKZ9eU+ribY+B/xwAwDKPfvuqHq0hjauJBMcjiQyKAWz9xEBR3WupOM7h9M8oU+/e0xJUTt/CDOrtJ0PCgUXeBLkqsApbBoXW3yomHEDHxYttKzrtoTimiP1bhrxurcpVNC4CUYD+q8gw3sRZlsrqpeYYAfU04kS0BM75W/sUT90znnHvOxFXrEdMMdenEFhZOsDyEK9ENzwhkKgOGb18MBY4kN5DoW4bVd4ItfZnNwdkQtpP/X99tMWJxO4yqpngbywZGnkfirLeuRwt/xRGFVbLOigjBpTVpcbBqe1t2Flhuf/bfWYX4FbyElA5FAgMBAAE="
}

View File

@ -0,0 +1,5 @@
{
"id": "QmYWHGZ9y1Bzx59bBzn85JsJxwmpBy5bpXDWDfwMfsHsxz",
"privKey": "CAASqQkwggSlAgEAAoIBAQDLVaPqWFA8WgK6ixuPvhTHeQfBblmEFLEmraLlIDSWbMUPva6aJ1V/hi2I5QLXNeeiig5sco+nF+RKhGnzQ9NpgHRVZ7Ze+LWq3Q4YxONdzFeNUjTvJrDSKgkubA5EKC/LI6pU33WZbjyKkomGo+Gzuqvlj4Rx1dLVXRIOjxUYcIQw3vpLQgwPpiz52eWCeoCpzn06DcsF6aNPjhlp9uJRZCRxZ4yeiwh/A0xxiQtnB4fdZuUPmia1r62+oaxrDl4hUwR7kzHYl0YGfXxAW9GT17KGtjES2yO4kAUgquelNh0hgBKZRvny9imwsObG7ntw5ZG7H62sP7UySIUJqoNRAgMBAAECggEBAKLVU25BCQg7wQGokwra2wMfPoG+IDuw4mkqFlBNKS/prSo86c2TgFmel2qQk2TLS1OUIZbha38RmAXA4qQohe5wKzmV06tcmwdY/YgCbF5aXSbUVYXLQ0Ea3r1pVUdps1SHnElZpnCXoi4Kyc2kAgSPkkdFVnhfFvc9EE/Ob8NgMkdFhlosE5WVNqm4BKQ+mqONddSz4JDbDOApPs/rRpgYm7pJKc3vkrYwniPjyQGYb5EoSbSWuu31RzIcn3Bhte3wKtfMMlpn8MMpPiYo2WJ2eVG6hlUOxhHgS93Y6czCfAgsDtD3C2JpteewuBjg8N0d6WRArKxny83J34q0qy0CgYEA6YSo5UDEq1TF8sbtSVYg6MKSX92NO5MQI/8fTjU4tEwxn/yxpGsnqUu0WGYIc2qVaZuxtcnk2CQxEilxQTbWSIxKuTt7qofEcpSjLLQ4f4chk4DpPsba+S8zSUdWdjthPHZT9IYzobylGBLfbPxyXXiYn1VuqAJfFy8iV9XqmdcCgYEA3ukROQQZCJcgsNTc5uFAKUeQvzv1iae3fGawgJmIJW3Bl8+4dSm1diqG3ZXP1WU31no2aX50PqOZjoIpbl1ggT76cnBDuu3pItR3dNJFQyMEpQOWOjO+NBWF7sRswCvlqbyjofWkzsdd0BioL7vWMjPftiusyyAFA55HRoeStxcCgYEA0tP7rKdSKKFr6inhl+GT6rGod7bOSSgYXXd7qx9v55AXCauaMqiv8TAxTdIo9RMYfHWd91OlMeNTDmOuJcO9qVhIKn5iw266VPyPac/4ZmL5VHQBobTlhC4yLomirTIlMvJeEBmNygtIPrjjUUGGe49itA/szPD/Ky5Z4lV27pcCgYAWU3mqIELxnVFk5K0LYtwuRkC1Jqg9FVNHXnGnL7l3JjsRnXh4I6lNII1JfEvIr86b6LmybzvtWi1zHI5Rw4B68XfcJmpiOpnzJxyf0r+lLci1Tlqpka0nQlCbzYim5r6l9YLeIeBT5Zv7z7xoq4OUm6V4dX9lCNv3tM6mvcVwGQKBgQC9hhjD64/VKXL8wYKZyTAOVO5xYCcqylrpI39qdzl+sS8oqmLUbXnKsGY4If9U61XdULld41BJCRlv6CsKreynm6ZN41j9YRuWWLu8STJcniV9Ef9uVl1M1zo8kfnCHMCym9LkTfJY+Ow/kYhqPukJJL6ve1CVmIuA4rnZlshjbg==",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLVaPqWFA8WgK6ixuPvhTHeQfBblmEFLEmraLlIDSWbMUPva6aJ1V/hi2I5QLXNeeiig5sco+nF+RKhGnzQ9NpgHRVZ7Ze+LWq3Q4YxONdzFeNUjTvJrDSKgkubA5EKC/LI6pU33WZbjyKkomGo+Gzuqvlj4Rx1dLVXRIOjxUYcIQw3vpLQgwPpiz52eWCeoCpzn06DcsF6aNPjhlp9uJRZCRxZ4yeiwh/A0xxiQtnB4fdZuUPmia1r62+oaxrDl4hUwR7kzHYl0YGfXxAW9GT17KGtjES2yO4kAUgquelNh0hgBKZRvny9imwsObG7ntw5ZG7H62sP7UySIUJqoNRAgMBAAE="
}

105
test/pnet/pnet.spec.js Normal file
View File

@ -0,0 +1,105 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const expect = chai.expect
const parallel = require('async/parallel')
const PeerId = require('peer-id')
const Connection = require('interface-connection').Connection
const pair = require('pull-pair/duplex')
const pull = require('pull-stream')
const Protector = require('libp2p-pnet')
const Errors = Protector.errors
const generate = Protector.generate
const swarmKeyBuffer = Buffer.alloc(95)
const wrongSwarmKeyBuffer = Buffer.alloc(95)
// Write new psk files to the buffers
generate(swarmKeyBuffer)
generate(wrongSwarmKeyBuffer)
describe('private network', () => {
before((done) => {
parallel([
(cb) => PeerId.createFromJSON(require('./fixtures/peer-a'), cb),
(cb) => PeerId.createFromJSON(require('./fixtures/peer-b'), cb)
], (err) => {
expect(err).to.not.exist()
done()
})
})
it('should accept a valid psk buffer', () => {
const protector = new Protector(swarmKeyBuffer)
expect(protector.tag).to.equal('/key/swarm/psk/1.0.0/')
expect(protector.psk.byteLength).to.equal(32)
})
it('should protect a simple connection', (done) => {
const p = pair()
const protector = new Protector(swarmKeyBuffer)
const aToB = protector.protect(new Connection(p[0]), (err) => {
expect(err).to.not.exist()
})
const bToA = protector.protect(new Connection(p[1]), (err) => {
expect(err).to.not.exist()
})
pull(
pull.values([Buffer.from('hello world'), Buffer.from('doo dah')]),
aToB
)
pull(
bToA,
pull.collect((err, chunks) => {
expect(err).to.not.exist()
expect(chunks).to.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
done()
})
)
})
it('should not connect to a peer with a different key', (done) => {
const p = pair()
const protector = new Protector(swarmKeyBuffer)
const protectorB = new Protector(wrongSwarmKeyBuffer)
const aToB = protector.protect(new Connection(p[0]), () => { })
const bToA = protectorB.protect(new Connection(p[1]), () => { })
pull(
pull.values([Buffer.from('hello world'), Buffer.from('doo dah')]),
aToB
)
pull(
bToA,
pull.collect((values) => {
expect(values).to.equal(null)
done()
})
)
})
describe('invalid psks', () => {
it('should not accept a bad psk', () => {
expect(() => {
return new Protector(Buffer.from('not-a-key'))
}).to.throw(Errors.INVALID_PSK)
})
it('should not accept a psk of incorrect length', () => {
expect(() => {
return new Protector(Buffer.from('/key/swarm/psk/1.0.0/\n/base16/\ndffb7e'))
}).to.throw(Errors.INVALID_PSK)
})
})
})

View File

@ -1,366 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const sinon = require('sinon')
const once = require('once')
const parallel = require('async/parallel')
const series = require('async/series')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const multiplex = require('pull-mplex')
const PeerBook = require('peer-book')
const getPorts = require('portfinder').getPorts
const utils = require('./utils')
const createInfos = utils.createInfos
const Swarm = require('libp2p-switch')
const switchOptions = {
denyTTL: 0 // nullifies denylisting
}
describe(`circuit`, function () {
describe('basic', () => {
let swarmA // TCP and WS
let swarmB // WS
let swarmC // no transports
let dialSpyA
before((done) => createInfos(3, (err, infos) => {
expect(err).to.not.exist()
const peerA = infos[0]
const peerB = infos[1]
const peerC = infos[2]
peerA.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002/ws')
swarmA = new Swarm(peerA, new PeerBook(), switchOptions)
swarmB = new Swarm(peerB, new PeerBook())
swarmC = new Swarm(peerC, new PeerBook())
swarmA.transport.add('tcp', new TCP())
swarmA.transport.add('ws', new WS())
swarmB.transport.add('ws', new WS())
dialSpyA = sinon.spy(swarmA.transport, 'dial')
done()
}))
after((done) => {
parallel([
(cb) => swarmA.stop(cb),
(cb) => swarmB.stop(cb)
], done)
})
it('circuit not enabled and all transports failed', (done) => {
swarmA.dial(swarmC._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Circuit not enabled and all transports failed to dial peer/)
expect(conn).to.not.exist()
done()
})
})
it('.enableCircuitRelay', () => {
swarmA.connection.enableCircuitRelay({ enabled: true })
expect(Object.keys(swarmA.transports).length).to.equal(3)
swarmB.connection.enableCircuitRelay({ enabled: true })
expect(Object.keys(swarmB.transports).length).to.equal(2)
})
it('listed on the transports map', () => {
expect(swarmA.transports.Circuit).to.exist()
expect(swarmB.transports.Circuit).to.exist()
})
it('add /p2p-circuit addrs on start', (done) => {
parallel([
(cb) => swarmA.start(cb),
(cb) => swarmB.start(cb)
], (err) => {
expect(err).to.not.exist()
expect(swarmA._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
.includes(`/p2p-circuit`)).length).to.be.at.least(3)
// ensure swarmA has had 0.0.0.0 replaced in the addresses
expect(swarmA._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
.includes(`/0.0.0.0`)).length).to.equal(0)
expect(swarmB._peerInfo.multiaddrs.toArray().filter((a) => a.toString()
.includes(`/p2p-circuit`)).length).to.be.at.least(2)
done()
})
})
it('dial circuit only once', (done) => {
swarmA._peerInfo.multiaddrs.clear()
swarmA._peerInfo.multiaddrs
.add(`/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star`)
swarmA.dial(swarmC._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/No available transports to dial peer/)
expect(conn).to.not.exist()
expect(dialSpyA.callCount).to.be.eql(1)
done()
})
})
it('dial circuit last', (done) => {
const peerC = swarmC._peerInfo
peerC.multiaddrs.clear()
peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`)
peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9998/ipfs/ABCD`)
peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9999/ws/ipfs/ABCD`)
swarmA.dial(peerC, (err, conn) => {
expect(err).to.exist()
expect(conn).to.not.exist()
expect(dialSpyA.lastCall.args[0]).to.be.eql('Circuit')
done()
})
})
it('should not try circuit if no transports enabled', (done) => {
swarmC.dial(swarmA._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(conn).to.not.exist()
expect(err).to.match(/No transports registered, dial not possible/)
done()
})
})
it('should not dial circuit if other transport succeed', (done) => {
swarmA.dial(swarmB._peerInfo, (err) => {
expect(err).not.to.exist()
expect(dialSpyA.lastCall.args[0]).to.not.be.eql('Circuit')
done()
})
})
})
describe('in a basic network', () => {
// Create 5 nodes
// Make node 1 act as a Bootstrap node and relay (speak tcp and ws)
// Make nodes 2 & 3 speak tcp only
// Make nodes 4 & 5 speak WS only
// Have all nodes dial node 1
// Each node should get the peers of node 1
// Attempt to dial to each peer
let bootstrapSwitch
let tcpSwitch1
let tcpSwitch2
let wsSwitch1
let wsSwitch2
let bootstrapPeer
let tcpPeer1
let tcpPeer2
let wsPeer1
let wsPeer2
before((done) => createInfos(5, (err, infos) => {
expect(err).to.not.exist()
getPorts(6, (err, ports) => {
expect(err).to.not.exist()
bootstrapPeer = infos[0]
tcpPeer1 = infos[1]
tcpPeer2 = infos[2]
wsPeer1 = infos[3]
wsPeer2 = infos[4]
// Setup the addresses of our nodes
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
tcpPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
tcpPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
wsPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
wsPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
// Setup the bootstrap node with the minimum needed for being a relay
bootstrapSwitch = new Swarm(bootstrapPeer, new PeerBook())
bootstrapSwitch.connection.addStreamMuxer(multiplex)
bootstrapSwitch.connection.reuse()
bootstrapSwitch.connection.enableCircuitRelay({
enabled: true,
// The relay needs to allow hopping
hop: {
enabled: true
}
})
// Setup the tcp1 node with the minimum needed for dialing via a relay
tcpSwitch1 = new Swarm(tcpPeer1, new PeerBook())
tcpSwitch1.connection.addStreamMuxer(multiplex)
tcpSwitch1.connection.reuse()
tcpSwitch1.connection.enableCircuitRelay({
enabled: true
})
// Setup tcp2 node to not be able to dial/listen over relay
tcpSwitch2 = new Swarm(tcpPeer2, new PeerBook())
tcpSwitch2.connection.reuse()
tcpSwitch2.connection.addStreamMuxer(multiplex)
// Setup the ws1 node with the minimum needed for dialing via a relay
wsSwitch1 = new Swarm(wsPeer1, new PeerBook())
wsSwitch1.connection.addStreamMuxer(multiplex)
wsSwitch1.connection.reuse()
wsSwitch1.connection.enableCircuitRelay({
enabled: true
})
// Setup the ws2 node with the minimum needed for dialing via a relay
wsSwitch2 = new Swarm(wsPeer2, new PeerBook())
wsSwitch2.connection.addStreamMuxer(multiplex)
wsSwitch2.connection.reuse()
wsSwitch2.connection.enableCircuitRelay({
enabled: true
})
bootstrapSwitch.transport.add('tcp', new TCP())
bootstrapSwitch.transport.add('ws', new WS())
tcpSwitch1.transport.add('tcp', new TCP())
tcpSwitch2.transport.add('tcp', new TCP())
wsSwitch1.transport.add('ws', new WS())
wsSwitch2.transport.add('ws', new WS())
series([
// start the nodes
(cb) => {
parallel([
(cb) => bootstrapSwitch.start(cb),
(cb) => tcpSwitch1.start(cb),
(cb) => tcpSwitch2.start(cb),
(cb) => wsSwitch1.start(cb),
(cb) => wsSwitch2.start(cb)
], cb)
},
// dial to the bootstrap node
(cb) => {
parallel([
(cb) => tcpSwitch1.dial(bootstrapPeer, cb),
(cb) => tcpSwitch2.dial(bootstrapPeer, cb),
(cb) => wsSwitch1.dial(bootstrapPeer, cb),
(cb) => wsSwitch2.dial(bootstrapPeer, cb)
], cb)
}
], (err) => {
if (err) return done(err)
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
return done()
}
done = once(done)
// Wait for everyone to connect, before we try relaying
bootstrapSwitch.on('peer-mux-established', () => {
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
done()
}
})
})
})
}))
before('wait so hop status can be negotiated', function (done) {
setTimeout(done, 1000)
})
after(function (done) {
parallel([
(cb) => bootstrapSwitch.stop(cb),
(cb) => tcpSwitch1.stop(cb),
(cb) => tcpSwitch2.stop(cb),
(cb) => wsSwitch1.stop(cb),
(cb) => wsSwitch2.stop(cb)
], done)
})
it('should be able to dial tcp -> tcp', (done) => {
tcpSwitch2.on('peer-mux-established', (peerInfo) => {
if (peerInfo.id.toB58String() === tcpPeer1.id.toB58String()) {
tcpSwitch2.removeAllListeners('peer-mux-established')
done()
}
})
tcpSwitch1.dial(tcpPeer2, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})
it('should be able to dial tcp -> ws over relay', (done) => {
wsSwitch1.on('peer-mux-established', (peerInfo) => {
if (peerInfo.id.toB58String() === tcpPeer1.id.toB58String()) {
wsSwitch1.removeAllListeners('peer-mux-established')
done()
}
})
tcpSwitch1.dial(wsPeer1, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})
it('should be able to dial ws -> ws', (done) => {
wsSwitch2.on('peer-mux-established', (peerInfo) => {
if (peerInfo.id.toB58String() === wsPeer1.id.toB58String()) {
wsSwitch2.removeAllListeners('peer-mux-established')
done()
}
})
wsSwitch1.dial(wsPeer2, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})
it('should be able to dial ws -> tcp over relay', (done) => {
tcpSwitch1.on('peer-mux-established', (peerInfo) => {
if (peerInfo.id.toB58String() === wsPeer2.id.toB58String()) {
tcpSwitch1.removeAllListeners('peer-mux-established')
expect(Object.keys(tcpSwitch1._peerBook.getAll())).to.include(wsPeer2.id.toB58String())
done()
}
})
wsSwitch2.dial(tcpPeer1, (err, connection) => {
expect(err).to.not.exist()
// We're not dialing a protocol, so we won't get a connection back
expect(connection).to.be.undefined()
})
})
it('shouldnt be able to dial to a non relay node', (done) => {
// tcpPeer2 doesnt have relay enabled
wsSwitch1.dial(tcpPeer2, (err, connection) => {
expect(err).to.exist()
expect(connection).to.not.exist()
done()
})
})
it('shouldnt be able to dial from a non relay node', (done) => {
// tcpSwitch2 doesnt have relay enabled
tcpSwitch2.dial(wsPeer1, (err, connection) => {
expect(err).to.exist()
expect(connection).to.not.exist()
done()
})
})
})
})

View File

@ -13,7 +13,6 @@ const secio = require('libp2p-secio')
const pull = require('pull-stream')
const multiplex = require('pull-mplex')
const spdy = require('libp2p-spdy')
const Connection = require('interface-connection').Connection
const Protector = require('libp2p-pnet')
const generatePSK = Protector.generate
@ -104,7 +103,7 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
done()
})
@ -118,7 +117,7 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
expect(() => connection.close(new Error('shutting down'))).to.not.throw()
done()
})
@ -171,11 +170,11 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
done()
})
@ -192,7 +191,7 @@ describe('ConnectionFSM', () => {
.callsArgWith(3, new Error('fail encrypt'))
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('close', () => {
@ -213,11 +212,11 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.upgrade()
})
connection.once('muxed', (conn) => {
@ -235,11 +234,11 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.upgrade()
})
connection.once('error:upgrade_failed', (err) => {
@ -261,11 +260,11 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.upgrade()
})
connection.once('muxed', (conn) => {
@ -273,7 +272,7 @@ describe('ConnectionFSM', () => {
connection.shake('/muxed-conn-test/1.0.0', (err, protocolConn) => {
expect(err).to.not.exist()
expect(protocolConn).to.be.an.instanceof(Connection)
expect(protocolConn).to.exist()
done()
})
})
@ -292,11 +291,11 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.upgrade()
})
connection.once('muxed', (conn) => {
@ -334,22 +333,22 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.upgrade()
})
connection.once('muxed', () => {
throw new Error('connection shouldnt be muxed')
})
connection.once('unmuxed', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.shake('/unmuxed-conn-test/1.0.0', (err, protocolConn) => {
expect(err).to.not.exist()
expect(protocolConn).to.be.an.instanceof(Connection)
expect(protocolConn).to.exist()
done()
})
})
@ -386,12 +385,12 @@ describe('ConnectionFSM', () => {
})
connection.once('private', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
done()
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.protect()
})
@ -420,7 +419,7 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection).mark()
expect(conn).to.exist().mark()
connection.protect()
})
@ -434,15 +433,15 @@ describe('ConnectionFSM', () => {
})
connection.once('connected', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.protect()
})
connection.once('private', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
connection.encrypt()
})
connection.once('encrypted', (conn) => {
expect(conn).to.be.an.instanceof(Connection)
expect(conn).to.exist()
done()
})

View File

@ -1,102 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const PeerBook = require('peer-book')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const MultiAddr = require('multiaddr')
const TestPeerInfos = require('./test-data/ids.json').infos
const getPeerInfo = require('libp2p-switch/get-peer-info')
describe('Get peer info', () => {
let peerBook
let peerInfoA
let multiaddrA
let peerIdA
before((done) => {
peerBook = new PeerBook()
PeerId.createFromJSON(TestPeerInfos[0].id, (err, id) => {
peerIdA = id
peerInfoA = new PeerInfo(peerIdA)
multiaddrA = MultiAddr('/ipfs/QmdWYwTywvXBeLKWthrVNjkq9SafEDn1PbAZdz4xZW7Jd9')
peerInfoA.multiaddrs.add(multiaddrA)
peerBook.put(peerInfoA)
done(err)
})
})
it('should be able get peer info from multiaddr', () => {
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
expect(peerBook.has(_peerInfo)).to.equal(true)
expect(peerInfoA).to.deep.equal(_peerInfo)
})
it('should return a new PeerInfo with a multiAddr not in the PeerBook', () => {
const wrongMultiAddr = MultiAddr('/ipfs/QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
const _peerInfo = getPeerInfo(wrongMultiAddr, peerBook)
expect(PeerInfo.isPeerInfo(_peerInfo)).to.equal(true)
expect(peerBook.has(_peerInfo)).to.equal(false)
})
it('should be able get peer info from peer id', () => {
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
expect(peerBook.has(_peerInfo)).to.equal(true)
expect(peerInfoA).to.deep.equal(_peerInfo)
})
it('should not be able to get the peer info for a wrong peer id', (done) => {
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
const func = () => {
getPeerInfo(id, peerBook)
}
expect(func).to.throw('Couldnt get PeerInfo')
done(err)
})
})
it('should add a peerInfo to the book', (done) => {
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
const peerInfo = new PeerInfo(id)
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(false)
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(true)
done(err)
})
})
it('should return the most up to date version of the peer', (done) => {
const ma1 = MultiAddr('/ip4/0.0.0.0/tcp/8080')
const ma2 = MultiAddr('/ip6/::/tcp/8080')
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
const peerInfo = new PeerInfo(id)
peerInfo.multiaddrs.add(ma1)
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
const peerInfo2 = new PeerInfo(id)
peerInfo2.multiaddrs.add(ma2)
const returnedPeerInfo = getPeerInfo(peerInfo2, peerBook)
expect(returnedPeerInfo.multiaddrs.toArray()).to.contain.members([
ma1, ma2
])
done(err)
})
})
it('an invalid peer type should throw an error', () => {
const func = () => {
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
}
expect(func).to.throw('peer type not recognized')
})
})

View File

@ -7,7 +7,7 @@ chai.use(require('chai-checkmark'))
const expect = chai.expect
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
const LimitDialer = require('libp2p-switch/limit-dialer')
const utils = require('./utils')
@ -37,14 +37,14 @@ describe('LimitDialer', () => {
// mock transport
const t1 = {
dial (addr, cb) {
setTimeout(() => cb(error), 1)
nextTick(cb, error)
return {}
}
}
dialer.dialMany(peers[0].id, t1, peers[0].multiaddrs.toArray(), (err, conn) => {
expect(err).to.exist()
expect(err).to.eql([error, error, error])
expect(err).to.include.members([error, error, error])
expect(conn).to.not.exist()
done()
})
@ -58,16 +58,16 @@ describe('LimitDialer', () => {
dial (addr, cb) {
const as = addr.toString()
if (as.match(/191/)) {
setImmediate(() => cb(new Error('fail')))
nextTick(cb, new Error('fail'))
return null
} else if (as.match(/192/)) {
setTimeout(cb, 2)
nextTick(cb)
return {
source: pull.values([1]),
sink: pull.drain()
}
} else if (as.match(/193/)) {
setTimeout(cb, 8)
nextTick(cb)
return {
source: pull.values([2]),
sink: pull.drain()

View File

@ -8,7 +8,6 @@ require('./stream-muxers.node')
require('./secio.node')
require('./swarm-no-muxing.node')
require('./swarm-muxing.node')
require('./circuit-relay.node')
require('./identify.node')
require('./limit-dialer.node')
require('./stats.node')