mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 05:11:33 +00:00
Compare commits
92 Commits
fix/remove
...
refactor/a
Author | SHA1 | Date | |
---|---|---|---|
7e1a9d677b | |||
1ba1ca7714 | |||
691e9b7f7b | |||
751a22741f | |||
461faca1ca | |||
31d1b2369a | |||
83409deaa6 | |||
c44e6e33ed | |||
999d086278 | |||
122a114d96 | |||
21362b5cbe | |||
0840739a00 | |||
f182f5bcd9 | |||
a1717dac6a | |||
dec8dc450f | |||
0d4b2bd23d | |||
fdb48c8df5 | |||
506af15b6b | |||
9f0f08f586 | |||
6ca19c5ef4 | |||
24c3ce6f8d | |||
56a1825639 | |||
92ed56657c | |||
9900beb243 | |||
4a871bbf8b | |||
a39889c4ea | |||
9bbe93c772 | |||
cc65a4b06f | |||
9c884a72b0 | |||
3ee1e22242 | |||
45f47023d2 | |||
af96dcc499 | |||
f540112835 | |||
3d30cb18cd | |||
64cbf90e02 | |||
7fc1900343 | |||
ad15d4ed09 | |||
600f761009 | |||
a2f31d99d2 | |||
edaa67dfd0 | |||
9b10e09cc0 | |||
8c6ad79630 | |||
1838a641d9 | |||
3cadeb39cb | |||
43440aa8a6 | |||
7c3371bf17 | |||
43b98e64b6 | |||
962081f448 | |||
754fbc2d0b | |||
0a8f9f3238 | |||
3b52236dee | |||
c7dcfe5e48 | |||
3b06283ad8 | |||
74bfe6bea5 | |||
53ce404260 | |||
43a3b85f1a | |||
e8bf12b68a | |||
7d1cb5423f | |||
c4be5f4aaf | |||
a37c5c0144 | |||
24c603741f | |||
ea62c52701 | |||
f9fe44f6b7 | |||
d5405dbb08 | |||
571fd3b7d1 | |||
cba2c6d8b2 | |||
f8540fa3ed | |||
0cacfe29a5 | |||
c4bc00be9c | |||
f3eb1f1201 | |||
dbb9e57311 | |||
11ed6bd14c | |||
fc22c36ba7 | |||
b518391a47 | |||
997ee166b0 | |||
acbbc0f84e | |||
995640ee2f | |||
b316cdd19b | |||
1ea945ad24 | |||
c37703dc17 | |||
86b275a0d3 | |||
3c79d33db9 | |||
34d57f8989 | |||
ced2dbf318 | |||
44d47087d1 | |||
797d8f0cf1 | |||
f3e276eb79 | |||
138bb0bbae | |||
af364b070b | |||
10c8553c58 | |||
a7d5e67e06 | |||
4f8043d259 |
129
.aegir.js
129
.aegir.js
@ -1,130 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
|
||||
const sigServer = require('libp2p-webrtc-star/src/sig-server')
|
||||
const promisify = require('promisify-es6')
|
||||
const mplex = require('pull-mplex')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const PeerBook = require('peer-book')
|
||||
const Libp2p = require('./src')
|
||||
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
|
||||
const Peers = require('./test/fixtures/peers')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const path = require('path')
|
||||
const Switch = require('./src/switch')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
|
||||
const Node = require('./test/utils/bundle-nodejs.js')
|
||||
const {
|
||||
getPeerRelay,
|
||||
WRTC_RENDEZVOUS_MULTIADDR,
|
||||
WS_RENDEZVOUS_MULTIADDR
|
||||
} = require('./test/utils/constants')
|
||||
|
||||
let wrtcRendezvous
|
||||
let wsRendezvous
|
||||
let node
|
||||
let peerInfo
|
||||
let switchA
|
||||
let switchB
|
||||
|
||||
function echo (protocol, conn) { pull(conn, conn) }
|
||||
function idJSON (id) {
|
||||
const p = path.join(__dirname, `./test/switch/test-data/id-${id}.json`)
|
||||
return require(p)
|
||||
}
|
||||
|
||||
function createSwitchA () {
|
||||
return new Promise((resolve, reject) => {
|
||||
PeerId.createFromJSON(idJSON(1), (err, id) => {
|
||||
if (err) { return reject(err) }
|
||||
|
||||
const peerA = new PeerInfo(id)
|
||||
const maA = '/ip4/127.0.0.1/tcp/15337/ws'
|
||||
|
||||
peerA.multiaddrs.add(maA)
|
||||
const sw = new Switch(peerA, new PeerBook())
|
||||
|
||||
sw.transport.add('ws', new WebSockets())
|
||||
sw.start((err) => {
|
||||
if (err) { return reject(err) }
|
||||
resolve(sw)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function createSwitchB () {
|
||||
return new Promise((resolve, reject) => {
|
||||
PeerId.createFromJSON(idJSON(2), (err, id) => {
|
||||
if (err) { return reject(err) }
|
||||
|
||||
const peerB = new PeerInfo(id)
|
||||
const maB = '/ip4/127.0.0.1/tcp/15347/ws'
|
||||
|
||||
peerB.multiaddrs.add(maB)
|
||||
const sw = new Switch(peerB, new PeerBook())
|
||||
|
||||
sw.transport.add('ws', new WebSockets())
|
||||
sw.connection.addStreamMuxer(mplex)
|
||||
sw.connection.addStreamMuxer(spdy)
|
||||
sw.connection.reuse()
|
||||
sw.handle('/echo/1.0.0', echo)
|
||||
sw.start((err) => {
|
||||
if (err) { return reject(err) }
|
||||
resolve(sw)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
const Muxer = require('libp2p-mplex')
|
||||
const Crypto = require('libp2p-secio')
|
||||
const pipe = require('it-pipe')
|
||||
let libp2p
|
||||
|
||||
const before = async () => {
|
||||
[
|
||||
wrtcRendezvous,
|
||||
wsRendezvous,
|
||||
peerInfo,
|
||||
switchA,
|
||||
switchB
|
||||
] = await Promise.all([
|
||||
sigServer.start({
|
||||
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
|
||||
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
|
||||
}),
|
||||
WebSocketStarRendezvous.start({
|
||||
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
|
||||
refreshPeerListIntervalMS: 1000,
|
||||
strictMultiaddr: false,
|
||||
cryptoChallenge: true
|
||||
}),
|
||||
getPeerRelay(),
|
||||
createSwitchA(),
|
||||
createSwitchB()
|
||||
])
|
||||
// Use the last peer
|
||||
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
peerInfo.multiaddrs.add(MULTIADDRS_WEBSOCKETS[0])
|
||||
|
||||
node = new Node({
|
||||
libp2p = new Libp2p({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
streamMuxer: [Muxer],
|
||||
connEncryption: [Crypto]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: true
|
||||
active: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
// Add the echo protocol
|
||||
libp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
|
||||
|
||||
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
await node.start()
|
||||
await libp2p.start()
|
||||
}
|
||||
|
||||
const after = () => {
|
||||
return Promise.all([
|
||||
wrtcRendezvous.stop(),
|
||||
wsRendezvous.stop(),
|
||||
node.stop(),
|
||||
promisify(switchA.stop, { context: switchA })(),
|
||||
promisify(switchB.stop, { context: switchB })()
|
||||
])
|
||||
const after = async () => {
|
||||
await libp2p.stop()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@ docs
|
||||
**/*.log
|
||||
test/repo-tests*
|
||||
**/bundle.js
|
||||
.cache
|
||||
|
||||
# Logs
|
||||
logs
|
||||
|
@ -21,7 +21,8 @@ jobs:
|
||||
- stage: check
|
||||
script:
|
||||
- npx aegir build --bundlesize
|
||||
- npx aegir dep-check -- -i wrtc -i electron-webrtc
|
||||
# Remove pull libs once ping is async
|
||||
- npx aegir dep-check -- -i pull-handshake -i pull-stream
|
||||
- npm run lint
|
||||
|
||||
- stage: test
|
||||
@ -29,16 +30,14 @@ jobs:
|
||||
addons:
|
||||
chrome: stable
|
||||
script:
|
||||
- npx aegir test -t browser
|
||||
- npx aegir test -t webworker
|
||||
- npx aegir test -t browser -t webworker
|
||||
|
||||
- stage: test
|
||||
name: firefox
|
||||
addons:
|
||||
firefox: latest
|
||||
script:
|
||||
- npx aegir test -t browser -- --browsers FirefoxHeadless
|
||||
- npx aegir test -t webworker -- --browsers FirefoxHeadless
|
||||
- npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
|
||||
|
||||
notifications:
|
||||
email: false
|
69
CHANGELOG.md
69
CHANGELOG.md
@ -1,3 +1,72 @@
|
||||
<a name="0.27.0-rc.0"></a>
|
||||
# [0.27.0-rc.0](https://github.com/libp2p/js-libp2p/compare/v0.27.0-pre.2...v0.27.0-rc.0) (2020-01-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* registrar should filter the disconnected conn ([#532](https://github.com/libp2p/js-libp2p/issues/532)) ([83409de](https://github.com/libp2p/js-libp2p/commit/83409de))
|
||||
* stop discoveries ([#530](https://github.com/libp2p/js-libp2p/issues/530)) ([c44e6e3](https://github.com/libp2p/js-libp2p/commit/c44e6e3))
|
||||
* use toB58String everywhere to be consistent ([#537](https://github.com/libp2p/js-libp2p/issues/537)) ([31d1b23](https://github.com/libp2p/js-libp2p/commit/31d1b23))
|
||||
|
||||
|
||||
|
||||
<a name="0.27.0-pre.2"></a>
|
||||
# [0.27.0-pre.2](https://github.com/libp2p/js-libp2p/compare/v0.27.0-pre.1...v0.27.0-pre.2) (2020-01-07)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* conn mngr min/max connection values ([#528](https://github.com/libp2p/js-libp2p/issues/528)) ([a1717da](https://github.com/libp2p/js-libp2p/commit/a1717da))
|
||||
* make dialer configurable ([#521](https://github.com/libp2p/js-libp2p/issues/521)) ([24c3ce6](https://github.com/libp2p/js-libp2p/commit/24c3ce6))
|
||||
* upgrader should not need muxers ([#517](https://github.com/libp2p/js-libp2p/issues/517)) ([56a1825](https://github.com/libp2p/js-libp2p/commit/56a1825))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add libp2p.connections getter ([#522](https://github.com/libp2p/js-libp2p/issues/522)) ([6ca19c5](https://github.com/libp2p/js-libp2p/commit/6ca19c5))
|
||||
* allow transport options to be passed on creation ([#524](https://github.com/libp2p/js-libp2p/issues/524)) ([0d4b2bd](https://github.com/libp2p/js-libp2p/commit/0d4b2bd))
|
||||
|
||||
|
||||
|
||||
<a name="0.27.0-pre.1"></a>
|
||||
# [0.27.0-pre.1](https://github.com/libp2p/js-libp2p/compare/v0.27.0-pre.0...v0.27.0-pre.1) (2019-12-15)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* coalescing dial support ([#518](https://github.com/libp2p/js-libp2p/issues/518)) ([4a871bb](https://github.com/libp2p/js-libp2p/commit/4a871bb))
|
||||
|
||||
|
||||
|
||||
<a name="0.27.0-pre.0"></a>
|
||||
# [0.27.0-pre.0](https://github.com/libp2p/js-libp2p/compare/v0.26.2...v0.27.0-pre.0) (2019-12-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* clean up peer discovery flow ([#494](https://github.com/libp2p/js-libp2p/issues/494)) ([f3eb1f1](https://github.com/libp2p/js-libp2p/commit/f3eb1f1))
|
||||
* clean up pending dials abort per feedback ([7c3371b](https://github.com/libp2p/js-libp2p/commit/7c3371b))
|
||||
* correct release readme ([c4bc00b](https://github.com/libp2p/js-libp2p/commit/c4bc00b))
|
||||
* examples readme typos ([#481](https://github.com/libp2p/js-libp2p/issues/481)) ([35ac02d](https://github.com/libp2p/js-libp2p/commit/35ac02d))
|
||||
* performance bottleneck in stat.js ([#463](https://github.com/libp2p/js-libp2p/issues/463)) ([93a1e42](https://github.com/libp2p/js-libp2p/commit/93a1e42))
|
||||
* release tokens as soon as they are available ([43440aa](https://github.com/libp2p/js-libp2p/commit/43440aa))
|
||||
* replace peerInfo addresses with listen addresses ([#485](https://github.com/libp2p/js-libp2p/issues/485)) ([acbbc0f](https://github.com/libp2p/js-libp2p/commit/acbbc0f))
|
||||
* token release logic ([1838a64](https://github.com/libp2p/js-libp2p/commit/1838a64))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* abort all pending dials on stop ([754fbc2](https://github.com/libp2p/js-libp2p/commit/754fbc2))
|
||||
* add early token recycling in ([24c6037](https://github.com/libp2p/js-libp2p/commit/24c6037))
|
||||
* add token based dialer ([f8540fa](https://github.com/libp2p/js-libp2p/commit/f8540fa))
|
||||
* discovery modules ([#486](https://github.com/libp2p/js-libp2p/issues/486)) ([997ee16](https://github.com/libp2p/js-libp2p/commit/997ee16))
|
||||
* discovery modules from transports should be added ([#510](https://github.com/libp2p/js-libp2p/issues/510)) ([af96dcc](https://github.com/libp2p/js-libp2p/commit/af96dcc))
|
||||
* peer store ([#470](https://github.com/libp2p/js-libp2p/issues/470)) ([f3e276e](https://github.com/libp2p/js-libp2p/commit/f3e276e))
|
||||
* registrar ([#471](https://github.com/libp2p/js-libp2p/issues/471)) ([797d8f0](https://github.com/libp2p/js-libp2p/commit/797d8f0))
|
||||
* support peer-id instances in peer store operations ([#491](https://github.com/libp2p/js-libp2p/issues/491)) ([11ed6bd](https://github.com/libp2p/js-libp2p/commit/11ed6bd))
|
||||
|
||||
|
||||
|
||||
<a name="0.26.2"></a>
|
||||
## [0.26.2](https://github.com/libp2p/js-libp2p/compare/v0.26.1...v0.26.2) (2019-09-24)
|
||||
|
||||
|
522
README.md
522
README.md
@ -45,12 +45,12 @@ We've come a long way, but this project is still in Alpha, lots of development i
|
||||
## Table of Contents
|
||||
|
||||
- [Background](#background)
|
||||
- [Bundles](#bundles)
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [Configuration](#configuration)
|
||||
- [API](#api)
|
||||
- [Events](#events)
|
||||
- [Getting Started](#getting-started)
|
||||
- [Tutorials and Examples](#tutorials-and-examples)
|
||||
- [Development](#development)
|
||||
- [Tests](#tests)
|
||||
- [Packages](#packages)
|
||||
@ -74,460 +74,29 @@ We are in the process of writing better documentation, blog posts, tutorials and
|
||||
|
||||
To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.
|
||||
|
||||
## Bundles
|
||||
|
||||
With its modular nature, libp2p can be found being used in different projects with different sets of features, while preserving the same top level API. `js-libp2p` is only a skeleton and should not be installed directly, if you are looking for a prebundled libp2p stack, please check:
|
||||
|
||||
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-nodejs.js) - The libp2p build used by js-ipfs when run in Node.js
|
||||
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-browser.js) - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
|
||||
|
||||
If you have developed a libp2p bundle, please consider submitting it to this list so that it can be found easily by the users of libp2p.
|
||||
|
||||
## Install
|
||||
|
||||
Again, as noted above, this module is only a skeleton and should not be used directly other than libp2p bundle implementors that want to extend its code.
|
||||
|
||||
```sh
|
||||
npm install --save libp2p
|
||||
npm install libp2p
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
**IMPORTANT NOTE**: We are currently on the way of migrating all our `libp2p` modules to use `async await` and `async iterators`, instead of callbacks and `pull-streams`. As a consequence, when you start a new libp2p project, we must check which versions of the modules you should use. For now, it is required to use the modules using callbacks with `libp2p`, while we are working on getting the remaining modules ready for a full migration. For more details, you can have a look at [libp2p/js-libp2p#266](https://github.com/libp2p/js-libp2p/issues/266).
|
||||
### Configuration
|
||||
|
||||
### [Tutorials and Examples](/examples)
|
||||
|
||||
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios.
|
||||
|
||||
### Creating your own libp2p bundle
|
||||
|
||||
The libp2p module acts as a glue for every libp2p module that you can use to create your own libp2p bundle. Creating your own libp2p bundle gives you a lot of freedom when it comes to customize it with features and default setup. We recommend creating your own libp2p bundle for the app you are developing that takes into account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have built one that leverages the Browser transports).
|
||||
|
||||
**Example:**
|
||||
|
||||
```JavaScript
|
||||
// Creating a bundle that adds:
|
||||
// transport: websockets + tcp
|
||||
// stream-muxing: spdy & mplex
|
||||
// crypto-channel: secio
|
||||
// discovery: multicast-dns
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const Protector = require('libp2p/src/pnet')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
class Node extends Libp2p {
|
||||
constructor (_options) {
|
||||
const peerInfo = _options.peerInfo
|
||||
const defaults = {
|
||||
// The libp2p modules for this libp2p bundle
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
new WS() // It can take instances too!
|
||||
],
|
||||
streamMuxer: [
|
||||
SPDY,
|
||||
MPLEX
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
/** Encryption for private networks. Needs additional private key to work **/
|
||||
// connProtector: new Protector(/*protector specific opts*/),
|
||||
/** Enable custom content routers, such as delegated routing **/
|
||||
// contentRouting: [
|
||||
// new DelegatedContentRouter(peerInfo.id)
|
||||
// ],
|
||||
/** Enable custom peer routers, such as delegated routing **/
|
||||
// peerRouting: [
|
||||
// new DelegatedPeerRouter()
|
||||
// ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS
|
||||
],
|
||||
dht: DHT, // DHT enables PeerRouting, ContentRouting and DHT itself components
|
||||
pubsub: GossipSub
|
||||
},
|
||||
|
||||
// libp2p config options (typically found on a config.json)
|
||||
config: { // The config object is the part of the config that can go into a file, config.json.
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
|
||||
mdns: { // mdns options
|
||||
interval: 1000, // ms
|
||||
enabled: true
|
||||
},
|
||||
webrtcStar: { // webrtc-star options
|
||||
interval: 1000, // ms
|
||||
enabled: false
|
||||
}
|
||||
// .. other discovery module options.
|
||||
},
|
||||
relay: { // Circuit Relay options
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabled: true,
|
||||
randomWalk: {
|
||||
enabled: true, // Allows to disable discovery (enabled by default)
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true, // whether the node should emit to self on publish, in the event of the topic being subscribed
|
||||
signMessages: true, // if messages should be signed
|
||||
strictSigning: true // if message signing should be required
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.
|
||||
```
|
||||
For all the information on how you can configure libp2p see [CONFIGURATION.md](./doc/CONFIGURATION.md).
|
||||
|
||||
### API
|
||||
|
||||
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
|
||||
The specification is available on [API.md](./doc/API.md).
|
||||
|
||||
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
|
||||
### Getting started
|
||||
|
||||
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
|
||||
If you are starting your journey with `js-libp2p`, read the [GETTING_STARTED.md](./doc/GETTING_STARTED.md) guide.
|
||||
|
||||
```js
|
||||
const { createLibp2p } = require('libp2p')
|
||||
createLibp2p(options, (err, libp2p) => {
|
||||
if (err) throw err
|
||||
libp2p.start((err) => {
|
||||
if (err) throw err
|
||||
})
|
||||
})
|
||||
```
|
||||
### Tutorials and Examples
|
||||
|
||||
- `options`: Object of libp2p configuration options
|
||||
- `callback`: Function with signature `function (Error, Libp2p) {}`
|
||||
|
||||
#### Create a Node alternative - `new Libp2p(options)`
|
||||
|
||||
> Creates an instance of Libp2p with a custom `PeerInfo` provided via `options.peerInfo`.
|
||||
|
||||
Required keys in the `options` object:
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
|
||||
- `modules.transport`: An array that must include at least 1 transport, such as `libp2p-tcp`.
|
||||
|
||||
#### `libp2p.start(callback)`
|
||||
|
||||
> Start the libp2p Node.
|
||||
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case starting the node fails.
|
||||
|
||||
#### `libp2p.stop(callback)`
|
||||
|
||||
> Stop the libp2p Node.
|
||||
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.dial(peer, callback)`
|
||||
|
||||
> Dials to another peer in the network, establishes the connection.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
|
||||
#### `libp2p.dialProtocol(peer, protocol, callback)`
|
||||
|
||||
> Dials to another peer in the network and selects a protocol to talk with that peer.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: Function with signature `function (err, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
|
||||
`callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
|
||||
#### `libp2p.dialFSM(peer, protocol, callback)`
|
||||
|
||||
> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: following signature `function (err, connFSM) {}`, where `connFSM` is a [Connection State Machine](https://github.com/libp2p/js-libp2p-switch#connection-state-machine)
|
||||
|
||||
#### `libp2p.hangUp(peer, callback)`
|
||||
|
||||
> Closes an open connection with a peer, graciously.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.peerRouting.findPeer(id, options, callback)`
|
||||
|
||||
> Looks up for multiaddrs of a peer in the DHT
|
||||
|
||||
- `id`: instance of [PeerId][]
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.contentRouting.findProviders(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
- `options.maxNumProviders` maximum number of providers to find
|
||||
|
||||
#### `libp2p.contentRouting.provide(key, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
|
||||
#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`
|
||||
|
||||
> Handle new protocol
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `handlerFunc`: following signature `function (protocol, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.
|
||||
|
||||
#### `libp2p.unhandle(protocol)`
|
||||
|
||||
> Stop handling protocol
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
|
||||
#### Events
|
||||
|
||||
##### `libp2p.on('start', () => {})`
|
||||
|
||||
> Libp2p has started, along with all its services.
|
||||
|
||||
##### `libp2p.on('stop', () => {})`
|
||||
|
||||
> Libp2p has stopped, along with all its services.
|
||||
|
||||
##### `libp2p.on('error', (err) => {})`
|
||||
|
||||
> An error has occurred
|
||||
|
||||
- `err`: instance of `Error`
|
||||
|
||||
##### `libp2p.on('peer:discovery', (peer) => {})`
|
||||
|
||||
> Peer has been discovered.
|
||||
|
||||
If `autoDial` is `true`, applications should **not** attempt to connect to the peer
|
||||
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('peer:connect', (peer) => {})`
|
||||
|
||||
> We have a new muxed connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('peer:disconnect', (peer) => {})`
|
||||
|
||||
> We have closed a connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('connection:start', (peer) => {})`
|
||||
|
||||
> We created a new connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('connection:end', (peer) => {})`
|
||||
|
||||
> We closed a connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
#### `libp2p.isStarted()`
|
||||
|
||||
> Check if libp2p is started
|
||||
|
||||
#### `libp2p.ping(peer [, options], callback)`
|
||||
|
||||
> Ping a node in the network
|
||||
|
||||
#### `libp2p.peerBook`
|
||||
|
||||
> PeerBook instance of the node
|
||||
|
||||
#### `libp2p.peerInfo`
|
||||
|
||||
> PeerInfo instance of the node
|
||||
|
||||
#### `libp2p.pubsub`
|
||||
|
||||
> Same API as IPFS PubSub, defined in the [CORE API Spec](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md). Just replace `ipfs` by `libp2p` and you are golden.
|
||||
|
||||
---------------------
|
||||
|
||||
`DHT methods also exposed for the time being`
|
||||
|
||||
#### `libp2p.dht.put(key, value, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `value`: Buffer
|
||||
|
||||
#### `libp2p.dht.get(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.dht.getMany(key, nVals, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `nVals`: Number
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
[PeerInfo]: https://github.com/libp2p/js-peer-info
|
||||
[PeerId]: https://github.com/libp2p/js-peer-id
|
||||
[PeerBook]: https://github.com/libp2p/js-peer-book
|
||||
[multiaddr]: https://github.com/multiformats/js-multiaddr
|
||||
[Connection]: https://github.com/libp2p/interface-connection
|
||||
|
||||
-------
|
||||
|
||||
### Switch Stats API
|
||||
|
||||
##### `libp2p.stats.emit('update')`
|
||||
|
||||
Every time any stat value changes, this object emits an `update` event.
|
||||
|
||||
#### Global stats
|
||||
|
||||
##### `libp2p.stats.global.snapshot`
|
||||
|
||||
Should return a stats snapshot, which is an object containing the following keys and respective values:
|
||||
|
||||
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
|
||||
##### `libp2p.stats.global.movingAverages`
|
||||
|
||||
Returns an object containing the following keys:
|
||||
|
||||
- dataSent
|
||||
- dataReceived
|
||||
|
||||
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` milliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-transport stats
|
||||
|
||||
##### `libp2p.stats.transports()`
|
||||
|
||||
Returns an array containing the tags (string) for each observed transport.
|
||||
|
||||
##### `libp2p.stats.forTransport(transportTag).snapshot`
|
||||
|
||||
Should return a stats snapshot, which is an object containing the following keys and respective values:
|
||||
|
||||
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
|
||||
##### `libp2p.stats.forTransport(transportTag).movingAverages`
|
||||
|
||||
Returns an object containing the following keys:
|
||||
|
||||
dataSent
|
||||
dataReceived
|
||||
|
||||
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` milliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-protocol stats
|
||||
|
||||
##### `libp2p.stats.protocols()`
|
||||
|
||||
Returns an array containing the tags (string) for each observed protocol.
|
||||
|
||||
##### `libp2p.stats.forProtocol(protocolTag).snapshot`
|
||||
|
||||
Should return a stats snapshot, which is an object containing the following keys and respective values:
|
||||
|
||||
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
|
||||
|
||||
##### `libp2p.stats.forProtocol(protocolTag).movingAverages`
|
||||
|
||||
Returns an object containing the following keys:
|
||||
|
||||
- dataSent
|
||||
- dataReceived
|
||||
|
||||
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` milliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-peer stats
|
||||
|
||||
##### `libp2p.stats.peers()`
|
||||
|
||||
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
|
||||
|
||||
##### `libp2p.stats.forPeer(peerId:String).snapshot`
|
||||
|
||||
Should return a stats snapshot, which is an object containing the following keys and respective values:
|
||||
|
||||
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
|
||||
|
||||
|
||||
##### `libp2p.stats.forPeer(peerId:String).movingAverages`
|
||||
|
||||
Returns an object containing the following keys:
|
||||
|
||||
- dataSent
|
||||
- dataReceived
|
||||
|
||||
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` milliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Stats update interval
|
||||
|
||||
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout`, defined in milliseconds.
|
||||
|
||||
### Private Networks
|
||||
|
||||
#### Enforcement
|
||||
|
||||
Libp2p provides support for connection protection, such as for private networks. You can enforce network protection by setting the environment variable `LIBP2P_FORCE_PNET=1`. When this variable is on, if no protector is set via `options.connProtector`, Libp2p will throw an error upon creation.
|
||||
|
||||
#### Protectors
|
||||
|
||||
Some available network protectors:
|
||||
* [libp2p-pnet](https://github.com/libp2p/js-libp2p/tree/master/src/pnet)
|
||||
You can find multiple examples on the [examples folder](./examples) that will guide you through using libp2p for several scenarios.
|
||||
|
||||
## Development
|
||||
|
||||
@ -563,50 +132,49 @@ List of packages currently in existence for libp2p
|
||||
| Package | Version | Deps | CI | Coverage | Lead Maintainer |
|
||||
| ---------|---------|---------|---------|---------|--------- |
|
||||
| **libp2p** |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://travis-ci.com/libp2p/js-libp2p) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon`](//github.com/libp2p/js-libp2p-daemon) | [](//github.com/libp2p/js-libp2p-daemon/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon) | [](https://travis-ci.com/libp2p/js-libp2p-daemon) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon-client`](//github.com/libp2p/js-libp2p-daemon-client) | [](//github.com/libp2p/js-libp2p-daemon-client/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon-client) | [](https://travis-ci.com/libp2p/js-libp2p-daemon-client) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon-client) | N/A |
|
||||
| [`libp2p-interfaces`](//github.com/libp2p/js-interfaces) | [](//github.com/libp2p/js-interfaces/releases) | [](https://david-dm.org/libp2p/js-interfaces) | [](https://travis-ci.com/libp2p/js-interfaces) | [](https://codecov.io/gh/libp2p/js-interfaces) | N/A |
|
||||
| [`interop-libp2p`](//github.com/libp2p/interop) | [](//github.com/libp2p/interop/releases) | [](https://david-dm.org/libp2p/interop) | [](https://travis-ci.com/libp2p/interop) | [](https://codecov.io/gh/libp2p/interop) | N/A |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://travis-ci.com/libp2p/js-libp2p) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon`](//github.com/libp2p/js-libp2p-daemon) | [](//github.com/libp2p/js-libp2p-daemon/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon) | [](https://travis-ci.com/libp2p/js-libp2p-daemon) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-daemon-client`](//github.com/libp2p/js-libp2p-daemon-client) | [](//github.com/libp2p/js-libp2p-daemon-client/releases) | [](https://david-dm.org/libp2p/js-libp2p-daemon-client) | [](https://travis-ci.com/libp2p/js-libp2p-daemon-client) | [](https://codecov.io/gh/libp2p/js-libp2p-daemon-client) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-interfaces`](//github.com/libp2p/js-interfaces) | [](//github.com/libp2p/js-interfaces/releases) | [](https://david-dm.org/libp2p/js-interfaces) | [](https://travis-ci.com/libp2p/js-interfaces) | [](https://codecov.io/gh/libp2p/js-interfaces) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`interop-libp2p`](//github.com/libp2p/interop) | [](//github.com/libp2p/interop/releases) | [](https://david-dm.org/libp2p/interop) | [](https://travis-ci.com/libp2p/interop) | [](https://codecov.io/gh/libp2p/interop) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| **transports** |
|
||||
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://travis-ci.com/libp2p/js-libp2p-tcp) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) | N/A |
|
||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://travis-ci.com/libp2p/js-libp2p-utp) | [](https://codecov.io/gh/libp2p/js-libp2p-utp) | N/A |
|
||||
| [`libp2p-webrtc-direct`](//github.com/libp2p/js-libp2p-webrtc-direct) | [](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | N/A |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://travis-ci.com/libp2p/js-libp2p-websockets) | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://travis-ci.com/libp2p/js-libp2p-tcp) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://travis-ci.com/libp2p/js-libp2p-utp) | [](https://codecov.io/gh/libp2p/js-libp2p-utp) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-webrtc-direct`](//github.com/libp2p/js-libp2p-webrtc-direct) | [](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://travis-ci.com/libp2p/js-libp2p-websockets) | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **secure channels** |
|
||||
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | [](https://travis-ci.com/libp2p/js-libp2p-secio) | [](https://codecov.io/gh/libp2p/js-libp2p-secio) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | [](https://travis-ci.com/libp2p/js-libp2p-secio) | [](https://codecov.io/gh/libp2p/js-libp2p-secio) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| **stream multiplexers** |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://travis-ci.com/libp2p/js-libp2p-spdy) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://travis-ci.com/libp2p/js-libp2p-spdy) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **peer discovery** |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://travis-ci.com/libp2p/js-libp2p-bootstrap) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://travis-ci.com/libp2p/js-libp2p-mdns) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [](https://travis-ci.com/libp2p/js-libp2p-rendezvous) | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | N/A |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://travis-ci.com/libp2p/js-libp2p-bootstrap) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://travis-ci.com/libp2p/js-libp2p-mdns) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [](https://travis-ci.com/libp2p/js-libp2p-rendezvous) | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **content routing** |
|
||||
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | N/A |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **peer routing** |
|
||||
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | N/A |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **utilities** |
|
||||
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://travis-ci.com/libp2p/js-libp2p-crypto) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-crypto-secp256k1`](//github.com/libp2p/js-libp2p-crypto-secp256k1) | [](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://travis-ci.com/libp2p/js-libp2p-crypto) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-crypto-secp256k1`](//github.com/libp2p/js-libp2p-crypto-secp256k1) | [](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| **data types** |
|
||||
| [`peer-book`](//github.com/libp2p/js-peer-book) | [](//github.com/libp2p/js-peer-book/releases) | [](https://david-dm.org/libp2p/js-peer-book) | [](https://travis-ci.com/libp2p/js-peer-book) | [](https://codecov.io/gh/libp2p/js-peer-book) | N/A |
|
||||
| [`peer-id`](//github.com/libp2p/js-peer-id) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://travis-ci.com/libp2p/js-peer-id) | [](https://codecov.io/gh/libp2p/js-peer-id) | N/A |
|
||||
| [`peer-info`](//github.com/libp2p/js-peer-info) | [](//github.com/libp2p/js-peer-info/releases) | [](https://david-dm.org/libp2p/js-peer-info) | [](https://travis-ci.com/libp2p/js-peer-info) | [](https://codecov.io/gh/libp2p/js-peer-info) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`peer-id`](//github.com/libp2p/js-peer-id) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://travis-ci.com/libp2p/js-peer-id) | [](https://codecov.io/gh/libp2p/js-peer-id) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`peer-info`](//github.com/libp2p/js-peer-info) | [](//github.com/libp2p/js-peer-info/releases) | [](https://david-dm.org/libp2p/js-peer-info) | [](https://travis-ci.com/libp2p/js-peer-info) | [](https://codecov.io/gh/libp2p/js-peer-info) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **pubsub** |
|
||||
| [`libp2p-pubsub`](//github.com/libp2p/js-libp2p-pubsub) | [](//github.com/libp2p/js-libp2p-pubsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-pubsub) | [](https://travis-ci.com/libp2p/js-libp2p-pubsub) | [](https://codecov.io/gh/libp2p/js-libp2p-pubsub) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-floodsub`](//github.com/libp2p/js-libp2p-floodsub) | [](//github.com/libp2p/js-libp2p-floodsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-floodsub) | [](https://travis-ci.com/libp2p/js-libp2p-floodsub) | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | N/A |
|
||||
| [`libp2p-gossipsub`](//github.com/ChainSafe/gossipsub-js) | [](//github.com/ChainSafe/gossipsub-js/releases) | [](https://david-dm.org/ChainSafe/gossipsub-js) | [](https://travis-ci.com/ChainSafe/gossipsub-js) | [](https://codecov.io/gh/ChainSafe/gossipsub-js) | N/A |
|
||||
| [`libp2p-pubsub`](//github.com/libp2p/js-libp2p-pubsub) | [](//github.com/libp2p/js-libp2p-pubsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-pubsub) | [](https://travis-ci.com/libp2p/js-libp2p-pubsub) | [](https://codecov.io/gh/libp2p/js-libp2p-pubsub) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-floodsub`](//github.com/libp2p/js-libp2p-floodsub) | [](//github.com/libp2p/js-libp2p-floodsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-floodsub) | [](https://travis-ci.com/libp2p/js-libp2p-floodsub) | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-gossipsub`](//github.com/ChainSafe/gossipsub-js) | [](//github.com/ChainSafe/gossipsub-js/releases) | [](https://david-dm.org/ChainSafe/gossipsub-js) | [](https://travis-ci.com/ChainSafe/gossipsub-js) | [](https://codecov.io/gh/ChainSafe/gossipsub-js) | [Cayman Nava](mailto:caymannava@gmail.com) |
|
||||
| **extensions** |
|
||||
| [`libp2p-nat-mgnr`](//github.com/libp2p/js-libp2p-nat-mgnr) | [](//github.com/libp2p/js-libp2p-nat-mgnr/releases) | [](https://david-dm.org/libp2p/js-libp2p-nat-mgnr) | [](https://travis-ci.com/libp2p/js-libp2p-nat-mgnr) | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mgnr) | N/A |
|
||||
| [`libp2p-utils`](//github.com/libp2p/js-libp2p-utils) | [](//github.com/libp2p/js-libp2p-utils/releases) | [](https://david-dm.org/libp2p/js-libp2p-utils) | [](https://travis-ci.com/libp2p/js-libp2p-utils) | [](https://codecov.io/gh/libp2p/js-libp2p-utils) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
| [`libp2p-nat-mgnr`](//github.com/libp2p/js-libp2p-nat-mgnr) | [](//github.com/libp2p/js-libp2p-nat-mgnr/releases) | [](https://david-dm.org/libp2p/js-libp2p-nat-mgnr) | [](https://travis-ci.com/libp2p/js-libp2p-nat-mgnr) | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mgnr) | N/A |
|
||||
| [`libp2p-utils`](//github.com/libp2p/js-libp2p-utils) | [](//github.com/libp2p/js-libp2p-utils/releases) | [](https://david-dm.org/libp2p/js-libp2p-utils) | [](https://travis-ci.com/libp2p/js-libp2p-utils) | [](https://codecov.io/gh/libp2p/js-libp2p-utils) | [Vasco Santos](mailto:santos.vasco10@gmail.com) |
|
||||
|
||||
## Contribute
|
||||
|
||||
|
@ -50,8 +50,8 @@ Would you like to contribute to the libp2p project and don't know how? Well, the
|
||||
- Check the issues with the `help wanted` label in the [libp2p repo](https://github.com/libp2p/js-libp2p/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22)
|
||||
- Join an IPFS All Hands, introduce yourself and let us know where you would like to contribute - https://github.com/ipfs/team-mgmt#all-hands-call
|
||||
- Hack with IPFS and show us what you made! The All Hands call is also the perfect venue for demos, join in and show us what you built
|
||||
- Join the discussion at http://discuss.ipfs.io/ and help users finding their answers.
|
||||
- Join the [⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽 ](https://github.com/ipfs/team-mgmt/issues/650) and be part of the Sprint action!
|
||||
- Join the discussion at http://discuss.libp2p.io/ and help users finding their answers.
|
||||
- Join the [⚡️libp2p Weekly Sync 🙌🏽](https://github.com/libp2p/team-mgmt/issues/16) and be part of the Sprint action!
|
||||
|
||||
# ⁉️ Do you have questions?
|
||||
|
||||
|
804
doc/API.md
Normal file
804
doc/API.md
Normal file
@ -0,0 +1,804 @@
|
||||
# API
|
||||
|
||||
* [Static Functions](#static-functions)
|
||||
* [`create`](#create)
|
||||
* [Instance Methods](#instance-methods)
|
||||
* [`start`](#start)
|
||||
* [`stop`](#stop)
|
||||
* [`dial`](#dial)
|
||||
* [`dialProtocol`](#dialprotocol)
|
||||
* [`hangUp`](#hangup)
|
||||
* [`handle`](#handle)
|
||||
* [`unhandle`](#unhandle)
|
||||
* [`ping`](#ping)
|
||||
* [`peerRouting.findPeer`](#peerroutingfindpeer)
|
||||
* [`contentRouting.findProviders`](#contentroutingfindproviders)
|
||||
* [`contentRouting.provide`](#contentroutingprovide)
|
||||
* [`contentRouting.put`](#contentroutingput)
|
||||
* [`contentRouting.get`](#contentroutingget)
|
||||
* [`contentRouting.getMany`](#contentroutinggetmany)
|
||||
* [`pubsub.getSubscribers`](#pubsubgetsubscribers)
|
||||
* [`pubsub.getTopics`](#pubsubgettopics)
|
||||
* [`pubsub.publish`](#pubsubpublish)
|
||||
* [`pubsub.subscribe`](#pubsubsubscribe)
|
||||
* [`pubsub.unsubscribe`](#pubsubunsubscribe)
|
||||
* [`connectionManager.setPeerValue`](#connectionmanagersetpeervalue)
|
||||
* [`metrics.global`](#metricsglobal)
|
||||
* [`metrics.peers`](#metricspeers)
|
||||
* [`metrics.protocols`](#metricsprotocols)
|
||||
* [`metrics.forPeer`](#metricsforpeer)
|
||||
* [`metrics.forProtocol`](#metricsforprotocol)
|
||||
* [Events](#events)
|
||||
* [Types](#types)
|
||||
* [`Stats`](#stats)
|
||||
|
||||
## Static Functions
|
||||
|
||||
### create
|
||||
|
||||
Creates an instance of Libp2p.
|
||||
|
||||
`create(options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| options | `Object` | libp2p options |
|
||||
| options.modules | `Array<Object>` | libp2p modules to use |
|
||||
| [options.config] | `Object` | libp2p modules configuration and core configuration |
|
||||
| [options.connectionManager] | `Object` | libp2p Connection Manager configuration |
|
||||
| [options.datastore] | `Object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
|
||||
| [options.dialer] | `Object` | libp2p Dialer configuration
|
||||
| [options.metrics] | `Object` | libp2p Metrics configuration
|
||||
| [options.peerInfo] | [PeerInfo](https://github.com/libp2p/js-peer-info) | peerInfo instance (it will be created if not provided) |
|
||||
|
||||
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Libp2p>` | Promise resolves with the Libp2p instance |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// specify options
|
||||
const options = {}
|
||||
|
||||
// create libp2p
|
||||
const libp2p = await Libp2p.create(options)
|
||||
```
|
||||
|
||||
Note: The `PeerInfo` option is not required and will be generated if it is not provided.
|
||||
|
||||
<details><summary>Alternative</summary>
|
||||
As an alternative, it is possible to create a Libp2p instance with the constructor:
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// specify options
|
||||
const options = {}
|
||||
|
||||
// create libp2p
|
||||
const libp2p = new Libp2p(options)
|
||||
```
|
||||
|
||||
Required keys in the `options` object:
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node (optional when using `.create`).
|
||||
- `modules.transport`: An array that must include at least 1 compliant transport. See [modules that implement the transport interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport#modules-that-implement-the-interface).
|
||||
|
||||
</details>
|
||||
|
||||
## Libp2p Instance Methods
|
||||
|
||||
### start
|
||||
|
||||
Starts the libp2p node.
|
||||
|
||||
`libp2p.start()`
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise` | Promise resolves when the node is ready |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// ...
|
||||
|
||||
const libp2p = await Libp2p.create(options)
|
||||
|
||||
// start libp2p
|
||||
await libp2p.start()
|
||||
```
|
||||
|
||||
### stop
|
||||
|
||||
Stops the libp2p node.
|
||||
|
||||
`libp2p.stop()`
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise` | Promise resolves when the node is fully stopped |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// ...
|
||||
const libp2p = await Libp2p.create(options)
|
||||
// ...
|
||||
|
||||
// stop libp2p
|
||||
await libp2p.stop()
|
||||
```
|
||||
|
||||
### connections
|
||||
|
||||
A Getter that returns a Map of the current Connections libp2p has to other peers.
|
||||
|
||||
`libp2p.connections`
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Map<string, Array<Connection>>` | A map of [`PeerId`][peer-id] strings to [`Connection`][connection] Arrays |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
for (const [peerId, connections] of libp2p.connections) {
|
||||
for (const connection of connections) {
|
||||
console.log(peerId, connection.remoteAddr.toString())
|
||||
// Logs the PeerId string and the observed remote multiaddr of each Connection
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### dial
|
||||
|
||||
Dials to another peer in the network and establishes the connection.
|
||||
|
||||
`dial(peer, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
|
||||
| [options] | `Object` | dial options |
|
||||
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Connection>` | Promise resolves with the [Connection][connection] instance |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const conn = await libp2p.dial(remotePeerInfo)
|
||||
|
||||
// create a new stream within the connection
|
||||
const { stream, protocol } = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])
|
||||
|
||||
// protocol negotiated: 'echo/1.0.0' means that the other party only supports the older version
|
||||
|
||||
// ...
|
||||
await conn.close()
|
||||
```
|
||||
|
||||
### dialProtocol
|
||||
|
||||
Dials to another peer in the network and selects a protocol to communicate with that peer. The stream between both parties is returned, together with the negotiated protocol.
|
||||
|
||||
`dialProtocol(peer, protocols, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
|
||||
| protocols | `String|Array<String>` | A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made. (e.g '/ipfs/bitswap/1.1.0') |
|
||||
| [options] | `Object` | dial options |
|
||||
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<{ stream:*, protocol:string }>` | Promise resolves with a [duplex stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) and the protocol used |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const { stream, protocol } = await libp2p.dialProtocol(remotePeerInfo, protocols)
|
||||
|
||||
// Use this new stream like any other duplex stream
|
||||
pipe([1, 2, 3], stream, consume)
|
||||
```
|
||||
|
||||
### hangUp
|
||||
|
||||
Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.
|
||||
|
||||
`hangUp(peer)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to hang up |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<void>` | Promise resolves once connection closes |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
await libp2p.hangUp(remotePeerInfo)
|
||||
```
|
||||
|
||||
### handle
|
||||
|
||||
Sets up [multistream-select routing](https://github.com/multiformats/multistream-select) of protocols to their application handlers. Whenever a stream is opened on one of the provided protocols, the handler will be called. `handle` must be called in order to register a handler and support for a given protocol. This also informs other peers of the protocols you support.
|
||||
|
||||
`libp2p.handle(protocols, handler)`
|
||||
|
||||
In the event of a new handler for the same protocol being added, the first one is discarded.
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| protocols | `Array<String>|String` | protocols to register |
|
||||
| handler | `function({ connection:*, stream:*, protocol:string })` | handler to call |
|
||||
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const handler = ({ connection, stream, protocol }) => {
|
||||
// use stream or connection according to the needs
|
||||
}
|
||||
|
||||
libp2p.handle('/echo/1.0.0', handler)
|
||||
```
|
||||
|
||||
### unhandle
|
||||
|
||||
Unregisters all handlers with the given protocols
|
||||
|
||||
`libp2p.unhandle(protocols)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| protocols | `Array<String>|String` | protocols to unregister |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
libp2p.unhandle(['/echo/1.0.0'])
|
||||
```
|
||||
|
||||
### ping
|
||||
|
||||
Pings a given peer and get the operation's latency.
|
||||
|
||||
`libp2p.ping(peer)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peer | `PeerInfo|PeerId|Multiaddr|string` | peer to ping |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<number>` | Latency of the operation in ms |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const latency = await libp2p.ping(otherPeerId)
|
||||
```
|
||||
|
||||
### peerRouting.findPeer
|
||||
|
||||
Iterates over all peer routers in series to find the given peer. If the DHT is enabled, it will be tried first.
|
||||
|
||||
`libp2p.peerRouting.findPeer(peerId, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | [`PeerId`][peer-id] | ID of the peer to find |
|
||||
| options | `Object` | operation options |
|
||||
| options.timeout | `number` | maximum time the query should run |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<PeerInfo>` | Peer info of a known peer |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const peerInfo = await libp2p.peerRouting.findPeer(peerId, options)
|
||||
```
|
||||
|
||||
### contentRouting.findProviders
|
||||
|
||||
Iterates over all content routers in series to find providers of the given key.
|
||||
Once a content router succeeds, the iteration will stop. If the DHT is enabled, it will be queried first.
|
||||
|
||||
`libp2p.contentRouting.findProviders(cid, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| cid | [`CID`](https://github.com/multiformats/js-cid) | cid to find |
|
||||
| options | `Object` | operation options |
|
||||
| options.timeout | `number` | maximum time the query should run |
|
||||
| options.maxNumProviders | `number` | maximum number of providers to find |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `AsyncIterator<PeerInfo>` | Async iterator for [`PeerInfo`](https://github.com/libp2p/js-peer-info) |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// Iterate over the providers found for the given cid
|
||||
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
|
||||
console.log(provider)
|
||||
}
|
||||
```
|
||||
|
||||
### contentRouting.provide
|
||||
|
||||
Iterates over all content routers in parallel, in order to notify it is a provider of the given key.
|
||||
|
||||
`libp2p.contentRouting.provide(cid)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| cid | [`CID`](https://github.com/multiformats/js-cid) | cid to provide |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<void>` | Promise resolves once notifications are sent |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
await libp2p.contentRouting.provide(cid)
|
||||
```
|
||||
|
||||
### contentRouting.put
|
||||
|
||||
Writes a value to a key in the DHT.
|
||||
|
||||
`libp2p.contentRouting.put(key, value, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| key | `String` | key to add to the dht |
|
||||
| value | `Buffer` | value to add to the dht |
|
||||
| [options] | `Object` | put options |
|
||||
| [options.minPeers] | `number` | minimum number of peers required to successfully put (default: closestPeers.length) |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<void>` | Promise resolves once value is stored |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const key = '/key'
|
||||
const value = Buffer.from('oh hello there')
|
||||
|
||||
await libp2p.contentRouting.put(key, value)
|
||||
```
|
||||
|
||||
### contentRouting.get
|
||||
|
||||
Queries the DHT for a value stored for a given key.
|
||||
|
||||
`libp2p.contentRouting.get(key, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| key | `String` | key to get from the dht |
|
||||
| [options] | `Object` | get options |
|
||||
| [options.timeout] | `number` | maximum time the query should run |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Buffer>` | Value obtained from the DHT |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
|
||||
const key = '/key'
|
||||
const value = await libp2p.contentRouting.get(key)
|
||||
```
|
||||
|
||||
### contentRouting.getMany
|
||||
|
||||
Queries the DHT for the n values stored for the given key (without sorting).
|
||||
|
||||
`libp2p.contentRouting.getMany(key, nvals, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| key | `String` | key to get from the dht |
|
||||
| nvals | `number` | number of values aimed |
|
||||
| [options] | `Object` | get options |
|
||||
| [options.timeout] | `number` | maximum time the query should run |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise<Array<{from: PeerId, val: Buffer}>>` | Array of records obtained from the DHT |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
|
||||
const key = '/key'
|
||||
const { from, val } = await libp2p.contentRouting.get(key)
|
||||
```
|
||||
|
||||
### pubsub.getSubscribers
|
||||
|
||||
Gets a list of the peer-ids that are subscribed to one topic.
|
||||
|
||||
`libp2p.pubsub.getSubscribers(topic)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to publish |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Array<String>` | peer-id subscribed to the topic |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const peerIds = libp2p.pubsub.getSubscribers(topic)
|
||||
```
|
||||
|
||||
### pubsub.getTopics
|
||||
|
||||
Gets a list of topics the node is subscribed to.
|
||||
|
||||
`libp2p.pubsub.getTopics()`
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Array<String>` | topics the node is subscribed to |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const topics = libp2p.pubsub.getTopics()
|
||||
```
|
||||
|
||||
### pubsub.publish
|
||||
|
||||
Publishes messages to the given topics.
|
||||
|
||||
`libp2p.pubsub.publish(topic, data)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to publish |
|
||||
| data | `Buffer` | data to publish |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Promise` | publish success |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const data = Buffer.from('data')
|
||||
|
||||
await libp2p.pubsub.publish(topic, data)
|
||||
```
|
||||
|
||||
### pubsub.subscribe
|
||||
|
||||
Subscribes the given handler to a pubsub topic.
|
||||
|
||||
`libp2p.pubsub.subscribe(topic, handler)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to subscribe |
|
||||
| handler | `function({ from: String, data: Buffer, seqno: Buffer, topicIDs: Array<String>, signature: Buffer, key: Buffer })` | handler for new data on topic |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `void` | |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
}
|
||||
|
||||
libp2p.pubsub.subscribe(topic, handler)
|
||||
```
|
||||
|
||||
### pubsub.unsubscribe
|
||||
|
||||
Unsubscribes the given handler from a pubsub topic. If no handler is provided, all handlers for the topic are removed.
|
||||
|
||||
`libp2p.pubsub.unsubscribe(topic, handler)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| topic | `string` | topic to unsubscribe |
|
||||
| handler | `function(<Object>)` | handler subscribed |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `void` | |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const topic = 'topic'
|
||||
const handler = (msg) => {
|
||||
// msg.data - pubsub data received
|
||||
}
|
||||
|
||||
libp2p.pubsub.unsubscribe(topic, handler)
|
||||
```
|
||||
|
||||
### connectionManager.setPeerValue
|
||||
|
||||
Enables users to change the value of certain peers in a range of 0 to 1. Peers with the lowest values will have their Connections pruned first, if any Connection Manager limits are exceeded. See [./CONFIGURATION.md#configuring-connection-manager](./CONFIGURATION.md#configuring-connection-manager) for details on how to configure these limits.
|
||||
|
||||
`libp2p.connectionManager.setPeerValue(peerId, value)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | `PeerId` | The peer to set the value for |
|
||||
| value | `number` | The value of the peer from 0 to 1 |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `void` | |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
libp2p.connectionManager.setPeerValue(highPriorityPeerId, 1)
|
||||
libp2p.connectionManager.setPeerValue(lowPriorityPeerId, 0)
|
||||
```
|
||||
|
||||
### metrics.global
|
||||
|
||||
A [`Stats`](#stats) object of tracking the global bandwidth of the libp2p node.
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const peerIdStrings = libp2p.metrics.peers
|
||||
```
|
||||
|
||||
### metrics.peers
|
||||
|
||||
An array of `PeerId` strings of each peer currently being tracked.
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const peerIdStrings = libp2p.metrics.peers
|
||||
```
|
||||
|
||||
### metrics.protocols
|
||||
|
||||
An array of protocol strings that are currently being tracked.
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const protocols = libp2p.metrics.protocols
|
||||
```
|
||||
|
||||
### metrics.forPeer
|
||||
|
||||
Returns the [`Stats`](#stats) object for a given `PeerId` if it is being tracked.
|
||||
|
||||
`libp2p.metrics.forPeer(peerId)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | `PeerId` | The peer to get stats for |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| [`Stats`](#stats) | The bandwidth stats of the peer |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const peerStats = libp2p.metrics.forPeer(peerInfo)
|
||||
console.log(peerStats.toJSON())
|
||||
```
|
||||
|
||||
### metrics.forProtocol
|
||||
|
||||
Returns the [`Stats`](#stats) object for a given protocol if it is being tracked.
|
||||
|
||||
`libp2p.metrics.forProtocol(protocol)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| protocol | `string` | The protocol to get stats for |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| [`Stats`](#stats) | The bandwidth stats of the protocol across all peers |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
const peerStats = libp2p.metrics.forProtocol('/meshsub/1.0.0')
|
||||
console.log(peerStats.toJSON())
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
|
||||
|
||||
#### An error has occurred
|
||||
|
||||
`libp2p.on('error', (err) => {})`
|
||||
|
||||
- `err`: instance of `Error`
|
||||
|
||||
#### A peer has been discovered
|
||||
|
||||
`libp2p.on('peer:discovery', (peer) => {})`
|
||||
|
||||
If `autoDial` option is `true`, applications should **not** attempt to connect to the peer
|
||||
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
|
||||
|
||||
- `peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
|
||||
|
||||
#### A new connection to a peer has been opened
|
||||
|
||||
This event will be triggered anytime a new Connection is established to another peer.
|
||||
|
||||
`libp2p.on('peer:connect', (peer) => {})`
|
||||
|
||||
- `peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
|
||||
|
||||
#### An existing connection to a peer has been closed
|
||||
|
||||
This event will be triggered anytime we are disconnected from another peer, regardless of the circumstances of that disconnection. If we happen to have multiple connections to a peer, this event will **only** be triggered when the last connection is closed.
|
||||
|
||||
`libp2p.on('peer:disconnect', (peer) => {})`
|
||||
|
||||
- `peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
|
||||
|
||||
## Types
|
||||
|
||||
### Stats
|
||||
|
||||
- `Stats`
|
||||
- `toJSON<function()>`: Returns a JSON snapshot of the stats.
|
||||
- `dataReceived<string>`: The stringified value of total incoming data for this stat.
|
||||
- `dataSent<string>`: The stringified value of total outgoing data for this stat.
|
||||
- `movingAverages<object>`: The properties are dependent on the configuration of the moving averages interval. Defaults are listed here.
|
||||
- `['60000']<Number>`: The calculated moving average at a 1 minute interval.
|
||||
- `['300000']<Number>`: The calculated moving average at a 5 minute interval.
|
||||
- `['900000']<Number>`: The calculated moving average at a 15 minute interval.
|
||||
- `snapshot<object>`: A getter that returns a clone of the raw stats.
|
||||
- `dataReceived<BigNumber>`: A [`BigNumber`](https://github.com/MikeMcl/bignumber.js/) of the amount of incoming data
|
||||
- `dataSent<BigNumber>`: A [`BigNumber`](https://github.com/MikeMcl/bignumber.js/) of the amount of outgoing data
|
||||
- `movingAverages<object>`: A getter that returns a clone of the raw [moving averages](https://www.npmjs.com/package/moving-averages) stats. **Note**: The properties of this are dependent on configuration. The defaults are shown here.
|
||||
- `['60000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 1 minute interval.
|
||||
- `['300000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval.
|
||||
- `['900000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval.
|
||||
|
||||
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
|
||||
[peer-id]: https://github.com/libp2p/js-peer-id
|
541
doc/CONFIGURATION.md
Normal file
541
doc/CONFIGURATION.md
Normal file
@ -0,0 +1,541 @@
|
||||
# Configuration
|
||||
|
||||
- [Configuration](#configuration)
|
||||
- [Overview](#overview)
|
||||
- [Modules](#modules)
|
||||
- [Transport](#transport)
|
||||
- [Stream Multiplexing](#stream-multiplexing)
|
||||
- [Connection Encryption](#connection-encryption)
|
||||
- [Peer Discovery](#peer-discovery)
|
||||
- [Content Routing](#content-routing)
|
||||
- [Peer Routing](#peer-routing)
|
||||
- [DHT](#dht)
|
||||
- [Pubsub](#pubsub)
|
||||
- [Customizing libp2p](#customizing-libp2p)
|
||||
- [Examples](#examples)
|
||||
- [Basic setup](#basic-setup)
|
||||
- [Customizing Peer Discovery](#customizing-peer-discovery)
|
||||
- [Setup webrtc transport and discovery](#setup-webrtc-transport-and-discovery)
|
||||
- [Customizing Pubsub](#customizing-pubsub)
|
||||
- [Customizing DHT](#customizing-dht)
|
||||
- [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing)
|
||||
- [Setup with Relay](#setup-with-relay)
|
||||
- [Configuring Dialing](#configuring-dialing)
|
||||
- [Configuring Connection Manager](#configuring-connection-manager)
|
||||
- [Configuring Metrics](#configuring-metrics)
|
||||
- [Customizing Transports](#customizing-transports)
|
||||
- [Configuration examples](#configuration-examples)
|
||||
|
||||
## Overview
|
||||
|
||||
libp2p is a modular networking stack. It's designed to be able to suit a variety of project needs. The configuration of libp2p is a key part of its structure. It enables you to bring exactly what you need, and only what you need. This document is a guide on how to configure libp2p for your particular project. Check out the [Configuration examples](#configuration-examples) section if you're just looking to leverage an existing configuration.
|
||||
|
||||
Regardless of how you configure libp2p, the top level [API](./API.md) will always remain the same. **Note**: if some modules are not configured, like Content Routing, using those methods will throw errors.
|
||||
|
||||
## Modules
|
||||
|
||||
`js-libp2p` acts as the composer for this modular p2p networking stack using libp2p compatible modules as its subsystems. For getting an instance of `js-libp2p` compliant with all types of networking requirements, it is possible to specify the following subsystems:
|
||||
|
||||
- Transports
|
||||
- Multiplexers
|
||||
- Connection encryption mechanisms
|
||||
- Peer discovery protocols
|
||||
- Content routing protocols
|
||||
- Peer routing protocols
|
||||
- DHT implementation
|
||||
- Pubsub router
|
||||
|
||||
The libp2p ecosystem contains at least one module for each of these subsystems. The user should install and import the modules that are relevant for their requirements. Moreover, thanks to the existing interfaces it is easy to create a libp2p compatible module and use it.
|
||||
|
||||
After selecting the modules to use, it is also possible to configure each one according to your needs.
|
||||
|
||||
Bear in mind that only a **transport** and **connection encryption** are required, while all the other subsystems are optional.
|
||||
|
||||
### Transport
|
||||
|
||||
> In a p2p system, we need to interact with other peers in the network. Transports are used to establish connections between peers. The libp2p transports to use should be decided according to the environment where your node will live, as well as other requirements that you might have.
|
||||
|
||||
Some available transports are:
|
||||
|
||||
- [libp2p/js-libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp)
|
||||
- [libp2p/js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
|
||||
- [libp2p/js-libp2p-webrtc-direct](https://github.com/libp2p/js-libp2p-webrtc-direct)
|
||||
- [libp2p/js-libp2p-websockets](https://github.com/libp2p/js-libp2p-websockets)
|
||||
- [libp2p/js-libp2p-utp](https://github.com/libp2p/js-libp2p-utp) (Work in Progress)
|
||||
|
||||
You should take into consideration that `js-libp2p-tcp` and `js-libp2p-utp` are not available in a **browser** environment.
|
||||
|
||||
If none of the available transports fulfills your needs, you can create a libp2p compatible transport. A libp2p transport just needs to be compliant with the [Transport Interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport).
|
||||
|
||||
If you want to know more about libp2p transports, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/transport
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
|
||||
### Stream Multiplexing
|
||||
|
||||
> Libp2p peers will need to communicate with each other through several protocols during their life. Stream multiplexing allows multiple independent logical streams to share a common underlying transport medium, instead of creating a new connection with the same peer per needed protocol.
|
||||
|
||||
Some available stream multiplexers are:
|
||||
|
||||
- [libp2p/js-libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex)
|
||||
|
||||
If none of the available stream multiplexers fulfills your needs, you can create a libp2p compatible stream multiplexer. A libp2p multiplexer just needs to be compliant with the [Stream Muxer Interface](https://github.com/libp2p/js-interfaces/tree/master/src/stream-muxer).
|
||||
|
||||
If you want to know more about libp2p stream multiplexing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/stream-multiplexing
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
- https://github.com/libp2p/specs/tree/master/mplex
|
||||
|
||||
### Connection Encryption
|
||||
|
||||
> A connection encryption mechanism must be used, in order to ensure all exchanged data between two peers is encrypted.
|
||||
|
||||
Some available connection encryption protocols:
|
||||
|
||||
- [libp2p/js-libp2p-secio](https://github.com/libp2p/js-libp2p-secio)
|
||||
|
||||
If none of the available connection encryption mechanisms fulfills your needs, you can create a libp2p compatible one. A libp2p connection encryption protocol just needs to be compliant with the [Crypto Interface](https://github.com/libp2p/js-interfaces/tree/master/src/crypto).
|
||||
|
||||
If you want to know more about libp2p connection encryption, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/secure-comms
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
|
||||
### Peer Discovery
|
||||
|
||||
> In a p2p network, peer discovery is critical to a functioning system.
|
||||
|
||||
Some available peer discovery modules are:
|
||||
|
||||
- [js-libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns)
|
||||
- [js-libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap)
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
|
||||
|
||||
**Note**: `peer-discovery` services within transports (such as `js-libp2p-webrtc-star`) are automatically gathered from the `transport`, via it's `discovery` property. As such, they do not need to be added in the discovery modules. However, these transports can also be configured and disabled as the other ones.
|
||||
|
||||
If none of the available peer discovery protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer discovery protocol just needs to be compliant with the [Peer Discovery Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-discovery).
|
||||
|
||||
If you want to know more about libp2p peer discovery, you should read the following content:
|
||||
|
||||
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
|
||||
|
||||
### Content Routing
|
||||
|
||||
> Content routing provides a way to find where content lives in the network. It works in two steps: 1) Peers provide (announce) to the network that they are holders of specific content and 2) Peers issue queries to find where that content lives. A Content Routing mechanism could be as complex as a DHT or as simple as a registry somewhere in the network.
|
||||
|
||||
Some available content routing modules are:
|
||||
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-delegated-content-routing](https://github.com/libp2p/js-libp2p-delegated-content-routing)
|
||||
|
||||
If none of the available content routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p content routing protocol just needs to be compliant with the [Content Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/content-routing). **(WIP: This module is not yet implemented)**
|
||||
|
||||
If you want to know more about libp2p content routing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/content-routing
|
||||
|
||||
### Peer Routing
|
||||
|
||||
> Peer Routing offers a way to find other peers in the network by issuing queries using a Peer Routing algorithm, which may be iterative or recursive. If the algorithm is unable to find the target peer, it will return the peers that are "closest" to the target peer, using a distance metric defined by the algorithm.
|
||||
|
||||
Some available peer routing modules are:
|
||||
|
||||
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [js-libp2p-delegated-peer-routing](https://github.com/libp2p/js-libp2p-delegated-peer-routing)
|
||||
|
||||
If none of the available peer routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer routing protocol just needs to be compliant with the [Peer Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-routing). **(WIP: This module is not yet implemented)**
|
||||
|
||||
If you want to know more about libp2p peer routing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/peer-routing
|
||||
|
||||
### DHT
|
||||
|
||||
> A DHT can provide content and peer routing capabilities in a p2p system, as well as peer discovery capabilities.
|
||||
|
||||
The DHT implementation currently available is [libp2p/js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht). This implementation is largely based on the Kademlia whitepaper, augmented with notions from S/Kademlia, Coral and mainlineDHT.
|
||||
|
||||
If this DHT implementation does not fulfill your needs and you want to create or use your own implementation, please get in touch with us through a github issue. We plan to work on improving the ability to bring your own DHT in a future release.
|
||||
|
||||
If you want to know more about libp2p DHT, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/protocols/#kad-dht
|
||||
- https://github.com/libp2p/specs/pull/108
|
||||
|
||||
### Pubsub
|
||||
|
||||
> Publish/Subscribe is a system where peers congregate around topics they are interested in. Peers interested in a topic are said to be subscribed to that topic and should receive the data published on it from other peers.
|
||||
|
||||
Some available pubsub routers are:
|
||||
|
||||
- [libp2p/js-libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub)
|
||||
- [ChainSafe/gossipsub-js](https://github.com/ChainSafe/gossipsub-js)
|
||||
|
||||
If none of the available pubsub routers fulfills your needs, you can create a libp2p compatible one. A libp2p pubsub router just needs to be created on top of [libp2p/js-libp2p-pubsub](https://github.com/libp2p/js-libp2p-pubsub), which ensures `js-libp2p` API expectations.
|
||||
|
||||
If you want to know more about libp2p pubsub, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/publish-subscribe
|
||||
- https://github.com/libp2p/specs/tree/master/pubsub
|
||||
|
||||
## Customizing libp2p
|
||||
|
||||
When [creating a libp2p node](./API.md#create), the modules needed should be specified as follows:
|
||||
|
||||
```js
|
||||
const modules = {
|
||||
transport: [],
|
||||
streamMuxer: [],
|
||||
connEncryption: [],
|
||||
contentRouting: [],
|
||||
peerRouting: [],
|
||||
peerDiscovery: [],
|
||||
dht: dhtImplementation,
|
||||
pubsub: pubsubImplementation
|
||||
}
|
||||
```
|
||||
|
||||
Moreover, the majority of the modules can be customized via option parameters. This way, it is also possible to provide this options through a `config` object. This config object should have the property name of each building block to configure, the same way as the modules specification.
|
||||
|
||||
Besides the `modules` and `config`, libp2p allows other internal options and configurations:
|
||||
- `datastore`: an instance of [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore/) modules.
|
||||
- This is used in modules such as the DHT. If it is not provided, `js-libp2p` will use an in memory datastore.
|
||||
- `peerInfo`: a previously created instance of [libp2p/js-peer-info](https://github.com/libp2p/js-peer-info).
|
||||
- This is particularly useful if you want to reuse the same `peer-id`, as well as for modules like `libp2p-delegated-content-routing`, which need a `peer-id` in their instantiation.
|
||||
|
||||
### Examples
|
||||
|
||||
#### Basic setup
|
||||
|
||||
```js
|
||||
// Creating a libp2p node with:
|
||||
// transport: websockets + tcp
|
||||
// stream-muxing: mplex
|
||||
// crypto-channel: secio
|
||||
// discovery: multicast-dns
|
||||
// dht: kad-dht
|
||||
// pubsub: gossipsub
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
new WS() // It can take instances too!
|
||||
],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
peerDiscovery: [MulticastDNS],
|
||||
dht: DHT,
|
||||
pubsub: GossipSub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Peer Discovery
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
peerDiscovery: [MulticastDNS]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
|
||||
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
||||
// The associated object, will be passed to the service when it is instantiated.
|
||||
[MulticastDNS.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
// .. other discovery module options.
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup webrtc transport and discovery
|
||||
|
||||
```js
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const WS = require('libp2p-websockets')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [
|
||||
WS,
|
||||
WebRTCStar
|
||||
],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
webRTCStar: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Pubsub
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
pubsub: GossipSub
|
||||
},
|
||||
config: {
|
||||
pubsub: { // The pubsub options (and defaults) can be found in the pubsub router documentation
|
||||
enabled: true,
|
||||
emitSelf: true, // whether the node should emit to self on publish
|
||||
signMessages: true, // if messages should be signed
|
||||
strictSigning: true // if message signing should be required
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing DHT
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
dht: { // The DHT options (and defaults) can be found in its documentation
|
||||
kBucketSize: 20,
|
||||
enabled: true,
|
||||
randomWalk: {
|
||||
enabled: true, // Allows to disable discovery (enabled by default)
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Content and Peer Routing
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
// create a peerInfo
|
||||
const peerInfo = await PeerInfo.create()
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO],
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerInfo.id)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter()
|
||||
],
|
||||
},
|
||||
peerInfo
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Relay
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
},
|
||||
config: {
|
||||
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
|
||||
enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay.
|
||||
hop: {
|
||||
enabled: true, // Allows you to be a relay for other peers
|
||||
active: true // You will attempt to dial destination peers if you are not connected to them
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring Dialing
|
||||
|
||||
Dialing in libp2p can be configured to limit the rate of dialing, and how long dials are allowed to take. The below configuration example shows the default values for the dialer.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
},
|
||||
dialer: {
|
||||
maxParallelDials: 100, // How many multiaddrs we can dial in parallel
|
||||
maxDialsPerPeer: 4, // How many multiaddrs we can dial per peer, in parallel
|
||||
dialTimeout: 30e3 // 30 second dial timeout per peer
|
||||
}
|
||||
```
|
||||
|
||||
#### Configuring Connection Manager
|
||||
|
||||
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md). The configuration values below show the defaults for Connection Manager. See [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md#options) for a full description of the parameters.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
},
|
||||
connectionManager: {
|
||||
maxConnections: Infinity,
|
||||
minConnections: 0,
|
||||
pollInterval: 2000,
|
||||
defaultPeerValue: 1,
|
||||
// The below values will only be taken into account when Metrics are enabled
|
||||
maxData: Infinity,
|
||||
maxSentData: Infinity,
|
||||
maxReceivedData: Infinity,
|
||||
maxEventLoopDelay: Infinity,
|
||||
movingAverageInterval: 60000
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring Metrics
|
||||
|
||||
Metrics are disabled in libp2p by default. You can enable and configure them as follows. Aside from enabled being `false` by default, the configuration options listed here are the current defaults.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
},
|
||||
metrics: {
|
||||
enabled: true,
|
||||
computeThrottleMaxQueueSize: 1000, // How many messages a stat will queue before processing
|
||||
computeThrottleTimeout: 2000, // Time in milliseconds a stat will wait, after the last item was added, before processing
|
||||
movingAverageIntervals: [ // The moving averages that will be computed
|
||||
60 * 1000, // 1 minute
|
||||
5 * 60 * 1000, // 5 minutes
|
||||
15 * 60 * 1000 // 15 minutes
|
||||
],
|
||||
maxOldPeersRetention: 50 // How many disconnected peers we will retain stats for
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Customizing Transports
|
||||
|
||||
Some Transports can be passed additional options when they are created. For example, `libp2p-webrtc-star` accepts an optional, custom `wrtc` implementation. In addition to libp2p passing itself and an `Upgrader` to handle connection upgrading, libp2p will also pass the options, if they are provided, from `config.transport`.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const wrtc = require('wrtc')
|
||||
|
||||
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebRTCStar],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
wrtc // You can use `wrtc` when running in Node.js
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Configuration examples
|
||||
|
||||
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
|
||||
|
||||
|
||||
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-nodejs.js) - libp2p configuration used by js-ipfs when running in Node.js
|
||||
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
|
||||
|
||||
If you have developed a project using `js-libp2p`, please consider submitting your configuration to this list so that it can be found easily by other users.
|
||||
|
||||
The [examples](../examples) are also a good source of help for finding a configuration that suits your needs.
|
20
doc/CONNECTION_MANAGER.md
Normal file
20
doc/CONNECTION_MANAGER.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Connection Manager
|
||||
|
||||
The Connection Manager works with the Registrar to keep connections across libp2p within acceptable ranges, which can be configured. By default Connection Manager will monitor:
|
||||
- The total number of open connections
|
||||
- The latency/delay of the event loop
|
||||
|
||||
If Metrics are enabled for libp2p, see [./CONFIGURATION.md#configuring-metrics](./CONFIGURATION.md#configuring-metrics) on how to configure metrics, the Connection Manager can be used to prune connections when certain limits are exceeded.
|
||||
|
||||
The following is a list of available options for setting limits for the Connection Manager to enforce.
|
||||
|
||||
## Options
|
||||
- `maxConnections`: the maximum number of connections libp2p is willing to have before it starts disconnecting. Defaults to `Infinity`
|
||||
- `minConnections`: the minimum number of connections below which libp2p 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 milliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
- `pollInterval`: sets the poll interval (in milliseconds) 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 milliseconds). Defaults to `60000` (1 minute). This must be an available interval configured in `Metrics`
|
||||
- `defaultPeerValue`: number between 0 and 1. Defaults to 1.
|
32
doc/DIALER.md
Normal file
32
doc/DIALER.md
Normal file
@ -0,0 +1,32 @@
|
||||
# js-libp2p Dialer
|
||||
|
||||
**Synopsis**
|
||||
* Parallel dials to the same peer will yield the same connection/error when the first dial settles.
|
||||
* All Dial Requests in js-libp2p must request a token(s) from the Dialer.
|
||||
* The number of tokens requested should be between 1 and the MAX_PER_PEER_DIALS max set in the Dialer.
|
||||
* If the number of available tokens is less than requested, the Dialer may return less than requested.
|
||||
* The number of tokens a DialRequest obtains reflects the maximum number of parallel Multiaddr Dials it can make.
|
||||
* If no tokens are available a DialRequest should immediately end and throw.
|
||||
* As tokens are limited, DialRequests should be given a prioritized list of Multiaddrs to minimize the potential request time.
|
||||
* Once a Multiaddr Dial has succeeded, all pending dials in that Dial Request should be aborted.
|
||||
* If DIAL_TIMEOUT time has elapsed before any one Multiaddr Dial succeeds, all remaining dials in the DialRequest should be aborted.
|
||||
* When a Multiaddr Dial is settled, if there are no more addresses to dial, its token should be released back to the dialer.
|
||||
* Once the DialRequest is settled, any remaining tokens should be released to the dialer.
|
||||
|
||||
## Multiaddr Confidence
|
||||
|
||||
An effective dialing system should involve the inclusion of a Confidence system for Multiaddrs. This enables ranking of Multiaddrs so that a prioritized list can be passed to DialRequests to maximize usage of Dialer Tokens, and minimize connection times.
|
||||
|
||||
**Not Yet Implemented**: This system will be designed and implemented in a future update.
|
||||
|
||||
## Notes
|
||||
|
||||
* A DialRequest gets a set of tokens from the Dialer, up to the MAX_PER_PEER_DIALS max.
|
||||
* A DialRequest SHOULD fail if no dial tokens are available. The DialRequest MAY proceed without tokens, but this should be reserved for High Priority actions and should be rare.
|
||||
* A DialRequest MUST NOT request more tokens than it has addresses to dial. Example: If the MAX_PER_PEER_DIALS is 4 and a DialRequest has 1 address, it should only request 1 token.
|
||||
* A DialRequest SHOULD execute parallel dials for each of its addresses up the total number of tokens it has.
|
||||
* On a successful dial, the DialRequest MUST abort any other in progress dials, return the successful connection and release all tokens.
|
||||
* A new DialRequest SHOULD be given a descending list of prioritized Multiaddrs, based on their confidence. Having higher confidence Multiaddrs first can help minimize the time a DialRequest is active.
|
||||
* A failed dial to a Multiaddr SHOULD add that Multiaddr to a temporary denyList.
|
||||
* A failed dial to a Multiaddr SHOULD lower the confidence of that Multiaddr.
|
||||
* A successful dial to a Multiaddr SHOULD increase the confidence of that Multiaddr.
|
247
doc/GETTING_STARTED.md
Normal file
247
doc/GETTING_STARTED.md
Normal file
@ -0,0 +1,247 @@
|
||||
# Getting Started
|
||||
|
||||
Welcome to libp2p! This guide will walk you through setting up a fully functional libp2p node 🚀
|
||||
|
||||
- [Getting Started](#getting-started)
|
||||
- [Install](#install)
|
||||
- [Configuring libp2p](#configuring-libp2p)
|
||||
- [Basic setup](#basic-setup)
|
||||
- [Transports](#transports)
|
||||
- [Connection Encryption](#connection-encryption)
|
||||
- [Multiplexing](#multiplexing)
|
||||
- [Running Libp2p](#running-libp2p)
|
||||
- [Custom setup](#custom-setup)
|
||||
- [Peer Discovery](#peer-discovery)
|
||||
- [Pubsub](#pubsub)
|
||||
- [What is next](#what-is-next)
|
||||
|
||||
## Install
|
||||
|
||||
The first step is to install libp2p in your project:
|
||||
|
||||
```sh
|
||||
npm install libp2p
|
||||
```
|
||||
|
||||
## Configuring libp2p
|
||||
|
||||
If you're new to libp2p, we recommend configuring your node in stages, as this can make troubleshooting configuration issues much easier. In this guide, we'll do just that. If you're more experienced with libp2p, you may wish to jump to the [Configuration readme](./CONFIGURATION.md).
|
||||
|
||||
### Basic setup
|
||||
|
||||
Now that we have libp2p installed, let's configure the minimum needed to get your node running. The only modules libp2p requires are a [**Transport**][transport] and [**Crypto**][crypto] module. However, we recommend that a basic setup should also have a [**Stream Multiplexer**](streamMuxer) configured, which we will explain shortly. Let's start by setting up a Transport.
|
||||
|
||||
#### Transports
|
||||
|
||||
Libp2p uses Transports to establish connections between peers over the network. Transports are the components responsible for performing the actual exchange of data between libp2p nodes. You can configure any number of Transports, but you only need 1 to start with. Supporting more Transports will improve the ability of your node to speak to a larger number of nodes on the network, as matching Transports are required for two nodes to communicate with one another.
|
||||
|
||||
You should select Transports according to the runtime of your application; Node.js or the browser. You can see a list of some of the available Transports in the [configuration readme](./CONFIGURATION.md#transport). For this guide let's install `libp2p-websockets`, as it can be used in both Node.js and the browser.
|
||||
|
||||
Start by installing `libp2p-websockets`:
|
||||
|
||||
```sh
|
||||
npm install libp2p-websockets
|
||||
```
|
||||
|
||||
Now that we have the module installed, let's configure libp2p to use the Transport. We'll use the [`Libp2p.create`](./API.md#create) method, which takes a single configuration object as its only parameter. We can add the Transport by passing it into the `modules.transport` array:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
There are multiple libp2p transports available, you should evaluate the needs of your application and select the Transport(s) that best suit your requirements. You can add as many transports as you like to `modules.transport` in order to establish connections with as many peers as possible.
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p transports, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/transport
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
</details>
|
||||
|
||||
#### Connection Encryption
|
||||
|
||||
Encryption is an important part of communicating on the libp2p network. Every connection must be encrypted to help ensure security for everyone. As such, Connection Encryption (Crypto) is a required component of libp2p.
|
||||
|
||||
There are a growing number of Crypto modules being developed for libp2p. As those are released they will be tracked in the [Connection Encryption section of the configuration readme](./CONFIGURATION.md#connection-encryption). For now, we are going to configure our node to use the `libp2p-secio` module, which is widely supported across the various libp2p implementations.
|
||||
|
||||
```sh
|
||||
npm install libp2p-secio
|
||||
```
|
||||
|
||||
With `libp2p-secio` installed, we can add it to our existing configuration by importing it and adding it to the `modules.connEncryption` array:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p connection encryption, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/secure-comms
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
</details>
|
||||
|
||||
#### Multiplexing
|
||||
|
||||
While multiplexers are not strictly required, they are highly recommended as they improve the effectiveness and efficiency of connections for the various protocols libp2p runs. Adding a multiplexer to your configuration will allow libp2p to run several of its internal protocols, like Identify, as well as allow your application to easily run any number of protocols over a single connection.
|
||||
|
||||
Looking at the [available stream multiplexing](./CONFIGURATION.md#stream-multiplexing) modules, js-libp2p currently only supports `libp2p-mplex`, so we will use that here. Bear in mind that future libp2p Transports might have `multiplexing` capabilities already built-in (such as `QUIC`).
|
||||
|
||||
You can install `libp2p-mplex` and add it to your libp2p node as follows in the next example.
|
||||
|
||||
```sh
|
||||
npm install libp2p-mplex
|
||||
```
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p stream multiplexing, you should read the following content:
|
||||
|
||||
- https://docs.libp2p.io/concepts/stream-multiplexing
|
||||
- https://github.com/libp2p/specs/tree/master/connections
|
||||
- https://github.com/libp2p/specs/tree/master/mplex
|
||||
</details>
|
||||
|
||||
#### Running Libp2p
|
||||
|
||||
Now that you have configured a [**Transport**][transport], [**Crypto**][crypto] and [**Stream Multiplexer**](streamMuxer) module, you can start your libp2p node. We can start and stop libp2p using the [`libp2p.start()`](./API.md#start) and [`libp2p.stop()`](./API.md#stop) methods.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
// start libp2p
|
||||
await node.start()
|
||||
console.log('libp2p has started')
|
||||
|
||||
// stop libp2p
|
||||
await node.stop()
|
||||
console.log('libp2p has stopped')
|
||||
```
|
||||
|
||||
### Custom setup
|
||||
|
||||
Once your libp2p node is running, it is time to get it connected to the public network. We can do this via peer discovery.
|
||||
|
||||
#### Peer Discovery
|
||||
|
||||
Peer discovery is an important part of creating a well connected libp2p node. A static list of peers will often be used to join the network, but it's useful to couple other discovery mechanisms to ensure you're able to discover other peers that are important to your application.
|
||||
|
||||
For each discovered peer libp2p will emit a `peer:discovery` event which includes metadata about that peer. You can read the [Events](./API.md#events) in the API doc to learn more.
|
||||
|
||||
Looking at the [available peer discovery](./CONFIGURATION.md#peer-discovery) protocols, there are several options to be considered:
|
||||
- If you already know the addresses of some other network peers, you should consider using `libp2p-bootstrap` as this is the easiest way of getting your peer into the network.
|
||||
- If it is likely that you will have other peers on your local network, `libp2p-mdns` is a must if you're node is not running in the browser. It allows peers to discover each other when on the same local network.
|
||||
- If your application is browser based you can use the `libp2p-webrtc-star` Transport, which includes a rendezvous based peer sharing service.
|
||||
- A random walk approach can be used via `libp2p-kad-dht`, to crawl the network and find new peers along the way.
|
||||
|
||||
For this guide we will configure `libp2p-bootstrap` as this is useful for joining the public network.
|
||||
|
||||
Let's install `libp2p-bootstrap`.
|
||||
|
||||
```sh
|
||||
npm install libp2p-bootstrap
|
||||
```
|
||||
|
||||
We can provide specific configurations for each protocol within a `config.peerDiscovery` property in the options as shown below.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
// Known peers addresses
|
||||
const bootstrapMultiaddrs = [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3'
|
||||
]
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
|
||||
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
||||
// The associated object, will be passed to the service when it is instantiated.
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapMultiaddrs // provide array of multiaddrs
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.on('peer:discovery', (peer) => {
|
||||
console.log('Discovered %s', peer.id.toB58String()) // Log discovered peer
|
||||
})
|
||||
|
||||
node.on('peer:connect', (peer) => {
|
||||
console.log('Connected to %s', peer.id.toB58String()) // Log connected peer
|
||||
})
|
||||
|
||||
// start libp2p
|
||||
await node.start()
|
||||
```
|
||||
|
||||
<details><summary>Read More</summary>
|
||||
If you want to know more about libp2p peer discovery, you should read the following content:
|
||||
|
||||
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
|
||||
</details>
|
||||
|
||||
## What is next
|
||||
|
||||
There are a lot of other concepts within `libp2p`, that are not covered in this guide. For additional configuration options we recommend checking out the [Configuration Readme](./CONFIGURATION.md) and the [examples folder](../examples). If you have any problems getting started, or if anything isn't clear, please let us know by submitting an issue!
|
||||
|
||||
|
||||
[transport]: https://github.com/libp2p/js-interfaces/tree/master/src/transport
|
||||
[crypto]: https://github.com/libp2p/js-interfaces/tree/master/src/crypto
|
||||
[streamMuxer]: https://github.com/libp2p/js-interfaces/tree/master/src/stream-muxer
|
28
doc/METRICS.md
Normal file
28
doc/METRICS.md
Normal file
@ -0,0 +1,28 @@
|
||||
# Bandwidth Metrics
|
||||
|
||||
- Metrics gathering is optional, as there is a performance hit to using it.
|
||||
- Metrics do NOT currently contain OS level stats, only libp2p application level Metrics. For example, TCP messages (ACK, FIN, etc) are not accounted for.
|
||||
- See the [API](./API.md) for Metrics usage. Metrics in libp2p do not emit events, as such applications wishing to read Metrics will need to do so actively. This ensures that the system is not unnecessarily firing update notifications.
|
||||
|
||||
## Tracking
|
||||
- When a transport hands off a connection for upgrading, Metrics are hooked up if enabled.
|
||||
- When a stream is created, Metrics will be tracked on that stream and associated to that streams protocol.
|
||||
- Tracked Metrics are associated to a specific peer, and count towards global bandwidth Metrics.
|
||||
|
||||
### Metrics Processing
|
||||
- The main Metrics object consists of individual `Stats` objects
|
||||
- The following categories are tracked:
|
||||
- Global stats; every byte in and out
|
||||
- Peer stats; every byte in and out, per peer
|
||||
- Protocol stats; every byte in and out, per protocol
|
||||
- When a message goes through Metrics:
|
||||
- It is added to the global stat
|
||||
- It is added to the stats for the remote peer
|
||||
- It is added to the protocol stats if there is one
|
||||
- When data is pushed onto a `Stat` it is added to a queue
|
||||
- The queue is processed at the earliest of either (configurable):
|
||||
- every 2 seconds after the last item was added to the queue
|
||||
- or once 1000 items have been queued
|
||||
- When the queue is processed:
|
||||
- The data length is added to either the `in` or `out` stat
|
||||
- The moving averages is calculated since the last queue processing (based on most recently processed item timestamp)
|
164
doc/STREAMING_ITERABLES.md
Normal file
164
doc/STREAMING_ITERABLES.md
Normal file
@ -0,0 +1,164 @@
|
||||
# Iterable Streams
|
||||
|
||||
> This document is a guide on how to use Iterable Streams in Libp2p. As a part of the [refactor away from callbacks](https://github.com/ipfs/js-ipfs/issues/1670), we have also moved to using Iterable Streams instead of [pull-streams](https://pull-stream.github.io/). If there are missing usage guides you feel should be added, please submit a PR!
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Iterable Streams](#iterable-streams)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Usage Guide](#usage-guide)
|
||||
- [Transforming Bidirectional Data](#transforming-bidirectional-data)
|
||||
- [Iterable Stream Types](#iterable-stream-types)
|
||||
- [Source](#source)
|
||||
- [Sink](#sink)
|
||||
- [Transform](#transform)
|
||||
- [Duplex](#duplex)
|
||||
- [Iterable Modules](#iterable-modules)
|
||||
|
||||
## Usage Guide
|
||||
|
||||
### Transforming Bidirectional Data
|
||||
|
||||
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).
|
||||
|
||||
```js
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Wrapper is what we will write and read from
|
||||
// This gives us two duplex iterables that are internally connected
|
||||
const [internal, external] = duplexPair()
|
||||
|
||||
// Now we can pipe our wrapper to the existing duplex iterable
|
||||
pipe(
|
||||
external, // The external half of the pair interacts with the existing duplex
|
||||
outgoingTransform, // A transform iterable to send data through (ie: encrypting)
|
||||
existingDuplex, // The original duplex iterable we are wrapping
|
||||
incomingTransform, // A transform iterable to read data through (ie: decrypting)
|
||||
external
|
||||
)
|
||||
|
||||
// We can now read and write from the other half of our pair
|
||||
pipe(
|
||||
['some data'],
|
||||
internal, // The internal half of the pair is what we will interact with to read/write data
|
||||
async (source) => {
|
||||
for await (const chunk of source) {
|
||||
console.log('Data: %s', chunk.toString())
|
||||
// > Data: some data
|
||||
}
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## Iterable Stream Types
|
||||
|
||||
These types are pulled from [@alanshaw's gist](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9) on streaming iterables.
|
||||
|
||||
### Source
|
||||
|
||||
A "source" is something that can be consumed. It is an iterable object.
|
||||
|
||||
```js
|
||||
const ints = {
|
||||
[Symbol.asyncIterator] () {
|
||||
let i = 0
|
||||
return {
|
||||
async next () {
|
||||
return { done: false, value: i++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const ints = (async function * () {
|
||||
let i = 0
|
||||
while (true) yield i++
|
||||
})()
|
||||
```
|
||||
|
||||
### Sink
|
||||
|
||||
A "sink" is something that consumes (or drains) a source. It is a function that takes a source and iterates over it. It optionally returns a value.
|
||||
|
||||
```js
|
||||
const logger = async source => {
|
||||
const it = source[Symbol.asyncIterator]()
|
||||
while (true) {
|
||||
const { done, value } = await it.next()
|
||||
if (done) break
|
||||
console.log(value) // prints 0, 1, 2, 3...
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const logger = async source => {
|
||||
for await (const chunk of source) {
|
||||
console.log(chunk) // prints 0, 1, 2, 3...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Transform
|
||||
|
||||
A "transform" is both a sink _and_ a source where the values it consumes and the values that can be consumed from it are connected in some way. It is a function that takes a source and returns a source.
|
||||
|
||||
```js
|
||||
const doubler = source => {
|
||||
return {
|
||||
[Symbol.asyncIterator] () {
|
||||
const it = source[Symbol.asyncIterator]()
|
||||
return {
|
||||
async next () {
|
||||
const { done, value } = await it.next()
|
||||
if (done) return { done }
|
||||
return { done, value: value * 2 }
|
||||
}
|
||||
return () {
|
||||
return it.return && it.return()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// or, more succinctly using a generator and for/await:
|
||||
|
||||
const doubler = source => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield chunk * 2
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
### Duplex
|
||||
|
||||
A "duplex" is similar to a transform but the values it consumes are not necessarily connected to the values that can be consumed from it. It is an object with two properties, `sink` and `source`.
|
||||
|
||||
```js
|
||||
const duplex = {
|
||||
sink: async source => {/* ... */},
|
||||
source: { [Symbol.asyncIterator] () {/* ... */} }
|
||||
}
|
||||
```
|
||||
|
||||
## Iterable Modules
|
||||
|
||||
- [it-handshake][it-handshake] Handshakes for binary protocols with iterable streams.
|
||||
- [it-length-prefixed][it-length-prefixed] Streaming length prefixed buffers with async iterables.
|
||||
- [it-pair][it-pair] Paired streams that are internally connected.
|
||||
- [it-pipe][it-pipe] Create a pipeline of iterables. Works with duplex streams.
|
||||
- [it-pushable][it-pushable] An iterable that you can push values into.
|
||||
- [it-reader][it-reader] Read an exact number of bytes from a binary, async iterable.
|
||||
- [streaming-iterables][streaming-iterables] A Swiss army knife for async iterables.
|
||||
|
||||
[it-handshake]: https://github.com/jacobheun/it-handshake
|
||||
[it-length-prefixed]: https://github.com/alanshaw/it-length-prefixed
|
||||
[it-pair]: https://github.com/alanshaw/it-pair
|
||||
[it-pipe]: https://github.com/alanshaw/it-pipe
|
||||
[it-pushable]: https://github.com/alanshaw/it-pushable
|
||||
[it-reader]: https://github.com/alanshaw/it-reader
|
||||
[streaming-iterables]: https://github.com/bustle/streaming-iterables
|
@ -4,76 +4,44 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const async = require('async')
|
||||
const Pushable = require('pull-pushable')
|
||||
const p = Pushable()
|
||||
let idListener
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
async.parallel([
|
||||
(callback) => {
|
||||
PeerId.createFromJSON(require('./peer-id-dialer'), (err, idDialer) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
callback(null, idDialer)
|
||||
})
|
||||
},
|
||||
(callback) => {
|
||||
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
callback(null, idListener)
|
||||
})
|
||||
}
|
||||
], (err, ids) => {
|
||||
if (err) throw err
|
||||
const peerDialer = new PeerInfo(ids[0])
|
||||
async function run() {
|
||||
const [idDialer, idListener] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./peer-id-dialer')),
|
||||
PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
])
|
||||
|
||||
// Create a new libp2p node on localhost with a randomly chosen port
|
||||
const peerDialer = new PeerInfo(idDialer)
|
||||
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
const nodeDialer = new Node({
|
||||
peerInfo: peerDialer
|
||||
})
|
||||
|
||||
const peerListener = new PeerInfo(ids[1])
|
||||
idListener = ids[1]
|
||||
// Create a PeerInfo with the listening peer's address
|
||||
const peerListener = new PeerInfo(idListener)
|
||||
peerListener.multiaddrs.add('/ip4/127.0.0.1/tcp/10333')
|
||||
nodeDialer.start((err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
console.log('Dialer ready, listening on:')
|
||||
// Start the libp2p host
|
||||
await nodeDialer.start()
|
||||
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
|
||||
nodeDialer.dialProtocol(peerListener, '/chat/1.0.0', (err, conn) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
console.log('nodeA dialed to nodeB on protocol: /chat/1.0.0')
|
||||
console.log('Type a message and see what happens')
|
||||
// Write operation. Data sent as a buffer
|
||||
pull(
|
||||
p,
|
||||
conn
|
||||
)
|
||||
// Sink, data converted from buffer to utf8 string
|
||||
pull(
|
||||
conn,
|
||||
pull.map((data) => {
|
||||
return data.toString('utf8').replace('\n', '')
|
||||
}),
|
||||
pull.drain(console.log)
|
||||
)
|
||||
|
||||
process.stdin.setEncoding('utf8')
|
||||
process.openStdin().on('data', (chunk) => {
|
||||
var data = chunk.toString()
|
||||
p.push(data)
|
||||
})
|
||||
})
|
||||
// Output this node's address
|
||||
console.log('Dialer ready, listening on:')
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
})
|
||||
|
||||
// Dial to the remote peer (the "listener")
|
||||
const { stream } = await nodeDialer.dialProtocol(peerListener, '/chat/1.0.0')
|
||||
|
||||
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
|
||||
console.log('Type a message and see what happens')
|
||||
|
||||
// Send stdin to the stream
|
||||
stdinToStream(stream)
|
||||
// Read the stream and output to console
|
||||
streamToConsole(stream)
|
||||
}
|
||||
|
||||
run()
|
||||
|
@ -1,41 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const secio = require('libp2p-secio')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') {
|
||||
return pref
|
||||
}
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return spdy
|
||||
case 'mplex': return mplex
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (muxers) {
|
||||
const muxerPrefs = process.env.LIBP2P_MUXER
|
||||
if (muxerPrefs && !muxers) {
|
||||
return mapMuxers(muxerPrefs.split(','))
|
||||
} else if (muxers) {
|
||||
return mapMuxers(muxers)
|
||||
} else {
|
||||
return [mplex, spdy]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
@ -44,29 +15,8 @@ class Node extends libp2p {
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [ secio ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 10000,
|
||||
enabled: false
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.bootstrapList
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
}
|
||||
streamMuxer: [ mplex ],
|
||||
connEncryption: [ secio ]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,53 +4,38 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle.js')
|
||||
const pull = require('pull-stream')
|
||||
const Pushable = require('pull-pushable')
|
||||
const p = Pushable()
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
async function run() {
|
||||
// Create a new libp2p node with the given multi-address
|
||||
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
const peerListener = new PeerInfo(idListener)
|
||||
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
|
||||
const nodeListener = new Node({
|
||||
peerInfo: peerListener
|
||||
})
|
||||
|
||||
nodeListener.start((err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
nodeListener.on('peer:connect', (peerInfo) => {
|
||||
console.log(peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
nodeListener.handle('/chat/1.0.0', (protocol, conn) => {
|
||||
pull(
|
||||
p,
|
||||
conn
|
||||
)
|
||||
|
||||
pull(
|
||||
conn,
|
||||
pull.map((data) => {
|
||||
return data.toString('utf8').replace('\n', '')
|
||||
}),
|
||||
pull.drain(console.log)
|
||||
)
|
||||
|
||||
process.stdin.setEncoding('utf8')
|
||||
process.openStdin().on('data', (chunk) => {
|
||||
var data = chunk.toString()
|
||||
p.push(data)
|
||||
})
|
||||
})
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
// Log a message when a remote peer connects to us
|
||||
nodeListener.on('peer:connect', (peerInfo) => {
|
||||
console.log(peerInfo.id.toB58String())
|
||||
})
|
||||
})
|
||||
|
||||
// Handle messages for the protocol
|
||||
await nodeListener.handle('/chat/1.0.0', async ({ stream }) => {
|
||||
// Send stdin to the stream
|
||||
stdinToStream(stream)
|
||||
// Read the stream and output to console
|
||||
streamToConsole(stream)
|
||||
})
|
||||
|
||||
// Start listening
|
||||
await nodeListener.start()
|
||||
|
||||
// Output listen addresses to the console
|
||||
console.log('Listener ready, listening on:')
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
|
40
examples/chat/src/stream.js
Normal file
40
examples/chat/src/stream.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
const lp = require('it-length-prefixed')
|
||||
|
||||
function stdinToStream(stream) {
|
||||
// Read utf-8 from stdin
|
||||
process.stdin.setEncoding('utf8')
|
||||
pipe(
|
||||
// Read from stdin (the source)
|
||||
process.stdin,
|
||||
// Encode with length prefix (so receiving side knows how much data is coming)
|
||||
lp.encode(),
|
||||
// Write to the stream (the sink)
|
||||
stream.sink
|
||||
)
|
||||
}
|
||||
|
||||
function streamToConsole(stream) {
|
||||
pipe(
|
||||
// Read from the stream (the source)
|
||||
stream.source,
|
||||
// Decode length-prefixed data
|
||||
lp.decode(),
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
for await (const msg of source) {
|
||||
// Output the data as a utf8 string
|
||||
console.log('> ' + msg.toString('utf8').replace('\n', ''))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
stdinToStream,
|
||||
streamToConsole
|
||||
}
|
@ -1,14 +1,11 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
|
||||
const bootstrapers = [
|
||||
@ -23,51 +20,35 @@ const bootstrapers = [
|
||||
'/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
|
||||
]
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
bootstrap: {
|
||||
interval: 20e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
;(async () => {
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
node.on('peer:connect', (peer) => {
|
||||
console.log('Connection established to:', peer.id.toB58String()) // Emitted when a peer has been found
|
||||
})
|
||||
|
||||
node.on('peer:discovery', (peer) => {
|
||||
// No need to dial, autoDial is on
|
||||
console.log('Discovered:', peer.id.toB58String())
|
||||
})
|
||||
|
||||
node.on('peer:connect', (peer) => {
|
||||
console.log('Connection established to:', peer.id.toB58String())
|
||||
})
|
||||
})
|
||||
await node.start()
|
||||
})();
|
||||
|
@ -1,63 +1,45 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO],
|
||||
peerDiscovery: [MulticastDNS]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
})();
|
||||
|
@ -10,36 +10,32 @@ These mechanisms save configuration and enable a node to operate without any exp
|
||||
|
||||
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, mplex and SECIO. You can see the complete example at [1.js](./1.js).
|
||||
|
||||
First, we create our libp2p bundle.
|
||||
First, we create our libp2p node.
|
||||
|
||||
```JavaScript
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
class MyBundle extends libp2p {
|
||||
constructor (peerInfo) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 2000,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
const Libp2p = require('libp2p')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const node = Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
In this bundle, we use a `bootstrappers` array listing peers to connect _on boot_. Here is the list used by js-ipfs and go-ipfs.
|
||||
In this configuration, we use a `bootstrappers` array listing peers to connect _on boot_. Here is the list used by js-ipfs and go-ipfs.
|
||||
|
||||
```JavaScript
|
||||
const bootstrapers = [
|
||||
@ -58,34 +54,37 @@ const bootstrapers = [
|
||||
Now, once we create and start the node, we can listen for events such as `peer:discovery` and `peer:connect`, these events tell us when we found a peer, independently of the discovery mechanism used and when we actually dialed to that peer.
|
||||
|
||||
```JavaScript
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ Bootstrap ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 60e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
|
||||
// Emitted when a peer has been found
|
||||
node.on('peer:discovery', (peer) => {
|
||||
console.log('Discovered:', peer.id.toB58String())
|
||||
// Note how we need to dial, even if just to warm up the Connection (by not
|
||||
// picking any protocol) in order to get a full Connection. The Peer Discovery
|
||||
// doesn't make any decisions for you.
|
||||
node.dial(peer, () => {})
|
||||
})
|
||||
|
||||
// Once the dial is complete, this event is emitted.
|
||||
node.on('peer:connect', (peer) => {
|
||||
console.log('Connection established to:', peer.id.toB58String())
|
||||
})
|
||||
})
|
||||
|
||||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
node.on('peer:connect', (peer) => {
|
||||
console.log('Connection established to:', peer.id.toB58String()) // Emitted when a peer has been found
|
||||
})
|
||||
|
||||
// Emitted when a peer has been found
|
||||
node.on('peer:discovery', (peer) => {
|
||||
console.log('Discovered:', peer.id.toB58String())
|
||||
})
|
||||
|
||||
await node.start()
|
||||
```
|
||||
|
||||
From running [1.js](./1.js), you should see the following:
|
||||
@ -101,76 +100,56 @@ Discovered: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
|
||||
Discovered: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
|
||||
Discovered: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
|
||||
Discovered: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
|
||||
Connection established to: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
|
||||
Connection established to: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
|
||||
Connection established to: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
|
||||
Connection established to: QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm
|
||||
Connection established to: QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM
|
||||
Connection established to: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
|
||||
Connection established to: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
|
||||
Connection established to: QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z
|
||||
Connection established to: QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu
|
||||
```
|
||||
|
||||
## 2. MulticastDNS to find other peers in the network
|
||||
|
||||
For this example, we need `libp2p-mdns`, go ahead and `npm install` it. You can find the complete solution at [2.js](./2.js).
|
||||
|
||||
Update your libp2p bundle to include MulticastDNS.
|
||||
Update your libp2p configuration to include MulticastDNS.
|
||||
|
||||
```JavaScript
|
||||
class MyBundle extends libp2p {
|
||||
constructor (peerInfo) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
// Run at 1s so we can observe more quickly, default is 10s
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
const Libp2p = require('libp2p')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
To observe it working, spawn two nodes.
|
||||
|
||||
```JavaScript
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
})
|
||||
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
||||
```
|
||||
|
||||
If you run this example, you will see a continuous stream of each peer discovering each other.
|
||||
If you run this example, you will see the other peers being discovered.
|
||||
|
||||
```bash
|
||||
> node 2.js
|
||||
Discovered: QmSSbQpuKrxkoXHm1v4Pi35hPN5hUHMZoBoawEs2Nhvi8m
|
||||
Discovered: QmRcXXhtG8vTqwVBRonKWtV4ovDoC1Fe56WYtcrw694eiJ
|
||||
Discovered: QmSSbQpuKrxkoXHm1v4Pi35hPN5hUHMZoBoawEs2Nhvi8m
|
||||
Discovered: QmRcXXhtG8vTqwVBRonKWtV4ovDoC1Fe56WYtcrw694eiJ
|
||||
Discovered: QmSSbQpuKrxkoXHm1v4Pi35hPN5hUHMZoBoawEs2Nhvi8m
|
||||
Discovered: QmRcXXhtG8vTqwVBRonKWtV4ovDoC1Fe56WYtcrw694eiJ
|
||||
```
|
||||
|
||||
## 3. Where to find other Peer Discovery Mechanisms
|
||||
|
@ -8,51 +8,54 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const async = require('async')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async.parallel([
|
||||
(cb) => PeerId.createFromJSON(require('./id-d'), cb),
|
||||
(cb) => PeerId.createFromJSON(require('./id-l'), cb)
|
||||
], (err, ids) => {
|
||||
if (err) { throw err }
|
||||
async function run() {
|
||||
const [dialerId, listenerId] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./id-d')),
|
||||
PeerId.createFromJSON(require('./id-l'))
|
||||
])
|
||||
|
||||
// Dialer
|
||||
const dialerId = ids[0]
|
||||
const dialerPeerInfo = new PeerInfo(dialerId)
|
||||
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
const dialerNode = new Node({
|
||||
peerInfo: dialerPeerInfo
|
||||
})
|
||||
|
||||
// Peer to Dial
|
||||
const listenerPeerInfo = new PeerInfo(ids[1])
|
||||
const listenerId = ids[1]
|
||||
// Peer to Dial (the listener)
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' +
|
||||
listenerId.toB58String()
|
||||
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
|
||||
|
||||
dialerNode.start((err) => {
|
||||
if (err) { throw err }
|
||||
// Start the dialer libp2p node
|
||||
await dialerNode.start()
|
||||
|
||||
console.log('Dialer ready, listening on:')
|
||||
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
|
||||
'/p2p/' + dialerId.toB58String()))
|
||||
console.log('Dialer ready, listening on:')
|
||||
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
|
||||
'/p2p/' + dialerId.toB58String()))
|
||||
|
||||
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
||||
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
// Dial the listener node
|
||||
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
||||
const { stream } = await dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0')
|
||||
|
||||
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
||||
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
||||
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
conn,
|
||||
pull.collect((err, data) => {
|
||||
if (err) { throw err }
|
||||
console.log('received echo:', data.toString())
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
pipe(
|
||||
// Source data
|
||||
['hey'],
|
||||
// Write to the stream, and pass its output to the next function
|
||||
stream,
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
for await (const data of source) {
|
||||
// Output the data
|
||||
console.log('received echo:', data.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
run()
|
||||
|
@ -1,41 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const secio = require('libp2p-secio')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') {
|
||||
return pref
|
||||
}
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return spdy
|
||||
case 'mplex': return mplex
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (muxers) {
|
||||
const muxerPrefs = process.env.LIBP2P_MUXER
|
||||
if (muxerPrefs && !muxers) {
|
||||
return mapMuxers(muxerPrefs.split(','))
|
||||
} else if (muxers) {
|
||||
return mapMuxers(muxers)
|
||||
} else {
|
||||
return [mplex, spdy]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
@ -44,29 +15,8 @@ class Node extends libp2p {
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [ secio ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 10000,
|
||||
enabled: false
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.bootstrapList
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
}
|
||||
streamMuxer: [ mplex ],
|
||||
connEncryption: [ secio ]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,39 +8,34 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const series = require('async/series')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
let listenerId
|
||||
let listenerNode
|
||||
async function run() {
|
||||
const listenerId = await PeerId.createFromJSON(require('./id-l'))
|
||||
|
||||
series([
|
||||
(cb) => {
|
||||
PeerId.createFromJSON(require('./id-l'), (err, id) => {
|
||||
if (err) { return cb(err) }
|
||||
listenerId = id
|
||||
cb()
|
||||
})
|
||||
},
|
||||
(cb) => {
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
|
||||
listenerNode = new Node({
|
||||
peerInfo: listenerPeerInfo
|
||||
})
|
||||
// Listener libp2p node
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
|
||||
const listenerNode = new Node({
|
||||
peerInfo: listenerPeerInfo
|
||||
})
|
||||
|
||||
listenerNode.on('peer:connect', (peerInfo) => {
|
||||
console.log('received dial to me from:', peerInfo.id.toB58String())
|
||||
})
|
||||
// Log a message when we receive a connection
|
||||
listenerNode.on('peer:connect', (peerInfo) => {
|
||||
console.log('received dial to me from:', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
listenerNode.start(cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
// Handle incoming connections for the protocol by piping from the stream
|
||||
// back to itself (an echo)
|
||||
await listenerNode.handle('/echo/1.0.0', ({ stream }) => pipe(stream.source, stream.sink))
|
||||
|
||||
// Start listening
|
||||
await listenerNode.start()
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
|
@ -1,63 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ SPDY ],
|
||||
connEncryption: [ SECIO ]
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node2.handle('/a-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
node2.handle('/a-protocol', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/a-protocol', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['This information is sent out encrypted to the other peer']), conn)
|
||||
})
|
||||
})
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/a-protocol')
|
||||
|
||||
await pipe(
|
||||
['This information is sent out encrypted to the other peer'],
|
||||
stream
|
||||
)
|
||||
})();
|
||||
|
@ -4,7 +4,7 @@ libp2p can leverage the encrypted communications from the transports it uses (i.
|
||||
|
||||
We call this usage a _connection upgrade_ where given a connection between peer A to peer B, a protocol handshake can be performed that gives that connection new properties.
|
||||
|
||||
A byproduct of having these encrypted communications modules is that we can authenticate the peers we are dialing to. You might have noticed that every time we dial to a peer in libp2p space, we always use its PeerId at the end (e.g /ip4/127.0.0.1/tcp/89765/ipfs/QmWCbVw1XZ8hiYBwwshPce2yaTDYTqTaP7GCHGpry3ykWb), this PeerId is generated by hashing the Public Key of the peer. With this, we can create a crypto challenge when dialing to another peer and prove that peer is the owner of a PrivateKey that matches the Public Key we know.
|
||||
A byproduct of having these encrypted communications modules is that we can authenticate the peers we are dialing to. You might have noticed that every time we dial to a peer in libp2p space, we always use its PeerId at the end (e.g /ip4/127.0.0.1/tcp/89765/p2p/QmWCbVw1XZ8hiYBwwshPce2yaTDYTqTaP7GCHGpry3ykWb), this PeerId is generated by hashing the Public Key of the peer. With this, we can create a crypto challenge when dialing to another peer and prove that peer is the owner of a PrivateKey that matches the Public Key we know.
|
||||
|
||||
# 1. Set up encrypted communications with SECIO
|
||||
|
||||
@ -12,24 +12,21 @@ We will build this example on top of example for [Protocol and Stream Multiplexi
|
||||
|
||||
SECIO is the crypto channel developed for IPFS, it is a TLS 1.3 like crypto channel that established an encrypted communication channel between two peers.
|
||||
|
||||
To add it to your libp2p bundle, all you have to do is:
|
||||
To add it to your libp2p configuration, all you have to do is:
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (peerInfo) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ SPDY ],
|
||||
// Attach secio as the crypto channel to use
|
||||
connEncryption: [ SECIO ]
|
||||
}
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
// Attach secio as the crypto channel to use
|
||||
connEncryption: [ SECIO ]
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
|
4
examples/libp2p-in-the-browser/.babelrc
Normal file
4
examples/libp2p-in-the-browser/.babelrc
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"presets": ["@babel/preset-env"],
|
||||
"plugins": ["syntax-async-functions","transform-regenerator"]
|
||||
}
|
1
examples/libp2p-in-the-browser/1/.gitignore
vendored
1
examples/libp2p-in-the-browser/1/.gitignore
vendored
@ -1 +0,0 @@
|
||||
bundle.js
|
@ -1,32 +0,0 @@
|
||||
{
|
||||
"name": "libp2p-in-the-browser",
|
||||
"version": "0.1.0",
|
||||
"description": "See other nodes in the network using WebRTC Star discovery mechanism",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"bundle": "browserify src/index.js > public/bundle.js",
|
||||
"serve": "static public -p 9090 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'",
|
||||
"start": "npm run bundle && npm run serve"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"browserify": "^14.5.0",
|
||||
"concat-stream": "^1.6.0",
|
||||
"detect-dom-ready": "^1.0.2",
|
||||
"node-static": "~0.7.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"detect-dom-ready": "^1.0.2",
|
||||
"libp2p": "../../../",
|
||||
"libp2p-bootstrap": "~0.9.7",
|
||||
"libp2p-gossipsub": "~0.0.4",
|
||||
"libp2p-kad-dht": "^0.15.3",
|
||||
"libp2p-mplex": "~0.8.5",
|
||||
"libp2p-secio": "~0.11.1",
|
||||
"libp2p-spdy": "~0.13.3",
|
||||
"libp2p-webrtc-star": "~0.15.8",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websockets": "~0.12.2",
|
||||
"peer-info": "~0.15.1"
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>libp2p in the browser</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>libp2p node running \o/</h1>
|
||||
<div id="my-peer"></div>
|
||||
<div id="swarm"></div>
|
||||
|
||||
<script src="bundle.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,94 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const libp2p = require('libp2p')
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
|
||||
const bootstrapList = [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/sfo-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/dns4/sfo-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
|
||||
]
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor ({ peerInfo }) {
|
||||
const wrtcStar = new WebRTCStar({ id: peerInfo.id })
|
||||
const wsstar = new WebSocketStar({ id: peerInfo.id })
|
||||
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
wrtcStar,
|
||||
WebSockets,
|
||||
wsstar
|
||||
],
|
||||
streamMuxer: [
|
||||
Mplex,
|
||||
SPDY
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcStar.discovery,
|
||||
wsstar.discovery,
|
||||
Bootstrap
|
||||
],
|
||||
dht: DHT,
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
webRTCStar: {
|
||||
enabled: true
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: true
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 20e3,
|
||||
enabled: true,
|
||||
list: bootstrapList
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
pubsub: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
connectionManager: {
|
||||
minPeers: 10,
|
||||
maxPeers: 50
|
||||
}
|
||||
}
|
||||
|
||||
super({ ...defaults, peerInfo })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./browser-bundle')
|
||||
|
||||
function createNode (callback) {
|
||||
PeerInfo.create((err, peerInfo) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const peerIdStr = peerInfo.id.toB58String()
|
||||
const webrtcAddr = `/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star/p2p/${peerIdStr}`
|
||||
const wsAddr = `/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star`
|
||||
|
||||
peerInfo.multiaddrs.add(webrtcAddr)
|
||||
peerInfo.multiaddrs.add(wsAddr)
|
||||
|
||||
const node = new Node({
|
||||
peerInfo
|
||||
})
|
||||
|
||||
node.idStr = peerIdStr
|
||||
callback(null, node)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = createNode
|
@ -1,57 +0,0 @@
|
||||
/* eslint no-console: ["error", { allow: ["log"] }] */
|
||||
/* eslint max-nested-callbacks: ["error", 5] */
|
||||
'use strict'
|
||||
|
||||
const domReady = require('detect-dom-ready')
|
||||
const createNode = require('./create-node')
|
||||
|
||||
domReady(() => {
|
||||
const myPeerDiv = document.getElementById('my-peer')
|
||||
const swarmDiv = document.getElementById('swarm')
|
||||
|
||||
createNode((err, node) => {
|
||||
if (err) {
|
||||
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
|
||||
}
|
||||
|
||||
node.on('peer:discovery', (peerInfo) => {
|
||||
console.log('Discovered a peer:', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
node.on('peer:connect', (peerInfo) => {
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
console.log('Got connection to: ' + idStr)
|
||||
const connDiv = document.createElement('div')
|
||||
connDiv.innerHTML = 'Connected to: ' + idStr
|
||||
connDiv.id = idStr
|
||||
swarmDiv.append(connDiv)
|
||||
})
|
||||
|
||||
node.on('peer:disconnect', (peerInfo) => {
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
const el = document.getElementById(idStr)
|
||||
el && el.remove()
|
||||
})
|
||||
|
||||
node.start((err) => {
|
||||
if (err) {
|
||||
return console.log(err)
|
||||
}
|
||||
|
||||
const idStr = node.peerInfo.id.toB58String()
|
||||
|
||||
const idDiv = document
|
||||
.createTextNode('Node is ready. ID: ' + idStr)
|
||||
|
||||
myPeerDiv.append(idDiv)
|
||||
|
||||
console.log('Node is listening o/')
|
||||
node.peerInfo.multiaddrs.toArray().forEach(ma => {
|
||||
console.log(ma.toString())
|
||||
})
|
||||
|
||||
// NOTE: to stop the node
|
||||
// node.stop((err) => {})
|
||||
})
|
||||
})
|
||||
})
|
@ -1,22 +1,55 @@
|
||||
# libp2p running in the Browser
|
||||
# libp2p in the browser
|
||||
|
||||
One of the primary goals with libp2p P2P was to get it fully working in the browser and interopable with the versions running in Go and in Node.js.
|
||||
This example leverages the [Parcel.js bundler](https://parceljs.org/) to compile and serve the libp2p code in the browser. Parcel uses [Babel](https://babeljs.io/) to handle transpilation of the code. You can use other bundlers such as Webpack or Browserify, but we will not be covering them here.
|
||||
|
||||
# 1. Setting up a simple app that lists connections to other nodes
|
||||
## Setup
|
||||
|
||||
Start by installing libp2p's dependencies.
|
||||
In order to run the example, first install the dependencies from same directory as this README:
|
||||
|
||||
```bash
|
||||
> cd ../../
|
||||
> npm install
|
||||
> cd examples/libp2p-in-the-browser
|
||||
```
|
||||
cd ./examples/libp2p-in-the-browser
|
||||
npm install
|
||||
```
|
||||
|
||||
Then simply go into the folder [1](./1) and execute the following
|
||||
## Signaling Server
|
||||
|
||||
```bash
|
||||
> cd 1
|
||||
> npm install
|
||||
> npm start
|
||||
# open your browser in port :9090
|
||||
This example uses the `libp2p-webrtc-star` module, which enables libp2p browser nodes to establish direct connections to one another via a central signaling server. For this example, we are using the signaling server that ships with `libp2p-webrtc-star`.
|
||||
|
||||
You can start the server by running `npm run server`. This will start a signaling server locally on port `9090`. If you'd like to run a signaling server outside of this example, you can see instructions on how to do so in the [`libp2p-webrtc-star` README](https://github.com/libp2p/js-libp2p-webrtc-star).
|
||||
|
||||
When you run the server, you should see output that looks something like this:
|
||||
|
||||
```log
|
||||
$ npm run server
|
||||
|
||||
> libp2p-in-browser@1.0.0 server
|
||||
> star-signal
|
||||
|
||||
Listening on: http://0.0.0.0:9090
|
||||
```
|
||||
|
||||
## Running the examples
|
||||
|
||||
Once you have started the signaling server, you can run the Parcel server.
|
||||
|
||||
```
|
||||
npm start
|
||||
```
|
||||
|
||||
The output should look something like this:
|
||||
|
||||
```log
|
||||
$ npm start
|
||||
|
||||
> libp2p-in-browser@1.0.0 start
|
||||
> parcel index.html
|
||||
|
||||
Server running at http://localhost:1234
|
||||
✨ Built in 1000ms.
|
||||
```
|
||||
|
||||
This will compile the code and start a server listening on port [http://localhost:1234](http://localhost:1234). Now open your browser to `http://localhost:1234`. You should see a log of your node's Peer ID, the discovered peers from the Bootstrap module, and connections to those peers as they are created.
|
||||
|
||||
Now, if you open a second browser tab to `http://localhost:1234`, you should discover your node from the previous tab. This is due to the fact that the `libp2p-webrtc-star` transport also acts as a Peer Discovery interface. Your node will be notified of any peer that connects to the same signaling server you are connected to. Once libp2p discovers this new peer, it will attempt to establish a direct WebRTC connection.
|
||||
|
||||
**Note**: In the example we assign libp2p to `window.libp2p`, in case you would like to play around with the API directly in the browser. You can of course make changes to `index.js` and Parcel will automatically rebuild and reload the browser tabs.
|
||||
|
23
examples/libp2p-in-the-browser/index.html
Normal file
23
examples/libp2p-in-the-browser/index.html
Normal file
@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>js-libp2p parcel.js browser example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1 id="status">Starting libp2p...</h1>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<pre id="output"></pre>
|
||||
</main>
|
||||
|
||||
<script src="./index.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
73
examples/libp2p-in-the-browser/index.js
Normal file
73
examples/libp2p-in-the-browser/index.js
Normal file
@ -0,0 +1,73 @@
|
||||
import 'babel-polyfill'
|
||||
import Libp2p from 'libp2p'
|
||||
import Websockets from 'libp2p-websockets'
|
||||
import WebRTCStar from 'libp2p-webrtc-star'
|
||||
import Secio from 'libp2p-secio'
|
||||
import Mplex from 'libp2p-mplex'
|
||||
import Boostrap from 'libp2p-bootstrap'
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
// Create our libp2p node
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets, WebRTCStar],
|
||||
connEncryption: [Secio],
|
||||
streamMuxer: [Mplex],
|
||||
peerDiscovery: [Boostrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
enabled: true,
|
||||
list: [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// UI elements
|
||||
const status = document.getElementById('status')
|
||||
const output = document.getElementById('output')
|
||||
|
||||
output.textContent = ''
|
||||
|
||||
function log (txt) {
|
||||
console.info(txt)
|
||||
output.textContent += `${txt.trim()}\n`
|
||||
}
|
||||
|
||||
// Add the signaling server address, along with our PeerId to our multiaddrs list
|
||||
// libp2p will automatically attempt to dial to the signaling server so that it can
|
||||
// receive inbound connections from other peers
|
||||
const webrtcAddr = '/ip4/0.0.0.0/tcp/9090/wss/p2p-webrtc-star'
|
||||
libp2p.peerInfo.multiaddrs.add(webrtcAddr)
|
||||
|
||||
// Listen for new peers
|
||||
libp2p.on('peer:discovery', (peerInfo) => {
|
||||
log(`Found peer ${peerInfo.id.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for new connections to peers
|
||||
libp2p.on('peer:connect', (peerInfo) => {
|
||||
log(`Connected to ${peerInfo.id.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for peers disconnecting
|
||||
libp2p.on('peer:disconnect', (peerInfo) => {
|
||||
log(`Disconnected from ${peerInfo.id.toB58String()}`)
|
||||
})
|
||||
|
||||
await libp2p.start()
|
||||
status.innerText = 'libp2p started!'
|
||||
log(`libp2p id is ${libp2p.peerInfo.id.toB58String()}`)
|
||||
|
||||
// Export libp2p to the window so you can play with the API
|
||||
window.libp2p = libp2p
|
||||
})
|
34
examples/libp2p-in-the-browser/package.json
Normal file
34
examples/libp2p-in-the-browser/package.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "libp2p-in-browser",
|
||||
"version": "1.0.0",
|
||||
"description": "A libp2p node running in the browser",
|
||||
"main": "index.js",
|
||||
"browserslist": [
|
||||
"last 2 Chrome versions"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "parcel index.html",
|
||||
"server": "star-signal"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@babel/preset-env": "^7.8.3",
|
||||
"libp2p": "../../",
|
||||
"libp2p-bootstrap": "^0.10.3",
|
||||
"libp2p-mplex": "^0.9.3",
|
||||
"libp2p-secio": "^0.12.2",
|
||||
"libp2p-webrtc-star": "^0.17.3",
|
||||
"libp2p-websockets": "^0.13.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.8.3",
|
||||
"@babel/core": "^7.8.3",
|
||||
"babel-plugin-syntax-async-functions": "^6.13.0",
|
||||
"babel-plugin-transform-regenerator": "^6.26.0",
|
||||
"babel-polyfill": "^6.26.0",
|
||||
"parcel-bundler": "^1.12.4"
|
||||
}
|
||||
}
|
@ -1,77 +1,55 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
// we add the DHT module that will enable Peer and Content Routing
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
}
|
||||
const delay = require('delay')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
|
||||
parallel([
|
||||
(cb) => node1.dial(node2.peerInfo, cb),
|
||||
(cb) => node2.dial(node3.peerInfo, cb),
|
||||
// Set up of the cons might take time
|
||||
(cb) => setTimeout(cb, 300)
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
|
||||
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
|
||||
if (err) { throw err }
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerInfo),
|
||||
node2.dial(node3.peerInfo)
|
||||
])
|
||||
|
||||
// The DHT routing tables need a moment to populate
|
||||
await delay(100)
|
||||
|
||||
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id)
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
})();
|
||||
|
@ -1,85 +1,61 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const CID = require('cids')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
// we add the DHT module that will enable Peer and Content Routing
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
}
|
||||
const all = require('it-all')
|
||||
const delay = require('delay')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
|
||||
parallel([
|
||||
(cb) => node1.dial(node2.peerInfo, cb),
|
||||
(cb) => node2.dial(node3.peerInfo, cb),
|
||||
// Set up of the cons might take time
|
||||
(cb) => setTimeout(cb, 300)
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||
|
||||
node1.contentRouting.provide(cid, (err) => {
|
||||
if (err) { throw err }
|
||||
|
||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||
|
||||
node3.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
||||
if (err) { throw err }
|
||||
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerInfo),
|
||||
node2.dial(node3.peerInfo)
|
||||
])
|
||||
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||
await node1.contentRouting.provide(cid)
|
||||
|
||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||
|
||||
// wait for propagation
|
||||
await delay(300)
|
||||
|
||||
const providers = await all(node3.contentRouting.findProviders(cid, { timeout: 3000 }))
|
||||
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
})();
|
||||
|
@ -10,31 +10,27 @@ Content Routing is the category of modules that offer a way to find where conten
|
||||
|
||||
This example builds on top of the [Protocol and Stream Muxing](../protocol-and-stream-muxing). We need to install `libp2p-kad-dht`, go ahead and `npm install libp2p-kad-dht`. If you want to see the final version, open [1.js](./1.js).
|
||||
|
||||
First, let's update our bundle to support Peer Routing and Content Routing.
|
||||
First, let's update our config to support Peer Routing and Content Routing.
|
||||
|
||||
```JavaScript
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
// we add the DHT module that will enable Peer and Content Routing
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
// dht must be enabled
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
}
|
||||
}
|
||||
}
|
||||
const Libp2p = require('libp2p')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
// we add the DHT module that will enable Peer and Content Routing
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
// dht must be enabled
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Once that is done, we can use the createNode function we developed in the previous example to create 3 nodes. Connect node 1 to node 2 and node 2 to node 3. We will use node 2 as a way to find the whereabouts of node 3
|
||||
@ -44,22 +40,18 @@ const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
|
||||
parallel([
|
||||
(cb) => node1.dial(node2.peerInfo, cb),
|
||||
(cb) => node2.dial(node3.peerInfo, cb),
|
||||
// Set up of the cons might take time
|
||||
(cb) => setTimeout(cb, 100)
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
await Promise.all([
|
||||
node1.dial(node2.peerInfo),
|
||||
node2.dial(node3.peerInfo)
|
||||
])
|
||||
|
||||
//
|
||||
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
|
||||
if (err) { throw err }
|
||||
// Set up of the cons might take time
|
||||
await delay(100)
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
})
|
||||
})
|
||||
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id)
|
||||
|
||||
console.log('Found it, multiaddrs are:')
|
||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
```
|
||||
|
||||
You should see the output being something like:
|
||||
@ -82,17 +74,12 @@ You can find this example completed in [2.js](./2.js), however as you will see i
|
||||
Instead of calling `peerRouting.findPeer`, we will use `contentRouting.provide` and `contentRouting.findProviders`.
|
||||
|
||||
```JavaScript
|
||||
node1.contentRouting.provide(cid, (err) => {
|
||||
if (err) { throw err }
|
||||
await node1.contentRouting.provide(cid)
|
||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||
|
||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||
const provs = await all(node3.contentRouting.findProviders(cid, { timeout: 5000 }))
|
||||
|
||||
node3.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
||||
if (err) { throw err }
|
||||
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
})
|
||||
})
|
||||
console.log('Found provider:', providers[0].id.toB58String())
|
||||
```
|
||||
|
||||
The output of your program should look like:
|
||||
|
@ -1,145 +0,0 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const IPFS = require('ipfs')
|
||||
const assert = require('assert').strict
|
||||
const { generate: writeKey } = require('libp2p/src/pnet')
|
||||
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.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.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)
|
@ -1,60 +0,0 @@
|
||||
'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/src/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
|
@ -1,5 +1,5 @@
|
||||
# Private Networking with IPFS
|
||||
This example shows how to set up a private network of IPFS nodes.
|
||||
# Private Networking
|
||||
This example shows how to set up a private network of libp2p nodes.
|
||||
|
||||
## Setup
|
||||
Install dependencies:
|
50
examples/pnet/index.js
Normal file
50
examples/pnet/index.js
Normal file
@ -0,0 +1,50 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const { generate } = require('libp2p/src/pnet')
|
||||
const privateLibp2pNode = require('./libp2p-node')
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
// Create a buffer and write the swarm key to it
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
generate(swarmKey)
|
||||
|
||||
// This key is for testing a different key not working
|
||||
const otherSwarmKey = Buffer.alloc(95)
|
||||
generate(otherSwarmKey)
|
||||
|
||||
;(async () => {
|
||||
const node1 = await privateLibp2pNode(swarmKey)
|
||||
|
||||
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
||||
const node2 = await privateLibp2pNode(swarmKey)
|
||||
// const node2 = await privateLibp2pNode(otherSwarmKey)
|
||||
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
|
||||
console.log('nodes started...')
|
||||
|
||||
await node1.dial(node2.peerInfo)
|
||||
|
||||
node2.handle('/private', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/private')
|
||||
|
||||
await pipe(
|
||||
['This message is sent on a private network'],
|
||||
stream
|
||||
)
|
||||
})();
|
36
examples/pnet/libp2p-node.js
Normal file
36
examples/pnet/libp2p-node.js
Normal file
@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Protector = require('libp2p/src/pnet')
|
||||
|
||||
/**
|
||||
* privateLibp2pNode returns a libp2p node function that will use the swarm
|
||||
* key at the given `swarmKeyPath` to create the Protector
|
||||
*
|
||||
* @param {Buffer} swarmKey
|
||||
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
|
||||
*/
|
||||
const privateLibp2pNode = async (swarmKeyPath) => {
|
||||
const node = await Libp2p.create({
|
||||
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(swarmKeyPath)
|
||||
}
|
||||
})
|
||||
|
||||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
return node
|
||||
}
|
||||
|
||||
module.exports = privateLibp2pNode
|
@ -11,10 +11,9 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ipfs": "^0.38.0",
|
||||
"libp2p": "^0.26.2",
|
||||
"libp2p-mplex": "^0.8.5",
|
||||
"libp2p-secio": "^0.11.1",
|
||||
"libp2p-tcp": "^0.13.2"
|
||||
"libp2p": "../..",
|
||||
"libp2p-mplex": "^0.9.3",
|
||||
"libp2p-secio": "^0.12.1",
|
||||
"libp2p-tcp": "^0.14.2"
|
||||
}
|
||||
}
|
@ -1,102 +1,79 @@
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ]
|
||||
}
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
// exact matching
|
||||
node2.handle('/your-protocol', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/your-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
// semver matching
|
||||
// multiple protocols
|
||||
/*
|
||||
node2.handle('/another-protocol/1.0.1', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
*/
|
||||
|
||||
// custom func matching
|
||||
/*
|
||||
node2.handle('/custom-match-func', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
}, (myProtocol, requestedProtocol, callback) => {
|
||||
if (myProtocol.indexOf(requestedProtocol)) {
|
||||
callback(null, true)
|
||||
} else {
|
||||
callback(null, false)
|
||||
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => {
|
||||
if (protocol === '/another-protocol/2.0.0') {
|
||||
// handle backwards compatibility
|
||||
}
|
||||
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
*/
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/your-protocol', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['my own protocol, wow!']), conn)
|
||||
})
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, ['/your-protocol'])
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
|
||||
/*
|
||||
node1.dialProtocol(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['semver me please']), conn)
|
||||
})
|
||||
*/
|
||||
const { stream } = node1.dialProtocol(node2.peerInfo, ['/another-protocol/1.0.0'])
|
||||
|
||||
/*
|
||||
node1.dialProtocol(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['do I fall into your criteria?']), conn)
|
||||
})
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
*/
|
||||
})
|
||||
})();
|
||||
|
@ -1,83 +1,63 @@
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
const pull = require('pull-stream')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ SPDY ]
|
||||
}
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
node2.handle('/a', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
|
||||
node2.handle('/b', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
await node.start()
|
||||
|
||||
series([
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/a', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['protocol (a)']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['protocol (b)']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['another conn on protocol (b)']), conn)
|
||||
cb()
|
||||
})
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
})
|
||||
|
||||
node2.handle(['/a', '/b'], ({ protocol, stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(`from: ${protocol}, msg: ${msg.toString()}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const { stream: stream1 } = await node1.dialProtocol(node2.peerInfo, ['/a'])
|
||||
await pipe(
|
||||
['protocol (a)'],
|
||||
stream1
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node1.dialProtocol(node2.peerInfo, ['/b'])
|
||||
await pipe(
|
||||
['protocol (b)'],
|
||||
stream2
|
||||
)
|
||||
|
||||
const { stream: stream3 } = await node1.dialProtocol(node2.peerInfo, ['/b'])
|
||||
await pipe(
|
||||
['another stream on protocol (b)'],
|
||||
stream3
|
||||
)
|
||||
})();
|
||||
|
@ -1,88 +1,69 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
const pull = require('pull-stream')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ SPDY ]
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.handle('/node-1', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
node1.handle('/node-1', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
|
||||
node2.handle('/node-2', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/node-2', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
series([
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/node-2', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['from 1 to 2']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node2.dialProtocol(node1.peerInfo, '/node-1', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['from 2 to 1']), conn)
|
||||
cb()
|
||||
})
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
console.log('Addresses by which both peers are connected')
|
||||
node1.peerBook
|
||||
.getAllArray()
|
||||
.forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString()))
|
||||
node2.peerBook
|
||||
.getAllArray()
|
||||
.forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString()))
|
||||
})
|
||||
})
|
||||
const { stream: stream1 } = await node1.dialProtocol(node2.peerInfo, ['/node-2'])
|
||||
await pipe(
|
||||
['from 1 to 2'],
|
||||
stream1
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node2.dialProtocol(node1.peerInfo, ['/node-1'])
|
||||
await pipe(
|
||||
['from 2 to 1'],
|
||||
stream2
|
||||
)
|
||||
})();
|
||||
|
@ -6,23 +6,30 @@ The feature of agreeing on a protocol over an established connection is what we
|
||||
|
||||
# 1. Handle multiple protocols
|
||||
|
||||
Let's see _protocol multiplexing_ in action! You will need the following modules for this example: `libp2p`, `libp2p-tcp`, `peer-info`, `async` and `pull-stream`. This example reuses the base left by the [Transports](../transports) example. You can see the complete solution at [1.js](./1.js).
|
||||
Let's see _protocol multiplexing_ in action! You will need the following modules for this example: `libp2p`, `libp2p-tcp`, `peer-info`, `it-pipe`, `it-buffer` and `streaming-iterables`. This example reuses the base left by the [Transports](../transports) example. You can see the complete solution at [1.js](./1.js).
|
||||
|
||||
After creating the nodes, we need to tell libp2p which protocols to handle.
|
||||
|
||||
```JavaScript
|
||||
const pipe = require('it-pipe')
|
||||
const { map } = require('streaming-iterables')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
|
||||
// ...
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
// Here we are telling libp2p that is someone dials this node to talk with the `/your-protocol`
|
||||
// multicodec, the protocol identifier, please call this callback and give it the connection
|
||||
// Here we are telling libp2p that if someone dials this node to talk with the `/your-protocol`
|
||||
// multicodec, the protocol identifier, please call this handler and give it the stream
|
||||
// so that incomming data can be handled
|
||||
node2.handle('/your-protocol', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/your-protocol', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
source => (async function () {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
})()
|
||||
)
|
||||
})
|
||||
```
|
||||
@ -30,53 +37,54 @@ node2.handle('/your-protocol', (protocol, conn) => {
|
||||
After the protocol is _handled_, now we can dial to it.
|
||||
|
||||
```JavaScript
|
||||
node1.dialProtocol(node2.peerInfo, '/your-protocol', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['my own protocol, wow!']), conn)
|
||||
})
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, ['/your-protocol'])
|
||||
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
```
|
||||
|
||||
You might have seen this in the [Transports](../transports) examples. However, what it was not explained is that you can do more than exact string matching, for example, you can use semver.
|
||||
|
||||
```JavaScript
|
||||
node2.handle('/another-protocol/1.0.1', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/another-protocol/1.0.1', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
// ...
|
||||
node1.dialProtocol(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['semver me please']), conn)
|
||||
})
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, ['/another-protocol/1.0.0'])
|
||||
|
||||
await pipe(
|
||||
['my own protocol, wow!'],
|
||||
stream
|
||||
)
|
||||
```
|
||||
|
||||
This feature is super power for network protocols. It works in the same way as versioning your RPC/REST API, but for anything that goes in the wire. We had to use this feature to upgrade protocols within the IPFS Stack (i.e Bitswap) and we successfully managed to do so without any network splits.
|
||||
|
||||
There is still one last feature, you can create your custom match functions.
|
||||
There is still one last feature, you can provide multiple protocols for the same handler. If you have a backwards incompatible change, but it only requires minor changes to the code, you may prefer to do protocol checking instead of having multiple handlers
|
||||
|
||||
```JavaScript
|
||||
node2.handle('/custom-match-func', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
}, (myProtocol, requestedProtocol, callback) => {
|
||||
// This is all custom. I'm checking the base path matches, think of this
|
||||
// as a HTTP routing table.
|
||||
if (myProtocol.indexOf(requestedProtocol)) {
|
||||
callback(null, true)
|
||||
} else {
|
||||
callback(null, false)
|
||||
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => {
|
||||
if (protocol === '/another-protocol/2.0.0') {
|
||||
// handle backwards compatibility
|
||||
}
|
||||
})
|
||||
// ...
|
||||
node1.dialProtocol(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['do I fall into your criteria?']), conn)
|
||||
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
@ -84,77 +92,68 @@ Try all of this out by executing [1.js](./1.js).
|
||||
|
||||
# 2. Reuse existing connection
|
||||
|
||||
The example above would require a node to create a whole new connection for every time it dials in one of the protocols, this is a waste of resources and also it might be simply not possible (e.g lack of file descriptors, not enough ports being open, etc). What we really want is to dial a connection once and then multiplex several virtual connections (stream) over a single connection, this is where _stream multiplexing_ comes into play.
|
||||
The examples above would require a node to create a whole new connection for every time it dials in one of the protocols, this is a waste of resources and also it might be simply not possible (e.g lack of file descriptors, not enough ports being open, etc). What we really want is to dial a connection once and then multiplex several virtual connections (stream) over a single connection, this is where _stream multiplexing_ comes into play.
|
||||
|
||||
Stream multiplexing is a old concept, in fact it happens in many of the layers of the [OSI System](https://en.wikipedia.org/wiki/OSI_model), in libp2p we make this feature to our avail by letting the user pick which module for stream multiplexing to use.
|
||||
Stream multiplexing is an old concept, in fact it happens in many of the layers of the [OSI System](https://en.wikipedia.org/wiki/OSI_model). In libp2p, we make this feature to our avail by letting the user pick which module for stream multiplexing to use.
|
||||
|
||||
Currently, we have two available [libp2p-spdy](https://github.com/libp2p/js-libp2p-spdy) and [libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex) and pluging them in is as easy as adding another transport. Let's revisit our libp2p bundle.
|
||||
Currently, we have [libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex) and pluging it in is as easy as adding a transport. Let's revisit our libp2p configuration.
|
||||
|
||||
```JavaScript
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
//...
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
// Here we are adding the SPDY muxer to our libp2p bundle.
|
||||
// Thanks to protocol muxing, a libp2p bundle can support multiple Stream Muxers at the same
|
||||
// time and pick the right one when dialing to a node
|
||||
streamMuxer: [ SPDY ]
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
const createNode = () => {
|
||||
return Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ]
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
With this, we can dial as many times as we want to a peer and always reuse the same established underlying connection.
|
||||
|
||||
```JavaScript
|
||||
node2.handle('/a', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle(['/a', '/b'], ({ protocol, stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(`from: ${protocol}, msg: ${msg.toString()}`)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
node2.handle('/b', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, ['/a'])
|
||||
await pipe(
|
||||
['protocol (a)'],
|
||||
stream
|
||||
)
|
||||
|
||||
series([
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/a', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['protocol (a)']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['protocol (b)']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['another conn on protocol (b)']), conn)
|
||||
cb()
|
||||
})
|
||||
])
|
||||
const { stream: stream2 } = await node1.dialProtocol(node2.peerInfo, ['/b'])
|
||||
await pipe(
|
||||
['protocol (b)'],
|
||||
stream2
|
||||
)
|
||||
|
||||
const { stream: stream3 } = await node1.dialProtocol(node2.peerInfo, ['/b'])
|
||||
await pipe(
|
||||
['another stream on protocol (b)'],
|
||||
stream3
|
||||
)
|
||||
```
|
||||
|
||||
By running [2.js](./2.js) you should see the following result:
|
||||
|
||||
```
|
||||
> node 2.js
|
||||
protocol (a)
|
||||
protocol (b)
|
||||
another protocol (b)
|
||||
from: /a, msg: protocol (a)
|
||||
from: /b, msg: protocol (b)
|
||||
from: /b, msg: another stream on protocol (b)
|
||||
```
|
||||
|
||||
# 3. Bidirectional connections
|
||||
@ -168,8 +167,5 @@ You can see this working on example [3.js](./3.js). The result should look like
|
||||
```Bash
|
||||
> node 3.js
|
||||
from 1 to 2
|
||||
Addresses by which both peers are connected
|
||||
node 1 to node 2: /ip4/127.0.0.1/tcp/50629/p2p/QmZwMKTo6wG4Te9A6M2eJnWDpR8uhsGed4YRegnV5DcKiv
|
||||
node 2 to node 1: /ip4/127.0.0.1/tcp/50630/p2p/QmRgormJQeDyXhDKma11eUtksoh8vWmeBoxghVt4meauW9
|
||||
from 2 to 1
|
||||
```
|
||||
|
@ -1,103 +1,51 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ],
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [SECIO],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({
|
||||
peerInfo
|
||||
})
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
node1.once('peer:connect', (peer) => {
|
||||
console.log('connected to %s', peer.id.toB58String())
|
||||
|
||||
series([
|
||||
// node1 subscribes to "news"
|
||||
(cb) => node1.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 subscribes to "news"
|
||||
(cb) => node2.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 publishes "news" every second
|
||||
(cb) => {
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(
|
||||
'news',
|
||||
Buffer.from('Bird bird bird, bird is the word!'),
|
||||
(err) => {
|
||||
if (err) { throw err }
|
||||
}
|
||||
)
|
||||
}, 1000)
|
||||
cb()
|
||||
},
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const topic = 'news'
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode(),
|
||||
])
|
||||
|
||||
await node1.dial(node2.peerInfo)
|
||||
|
||||
await node1.pubsub.subscribe(topic, (msg) => {
|
||||
console.log(`node1 received: ${msg.data.toString()}`)
|
||||
})
|
||||
|
||||
await node2.pubsub.subscribe(topic, (msg) => {
|
||||
console.log(`node2 received: ${msg.data.toString()}`)
|
||||
})
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
})();
|
||||
|
@ -14,44 +14,47 @@ For this example, we will use MulticastDNS for automatic Peer Discovery. This ex
|
||||
|
||||
Using PubSub is super simple, you only need to provide the implementation of your choice and you are ready to go. No need for extra configuration.
|
||||
|
||||
```JavaScript
|
||||
node1.once('peer:connect', (peer) => {
|
||||
console.log('connected to %s', peer.id.toB58String())
|
||||
First, let's update our libp2p configuration with a pubsub implementation.
|
||||
|
||||
series([
|
||||
// node1 subscribes to "news"
|
||||
(cb) => node1.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 subscribes to "news"
|
||||
(cb) => node2.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 publishes "news" every second
|
||||
(cb) => {
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(
|
||||
'news',
|
||||
Buffer.from('Bird bird bird, bird is the word!'),
|
||||
(err) => {
|
||||
if (err) { throw err }
|
||||
}
|
||||
)
|
||||
}, 1000)
|
||||
cb()
|
||||
},
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
// we add the Pubsub module we want
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Once that is done, we only need to create a few libp2p nodes, connect them and everything is ready to start using pubsub.
|
||||
|
||||
```JavaScript
|
||||
const topic = 'news'
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
await node1.dial(node2.peerInfo)
|
||||
|
||||
await node1.pubsub.subscribe(topic, (msg) => {
|
||||
console.log(`node1 received: ${msg.data.toString()}`)
|
||||
})
|
||||
|
||||
await node2.pubsub.subscribe(topic, (msg) => {
|
||||
console.log(`node2 received: ${msg.data.toString()}`)
|
||||
})
|
||||
|
||||
// node2 publishes "news" every second
|
||||
setInterval(() => {
|
||||
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||
}, 1000)
|
||||
```
|
||||
|
||||
The output of the program should look like:
|
||||
|
||||
```
|
||||
@ -68,12 +71,6 @@ You can change the pubsub `emitSelf` option if you don't want the publishing nod
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: false
|
||||
|
@ -1,39 +1,33 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP
|
||||
]
|
||||
}
|
||||
const createNode = async (peerInfo) => {
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
;(async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
const node = await createNode(peerInfo)
|
||||
|
||||
console.log('node has started (true/false):', node.isStarted())
|
||||
console.log('listening on:')
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
})
|
||||
})();
|
||||
|
@ -1,39 +1,31 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP
|
||||
]
|
||||
}
|
||||
const pipe = require('it-pipe')
|
||||
const concat = require('it-concat')
|
||||
|
||||
const createNode = async (peerInfo) => {
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
function printAddrs (node, number) {
|
||||
@ -41,29 +33,31 @@ function printAddrs (node, number) {
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
;(async () => {
|
||||
const [peerInfo1, peerInfo2] = await Promise.all([
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create()
|
||||
])
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(peerInfo1),
|
||||
createNode(peerInfo2)
|
||||
])
|
||||
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
|
||||
node2.handle('/print', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/print', async ({ stream }) => {
|
||||
const result = await pipe(
|
||||
stream,
|
||||
concat
|
||||
)
|
||||
console.log(result.toString())
|
||||
})
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/print')
|
||||
|
||||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
||||
})
|
||||
})
|
||||
await pipe(
|
||||
['Hello', ' ', 'p2p', ' ', 'world', '!'],
|
||||
stream
|
||||
)
|
||||
})();
|
||||
|
@ -1,70 +1,62 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WebSockets
|
||||
]
|
||||
}
|
||||
}
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
function createNode (addrs, callback) {
|
||||
if (!Array.isArray(addrs)) {
|
||||
addrs = [addrs]
|
||||
const createNode = async (peerInfo, transports, multiaddrs = []) => {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
let node
|
||||
multiaddrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
node.start(cb)
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: transports,
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
function printAddrs (node, number) {
|
||||
function printAddrs(node, number) {
|
||||
console.log('node %s is listening on:', number)
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
}
|
||||
|
||||
function print (protocol, conn) {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
function print ({ stream }) {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode('/ip4/0.0.0.0/tcp/0', cb),
|
||||
(cb) => createNode(['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws'], cb),
|
||||
(cb) => createNode('/ip4/127.0.0.1/tcp/20000/ws', cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
;(async () => {
|
||||
const [peerInfo1, peerInfo2, peerInfo3] = await Promise.all([
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create()
|
||||
])
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(peerInfo1, [TCP], '/ip4/0.0.0.0/tcp/0'),
|
||||
createNode(peerInfo2, [TCP, WebSockets], ['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws']),
|
||||
createNode(peerInfo3, [WebSockets], '/ip4/127.0.0.1/tcp/20000/ws')
|
||||
])
|
||||
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
@ -74,21 +66,24 @@ parallel([
|
||||
node2.handle('/print', print)
|
||||
node3.handle('/print', print)
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
// node 1 (TCP) dials to node 2 (TCP+WebSockets)
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/print')
|
||||
await pipe(
|
||||
['node 1 dialed to node 2 successfully'],
|
||||
stream
|
||||
)
|
||||
|
||||
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
||||
})
|
||||
// node 2 (TCP+WebSockets) dials to node 2 (WebSockets)
|
||||
const { stream: stream2 } = await node2.dialProtocol(node3.peerInfo, '/print')
|
||||
await pipe(
|
||||
['node 2 dialed to node 3 successfully'],
|
||||
stream2
|
||||
)
|
||||
|
||||
node2.dialProtocol(node3.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
|
||||
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
||||
})
|
||||
|
||||
node3.dialProtocol(node1.peerInfo, '/print', (err, conn) => {
|
||||
if (err) {
|
||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||
}
|
||||
})
|
||||
})
|
||||
// node 3 (listening WebSockets) can dial node 1 (TCP)
|
||||
try {
|
||||
await node3.dialProtocol(node1.peerInfo, '/print')
|
||||
} catch (err) {
|
||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||
}
|
||||
})();
|
||||
|
@ -1,85 +1,65 @@
|
||||
# [Transports](http://libp2p.io/implementations/#transports)
|
||||
|
||||
libp2p doesn't make assumptions for you, instead, it enables you as the developer of the application to pick the modules you need to run your application, which can vary depending on the runtime you are executing. A libp2p node can use one or more Transports to dial and listen for Connections. These transports are modules that offer a clean interface for dialing and listening, defined by the [interface-transport](https://github.com/libp2p/interface-transport) specification. Some examples of possible transports are: TCP, UTP, WebRTC, QUIC, HTTP, Pigeon and so on.
|
||||
libp2p doesn't make assumptions for you, instead, it enables you as the developer of the application to pick the modules you need to run your application, which can vary depending on the runtime you are executing. A libp2p node can use one or more Transports to dial and listen for Connections. These transports are modules that offer a clean interface for dialing and listening, defined by the [interface-transport](https://github.com/libp2p/js-interfaces/tree/master/src/transport) specification. Some examples of possible transports are: TCP, UTP, WebRTC, QUIC, HTTP, Pigeon and so on.
|
||||
|
||||
A more complete definition of what is a transport can be found on the [interface-transport](https://github.com/libp2p/interface-transport) specification. A way to recognize a candidate transport is through the badge:
|
||||
A more complete definition of what is a transport can be found on the [interface-transport](https://github.com/libp2p/js-interfaces/tree/master/src/transport) specification. A way to recognize a candidate transport is through the badge:
|
||||
|
||||
[](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)
|
||||
|
||||
## 1. Creating a libp2p Bundle with TCP
|
||||
## 1. Creating a libp2p node with TCP
|
||||
|
||||
When using libp2p, you always want to create your own libp2p Bundle, that is, pick your set of modules and create your network stack with the properties you need. In this example, we will create a bundle with TCP. You can find the complete solution on the file [1.js](./1.js).
|
||||
When using libp2p, you need properly configure it, that is, pick your set of modules and create your network stack with the properties you need. In this example, we will create a libp2p node TCP. You can find the complete solution on the file [1.js](./1.js).
|
||||
|
||||
You will need 5 deps total, so go ahead and install all of them with:
|
||||
You will need 4 dependencies total, so go ahead and install all of them with:
|
||||
|
||||
```bash
|
||||
> npm install libp2p libp2p-tcp peer-info async @nodeutils/defaults-deep
|
||||
> npm install libp2p libp2p-tcp libp2p-secio peer-info
|
||||
```
|
||||
|
||||
Then, on your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
|
||||
|
||||
First thing is to create our own bundle! Insert:
|
||||
First thing is to create our own libp2p node! Insert:
|
||||
|
||||
```JavaScript
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('libp2p')
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const SECIO = require('libp2p-secio')
|
||||
|
||||
// This MyBundle class is your libp2p bundle packed with TCP
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
// modules is a JS object that will describe the components
|
||||
// we want for our libp2p bundle
|
||||
modules: {
|
||||
transport: [
|
||||
TCP
|
||||
]
|
||||
}
|
||||
const createNode = async (peerInfo) => {
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
connEncryption: [ SECIO ]
|
||||
}
|
||||
})
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
Now that we have our own MyBundle class that extends libp2p, let's create a node with it. We will use `async/waterfall` just for code structure, but you don't need to. Append to the same file:
|
||||
Now that we have a function to create our own libp2p node, let's create a node with it.
|
||||
|
||||
```JavaScript
|
||||
let node
|
||||
const peerInfo = await PeerInfo.create()
|
||||
const node = await createNode(peerInfo)
|
||||
|
||||
waterfall([
|
||||
// First we create a PeerInfo object, which will pack the
|
||||
// info about our peer. Creating a PeerInfo is an async
|
||||
// operation because we use the WebCrypto API
|
||||
// (yeei Universal JS)
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
// Now we can create a node with that PeerInfo object
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
// Last, we start the node!
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
|
||||
// At this point the node has started
|
||||
console.log('node has started (true/false):', node.isStarted())
|
||||
// And we can print the now listening addresses.
|
||||
// If you are familiar with TCP, you might have noticed
|
||||
// that we specified the node to listen in 0.0.0.0 and port
|
||||
// 0, which means "listen in any network interface and pick
|
||||
// a port for me
|
||||
console.log('listening on:')
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
})
|
||||
// At this point the node has started
|
||||
console.log('node has started (true/false):', node.isStarted())
|
||||
// And we can print the now listening addresses.
|
||||
// If you are familiar with TCP, you might have noticed
|
||||
// that we specified the node to listen in 0.0.0.0 and port
|
||||
// 0, which means "listen in any network interface and pick
|
||||
// a port for me
|
||||
console.log('listening on:')
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
```
|
||||
|
||||
Running this should result in something like:
|
||||
@ -96,78 +76,64 @@ That `QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ` is the PeerId that was cre
|
||||
|
||||
## 2. Dialing from one node to another node
|
||||
|
||||
Now that we have our bundle, let's create two nodes and make them dial to each other! You can find the complete solution at [2.js](./2.js).
|
||||
Now that we have our `createNode` function, let's create two nodes and make them dial to each other! You can find the complete solution at [2.js](./2.js).
|
||||
|
||||
For this step, we will need one more dependency.
|
||||
|
||||
```bash
|
||||
> npm install pull-stream
|
||||
> npm install it-pipe it-buffer
|
||||
```
|
||||
|
||||
And we also need to import the module on our .js file:
|
||||
|
||||
```js
|
||||
const pull = require('pull-stream')
|
||||
const pipe = require('it-pipe')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
```
|
||||
|
||||
We are going to reuse the MyBundle class from step 1, but this time to make things simpler, we will create two functions, one to create nodes and another to print the addrs to avoid duplicating code.
|
||||
We are going to reuse the `createNode` function from step 1, but this time to make things simpler, we will create another function to print the addrs to avoid duplicating code.
|
||||
|
||||
```JavaScript
|
||||
function createNode (callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
function printAddrs (node, number) {
|
||||
console.log('node %s is listening on:', number)
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Now we are going to use `async/parallel` to create two nodes, print their addresses and dial from one node to the other. We already added `async` as a dependency, but still need to import `async/parallel`:
|
||||
|
||||
```js
|
||||
const parallel = require('async/parallel')
|
||||
```
|
||||
|
||||
Then,
|
||||
|
||||
```js
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
;(async () => {
|
||||
const [peerInfo1, peerInfo2] = await Promise.all([
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create()
|
||||
])
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
|
||||
node2.handle('/print', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/print', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/print')
|
||||
|
||||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
||||
})
|
||||
})
|
||||
await pipe(
|
||||
['Hello', ' ', 'p2p', ' ', 'world', '!'],
|
||||
stream
|
||||
)
|
||||
})();
|
||||
```
|
||||
|
||||
The result should be look like:
|
||||
@ -195,46 +161,28 @@ In this example, we will need to also install `libp2p-websockets`, go ahead and
|
||||
> npm install libp2p-websockets
|
||||
```
|
||||
|
||||
We want to create 3 nodes, one with TCP, one with TCP+WebSockets and one with just WebSockets. We need to update our `MyBundle` class to contemplate WebSockets as well:
|
||||
We want to create 3 nodes, one with TCP, one with TCP+WebSockets and one with just WebSockets. We need to update our `createNode` function to contemplate WebSockets as well. Moreover, let's upgrade our function to enable us to pick the addrs in which a node will start a listener:
|
||||
|
||||
```JavaScript
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
// ...
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WebSockets
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now that we have our bundle ready, let's upgrade our createNode function to enable us to pick the addrs in which a node will start a listener.
|
||||
|
||||
```JavaScript
|
||||
function createNode (addrs, callback) {
|
||||
if (!Array.isArray(addrs)) {
|
||||
addrs = [addrs]
|
||||
const createNode = async (peerInfo, transports, multiaddrs = []) => {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
let node
|
||||
multiaddrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerInfo.create(cb),
|
||||
(peerInfo, cb) => {
|
||||
addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
node = new MyBundle({ peerInfo: peerInfo })
|
||||
node.start(cb)
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: transports,
|
||||
connEncryption: [ SECIO ]
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
@ -243,58 +191,61 @@ As a rule, a libp2p node will only be capable of using a transport if: a) it has
|
||||
Let's update our flow to create nodes and see how they behave when dialing to each other:
|
||||
|
||||
```JavaScript
|
||||
parallel([
|
||||
(cb) => createNode('/ip4/0.0.0.0/tcp/0', cb),
|
||||
// Here we add an extra multiaddr that has a /ws at the end, this means that we want
|
||||
// to create a TCP socket, but mount it as WebSockets instead.
|
||||
(cb) => createNode(['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws'], cb),
|
||||
(cb) => createNode('/ip4/127.0.0.1/tcp/20000/ws', cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const TCP = require('libp2p-tcp')
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
const node3 = nodes[2]
|
||||
const [peerInfo1, peerInfo2, peerInfo3] = await Promise.all([
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create()
|
||||
])
|
||||
const [node1, node2, node3] = await Promise.all([
|
||||
createNode(peerInfo1, [TCP], '/ip4/0.0.0.0/tcp/0'),
|
||||
createNode(peerInfo2, [TCP, WebSockets], ['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws']),
|
||||
createNode(peerInfo3, [WebSockets], '/ip4/127.0.0.1/tcp/20000/ws')
|
||||
])
|
||||
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
printAddrs(node3, '3')
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
printAddrs(node3, '3')
|
||||
|
||||
node1.handle('/print', print)
|
||||
node2.handle('/print', print)
|
||||
node3.handle('/print', print)
|
||||
node1.handle('/print', print)
|
||||
node2.handle('/print', print)
|
||||
node3.handle('/print', print)
|
||||
|
||||
// node 1 (TCP) dials to node 2 (TCP+WebSockets)
|
||||
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
// node 1 (TCP) dials to node 2 (TCP+WebSockets)
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/print')
|
||||
await pipe(
|
||||
['node 1 dialed to node 2 successfully'],
|
||||
stream
|
||||
)
|
||||
|
||||
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
||||
})
|
||||
// node 2 (TCP+WebSockets) dials to node 2 (WebSockets)
|
||||
const { stream: stream2 } = await node2.dialProtocol(node3.peerInfo, '/print')
|
||||
await pipe(
|
||||
['node 2 dialed to node 3 successfully'],
|
||||
stream2
|
||||
)
|
||||
|
||||
// node 2 (TCP+WebSockets) dials to node 2 (WebSockets)
|
||||
node2.dialProtocol(node3.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
|
||||
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
||||
})
|
||||
|
||||
// node 3 (WebSockets) attempts to dial to node 1 (TCP)
|
||||
node3.dialProtocol(node1.peerInfo, '/print', (err, conn) => {
|
||||
if (err) {
|
||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||
}
|
||||
})
|
||||
})
|
||||
// node 3 (WebSockets) attempts to dial to node 1 (TCP)
|
||||
try {
|
||||
await node3.dialProtocol(node1.peerInfo, '/print')
|
||||
} catch (err) {
|
||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||
}
|
||||
```
|
||||
|
||||
`print` is a function created using the code from 2.js, but factored into its own function to save lines, here it is:
|
||||
|
||||
```JavaScript
|
||||
function print (protocol, conn) {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
function print ({ stream }) {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
```
|
||||
@ -312,18 +263,19 @@ node 2 is listening on:
|
||||
/ip4/192.168.2.156/tcp/62619/p2p/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
node 3 is listening on:
|
||||
/ip4/127.0.0.1/tcp/20000/ws/p2p/QmVq1PWh3VSDYdFqYMtqp4YQyXcrH27N7968tGdM1VQPj1
|
||||
node 3 failed to dial to node 1 with: No available transport to dial to
|
||||
node 1 dialed to node 2 successfully
|
||||
node 2 dialed to node 3 successfully
|
||||
node 3 failed to dial to node 1 with:
|
||||
Error: No transport available for address /ip4/127.0.0.1/tcp/51482
|
||||
```
|
||||
|
||||
As expected, we created 3 nodes, node 1 with TCP, node 2 with TCP+WebSockets and node 3 with just WebSockets. node 1 -> node 2 and node 2 -> node 3 managed to dial correctly because they shared a common transport, however, node 3 -> node 1 failed because they didn't share any.
|
||||
|
||||
## 4. How to create a new libp2p transport
|
||||
|
||||
Today there are already 3 transports available, one in the works and plenty to come, you can find these at [interface-transport implementations](https://github.com/libp2p/interface-transport#modules-that-implement-the-interface) list.
|
||||
Today there are already several transports available and plenty to come, you can find these at [interface-transport implementations](https://github.com/libp2p/js-interfaces/tree/master/src/transport#modules-that-implement-the-interface) list.
|
||||
|
||||
Adding more transports is done through the same way as you added TCP and WebSockets. Some transports might offer extra functionalities, but as far as libp2p is concerned, if it follows the interface defined at the [spec](https://github.com/libp2p/interface-transport#api) it will be able to use it.
|
||||
Adding more transports is done through the same way as you added TCP and WebSockets. Some transports might offer extra functionalities, but as far as libp2p is concerned, if it follows the interface defined at the [spec](https://github.com/libp2p/js-interfaces/tree/master/src/transport#api) it will be able to use it.
|
||||
|
||||
If you decide to implement a transport yourself, please consider adding to the list so that others can use it as well.
|
||||
|
||||
|
@ -51,7 +51,6 @@
|
||||
["libp2p/js-libp2p-crypto-secp256k1", "libp2p-crypto-secp256k1"],
|
||||
|
||||
"data types",
|
||||
["libp2p/js-peer-book", "peer-book"],
|
||||
["libp2p/js-peer-id", "peer-id"],
|
||||
["libp2p/js-peer-info", "peer-info"],
|
||||
|
||||
|
105
package.json
105
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.26.2",
|
||||
"version": "0.27.0-rc.0",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
@ -11,8 +11,8 @@
|
||||
"scripts": {
|
||||
"lint": "aegir lint",
|
||||
"build": "aegir build",
|
||||
"test": "aegir test -t node -t browser",
|
||||
"test:node": "aegir test -t node",
|
||||
"test": "npm run test:node && npm run test:browser",
|
||||
"test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
|
||||
"test:browser": "aegir test -t browser",
|
||||
"release": "aegir release -t node -t browser",
|
||||
"release-minor": "aegir release --type minor -t node -t browser",
|
||||
@ -36,80 +36,74 @@
|
||||
},
|
||||
"homepage": "https://libp2p.io",
|
||||
"license": "MIT",
|
||||
"browser": {
|
||||
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0",
|
||||
"npm": ">=6.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.6.2",
|
||||
"abort-controller": "^3.0.0",
|
||||
"aggregate-error": "^3.0.1",
|
||||
"any-signal": "^1.1.0",
|
||||
"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",
|
||||
"it-all": "^1.0.1",
|
||||
"it-buffer": "^0.1.1",
|
||||
"it-handshake": "^1.0.1",
|
||||
"it-length-prefixed": "^3.0.0",
|
||||
"it-pipe": "^1.1.0",
|
||||
"it-protocol-buffers": "^0.2.0",
|
||||
"latency-monitor": "~0.2.1",
|
||||
"libp2p-crypto": "^0.16.2",
|
||||
"libp2p-websockets": "^0.12.2",
|
||||
"mafmt": "^6.0.7",
|
||||
"merge-options": "^1.0.1",
|
||||
"libp2p-crypto": "^0.17.1",
|
||||
"libp2p-interfaces": "^0.2.3",
|
||||
"mafmt": "^7.0.0",
|
||||
"merge-options": "^2.0.0",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^6.1.0",
|
||||
"multistream-select": "~0.14.6",
|
||||
"once": "^1.4.0",
|
||||
"peer-book": "^0.9.1",
|
||||
"peer-id": "^0.12.2",
|
||||
"peer-info": "~0.15.1",
|
||||
"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",
|
||||
"multiaddr": "^7.2.1",
|
||||
"multistream-select": "^0.15.0",
|
||||
"mutable-proxy": "^1.0.0",
|
||||
"p-any": "^2.1.0",
|
||||
"p-fifo": "^1.0.0",
|
||||
"p-settle": "^3.1.0",
|
||||
"peer-id": "^0.13.4",
|
||||
"peer-info": "^0.17.0",
|
||||
"protons": "^1.0.1",
|
||||
"retimer": "^2.0.0",
|
||||
"superstruct": "^0.6.0",
|
||||
"timeout-abort-controller": "^1.0.0",
|
||||
"xsalsa20": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"aegir": "^20.0.0",
|
||||
"abortable-iterator": "^2.1.0",
|
||||
"aegir": "^20.5.1",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"cids": "^0.7.1",
|
||||
"delay": "^4.3.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"electron-webrtc": "^0.3.0",
|
||||
"interface-datastore": "^0.6.0",
|
||||
"libp2p-bootstrap": "^0.9.7",
|
||||
"libp2p-delegated-content-routing": "^0.2.2",
|
||||
"libp2p-delegated-peer-routing": "^0.2.2",
|
||||
"libp2p-floodsub": "~0.17.0",
|
||||
"libp2p-gossipsub": "~0.0.4",
|
||||
"libp2p-kad-dht": "^0.15.3",
|
||||
"libp2p-mdns": "^0.12.3",
|
||||
"libp2p-mplex": "^0.8.4",
|
||||
"libp2p-pnet": "~0.1.0",
|
||||
"libp2p-secio": "^0.11.1",
|
||||
"libp2p-spdy": "^0.13.2",
|
||||
"libp2p-tcp": "^0.13.0",
|
||||
"libp2p-webrtc-star": "^0.16.1",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websocket-star-rendezvous": "~0.4.1",
|
||||
"lodash.times": "^4.3.2",
|
||||
"it-concat": "^1.0.0",
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pushable": "^1.4.0",
|
||||
"libp2p-bootstrap": "^0.10.3",
|
||||
"libp2p-delegated-content-routing": "^0.4.1",
|
||||
"libp2p-delegated-peer-routing": "^0.4.0",
|
||||
"libp2p-floodsub": "^0.20.0",
|
||||
"libp2p-gossipsub": "^0.2.0",
|
||||
"libp2p-kad-dht": "^0.18.2",
|
||||
"libp2p-mdns": "^0.13.0",
|
||||
"libp2p-mplex": "^0.9.1",
|
||||
"libp2p-secio": "^0.12.1",
|
||||
"libp2p-tcp": "^0.14.1",
|
||||
"libp2p-webrtc-star": "^0.17.0",
|
||||
"libp2p-websockets": "^0.13.1",
|
||||
"nock": "^10.0.6",
|
||||
"portfinder": "^1.0.20",
|
||||
"pull-goodbye": "0.0.2",
|
||||
"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",
|
||||
"sinon": "^7.2.7",
|
||||
"p-defer": "^3.0.0",
|
||||
"p-times": "^2.1.0",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"sinon": "^8.1.0",
|
||||
"streaming-iterables": "^4.1.0",
|
||||
"wrtc": "^0.4.1"
|
||||
},
|
||||
"contributors": [
|
||||
@ -122,6 +116,7 @@
|
||||
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"Didrik Nordström <didrik.nordstrom@gmail.com>",
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
@ -158,10 +153,12 @@
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"a1300 <a1300@users.noreply.github.com>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
"isan_rivkin <isanrivkin@gmail.com>",
|
||||
"mayerwin <mayerwin@users.noreply.github.com>",
|
||||
"phillmac <phillmac@users.noreply.github.com>",
|
||||
"swedneck <40505480+swedneck@users.noreply.github.com>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
|
||||
]
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 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.
|
||||
> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/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.
|
||||
|
||||
@ -24,15 +24,16 @@ Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes
|
||||
|
||||
## 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)
|
||||
- [js-libp2p-circuit](#js-libp2p-circuit)
|
||||
- [Why?](#why)
|
||||
- [libp2p-circuit and IPFS](#libp2p-circuit-and-ipfs)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Usage](#usage)
|
||||
- [Example](#example)
|
||||
- [Create dialer/listener](#create-dialerlistener)
|
||||
- [Create `relay`](#create-relay)
|
||||
- [API](#api)
|
||||
- [Implementation rational](#implementation-rational)
|
||||
|
||||
## Usage
|
||||
|
||||
@ -45,7 +46,7 @@ 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 mh1 = multiaddr('/p2p-circuit/p2p/QmHash') // dial /p2p/QmHash over any circuit
|
||||
|
||||
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
|
||||
|
||||
@ -88,37 +89,13 @@ 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://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`
|
||||
`/p2p-circuit/ip4/127.0.0.1/tcp/4001/p2p/QmHash`
|
||||
|
||||
Both for dialing and listening.
|
||||
|
||||
|
@ -1,126 +0,0 @@
|
||||
'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
|
@ -1,275 +0,0 @@
|
||||
'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
|
@ -1,283 +1,136 @@
|
||||
'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 { validateAddrs } = 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 { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
const pipe = require('it-pipe')
|
||||
const errCode = require('err-code')
|
||||
const { codes: Errors } = require('../../errors')
|
||||
|
||||
const { stop } = require('./stop')
|
||||
|
||||
const multicodec = require('./../multicodec')
|
||||
|
||||
const log = debug('libp2p:circuit:relay')
|
||||
log.err = debug('libp2p:circuit:error:relay')
|
||||
const log = debug('libp2p:circuit:hop')
|
||||
log.error = debug('libp2p:circuit:hop:error')
|
||||
|
||||
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'))
|
||||
module.exports.handleHop = async function handleHop ({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
}) {
|
||||
// Ensure hop is enabled
|
||||
if (!circuit._options.hop.enabled) {
|
||||
log('HOP request received but we are not acting as a relay')
|
||||
return streamHandler.end({
|
||||
type: CircuitPB.Type.STATUS,
|
||||
code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
// Validate the HOP request has the required input
|
||||
try {
|
||||
validateAddrs(request, streamHandler)
|
||||
} catch (err) {
|
||||
return log.error('invalid hop request via peer %s', connection.remotePeer.toB58String(), err)
|
||||
}
|
||||
|
||||
return this.utils.writeResponse(
|
||||
srcSh,
|
||||
proto.Status.SUCCESS,
|
||||
(err) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
return callback(null, dstConn)
|
||||
})
|
||||
// Get the connection to the destination (stop) peer
|
||||
const destinationPeer = new PeerId(request.dstPeer.id)
|
||||
|
||||
const destinationConnection = circuit._registrar.getConnection(new PeerInfo(destinationPeer))
|
||||
if (!destinationConnection && !circuit._options.hop.active) {
|
||||
log('HOP request received but we are not connected to the destination peer')
|
||||
return streamHandler.end({
|
||||
type: CircuitPB.Type.STATUS,
|
||||
code: CircuitPB.Status.HOP_NO_CONN_TO_DST
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
// TODO: Handle being an active relay
|
||||
|
||||
// Handle the incoming HOP request by performing a STOP request
|
||||
const stopRequest = {
|
||||
type: CircuitPB.Type.STOP,
|
||||
dstPeer: request.dstPeer,
|
||||
srcPeer: request.srcPeer
|
||||
}
|
||||
|
||||
let destinationStream
|
||||
try {
|
||||
destinationStream = await stop({
|
||||
connection: destinationConnection,
|
||||
request: stopRequest,
|
||||
circuit
|
||||
})
|
||||
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)
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
return log.error(err)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
}
|
||||
log('hop request from %s is valid', connection.remotePeer.toB58String())
|
||||
streamHandler.write({
|
||||
type: CircuitPB.Type.STATUS,
|
||||
code: CircuitPB.Status.SUCCESS
|
||||
})
|
||||
const sourceStream = streamHandler.rest()
|
||||
|
||||
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)
|
||||
}))
|
||||
}
|
||||
// Short circuit the two streams to create the relayed connection
|
||||
return pipe(
|
||||
sourceStream,
|
||||
destinationStream,
|
||||
sourceStream
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = Hop
|
||||
/**
|
||||
* Performs a HOP request to a relay peer, to request a connection to another
|
||||
* peer. A new, virtual, connection will be created between the two via the relay.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {Connection} options.connection Connection to the relay
|
||||
* @param {*} options.request
|
||||
* @param {Circuit} options.circuit
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
module.exports.hop = async function hop ({
|
||||
connection,
|
||||
request
|
||||
}) {
|
||||
// Create a new stream to the relay
|
||||
const { stream } = await connection.newStream([multicodec.relay])
|
||||
// Send the HOP request
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
streamHandler.write(request)
|
||||
|
||||
const response = await streamHandler.read()
|
||||
|
||||
if (response.code === CircuitPB.Status.SUCCESS) {
|
||||
log('hop request was successful')
|
||||
return streamHandler.rest()
|
||||
}
|
||||
|
||||
log('hop request failed with code %d, closing stream', response.code)
|
||||
streamHandler.close()
|
||||
throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an unencoded CAN_HOP response based on the Circuits configuration
|
||||
* @private
|
||||
*/
|
||||
module.exports.handleCanHop = function handleCanHop ({
|
||||
connection,
|
||||
streamHandler,
|
||||
circuit
|
||||
}) {
|
||||
const canHop = circuit._options.hop.enabled
|
||||
log('can hop (%s) request from %s', canHop, connection.remotePeer.toB58String())
|
||||
streamHandler.end({
|
||||
type: CircuitPB.Type.STATUS,
|
||||
code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
||||
})
|
||||
}
|
||||
|
@ -1,56 +1,69 @@
|
||||
'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 { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
const multicodec = require('../multicodec')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
const { validateAddrs } = require('./utils')
|
||||
|
||||
const debug = require('debug')
|
||||
|
||||
const log = debug('libp2p:circuit:stop')
|
||||
log.err = debug('libp2p:circuit:error:stop')
|
||||
log.error = debug('libp2p:circuit:stop:error')
|
||||
|
||||
class Stop extends EE {
|
||||
constructor (swarm) {
|
||||
super()
|
||||
this.swarm = swarm
|
||||
this.utils = utilsFactory(swarm)
|
||||
/**
|
||||
* Handles incoming STOP requests
|
||||
*
|
||||
* @private
|
||||
* @param {*} options
|
||||
* @param {Connection} options.connection
|
||||
* @param {*} options.request The CircuitRelay protobuf request (unencoded)
|
||||
* @param {StreamHandler} options.streamHandler
|
||||
* @returns {Promise<*>} Resolves a duplex iterable
|
||||
*/
|
||||
module.exports.handleStop = function handleStop ({
|
||||
connection,
|
||||
request,
|
||||
streamHandler
|
||||
}) {
|
||||
// Validate the STOP request has the required input
|
||||
try {
|
||||
validateAddrs(request, streamHandler)
|
||||
} catch (err) {
|
||||
return log.error('invalid stop request via peer %s', connection.remotePeer.toB58String(), err)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
})
|
||||
}
|
||||
// The request is valid
|
||||
log('stop request is valid')
|
||||
streamHandler.write({
|
||||
type: CircuitPB.Type.STATUS,
|
||||
code: CircuitPB.Status.SUCCESS
|
||||
})
|
||||
return streamHandler.rest()
|
||||
}
|
||||
|
||||
module.exports = Stop
|
||||
/**
|
||||
* Creates a STOP request
|
||||
* @private
|
||||
* @param {*} options
|
||||
* @param {Connection} options.connection
|
||||
* @param {*} options.request The CircuitRelay protobuf request (unencoded)
|
||||
* @returns {Promise<*>} Resolves a duplex iterable
|
||||
*/
|
||||
module.exports.stop = async function stop ({
|
||||
connection,
|
||||
request
|
||||
}) {
|
||||
const { stream } = await connection.newStream([multicodec.relay])
|
||||
log('starting stop request to %s', connection.remotePeer.toB58String())
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
|
||||
streamHandler.write(request)
|
||||
const response = await streamHandler.read()
|
||||
|
||||
if (response.code === CircuitPB.Status.SUCCESS) {
|
||||
log('stop request to %s was successful', connection.remotePeer.toB58String())
|
||||
return streamHandler.rest()
|
||||
}
|
||||
|
||||
log('stop request failed with code %d', response.code)
|
||||
streamHandler.close()
|
||||
}
|
||||
|
@ -1,139 +1,79 @@
|
||||
'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 lp = require('it-length-prefixed')
|
||||
const handshake = require('it-handshake')
|
||||
const { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:stream-handler')
|
||||
log.err = debug('libp2p:circuit:error:stream-handler')
|
||||
log.error = debug('libp2p:circuit:stream-handler:error')
|
||||
|
||||
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
|
||||
* @param {object} options
|
||||
* @param {*} options.stream - A duplex iterable
|
||||
* @param {Number} options.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
|
||||
constructor ({ stream, maxLength = 4096 }) {
|
||||
this.stream = stream
|
||||
|
||||
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
|
||||
this.shake = handshake(this.stream)
|
||||
this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength })
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and decode message
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @returns {void|Function}
|
||||
* @async
|
||||
* @returns {void}
|
||||
*/
|
||||
read (cb) {
|
||||
if (!this.isValid()) {
|
||||
return cb(new Error('handler is not in a valid state'))
|
||||
async read () {
|
||||
const msg = await this.decoder.next()
|
||||
if (msg.value) {
|
||||
const value = CircuitPB.decode(msg.value.slice())
|
||||
log('read message type', value.type)
|
||||
return value
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
log('read received no value, closing stream')
|
||||
// End the stream, we didn't get data
|
||||
this.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode and write array of buffers
|
||||
*
|
||||
* @param {Buffer[]} msg
|
||||
* @param {Function} [cb]
|
||||
* @returns {Function}
|
||||
* @param {*} msg An unencoded CircuitRelay protobuf message
|
||||
*/
|
||||
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
|
||||
write (msg) {
|
||||
log('write message type %s', msg.type)
|
||||
this.shake.write(lp.encode.single(CircuitPB.encode(msg)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the handshake rest stream and invalidate handler
|
||||
*
|
||||
* @return {*|{source, sink}}
|
||||
* @return {*} A duplex iterable
|
||||
*/
|
||||
rest () {
|
||||
const rest = this.shake.rest()
|
||||
this.shake.rest()
|
||||
return this.shake.stream
|
||||
}
|
||||
|
||||
this.conn = null
|
||||
this.stream = null
|
||||
this.shake = null
|
||||
return rest
|
||||
end (msg) {
|
||||
this.write(msg)
|
||||
this.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream
|
||||
*
|
||||
* @returns {undefined}
|
||||
* @returns {void}
|
||||
*/
|
||||
close () {
|
||||
if (!this.isValid()) {
|
||||
return
|
||||
}
|
||||
|
||||
// close stream
|
||||
pull(
|
||||
empty(),
|
||||
this.rest()
|
||||
)
|
||||
log('closing the stream')
|
||||
this.rest().sink([])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,118 +1,51 @@
|
||||
'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')
|
||||
const { CircuitRelay } = require('../protocol')
|
||||
|
||||
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()
|
||||
}
|
||||
/**
|
||||
* Write a response
|
||||
*
|
||||
* @param {StreamHandler} streamHandler
|
||||
* @param {CircuitRelay.Status} status
|
||||
*/
|
||||
function writeResponse (streamHandler, status) {
|
||||
streamHandler.write({
|
||||
type: CircuitRelay.Type.STATUS,
|
||||
code: status
|
||||
})
|
||||
}
|
||||
|
||||
return b58Id
|
||||
/**
|
||||
* Validate incomming HOP/STOP message
|
||||
*
|
||||
* @param {*} msg A CircuitRelay unencoded protobuf message
|
||||
* @param {StreamHandler} streamHandler
|
||||
*/
|
||||
function validateAddrs (msg, streamHandler) {
|
||||
try {
|
||||
msg.dstPeer.addrs.forEach((addr) => {
|
||||
return multiaddr(addr)
|
||||
})
|
||||
} catch (err) {
|
||||
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
|
||||
? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
|
||||
: CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
|
||||
throw err
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
try {
|
||||
msg.srcPeer.addrs.forEach((addr) => {
|
||||
return multiaddr(addr)
|
||||
})
|
||||
} catch (err) {
|
||||
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
|
||||
? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
|
||||
: CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validateAddrs
|
||||
}
|
||||
|
@ -1,3 +1,186 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = require('./circuit')
|
||||
const mafmt = require('mafmt')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const withIs = require('class-is')
|
||||
const { CircuitRelay: CircuitPB } = require('./protocol')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit')
|
||||
log.error = debug('libp2p:circuit:error')
|
||||
|
||||
const { relay: multicodec } = require('./multicodec')
|
||||
const createListener = require('./listener')
|
||||
const { handleCanHop, handleHop, hop } = require('./circuit/hop')
|
||||
const { handleStop } = require('./circuit/stop')
|
||||
const StreamHandler = require('./circuit/stream-handler')
|
||||
const toConnection = require('./stream-to-conn')
|
||||
|
||||
class Circuit {
|
||||
/**
|
||||
* Creates an instance of Circuit.
|
||||
*
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {Libp2p} options.libp2p
|
||||
* @param {Upgrader} options.upgrader
|
||||
*/
|
||||
constructor ({ libp2p, upgrader }) {
|
||||
this._dialer = libp2p.dialer
|
||||
this._registrar = libp2p.registrar
|
||||
this._upgrader = upgrader
|
||||
this._options = libp2p._config.relay
|
||||
this.peerInfo = libp2p.peerInfo
|
||||
this._registrar.handle(multicodec, this._onProtocol.bind(this))
|
||||
}
|
||||
|
||||
async _onProtocol ({ connection, stream, protocol }) {
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
const request = await streamHandler.read()
|
||||
const circuit = this
|
||||
let virtualConnection
|
||||
|
||||
switch (request.type) {
|
||||
case CircuitPB.Type.CAN_HOP: {
|
||||
log('received CAN_HOP request from %s', connection.remotePeer.toB58String())
|
||||
await handleCanHop({ circuit, connection, streamHandler })
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.HOP: {
|
||||
log('received HOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleHop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
})
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.STOP: {
|
||||
log('received STOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleStop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
})
|
||||
break
|
||||
}
|
||||
default: {
|
||||
log('Request of type %s not supported', request.type)
|
||||
}
|
||||
}
|
||||
|
||||
if (virtualConnection) {
|
||||
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
|
||||
const localAddr = multiaddr(request.srcPeer.addrs[0])
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr,
|
||||
localAddr
|
||||
})
|
||||
const type = CircuitPB.Type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
||||
log('new %s connection %s', type, maConn.remoteAddr)
|
||||
|
||||
const conn = await this._upgrader.upgradeInbound(maConn)
|
||||
log('%s connection %s upgraded', type, maConn.remoteAddr)
|
||||
this.handler && this.handler(conn)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
*
|
||||
* @param {multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Object} options - dial options
|
||||
* @param {AbortSignal} [options.signal] - An optional abort signal
|
||||
* @returns {Connection} - the connection
|
||||
*/
|
||||
async dial (ma, options) {
|
||||
// Check the multiaddr to see if it contains a relay and a destination peer
|
||||
const addrs = ma.toString().split('/p2p-circuit')
|
||||
const relayAddr = multiaddr(addrs[0])
|
||||
const destinationAddr = multiaddr(addrs[addrs.length - 1])
|
||||
const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
|
||||
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
|
||||
|
||||
let disconnectOnFailure = false
|
||||
let relayConnection = this._registrar.getConnection(new PeerInfo(relayPeer))
|
||||
if (!relayConnection) {
|
||||
relayConnection = await this._dialer.connectToPeer(relayAddr, options)
|
||||
disconnectOnFailure = true
|
||||
}
|
||||
|
||||
try {
|
||||
const virtualConnection = await hop({
|
||||
connection: relayConnection,
|
||||
circuit: this,
|
||||
request: {
|
||||
type: CircuitPB.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.peerInfo.id.toBytes(),
|
||||
addrs: this.peerInfo.multiaddrs.toArray().map(addr => addr.buffer)
|
||||
},
|
||||
dstPeer: {
|
||||
id: destinationPeer.toBytes(),
|
||||
addrs: [multiaddr(destinationAddr).buffer]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerInfo.id.toB58String()}`)
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr: ma,
|
||||
localAddr
|
||||
})
|
||||
log('new outbound connection %s', maConn.remoteAddr)
|
||||
|
||||
return this._upgrader.upgradeOutbound(maConn)
|
||||
} catch (err) {
|
||||
log.error('Circuit relay dial failed', err)
|
||||
disconnectOnFailure && await relayConnection.close()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener
|
||||
*
|
||||
* @param {any} options
|
||||
* @param {Function} handler
|
||||
* @return {listener}
|
||||
*/
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
// Called on successful HOP and STOP requests
|
||||
this.handler = handler
|
||||
|
||||
return createListener(this, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter check for all Multiaddrs that this transport can dial on
|
||||
*
|
||||
* @param {Array<Multiaddr>} multiaddrs
|
||||
* @returns {Array<Multiaddr>}
|
||||
*/
|
||||
filter (multiaddrs) {
|
||||
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
|
||||
|
||||
return multiaddrs.filter((ma) => {
|
||||
return mafmt.Circuit.matches(ma)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Circuit}
|
||||
*/
|
||||
module.exports = withIs(Circuit, { className: 'Circuit', symbolName: '@libp2p/js-libp2p-circuit/circuit' })
|
||||
|
@ -1,94 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
const multicodec = require('./multicodec')
|
||||
const EE = require('events').EventEmitter
|
||||
const EventEmitter = require('events')
|
||||
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)
|
||||
/**
|
||||
* @param {*} circuit
|
||||
* @returns {Listener} a transport listener
|
||||
*/
|
||||
module.exports = (circuit) => {
|
||||
const listener = new EventEmitter()
|
||||
const listeningAddrs = new Map()
|
||||
|
||||
/**
|
||||
* Add swarm handler and listen for incoming connections
|
||||
*
|
||||
* @param {Multiaddr} ma
|
||||
* @param {Function} callback
|
||||
* @param {Multiaddr} addr
|
||||
* @return {void}
|
||||
*/
|
||||
listener.listen = (ma, callback) => {
|
||||
callback = callback || (() => {})
|
||||
listener.listen = async (addr) => {
|
||||
const [addrString] = String(addr).split('/p2p-circuit').slice(-1)
|
||||
|
||||
swarm.handle(multicodec.relay, (_, conn) => {
|
||||
const sh = new StreamHandler(conn)
|
||||
const relayConn = await circuit._dialer.connectToPeer(multiaddr(addrString))
|
||||
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
||||
|
||||
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()
|
||||
listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)
|
||||
listener.emit('listening')
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove swarm listener
|
||||
* TODO: Remove the peers from our topology
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @return {void}
|
||||
*/
|
||||
listener.close = (cb) => {
|
||||
swarm.unhandle(multicodec.relay)
|
||||
setImmediate(() => listener.emit('close'))
|
||||
cb()
|
||||
}
|
||||
listener.close = () => {}
|
||||
|
||||
/**
|
||||
* Get fixed up multiaddrs
|
||||
@ -104,45 +52,14 @@ module.exports = (swarm, options, connHandler) => {
|
||||
* 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}
|
||||
* @return {Multiaddr[]}
|
||||
*/
|
||||
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
|
||||
listener.getAddrs = () => {
|
||||
const addrs = []
|
||||
for (const addr of listeningAddrs.values()) {
|
||||
addrs.push(addr)
|
||||
}
|
||||
|
||||
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 addrs
|
||||
}
|
||||
|
||||
return listener
|
||||
|
49
src/circuit/stream-to-conn.js
Normal file
49
src/circuit/stream-to-conn.js
Normal file
@ -0,0 +1,49 @@
|
||||
'use strict'
|
||||
|
||||
const abortable = require('abortable-iterator')
|
||||
const log = require('debug')('libp2p:circuit:stream')
|
||||
|
||||
// Convert a duplex iterable into a MultiaddrConnection
|
||||
// https://github.com/libp2p/interface-transport#multiaddrconnection
|
||||
module.exports = ({ stream, remoteAddr, localAddr }, options = {}) => {
|
||||
const { sink, source } = stream
|
||||
const maConn = {
|
||||
async sink (source) {
|
||||
if (options.signal) {
|
||||
source = abortable(source, options.signal)
|
||||
}
|
||||
|
||||
try {
|
||||
await sink(source)
|
||||
} catch (err) {
|
||||
// If aborted we can safely ignore
|
||||
if (err.type !== 'aborted') {
|
||||
// If the source errored the socket will already have been destroyed by
|
||||
// toIterable.duplex(). If the socket errored it will already be
|
||||
// destroyed. There's nothing to do here except log the error & return.
|
||||
log(err)
|
||||
}
|
||||
}
|
||||
close()
|
||||
},
|
||||
|
||||
source: options.signal ? abortable(source, options.signal) : source,
|
||||
conn: stream,
|
||||
localAddr,
|
||||
remoteAddr,
|
||||
timeline: { open: Date.now() },
|
||||
|
||||
close () {
|
||||
sink([])
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
function close () {
|
||||
if (!maConn.timeline.close) {
|
||||
maConn.timeline.close = Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
return maConn
|
||||
}
|
@ -1,13 +1,20 @@
|
||||
'use strict'
|
||||
|
||||
const mergeOptions = require('merge-options')
|
||||
const { struct, superstruct } = require('superstruct')
|
||||
const { optional, list } = struct
|
||||
const Constants = require('./constants')
|
||||
|
||||
const DefaultConfig = {
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
},
|
||||
dialer: {
|
||||
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
||||
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
||||
dialTimeout: Constants.DIAL_TIMEOUT
|
||||
},
|
||||
metrics: {
|
||||
enabled: false
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false,
|
||||
@ -34,71 +41,15 @@ const DefaultConfig = {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
},
|
||||
transport: {}
|
||||
}
|
||||
}
|
||||
|
||||
// Define custom types
|
||||
const s = superstruct({
|
||||
types: {
|
||||
transport: value => {
|
||||
if (value.length === 0) return 'ERROR_EMPTY'
|
||||
value.forEach(i => {
|
||||
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
|
||||
})
|
||||
return true
|
||||
},
|
||||
protector: value => {
|
||||
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const modulesSchema = s({
|
||||
connEncryption: optional(list([s('object|function')])),
|
||||
// this is hacky to simulate optional because interface doesnt work correctly with it
|
||||
// change to optional when fixed upstream
|
||||
connProtector: s('undefined|protector'),
|
||||
contentRouting: optional(list(['object'])),
|
||||
dht: optional(s('null|function|object')),
|
||||
pubsub: optional(s('null|function|object')),
|
||||
peerDiscovery: optional(list([s('object|function')])),
|
||||
peerRouting: optional(list(['object'])),
|
||||
streamMuxer: optional(list([s('object|function')])),
|
||||
transport: 'transport'
|
||||
})
|
||||
|
||||
const configSchema = s({
|
||||
peerDiscovery: 'object?',
|
||||
relay: 'object?',
|
||||
dht: 'object?',
|
||||
pubsub: 'object?'
|
||||
})
|
||||
|
||||
const optionsSchema = s({
|
||||
switch: 'object?',
|
||||
connectionManager: 'object?',
|
||||
datastore: 'object?',
|
||||
peerInfo: 'object',
|
||||
peerBook: 'object?',
|
||||
modules: modulesSchema,
|
||||
config: configSchema
|
||||
})
|
||||
|
||||
module.exports.validate = (opts) => {
|
||||
opts = mergeOptions(DefaultConfig, opts)
|
||||
const [error, options] = optionsSchema.validate(opts)
|
||||
|
||||
// Improve errors throwed, reduce stack by throwing here and add reason to the message
|
||||
if (error) {
|
||||
throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`)
|
||||
} else {
|
||||
// Throw when dht is enabled but no dht module provided
|
||||
if (options.config.dht.enabled) {
|
||||
s('function|object')(options.modules.dht)
|
||||
}
|
||||
}
|
||||
if (opts.modules.transport.length < 1) throw new Error("'options.modules.transport' must contain at least 1 transport")
|
||||
|
||||
return options
|
||||
return opts
|
||||
}
|
||||
|
@ -1,99 +0,0 @@
|
||||
# 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.
|
@ -1,12 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const assert = require('assert')
|
||||
const mergeOptions = require('merge-options')
|
||||
const LatencyMonitor = require('latency-monitor').default
|
||||
const debug = require('debug')('libp2p:connection-manager')
|
||||
const retimer = require('retimer')
|
||||
|
||||
const defaultOptions = {
|
||||
maxPeers: Infinity,
|
||||
minPeers: 0,
|
||||
maxConnections: Infinity,
|
||||
minConnections: 0,
|
||||
maxData: Infinity,
|
||||
maxSentData: Infinity,
|
||||
maxReceivedData: Infinity,
|
||||
@ -16,54 +18,75 @@ const defaultOptions = {
|
||||
defaultPeerValue: 1
|
||||
}
|
||||
|
||||
class ConnectionManager extends EventEmitter {
|
||||
class ConnectionManager {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {Libp2p} libp2p
|
||||
* @param {object} options
|
||||
* @param {Number} options.maxConnections The maximum number of connections allowed. Default=Infinity
|
||||
* @param {Number} options.minConnections The minimum number of connections to avoid pruning. Default=0
|
||||
* @param {Number} options.maxData The max data (in and out), per average interval to allow. Default=Infinity
|
||||
* @param {Number} options.maxSentData The max outgoing data, per average interval to allow. Default=Infinity
|
||||
* @param {Number} options.maxReceivedData The max incoming data, per average interval to allow.. Default=Infinity
|
||||
* @param {Number} options.maxEventLoopDelay The upper limit the event loop can take to run. Default=Infinity
|
||||
* @param {Number} options.pollInterval How often, in milliseconds, metrics and latency should be checked. Default=2000
|
||||
* @param {Number} options.movingAverageInterval How often, in milliseconds, to compute averages. Default=60000
|
||||
* @param {Number} options.defaultPeerValue The value of the peer. Default=1
|
||||
*/
|
||||
constructor (libp2p, options) {
|
||||
super()
|
||||
this._libp2p = libp2p
|
||||
this._options = Object.assign({}, defaultOptions, options)
|
||||
this._options.maxPeersPerProtocol = fixMaxPeersPerProtocol(this._options.maxPeersPerProtocol)
|
||||
this._registrar = libp2p.registrar
|
||||
this._peerId = libp2p.peerInfo.id.toB58String()
|
||||
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
|
||||
assert(
|
||||
this._options.maxConnections > this._options.minConnections,
|
||||
'Connection Manager maxConnections must be greater than minConnections'
|
||||
)
|
||||
|
||||
debug('options: %j', this._options)
|
||||
|
||||
this._stats = libp2p.stats
|
||||
if (options && !this._stats) {
|
||||
throw new Error('No libp2p.stats')
|
||||
}
|
||||
this._metrics = libp2p.metrics
|
||||
|
||||
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))
|
||||
}
|
||||
this._connections = new Map()
|
||||
this._timer = null
|
||||
this._checkMetrics = this._checkMetrics.bind(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
||||
* only event loop and connection limits will be monitored.
|
||||
*/
|
||||
start () {
|
||||
this._stats.on('update', this._onStatsUpdate)
|
||||
this._libp2p.on('connection:start', this._onPeerConnect)
|
||||
this._libp2p.on('connection:end', this._onPeerDisconnect)
|
||||
if (this._metrics) {
|
||||
this._timer = this._timer || retimer(this._checkMetrics, this._options.pollInterval)
|
||||
}
|
||||
|
||||
// latency monitor
|
||||
this._latencyMonitor = new LatencyMonitor({
|
||||
latencyCheckIntervalMs: this._options.pollInterval,
|
||||
dataEmitIntervalMs: this._options.pollInterval
|
||||
})
|
||||
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
||||
this._latencyMonitor.on('data', this._onLatencyMeasure)
|
||||
debug('started')
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the Connection Manager
|
||||
*/
|
||||
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)
|
||||
this._timer && this._timer.clear()
|
||||
this._latencyMonitor && this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
|
||||
debug('stopped')
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the given peer. Peers with lower values
|
||||
* will be disconnected first.
|
||||
* @param {PeerId} peerId
|
||||
* @param {number} value A number between 0 and 1
|
||||
*/
|
||||
setPeerValue (peerId, value) {
|
||||
if (value < 0 || value > 1) {
|
||||
throw new Error('value should be a number between 0 and 1')
|
||||
@ -74,112 +97,92 @@ class ConnectionManager extends EventEmitter {
|
||||
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()
|
||||
/**
|
||||
* Checks the libp2p metrics to determine if any values have exceeded
|
||||
* the configured maximums.
|
||||
* @private
|
||||
*/
|
||||
_checkMetrics () {
|
||||
const movingAverages = this._metrics.global.movingAverages
|
||||
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkLimit('maxReceivedData', received)
|
||||
const sent = movingAvgs.dataSent[this._options.movingAverageInterval].movingAverage()
|
||||
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkLimit('maxSentData', sent)
|
||||
const total = received + sent
|
||||
this._checkLimit('maxData', total)
|
||||
debug('stats update', total)
|
||||
debug('metrics update', total)
|
||||
this._timer.reschedule(this._options.pollInterval)
|
||||
}
|
||||
|
||||
_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]--
|
||||
}
|
||||
})
|
||||
/**
|
||||
* Tracks the incoming connection and check the connection limit
|
||||
* @param {Connection} connection
|
||||
*/
|
||||
onConnect (connection) {
|
||||
const peerId = connection.remotePeer.toB58String()
|
||||
this._connections.set(connection.id, connection)
|
||||
if (!this._peerValues.has(peerId)) {
|
||||
this._peerValues.set(peerId, this._options.defaultPeerValue)
|
||||
}
|
||||
|
||||
this.emit('disconnected', peerId)
|
||||
this._checkLimit('maxConnections', this._connections.size)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the connection from tracking
|
||||
* @param {Connection} connection
|
||||
*/
|
||||
onDisconnect (connection) {
|
||||
this._connections.delete(connection.id)
|
||||
this._peerValues.delete(connection.remotePeer.toB58String())
|
||||
}
|
||||
|
||||
/**
|
||||
* If the event loop is slow, maybe close a connection
|
||||
* @private
|
||||
* @param {*} summary The LatencyMonitor summary
|
||||
*/
|
||||
_onLatencyMeasure (summary) {
|
||||
this._checkLimit('maxEventLoopDelay', summary.avgMs)
|
||||
}
|
||||
|
||||
/**
|
||||
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
||||
* @private
|
||||
* @param {string} name The name of the field to check limits for
|
||||
* @param {number} value The current value of the field
|
||||
*/
|
||||
_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()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If we have more connections than our maximum, close a connection
|
||||
* to the lowest valued peer.
|
||||
* @private
|
||||
*/
|
||||
_maybeDisconnectOne () {
|
||||
if (this._options.minPeers < this._peerValues.size) {
|
||||
if (this._options.minConnections < this._connections.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)
|
||||
debug('%s: closing a connection to %j', this._peerId, peerId)
|
||||
for (const connection of this._connections.values()) {
|
||||
if (connection.remotePeer.toB58String() === peerId) {
|
||||
connection.close()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_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
|
||||
@ -187,32 +190,3 @@ 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()
|
||||
}
|
||||
|
17
src/constants.js
Normal file
17
src/constants.js
Normal file
@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
|
||||
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
|
||||
MAX_PER_PEER_DIALS: 4, // Allowed parallel dials per DialRequest
|
||||
METRICS: {
|
||||
computeThrottleMaxQueueSize: 1000,
|
||||
computeThrottleTimeout: 2000,
|
||||
movingAverageIntervals: [
|
||||
60 * 1000, // 1 minute
|
||||
5 * 60 * 1000, // 5 minutes
|
||||
15 * 60 * 1000 // 15 minutes
|
||||
],
|
||||
maxOldPeersRetention: 50
|
||||
}
|
||||
}
|
@ -1,16 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const parallel = require('async/parallel')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
const { messages, codes } = require('./errors')
|
||||
|
||||
const all = require('it-all')
|
||||
const pAny = require('p-any')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.contentRouting || []
|
||||
const dht = node._dht
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (node._dht) {
|
||||
routers.unshift(node._dht)
|
||||
if (dht) {
|
||||
routers.unshift(dht)
|
||||
}
|
||||
|
||||
return {
|
||||
@ -19,66 +21,93 @@ module.exports = (node) => {
|
||||
* Once a content router succeeds, iteration will stop.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {number} options.maxNumProviders - maximum number of providers to find
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
* @param {object} [options]
|
||||
* @param {number} [options.timeout] How long the query should run
|
||||
* @param {number} [options.maxNumProviders] - maximum number of providers to find
|
||||
* @returns {AsyncIterable<PeerInfo>}
|
||||
*/
|
||||
findProviders: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
} else if (typeof options === 'number') { // This can be deprecated in a future release
|
||||
options = {
|
||||
maxTimeout: options
|
||||
}
|
||||
}
|
||||
|
||||
async * findProviders (key, options) {
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findProviders(key, options, (err, results) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
const result = await pAny(
|
||||
routers.map(async (router) => {
|
||||
const provs = await all(router.findProviders(key, options))
|
||||
|
||||
// If we don't have any results, we need to provide an error to keep trying
|
||||
if (!results || Object.keys(results).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
if (!provs || !provs.length) {
|
||||
throw errCode(new Error('not found'), 'NOT_FOUND')
|
||||
}
|
||||
|
||||
cb(null, results)
|
||||
return provs
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err && err.code !== 'NOT_FOUND') {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
}),
|
||||
for (const pInfo of result) {
|
||||
yield pInfo
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Iterates over all content routers in parallel to notify it is
|
||||
* a provider of the given key.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
provide: promisify((key, callback) => {
|
||||
async provide (key) { // eslint-disable-line require-await
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
parallel(routers.map((router) => {
|
||||
return (cb) => router.provide(key, cb)
|
||||
}), callback)
|
||||
})
|
||||
return Promise.all(routers.map((router) => router.provide(key)))
|
||||
},
|
||||
|
||||
/**
|
||||
* Store the given key/value pair in the DHT.
|
||||
* @param {Buffer} key
|
||||
* @param {Buffer} value
|
||||
* @param {Object} [options] - put options
|
||||
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async put (key, value, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.put(key, value, options)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the value to the given key.
|
||||
* Times out after 1 minute by default.
|
||||
* @param {Buffer} key
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<{from: PeerId, val: Buffer}>}
|
||||
*/
|
||||
async get (key, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.get(key, options)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the `n` values to the given key without sorting.
|
||||
* @param {Buffer} key
|
||||
* @param {number} nVals
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<Array<{from: PeerId, val: Buffer}>>}
|
||||
*/
|
||||
async getMany (key, nVals, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.getMany(key, nVals, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
src/dht.js
43
src/dht.js
@ -1,43 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const nextTick = require('async/nextTick')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
const { messages, codes } = require('./errors')
|
||||
|
||||
module.exports = (node) => {
|
||||
return {
|
||||
put: promisify((key, value, callback) => {
|
||||
if (!node._dht) {
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.put(key, value, callback)
|
||||
}),
|
||||
get: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!node._dht) {
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.get(key, options, callback)
|
||||
}),
|
||||
getMany: promisify((key, nVals, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!node._dht) {
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.getMany(key, nVals, options, callback)
|
||||
})
|
||||
}
|
||||
}
|
80
src/dialer/dial-request.js
Normal file
80
src/dialer/dial-request.js
Normal file
@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const AbortController = require('abort-controller')
|
||||
const anySignal = require('any-signal')
|
||||
const debug = require('debug')
|
||||
const errCode = require('err-code')
|
||||
const log = debug('libp2p:dialer:request')
|
||||
log.error = debug('libp2p:dialer:request:error')
|
||||
const FIFO = require('p-fifo')
|
||||
const pAny = require('p-any')
|
||||
|
||||
class DialRequest {
|
||||
/**
|
||||
* Manages running the `dialAction` on multiple provided `addrs` in parallel
|
||||
* up to a maximum determined by the number of tokens returned
|
||||
* from `dialer.getTokens`. Once a DialRequest is created, it can be
|
||||
* started using `DialRequest.run(options)`. Once a single dial has succeeded,
|
||||
* all other dials in the request will be cancelled.
|
||||
* @param {object} options
|
||||
* @param {Multiaddr[]} options.addrs
|
||||
* @param {function(Multiaddr):Promise<Connection>} options.dialAction
|
||||
* @param {Dialer} options.dialer
|
||||
*/
|
||||
constructor ({
|
||||
addrs,
|
||||
dialAction,
|
||||
dialer
|
||||
}) {
|
||||
this.addrs = addrs
|
||||
this.dialer = dialer
|
||||
this.dialAction = dialAction
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* @param {object} options
|
||||
* @param {AbortSignal} options.signal An AbortController signal
|
||||
* @returns {Connection}
|
||||
*/
|
||||
async run (options) {
|
||||
const tokens = this.dialer.getTokens(this.addrs.length)
|
||||
// If no tokens are available, throw
|
||||
if (tokens.length < 1) {
|
||||
throw errCode(new Error('No dial tokens available'), 'ERR_NO_DIAL_TOKENS')
|
||||
}
|
||||
|
||||
const tokenHolder = new FIFO()
|
||||
tokens.forEach(token => tokenHolder.push(token))
|
||||
const dialAbortControllers = this.addrs.map(() => new AbortController())
|
||||
let completedDials = 0
|
||||
|
||||
try {
|
||||
return await pAny(this.addrs.map(async (addr, i) => {
|
||||
const token = await tokenHolder.shift() // get token
|
||||
let conn
|
||||
try {
|
||||
const signal = dialAbortControllers[i].signal
|
||||
conn = await this.dialAction(addr, { ...options, signal: anySignal([signal, options.signal]) })
|
||||
// Remove the successful AbortController so it is not aborted
|
||||
dialAbortControllers.splice(i, 1)
|
||||
} finally {
|
||||
completedDials++
|
||||
// If we have more or equal dials remaining than tokens, recycle the token, otherwise release it
|
||||
if (this.addrs.length - completedDials >= tokens.length) {
|
||||
tokenHolder.push(token)
|
||||
} else {
|
||||
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
||||
}
|
||||
}
|
||||
|
||||
return conn
|
||||
}))
|
||||
} finally {
|
||||
dialAbortControllers.map(c => c.abort()) // success/failure happened, abort everything else
|
||||
tokens.forEach(token => this.dialer.releaseToken(token)) // release tokens back to the dialer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.DialRequest = DialRequest
|
214
src/dialer/index.js
Normal file
214
src/dialer/index.js
Normal file
@ -0,0 +1,214 @@
|
||||
'use strict'
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const errCode = require('err-code')
|
||||
const TimeoutController = require('timeout-abort-controller')
|
||||
const anySignal = require('any-signal')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:dialer')
|
||||
log.error = debug('libp2p:dialer:error')
|
||||
const { DialRequest } = require('./dial-request')
|
||||
|
||||
const { codes } = require('../errors')
|
||||
const {
|
||||
DIAL_TIMEOUT,
|
||||
MAX_PARALLEL_DIALS,
|
||||
MAX_PER_PEER_DIALS
|
||||
} = require('../constants')
|
||||
|
||||
class Dialer {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {TransportManager} options.transportManager
|
||||
* @param {Peerstore} peerStore
|
||||
* @param {number} options.concurrency Number of max concurrent dials. Defaults to `MAX_PARALLEL_DIALS`
|
||||
* @param {number} options.timeout How long a dial attempt is allowed to take. Defaults to `DIAL_TIMEOUT`
|
||||
*/
|
||||
constructor ({
|
||||
transportManager,
|
||||
peerStore,
|
||||
concurrency = MAX_PARALLEL_DIALS,
|
||||
timeout = DIAL_TIMEOUT,
|
||||
perPeerLimit = MAX_PER_PEER_DIALS
|
||||
}) {
|
||||
this.transportManager = transportManager
|
||||
this.peerStore = peerStore
|
||||
this.concurrency = concurrency
|
||||
this.timeout = timeout
|
||||
this.perPeerLimit = perPeerLimit
|
||||
this.tokens = [...new Array(concurrency)].map((_, index) => index)
|
||||
this._pendingDials = new Map()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears any pending dials
|
||||
*/
|
||||
destroy () {
|
||||
for (const dial of this._pendingDials.values()) {
|
||||
try {
|
||||
dial.controller.abort()
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
}
|
||||
this._pendingDials.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a given `PeerId` or `Multiaddr` by dialing all of its known addresses.
|
||||
* The dial to the first address that is successfully able to upgrade a connection
|
||||
* will be used.
|
||||
*
|
||||
* @param {PeerInfo|Multiaddr} peer The peer to dial
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal] An AbortController signal
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async connectToPeer (peer, options = {}) {
|
||||
const dialTarget = this._createDialTarget(peer)
|
||||
if (dialTarget.addrs.length === 0) {
|
||||
throw errCode(new Error('The dial request has no addresses'), 'ERR_NO_DIAL_MULTIADDRS')
|
||||
}
|
||||
const pendingDial = this._pendingDials.get(dialTarget.id) || this._createPendingDial(dialTarget, options)
|
||||
|
||||
try {
|
||||
const connection = await pendingDial.promise
|
||||
log('dial succeeded to %s', dialTarget.id)
|
||||
return connection
|
||||
} catch (err) {
|
||||
// Error is a timeout
|
||||
if (pendingDial.controller.signal.aborted) {
|
||||
err.code = codes.ERR_TIMEOUT
|
||||
}
|
||||
log.error(err)
|
||||
throw err
|
||||
} finally {
|
||||
pendingDial.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef DialTarget
|
||||
* @property {string} id
|
||||
* @property {Multiaddr[]} addrs
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a DialTarget. The DialTarget is used to create and track
|
||||
* the DialRequest to a given peer.
|
||||
* @private
|
||||
* @param {PeerInfo|Multiaddr} peer A PeerId or Multiaddr
|
||||
* @returns {DialTarget}
|
||||
*/
|
||||
_createDialTarget (peer) {
|
||||
const dialable = Dialer.getDialable(peer)
|
||||
if (multiaddr.isMultiaddr(dialable)) {
|
||||
return {
|
||||
id: dialable.toString(),
|
||||
addrs: [dialable]
|
||||
}
|
||||
}
|
||||
const addrs = this.peerStore.multiaddrsForPeer(dialable)
|
||||
return {
|
||||
id: dialable.id.toB58String(),
|
||||
addrs
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef PendingDial
|
||||
* @property {DialRequest} dialRequest
|
||||
* @property {TimeoutController} controller
|
||||
* @property {Promise} promise
|
||||
* @property {function():void} destroy
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a PendingDial that wraps the underlying DialRequest
|
||||
* @private
|
||||
* @param {DialTarget} dialTarget
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal] An AbortController signal
|
||||
* @returns {PendingDial}
|
||||
*/
|
||||
_createPendingDial (dialTarget, options) {
|
||||
const dialAction = (addr, options) => {
|
||||
if (options.signal.aborted) throw errCode(new Error('already aborted'), 'ERR_ALREADY_ABORTED')
|
||||
return this.transportManager.dial(addr, options)
|
||||
}
|
||||
|
||||
const dialRequest = new DialRequest({
|
||||
addrs: dialTarget.addrs,
|
||||
dialAction,
|
||||
dialer: this
|
||||
})
|
||||
|
||||
// Combine the timeout signal and options.signal, if provided
|
||||
const timeoutController = new TimeoutController(this.timeout)
|
||||
const signals = [timeoutController.signal]
|
||||
options.signal && signals.push(options.signal)
|
||||
const signal = anySignal(signals)
|
||||
|
||||
const pendingDial = {
|
||||
dialRequest,
|
||||
controller: timeoutController,
|
||||
promise: dialRequest.run({ ...options, signal }),
|
||||
destroy: () => {
|
||||
timeoutController.clear()
|
||||
this._pendingDials.delete(dialTarget.id)
|
||||
}
|
||||
}
|
||||
this._pendingDials.set(dialTarget.id, pendingDial)
|
||||
return pendingDial
|
||||
}
|
||||
|
||||
getTokens (num) {
|
||||
const total = Math.min(num, this.perPeerLimit, this.tokens.length)
|
||||
const tokens = this.tokens.splice(0, total)
|
||||
log('%d tokens request, returning %d, %d remaining', num, total, this.tokens.length)
|
||||
return tokens
|
||||
}
|
||||
|
||||
releaseToken (token) {
|
||||
// Guard against duplicate releases
|
||||
if (this.tokens.indexOf(token) > -1) return
|
||||
log('token %d released', token)
|
||||
this.tokens.push(token)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given `peer` into a `PeerInfo` or `Multiaddr`.
|
||||
* @static
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @returns {PeerInfo|Multiaddr}
|
||||
*/
|
||||
static getDialable (peer) {
|
||||
if (PeerInfo.isPeerInfo(peer)) return peer
|
||||
if (typeof peer === 'string') {
|
||||
peer = multiaddr(peer)
|
||||
}
|
||||
|
||||
let addr
|
||||
if (multiaddr.isMultiaddr(peer)) {
|
||||
addr = peer
|
||||
try {
|
||||
peer = PeerId.createFromCID(peer.getPeerId())
|
||||
} catch (err) {
|
||||
// Couldn't get the PeerId, just use the address
|
||||
return peer
|
||||
}
|
||||
}
|
||||
|
||||
if (PeerId.isPeerId(peer)) {
|
||||
peer = new PeerInfo(peer)
|
||||
}
|
||||
|
||||
addr && peer.multiaddrs.add(addr)
|
||||
return peer
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dialer
|
@ -8,6 +8,21 @@ exports.messages = {
|
||||
exports.codes = {
|
||||
DHT_DISABLED: 'ERR_DHT_DISABLED',
|
||||
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
||||
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
|
||||
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
|
||||
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
|
||||
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
||||
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF'
|
||||
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
|
||||
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
|
||||
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
||||
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
||||
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
|
||||
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
|
||||
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
|
||||
ERR_INVALID_PEER: 'ERR_INVALID_PEER',
|
||||
ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
|
||||
ERR_TIMEOUT: 'ERR_TIMEOUT',
|
||||
ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE',
|
||||
ERR_TRANSPORT_DIAL_FAILED: 'ERR_TRANSPORT_DIAL_FAILED',
|
||||
ERR_UNSUPPORTED_PROTOCOL: 'ERR_UNSUPPORTED_PROTOCOL'
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ const errCode = require('err-code')
|
||||
|
||||
/**
|
||||
* 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`.
|
||||
* The `PeerStore` will be checked for the resulting peer, and
|
||||
* the peer will be updated in the `PeerStore`.
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @param {PeerBook} peerBook
|
||||
* @param {PeerStore} peerStore
|
||||
* @returns {PeerInfo}
|
||||
*/
|
||||
function getPeerInfo (peer, peerBook) {
|
||||
function getPeerInfo (peer, peerStore) {
|
||||
if (typeof peer === 'string') {
|
||||
peer = multiaddr(peer)
|
||||
}
|
||||
@ -38,7 +38,7 @@ function getPeerInfo (peer, peerBook) {
|
||||
|
||||
addr && peer.multiaddrs.add(addr)
|
||||
|
||||
return peerBook ? peerBook.put(peer) : peer
|
||||
return peerStore ? peerStore.put(peer) : peer
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,12 +54,9 @@ function getPeerInfoRemote (peer, libp2p) {
|
||||
let peerInfo
|
||||
|
||||
try {
|
||||
peerInfo = getPeerInfo(peer, libp2p.peerBook)
|
||||
peerInfo = getPeerInfo(peer, libp2p.peerStore)
|
||||
} catch (err) {
|
||||
return Promise.reject(errCode(
|
||||
new Error(`${peer} is not a valid peer type`),
|
||||
'ERR_INVALID_PEER_TYPE'
|
||||
))
|
||||
throw errCode(err, 'ERR_INVALID_PEER_TYPE')
|
||||
}
|
||||
|
||||
// If we don't have an address for the peer, attempt to find it
|
||||
@ -67,7 +64,7 @@ function getPeerInfoRemote (peer, libp2p) {
|
||||
return libp2p.peerRouting.findPeer(peerInfo.id)
|
||||
}
|
||||
|
||||
return Promise.resolve(peerInfo)
|
||||
return peerInfo
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
@ -6,32 +6,8 @@
|
||||
|
||||
## 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.
|
||||
Identify is a STUN protocol, used by libp2p 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.
|
||||
The spec for Identify and Identify Push is at [libp2p/specs](https://github.com/libp2p/specs/tree/master/identify).
|
||||
|
6
src/identify/consts.js
Normal file
6
src/identify/consts.js
Normal file
@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
|
||||
module.exports.AGENT_VERSION = 'js-libp2p/0.1.0'
|
||||
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
|
||||
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'
|
@ -1,87 +0,0 @@
|
||||
'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
|
||||
}
|
@ -1,7 +1,298 @@
|
||||
'use strict'
|
||||
|
||||
exports = module.exports
|
||||
exports.multicodec = '/ipfs/id/1.0.0'
|
||||
exports.listener = require('./listener')
|
||||
exports.dialer = require('./dialer')
|
||||
exports.message = require('./message')
|
||||
const debug = require('debug')
|
||||
const pb = require('it-protocol-buffers')
|
||||
const lp = require('it-length-prefixed')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect, take } = require('streaming-iterables')
|
||||
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
|
||||
const Message = require('./message')
|
||||
|
||||
const log = debug('libp2p:identify')
|
||||
log.error = debug('libp2p:identify:error')
|
||||
|
||||
const {
|
||||
MULTICODEC_IDENTIFY,
|
||||
MULTICODEC_IDENTIFY_PUSH,
|
||||
AGENT_VERSION,
|
||||
PROTOCOL_VERSION
|
||||
} = require('./consts')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { codes } = require('../errors')
|
||||
|
||||
class IdentifyService {
|
||||
/**
|
||||
* Replaces the multiaddrs on the given `peerInfo`,
|
||||
* with the provided `multiaddrs`
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {Array<Multiaddr>|Array<Buffer>} multiaddrs
|
||||
*/
|
||||
static updatePeerAddresses (peerInfo, multiaddrs) {
|
||||
if (multiaddrs && multiaddrs.length > 0) {
|
||||
peerInfo.multiaddrs.clear()
|
||||
multiaddrs.forEach(ma => {
|
||||
try {
|
||||
peerInfo.multiaddrs.add(ma)
|
||||
} catch (err) {
|
||||
log.error('could not add multiaddr', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the protocols on the given `peerInfo`,
|
||||
* with the provided `protocols`
|
||||
* @static
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {Array<string>} protocols
|
||||
*/
|
||||
static updatePeerProtocols (peerInfo, protocols) {
|
||||
if (protocols && protocols.length > 0) {
|
||||
peerInfo.protocols.clear()
|
||||
protocols.forEach(proto => peerInfo.protocols.add(proto))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the `addr` and converts it to a Multiaddr if possible
|
||||
* @param {Buffer|String} addr
|
||||
* @returns {Multiaddr|null}
|
||||
*/
|
||||
static getCleanMultiaddr (addr) {
|
||||
if (addr && addr.length > 0) {
|
||||
try {
|
||||
return multiaddr(addr)
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {Registrar} options.registrar
|
||||
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
|
||||
* @param {PeerInfo} options.peerInfo The peer running the identify service
|
||||
*/
|
||||
constructor (options) {
|
||||
/**
|
||||
* @property {Registrar}
|
||||
*/
|
||||
this.registrar = options.registrar
|
||||
/**
|
||||
* @property {PeerInfo}
|
||||
*/
|
||||
this.peerInfo = options.peerInfo
|
||||
|
||||
this._protocols = options.protocols
|
||||
|
||||
this.handleMessage = this.handleMessage.bind(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an Identify Push update to the list of connections
|
||||
* @param {Array<Connection>} connections
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
push (connections) {
|
||||
const pushes = connections.map(async connection => {
|
||||
try {
|
||||
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
|
||||
|
||||
await pipe(
|
||||
[{
|
||||
listenAddrs: this.peerInfo.multiaddrs.toArray().map((ma) => ma.buffer),
|
||||
protocols: Array.from(this._protocols.keys())
|
||||
}],
|
||||
pb.encode(Message),
|
||||
stream
|
||||
)
|
||||
} catch (err) {
|
||||
// Just log errors
|
||||
log.error('could not push identify update to peer', err)
|
||||
}
|
||||
})
|
||||
|
||||
return Promise.all(pushes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls `push` for all peers in the `peerStore` that are connected
|
||||
* @param {PeerStore} peerStore
|
||||
*/
|
||||
pushToPeerStore (peerStore) {
|
||||
const connections = []
|
||||
let connection
|
||||
for (const peer of peerStore.peers.values()) {
|
||||
if (peer.protocols.has(MULTICODEC_IDENTIFY_PUSH) && (connection = this.registrar.getConnection(peer))) {
|
||||
connections.push(connection)
|
||||
}
|
||||
}
|
||||
|
||||
this.push(connections)
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the `Identify` message from peer associated with the given `connection`.
|
||||
* If the identified peer does not match the `PeerId` associated with the connection,
|
||||
* an error will be thrown.
|
||||
*
|
||||
* @async
|
||||
* @param {Connection} connection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async identify (connection) {
|
||||
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
|
||||
const [data] = await pipe(
|
||||
stream,
|
||||
lp.decode(),
|
||||
take(1),
|
||||
toBuffer,
|
||||
collect
|
||||
)
|
||||
|
||||
if (!data) {
|
||||
throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
|
||||
}
|
||||
|
||||
let message
|
||||
try {
|
||||
message = Message.decode(data)
|
||||
} catch (err) {
|
||||
throw errCode(err, codes.ERR_INVALID_MESSAGE)
|
||||
}
|
||||
|
||||
let {
|
||||
publicKey,
|
||||
listenAddrs,
|
||||
protocols,
|
||||
observedAddr
|
||||
} = message
|
||||
|
||||
const id = await PeerId.createFromPubKey(publicKey)
|
||||
const peerInfo = new PeerInfo(id)
|
||||
if (connection.remotePeer.toB58String() !== id.toB58String()) {
|
||||
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
|
||||
}
|
||||
|
||||
// Get the observedAddr if there is one
|
||||
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
|
||||
|
||||
// Copy the listenAddrs and protocols
|
||||
IdentifyService.updatePeerAddresses(peerInfo, listenAddrs)
|
||||
IdentifyService.updatePeerProtocols(peerInfo, protocols)
|
||||
|
||||
this.registrar.peerStore.replace(peerInfo)
|
||||
// TODO: Track our observed address so that we can score it
|
||||
log('received observed address of %s', observedAddr)
|
||||
}
|
||||
|
||||
/**
|
||||
* A handler to register with Libp2p to process identify messages.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {String} options.protocol
|
||||
* @param {*} options.stream
|
||||
* @param {Connection} options.connection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
handleMessage ({ connection, stream, protocol }) {
|
||||
switch (protocol) {
|
||||
case MULTICODEC_IDENTIFY:
|
||||
return this._handleIdentify({ connection, stream })
|
||||
case MULTICODEC_IDENTIFY_PUSH:
|
||||
return this._handlePush({ connection, stream })
|
||||
default:
|
||||
log.error('cannot handle unknown protocol %s', protocol)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the `Identify` response to the requesting peer over the
|
||||
* given `connection`
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {*} options.stream
|
||||
* @param {Connection} options.connection
|
||||
*/
|
||||
_handleIdentify ({ connection, stream }) {
|
||||
let publicKey = Buffer.alloc(0)
|
||||
if (this.peerInfo.id.pubKey) {
|
||||
publicKey = this.peerInfo.id.pubKey.bytes
|
||||
}
|
||||
|
||||
const message = Message.encode({
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
agentVersion: AGENT_VERSION,
|
||||
publicKey,
|
||||
listenAddrs: this.peerInfo.multiaddrs.toArray().map((ma) => ma.buffer),
|
||||
observedAddr: connection.remoteAddr.buffer,
|
||||
protocols: Array.from(this._protocols.keys())
|
||||
})
|
||||
|
||||
pipe(
|
||||
[message],
|
||||
lp.encode(),
|
||||
stream
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the Identify Push message from the given `connection`
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {*} options.stream
|
||||
* @param {Connection} options.connection
|
||||
*/
|
||||
async _handlePush ({ connection, stream }) {
|
||||
const [data] = await pipe(
|
||||
stream,
|
||||
lp.decode(),
|
||||
take(1),
|
||||
toBuffer,
|
||||
collect
|
||||
)
|
||||
|
||||
let message
|
||||
try {
|
||||
message = Message.decode(data)
|
||||
} catch (err) {
|
||||
return log.error('received invalid message', err)
|
||||
}
|
||||
|
||||
// Update the listen addresses
|
||||
const peerInfo = new PeerInfo(connection.remotePeer)
|
||||
|
||||
try {
|
||||
IdentifyService.updatePeerAddresses(peerInfo, message.listenAddrs)
|
||||
} catch (err) {
|
||||
return log.error('received invalid listen addrs', err)
|
||||
}
|
||||
|
||||
// Update the protocols
|
||||
IdentifyService.updatePeerProtocols(peerInfo, message.protocols)
|
||||
|
||||
// Update the peer in the PeerStore
|
||||
this.registrar.peerStore.replace(peerInfo)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.IdentifyService = IdentifyService
|
||||
/**
|
||||
* The protocols the IdentifyService supports
|
||||
* @property multicodecs
|
||||
*/
|
||||
module.exports.multicodecs = {
|
||||
IDENTIFY: MULTICODEC_IDENTIFY,
|
||||
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
|
||||
}
|
||||
module.exports.Message = Message
|
||||
|
@ -1,35 +0,0 @@
|
||||
'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
|
||||
)
|
||||
})
|
||||
}
|
702
src/index.js
702
src/index.js
@ -1,48 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const { EventEmitter } = require('events')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p')
|
||||
log.error = debug('libp2p:error')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
const each = require('async/each')
|
||||
const series = require('async/series')
|
||||
const parallel = require('async/parallel')
|
||||
const nextTick = require('async/nextTick')
|
||||
|
||||
const PeerBook = require('peer-book')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Switch = require('./switch')
|
||||
const Ping = require('./ping')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const ConnectionManager = require('./connection-manager')
|
||||
|
||||
const { emitFirst } = require('./util')
|
||||
const peerRouting = require('./peer-routing')
|
||||
const contentRouting = require('./content-routing')
|
||||
const dht = require('./dht')
|
||||
const pubsub = require('./pubsub')
|
||||
const { getPeerInfoRemote } = require('./get-peer-info')
|
||||
const validateConfig = require('./config').validate
|
||||
const { getPeerInfo } = require('./get-peer-info')
|
||||
const { validate: validateConfig } = require('./config')
|
||||
const { codes } = require('./errors')
|
||||
|
||||
const notStarted = (action, state) => {
|
||||
return errCode(
|
||||
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
|
||||
codes.ERR_NODE_NOT_STARTED
|
||||
)
|
||||
}
|
||||
const ConnectionManager = require('./connection-manager')
|
||||
const Circuit = require('./circuit')
|
||||
const Dialer = require('./dialer')
|
||||
const Metrics = require('./metrics')
|
||||
const TransportManager = require('./transport-manager')
|
||||
const Upgrader = require('./upgrader')
|
||||
const PeerStore = require('./peer-store')
|
||||
const Registrar = require('./registrar')
|
||||
const ping = require('./ping')
|
||||
const {
|
||||
IdentifyService,
|
||||
multicodecs: IDENTIFY_PROTOCOLS
|
||||
} = require('./identify')
|
||||
|
||||
/**
|
||||
* @fires Libp2p#error Emitted when an error occurs
|
||||
* @fires Libp2p#peer:connect Emitted when a peer is connected to this node
|
||||
* @fires Libp2p#peer:disconnect Emitted when a peer disconnects from this node
|
||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
||||
* @fires Libp2p#start Emitted when the node and its services has started
|
||||
* @fires Libp2p#stop Emitted when the node and its services has stopped
|
||||
*/
|
||||
class Libp2p extends EventEmitter {
|
||||
constructor (_options) {
|
||||
@ -53,77 +43,124 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
this.datastore = this._options.datastore
|
||||
this.peerInfo = this._options.peerInfo
|
||||
this.peerBook = this._options.peerBook || new PeerBook()
|
||||
this.peerStore = new PeerStore()
|
||||
|
||||
this._modules = this._options.modules
|
||||
this._config = this._options.config
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = [] // Discovery service instances/references
|
||||
this._discovery = new Map() // Discovery service instances/references
|
||||
|
||||
// create the switch, and listen for errors
|
||||
this._switch = new Switch(this.peerInfo, this.peerBook, this._options.switch)
|
||||
this._switch.on('error', (...args) => this.emit('error', ...args))
|
||||
|
||||
this.stats = this._switch.stats
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
|
||||
// Attach stream multiplexers
|
||||
if (this._modules.streamMuxer) {
|
||||
const muxers = this._modules.streamMuxer
|
||||
muxers.forEach((muxer) => this._switch.connection.addStreamMuxer(muxer))
|
||||
|
||||
// If muxer exists
|
||||
// we can use Identify
|
||||
this._switch.connection.reuse()
|
||||
// we can use Relay for listening/dialing
|
||||
this._switch.connection.enableCircuitRelay(this._config.relay)
|
||||
|
||||
// Received incomming dial and muxer upgrade happened,
|
||||
// reuse this muxed connection
|
||||
this._switch.on('peer-mux-established', (peerInfo) => {
|
||||
this.emit('peer:connect', peerInfo)
|
||||
})
|
||||
|
||||
this._switch.on('peer-mux-closed', (peerInfo) => {
|
||||
this.emit('peer:disconnect', peerInfo)
|
||||
})
|
||||
if (this._options.metrics.enabled) {
|
||||
this.metrics = new Metrics(this._options.metrics)
|
||||
}
|
||||
|
||||
// Events for anytime connections are created/removed
|
||||
this._switch.on('connection:start', (peerInfo) => {
|
||||
this.emit('connection:start', peerInfo)
|
||||
// Setup the Upgrader
|
||||
this.upgrader = new Upgrader({
|
||||
localPeer: this.peerInfo.id,
|
||||
metrics: this.metrics,
|
||||
onConnection: (connection) => {
|
||||
const peerInfo = this.peerStore.put(new PeerInfo(connection.remotePeer), { silent: true })
|
||||
this.registrar.onConnect(peerInfo, connection)
|
||||
this.connectionManager.onConnect(connection)
|
||||
this.emit('peer:connect', peerInfo)
|
||||
|
||||
// Run identify for every connection
|
||||
if (this.identifyService) {
|
||||
this.identifyService.identify(connection, connection.remotePeer)
|
||||
.catch(log.error)
|
||||
}
|
||||
},
|
||||
onConnectionEnd: (connection) => {
|
||||
const peerInfo = Dialer.getDialable(connection.remotePeer)
|
||||
this.registrar.onDisconnect(peerInfo, connection)
|
||||
this.connectionManager.onDisconnect(connection)
|
||||
|
||||
// If there are no connections to the peer, disconnect
|
||||
if (!this.registrar.getConnection(peerInfo)) {
|
||||
this.emit('peer:disconnect', peerInfo)
|
||||
this.metrics && this.metrics.onPeerDisconnected(peerInfo.id)
|
||||
}
|
||||
}
|
||||
})
|
||||
this._switch.on('connection:end', (peerInfo) => {
|
||||
this.emit('connection:end', peerInfo)
|
||||
|
||||
// Create the Registrar
|
||||
this.registrar = new Registrar({ peerStore: this.peerStore })
|
||||
this.handle = this.handle.bind(this)
|
||||
this.registrar.handle = this.handle
|
||||
|
||||
// Create the Connection Manager
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
|
||||
// Setup the transport manager
|
||||
this.transportManager = new TransportManager({
|
||||
libp2p: this,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
|
||||
// Attach crypto channels
|
||||
if (this._modules.connEncryption) {
|
||||
const cryptos = this._modules.connEncryption
|
||||
cryptos.forEach((crypto) => {
|
||||
this._switch.connection.crypto(crypto.tag, crypto.encrypt)
|
||||
this.upgrader.cryptos.set(crypto.protocol, crypto)
|
||||
})
|
||||
}
|
||||
|
||||
this.dialer = new Dialer({
|
||||
transportManager: this.transportManager,
|
||||
peerStore: this.peerStore,
|
||||
concurrency: this._options.dialer.maxParallelDials,
|
||||
perPeerLimit: this._options.dialer.maxDialsPerPeer,
|
||||
timeout: this._options.dialer.dialTimeout
|
||||
})
|
||||
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
const key = Transport.prototype[Symbol.toStringTag]
|
||||
const transportOptions = this._config.transport[key]
|
||||
this.transportManager.add(key, Transport, transportOptions)
|
||||
})
|
||||
|
||||
if (this._config.relay.enabled) {
|
||||
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
|
||||
}
|
||||
|
||||
// Attach stream multiplexers
|
||||
if (this._modules.streamMuxer) {
|
||||
const muxers = this._modules.streamMuxer
|
||||
muxers.forEach((muxer) => {
|
||||
this.upgrader.muxers.set(muxer.multicodec, muxer)
|
||||
})
|
||||
|
||||
// Add the identify service since we can multiplex
|
||||
this.identifyService = new IdentifyService({
|
||||
registrar: this.registrar,
|
||||
peerInfo: this.peerInfo,
|
||||
protocols: this.upgrader.protocols
|
||||
})
|
||||
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
|
||||
}
|
||||
|
||||
// Attach private network protector
|
||||
if (this._modules.connProtector) {
|
||||
this._switch.protector = this._modules.connProtector
|
||||
this.upgrader.protector = this._modules.connProtector
|
||||
} else if (process.env.LIBP2P_FORCE_PNET) {
|
||||
throw new Error('Private network is enforced, but no protector was provided')
|
||||
}
|
||||
|
||||
// dht provided components (peerRouting, contentRouting, dht)
|
||||
if (this._config.dht.enabled) {
|
||||
if (this._modules.dht) {
|
||||
const DHT = this._modules.dht
|
||||
|
||||
this._dht = new DHT(this._switch, {
|
||||
this._dht = new DHT({
|
||||
dialer: this.dialer,
|
||||
peerInfo: this.peerInfo,
|
||||
peerStore: this.peerStore,
|
||||
registrar: this.registrar,
|
||||
datastore: this.datastore,
|
||||
...this._config.dht
|
||||
})
|
||||
}
|
||||
|
||||
// start pubsub
|
||||
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
|
||||
if (this._modules.pubsub) {
|
||||
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
|
||||
}
|
||||
|
||||
@ -131,65 +168,11 @@ class Libp2p extends EventEmitter {
|
||||
// peer and content routing will automatically get modules from _modules and _dht
|
||||
this.peerRouting = peerRouting(this)
|
||||
this.contentRouting = contentRouting(this)
|
||||
this.dht = dht(this)
|
||||
|
||||
// Mount default protocols
|
||||
Ping.mount(this._switch)
|
||||
ping.mount(this)
|
||||
|
||||
this.state = new FSM('STOPPED', {
|
||||
STOPPED: {
|
||||
start: 'STARTING',
|
||||
stop: 'STOPPED'
|
||||
},
|
||||
STARTING: {
|
||||
done: 'STARTED',
|
||||
abort: 'STOPPED',
|
||||
stop: 'STOPPING'
|
||||
},
|
||||
STARTED: {
|
||||
stop: 'STOPPING',
|
||||
start: 'STARTED'
|
||||
},
|
||||
STOPPING: {
|
||||
stop: 'STOPPING',
|
||||
done: 'STOPPED'
|
||||
}
|
||||
})
|
||||
this.state.on('STARTING', () => {
|
||||
log('libp2p is starting')
|
||||
this._onStarting()
|
||||
})
|
||||
this.state.on('STOPPING', () => {
|
||||
log('libp2p is stopping')
|
||||
this._onStopping()
|
||||
})
|
||||
this.state.on('STARTED', () => {
|
||||
log('libp2p has started')
|
||||
this.emit('start')
|
||||
})
|
||||
this.state.on('STOPPED', () => {
|
||||
log('libp2p has stopped')
|
||||
this.emit('stop')
|
||||
})
|
||||
this.state.on('error', (err) => {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
})
|
||||
|
||||
// Once we start, emit and dial any peers we may have already discovered
|
||||
this.state.on('STARTED', () => {
|
||||
this.peerBook.getAllArray().forEach((peerInfo) => {
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
})
|
||||
})
|
||||
|
||||
this._peerDiscovered = this._peerDiscovered.bind(this)
|
||||
|
||||
// promisify all instance methods
|
||||
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
|
||||
this[method] = promisify(this[method], { context: this })
|
||||
})
|
||||
this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -208,301 +191,246 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the libp2p node and all sub services
|
||||
* Starts the libp2p node and all its subsystems
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
start (callback = () => {}) {
|
||||
emitFirst(this, ['error', 'start'], callback)
|
||||
this.state('start')
|
||||
async start () {
|
||||
log('libp2p is starting')
|
||||
try {
|
||||
await this._onStarting()
|
||||
await this._onDidStart()
|
||||
log('libp2p has started')
|
||||
} catch (err) {
|
||||
this.emit('error', err)
|
||||
log.error('An error occurred starting libp2p', err)
|
||||
await this.stop()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @async
|
||||
* @returns {void}
|
||||
*/
|
||||
stop (callback = () => {}) {
|
||||
emitFirst(this, ['error', 'stop'], callback)
|
||||
this.state('stop')
|
||||
async stop () {
|
||||
log('libp2p is stopping')
|
||||
|
||||
try {
|
||||
for (const service of this._discovery.values()) {
|
||||
service.removeListener('peer', this._onDiscoveryPeer)
|
||||
}
|
||||
|
||||
await Promise.all(Array.from(this._discovery.values(), s => s.stop()))
|
||||
|
||||
this.connectionManager.stop()
|
||||
|
||||
await Promise.all([
|
||||
this.pubsub && this.pubsub.stop(),
|
||||
this._dht && this._dht.stop(),
|
||||
this.metrics && this.metrics.stop()
|
||||
])
|
||||
|
||||
await this.transportManager.close()
|
||||
await this.registrar.close()
|
||||
|
||||
ping.unmount(this)
|
||||
this.dialer.destroy()
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
}
|
||||
this._isStarted = false
|
||||
log('libp2p has stopped')
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
return this.state ? this.state._state === 'STARTED' : false
|
||||
return this._isStarted
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Map of the current connections. The keys are the stringified
|
||||
* `PeerId` of the peer. The value is an array of Connections to that peer.
|
||||
* @returns {Map<string, Connection[]>}
|
||||
*/
|
||||
get connections () {
|
||||
return this.registrar.connections
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials to the provided peer. If successful, the `PeerInfo` of the
|
||||
* peer will be added to the nodes `PeerBook`
|
||||
* peer will be added to the nodes `peerStore`
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
* @param {object} options
|
||||
* @param {AbortSignal} [options.signal]
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
dial (peer, callback) {
|
||||
this.dialProtocol(peer, null, callback)
|
||||
dial (peer, options) {
|
||||
return this.dialProtocol(peer, null, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials to the provided peer and handshakes with the given protocol.
|
||||
* If successful, the `PeerInfo` of the peer will be added to the nodes `PeerBook`,
|
||||
* If successful, the `PeerInfo` of the peer will be added to the nodes `peerStore`,
|
||||
* and the `Connection` will be sent in the callback
|
||||
*
|
||||
* @async
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
* @param {string[]|string} protocols
|
||||
* @param {object} options
|
||||
* @param {AbortSignal} [options.signal]
|
||||
* @returns {Promise<Connection|*>}
|
||||
*/
|
||||
dialProtocol (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
async dialProtocol (peer, protocols, options) {
|
||||
const dialable = Dialer.getDialable(peer)
|
||||
let connection
|
||||
if (PeerInfo.isPeerInfo(dialable)) {
|
||||
this.peerStore.put(dialable, { silent: true })
|
||||
connection = this.registrar.getConnection(dialable)
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
if (!connection) {
|
||||
connection = await this.dialer.connectToPeer(dialable, options)
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.dial(peerInfo, protocol, callback)
|
||||
}, callback)
|
||||
// If a protocol was provided, create a new stream
|
||||
if (protocols) {
|
||||
return connection.newStream(protocols)
|
||||
}
|
||||
|
||||
return connection
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `dial` and `dialProtocol`, but the callback will contain a
|
||||
* Connection State Machine.
|
||||
* Disconnects all connections to the given `peer`
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, ConnectionFSM)} callback
|
||||
* @returns {void}
|
||||
* @param {PeerInfo|PeerId|multiaddr|string} peer the peer to close connections to
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
dialFSM (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.dialFSM(peerInfo, protocol, callback)
|
||||
}, callback)
|
||||
hangUp (peer) {
|
||||
const peerInfo = getPeerInfo(peer, this.peerStore)
|
||||
return Promise.all(
|
||||
this.registrar.connections.get(peerInfo.id.toB58String()).map(connection => {
|
||||
return connection.close()
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the given peer
|
||||
*
|
||||
* Pings the given peer
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
hangUp (peer, callback) {
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
}, callback)
|
||||
async ping (peer) {
|
||||
const peerInfo = await getPeerInfo(peer, this.peerStore)
|
||||
|
||||
return ping(this, peerInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings the provided peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error, Ping)} callback
|
||||
* @returns {void}
|
||||
* Registers the `handler` for each protocol
|
||||
* @param {string[]|string} protocols
|
||||
* @param {function({ connection:*, stream:*, protocol:string })} handler
|
||||
*/
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('ping', this.state._state))
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
}, callback)
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
|
||||
_onStarting () {
|
||||
if (!this._modules.transport) {
|
||||
this.emit('error', new Error('no transports were present'))
|
||||
return this.state('abort')
|
||||
}
|
||||
|
||||
let ws
|
||||
|
||||
// so that we can have webrtc-star addrs without adding manually the id
|
||||
const maOld = []
|
||||
const maNew = []
|
||||
this.peerInfo.multiaddrs.toArray().forEach((ma) => {
|
||||
if (!ma.getPeerId()) {
|
||||
maOld.push(ma)
|
||||
maNew.push(ma.encapsulate('/p2p/' + this.peerInfo.id.toB58String()))
|
||||
}
|
||||
handle (protocols, handler) {
|
||||
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
||||
protocols.forEach(protocol => {
|
||||
this.upgrader.protocols.set(protocol, handler)
|
||||
})
|
||||
this.peerInfo.multiaddrs.replace(maOld, maNew)
|
||||
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted() && this.identifyService) {
|
||||
this.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the handler for each protocol. The protocol
|
||||
* will no longer be supported on streams.
|
||||
* @param {string[]|string} protocols
|
||||
*/
|
||||
unhandle (protocols) {
|
||||
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
||||
protocols.forEach(protocol => {
|
||||
this.upgrader.protocols.delete(protocol)
|
||||
})
|
||||
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted() && this.identifyService) {
|
||||
this.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
}
|
||||
|
||||
async _onStarting () {
|
||||
// Listen on the addresses supplied in the peerInfo
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
let t
|
||||
await this.transportManager.listen(multiaddrs)
|
||||
|
||||
if (typeof Transport === 'function') {
|
||||
t = new Transport({ libp2p: this })
|
||||
} else {
|
||||
t = Transport
|
||||
}
|
||||
// The addresses may change once the listener starts
|
||||
// eg /ip4/0.0.0.0/tcp/0 => /ip4/192.168.1.0/tcp/58751
|
||||
this.peerInfo.multiaddrs.clear()
|
||||
for (const ma of this.transportManager.getAddrs()) {
|
||||
this.peerInfo.multiaddrs.add(ma)
|
||||
}
|
||||
|
||||
if (t.filter(multiaddrs).length > 0) {
|
||||
this._switch.transport.add(t.tag || t[Symbol.toStringTag], t)
|
||||
} else if (WebSockets.isWebSockets(t)) {
|
||||
// TODO find a cleaner way to signal that a transport is always used
|
||||
// for dialing, even if no listener
|
||||
ws = t
|
||||
}
|
||||
this._transport.push(t)
|
||||
})
|
||||
if (this._config.pubsub.enabled) {
|
||||
this.pubsub && this.pubsub.start()
|
||||
}
|
||||
|
||||
series([
|
||||
(cb) => {
|
||||
this.connectionManager.start()
|
||||
this._switch.start(cb)
|
||||
},
|
||||
(cb) => {
|
||||
if (ws) {
|
||||
// always add dialing on websockets
|
||||
this._switch.transport.add(ws.tag || ws.constructor.name, ws)
|
||||
}
|
||||
// DHT subsystem
|
||||
if (this._config.dht.enabled) {
|
||||
this._dht && this._dht.start()
|
||||
|
||||
// detect which multiaddrs we don't have a transport for and remove them
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
// TODO: this should be modified once random-walk is used as
|
||||
// the other discovery modules
|
||||
this._dht.on('peer', this._onDiscoveryPeer)
|
||||
}
|
||||
|
||||
multiaddrs.forEach((multiaddr) => {
|
||||
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
|
||||
!this._transport.find((transport) => transport.filter(multiaddr).length > 0)) {
|
||||
this.peerInfo.multiaddrs.delete(multiaddr)
|
||||
}
|
||||
})
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
if (this._dht) {
|
||||
this._dht.start(() => {
|
||||
this._dht.on('peer', this._peerDiscovered)
|
||||
cb()
|
||||
})
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
(cb) => {
|
||||
if (this.pubsub) {
|
||||
return this.pubsub.start(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
// Peer Discovery
|
||||
(cb) => {
|
||||
if (this._modules.peerDiscovery) {
|
||||
this._setupPeerDiscovery(cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
], (err) => {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
return this.state('stop')
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
_onStopping () {
|
||||
series([
|
||||
(cb) => {
|
||||
// stop all discoveries before continuing with shutdown
|
||||
parallel(
|
||||
this._discovery.map((d) => {
|
||||
d.removeListener('peer', this._peerDiscovered)
|
||||
return (_cb) => d.stop((err) => {
|
||||
log.error('an error occurred stopping the discovery service', err)
|
||||
_cb()
|
||||
})
|
||||
}),
|
||||
cb
|
||||
)
|
||||
},
|
||||
(cb) => {
|
||||
if (this.pubsub) {
|
||||
return this.pubsub.stop(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
if (this._dht) {
|
||||
this._dht.removeListener('peer', this._peerDiscovered)
|
||||
return this._dht.stop(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
this.connectionManager.stop()
|
||||
this._switch.stop(cb)
|
||||
},
|
||||
(cb) => {
|
||||
// Ensures idempotent restarts, ignore any errors
|
||||
// from removeAll, they're not useful at this point
|
||||
this._switch.transport.removeAll(() => cb())
|
||||
}
|
||||
], (err) => {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
// Start metrics if present
|
||||
this.metrics && this.metrics.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles discovered peers. Each discovered peer will be emitted via
|
||||
* the `peer:discovery` event. If auto dial is enabled for libp2p
|
||||
* and the current connection count is under the low watermark, the
|
||||
* peer will be dialed.
|
||||
*
|
||||
* TODO: If `peerBook.put` becomes centralized, https://github.com/libp2p/js-libp2p/issues/345,
|
||||
* it would be ideal if only new peers were emitted. Currently, with
|
||||
* other modules adding peers to the `PeerBook` we have no way of knowing
|
||||
* if a peer is new or not, so it has to be emitted.
|
||||
*
|
||||
* Called when libp2p has started and before it returns
|
||||
* @private
|
||||
*/
|
||||
_onDidStart () {
|
||||
this._isStarted = true
|
||||
|
||||
this.connectionManager.start()
|
||||
|
||||
this.peerStore.on('peer', peerInfo => {
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
})
|
||||
|
||||
// Peer discovery
|
||||
this._setupPeerDiscovery()
|
||||
|
||||
// Once we start, emit and dial any peers we may have already discovered
|
||||
for (const peerInfo of this.peerStore.peers.values()) {
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called whenever peer discovery services emit `peer` events.
|
||||
* Known peers may be emitted.
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
_peerDiscovered (peerInfo) {
|
||||
_onDiscoveryPeer (peerInfo) {
|
||||
if (peerInfo.id.toB58String() === this.peerInfo.id.toB58String()) {
|
||||
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
||||
return
|
||||
}
|
||||
peerInfo = this.peerBook.put(peerInfo)
|
||||
|
||||
if (!this.isStarted()) return
|
||||
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
this.peerStore.put(peerInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -512,15 +440,17 @@ class Libp2p extends EventEmitter {
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
_maybeConnect (peerInfo) {
|
||||
// If auto dialing is on, check if we should dial
|
||||
if (this._config.peerDiscovery.autoDial === true && !peerInfo.isConnected()) {
|
||||
async _maybeConnect (peerInfo) {
|
||||
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
||||
if (this._config.peerDiscovery.autoDial === true && !this.registrar.getConnection(peerInfo)) {
|
||||
const minPeers = this._options.connectionManager.minPeers || 0
|
||||
if (minPeers > Object.keys(this._switch.connection.connections).length) {
|
||||
log('connecting to discovered peer')
|
||||
this._switch.dialer.connect(peerInfo, (err) => {
|
||||
err && log.error('could not connect to discovered peer', err)
|
||||
})
|
||||
if (minPeers > this.connectionManager._connections.size) {
|
||||
log('connecting to discovered peer %s', peerInfo.id.toB58String())
|
||||
try {
|
||||
await this.dialer.connectToPeer(peerInfo)
|
||||
} catch (err) {
|
||||
log.error('could not connect to discovered peer', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -529,10 +459,10 @@ class Libp2p extends EventEmitter {
|
||||
* Initializes and starts peer discovery services
|
||||
*
|
||||
* @private
|
||||
* @param {function(Error)} callback
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
_setupPeerDiscovery (callback) {
|
||||
for (const DiscoveryService of this._modules.peerDiscovery) {
|
||||
_setupPeerDiscovery () {
|
||||
const setupService = (DiscoveryService) => {
|
||||
let config = {
|
||||
enabled: true // on by default
|
||||
}
|
||||
@ -543,7 +473,8 @@ class Libp2p extends EventEmitter {
|
||||
config = { ...config, ...this._config.peerDiscovery[DiscoveryService.tag] }
|
||||
}
|
||||
|
||||
if (config.enabled) {
|
||||
if (config.enabled &&
|
||||
!this._discovery.has(DiscoveryService.tag)) { // not already added
|
||||
let discoveryService
|
||||
|
||||
if (typeof DiscoveryService === 'function') {
|
||||
@ -552,14 +483,24 @@ class Libp2p extends EventEmitter {
|
||||
discoveryService = DiscoveryService
|
||||
}
|
||||
|
||||
discoveryService.on('peer', this._peerDiscovered)
|
||||
this._discovery.push(discoveryService)
|
||||
discoveryService.on('peer', this._onDiscoveryPeer)
|
||||
this._discovery.set(DiscoveryService.tag, discoveryService)
|
||||
}
|
||||
}
|
||||
|
||||
each(this._discovery, (d, cb) => {
|
||||
d.start(cb)
|
||||
}, callback)
|
||||
// Discovery modules
|
||||
for (const DiscoveryService of this._modules.peerDiscovery || []) {
|
||||
setupService(DiscoveryService)
|
||||
}
|
||||
|
||||
// Transport modules with discovery
|
||||
for (const Transport of this.transportManager.getTransports()) {
|
||||
if (Transport.discovery) {
|
||||
setupService(Transport.discovery)
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all(Array.from(this._discovery.values(), d => d.start()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -568,16 +509,15 @@ module.exports = Libp2p
|
||||
* Like `new Libp2p(options)` except it will create a `PeerInfo`
|
||||
* instance if one is not provided in options.
|
||||
* @param {object} options Libp2p configuration options
|
||||
* @param {function(Error, Libp2p)} callback
|
||||
* @returns {void}
|
||||
* @returns {Libp2p}
|
||||
*/
|
||||
module.exports.createLibp2p = promisify((options, callback) => {
|
||||
module.exports.create = async (options = {}) => {
|
||||
if (options.peerInfo) {
|
||||
return nextTick(callback, null, new Libp2p(options))
|
||||
return new Libp2p(options)
|
||||
}
|
||||
PeerInfo.create((err, peerInfo) => {
|
||||
if (err) return callback(err)
|
||||
options.peerInfo = peerInfo
|
||||
callback(null, new Libp2p(options))
|
||||
})
|
||||
})
|
||||
|
||||
const peerInfo = await PeerInfo.create()
|
||||
|
||||
options.peerInfo = peerInfo
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
67
src/insecure/plaintext.js
Normal file
67
src/insecure/plaintext.js
Normal file
@ -0,0 +1,67 @@
|
||||
'use strict'
|
||||
|
||||
const handshake = require('it-handshake')
|
||||
const lp = require('it-length-prefixed')
|
||||
const PeerId = require('peer-id')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:plaintext')
|
||||
log.error = debug('libp2p:plaintext:error')
|
||||
const { UnexpectedPeerError, InvalidCryptoExchangeError } = require('libp2p-interfaces/src/crypto/errors')
|
||||
|
||||
const { Exchange, KeyType } = require('./proto')
|
||||
const protocol = '/plaintext/2.0.0'
|
||||
|
||||
function lpEncodeExchange (exchange) {
|
||||
const pb = Exchange.encode(exchange)
|
||||
return lp.encode.single(pb)
|
||||
}
|
||||
|
||||
async function encrypt (localId, conn, remoteId) {
|
||||
const shake = handshake(conn)
|
||||
|
||||
// Encode the public key and write it to the remote peer
|
||||
shake.write(lpEncodeExchange({
|
||||
id: localId.toBytes(),
|
||||
pubkey: {
|
||||
Type: KeyType.RSA, // TODO: dont hard code
|
||||
Data: localId.marshalPubKey()
|
||||
}
|
||||
}))
|
||||
|
||||
log('write pubkey exchange to peer %j', remoteId)
|
||||
|
||||
// Get the Exchange message
|
||||
const response = (await lp.decode.fromReader(shake.reader).next()).value
|
||||
const id = Exchange.decode(response.slice())
|
||||
log('read pubkey exchange from peer %j', remoteId)
|
||||
|
||||
let peerId
|
||||
try {
|
||||
peerId = await PeerId.createFromPubKey(id.pubkey.Data)
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
throw new InvalidCryptoExchangeError('Remote did not provide its public key')
|
||||
}
|
||||
|
||||
if (remoteId && !peerId.isEqual(remoteId)) {
|
||||
throw new UnexpectedPeerError()
|
||||
}
|
||||
|
||||
log('plaintext key exchange completed successfully with peer %j', peerId)
|
||||
|
||||
shake.rest()
|
||||
return {
|
||||
conn: shake.stream,
|
||||
remotePeer: peerId
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
protocol,
|
||||
secureInbound: (localId, conn, remoteId) => {
|
||||
return encrypt(localId, conn, remoteId)
|
||||
},
|
||||
secureOutbound: (localId, conn, remoteId) => {
|
||||
return encrypt(localId, conn, remoteId)
|
||||
}
|
||||
}
|
22
src/insecure/proto.js
Normal file
22
src/insecure/proto.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const protobuf = require('protons')
|
||||
|
||||
module.exports = protobuf(`
|
||||
message Exchange {
|
||||
optional bytes id = 1;
|
||||
optional PublicKey pubkey = 2;
|
||||
}
|
||||
|
||||
enum KeyType {
|
||||
RSA = 0;
|
||||
Ed25519 = 1;
|
||||
Secp256k1 = 2;
|
||||
ECDSA = 3;
|
||||
}
|
||||
|
||||
message PublicKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
||||
`)
|
247
src/metrics/index.js
Normal file
247
src/metrics/index.js
Normal file
@ -0,0 +1,247 @@
|
||||
'use strict'
|
||||
|
||||
const mergeOptions = require('merge-options')
|
||||
const pipe = require('it-pipe')
|
||||
const { tap } = require('streaming-iterables')
|
||||
const oldPeerLRU = require('./old-peers')
|
||||
const { METRICS: defaultOptions } = require('../constants')
|
||||
const Stats = require('./stats')
|
||||
|
||||
const initialCounters = [
|
||||
'dataReceived',
|
||||
'dataSent'
|
||||
]
|
||||
|
||||
const directionToEvent = {
|
||||
in: 'dataReceived',
|
||||
out: 'dataSent'
|
||||
}
|
||||
|
||||
class Metrics {
|
||||
/**
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {number} options.computeThrottleMaxQueueSize
|
||||
* @param {number} options.computeThrottleTimeout
|
||||
* @param {Array<number>} options.movingAverageIntervals
|
||||
* @param {number} options.maxOldPeersRetention
|
||||
*/
|
||||
constructor (options) {
|
||||
this._options = mergeOptions(defaultOptions, options)
|
||||
this._globalStats = new Stats(initialCounters, this._options)
|
||||
this._peerStats = new Map()
|
||||
this._protocolStats = new Map()
|
||||
this._oldPeers = oldPeerLRU(this._options.maxOldPeersRetention)
|
||||
this._running = false
|
||||
this._onMessage = this._onMessage.bind(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Must be called for stats to saved. Any data pushed for tracking
|
||||
* will be ignored.
|
||||
*/
|
||||
start () {
|
||||
this._running = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all averages timers and prevents new data from being tracked.
|
||||
* Once `stop` is called, `start` must be called to resume stats tracking.
|
||||
*/
|
||||
stop () {
|
||||
this._running = false
|
||||
this._globalStats.stop()
|
||||
for (const stats of this._peerStats.values()) {
|
||||
stats.stop()
|
||||
}
|
||||
for (const stats of this._protocolStats.values()) {
|
||||
stats.stop()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the global `Stats` object
|
||||
* @returns {Stats}
|
||||
*/
|
||||
get global () {
|
||||
return this._globalStats
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of `PeerId` strings currently being tracked
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
get peers () {
|
||||
return Array.from(this._peerStats.keys())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `Stats` object for the given `PeerId` whether it
|
||||
* is a live peer, or in the disconnected peer LRU cache.
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Stats}
|
||||
*/
|
||||
forPeer (peerId) {
|
||||
const idString = peerId.toB58String()
|
||||
return this._peerStats.get(idString) || this._oldPeers.get(idString)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all protocol strings currently being tracked.
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
get protocols () {
|
||||
return Array.from(this._protocolStats.keys())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `Stats` object for the given `protocol`.
|
||||
* @param {string} protocol
|
||||
* @returns {Stats}
|
||||
*/
|
||||
forProtocol (protocol) {
|
||||
return this._protocolStats.get(protocol)
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called when all connections to a given peer
|
||||
* have closed. The `Stats` collection for the peer will
|
||||
* be stopped and moved to an LRU for temporary retention.
|
||||
* @param {PeerId} peerId
|
||||
*/
|
||||
onPeerDisconnected (peerId) {
|
||||
const idString = peerId.toB58String()
|
||||
const peerStats = this._peerStats.get(idString)
|
||||
if (peerStats) {
|
||||
peerStats.stop()
|
||||
this._peerStats.delete(idString)
|
||||
this._oldPeers.set(idString, peerStats)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the metadata for a message and tracks it in the
|
||||
* appropriate categories. If the protocol is present, protocol
|
||||
* stats will also be tracked.
|
||||
*
|
||||
* @private
|
||||
* @param {object} params
|
||||
* @param {PeerId} params.remotePeer Remote peer
|
||||
* @param {string} [params.protocol] Protocol string the stream is running
|
||||
* @param {string} params.direction One of ['in','out']
|
||||
* @param {number} params.dataLength Size of the message
|
||||
* @returns {void}
|
||||
*/
|
||||
_onMessage ({ remotePeer, protocol, direction, dataLength }) {
|
||||
if (!this._running) return
|
||||
|
||||
const key = directionToEvent[direction]
|
||||
|
||||
let peerStats = this.forPeer(remotePeer)
|
||||
if (!peerStats) {
|
||||
peerStats = new Stats(initialCounters, this._options)
|
||||
this._peerStats.set(remotePeer.toB58String(), peerStats)
|
||||
}
|
||||
|
||||
// Peer and global stats
|
||||
peerStats.push(key, dataLength)
|
||||
this._globalStats.push(key, dataLength)
|
||||
|
||||
// Protocol specific stats
|
||||
if (protocol) {
|
||||
let protocolStats = this.forProtocol(protocol)
|
||||
if (!protocolStats) {
|
||||
protocolStats = new Stats(initialCounters, this._options)
|
||||
this._protocolStats.set(protocol, protocolStats)
|
||||
}
|
||||
protocolStats.push(key, dataLength)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the `PeerId` string with the given `peerId`.
|
||||
* If stats are already being tracked for the given `peerId`, the
|
||||
* placeholder stats will be merged with the existing stats.
|
||||
* @param {PeerId} placeholder A peerId string
|
||||
* @param {PeerId} peerId
|
||||
*/
|
||||
updatePlaceholder (placeholder, peerId) {
|
||||
if (!this._running) return
|
||||
const placeholderStats = this.forPeer(placeholder)
|
||||
const peerIdString = peerId.toB58String()
|
||||
const existingStats = this.forPeer(peerId)
|
||||
let mergedStats = placeholderStats
|
||||
|
||||
// If we already have stats, merge the two
|
||||
if (existingStats) {
|
||||
// If existing, merge
|
||||
mergedStats = Metrics.mergeStats(existingStats, mergedStats)
|
||||
// Attempt to delete from the old peers list just in case it was tracked there
|
||||
this._oldPeers.delete(peerIdString)
|
||||
}
|
||||
|
||||
this._peerStats.delete(placeholder.toB58String())
|
||||
this._peerStats.set(peerIdString, mergedStats)
|
||||
mergedStats.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks data running through a given Duplex Iterable `stream`. If
|
||||
* the `peerId` is not provided, a placeholder string will be created and
|
||||
* returned. This allows lazy tracking of a peer when the peer is not yet known.
|
||||
* When the `PeerId` is known, `Metrics.updatePlaceholder` should be called
|
||||
* with the placeholder string returned from here, and the known `PeerId`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {{ sink: function(*), source: function() }} options.stream A duplex iterable stream
|
||||
* @param {PeerId} [options.peerId] The id of the remote peer that's connected
|
||||
* @param {string} [options.protocol] The protocol the stream is running
|
||||
* @returns {string} The peerId string or placeholder string
|
||||
*/
|
||||
trackStream ({ stream, remotePeer, protocol }) {
|
||||
const metrics = this
|
||||
const _source = stream.source
|
||||
stream.source = tap(chunk => metrics._onMessage({
|
||||
remotePeer,
|
||||
protocol,
|
||||
direction: 'in',
|
||||
dataLength: chunk.length
|
||||
}))(_source)
|
||||
|
||||
const _sink = stream.sink
|
||||
stream.sink = source => {
|
||||
pipe(
|
||||
source,
|
||||
tap(chunk => metrics._onMessage({
|
||||
remotePeer,
|
||||
protocol,
|
||||
direction: 'out',
|
||||
dataLength: chunk.length
|
||||
})),
|
||||
_sink
|
||||
)
|
||||
}
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges `other` into `target`. `target` will be modified
|
||||
* and returned.
|
||||
* @param {Stats} target
|
||||
* @param {Stats} other
|
||||
* @returns {Stats}
|
||||
*/
|
||||
static mergeStats (target, other) {
|
||||
target.stop()
|
||||
other.stop()
|
||||
|
||||
// Merge queues
|
||||
target._queue = [...target._queue, ...other._queue]
|
||||
|
||||
// TODO: how to merge moving averages?
|
||||
return target
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Metrics
|
@ -83,6 +83,31 @@ class Stats extends EventEmitter {
|
||||
return Object.assign({}, this._movingAverages)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain JSON object of the stats
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
toJSON () {
|
||||
const snapshot = this.snapshot
|
||||
const movingAverages = this.movingAverages
|
||||
const data = {
|
||||
dataReceived: snapshot.dataReceived.toString(),
|
||||
dataSent: snapshot.dataSent.toString(),
|
||||
movingAverages: {}
|
||||
}
|
||||
|
||||
const counters = Object.keys(movingAverages)
|
||||
for (const key of counters) {
|
||||
data.movingAverages[key] = {}
|
||||
for (const interval of Object.keys(movingAverages[key])) {
|
||||
data.movingAverages[key][interval] = movingAverages[key][interval].movingAverage()
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes the given operation data to the queue, along with the
|
||||
* current Timestamp, then resets the update timer.
|
||||
@ -151,7 +176,7 @@ class Stats extends EventEmitter {
|
||||
}
|
||||
|
||||
/**
|
||||
* For each key in the stats, the frequncy and moving averages
|
||||
* For each key in the stats, the frequency and moving averages
|
||||
* will be updated via Stats._updateFrequencyFor based on the time
|
||||
* difference between calls to this method.
|
||||
*
|
@ -1,8 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
const pAny = require('p-any')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.peerRouting || []
|
||||
@ -17,43 +16,25 @@ module.exports = (node) => {
|
||||
* Iterates over all peer routers in series to find the given peer.
|
||||
*
|
||||
* @param {String} id The id of the peer to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
* @param {object} [options]
|
||||
* @param {number} [options.timeout] How long the query should run
|
||||
* @returns {Promise<PeerInfo>}
|
||||
*/
|
||||
findPeer: promisify((id, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
findPeer: async (id, options) => { // eslint-disable-line require-await
|
||||
if (!routers.length) {
|
||||
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findPeer(id, options, (err, result) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
return pAny(routers.map(async (router) => {
|
||||
const result = await router.findPeer(id, options)
|
||||
|
||||
// If we don't have a result, we need to provide an error to keep trying
|
||||
if (!result || Object.keys(result).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
}
|
||||
|
||||
cb(null, result)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
// If we don't have a result, we need to provide an error to keep trying
|
||||
if (!result || Object.keys(result).length === 0) {
|
||||
throw errCode(new Error('not found'), 'NOT_FOUND')
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
})
|
||||
|
||||
return result
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
3
src/peer-store/README.md
Normal file
3
src/peer-store/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Peerstore
|
||||
|
||||
WIP
|
236
src/peer-store/index.js
Normal file
236
src/peer-store/index.js
Normal file
@ -0,0 +1,236 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:peer-store')
|
||||
log.error = debug('libp2p:peer-store:error')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
/**
|
||||
* Responsible for managing known peers, as well as their addresses and metadata
|
||||
* @fires PeerStore#peer Emitted when a peer is connected to this node
|
||||
* @fires PeerStore#change:protocols
|
||||
* @fires PeerStore#change:multiaddrs
|
||||
*/
|
||||
class PeerStore extends EventEmitter {
|
||||
constructor () {
|
||||
super()
|
||||
|
||||
/**
|
||||
* Map of peers
|
||||
*
|
||||
* @type {Map<string, PeerInfo>}
|
||||
*/
|
||||
this.peers = new Map()
|
||||
|
||||
// TODO: Track ourselves. We should split `peerInfo` up into its pieces so we get better
|
||||
// control and observability. This will be the initial step for removing PeerInfo
|
||||
// https://github.com/libp2p/go-libp2p-core/blob/master/peerstore/peerstore.go
|
||||
// this.addressBook = new Map()
|
||||
// this.protoBook = new Map()
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the peerInfo of a new peer.
|
||||
* If already exist, its info is updated. If `silent` is set to
|
||||
* true, no 'peer' event will be emitted. This can be useful if you
|
||||
* are already in the process of dialing the peer. The peer is technically
|
||||
* known, but may not have been added to the PeerStore yet.
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {object} [options]
|
||||
* @param {boolean} [options.silent] (Default=false)
|
||||
* @return {PeerInfo}
|
||||
*/
|
||||
put (peerInfo, options = { silent: false }) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
let peer
|
||||
// Already know the peer?
|
||||
if (this.has(peerInfo.id)) {
|
||||
peer = this.update(peerInfo)
|
||||
} else {
|
||||
peer = this.add(peerInfo)
|
||||
|
||||
// Emit the peer if silent = false
|
||||
!options.silent && this.emit('peer', peerInfo)
|
||||
}
|
||||
return peer
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new peer to the store.
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @return {PeerInfo}
|
||||
*/
|
||||
add (peerInfo) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
// Create new instance and add values to it
|
||||
const newPeerInfo = new PeerInfo(peerInfo.id)
|
||||
|
||||
peerInfo.multiaddrs.forEach((ma) => newPeerInfo.multiaddrs.add(ma))
|
||||
peerInfo.protocols.forEach((p) => newPeerInfo.protocols.add(p))
|
||||
|
||||
const connectedMa = peerInfo.isConnected()
|
||||
connectedMa && newPeerInfo.connect(connectedMa)
|
||||
|
||||
const peerProxy = new Proxy(newPeerInfo, {
|
||||
set: (obj, prop, value) => {
|
||||
if (prop === 'multiaddrs') {
|
||||
this.emit('change:multiaddrs', {
|
||||
peerInfo: obj,
|
||||
multiaddrs: value.toArray()
|
||||
})
|
||||
} else if (prop === 'protocols') {
|
||||
this.emit('change:protocols', {
|
||||
peerInfo: obj,
|
||||
protocols: Array.from(value)
|
||||
})
|
||||
}
|
||||
return Reflect.set(...arguments)
|
||||
}
|
||||
})
|
||||
|
||||
this.peers.set(peerInfo.id.toB58String(), peerProxy)
|
||||
return peerProxy
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an already known peer.
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @return {PeerInfo}
|
||||
*/
|
||||
update (peerInfo) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
const id = peerInfo.id.toB58String()
|
||||
const recorded = this.peers.get(id)
|
||||
|
||||
// pass active connection state
|
||||
const ma = peerInfo.isConnected()
|
||||
if (ma) {
|
||||
recorded.connect(ma)
|
||||
}
|
||||
|
||||
// Verify new multiaddrs
|
||||
// TODO: better track added and removed multiaddrs
|
||||
const multiaddrsIntersection = [
|
||||
...recorded.multiaddrs.toArray()
|
||||
].filter((m) => peerInfo.multiaddrs.has(m))
|
||||
|
||||
if (multiaddrsIntersection.length !== peerInfo.multiaddrs.size ||
|
||||
multiaddrsIntersection.length !== recorded.multiaddrs.size) {
|
||||
for (const ma of peerInfo.multiaddrs.toArray()) {
|
||||
recorded.multiaddrs.add(ma)
|
||||
}
|
||||
|
||||
this.emit('change:multiaddrs', {
|
||||
peerInfo: recorded,
|
||||
multiaddrs: recorded.multiaddrs.toArray()
|
||||
})
|
||||
}
|
||||
|
||||
// Update protocols
|
||||
// TODO: better track added and removed protocols
|
||||
const protocolsIntersection = new Set(
|
||||
[...recorded.protocols].filter((p) => peerInfo.protocols.has(p))
|
||||
)
|
||||
|
||||
if (protocolsIntersection.size !== peerInfo.protocols.size ||
|
||||
protocolsIntersection.size !== recorded.protocols.size) {
|
||||
for (const protocol of peerInfo.protocols) {
|
||||
recorded.protocols.add(protocol)
|
||||
}
|
||||
|
||||
this.emit('change:protocols', {
|
||||
peerInfo: recorded,
|
||||
protocols: Array.from(recorded.protocols)
|
||||
})
|
||||
}
|
||||
|
||||
// Add the public key if missing
|
||||
if (!recorded.id.pubKey && peerInfo.id.pubKey) {
|
||||
recorded.id.pubKey = peerInfo.id.pubKey
|
||||
}
|
||||
|
||||
return recorded
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the info to the given id.
|
||||
* @param {PeerId|string} peerId b58str id
|
||||
* @returns {PeerInfo}
|
||||
*/
|
||||
get (peerId) {
|
||||
// TODO: deprecate this and just accept `PeerId` instances
|
||||
if (PeerId.isPeerId(peerId)) {
|
||||
peerId = peerId.toB58String()
|
||||
}
|
||||
|
||||
return this.peers.get(peerId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the info to the given id.
|
||||
* @param {PeerId|string} peerId b58str id
|
||||
* @returns {boolean}
|
||||
*/
|
||||
has (peerId) {
|
||||
// TODO: deprecate this and just accept `PeerId` instances
|
||||
if (PeerId.isPeerId(peerId)) {
|
||||
peerId = peerId.toB58String()
|
||||
}
|
||||
|
||||
return this.peers.has(peerId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the Peer with the matching `peerId` from the PeerStore
|
||||
* @param {PeerId|string} peerId b58str id
|
||||
* @returns {boolean} true if found and removed
|
||||
*/
|
||||
remove (peerId) {
|
||||
// TODO: deprecate this and just accept `PeerId` instances
|
||||
if (PeerId.isPeerId(peerId)) {
|
||||
peerId = peerId.toB58String()
|
||||
}
|
||||
|
||||
return this.peers.delete(peerId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Completely replaces the existing peers metadata with the given `peerInfo`
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @returns {void}
|
||||
*/
|
||||
replace (peerInfo) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
this.remove(peerInfo.id.toB58String())
|
||||
this.add(peerInfo)
|
||||
|
||||
// This should be cleaned up in PeerStore v2
|
||||
this.emit('change:multiaddrs', {
|
||||
peerInfo,
|
||||
multiaddrs: peerInfo.multiaddrs.toArray()
|
||||
})
|
||||
this.emit('change:protocols', {
|
||||
peerInfo,
|
||||
protocols: Array.from(peerInfo.protocols)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the known multiaddrs for a given `PeerInfo`
|
||||
* @param {PeerInfo} peer
|
||||
* @returns {Array<Multiaddr>}
|
||||
*/
|
||||
multiaddrsForPeer (peer) {
|
||||
return this.put(peer, true).multiaddrs.toArray()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PeerStore
|
@ -8,16 +8,11 @@ libp2p-ping JavaScript Implementation
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
var Ping = require('libp2p-ping')
|
||||
var Ping = require('libp2p/src/ping')
|
||||
|
||||
Ping.mount(swarm) // Enable this peer to echo Ping requests
|
||||
Ping.mount(libp2p) // Enable this peer to echo Ping requests
|
||||
|
||||
var p = new Ping(swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object
|
||||
const latency = await Ping(libp2p, peerDst)
|
||||
|
||||
p.on('ping', function (time) {
|
||||
console.log(time + 'ms')
|
||||
p.stop() // stop sending pings
|
||||
})
|
||||
|
||||
p.start()
|
||||
Ping.unmount(libp2p)
|
||||
```
|
||||
|
@ -1,50 +0,0 @@
|
||||
'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
|
@ -1,7 +1,62 @@
|
||||
'use strict'
|
||||
|
||||
const handler = require('./handler')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p-ping')
|
||||
log.error = debug('libp2p-ping:error')
|
||||
const errCode = require('err-code')
|
||||
|
||||
exports = module.exports = require('./ping')
|
||||
exports.mount = handler.mount
|
||||
exports.unmount = handler.unmount
|
||||
const crypto = require('libp2p-crypto')
|
||||
const pipe = require('it-pipe')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
const { collect } = require('streaming-iterables')
|
||||
|
||||
const { PROTOCOL, PING_LENGTH } = require('./constants')
|
||||
|
||||
/**
|
||||
* Ping a given peer and wait for its response, getting the operation latency.
|
||||
* @param {Libp2p} node
|
||||
* @param {PeerInfo} peer
|
||||
* @returns {Promise<Number>}
|
||||
*/
|
||||
async function ping (node, peer) {
|
||||
log('dialing %s to %s', PROTOCOL, peer.id.toB58String())
|
||||
|
||||
const { stream } = await node.dialProtocol(peer, PROTOCOL)
|
||||
|
||||
const start = new Date()
|
||||
const data = crypto.randomBytes(PING_LENGTH)
|
||||
|
||||
const [result] = await pipe(
|
||||
[data],
|
||||
stream,
|
||||
toBuffer,
|
||||
collect
|
||||
)
|
||||
const end = Date.now()
|
||||
|
||||
if (!data.equals(result)) {
|
||||
throw errCode(new Error('Received wrong ping ack'), 'ERR_WRONG_PING_ACK')
|
||||
}
|
||||
|
||||
return end - start
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe ping protocol handler.
|
||||
* @param {Libp2p} node
|
||||
*/
|
||||
function mount (node) {
|
||||
node.handle(PROTOCOL, ({ stream }) => pipe(stream, stream))
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe ping protocol handler.
|
||||
* @param {Libp2p} node
|
||||
*/
|
||||
function unmount (node) {
|
||||
node.unhandle(PROTOCOL)
|
||||
}
|
||||
|
||||
exports = module.exports = ping
|
||||
exports.mount = mount
|
||||
exports.unmount = unmount
|
||||
|
@ -1,83 +0,0 @@
|
||||
'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
|
@ -1,6 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const debug = require('debug')
|
||||
const Errors = require('./errors')
|
||||
const xsalsa20 = require('xsalsa20')
|
||||
@ -8,45 +7,40 @@ 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')
|
||||
log.error = debug('libp2p:pnet:err')
|
||||
|
||||
/**
|
||||
* Creates a pull stream to encrypt messages in a private network
|
||||
* Creates a stream iterable 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
|
||||
* @returns {*} a through iterable
|
||||
*/
|
||||
module.exports.createBoxStream = (nonce, psk) => {
|
||||
const xor = xsalsa20(nonce, psk)
|
||||
return pull(
|
||||
ensureBuffer(),
|
||||
pull.map((chunk) => {
|
||||
return xor.update(chunk, chunk)
|
||||
})
|
||||
)
|
||||
return (source) => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a pull stream to decrypt messages in a private network
|
||||
* Creates a stream iterable to decrypt messages in a private network
|
||||
*
|
||||
* @param {Object} remote Holds the nonce of the peer
|
||||
* @param {Buffer} nonce The nonce of the remote peer
|
||||
* @param {Buffer} psk The private shared key to use in decryption
|
||||
* @returns {PullStream} a through stream
|
||||
* @returns {*} a through iterable
|
||||
*/
|
||||
module.exports.createUnboxStream = (remote, psk) => {
|
||||
let xor
|
||||
return pull(
|
||||
ensureBuffer(),
|
||||
pull.map((chunk) => {
|
||||
if (!xor) {
|
||||
xor = xsalsa20(remote.nonce, psk)
|
||||
log.trace('Decryption enabled')
|
||||
}
|
||||
module.exports.createUnboxStream = (nonce, psk) => {
|
||||
return (source) => (async function * () {
|
||||
const xor = xsalsa20(nonce, psk)
|
||||
log.trace('Decryption enabled')
|
||||
|
||||
return xor.update(chunk, chunk)
|
||||
})
|
||||
)
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,7 +55,7 @@ module.exports.decodeV1PSK = (pskBuffer) => {
|
||||
// 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
|
||||
// programmatically instead of making assumptions about the
|
||||
// encodings of each line.
|
||||
const metadata = pskBuffer.toString().split(/(?:\r\n|\r|\n)/g)
|
||||
const pskTag = metadata.shift()
|
||||
@ -78,21 +72,7 @@ module.exports.decodeV1PSK = (pskBuffer) => {
|
||||
psk: psk
|
||||
}
|
||||
} catch (err) {
|
||||
log.error(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
|
||||
})
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const Connection = require('interface-connection').Connection
|
||||
const pipe = require('it-pipe')
|
||||
const assert = require('assert')
|
||||
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const crypto = require('libp2p-crypto')
|
||||
const Errors = require('./errors')
|
||||
const State = require('./state')
|
||||
const decodeV1PSK = require('./crypto').decodeV1PSK
|
||||
const {
|
||||
createBoxStream,
|
||||
createUnboxStream,
|
||||
decodeV1PSK
|
||||
} = require('./crypto')
|
||||
const handshake = require('it-handshake')
|
||||
const { NONCE_LENGTH } = require('./key-generator')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:pnet')
|
||||
log.err = debug('libp2p:pnet:err')
|
||||
@ -27,41 +32,41 @@ class Protector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a given Connection and creates a privaste encryption stream
|
||||
* Takes a given Connection and creates a private 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
|
||||
* @returns {*} A protected duplex iterable
|
||||
*/
|
||||
protect (connection, callback) {
|
||||
async protect (connection) {
|
||||
assert(connection, Errors.NO_HANDSHAKE_CONNECTION)
|
||||
|
||||
const protectedConnection = new Connection(undefined, connection)
|
||||
const state = new State(this.psk)
|
||||
|
||||
// Exchange nonces
|
||||
log('protecting the connection')
|
||||
const localNonce = crypto.randomBytes(NONCE_LENGTH)
|
||||
|
||||
// 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)
|
||||
}
|
||||
const shake = handshake(connection)
|
||||
shake.write(localNonce)
|
||||
|
||||
connection.getPeerInfo(() => {
|
||||
protectedConnection.setInnerConn(new Connection(encryptedOuterStream, connection))
|
||||
log('the connection has been successfully wrapped by the protector')
|
||||
callback()
|
||||
})
|
||||
}),
|
||||
connection
|
||||
const result = await shake.reader.next(NONCE_LENGTH)
|
||||
const remoteNonce = result.value.slice()
|
||||
shake.rest()
|
||||
|
||||
// Create the boxing/unboxing pipe
|
||||
log('exchanged nonces')
|
||||
const [internal, external] = duplexPair()
|
||||
pipe(
|
||||
external,
|
||||
// Encrypt all outbound traffic
|
||||
createBoxStream(localNonce, this.psk),
|
||||
shake.stream,
|
||||
// Decrypt all inbound traffic
|
||||
createUnboxStream(remoteNonce, this.psk),
|
||||
external
|
||||
)
|
||||
|
||||
return protectedConnection
|
||||
return internal
|
||||
}
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user