mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 21:31:32 +00:00
Compare commits
60 Commits
docs/deleg
...
refactor-a
Author | SHA1 | Date | |
---|---|---|---|
ddca720b2a | |||
1ca2d87287 | |||
340edf53e3 | |||
4cc9736485 | |||
cdfd02306f | |||
dcd127b7a5 | |||
751bc00f1b | |||
ec5319a50e | |||
dca4727112 | |||
695d71e44f | |||
0fc6668321 | |||
d7c38d3fec | |||
53bcd2d745 | |||
fba058370a | |||
a23d4d23cb | |||
6ecc9b80c3 | |||
cd97abfcc3 | |||
c1da30bc74 | |||
35aa45ce92 | |||
b11c6fc7e9 | |||
ebedd3510b | |||
ae6af20e8e | |||
2a80618740 | |||
5b1bd389f8 | |||
3e31c2d0df | |||
8079c2078b | |||
80cf0777b5 | |||
60b0cbc179 | |||
3eef695bc0 | |||
b3deb356f1 | |||
299cfefa01 | |||
aa95ab9928 | |||
b0f124b5ff | |||
b294301456 | |||
d92306f222 | |||
fd738f9d51 | |||
d788433b43 | |||
d5a977b227 | |||
0489972b4b | |||
3f31b1f422 | |||
a2b3446ed7 | |||
ff7a6c86a0 | |||
9a8d609a59 | |||
9fef58cb7d | |||
684f283aec | |||
3e95e6f9e4 | |||
f4f3f0f03a | |||
7c2c852fc0 | |||
e8d8aab278 | |||
dd48d268ec | |||
99a53592e2 | |||
2a2e7a1012 | |||
791f39a09b | |||
65d52857a5 | |||
48b1b442e9 | |||
9554b05c6f | |||
df6ef45a2d | |||
b4a70ea476 | |||
45716da465 | |||
905c911946 |
100
.aegir.js
100
.aegir.js
@ -1,82 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const parallel = require('async/parallel')
|
||||
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
|
||||
const sigServer = require('libp2p-webrtc-star/src/sig-server')
|
||||
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 WebSockets = require('libp2p-websockets')
|
||||
const Muxer = require('libp2p-mplex')
|
||||
const Crypto = require('./src/insecure/plaintext')
|
||||
const pipe = require('it-pipe')
|
||||
let libp2p
|
||||
|
||||
const Node = require('./test/utils/bundle-nodejs.js')
|
||||
const {
|
||||
getPeerRelay,
|
||||
WRTC_RENDEZVOUS_MULTIADDR,
|
||||
WS_RENDEZVOUS_MULTIADDR
|
||||
} = require('./test/utils/constants')
|
||||
const before = async () => {
|
||||
// Use the last peer
|
||||
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
peerInfo.multiaddrs.add(MULTIADDRS_WEBSOCKETS[0])
|
||||
|
||||
let wrtcRendezvous
|
||||
let wsRendezvous
|
||||
let node
|
||||
|
||||
const before = (done) => {
|
||||
parallel([
|
||||
(cb) => {
|
||||
sigServer.start({
|
||||
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
|
||||
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
|
||||
})
|
||||
.then(server => {
|
||||
wrtcRendezvous = server
|
||||
cb()
|
||||
})
|
||||
.catch(cb)
|
||||
},
|
||||
(cb) => {
|
||||
WebSocketStarRendezvous.start({
|
||||
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
|
||||
refreshPeerListIntervalMS: 1000,
|
||||
strictMultiaddr: false,
|
||||
cryptoChallenge: true
|
||||
}, (err, _server) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
wsRendezvous = _server
|
||||
cb()
|
||||
})
|
||||
},
|
||||
(cb) => {
|
||||
getPeerRelay((err, peerInfo) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
node = new Node({
|
||||
peerInfo,
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
node.start(cb)
|
||||
})
|
||||
libp2p = new Libp2p({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WebSockets],
|
||||
streamMuxer: [Muxer],
|
||||
connEncryption: [Crypto]
|
||||
}
|
||||
], done)
|
||||
})
|
||||
// Add the echo protocol
|
||||
libp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
|
||||
|
||||
await libp2p.start()
|
||||
}
|
||||
|
||||
const after = (done) => {
|
||||
setTimeout(() =>
|
||||
parallel([
|
||||
(cb) => wrtcRendezvous.stop().then(cb).catch(cb),
|
||||
...[node, wsRendezvous].map((s) => (cb) => s.stop(cb)),
|
||||
], done),
|
||||
2000
|
||||
)
|
||||
const after = async () => {
|
||||
await libp2p.stop()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,6 +9,7 @@ logs
|
||||
*.log
|
||||
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
|
@ -20,7 +20,7 @@ jobs:
|
||||
include:
|
||||
- stage: check
|
||||
script:
|
||||
- npx aegir build --bundlesize
|
||||
# - npx aegir build --bundlesize
|
||||
- npx aegir dep-check -- -i wrtc -i electron-webrtc
|
||||
- npm run lint
|
||||
|
||||
@ -29,16 +29,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
|
104
CHANGELOG.md
104
CHANGELOG.md
@ -1,3 +1,107 @@
|
||||
<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)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* pubsub promisify ([#456](https://github.com/libp2p/js-libp2p/issues/456)) ([ae6af20](https://github.com/libp2p/js-libp2p/commit/ae6af20))
|
||||
|
||||
|
||||
|
||||
<a name="0.26.1"></a>
|
||||
## [0.26.1](https://github.com/libp2p/js-libp2p/compare/v0.26.0...v0.26.1) (2019-08-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* avoid using superstruct interface ([aa95ab9](https://github.com/libp2p/js-libp2p/commit/aa95ab9))
|
||||
* improve config defaults ([#409](https://github.com/libp2p/js-libp2p/issues/409)) ([3eef695](https://github.com/libp2p/js-libp2p/commit/3eef695)), closes [#406](https://github.com/libp2p/js-libp2p/issues/406)
|
||||
* pubsub configuration ([#404](https://github.com/libp2p/js-libp2p/issues/404)) ([b0f124b](https://github.com/libp2p/js-libp2p/commit/b0f124b)), closes [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401)
|
||||
* reference files directly to avoid npm install failures ([#408](https://github.com/libp2p/js-libp2p/issues/408)) ([b3deb35](https://github.com/libp2p/js-libp2p/commit/b3deb35))
|
||||
* reject rather than throw in get peer info ([#410](https://github.com/libp2p/js-libp2p/issues/410)) ([60b0cbc](https://github.com/libp2p/js-libp2p/commit/60b0cbc)), closes [#400](https://github.com/libp2p/js-libp2p/issues/400)
|
||||
|
||||
|
||||
|
||||
<a name="0.26.0"></a>
|
||||
# [0.26.0](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.3...v0.26.0) (2019-08-07)
|
||||
|
||||
|
||||
|
||||
<a name="0.26.0-rc.3"></a>
|
||||
# [0.26.0-rc.3](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.2...v0.26.0-rc.3) (2019-08-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* promisified methods ([#398](https://github.com/libp2p/js-libp2p/issues/398)) ([ff7a6c8](https://github.com/libp2p/js-libp2p/commit/ff7a6c8))
|
||||
|
||||
|
||||
|
||||
<a name="0.26.0-rc.2"></a>
|
||||
# [0.26.0-rc.2](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.1...v0.26.0-rc.2) (2019-08-01)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* dont override methods of created instance ([#394](https://github.com/libp2p/js-libp2p/issues/394)) ([3e95e6f](https://github.com/libp2p/js-libp2p/commit/3e95e6f))
|
||||
* pubsub default config ([#393](https://github.com/libp2p/js-libp2p/issues/393)) ([f4f3f0f](https://github.com/libp2p/js-libp2p/commit/f4f3f0f))
|
||||
|
||||
|
||||
### Chores
|
||||
|
||||
* update switch ([#395](https://github.com/libp2p/js-libp2p/issues/395)) ([684f283](https://github.com/libp2p/js-libp2p/commit/684f283))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* switch configuration has changed.
|
||||
'blacklistTTL' is now 'denyTTL' and 'blackListAttempts' is now 'denyAttempts'
|
||||
|
||||
|
||||
|
||||
<a name="0.26.0-rc.1"></a>
|
||||
# [0.26.0-rc.1](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.0...v0.26.0-rc.1) (2019-07-31)
|
||||
|
||||
|
||||
|
||||
<a name="0.26.0-rc.0"></a>
|
||||
# [0.26.0-rc.0](https://github.com/libp2p/js-libp2p/compare/v0.25.5...v0.26.0-rc.0) (2019-07-31)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* make subscribe comply with ipfs interface ([#389](https://github.com/libp2p/js-libp2p/issues/389)) ([9554b05](https://github.com/libp2p/js-libp2p/commit/9554b05))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* integrate gossipsub by default ([#365](https://github.com/libp2p/js-libp2p/issues/365)) ([791f39a](https://github.com/libp2p/js-libp2p/commit/791f39a))
|
||||
* promisify all api methods that accept callbacks ([#381](https://github.com/libp2p/js-libp2p/issues/381)) ([df6ef45](https://github.com/libp2p/js-libp2p/commit/df6ef45))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* new configuration for deciding the implementation of pubsub to be used.
|
||||
In this context, the experimental flags were also removed. See the README for the latest usage.
|
||||
* The ipfs interface specified that options
|
||||
should be provided after the handler, not before.
|
||||
https://github.com/ipfs/interface-js-ipfs-core/blob/v0.109.0/SPEC/PUBSUB.md#pubsubsubscribe
|
||||
|
||||
This corrects the order of parameters. See the jsdocs examples
|
||||
for subscribe to see how it should be used.
|
||||
|
||||
|
||||
|
||||
<a name="0.25.5"></a>
|
||||
## [0.25.5](https://github.com/libp2p/js-libp2p/compare/v0.25.4...v0.25.5) (2019-07-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* peer routing for delegate router ([#377](https://github.com/libp2p/js-libp2p/issues/377)) ([905c911](https://github.com/libp2p/js-libp2p/commit/905c911)), closes [/github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L15-L24](https://github.com//github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go/issues/L15-L24)
|
||||
|
||||
|
||||
|
||||
<a name="0.25.4"></a>
|
||||
## [0.25.4](https://github.com/libp2p/js-libp2p/compare/v0.25.3...v0.25.4) (2019-06-07)
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
# Contributing guidelines
|
||||
|
||||
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/contribution-guidelines.md).
|
||||
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md).
|
||||
|
||||
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/js-code-guidelines.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
|
||||
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
|
||||
|
||||
We appreciate your time and attention for going over these. Please open an issue on [ipfs/community](https://github.com/ipfs/community) if you have any question.
|
||||
|
||||
|
66
README.md
66
README.md
@ -8,8 +8,9 @@
|
||||
<a href="http://ipn.io"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
|
||||
<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
|
||||
<a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
|
||||
<a href="https://riot.permaweb.io/#/room/#libp2p:permaweb.io"><img src="https://img.shields.io/badge/matrix-%23libp2p%3Apermaweb.io-blue.svg?style=flat-square" /> </a>
|
||||
<a href="https://discord.gg/66KBrm2"><img src="https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square" /></a>
|
||||
<a href="https://discuss.libp2p.io"><img src="https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg" /></a>
|
||||
<a href="https://waffle.io/libp2p/libp2p"><img src="https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square" /></a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@ -29,9 +30,7 @@
|
||||
|
||||
We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..
|
||||
|
||||
**Want to get started?** Check our [examples folder](/examples). You can check the development status at the [Waffle Board](https://waffle.io/libp2p/js-libp2p).
|
||||
|
||||
[](https://waffle.io/libp2p/js-libp2p/metrics/throughput)
|
||||
**Want to get started?** Check our [examples folder](/examples).
|
||||
|
||||
[**`Weekly Core Dev Calls`**](https://github.com/ipfs/pm/issues/650)
|
||||
|
||||
@ -94,6 +93,8 @@ npm install --save 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).
|
||||
|
||||
### [Tutorials and Examples](/examples)
|
||||
|
||||
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios.
|
||||
@ -119,6 +120,7 @@ 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-pnet')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
@ -154,7 +156,8 @@ class Node extends Libp2p {
|
||||
peerDiscovery: [
|
||||
MulticastDNS
|
||||
],
|
||||
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components
|
||||
dht: DHT, // DHT enables PeerRouting, ContentRouting and DHT itself components
|
||||
pubsub: GossipSub
|
||||
},
|
||||
|
||||
// libp2p config options (typically found on a config.json)
|
||||
@ -187,9 +190,11 @@ class Node extends Libp2p {
|
||||
timeout: 10e3
|
||||
}
|
||||
},
|
||||
// Enable/Disable Experimental features
|
||||
EXPERIMENTAL: { // Experimental features ("behind a flag")
|
||||
pubsub: false
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -204,22 +209,20 @@ class Node extends Libp2p {
|
||||
|
||||
### API
|
||||
|
||||
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
|
||||
**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.
|
||||
|
||||
#### Create a Node - `Libp2p.create(options)`
|
||||
|
||||
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
|
||||
|
||||
```js
|
||||
const { createLibp2p } = require('libp2p')
|
||||
createLibp2p(options, (err, libp2p) => {
|
||||
if (err) throw err
|
||||
libp2p.start((err) => {
|
||||
if (err) throw err
|
||||
})
|
||||
})
|
||||
const { create } = require('libp2p')
|
||||
const libp2p = await create(options)
|
||||
|
||||
await libp2p.start()
|
||||
```
|
||||
|
||||
- `options`: Object of libp2p configuration options
|
||||
- `callback`: Function with signature `function (Error, Libp2p) {}`
|
||||
|
||||
#### Create a Node alternative - `new Libp2p(options)`
|
||||
|
||||
@ -329,7 +332,7 @@ Required keys in the `options` object:
|
||||
> 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.
|
||||
unless they are performing a specific action. See [peer discovery and auto dial](./doc/PEER_DISCOVERY.md) for more information.
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
@ -563,14 +566,11 @@ List of packages currently in existence for libp2p
|
||||
| **Transport** |
|
||||
| [`interface-transport`](//github.com/libp2p/interface-transport) | [](//github.com/libp2p/interface-transport/releases) | [](https://david-dm.org/libp2p/interface-transport) | [](https://travis-ci.com/libp2p/interface-transport) | [](https://codecov.io/gh/libp2p/interface-transport) | [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-udp`](//github.com/libp2p/js-libp2p-udp) | [](//github.com/libp2p/js-libp2p-udp/releases) | [](https://david-dm.org/libp2p/js-libp2p-udp) | [](https://travis-ci.com/libp2p/js-libp2p-udp) | [](https://codecov.io/gh/libp2p/js-libp2p-udp) | N/A |
|
||||
| [`libp2p-udt`](//github.com/libp2p/js-libp2p-udt) | [](//github.com/libp2p/js-libp2p-udt/releases) | [](https://david-dm.org/libp2p/js-libp2p-udt) | [](https://travis-ci.com/libp2p/js-libp2p-udt) | [](https://codecov.io/gh/libp2p/js-libp2p-udt) | 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) | [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) |
|
||||
| [`libp2p-websocket-star-rendezvous`](//github.com/libp2p/js-libp2p-websocket-star-rendezvous) | [](//github.com/libp2p/js-libp2p-websocket-star-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star-rendezvous) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star-rendezvous) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Crypto 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) |
|
||||
| **Stream Muxers** |
|
||||
@ -585,13 +585,6 @@ List of packages currently in existence for libp2p
|
||||
| [`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) |
|
||||
| **NAT Traversal** |
|
||||
| [`libp2p-circuit`](//github.com/libp2p/js-libp2p-circuit) | [](//github.com/libp2p/js-libp2p-circuit/releases) | [](https://david-dm.org/libp2p/js-libp2p-circuit) | [](https://travis-ci.com/libp2p/js-libp2p-circuit) | [](https://codecov.io/gh/libp2p/js-libp2p-circuit) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-nat-mngr`](//github.com/libp2p/js-libp2p-nat-mngr) | [](//github.com/libp2p/js-libp2p-nat-mngr/releases) | [](https://david-dm.org/libp2p/js-libp2p-nat-mngr) | [](https://travis-ci.com/libp2p/js-libp2p-nat-mngr) | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mngr) | N/A |
|
||||
| **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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| **Content Routing** |
|
||||
| [`interface-content-routing`](//github.com/libp2p/interface-content-routing) | [](//github.com/libp2p/interface-content-routing/releases) | [](https://david-dm.org/libp2p/interface-content-routing) | [](https://travis-ci.com/libp2p/interface-content-routing) | [](https://codecov.io/gh/libp2p/interface-content-routing) | N/A |
|
||||
| [`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) |
|
||||
@ -600,22 +593,15 @@ List of packages currently in existence for libp2p
|
||||
| [`interface-peer-routing`](//github.com/libp2p/interface-peer-routing) | [](//github.com/libp2p/interface-peer-routing/releases) | [](https://david-dm.org/libp2p/interface-peer-routing) | [](https://travis-ci.com/libp2p/interface-peer-routing) | [](https://codecov.io/gh/libp2p/interface-peer-routing) | N/A |
|
||||
| [`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) |
|
||||
| **Record Store** |
|
||||
| [`interface-record-store`](//github.com/libp2p/interface-record-store) | [](//github.com/libp2p/interface-record-store/releases) | [](https://david-dm.org/libp2p/interface-record-store) | [](https://travis-ci.com/libp2p/interface-record-store) | [](https://codecov.io/gh/libp2p/interface-record-store) | N/A |
|
||||
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [](//github.com/libp2p/js-libp2p-record/releases) | [](https://david-dm.org/libp2p/js-libp2p-record) | [](https://travis-ci.com/libp2p/js-libp2p-record) | [](https://codecov.io/gh/libp2p/js-libp2p-record) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **Generics** |
|
||||
| [`libp2p-connection-manager`](//github.com/libp2p/js-libp2p-connection-manager) | [](//github.com/libp2p/js-libp2p-connection-manager/releases) | [](https://david-dm.org/libp2p/js-libp2p-connection-manager) | [](https://travis-ci.com/libp2p/js-libp2p-connection-manager) | [](https://codecov.io/gh/libp2p/js-libp2p-connection-manager) | N/A |
|
||||
| **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) | [Friedel Ziegelmayer](mailto:dignifiedquire@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-switch`](//github.com/libp2p/js-libp2p-switch) | [](//github.com/libp2p/js-libp2p-switch/releases) | [](https://david-dm.org/libp2p/js-libp2p-switch) | [](https://travis-ci.com/libp2p/js-libp2p-switch) | [](https://codecov.io/gh/libp2p/js-libp2p-switch) | [Jacob Heun](mailto:jacobheun@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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`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) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| **Extensions** |
|
||||
| [`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-identify`](//github.com/libp2p/js-libp2p-identify) | [](//github.com/libp2p/js-libp2p-identify/releases) | [](https://david-dm.org/libp2p/js-libp2p-identify) | [](https://travis-ci.com/libp2p/js-libp2p-identify) | [](https://codecov.io/gh/libp2p/js-libp2p-identify) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-keychain`](//github.com/libp2p/js-libp2p-keychain) | [](//github.com/libp2p/js-libp2p-keychain/releases) | [](https://david-dm.org/libp2p/js-libp2p-keychain) | [](https://travis-ci.com/libp2p/js-libp2p-keychain) | [](https://codecov.io/gh/libp2p/js-libp2p-keychain) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [](//github.com/libp2p/js-libp2p-ping/releases) | [](https://david-dm.org/libp2p/js-libp2p-ping) | [](https://travis-ci.com/libp2p/js-libp2p-ping) | [](https://codecov.io/gh/libp2p/js-libp2p-ping) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-pnet`](//github.com/libp2p/js-libp2p-pnet) | [](//github.com/libp2p/js-libp2p-pnet/releases) | [](https://david-dm.org/libp2p/js-libp2p-pnet) | [](https://travis-ci.com/libp2p/js-libp2p-pnet) | [](https://codecov.io/gh/libp2p/js-libp2p-pnet) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Utilities** |
|
||||
| [`p2pcat`](//github.com/libp2p/js-p2pcat) | [](//github.com/libp2p/js-p2pcat/releases) | [](https://david-dm.org/libp2p/js-p2pcat) | [](https://travis-ci.com/libp2p/js-p2pcat) | [](https://codecov.io/gh/libp2p/js-p2pcat) | N/A |
|
||||
|
||||
## Contribute
|
||||
|
||||
|
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
|
@ -17,7 +17,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",
|
||||
|
@ -8,8 +8,8 @@ const SPDY = require('libp2p-spdy')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../../../')
|
||||
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 = [
|
||||
@ -26,9 +26,9 @@ const bootstrapList = [
|
||||
]
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
|
||||
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
|
||||
constructor ({ peerInfo }) {
|
||||
const wrtcStar = new WebRTCStar({ id: peerInfo.id })
|
||||
const wsstar = new WebSocketStar({ id: peerInfo.id })
|
||||
|
||||
const defaults = {
|
||||
modules: {
|
||||
@ -49,7 +49,8 @@ class Node extends libp2p {
|
||||
wsstar.discovery,
|
||||
Bootstrap
|
||||
],
|
||||
dht: DHT
|
||||
dht: DHT,
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
@ -76,8 +77,8 @@ class Node extends libp2p {
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
pubsub: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
connectionManager: {
|
||||
@ -86,7 +87,7 @@ class Node extends libp2p {
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
super({ ...defaults, peerInfo })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,11 @@ function createNode (callback) {
|
||||
}
|
||||
|
||||
const peerIdStr = peerInfo.id.toB58String()
|
||||
const ma = `/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star/p2p/${peerIdStr}`
|
||||
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(ma)
|
||||
peerInfo.multiaddrs.add(webrtcAddr)
|
||||
peerInfo.multiaddrs.add(wsAddr)
|
||||
|
||||
const node = new Node({
|
||||
peerInfo
|
||||
|
@ -46,6 +46,9 @@ domReady(() => {
|
||||
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) => {})
|
||||
|
@ -20,5 +20,3 @@ Then simply go into the folder [1](./1) and execute the following
|
||||
> npm start
|
||||
# open your browser in port :9090
|
||||
```
|
||||
|
||||
[Version Published on IPFS](http://ipfs.io/ipfs/Qmbc1J7ehw1dNYachbkCWPto4RsnVvqCKNVzmYEod2gXcy)
|
||||
|
1
examples/pnet-ipfs/.gitignore
vendored
Normal file
1
examples/pnet-ipfs/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
tmp/
|
29
examples/pnet-ipfs/README.md
Normal file
29
examples/pnet-ipfs/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Private Networking with IPFS
|
||||
This example shows how to set up a private network of IPFS nodes.
|
||||
|
||||
## Setup
|
||||
Install dependencies:
|
||||
|
||||
```
|
||||
npm install
|
||||
```
|
||||
|
||||
## Run
|
||||
Running the example will cause two nodes with the same swarm key to be started and exchange basic information.
|
||||
|
||||
```
|
||||
node index.js
|
||||
```
|
||||
|
||||
### Using different keys
|
||||
This example includes `TASK` comments that can be used to try the example with different swarm keys. This will
|
||||
allow you to see how nodes will fail to connect if they are on different private networks and try to connect to
|
||||
one another.
|
||||
|
||||
To change the swarm key of one of the nodes, look through `index.js` for comments starting with `TASK` to indicate
|
||||
where lines are that pertain to changing the swarm key of node 2.
|
||||
|
||||
### Exploring the repos
|
||||
Once you've run the example you can take a look at the repos in the `./tmp` directory to see how they differ, including
|
||||
the swarm keys. You should see a `swarm.key` file in each of the repos and when the nodes are on the same private network
|
||||
this contents of the `swarm.key` files should be the same.
|
145
examples/pnet-ipfs/index.js
Normal file
145
examples/pnet-ipfs/index.js
Normal file
@ -0,0 +1,145 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
'use strict'
|
||||
|
||||
const IPFS = require('ipfs')
|
||||
const assert = require('assert').strict
|
||||
const writeKey = require('libp2p-pnet').generate
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const privateLibp2pBundle = require('./libp2p-bundle')
|
||||
const { mkdirp } = require('./utils')
|
||||
|
||||
// Create two separate repo paths so we can run two nodes and check their output
|
||||
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
|
||||
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
|
||||
mkdirp(repo1)
|
||||
mkdirp(repo2)
|
||||
|
||||
// Create a buffer and write the swarm key to it
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
writeKey(swarmKey)
|
||||
|
||||
// This key is for the `TASK` mentioned in the writeFileSync calls below
|
||||
const otherSwarmKey = Buffer.alloc(95)
|
||||
writeKey(otherSwarmKey)
|
||||
|
||||
// Add the swarm key to both repos
|
||||
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
|
||||
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
|
||||
fs.writeFileSync(swarmKey1Path, swarmKey)
|
||||
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
||||
fs.writeFileSync(swarmKey2Path, swarmKey)
|
||||
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)
|
||||
|
||||
// Create the first ipfs node
|
||||
const node1 = new IPFS({
|
||||
repo: repo1,
|
||||
libp2p: privateLibp2pBundle(swarmKey1Path),
|
||||
config: {
|
||||
Addresses: {
|
||||
// Set the swarm address so we dont get port collision on the nodes
|
||||
Swarm: ['/ip4/0.0.0.0/tcp/9101']
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Create the second ipfs node
|
||||
const node2 = new IPFS({
|
||||
repo: repo2,
|
||||
libp2p: privateLibp2pBundle(swarmKey2Path),
|
||||
config: {
|
||||
Addresses: {
|
||||
// Set the swarm address so we dont get port collision on the nodes
|
||||
Swarm: ['/ip4/0.0.0.0/tcp/9102']
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
console.log('auto starting the nodes...')
|
||||
|
||||
// `nodesStarted` keeps track of how many of our nodes have started
|
||||
let nodesStarted = 0
|
||||
/**
|
||||
* Calls `connectAndTalk` when both nodes have started
|
||||
* @returns {void}
|
||||
*/
|
||||
const didStartHandler = () => {
|
||||
if (++nodesStarted === 2) {
|
||||
// If both nodes are up, start talking
|
||||
connectAndTalk()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exits the process when all started nodes have stopped
|
||||
* @returns {void}
|
||||
*/
|
||||
const didStopHandler = () => {
|
||||
if (--nodesStarted < 1) {
|
||||
console.log('all nodes stopped, exiting.')
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the running nodes
|
||||
* @param {Error} err An optional error to log to the console
|
||||
* @returns {void}
|
||||
*/
|
||||
const doStop = (err) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
console.log('Shutting down...')
|
||||
node1.stop()
|
||||
node2.stop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the IPFS nodes and transfers data between them
|
||||
* @returns {void}
|
||||
*/
|
||||
const connectAndTalk = async () => {
|
||||
console.log('connecting the nodes...')
|
||||
const node2Id = await node2.id()
|
||||
const dataToAdd = Buffer.from('Hello, private friend!')
|
||||
|
||||
// Connect the nodes
|
||||
// This will error when different private keys are used
|
||||
try {
|
||||
await node1.swarm.connect(node2Id.addresses[0])
|
||||
} catch (err) {
|
||||
return doStop(err)
|
||||
}
|
||||
console.log('the nodes are connected, let\'s add some data')
|
||||
|
||||
// Add some data to node 1
|
||||
let addedCID
|
||||
try {
|
||||
addedCID = await node1.files.add(dataToAdd)
|
||||
} catch (err) {
|
||||
return doStop(err)
|
||||
}
|
||||
console.log(`added ${addedCID[0].path} to the node1`)
|
||||
|
||||
// Retrieve the data from node 2
|
||||
let cattedData
|
||||
try {
|
||||
cattedData = await node2.files.cat(addedCID[0].path)
|
||||
} catch (err) {
|
||||
return doStop(err)
|
||||
}
|
||||
assert.deepEqual(cattedData.toString(), dataToAdd.toString(), 'Should have equal data')
|
||||
console.log(`successfully retrieved "${dataToAdd.toString()}" from node2`)
|
||||
|
||||
doStop()
|
||||
}
|
||||
|
||||
// Wait for the nodes to boot
|
||||
node1.once('start', didStartHandler)
|
||||
node2.once('start', didStartHandler)
|
||||
|
||||
// Listen for the nodes stopping so we can cleanup
|
||||
node1.once('stop', didStopHandler)
|
||||
node2.once('stop', didStopHandler)
|
60
examples/pnet-ipfs/libp2p-bundle.js
Normal file
60
examples/pnet-ipfs/libp2p-bundle.js
Normal file
@ -0,0 +1,60 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const fs = require('fs')
|
||||
const Protector = require('libp2p-pnet')
|
||||
|
||||
/**
|
||||
* Options for the libp2p bundle
|
||||
* @typedef {Object} libp2pBundle~options
|
||||
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
|
||||
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
|
||||
* @property {Object} config - The config of the IPFS node
|
||||
* @property {Object} options - The options given to the IPFS node
|
||||
*/
|
||||
|
||||
/**
|
||||
* privateLibp2pBundle returns a libp2p bundle function that will use the swarm
|
||||
* key at the given `swarmKeyPath` to create the Protector
|
||||
*
|
||||
* @param {string} swarmKeyPath The path to our swarm key
|
||||
* @returns {libp2pBundle} Returns a libp2pBundle function for use in IPFS creation
|
||||
*/
|
||||
const privateLibp2pBundle = (swarmKeyPath) => {
|
||||
/**
|
||||
* This is the bundle we will use to create our fully customized libp2p bundle.
|
||||
*
|
||||
* @param {libp2pBundle~options} opts The options to use when generating the libp2p node
|
||||
* @returns {Libp2p} Our new libp2p node
|
||||
*/
|
||||
const libp2pBundle = (opts) => {
|
||||
// Set convenience variables to clearly showcase some of the useful things that are available
|
||||
const peerInfo = opts.peerInfo
|
||||
const peerBook = opts.peerBook
|
||||
|
||||
// Build and return our libp2p node
|
||||
return new Libp2p({
|
||||
peerInfo,
|
||||
peerBook,
|
||||
modules: {
|
||||
transport: [TCP], // We're only using the TCP transport for this example
|
||||
streamMuxer: [MPLEX], // We're only using mplex muxing
|
||||
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
|
||||
// care about node identity, and only the presence of private keys
|
||||
connEncryption: [SECIO],
|
||||
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
|
||||
// being left in for explicit readability.
|
||||
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
|
||||
peerDiscovery: [],
|
||||
connProtector: new Protector(fs.readFileSync(swarmKeyPath))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return libp2pBundle
|
||||
}
|
||||
|
||||
module.exports = privateLibp2pBundle
|
21
examples/pnet-ipfs/package.json
Normal file
21
examples/pnet-ipfs/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "pnet-ipfs-example",
|
||||
"version": "1.0.0",
|
||||
"description": "An example of private networking with IPFS",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ipfs": "~0.32.3",
|
||||
"libp2p": "~0.23.1",
|
||||
"libp2p-mplex": "~0.8.2",
|
||||
"libp2p-pnet": "../../",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-tcp": "~0.13.0"
|
||||
}
|
||||
}
|
28
examples/pnet-ipfs/utils.js
Normal file
28
examples/pnet-ipfs/utils.js
Normal file
@ -0,0 +1,28 @@
|
||||
'use strict'
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
/**
|
||||
* mkdirp recursively creates needed folders for the given dir path
|
||||
* @param {string} dir
|
||||
* @returns {string} The path that was created
|
||||
*/
|
||||
module.exports.mkdirp = (dir) => {
|
||||
return path
|
||||
.resolve(dir)
|
||||
.split(path.sep)
|
||||
.reduce((acc, cur) => {
|
||||
const currentPath = path.normalize(acc + path.sep + cur)
|
||||
|
||||
try {
|
||||
fs.statSync(currentPath)
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
fs.mkdirSync(currentPath)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return currentPath
|
||||
}, '')
|
||||
}
|
@ -7,6 +7,7 @@ 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')
|
||||
@ -19,7 +20,8 @@ class MyBundle extends libp2p {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
peerDiscovery: [ MulticastDNS ],
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
@ -28,8 +30,9 @@ class MyBundle extends libp2p {
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,19 +68,36 @@ parallel([
|
||||
node1.once('peer:connect', (peer) => {
|
||||
console.log('connected to %s', peer.id.toB58String())
|
||||
|
||||
// Subscribe to the topic 'news'
|
||||
node1.pubsub.subscribe('news',
|
||||
(msg) => console.log(msg.from, msg.data.toString()),
|
||||
() => {
|
||||
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(() => {
|
||||
// Publish the message on topic 'news'
|
||||
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 }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Publish Subscribe
|
||||
|
||||
Publish Subscribe is also included on the stack. Currently, we have on PubSub implementation which we ship by default [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
|
||||
Publish Subscribe is also included on the stack. Currently, we have two PubSub implementation available [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub) and [libp2p-gossipsub](https://github.com/ChainSafe/gossipsub-js), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
|
||||
|
||||
We've seen many interesting use cases appear with this, here are some highlights:
|
||||
|
||||
@ -12,26 +12,43 @@ We've seen many interesting use cases appear with this, here are some highlights
|
||||
|
||||
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).
|
||||
|
||||
Using PubSub is super simple, all you have to do is start a libp2p node with `EXPERIMENTAL.pubsub` set to true.
|
||||
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())
|
||||
|
||||
// Subscribe to the topic 'news'
|
||||
node1.pubsub.subscribe('news',
|
||||
(msg) => console.log(msg.from, msg.data.toString()),
|
||||
() => {
|
||||
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(() => {
|
||||
// Publish the message on topic 'news'
|
||||
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 }
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
@ -40,11 +57,29 @@ The output of the program should look like:
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
|
||||
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: false
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Future work
|
||||
|
@ -3,7 +3,7 @@
|
||||
"Package",
|
||||
"Version",
|
||||
"Deps",
|
||||
"CI/Travis",
|
||||
"CI",
|
||||
"Coverage",
|
||||
"Lead Maintainer"
|
||||
],
|
||||
@ -18,14 +18,11 @@
|
||||
"Transport",
|
||||
["libp2p/interface-transport", "interface-transport"],
|
||||
["libp2p/js-libp2p-tcp", "libp2p-tcp"],
|
||||
["libp2p/js-libp2p-udp", "libp2p-udp"],
|
||||
["libp2p/js-libp2p-udt", "libp2p-udt"],
|
||||
["libp2p/js-libp2p-utp", "libp2p-utp"],
|
||||
["libp2p/js-libp2p-webrtc-direct", "libp2p-webrtc-direct"],
|
||||
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
|
||||
["libp2p/js-libp2p-websockets", "libp2p-websockets"],
|
||||
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
|
||||
["libp2p/js-libp2p-websocket-star-rendezvous", "libp2p-websocket-star-rendezvous"],
|
||||
|
||||
"Crypto Channels",
|
||||
["libp2p/js-libp2p-secio", "libp2p-secio"],
|
||||
@ -44,15 +41,6 @@
|
||||
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
|
||||
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
|
||||
|
||||
"NAT Traversal",
|
||||
["libp2p/js-libp2p-circuit", "libp2p-circuit"],
|
||||
["libp2p/js-libp2p-nat-mngr", "libp2p-nat-mngr"],
|
||||
|
||||
"Data Types",
|
||||
["libp2p/js-peer-book", "peer-book"],
|
||||
["libp2p/js-peer-id", "peer-id"],
|
||||
["libp2p/js-peer-info", "peer-info"],
|
||||
|
||||
"Content Routing",
|
||||
["libp2p/interface-content-routing", "interface-content-routing"],
|
||||
["libp2p/js-libp2p-delegated-content-routing", "libp2p-delegated-content-routing"],
|
||||
@ -63,24 +51,16 @@
|
||||
["libp2p/js-libp2p-delegated-peer-routing", "libp2p-delegated-peer-routing"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
|
||||
"Record Store",
|
||||
["libp2p/interface-record-store", "interface-record-store"],
|
||||
["libp2p/js-libp2p-record", "libp2p-record"],
|
||||
|
||||
"Generics",
|
||||
["libp2p/js-libp2p-connection-manager", "libp2p-connection-manager"],
|
||||
"Utilities",
|
||||
["libp2p/js-libp2p-crypto", "libp2p-crypto"],
|
||||
["libp2p/js-libp2p-crypto-secp256k1", "libp2p-crypto-secp256k1"],
|
||||
["libp2p/js-libp2p-switch", "libp2p-switch"],
|
||||
|
||||
"Data Types",
|
||||
["libp2p/js-peer-book", "peer-book"],
|
||||
["libp2p/js-peer-id", "peer-id"],
|
||||
["libp2p/js-peer-info", "peer-info"],
|
||||
|
||||
"Extensions",
|
||||
["libp2p/js-libp2p-floodsub", "libp2p-floodsub"],
|
||||
["libp2p/js-libp2p-identify", "libp2p-identify"],
|
||||
["libp2p/js-libp2p-keychain", "libp2p-keychain"],
|
||||
["libp2p/js-libp2p-ping", "libp2p-ping"],
|
||||
["libp2p/js-libp2p-pnet", "libp2p-pnet"],
|
||||
|
||||
"Utilities",
|
||||
["libp2p/js-p2pcat", "p2pcat"]
|
||||
["libp2p/js-libp2p-floodsub", "libp2p-floodsub"]
|
||||
]
|
||||
}
|
||||
|
81
package.json
81
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.25.4",
|
||||
"version": "0.26.2",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
@ -16,7 +16,8 @@
|
||||
"test:browser": "aegir test -t browser",
|
||||
"release": "aegir release -t node -t browser",
|
||||
"release-minor": "aegir release --type minor -t node -t browser",
|
||||
"release-major": "aegir release --type major -t node -t browser"
|
||||
"release-major": "aegir release --type major -t node -t browser",
|
||||
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -35,66 +36,91 @@
|
||||
},
|
||||
"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": {
|
||||
"abort-controller": "^3.0.0",
|
||||
"async": "^2.6.2",
|
||||
"bignumber.js": "^9.0.0",
|
||||
"class-is": "^1.1.0",
|
||||
"debug": "^4.1.1",
|
||||
"err-code": "^1.1.2",
|
||||
"fsm-event": "^2.1.0",
|
||||
"libp2p-connection-manager": "^0.1.0",
|
||||
"libp2p-floodsub": "^0.16.1",
|
||||
"libp2p-ping": "^0.8.5",
|
||||
"libp2p-switch": "^0.42.12",
|
||||
"libp2p-websockets": "^0.12.2",
|
||||
"mafmt": "^6.0.7",
|
||||
"multiaddr": "^6.1.0",
|
||||
"hashlru": "^2.3.0",
|
||||
"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.17.1",
|
||||
"libp2p-interfaces": "^0.1.5",
|
||||
"mafmt": "^7.0.0",
|
||||
"merge-options": "^1.0.1",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^7.1.0",
|
||||
"multistream-select": "^0.15.0",
|
||||
"once": "^1.4.0",
|
||||
"peer-book": "^0.9.1",
|
||||
"peer-id": "^0.12.2",
|
||||
"peer-info": "^0.15.1",
|
||||
"superstruct": "^0.6.0"
|
||||
"p-map": "^3.0.0",
|
||||
"p-queue": "^6.1.1",
|
||||
"p-settle": "^3.1.0",
|
||||
"peer-id": "^0.13.3",
|
||||
"peer-info": "^0.17.0",
|
||||
"promisify-es6": "^1.0.3",
|
||||
"protons": "^1.0.1",
|
||||
"pull-cat": "^1.1.11",
|
||||
"pull-handshake": "^1.1.4",
|
||||
"pull-stream": "^3.6.9",
|
||||
"retimer": "^2.0.0",
|
||||
"xsalsa20": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"aegir": "^19.0.3",
|
||||
"abortable-iterator": "^2.1.0",
|
||||
"aegir": "^20.0.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"cids": "^0.7.1",
|
||||
"delay": "^4.3.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"electron-webrtc": "^0.3.0",
|
||||
"glob": "^7.1.4",
|
||||
"interface-datastore": "^0.6.0",
|
||||
"it-pair": "^1.0.0",
|
||||
"libp2p-bootstrap": "^0.9.7",
|
||||
"libp2p-circuit": "^0.3.7",
|
||||
"libp2p-delegated-content-routing": "^0.2.2",
|
||||
"libp2p-delegated-peer-routing": "^0.2.2",
|
||||
"libp2p-kad-dht": "^0.15.2",
|
||||
"libp2p-floodsub": "^0.19.0",
|
||||
"libp2p-gossipsub": "ChainSafe/gossipsub-js#beta/async",
|
||||
"libp2p-kad-dht": "^0.15.3",
|
||||
"libp2p-mdns": "^0.12.3",
|
||||
"libp2p-mplex": "^0.8.4",
|
||||
"libp2p-mplex": "^0.9.1",
|
||||
"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.3.0",
|
||||
"libp2p-tcp": "^0.14.1",
|
||||
"libp2p-websockets": "^0.13.1",
|
||||
"lodash.times": "^4.3.2",
|
||||
"nock": "^10.0.6",
|
||||
"p-defer": "^3.0.0",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"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",
|
||||
"pull-stream": "^3.6.12",
|
||||
"sinon": "^7.2.7",
|
||||
"streaming-iterables": "^4.1.0",
|
||||
"wrtc": "^0.4.1"
|
||||
},
|
||||
"contributors": [
|
||||
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
|
||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||
"Alan Shaw <alan@tableflip.io>",
|
||||
"Alex Potsides <alex@achingbrain.net>",
|
||||
"Andrew Nesbitt <andrewnez@gmail.com>",
|
||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||
@ -103,6 +129,7 @@
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
"Fei Liu <liu.feiwood@gmail.com>",
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
@ -129,15 +156,17 @@
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Thomas Eizinger <thomas@eizinger.io>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"Vasco Santos <vasco.santos@ua.pt>",
|
||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||
"Vasco Santos <vasco.santos@ua.pt>",
|
||||
"Volker Mische <volker.mische@gmail.com>",
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"a1300 <a1300@users.noreply.github.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
"isan_rivkin <isanrivkin@gmail.com>",
|
||||
"mayerwin <mayerwin@users.noreply.github.com>",
|
||||
"swedneck <40505480+swedneck@users.noreply.github.com>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
|
||||
]
|
||||
}
|
||||
|
128
src/circuit/IMPLEMENTATION_NOTES.md
Normal file
128
src/circuit/IMPLEMENTATION_NOTES.md
Normal file
@ -0,0 +1,128 @@
|
||||
EDIT: This document is outdated and here only for historical purposes
|
||||
|
||||
NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation
|
||||
|
||||
Example:
|
||||
|
||||
- if there are apples
|
||||
- eat them
|
||||
- if not, check for pears
|
||||
- then eat them
|
||||
- if not, check for cherries
|
||||
- then eat them
|
||||
|
||||
Or,
|
||||
|
||||
- if there are apples
|
||||
- eat them
|
||||
- if not
|
||||
- check for pears
|
||||
- then eat them
|
||||
- if not
|
||||
- check for cherries
|
||||
- then eat them
|
||||
|
||||
In order to minimize nesting, the first example is preferred
|
||||
|
||||
# Relay flow
|
||||
|
||||
## Relay transport (dialer/listener)
|
||||
|
||||
- ### Dial over a relay
|
||||
- See if there is a relay that's already connected to the destination peer, if not
|
||||
- Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful.
|
||||
- If successful
|
||||
- Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address
|
||||
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`.
|
||||
- If no relays could connect, fail the same way a regular transport would
|
||||
- Once the connection has been established, the swarm should treat it as a regular connection,
|
||||
- i.e. muxing, encrypt, etc should all be performed on the relayed connection
|
||||
|
||||
- ### Listen for relayed connections
|
||||
- Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections
|
||||
- A connection arrives
|
||||
- read the address of the source peer from the incoming connection stream
|
||||
- if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list
|
||||
- pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow
|
||||
|
||||
- ### Relay discovery and static relay addresses in swarm config
|
||||
|
||||
- #### Relay address in swarm config
|
||||
- A peer has relay addresses in its swarm config section
|
||||
- On node startup, connect to the relays in swarm config
|
||||
- if successful add address to swarms PeerInfo's multiaddresses
|
||||
- `identify` should take care of announcing that the peer is reachable over the listed relays
|
||||
|
||||
- #### Passive relay discovery
|
||||
- A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.)
|
||||
- *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible*
|
||||
- *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list*
|
||||
- *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.*
|
||||
- *Should it be possible to disable passive relay discovery?*
|
||||
- This could be useful when the peer wants to be reachable **only** over the listed relays
|
||||
- If the incoming peer is a relay, send an `ls` and record its peers
|
||||
|
||||
## Relay Nodes
|
||||
|
||||
- ### Passive relay node
|
||||
- *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.*
|
||||
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
|
||||
- check its swarm's peerbook(?) see if its a known peer, if it is
|
||||
- use the swarms existing connection and
|
||||
- send the multistream header and the source peer address to the dest peer
|
||||
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
|
||||
- circuit the source and dest connections
|
||||
- if couldn't dial, or the connection/stream to the dest peer closed prematurelly
|
||||
- close the src stream
|
||||
|
||||
|
||||
- ### Active relay node
|
||||
- *An active relay node can dial other peers even if its swarm doesnt know about those peers*
|
||||
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
|
||||
- use the swarm to dial to the dest node
|
||||
- send the multistream header and the source peer address to the dest peer
|
||||
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
|
||||
- circuit the source and dest connections
|
||||
- if couldn't dial, or the connection/stream to the dest peer closed prematurely
|
||||
- close the src stream
|
||||
|
||||
- ### `ls` command
|
||||
- *A relay node can allow the peers known to it's swarm to be listed*
|
||||
- *this should be possible to enable/disable from the config*
|
||||
- when a relay gets the `ls` request
|
||||
- if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs
|
||||
- e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]`
|
||||
- if disabled, respond with `na`
|
||||
|
||||
|
||||
## Relay Implementation notes
|
||||
|
||||
- ### Relay transport
|
||||
- Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution.
|
||||
|
||||
- ### `circuit-relay`
|
||||
- This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay.
|
||||
|
||||
## Relay swarm integration
|
||||
|
||||
- The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p
|
||||
- Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods
|
||||
|
||||
- ### Listener
|
||||
- the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto
|
||||
- if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate
|
||||
- when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed
|
||||
|
||||
- ### Dialer
|
||||
- When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on
|
||||
- *The relay will be used in two cases*
|
||||
- If the peer has an explicit relay address that it can be reached on
|
||||
- no other transport is available
|
||||
- The relay will attempt to dial the peer over that relay
|
||||
- If no explicit relay address is provided
|
||||
- no other transport is available
|
||||
- A generic circuit address will be added to the peers multiaddr list
|
||||
- i.e. `/p2p-circuit/ipfs/QmDest`
|
||||
- If another transport is available, then use that instead of the relay
|
||||
|
||||
|
159
src/circuit/README.md
Normal file
159
src/circuit/README.md
Normal file
@ -0,0 +1,159 @@
|
||||
# js-libp2p-circuit
|
||||
|
||||
> 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.
|
||||
|
||||
`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node.
|
||||
|
||||
This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
|
||||
|
||||
### Why?
|
||||
|
||||
`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
|
||||
|
||||
The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
|
||||
- routing traffic between private nets and circumventing NAT layers
|
||||
- route mangling for better privacy (matreshka/shallot dialing).
|
||||
|
||||
It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
|
||||
|
||||
### libp2p-circuit and IPFS
|
||||
|
||||
Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [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)
|
||||
- [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)
|
||||
- [Implementation rational](#implementation-rational)
|
||||
|
||||
## Usage
|
||||
|
||||
### Example
|
||||
|
||||
#### Create dialer/listener
|
||||
|
||||
```js
|
||||
const Circuit = require('libp2p-circuit')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
const mh1 = multiaddr('/p2p-circuit/ipfs/QmHash') // dial /ipfs/QmHash over any circuit
|
||||
|
||||
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
|
||||
|
||||
const listener = circuit.createListener(mh1, (connection) => {
|
||||
console.log('new connection opened')
|
||||
pull(
|
||||
pull.values(['hello']),
|
||||
socket
|
||||
)
|
||||
})
|
||||
|
||||
listener.listen(() => {
|
||||
console.log('listening')
|
||||
|
||||
pull(
|
||||
circuit.dial(mh1),
|
||||
pull.log,
|
||||
pull.onEnd(() => {
|
||||
circuit.close()
|
||||
})
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
Outputs:
|
||||
|
||||
```sh
|
||||
listening
|
||||
new connection opened
|
||||
hello
|
||||
```
|
||||
|
||||
#### Create `relay`
|
||||
|
||||
```js
|
||||
const Relay = require('libp2p-circuit').Relay
|
||||
|
||||
const relay = new Relay(options)
|
||||
|
||||
relay.mount(swarmInstance) // start relaying traffic
|
||||
```
|
||||
|
||||
### This module uses `pull-streams`
|
||||
|
||||
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||
|
||||
You can learn more about pull-streams at:
|
||||
|
||||
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||
|
||||
#### Converting `pull-streams` to Node.js Streams
|
||||
|
||||
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
|
||||
|
||||
```js
|
||||
const pullToStream = require('pull-stream-to-stream')
|
||||
|
||||
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||
// nodeStreamInstance is an instance of a Node.js Stream
|
||||
```
|
||||
|
||||
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
|
||||
|
||||
## API
|
||||
|
||||
[](https://github.com/libp2p/interface-transport)
|
||||
|
||||
`libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
|
||||
|
||||
`/p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
||||
|
||||
Both for dialing and listening.
|
||||
|
||||
### Implementation rational
|
||||
|
||||
This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p-swarm`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p-swarm` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
|
||||
|
||||
```
|
||||
libp2p libp2p-circuit (transport)
|
||||
+-------------------------------------------------+ +--------------------------+
|
||||
| +---------------------------------+ | | |
|
||||
| | | | | +------------------+ |
|
||||
| | | | circuit-relay listens for the HOP | | | |
|
||||
| | libp2p-swarm <------------------------------------------------| circuit-relay | |
|
||||
| | | | message to handle incomming relay | | | |
|
||||
| | | | requests from other nodes | +------------------+ |
|
||||
| +---------------------------------+ | | |
|
||||
| ^ ^ ^ ^ ^ ^ | | +------------------+ |
|
||||
| | | | | | | | | | +-------------+ | |
|
||||
| | | | | | | | dialer uses libp2p-swarm to dial | | | | | |
|
||||
| | | | +----------------------------------------------------------------------> dialer | | |
|
||||
| | | transports | | to a circuit-relay node using the | | | | | |
|
||||
| | | | | | | HOP message | | +-------------+ | |
|
||||
| | | | | | | | | | |
|
||||
| v v | v v | | | | |
|
||||
|+------------------|----------------------------+| | | +-------------+ | |
|
||||
|| | | | | || | | | | | |
|
||||
||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | |
|
||||
|| | +--------------------------------------------------------------------------> | | |
|
||||
|| | | |plugs in just || circuit-relay nodes | | +-------------+ | |
|
||||
|| | | |as any other || | | | |
|
||||
|| | | |transport || | +------------------+ |
|
||||
|+-----------------------------------------------+| | |
|
||||
+-------------------------------------------------+ +--------------------------+
|
||||
```
|
126
src/circuit/circuit.js
Normal file
126
src/circuit/circuit.js
Normal file
@ -0,0 +1,126 @@
|
||||
'use strict'
|
||||
|
||||
const mafmt = require('mafmt')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const CircuitDialer = require('./circuit/dialer')
|
||||
const utilsFactory = require('./circuit/utils')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:transportdialer')
|
||||
log.err = debug('libp2p:circuit:error:transportdialer')
|
||||
|
||||
const createListener = require('./listener')
|
||||
|
||||
class Circuit {
|
||||
static get tag () {
|
||||
return 'Circuit'
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of Dialer.
|
||||
*
|
||||
* @param {Swarm} swarm - the swarm
|
||||
* @param {any} options - config options
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
constructor (swarm, options) {
|
||||
this.options = options || {}
|
||||
|
||||
this.swarm = swarm
|
||||
this.dialer = null
|
||||
this.utils = utilsFactory(swarm)
|
||||
this.peerInfo = this.swarm._peerInfo
|
||||
this.relays = this.filter(this.peerInfo.multiaddrs.toArray())
|
||||
|
||||
// if no explicit relays, add a default relay addr
|
||||
if (this.relays.length === 0) {
|
||||
this.peerInfo
|
||||
.multiaddrs
|
||||
.add(`/p2p-circuit/ipfs/${this.peerInfo.id.toB58String()}`)
|
||||
}
|
||||
|
||||
this.dialer = new CircuitDialer(swarm, options)
|
||||
|
||||
this.swarm.on('peer-mux-established', (peerInfo) => {
|
||||
this.dialer.canHop(peerInfo)
|
||||
})
|
||||
this.swarm.on('peer-mux-closed', (peerInfo) => {
|
||||
this.dialer.relayPeers.delete(peerInfo.id.toB58String())
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial the relays in the Addresses.Swarm config
|
||||
*
|
||||
* @param {Array} relays
|
||||
* @return {void}
|
||||
*/
|
||||
_dialSwarmRelays () {
|
||||
// if we have relay addresses in swarm config, then dial those relays
|
||||
this.relays.forEach((relay) => {
|
||||
const relaySegments = relay
|
||||
.toString()
|
||||
.split('/p2p-circuit')
|
||||
.filter(segment => segment.length)
|
||||
|
||||
relaySegments.forEach((relaySegment) => {
|
||||
const ma = this.utils.peerInfoFromMa(multiaddr(relaySegment))
|
||||
this.dialer._dialRelay(ma)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
*
|
||||
* @param {multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Object} options - dial options
|
||||
* @param {Function} cb - a callback called once dialed
|
||||
* @returns {Connection} - the connection
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
dial (ma, options, cb) {
|
||||
return this.dialer.dial(ma, options, cb)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener
|
||||
*
|
||||
* @param {any} options
|
||||
* @param {Function} handler
|
||||
* @return {listener}
|
||||
*/
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = this.options || {}
|
||||
}
|
||||
|
||||
const listener = createListener(this.swarm, options, handler)
|
||||
listener.on('listen', this._dialSwarmRelays.bind(this))
|
||||
return listener
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter check for all multiaddresses
|
||||
* that this transport can dial on
|
||||
*
|
||||
* @param {any} multiaddrs
|
||||
* @returns {Array<multiaddr>}
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
filter (multiaddrs) {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
return multiaddrs.filter((ma) => {
|
||||
return mafmt.Circuit.matches(ma)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Circuit
|
275
src/circuit/circuit/dialer.js
Normal file
275
src/circuit/circuit/dialer.js
Normal file
@ -0,0 +1,275 @@
|
||||
'use strict'
|
||||
|
||||
const once = require('once')
|
||||
const PeerId = require('peer-id')
|
||||
const waterfall = require('async/waterfall')
|
||||
const setImmediate = require('async/setImmediate')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
|
||||
const utilsFactory = require('./utils')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:dialer')
|
||||
log.err = debug('libp2p:circuit:error:dialer')
|
||||
|
||||
const multicodec = require('../multicodec')
|
||||
const proto = require('../protocol')
|
||||
|
||||
class Dialer {
|
||||
/**
|
||||
* Creates an instance of Dialer.
|
||||
* @param {Swarm} swarm - the swarm
|
||||
* @param {any} options - config options
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
constructor (swarm, options) {
|
||||
this.swarm = swarm
|
||||
this.relayPeers = new Map()
|
||||
this.relayConns = new Map()
|
||||
this.options = options
|
||||
this.utils = utilsFactory(swarm)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper that returns a relay connection
|
||||
*
|
||||
* @param {*} relay
|
||||
* @param {*} callback
|
||||
* @returns {Function} - callback
|
||||
*/
|
||||
_dialRelayHelper (relay, callback) {
|
||||
if (this.relayConns.has(relay.id.toB58String())) {
|
||||
return callback(null, this.relayConns.get(relay.id.toB58String()))
|
||||
}
|
||||
|
||||
return this._dialRelay(relay, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
*
|
||||
* @param {multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Function} cb - a callback called once dialed
|
||||
* @returns {Connection} - the connection
|
||||
*
|
||||
*/
|
||||
dial (ma, cb) {
|
||||
cb = cb || (() => { })
|
||||
const strMa = ma.toString()
|
||||
if (!strMa.includes('/p2p-circuit')) {
|
||||
log.err('invalid circuit address')
|
||||
return cb(new Error('invalid circuit address'))
|
||||
}
|
||||
|
||||
const addr = strMa.split('p2p-circuit') // extract relay address if any
|
||||
const relay = addr[0] === '/' ? null : multiaddr(addr[0])
|
||||
const peer = multiaddr(addr[1] || addr[0])
|
||||
|
||||
const dstConn = new Connection()
|
||||
setImmediate(
|
||||
this._dialPeer.bind(this),
|
||||
peer,
|
||||
relay,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
dstConn.setInnerConn(conn)
|
||||
cb(null, dstConn)
|
||||
})
|
||||
|
||||
return dstConn
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the peer support the HOP protocol
|
||||
*
|
||||
* @param {PeerInfo} peer
|
||||
* @param {Function} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
canHop (peer, callback) {
|
||||
callback = once(callback || (() => { }))
|
||||
|
||||
this._dialRelayHelper(peer, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const sh = new StreamHandler(conn)
|
||||
waterfall([
|
||||
(cb) => sh.write(proto.CircuitRelay.encode({
|
||||
type: proto.CircuitRelay.Type.CAN_HOP
|
||||
}), cb),
|
||||
(cb) => sh.read(cb)
|
||||
], (err, msg) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const response = proto.CircuitRelay.decode(msg)
|
||||
|
||||
if (response.code !== proto.CircuitRelay.Status.SUCCESS) {
|
||||
const err = new Error(`HOP not supported, skipping - ${this.utils.getB58String(peer)}`)
|
||||
log(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
log('HOP supported adding as relay - %s', this.utils.getB58String(peer))
|
||||
this.relayPeers.set(this.utils.getB58String(peer), peer)
|
||||
sh.close()
|
||||
callback()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial the destination peer over a relay
|
||||
*
|
||||
* @param {multiaddr} dstMa
|
||||
* @param {Connection|PeerInfo} relay
|
||||
* @param {Function} cb
|
||||
* @return {Function|void}
|
||||
* @private
|
||||
*/
|
||||
_dialPeer (dstMa, relay, cb) {
|
||||
if (typeof relay === 'function') {
|
||||
cb = relay
|
||||
relay = null
|
||||
}
|
||||
|
||||
if (!cb) {
|
||||
cb = () => {}
|
||||
}
|
||||
|
||||
dstMa = multiaddr(dstMa)
|
||||
// if no relay provided, dial on all available relays until one succeeds
|
||||
if (!relay) {
|
||||
const relays = Array.from(this.relayPeers.values())
|
||||
const next = (nextRelay) => {
|
||||
if (!nextRelay) {
|
||||
const err = 'no relay peers were found or all relays failed to dial'
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return this._negotiateRelay(
|
||||
nextRelay,
|
||||
dstMa,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return next(relays.shift())
|
||||
}
|
||||
cb(null, conn)
|
||||
})
|
||||
}
|
||||
next(relays.shift())
|
||||
} else {
|
||||
return this._negotiateRelay(
|
||||
relay,
|
||||
dstMa,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err('An error has occurred negotiating the relay connection', err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return cb(null, conn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Negotiate the relay connection
|
||||
*
|
||||
* @param {Multiaddr|PeerInfo|Connection} relay - the Connection or PeerInfo of the relay
|
||||
* @param {multiaddr} dstMa - the multiaddr of the peer to relay the connection for
|
||||
* @param {Function} callback - a callback which gets the negotiated relay connection
|
||||
* @returns {void}
|
||||
* @private
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
_negotiateRelay (relay, dstMa, callback) {
|
||||
dstMa = multiaddr(dstMa)
|
||||
relay = this.utils.peerInfoFromMa(relay)
|
||||
const srcMas = this.swarm._peerInfo.multiaddrs.toArray()
|
||||
this._dialRelayHelper(relay, (err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
const sh = new StreamHandler(conn)
|
||||
waterfall([
|
||||
(cb) => {
|
||||
log('negotiating relay for peer %s', dstMa.getPeerId())
|
||||
let dstPeerId
|
||||
try {
|
||||
dstPeerId = PeerId.createFromB58String(dstMa.getPeerId()).id
|
||||
} catch (err) {
|
||||
return cb(err)
|
||||
}
|
||||
sh.write(
|
||||
proto.CircuitRelay.encode({
|
||||
type: proto.CircuitRelay.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.swarm._peerInfo.id.id,
|
||||
addrs: srcMas.map((addr) => addr.buffer)
|
||||
},
|
||||
dstPeer: {
|
||||
id: dstPeerId,
|
||||
addrs: [dstMa.buffer]
|
||||
}
|
||||
}), cb)
|
||||
},
|
||||
(cb) => sh.read(cb)
|
||||
], (err, msg) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const message = proto.CircuitRelay.decode(msg)
|
||||
if (message.type !== proto.CircuitRelay.Type.STATUS) {
|
||||
return callback(new Error('Got invalid message type - ' +
|
||||
`expected ${proto.CircuitRelay.Type.STATUS} got ${message.type}`))
|
||||
}
|
||||
|
||||
if (message.code !== proto.CircuitRelay.Status.SUCCESS) {
|
||||
return callback(new Error(`Got ${message.code} error code trying to dial over relay`))
|
||||
}
|
||||
|
||||
callback(null, new Connection(sh.rest()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a relay peer by its PeerInfo
|
||||
*
|
||||
* @param {PeerInfo} peer - the PeerInfo of the relay peer
|
||||
* @param {Function} cb - a callback with the connection to the relay peer
|
||||
* @returns {void}
|
||||
* @private
|
||||
*/
|
||||
_dialRelay (peer, cb) {
|
||||
cb = once(cb || (() => { }))
|
||||
|
||||
this.swarm.dial(
|
||||
peer,
|
||||
multicodec.relay,
|
||||
once((err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
cb(null, conn)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dialer
|
283
src/circuit/circuit/hop.js
Normal file
283
src/circuit/circuit/hop.js
Normal file
@ -0,0 +1,283 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const debug = require('debug')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const EE = require('events').EventEmitter
|
||||
const once = require('once')
|
||||
const utilsFactory = require('./utils')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
const proto = require('../protocol').CircuitRelay
|
||||
const multiaddr = require('multiaddr')
|
||||
const series = require('async/series')
|
||||
const waterfall = require('async/waterfall')
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
const multicodec = require('./../multicodec')
|
||||
|
||||
const log = debug('libp2p:circuit:relay')
|
||||
log.err = debug('libp2p:circuit:error:relay')
|
||||
|
||||
class Hop extends EE {
|
||||
/**
|
||||
* Construct a Circuit object
|
||||
*
|
||||
* This class will handle incoming circuit connections and
|
||||
* either start a relay or hand the relayed connection to
|
||||
* the swarm
|
||||
*
|
||||
* @param {Swarm} swarm
|
||||
* @param {Object} options
|
||||
*/
|
||||
constructor (swarm, options) {
|
||||
super()
|
||||
this.swarm = swarm
|
||||
this.peerInfo = this.swarm._peerInfo
|
||||
this.utils = utilsFactory(swarm)
|
||||
this.config = options || { active: false, enabled: false }
|
||||
this.active = this.config.active
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the relay message
|
||||
*
|
||||
* @param {CircuitRelay} message
|
||||
* @param {StreamHandler} sh
|
||||
* @returns {*}
|
||||
*/
|
||||
handle (message, sh) {
|
||||
if (!this.config.enabled) {
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.HOP_CANT_SPEAK_RELAY)
|
||||
return sh.close()
|
||||
}
|
||||
|
||||
// check if message is `CAN_HOP`
|
||||
if (message.type === proto.Type.CAN_HOP) {
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.SUCCESS)
|
||||
return sh.close()
|
||||
}
|
||||
|
||||
// This is a relay request - validate and create a circuit
|
||||
let srcPeerId = null
|
||||
let dstPeerId = null
|
||||
try {
|
||||
srcPeerId = PeerId.createFromBytes(message.srcPeer.id).toB58String()
|
||||
dstPeerId = PeerId.createFromBytes(message.dstPeer.id).toB58String()
|
||||
} catch (err) {
|
||||
log.err(err)
|
||||
|
||||
if (!srcPeerId) {
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.HOP_SRC_MULTIADDR_INVALID)
|
||||
return sh.close()
|
||||
}
|
||||
|
||||
if (!dstPeerId) {
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.HOP_DST_MULTIADDR_INVALID)
|
||||
return sh.close()
|
||||
}
|
||||
}
|
||||
|
||||
if (srcPeerId === dstPeerId) {
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.HOP_CANT_RELAY_TO_SELF)
|
||||
return sh.close()
|
||||
}
|
||||
|
||||
if (!message.dstPeer.addrs.length) {
|
||||
// TODO: use encapsulate here
|
||||
const addr = multiaddr(`/p2p-circuit/ipfs/${dstPeerId}`).buffer
|
||||
message.dstPeer.addrs.push(addr)
|
||||
}
|
||||
|
||||
log('trying to establish a circuit: %s <-> %s', srcPeerId, dstPeerId)
|
||||
const noPeer = () => {
|
||||
// log.err(err)
|
||||
this.utils.writeResponse(
|
||||
sh,
|
||||
proto.Status.HOP_NO_CONN_TO_DST)
|
||||
return sh.close()
|
||||
}
|
||||
|
||||
const isConnected = (cb) => {
|
||||
let dstPeer
|
||||
try {
|
||||
dstPeer = this.swarm._peerBook.get(dstPeerId)
|
||||
if (!dstPeer.isConnected() && !this.active) {
|
||||
const err = new Error(`No Connection to peer ${dstPeerId}`)
|
||||
noPeer(err)
|
||||
return cb(err)
|
||||
}
|
||||
} catch (err) {
|
||||
if (!this.active) {
|
||||
noPeer(err)
|
||||
return cb(err)
|
||||
}
|
||||
}
|
||||
cb()
|
||||
}
|
||||
|
||||
series([
|
||||
(cb) => this.utils.validateAddrs(message, sh, proto.Type.HOP, cb),
|
||||
(cb) => isConnected(cb),
|
||||
(cb) => this._circuit(sh, message, cb)
|
||||
], (err) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
sh.close()
|
||||
return setImmediate(() => this.emit('circuit:error', err))
|
||||
}
|
||||
setImmediate(() => this.emit('circuit:success'))
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to STOP
|
||||
*
|
||||
* @param {PeerInfo} peer
|
||||
* @param {StreamHandler} srcSh
|
||||
* @param {function} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
_connectToStop (peer, srcSh, callback) {
|
||||
this._dialPeer(peer, (err, dstConn) => {
|
||||
if (err) {
|
||||
this.utils.writeResponse(
|
||||
srcSh,
|
||||
proto.Status.HOP_CANT_DIAL_DST)
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
return this.utils.writeResponse(
|
||||
srcSh,
|
||||
proto.Status.SUCCESS,
|
||||
(err) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
return callback(null, dstConn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Negotiate STOP
|
||||
*
|
||||
* @param {StreamHandler} dstSh
|
||||
* @param {StreamHandler} srcSh
|
||||
* @param {CircuitRelay} message
|
||||
* @param {function} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
_negotiateStop (dstSh, srcSh, message, callback) {
|
||||
const stopMsg = Object.assign({}, message, {
|
||||
type: proto.Type.STOP // change the message type
|
||||
})
|
||||
dstSh.write(proto.encode(stopMsg),
|
||||
(err) => {
|
||||
if (err) {
|
||||
this.utils.writeResponse(
|
||||
srcSh,
|
||||
proto.Status.HOP_CANT_OPEN_DST_STREAM)
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// read response from STOP
|
||||
dstSh.read((err, msg) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const message = proto.decode(msg)
|
||||
if (message.code !== proto.Status.SUCCESS) {
|
||||
return callback(new Error('Unable to create circuit!'))
|
||||
}
|
||||
|
||||
return callback(null, msg)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to make a circuit from A <-> R <-> B where R is this relay
|
||||
*
|
||||
* @param {StreamHandler} srcSh - the source stream handler
|
||||
* @param {CircuitRelay} message - the message with the src and dst entries
|
||||
* @param {Function} callback - callback to signal success or failure
|
||||
* @returns {void}
|
||||
* @private
|
||||
*/
|
||||
_circuit (srcSh, message, callback) {
|
||||
let dstSh = null
|
||||
waterfall([
|
||||
(cb) => this._connectToStop(message.dstPeer, srcSh, cb),
|
||||
(_dstConn, cb) => {
|
||||
dstSh = new StreamHandler(_dstConn)
|
||||
this._negotiateStop(dstSh, srcSh, message, cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) {
|
||||
// close/end the source stream if there was an error
|
||||
if (srcSh) {
|
||||
srcSh.close()
|
||||
}
|
||||
|
||||
if (dstSh) {
|
||||
dstSh.close()
|
||||
}
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const src = srcSh.rest()
|
||||
const dst = dstSh.rest()
|
||||
|
||||
const srcIdStr = PeerId.createFromBytes(message.srcPeer.id).toB58String()
|
||||
const dstIdStr = PeerId.createFromBytes(message.dstPeer.id).toB58String()
|
||||
|
||||
// circuit the src and dst streams
|
||||
pull(
|
||||
src,
|
||||
dst,
|
||||
src
|
||||
)
|
||||
log('circuit %s <-> %s established', srcIdStr, dstIdStr)
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial the dest peer and create a circuit
|
||||
*
|
||||
* @param {Multiaddr} dstPeer
|
||||
* @param {Function} callback
|
||||
* @returns {void}
|
||||
* @private
|
||||
*/
|
||||
_dialPeer (dstPeer, callback) {
|
||||
const peerInfo = new PeerInfo(PeerId.createFromBytes(dstPeer.id))
|
||||
dstPeer.addrs.forEach((a) => peerInfo.multiaddrs.add(a))
|
||||
this.swarm.dial(peerInfo, multicodec.relay, once((err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
callback(null, conn)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Hop
|
56
src/circuit/circuit/stop.js
Normal file
56
src/circuit/circuit/stop.js
Normal file
@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
const EE = require('events').EventEmitter
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const utilsFactory = require('./utils')
|
||||
const PeerInfo = require('peer-info')
|
||||
const proto = require('../protocol').CircuitRelay
|
||||
const series = require('async/series')
|
||||
|
||||
const debug = require('debug')
|
||||
|
||||
const log = debug('libp2p:circuit:stop')
|
||||
log.err = debug('libp2p:circuit:error:stop')
|
||||
|
||||
class Stop extends EE {
|
||||
constructor (swarm) {
|
||||
super()
|
||||
this.swarm = swarm
|
||||
this.utils = utilsFactory(swarm)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the incoming STOP message
|
||||
*
|
||||
* @param {{}} msg - the parsed protobuf message
|
||||
* @param {StreamHandler} sh - the stream handler wrapped connection
|
||||
* @param {Function} callback - callback
|
||||
* @returns {undefined}
|
||||
*/
|
||||
handle (msg, sh, callback) {
|
||||
callback = callback || (() => {})
|
||||
|
||||
series([
|
||||
(cb) => this.utils.validateAddrs(msg, sh, proto.Type.STOP, cb),
|
||||
(cb) => this.utils.writeResponse(sh, proto.Status.Success, cb)
|
||||
], (err) => {
|
||||
if (err) {
|
||||
// we don't return the error here,
|
||||
// since multistream select don't expect one
|
||||
callback()
|
||||
return log(err)
|
||||
}
|
||||
|
||||
const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
|
||||
msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
const newConn = new Connection(sh.rest())
|
||||
newConn.setPeerInfo(peerInfo)
|
||||
setImmediate(() => this.emit('connection', newConn))
|
||||
callback(newConn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Stop
|
140
src/circuit/circuit/stream-handler.js
Normal file
140
src/circuit/circuit/stream-handler.js
Normal file
@ -0,0 +1,140 @@
|
||||
'use strict'
|
||||
|
||||
const values = require('pull-stream/sources/values')
|
||||
const collect = require('pull-stream/sinks/collect')
|
||||
const empty = require('pull-stream/sources/empty')
|
||||
const pull = require('pull-stream/pull')
|
||||
const lp = require('pull-length-prefixed')
|
||||
const handshake = require('pull-handshake')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:stream-handler')
|
||||
log.err = debug('libp2p:circuit:error:stream-handler')
|
||||
|
||||
class StreamHandler {
|
||||
/**
|
||||
* Create a stream handler for connection
|
||||
*
|
||||
* @param {Connection} conn - connection to read/write
|
||||
* @param {Function|undefined} cb - handshake callback called on error
|
||||
* @param {Number} timeout - handshake timeout
|
||||
* @param {Number} maxLength - max bytes length of message
|
||||
*/
|
||||
constructor (conn, cb, timeout, maxLength) {
|
||||
this.conn = conn
|
||||
this.stream = null
|
||||
this.shake = null
|
||||
this.timeout = cb || 1000 * 60
|
||||
this.maxLength = maxLength || 4096
|
||||
|
||||
if (typeof cb === 'function') {
|
||||
this.timeout = timeout || 1000 * 60
|
||||
}
|
||||
|
||||
this.stream = handshake({ timeout: this.timeout }, cb)
|
||||
this.shake = this.stream.handshake
|
||||
|
||||
pull(this.stream, conn, this.stream)
|
||||
}
|
||||
|
||||
isValid () {
|
||||
return this.conn && this.shake && this.stream
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and decode message
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @returns {void|Function}
|
||||
*/
|
||||
read (cb) {
|
||||
if (!this.isValid()) {
|
||||
return cb(new Error('handler is not in a valid state'))
|
||||
}
|
||||
|
||||
lp.decodeFromReader(
|
||||
this.shake,
|
||||
{ maxLength: this.maxLength },
|
||||
(err, msg) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
// this.shake.abort(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return cb(null, msg)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode and write array of buffers
|
||||
*
|
||||
* @param {Buffer[]} msg
|
||||
* @param {Function} [cb]
|
||||
* @returns {Function}
|
||||
*/
|
||||
write (msg, cb) {
|
||||
cb = cb || (() => {})
|
||||
|
||||
if (!this.isValid()) {
|
||||
return cb(new Error('handler is not in a valid state'))
|
||||
}
|
||||
|
||||
pull(
|
||||
values([msg]),
|
||||
lp.encode(),
|
||||
collect((err, encoded) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
this.shake.abort(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
encoded.forEach((e) => this.shake.write(e))
|
||||
cb()
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw Connection
|
||||
*
|
||||
* @returns {null|Connection|*}
|
||||
*/
|
||||
getRawConn () {
|
||||
return this.conn
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the handshake rest stream and invalidate handler
|
||||
*
|
||||
* @return {*|{source, sink}}
|
||||
*/
|
||||
rest () {
|
||||
const rest = this.shake.rest()
|
||||
|
||||
this.conn = null
|
||||
this.stream = null
|
||||
this.shake = null
|
||||
return rest
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream
|
||||
*
|
||||
* @returns {undefined}
|
||||
*/
|
||||
close () {
|
||||
if (!this.isValid()) {
|
||||
return
|
||||
}
|
||||
|
||||
// close stream
|
||||
pull(
|
||||
empty(),
|
||||
this.rest()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StreamHandler
|
118
src/circuit/circuit/utils.js
Normal file
118
src/circuit/circuit/utils.js
Normal file
@ -0,0 +1,118 @@
|
||||
'use strict'
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const proto = require('../protocol')
|
||||
const { getPeerInfo } = require('../../get-peer-info')
|
||||
|
||||
module.exports = function (swarm) {
|
||||
/**
|
||||
* Get b58 string from multiaddr or peerinfo
|
||||
*
|
||||
* @param {Multiaddr|PeerInfo} peer
|
||||
* @return {*}
|
||||
*/
|
||||
function getB58String (peer) {
|
||||
let b58Id = null
|
||||
if (multiaddr.isMultiaddr(peer)) {
|
||||
const relayMa = multiaddr(peer)
|
||||
b58Id = relayMa.getPeerId()
|
||||
} else if (PeerInfo.isPeerInfo(peer)) {
|
||||
b58Id = peer.id.toB58String()
|
||||
}
|
||||
|
||||
return b58Id
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to make a peer info from a multiaddrs
|
||||
*
|
||||
* @param {Multiaddr|PeerInfo|PeerId} peer
|
||||
* @return {PeerInfo}
|
||||
* @private
|
||||
*/
|
||||
function peerInfoFromMa (peer) {
|
||||
return getPeerInfo(peer, swarm._peerBook)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if peer has an existing connection
|
||||
*
|
||||
* @param {String} peerId
|
||||
* @param {Swarm} swarm
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function isPeerConnected (peerId) {
|
||||
return swarm.muxedConns[peerId] || swarm.conns[peerId]
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a response
|
||||
*
|
||||
* @param {StreamHandler} streamHandler
|
||||
* @param {CircuitRelay.Status} status
|
||||
* @param {Function} cb
|
||||
* @returns {*}
|
||||
*/
|
||||
function writeResponse (streamHandler, status, cb) {
|
||||
cb = cb || (() => {})
|
||||
streamHandler.write(proto.CircuitRelay.encode({
|
||||
type: proto.CircuitRelay.Type.STATUS,
|
||||
code: status
|
||||
}))
|
||||
return cb()
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate incomming HOP/STOP message
|
||||
*
|
||||
* @param {CircuitRelay} msg
|
||||
* @param {StreamHandler} streamHandler
|
||||
* @param {CircuitRelay.Type} type
|
||||
* @returns {*}
|
||||
* @param {Function} cb
|
||||
*/
|
||||
function validateAddrs (msg, streamHandler, type, cb) {
|
||||
try {
|
||||
msg.dstPeer.addrs.forEach((addr) => {
|
||||
return multiaddr(addr)
|
||||
})
|
||||
} catch (err) {
|
||||
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
|
||||
? proto.CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
|
||||
: proto.CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
try {
|
||||
msg.srcPeer.addrs.forEach((addr) => {
|
||||
return multiaddr(addr)
|
||||
})
|
||||
} catch (err) {
|
||||
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
|
||||
? proto.CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
|
||||
: proto.CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return cb(null)
|
||||
}
|
||||
|
||||
function peerIdFromId (id) {
|
||||
if (typeof id === 'string') {
|
||||
return PeerId.createFromB58String(id)
|
||||
}
|
||||
|
||||
return PeerId.createFromBytes(id)
|
||||
}
|
||||
|
||||
return {
|
||||
getB58String,
|
||||
peerInfoFromMa,
|
||||
isPeerConnected,
|
||||
validateAddrs,
|
||||
writeResponse,
|
||||
peerIdFromId
|
||||
}
|
||||
}
|
3
src/circuit/index.js
Normal file
3
src/circuit/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = require('./circuit')
|
149
src/circuit/listener.js
Normal file
149
src/circuit/listener.js
Normal file
@ -0,0 +1,149 @@
|
||||
'use strict'
|
||||
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
const multicodec = require('./multicodec')
|
||||
const EE = require('events').EventEmitter
|
||||
const multiaddr = require('multiaddr')
|
||||
const mafmt = require('mafmt')
|
||||
const Stop = require('./circuit/stop')
|
||||
const Hop = require('./circuit/hop')
|
||||
const proto = require('./protocol')
|
||||
const utilsFactory = require('./circuit/utils')
|
||||
|
||||
const StreamHandler = require('./circuit/stream-handler')
|
||||
|
||||
const debug = require('debug')
|
||||
|
||||
const log = debug('libp2p:circuit:listener')
|
||||
log.err = debug('libp2p:circuit:error:listener')
|
||||
|
||||
module.exports = (swarm, options, connHandler) => {
|
||||
const listener = new EE()
|
||||
const utils = utilsFactory(swarm)
|
||||
|
||||
listener.stopHandler = new Stop(swarm)
|
||||
listener.stopHandler.on('connection', (conn) => listener.emit('connection', conn))
|
||||
listener.hopHandler = new Hop(swarm, options.hop)
|
||||
|
||||
/**
|
||||
* Add swarm handler and listen for incoming connections
|
||||
*
|
||||
* @param {Multiaddr} ma
|
||||
* @param {Function} callback
|
||||
* @return {void}
|
||||
*/
|
||||
listener.listen = (ma, callback) => {
|
||||
callback = callback || (() => {})
|
||||
|
||||
swarm.handle(multicodec.relay, (_, conn) => {
|
||||
const sh = new StreamHandler(conn)
|
||||
|
||||
sh.read((err, msg) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return
|
||||
}
|
||||
|
||||
let request = null
|
||||
try {
|
||||
request = proto.CircuitRelay.decode(msg)
|
||||
} catch (err) {
|
||||
return utils.writeResponse(
|
||||
sh,
|
||||
proto.CircuitRelay.Status.MALFORMED_MESSAGE)
|
||||
}
|
||||
|
||||
switch (request.type) {
|
||||
case proto.CircuitRelay.Type.CAN_HOP:
|
||||
case proto.CircuitRelay.Type.HOP: {
|
||||
return listener.hopHandler.handle(request, sh)
|
||||
}
|
||||
|
||||
case proto.CircuitRelay.Type.STOP: {
|
||||
return listener.stopHandler.handle(request, sh, connHandler)
|
||||
}
|
||||
|
||||
default: {
|
||||
utils.writeResponse(
|
||||
sh,
|
||||
proto.CircuitRelay.Status.INVALID_MSG_TYPE)
|
||||
return sh.close()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
setImmediate(() => listener.emit('listen'))
|
||||
callback()
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove swarm listener
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @return {void}
|
||||
*/
|
||||
listener.close = (cb) => {
|
||||
swarm.unhandle(multicodec.relay)
|
||||
setImmediate(() => listener.emit('close'))
|
||||
cb()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fixed up multiaddrs
|
||||
*
|
||||
* NOTE: This method will grab the peers multiaddrs and expand them such that:
|
||||
*
|
||||
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
|
||||
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
|
||||
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
|
||||
* `QmPeer` is this peers id
|
||||
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
|
||||
* addr, such when dialing over a relay with this address, it will create the circuit using
|
||||
* the encapsulated transport address. This is useful when for example, a peer should only
|
||||
* be dialed over TCP rather than any other transport
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {void}
|
||||
*/
|
||||
listener.getAddrs = (callback) => {
|
||||
let addrs = swarm._peerInfo.multiaddrs.toArray()
|
||||
|
||||
// get all the explicit relay addrs excluding self
|
||||
const p2pAddrs = addrs.filter((addr) => {
|
||||
return mafmt.Circuit.matches(addr) &&
|
||||
!addr.toString().includes(swarm._peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
// use the explicit relays instead of any relay
|
||||
if (p2pAddrs.length) {
|
||||
addrs = p2pAddrs
|
||||
}
|
||||
|
||||
const listenAddrs = []
|
||||
addrs.forEach((addr) => {
|
||||
const peerMa = `/p2p-circuit/ipfs/${swarm._peerInfo.id.toB58String()}`
|
||||
if (addr.toString() === peerMa) {
|
||||
listenAddrs.push(multiaddr(peerMa))
|
||||
return
|
||||
}
|
||||
|
||||
if (!mafmt.Circuit.matches(addr)) {
|
||||
if (addr.getPeerId()) {
|
||||
// by default we're reachable over any relay
|
||||
listenAddrs.push(multiaddr('/p2p-circuit').encapsulate(addr))
|
||||
} else {
|
||||
const ma = `${addr}/ipfs/${swarm._peerInfo.id.toB58String()}`
|
||||
listenAddrs.push(multiaddr('/p2p-circuit').encapsulate(ma))
|
||||
}
|
||||
} else {
|
||||
listenAddrs.push(addr.encapsulate(`/ipfs/${swarm._peerInfo.id.toB58String()}`))
|
||||
}
|
||||
})
|
||||
|
||||
callback(null, listenAddrs)
|
||||
}
|
||||
|
||||
return listener
|
||||
}
|
5
src/circuit/multicodec.js
Normal file
5
src/circuit/multicodec.js
Normal file
@ -0,0 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
relay: '/libp2p/circuit/relay/0.1.0'
|
||||
}
|
44
src/circuit/protocol/index.js
Normal file
44
src/circuit/protocol/index.js
Normal file
@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
const protobuf = require('protons')
|
||||
module.exports = protobuf(`
|
||||
message CircuitRelay {
|
||||
|
||||
enum Status {
|
||||
SUCCESS = 100;
|
||||
HOP_SRC_ADDR_TOO_LONG = 220;
|
||||
HOP_DST_ADDR_TOO_LONG = 221;
|
||||
HOP_SRC_MULTIADDR_INVALID = 250;
|
||||
HOP_DST_MULTIADDR_INVALID = 251;
|
||||
HOP_NO_CONN_TO_DST = 260;
|
||||
HOP_CANT_DIAL_DST = 261;
|
||||
HOP_CANT_OPEN_DST_STREAM = 262;
|
||||
HOP_CANT_SPEAK_RELAY = 270;
|
||||
HOP_CANT_RELAY_TO_SELF = 280;
|
||||
STOP_SRC_ADDR_TOO_LONG = 320;
|
||||
STOP_DST_ADDR_TOO_LONG = 321;
|
||||
STOP_SRC_MULTIADDR_INVALID = 350;
|
||||
STOP_DST_MULTIADDR_INVALID = 351;
|
||||
STOP_RELAY_REFUSED = 390;
|
||||
MALFORMED_MESSAGE = 400;
|
||||
}
|
||||
|
||||
enum Type { // RPC identifier, either HOP, STOP or STATUS
|
||||
HOP = 1;
|
||||
STOP = 2;
|
||||
STATUS = 3;
|
||||
CAN_HOP = 4;
|
||||
}
|
||||
|
||||
message Peer {
|
||||
required bytes id = 1; // peer id
|
||||
repeated bytes addrs = 2; // peer's known addresses
|
||||
}
|
||||
|
||||
optional Type type = 1; // Type of the message
|
||||
|
||||
optional Peer srcPeer = 2; // srcPeer and dstPeer are used when Type is HOP or STATUS
|
||||
optional Peer dstPeer = 3;
|
||||
|
||||
optional Status code = 4; // Status code, used when Type is STATUS
|
||||
}
|
||||
`)
|
128
src/config.js
128
src/config.js
@ -1,101 +1,45 @@
|
||||
'use strict'
|
||||
|
||||
const { struct, superstruct } = require('superstruct')
|
||||
const { optional, list } = struct
|
||||
const mergeOptions = require('merge-options')
|
||||
|
||||
// Define custom types
|
||||
const s = superstruct()
|
||||
const transport = s.union([
|
||||
s.interface({
|
||||
createListener: 'function',
|
||||
dial: 'function'
|
||||
}),
|
||||
'function'
|
||||
])
|
||||
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.union(['undefined', s.interface({ protect: 'function' })]),
|
||||
contentRouting: optional(list(['object'])),
|
||||
dht: optional(s('null|function|object')),
|
||||
peerDiscovery: optional(list([s('object|function')])),
|
||||
peerRouting: optional(list(['object'])),
|
||||
streamMuxer: optional(list([s('object|function')])),
|
||||
transport: s.intersection([[transport], s.interface({
|
||||
length (v) {
|
||||
return v > 0 ? true : 'ERROR_EMPTY'
|
||||
}
|
||||
})])
|
||||
})
|
||||
|
||||
const configSchema = s({
|
||||
peerDiscovery: s('object', {
|
||||
autoDial: true
|
||||
}),
|
||||
relay: s({
|
||||
enabled: 'boolean',
|
||||
hop: optional(s({
|
||||
enabled: 'boolean',
|
||||
active: 'boolean'
|
||||
}, {
|
||||
// HOP defaults
|
||||
enabled: false,
|
||||
active: false
|
||||
}))
|
||||
}, {
|
||||
// Relay defaults
|
||||
enabled: true
|
||||
}),
|
||||
// DHT config
|
||||
dht: s('object?', {
|
||||
// DHT defaults
|
||||
enabled: false,
|
||||
kBucketSize: 20,
|
||||
randomWalk: {
|
||||
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
}),
|
||||
// Experimental config
|
||||
EXPERIMENTAL: s({
|
||||
pubsub: 'boolean'
|
||||
}, {
|
||||
// Experimental defaults
|
||||
pubsub: false
|
||||
})
|
||||
}, {})
|
||||
|
||||
const optionsSchema = s({
|
||||
switch: 'object?',
|
||||
connectionManager: s('object', {
|
||||
const DefaultConfig = {
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
}),
|
||||
datastore: 'object?',
|
||||
peerInfo: 'object',
|
||||
peerBook: 'object?',
|
||||
modules: modulesSchema,
|
||||
config: configSchema
|
||||
})
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false,
|
||||
kBucketSize: 20,
|
||||
randomWalk: {
|
||||
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
},
|
||||
peerDiscovery: {
|
||||
autoDial: true
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true,
|
||||
signMessages: true,
|
||||
strictSigning: true
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.validate = (opts) => {
|
||||
const [error, options] = optionsSchema.validate(opts)
|
||||
opts = mergeOptions(DefaultConfig, 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")
|
||||
|
||||
if (options.config.peerDiscovery.autoDial === undefined) {
|
||||
options.config.peerDiscovery.autoDial = true
|
||||
}
|
||||
|
||||
return options
|
||||
return opts
|
||||
}
|
||||
|
99
src/connection-manager/README.md
Normal file
99
src/connection-manager/README.md
Normal file
@ -0,0 +1,99 @@
|
||||
# libp2p-connection-manager
|
||||
|
||||
> JavaScript connection manager for libp2p
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-connection-manager.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Install](#install)
|
||||
- [npm](#npm)
|
||||
- [Use in Node.js, a browser with browserify, webpack or any other bundler](##use-in-nodejs-or-in-the-browser-with-browserify-webpack-or-any-other-bundler)
|
||||
- [Usage](#usage)
|
||||
- [API](#api)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## API
|
||||
|
||||
A connection manager manages the peers you're connected to. The application provides one or more limits that will trigger the disconnection of peers. These limits can be any of the following:
|
||||
|
||||
* number of connected peers
|
||||
* maximum bandwidth (sent / received or both)
|
||||
* maximum event loop delay
|
||||
|
||||
The connection manager will disconnect peers (starting from the less important peers) until all the measures are withing the stated limits.
|
||||
|
||||
A connection manager first disconnects the peers with the least value. By default all peers have the same importance (1), but the application can define otherwise. Once a peer disconnects the connection manager discards the peer importance. (If necessary, the application should redefine the peer state if the peer is again connected).
|
||||
|
||||
|
||||
### Create a ConnectionManager
|
||||
|
||||
```js
|
||||
const libp2p = // …
|
||||
const options = {…}
|
||||
const connManager = new ConnManager(libp2p, options)
|
||||
```
|
||||
|
||||
Options is an optional object with the following key-value pairs:
|
||||
|
||||
* **`maxPeers`**: number identifying the maximum number of peers the current peer is willing to be connected to before is starts disconnecting. Defaults to `Infinity`
|
||||
* **`maxPeersPerProtocol`**: Object with key-value pairs, where a key is the protocol tag (case-insensitive) and the value is a number, representing the maximum number of peers allowing to connect for each protocol. Defaults to `{}`.
|
||||
* **`minPeers`**: number identifying the number of peers below which this node will not activate preemptive disconnections. Defaults to `0`.
|
||||
* **`maxData`**: sets the maximum data — in bytes per second - (sent and received) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
* **`maxSentData`**: sets the maximum sent data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
* **`maxReceivedData`**: sets the maximum received data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
* **`maxEventLoopDelay`**: sets the maximum event loop delay (measured in miliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
|
||||
* **`pollInterval`**: sets the poll interval (in miliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
|
||||
* **`movingAverageInterval`**: the interval used to calculate moving averages (in miliseconds). Defaults to `60000` (1 minute).
|
||||
* **`defaultPeerValue`**: number between 0 and 1. Defaults to 1.
|
||||
|
||||
|
||||
### `connManager.start()`
|
||||
|
||||
Starts the connection manager.
|
||||
|
||||
### `connManager.stop()`
|
||||
|
||||
Stops the connection manager.
|
||||
|
||||
|
||||
### `connManager.setPeerValue(peerId, value)`
|
||||
|
||||
Sets the peer value for a given peer id. This is used to sort peers (in reverse order of value) to determine which to disconnect from first.
|
||||
|
||||
Arguments:
|
||||
|
||||
* peerId: B58-encoded string or [`peer-id`](https://github.com/libp2p/js-peer-id) instance.
|
||||
* value: a number between 0 and 1, which represents a scale of how valuable this given peer id is to the application.
|
||||
|
||||
### `connManager.peers()`
|
||||
|
||||
Returns the peers this connection manager is connected to.
|
||||
|
||||
Returns an array of [PeerInfo](https://github.com/libp2p/js-peer-info).
|
||||
|
||||
### `connManager.emit('limit:exceeded', limitName, measured)`
|
||||
|
||||
Emitted when a limit is exceeded. Limit names can be:
|
||||
|
||||
* `maxPeers`
|
||||
* `minPeers`
|
||||
* `maxData`
|
||||
* `maxSentData`
|
||||
* `maxReceivedData`
|
||||
* `maxEventLoopDelay`
|
||||
* a protocol tag string (lower-cased)
|
||||
|
||||
|
||||
### `connManager.emit('disconnect:preemptive', peerId)`
|
||||
|
||||
Emitted when a peer is about to be preemptively disconnected.
|
||||
|
||||
### `connManager.emit('disconnected', peerId)`
|
||||
|
||||
Emitted when a peer is disconnected (preemptively or note). If this peer reconnects, you will need to reset it's value, since the connection manager does not remember it.
|
||||
|
||||
### `connManager.emit('connected', peerId: String)`
|
||||
|
||||
Emitted when a peer connects. This is a good event to set the peer value, so you can get some control over who gets banned once a maximum number of peers is reached.
|
218
src/connection-manager/index.js
Normal file
218
src/connection-manager/index.js
Normal file
@ -0,0 +1,218 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const LatencyMonitor = require('latency-monitor').default
|
||||
const debug = require('debug')('libp2p:connection-manager')
|
||||
|
||||
const defaultOptions = {
|
||||
maxPeers: Infinity,
|
||||
minPeers: 0,
|
||||
maxData: Infinity,
|
||||
maxSentData: Infinity,
|
||||
maxReceivedData: Infinity,
|
||||
maxEventLoopDelay: Infinity,
|
||||
pollInterval: 2000,
|
||||
movingAverageInterval: 60000,
|
||||
defaultPeerValue: 1
|
||||
}
|
||||
|
||||
class ConnectionManager extends EventEmitter {
|
||||
constructor (libp2p, options) {
|
||||
super()
|
||||
this._libp2p = libp2p
|
||||
this._options = Object.assign({}, defaultOptions, options)
|
||||
this._options.maxPeersPerProtocol = fixMaxPeersPerProtocol(this._options.maxPeersPerProtocol)
|
||||
|
||||
debug('options: %j', this._options)
|
||||
|
||||
this._stats = libp2p.stats
|
||||
if (options && !this._stats) {
|
||||
throw new Error('No libp2p.stats')
|
||||
}
|
||||
|
||||
this._peerValues = new Map()
|
||||
this._peers = new Map()
|
||||
this._peerProtocols = new Map()
|
||||
this._peerCountPerProtocol = new Map()
|
||||
this._onStatsUpdate = this._onStatsUpdate.bind(this)
|
||||
this._onPeerConnect = this._onPeerConnect.bind(this)
|
||||
this._onPeerDisconnect = this._onPeerDisconnect.bind(this)
|
||||
|
||||
if (this._libp2p.isStarted()) {
|
||||
this._onceStarted()
|
||||
} else {
|
||||
this._libp2p.once('start', this._onceStarted.bind(this))
|
||||
}
|
||||
}
|
||||
|
||||
start () {
|
||||
this._stats.on('update', this._onStatsUpdate)
|
||||
this._libp2p.on('connection:start', this._onPeerConnect)
|
||||
this._libp2p.on('connection:end', this._onPeerDisconnect)
|
||||
// latency monitor
|
||||
this._latencyMonitor = new LatencyMonitor({
|
||||
dataEmitIntervalMs: this._options.pollInterval
|
||||
})
|
||||
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
||||
this._latencyMonitor.on('data', this._onLatencyMeasure)
|
||||
}
|
||||
|
||||
stop () {
|
||||
this._stats.removeListener('update', this._onStatsUpdate)
|
||||
this._libp2p.removeListener('connection:start', this._onPeerConnect)
|
||||
this._libp2p.removeListener('connection:end', this._onPeerDisconnect)
|
||||
this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
|
||||
}
|
||||
|
||||
setPeerValue (peerId, value) {
|
||||
if (value < 0 || value > 1) {
|
||||
throw new Error('value should be a number between 0 and 1')
|
||||
}
|
||||
if (peerId.toB58String) {
|
||||
peerId = peerId.toB58String()
|
||||
}
|
||||
this._peerValues.set(peerId, value)
|
||||
}
|
||||
|
||||
_onceStarted () {
|
||||
this._peerId = this._libp2p.peerInfo.id.toB58String()
|
||||
}
|
||||
|
||||
_onStatsUpdate () {
|
||||
const movingAvgs = this._stats.global.movingAverages
|
||||
const received = movingAvgs.dataReceived[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkLimit('maxReceivedData', received)
|
||||
const sent = movingAvgs.dataSent[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkLimit('maxSentData', sent)
|
||||
const total = received + sent
|
||||
this._checkLimit('maxData', total)
|
||||
debug('stats update', total)
|
||||
}
|
||||
|
||||
_onPeerConnect (peerInfo) {
|
||||
const peerId = peerInfo.id.toB58String()
|
||||
debug('%s: connected to %s', this._peerId, peerId)
|
||||
this._peerValues.set(peerId, this._options.defaultPeerValue)
|
||||
this._peers.set(peerId, peerInfo)
|
||||
this.emit('connected', peerId)
|
||||
this._checkLimit('maxPeers', this._peers.size)
|
||||
|
||||
protocolsFromPeerInfo(peerInfo).forEach((protocolTag) => {
|
||||
const protocol = this._peerCountPerProtocol[protocolTag]
|
||||
if (!protocol) {
|
||||
this._peerCountPerProtocol[protocolTag] = 0
|
||||
}
|
||||
this._peerCountPerProtocol[protocolTag]++
|
||||
|
||||
let peerProtocols = this._peerProtocols[peerId]
|
||||
if (!peerProtocols) {
|
||||
peerProtocols = this._peerProtocols[peerId] = new Set()
|
||||
}
|
||||
peerProtocols.add(protocolTag)
|
||||
this._checkProtocolMaxPeersLimit(protocolTag, this._peerCountPerProtocol[protocolTag])
|
||||
})
|
||||
}
|
||||
|
||||
_onPeerDisconnect (peerInfo) {
|
||||
const peerId = peerInfo.id.toB58String()
|
||||
debug('%s: disconnected from %s', this._peerId, peerId)
|
||||
this._peerValues.delete(peerId)
|
||||
this._peers.delete(peerId)
|
||||
|
||||
const peerProtocols = this._peerProtocols[peerId]
|
||||
if (peerProtocols) {
|
||||
Array.from(peerProtocols).forEach((protocolTag) => {
|
||||
const peerCountForProtocol = this._peerCountPerProtocol[protocolTag]
|
||||
if (peerCountForProtocol) {
|
||||
this._peerCountPerProtocol[protocolTag]--
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
this.emit('disconnected', peerId)
|
||||
}
|
||||
|
||||
_onLatencyMeasure (summary) {
|
||||
this._checkLimit('maxEventLoopDelay', summary.avgMs)
|
||||
}
|
||||
|
||||
_checkLimit (name, value) {
|
||||
const limit = this._options[name]
|
||||
debug('checking limit of %s. current value: %d of %d', name, value, limit)
|
||||
if (value > limit) {
|
||||
debug('%s: limit exceeded: %s, %d', this._peerId, name, value)
|
||||
this.emit('limit:exceeded', name, value)
|
||||
this._maybeDisconnectOne()
|
||||
}
|
||||
}
|
||||
|
||||
_checkProtocolMaxPeersLimit (protocolTag, value) {
|
||||
debug('checking protocol limit. current value of %s is %d', protocolTag, value)
|
||||
const limit = this._options.maxPeersPerProtocol[protocolTag]
|
||||
if (value > limit) {
|
||||
debug('%s: protocol max peers limit exceeded: %s, %d', this._peerId, protocolTag, value)
|
||||
this.emit('limit:exceeded', protocolTag, value)
|
||||
this._maybeDisconnectOne()
|
||||
}
|
||||
}
|
||||
|
||||
_maybeDisconnectOne () {
|
||||
if (this._options.minPeers < this._peerValues.size) {
|
||||
const peerValues = Array.from(this._peerValues).sort(byPeerValue)
|
||||
debug('%s: sorted peer values: %j', this._peerId, peerValues)
|
||||
const disconnectPeer = peerValues[0]
|
||||
if (disconnectPeer) {
|
||||
const peerId = disconnectPeer[0]
|
||||
debug('%s: lowest value peer is %s', this._peerId, peerId)
|
||||
debug('%s: forcing disconnection from %j', this._peerId, peerId)
|
||||
this._disconnectPeer(peerId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_disconnectPeer (peerId) {
|
||||
debug('preemptively disconnecting peer', peerId)
|
||||
this.emit('%s: disconnect:preemptive', this._peerId, peerId)
|
||||
const peer = this._peers.get(peerId)
|
||||
this._libp2p.hangUp(peer, (err) => {
|
||||
if (err) {
|
||||
this.emit('error', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConnectionManager
|
||||
|
||||
function byPeerValue (peerValueEntryA, peerValueEntryB) {
|
||||
return peerValueEntryA[1] - peerValueEntryB[1]
|
||||
}
|
||||
|
||||
function fixMaxPeersPerProtocol (maxPeersPerProtocol) {
|
||||
if (!maxPeersPerProtocol) {
|
||||
maxPeersPerProtocol = {}
|
||||
}
|
||||
|
||||
Object.keys(maxPeersPerProtocol).forEach((transportTag) => {
|
||||
const max = maxPeersPerProtocol[transportTag]
|
||||
delete maxPeersPerProtocol[transportTag]
|
||||
maxPeersPerProtocol[transportTag.toLowerCase()] = max
|
||||
})
|
||||
|
||||
return maxPeersPerProtocol
|
||||
}
|
||||
|
||||
function protocolsFromPeerInfo (peerInfo) {
|
||||
const protocolTags = new Set()
|
||||
peerInfo.multiaddrs.forEach((multiaddr) => {
|
||||
multiaddr.protos().map(protocolToProtocolTag).forEach((protocolTag) => {
|
||||
protocolTags.add(protocolTag)
|
||||
})
|
||||
})
|
||||
|
||||
return Array.from(protocolTags)
|
||||
}
|
||||
|
||||
function protocolToProtocolTag (protocol) {
|
||||
return protocol.name.toLowerCase()
|
||||
}
|
12
src/constants.js
Normal file
12
src/constants.js
Normal file
@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
|
||||
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
|
||||
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
|
||||
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
|
||||
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
|
||||
QUARTER_HOUR: 15 * 60e3,
|
||||
PRIORITY_HIGH: 10,
|
||||
PRIORITY_LOW: 20
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
const tryEach = require('async/tryEach')
|
||||
const parallel = require('async/parallel')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.contentRouting || []
|
||||
@ -24,7 +25,7 @@ module.exports = (node) => {
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findProviders: (key, options, callback) => {
|
||||
findProviders: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
@ -60,7 +61,7 @@ module.exports = (node) => {
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
},
|
||||
}),
|
||||
|
||||
/**
|
||||
* Iterates over all content routers in parallel to notify it is
|
||||
@ -70,7 +71,7 @@ module.exports = (node) => {
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
provide: (key, callback) => {
|
||||
provide: promisify((key, callback) => {
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
@ -78,6 +79,6 @@ module.exports = (node) => {
|
||||
parallel(routers.map((router) => {
|
||||
return (cb) => router.provide(key, cb)
|
||||
}), callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
13
src/dht.js
13
src/dht.js
@ -2,19 +2,20 @@
|
||||
|
||||
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: (key, value, callback) => {
|
||||
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: (key, options, callback) => {
|
||||
}),
|
||||
get: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
@ -25,8 +26,8 @@ module.exports = (node) => {
|
||||
}
|
||||
|
||||
node._dht.get(key, options, callback)
|
||||
},
|
||||
getMany: (key, nVals, options, callback) => {
|
||||
}),
|
||||
getMany: promisify((key, nVals, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
@ -37,6 +38,6 @@ module.exports = (node) => {
|
||||
}
|
||||
|
||||
node._dht.getMany(key, nVals, options, callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
127
src/dialer.js
Normal file
127
src/dialer.js
Normal file
@ -0,0 +1,127 @@
|
||||
'use strict'
|
||||
|
||||
const nextTick = require('async/nextTick')
|
||||
const multiaddr = require('multiaddr')
|
||||
const errCode = require('err-code')
|
||||
const { default: PQueue } = require('p-queue')
|
||||
const AbortController = require('abort-controller')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:dialer')
|
||||
log.error = debug('libp2p:dialer:error')
|
||||
|
||||
const { codes } = require('./errors')
|
||||
const {
|
||||
MAX_PARALLEL_DIALS,
|
||||
DIAL_TIMEOUT
|
||||
} = require('./constants')
|
||||
|
||||
class Dialer {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {TransportManager} options.transportManager
|
||||
* @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,
|
||||
concurrency = MAX_PARALLEL_DIALS,
|
||||
timeout = DIAL_TIMEOUT
|
||||
}) {
|
||||
this.transportManager = transportManager
|
||||
this.concurrency = concurrency
|
||||
this.timeout = timeout
|
||||
this.queue = new PQueue({ concurrency, timeout, throwOnTimeout: true })
|
||||
|
||||
/**
|
||||
* @property {IdentifyService}
|
||||
*/
|
||||
this._identifyService = null
|
||||
}
|
||||
|
||||
set identifyService (service) {
|
||||
this._identifyService = service
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {IdentifyService}
|
||||
*/
|
||||
get identifyService () {
|
||||
return this._identifyService
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a given `Multiaddr`. `addr` should include the id of the peer being
|
||||
* dialed, it will be used for encryption verification.
|
||||
*
|
||||
* @async
|
||||
* @param {Multiaddr} addr The address to dial
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal] An AbortController signal
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async connectToMultiaddr (addr, options = {}) {
|
||||
addr = multiaddr(addr)
|
||||
let conn
|
||||
let controller
|
||||
|
||||
if (!options.signal) {
|
||||
controller = new AbortController()
|
||||
options.signal = controller.signal
|
||||
}
|
||||
|
||||
try {
|
||||
conn = await this.queue.add(() => this.transportManager.dial(addr, options))
|
||||
} catch (err) {
|
||||
if (err.name === 'TimeoutError') {
|
||||
controller.abort()
|
||||
err.code = codes.ERR_TIMEOUT
|
||||
}
|
||||
log.error('Error dialing address %s,', addr, err)
|
||||
throw err
|
||||
}
|
||||
|
||||
// Perform a delayed Identify handshake
|
||||
if (this.identifyService) {
|
||||
nextTick(async () => {
|
||||
try {
|
||||
await this.identifyService.identify(conn, conn.remotePeer)
|
||||
// TODO: Update the PeerStore with the information from identify
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a given `PeerInfo` by dialing all of its known addresses.
|
||||
* The dial to the first address that is successfully able to upgrade a connection
|
||||
* will be used.
|
||||
*
|
||||
* @async
|
||||
* @param {PeerInfo} peerInfo The remote peer to dial
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal] An AbortController signal
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async connectToPeer (peerInfo, options = {}) {
|
||||
const addrs = peerInfo.multiaddrs.toArray()
|
||||
for (const addr of addrs) {
|
||||
try {
|
||||
return await this.connectToMultiaddr(addr, options)
|
||||
} catch (_) {
|
||||
// The error is already logged, just move to the next addr
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
const err = errCode(new Error('Could not dial peer, all addresses failed'), codes.ERR_CONNECTION_FAILED)
|
||||
log.error(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dialer
|
@ -8,6 +8,19 @@ exports.messages = {
|
||||
exports.codes = {
|
||||
DHT_DISABLED: 'ERR_DHT_DISABLED',
|
||||
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_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_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'
|
||||
}
|
||||
|
@ -5,62 +5,72 @@ const PeerInfo = require('peer-info')
|
||||
const multiaddr = require('multiaddr')
|
||||
const errCode = require('err-code')
|
||||
|
||||
module.exports = (node) => {
|
||||
/*
|
||||
* Helper method to check the data type of peer and convert it to PeerInfo
|
||||
*/
|
||||
return function (peer, callback) {
|
||||
let p
|
||||
// PeerInfo
|
||||
if (PeerInfo.isPeerInfo(peer)) {
|
||||
p = peer
|
||||
// Multiaddr instance or Multiaddr String
|
||||
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
||||
if (typeof peer === 'string') {
|
||||
try {
|
||||
peer = multiaddr(peer)
|
||||
} catch (err) {
|
||||
return callback(
|
||||
errCode(err, 'ERR_INVALID_MULTIADDR')
|
||||
)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Converts the given `peer` to a `PeerInfo` instance.
|
||||
* The `PeerStore` will be checked for the resulting peer, and
|
||||
* the peer will be updated in the `PeerStore`.
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @param {PeerStore} peerStore
|
||||
* @returns {PeerInfo}
|
||||
*/
|
||||
function getPeerInfo (peer, peerStore) {
|
||||
if (typeof peer === 'string') {
|
||||
peer = multiaddr(peer)
|
||||
}
|
||||
|
||||
const peerIdB58Str = peer.getPeerId()
|
||||
|
||||
if (!peerIdB58Str) {
|
||||
return callback(
|
||||
errCode(
|
||||
new Error('peer multiaddr instance or string must include peerId'),
|
||||
'ERR_INVALID_MULTIADDR'
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
p = node.peerBook.get(peerIdB58Str)
|
||||
} catch (err) {
|
||||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
||||
}
|
||||
p.multiaddrs.add(peer)
|
||||
|
||||
// PeerId
|
||||
} else if (PeerId.isPeerId(peer)) {
|
||||
const peerIdB58Str = peer.toB58String()
|
||||
try {
|
||||
p = node.peerBook.get(peerIdB58Str)
|
||||
} catch (err) {
|
||||
return node.peerRouting.findPeer(peer, callback)
|
||||
}
|
||||
} else {
|
||||
return callback(
|
||||
errCode(
|
||||
new Error(`${p} is not a valid peer type`),
|
||||
'ERR_INVALID_PEER_TYPE'
|
||||
)
|
||||
let addr
|
||||
if (multiaddr.isMultiaddr(peer)) {
|
||||
addr = peer
|
||||
try {
|
||||
peer = PeerId.createFromB58String(peer.getPeerId())
|
||||
} catch (err) {
|
||||
throw errCode(
|
||||
new Error(`${peer} is not a valid peer type`),
|
||||
'ERR_INVALID_MULTIADDR'
|
||||
)
|
||||
}
|
||||
|
||||
callback(null, p)
|
||||
}
|
||||
|
||||
if (PeerId.isPeerId(peer)) {
|
||||
peer = new PeerInfo(peer)
|
||||
}
|
||||
|
||||
addr && peer.multiaddrs.add(addr)
|
||||
|
||||
return peerStore ? peerStore.put(peer) : peer
|
||||
}
|
||||
|
||||
/**
|
||||
* If `getPeerInfo` does not return a peer with multiaddrs,
|
||||
* the `libp2p` PeerRouter will be used to attempt to find the peer.
|
||||
*
|
||||
* @async
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @param {Libp2p} libp2p
|
||||
* @returns {Promise<PeerInfo>}
|
||||
*/
|
||||
function getPeerInfoRemote (peer, libp2p) {
|
||||
let peerInfo
|
||||
|
||||
try {
|
||||
peerInfo = getPeerInfo(peer, libp2p.peerStore)
|
||||
} catch (err) {
|
||||
return Promise.reject(errCode(
|
||||
new Error(`${peer} is not a valid peer type`),
|
||||
'ERR_INVALID_PEER_TYPE'
|
||||
))
|
||||
}
|
||||
|
||||
// If we don't have an address for the peer, attempt to find it
|
||||
if (peerInfo.multiaddrs.size < 1) {
|
||||
return libp2p.peerRouting.findPeer(peerInfo.id)
|
||||
}
|
||||
|
||||
return Promise.resolve(peerInfo)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getPeerInfoRemote,
|
||||
getPeerInfo
|
||||
}
|
||||
|
13
src/identify/README.md
Normal file
13
src/identify/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# js-libp2p-identify
|
||||
|
||||
> libp2p Identify Protocol
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-identify.
|
||||
|
||||
## Description
|
||||
|
||||
Identify is a STUN protocol, used by libp2p 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
|
||||
|
||||
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'
|
299
src/identify/index.js
Normal file
299
src/identify/index.js
Normal file
@ -0,0 +1,299 @@
|
||||
'use strict'
|
||||
|
||||
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('../util')
|
||||
|
||||
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
|
||||
* @param {PeerID} expectedPeer The PeerId the identify response should match
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async identify (connection, expectedPeer) {
|
||||
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 (expectedPeer && expectedPeer.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.update(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.update(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
|
30
src/identify/message.js
Normal file
30
src/identify/message.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const protons = require('protons')
|
||||
const schema = `
|
||||
message Identify {
|
||||
// protocolVersion determines compatibility between peers
|
||||
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
|
||||
|
||||
// agentVersion is like a UserAgent string in browsers, or client version in bittorrent
|
||||
// includes the client name and client.
|
||||
optional string agentVersion = 6; // e.g. go-ipfs/0.1.0
|
||||
|
||||
// publicKey is this node's public key (which also gives its node.ID)
|
||||
// - may not need to be sent, as secure channel implies it has been sent.
|
||||
// - then again, if we change / disable secure channel, may still want it.
|
||||
optional bytes publicKey = 1;
|
||||
|
||||
// listenAddrs are the multiaddrs the sender node listens for open connections on
|
||||
repeated bytes listenAddrs = 2;
|
||||
|
||||
// oservedAddr is the multiaddr of the remote endpoint that the sender node perceives
|
||||
// this is useful information to convey to the other side, as it helps the remote endpoint
|
||||
// determine whether its connection to the local peer goes through NAT.
|
||||
optional bytes observedAddr = 4;
|
||||
|
||||
repeated string protocols = 3;
|
||||
}
|
||||
`
|
||||
|
||||
module.exports = protons(schema).Identify
|
452
src/index.js
452
src/index.js
@ -1,33 +1,39 @@
|
||||
'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('libp2p-switch')
|
||||
const Ping = require('libp2p-ping')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const ConnectionManager = require('libp2p-connection-manager')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Switch = require('./switch')
|
||||
const Ping = require('./ping')
|
||||
|
||||
const { emitFirst } = require('./util')
|
||||
const peerRouting = require('./peer-routing')
|
||||
const contentRouting = require('./content-routing')
|
||||
const dht = require('./dht')
|
||||
const pubsub = require('./pubsub')
|
||||
const getPeerInfo = require('./get-peer-info')
|
||||
const validateConfig = require('./config').validate
|
||||
const { getPeerInfo, getPeerInfoRemote } = require('./get-peer-info')
|
||||
const { validate: validateConfig } = require('./config')
|
||||
const { codes } = require('./errors')
|
||||
|
||||
const Dialer = require('./dialer')
|
||||
const TransportManager = require('./transport-manager')
|
||||
const Upgrader = require('./upgrader')
|
||||
const PeerStore = require('./peer-store')
|
||||
const Registrar = require('./registrar')
|
||||
const {
|
||||
IdentifyService,
|
||||
multicodecs: IDENTIFY_PROTOCOLS
|
||||
} = require('./identify')
|
||||
|
||||
const notStarted = (action, state) => {
|
||||
return errCode(
|
||||
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
|
||||
@ -52,61 +58,81 @@ 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.peerStore = new PeerStore()
|
||||
|
||||
// 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._switch = new Switch(this.peerInfo, this.peerStore, this._options.switch)
|
||||
|
||||
this.stats = this._switch.stats
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
// Setup the Upgrader
|
||||
this.upgrader = new Upgrader({
|
||||
localPeer: this.peerInfo.id,
|
||||
onConnection: (connection) => {
|
||||
const peerInfo = getPeerInfo(connection.remotePeer)
|
||||
|
||||
// Attach stream multiplexers
|
||||
if (this._modules.streamMuxer) {
|
||||
let 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.peerStore.put(peerInfo)
|
||||
this.registrar.onConnect(peerInfo, connection)
|
||||
this.emit('peer:connect', peerInfo)
|
||||
})
|
||||
},
|
||||
onConnectionEnd: (connection) => {
|
||||
const peerInfo = getPeerInfo(connection.remotePeer)
|
||||
|
||||
this._switch.on('peer-mux-closed', (peerInfo) => {
|
||||
this.registrar.onDisconnect(peerInfo, connection)
|
||||
this.emit('peer:disconnect', peerInfo)
|
||||
})
|
||||
}
|
||||
|
||||
// Events for anytime connections are created/removed
|
||||
this._switch.on('connection:start', (peerInfo) => {
|
||||
this.emit('connection:start', peerInfo)
|
||||
}
|
||||
})
|
||||
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
|
||||
|
||||
// Setup the transport manager
|
||||
this.transportManager = new TransportManager({
|
||||
libp2p: this,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
|
||||
})
|
||||
|
||||
// Attach crypto channels
|
||||
if (this._modules.connEncryption) {
|
||||
let cryptos = 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
|
||||
})
|
||||
|
||||
// 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.dialer.identifyService = new IdentifyService({
|
||||
registrar: this.registrar,
|
||||
peerInfo: this.peerInfo,
|
||||
protocols: this.upgrader.protocols
|
||||
})
|
||||
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.dialer.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')
|
||||
}
|
||||
@ -121,9 +147,9 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
// enable/disable pubsub
|
||||
if (this._config.EXPERIMENTAL.pubsub) {
|
||||
this.pubsub = pubsub(this)
|
||||
// start pubsub
|
||||
if (this._modules.pubsub) {
|
||||
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
|
||||
}
|
||||
|
||||
// Attach remaining APIs
|
||||
@ -132,15 +158,14 @@ class Libp2p extends EventEmitter {
|
||||
this.contentRouting = contentRouting(this)
|
||||
this.dht = dht(this)
|
||||
|
||||
this._getPeerInfo = getPeerInfo(this)
|
||||
|
||||
// Mount default protocols
|
||||
Ping.mount(this._switch)
|
||||
|
||||
this.state = new FSM('STOPPED', {
|
||||
STOPPED: {
|
||||
start: 'STARTING',
|
||||
stop: 'STOPPED'
|
||||
stop: 'STOPPED',
|
||||
done: 'STOPPED'
|
||||
},
|
||||
STARTING: {
|
||||
done: 'STARTED',
|
||||
@ -162,7 +187,6 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
this.state.on('STOPPING', () => {
|
||||
log('libp2p is stopping')
|
||||
this._onStopping()
|
||||
})
|
||||
this.state.on('STARTED', () => {
|
||||
log('libp2p has started')
|
||||
@ -179,13 +203,18 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
// Once we start, emit and dial any peers we may have already discovered
|
||||
this.state.on('STARTED', () => {
|
||||
this.peerBook.getAllArray().forEach((peerInfo) => {
|
||||
for (const peerInfo of this.peerStore.peers) {
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
this._peerDiscovered = this._peerDiscovered.bind(this)
|
||||
|
||||
// promisify all instance methods
|
||||
;['start', 'hangUp', 'ping'].forEach(method => {
|
||||
this[method] = promisify(this[method], { context: this })
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,13 +245,23 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
/**
|
||||
* 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)
|
||||
async stop () {
|
||||
this.state('stop')
|
||||
|
||||
try {
|
||||
this.pubsub && await this.pubsub.stop()
|
||||
await this.transportManager.close()
|
||||
await this._switch.stop()
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
}
|
||||
this.state('done')
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
@ -231,237 +270,141 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
let connection
|
||||
if (multiaddr.isMultiaddr(peer)) {
|
||||
connection = await this.dialer.connectToMultiaddr(peer, options)
|
||||
} else {
|
||||
peer = await getPeerInfoRemote(peer, this)
|
||||
connection = await this.dialer.connectToPeer(peer, options)
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
const peerInfo = getPeerInfo(connection.remotePeer)
|
||||
|
||||
// If a protocol was provided, create a new stream
|
||||
if (protocols) {
|
||||
const stream = await connection.newStream(protocols)
|
||||
|
||||
peerInfo.protocols.add(stream.protocol)
|
||||
this.peerStore.put(peerInfo)
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dial(peerInfo, protocol, callback)
|
||||
})
|
||||
this.peerStore.put(peerInfo)
|
||||
return connection
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `dial` and `dialProtocol`, but the callback will contain a
|
||||
* Connection State Machine.
|
||||
* Disconnects from the given peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, ConnectionFSM)} callback
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialFSM (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dialFSM(peerInfo, protocol, callback)
|
||||
})
|
||||
}
|
||||
|
||||
hangUp (peer, callback) {
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
})
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
}, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings the provided peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error, Ping)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('ping', this.state._state))
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
}, callback)
|
||||
}
|
||||
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
/**
|
||||
* Registers the `handler` for each protocol
|
||||
* @param {string[]|string} protocols
|
||||
* @param {function({ connection:*, stream:*, protocol:string })} handler
|
||||
*/
|
||||
handle (protocols, handler) {
|
||||
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
||||
protocols.forEach(protocol => {
|
||||
this.upgrader.protocols.set(protocol, handler)
|
||||
})
|
||||
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
/**
|
||||
* 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)
|
||||
})
|
||||
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
|
||||
_onStarting () {
|
||||
async _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()))
|
||||
}
|
||||
})
|
||||
this.peerInfo.multiaddrs.replace(maOld, maNew)
|
||||
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
let t
|
||||
// Start parallel tasks
|
||||
const tasks = [
|
||||
this.transportManager.listen(multiaddrs)
|
||||
]
|
||||
|
||||
if (typeof Transport === 'function') {
|
||||
t = new Transport({ libp2p: this })
|
||||
} else {
|
||||
t = Transport
|
||||
}
|
||||
if (this._config.pubsub.enabled) {
|
||||
this.pubsub && this.pubsub.start()
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
try {
|
||||
await Promise.all(tasks)
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
return this.state('stop')
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// detect which multiaddrs we don't have a transport for and remove them
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
|
||||
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._floodSub) {
|
||||
return this._floodSub.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._floodSub) {
|
||||
return this._floodSub.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')
|
||||
})
|
||||
// libp2p has started
|
||||
this.state('done')
|
||||
}
|
||||
|
||||
/**
|
||||
@ -469,12 +412,6 @@ class Libp2p extends EventEmitter {
|
||||
* 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.
|
||||
*
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
@ -483,7 +420,7 @@ class Libp2p extends EventEmitter {
|
||||
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
||||
return
|
||||
}
|
||||
peerInfo = this.peerBook.put(peerInfo)
|
||||
peerInfo = this.peerStore.put(peerInfo)
|
||||
|
||||
if (!this.isStarted()) return
|
||||
|
||||
@ -554,16 +491,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 = (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;
|
||||
}
|
||||
`)
|
@ -2,6 +2,7 @@
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.peerRouting || []
|
||||
@ -21,7 +22,7 @@ module.exports = (node) => {
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findPeer: (id, options, callback) => {
|
||||
findPeer: promisify((id, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
@ -47,12 +48,12 @@ module.exports = (node) => {
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err && err.code !== 'NOT_FOUND') {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
3
src/peer-store/README.md
Normal file
3
src/peer-store/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Peerstore
|
||||
|
||||
WIP
|
190
src/peer-store/index.js
Normal file
190
src/peer-store/index.js
Normal file
@ -0,0 +1,190 @@
|
||||
'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 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.
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
put (peerInfo) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
// Already know the peer?
|
||||
if (this.peers.has(peerInfo.id.toB58String())) {
|
||||
this.update(peerInfo)
|
||||
} else {
|
||||
this.add(peerInfo)
|
||||
|
||||
// Emit the new peer found
|
||||
this.emit('peer', peerInfo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new peer to the store.
|
||||
* @param {PeerInfo} 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an already known peer.
|
||||
* @param {PeerInfo} 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) {
|
||||
// recorded.multiaddrs = peerInfo.multiaddrs
|
||||
recorded.multiaddrs.clear()
|
||||
|
||||
for (const ma of peerInfo.multiaddrs.toArray()) {
|
||||
recorded.multiaddrs.add(ma)
|
||||
}
|
||||
|
||||
this.emit('change:multiaddrs', {
|
||||
peerInfo: peerInfo,
|
||||
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) {
|
||||
recorded.protocols.clear()
|
||||
|
||||
for (const protocol of peerInfo.protocols) {
|
||||
recorded.protocols.add(protocol)
|
||||
}
|
||||
|
||||
this.emit('change:protocols', {
|
||||
peerInfo: peerInfo,
|
||||
protocols: Array.from(recorded.protocols)
|
||||
})
|
||||
}
|
||||
|
||||
// Add the public key if missing
|
||||
if (!recorded.id.pubKey && peerInfo.id.pubKey) {
|
||||
recorded.id.pubKey = peerInfo.id.pubKey
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the info to the given id.
|
||||
* @param {string} peerId b58str id
|
||||
* @returns {PeerInfo}
|
||||
*/
|
||||
get (peerId) {
|
||||
const peerInfo = this.peers.get(peerId)
|
||||
|
||||
if (peerInfo) {
|
||||
return peerInfo
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the Peer with the matching `peerId` from the PeerStore
|
||||
* @param {string} peerId b58str id
|
||||
* @returns {boolean} true if found and removed
|
||||
*/
|
||||
remove (peerId) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PeerStore
|
23
src/ping/README.md
Normal file
23
src/ping/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
libp2p-ping JavaScript Implementation
|
||||
=====================================
|
||||
|
||||
> IPFS ping protocol JavaScript implementation
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-ping.
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
var Ping = require('libp2p-ping')
|
||||
|
||||
Ping.mount(swarm) // Enable this peer to echo Ping requests
|
||||
|
||||
var p = new Ping(swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object
|
||||
|
||||
p.on('ping', function (time) {
|
||||
console.log(time + 'ms')
|
||||
p.stop() // stop sending pings
|
||||
})
|
||||
|
||||
p.start()
|
||||
```
|
6
src/ping/constants.js
Normal file
6
src/ping/constants.js
Normal file
@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
PROTOCOL: '/ipfs/ping/1.0.0',
|
||||
PING_LENGTH: 32
|
||||
}
|
50
src/ping/handler.js
Normal file
50
src/ping/handler.js
Normal file
@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const handshake = require('pull-handshake')
|
||||
const constants = require('./constants')
|
||||
const PROTOCOL = constants.PROTOCOL
|
||||
const PING_LENGTH = constants.PING_LENGTH
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p-ping')
|
||||
log.error = debug('libp2p-ping:error')
|
||||
|
||||
function mount (swarm) {
|
||||
swarm.handle(PROTOCOL, (protocol, conn) => {
|
||||
const stream = handshake({ timeout: 0 })
|
||||
const shake = stream.handshake
|
||||
|
||||
// receive and echo back
|
||||
function next () {
|
||||
shake.read(PING_LENGTH, (err, buf) => {
|
||||
if (err === true) {
|
||||
// stream closed
|
||||
return
|
||||
}
|
||||
if (err) {
|
||||
return log.error(err)
|
||||
}
|
||||
|
||||
shake.write(buf)
|
||||
return next()
|
||||
})
|
||||
}
|
||||
|
||||
pull(
|
||||
conn,
|
||||
stream,
|
||||
conn
|
||||
)
|
||||
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
function unmount (swarm) {
|
||||
swarm.unhandle(PROTOCOL)
|
||||
}
|
||||
|
||||
exports = module.exports
|
||||
exports.mount = mount
|
||||
exports.unmount = unmount
|
7
src/ping/index.js
Normal file
7
src/ping/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const handler = require('./handler')
|
||||
|
||||
exports = module.exports = require('./ping')
|
||||
exports.mount = handler.mount
|
||||
exports.unmount = handler.unmount
|
83
src/ping/ping.js
Normal file
83
src/ping/ping.js
Normal file
@ -0,0 +1,83 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const pull = require('pull-stream/pull')
|
||||
const empty = require('pull-stream/sources/empty')
|
||||
const handshake = require('pull-handshake')
|
||||
const constants = require('./constants')
|
||||
const util = require('./util')
|
||||
const rnd = util.rnd
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p-ping')
|
||||
log.error = debug('libp2p-ping:error')
|
||||
|
||||
const PROTOCOL = constants.PROTOCOL
|
||||
const PING_LENGTH = constants.PING_LENGTH
|
||||
|
||||
class Ping extends EventEmitter {
|
||||
constructor (swarm, peer) {
|
||||
super()
|
||||
|
||||
this._stopped = false
|
||||
this.peer = peer
|
||||
this.swarm = swarm
|
||||
}
|
||||
|
||||
start () {
|
||||
log('dialing %s to %s', PROTOCOL, this.peer.id.toB58String())
|
||||
|
||||
this.swarm.dial(this.peer, PROTOCOL, (err, conn) => {
|
||||
if (err) {
|
||||
return this.emit('error', err)
|
||||
}
|
||||
|
||||
const stream = handshake({ timeout: 0 })
|
||||
this.shake = stream.handshake
|
||||
|
||||
pull(
|
||||
stream,
|
||||
conn,
|
||||
stream
|
||||
)
|
||||
|
||||
// write and wait to see ping back
|
||||
const self = this
|
||||
function next () {
|
||||
const start = new Date()
|
||||
const buf = rnd(PING_LENGTH)
|
||||
self.shake.write(buf)
|
||||
self.shake.read(PING_LENGTH, (err, bufBack) => {
|
||||
const end = new Date()
|
||||
if (err || !buf.equals(bufBack)) {
|
||||
const err = new Error('Received wrong ping ack')
|
||||
return self.emit('error', err)
|
||||
}
|
||||
|
||||
self.emit('ping', end - start)
|
||||
|
||||
if (self._stopped) {
|
||||
return
|
||||
}
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
stop () {
|
||||
if (this._stopped || !this.shake) {
|
||||
return
|
||||
}
|
||||
|
||||
this._stopped = true
|
||||
|
||||
pull(
|
||||
empty(),
|
||||
this.shake.rest()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Ping
|
13
src/ping/util.js
Normal file
13
src/ping/util.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const crypto = require('libp2p-crypto')
|
||||
const constants = require('./constants')
|
||||
|
||||
exports = module.exports
|
||||
|
||||
exports.rnd = (length) => {
|
||||
if (!length) {
|
||||
length = constants.PING_LENGTH
|
||||
}
|
||||
return crypto.randomBytes(length)
|
||||
}
|
67
src/pnet/README.md
Normal file
67
src/pnet/README.md
Normal file
@ -0,0 +1,67 @@
|
||||
js-libp2p-pnet
|
||||
==================
|
||||
|
||||
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-pnet.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Usage](#usage)
|
||||
- [Examples](#examples)
|
||||
- [Private Shared Keys (PSK)](#private-shared-keys)
|
||||
- [PSK Generation](#psk-generation)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const Protector = require('libp2p-pnet')
|
||||
const protector = new Protector(swarmKeyBuffer)
|
||||
const privateConnection = protector.protect(myPublicConnection, (err) => { })
|
||||
```
|
||||
|
||||
### Examples
|
||||
[Private Networks with IPFS](./examples/pnet-ipfs)
|
||||
|
||||
### Private Shared Keys
|
||||
|
||||
Private Shared Keys are expected to be in the following format:
|
||||
|
||||
```
|
||||
/key/swarm/psk/1.0.0/
|
||||
/base16/
|
||||
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
|
||||
```
|
||||
|
||||
### PSK Generation
|
||||
|
||||
A utility method has been created to generate a key for your private network. You can
|
||||
use one of the methods below to generate your key.
|
||||
|
||||
#### From libp2p-pnet
|
||||
|
||||
If you have libp2p-pnet locally, you can run the following from the projects root.
|
||||
|
||||
```sh
|
||||
node ./key-generator.js > swarm.key
|
||||
```
|
||||
|
||||
#### From a module using libp2p
|
||||
|
||||
If you have a module locally that depends on libp2p-pnet, you can run the following from
|
||||
that project, assuming the node_modules are installed.
|
||||
|
||||
```sh
|
||||
node -e "require('libp2p-pnet').generate(process.stdout)" > swarm.key
|
||||
```
|
||||
|
||||
#### Programmatically
|
||||
|
||||
```js
|
||||
const writeKey = require('libp2p-pnet').generate
|
||||
const swarmKey = Buffer.alloc(95)
|
||||
writeKey(swarmKey)
|
||||
fs.writeFileSync('swarm.key', swarmKey)
|
||||
```
|
78
src/pnet/crypto.js
Normal file
78
src/pnet/crypto.js
Normal file
@ -0,0 +1,78 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const Errors = require('./errors')
|
||||
const xsalsa20 = require('xsalsa20')
|
||||
const KEY_LENGTH = require('./key-generator').KEY_LENGTH
|
||||
|
||||
const log = debug('libp2p:pnet')
|
||||
log.trace = debug('libp2p:pnet:trace')
|
||||
log.error = debug('libp2p:pnet:err')
|
||||
|
||||
/**
|
||||
* 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 {*} a through iterable
|
||||
*/
|
||||
module.exports.createBoxStream = (nonce, psk) => {
|
||||
const xor = xsalsa20(nonce, psk)
|
||||
return (source) => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a stream iterable to decrypt messages in a private network
|
||||
*
|
||||
* @param {Buffer} nonce The nonce of the remote peer
|
||||
* @param {Buffer} psk The private shared key to use in decryption
|
||||
* @returns {*} a through iterable
|
||||
*/
|
||||
module.exports.createUnboxStream = (nonce, psk) => {
|
||||
return (source) => (async function * () {
|
||||
const xor = xsalsa20(nonce, psk)
|
||||
log.trace('Decryption enabled')
|
||||
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.from(xor.update(chunk.slice()))
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the version 1 psk from the given Buffer
|
||||
*
|
||||
* @param {Buffer} pskBuffer
|
||||
* @throws {INVALID_PSK}
|
||||
* @returns {Object} The PSK metadata (tag, codecName, psk)
|
||||
*/
|
||||
module.exports.decodeV1PSK = (pskBuffer) => {
|
||||
try {
|
||||
// This should pull from multibase/multicodec to allow for
|
||||
// more encoding flexibility. Ideally we'd consume the codecs
|
||||
// from the buffer line by line to evaluate the next line
|
||||
// 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()
|
||||
const codec = metadata.shift()
|
||||
const psk = Buffer.from(metadata.shift(), 'hex')
|
||||
|
||||
if (psk.byteLength !== KEY_LENGTH) {
|
||||
throw new Error(Errors.INVALID_PSK)
|
||||
}
|
||||
|
||||
return {
|
||||
tag: pskTag,
|
||||
codecName: codec,
|
||||
psk: psk
|
||||
}
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
throw new Error(Errors.INVALID_PSK)
|
||||
}
|
||||
}
|
7
src/pnet/errors.js
Normal file
7
src/pnet/errors.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
module.exports.INVALID_PEER = 'Not a valid peer connection'
|
||||
module.exports.INVALID_PSK = 'Your private shared key is invalid'
|
||||
module.exports.NO_LOCAL_ID = 'No local private key provided'
|
||||
module.exports.NO_HANDSHAKE_CONNECTION = 'No connection for the handshake provided'
|
||||
module.exports.STREAM_ENDED = 'Stream ended prematurely'
|
75
src/pnet/index.js
Normal file
75
src/pnet/index.js
Normal file
@ -0,0 +1,75 @@
|
||||
'use strict'
|
||||
|
||||
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 {
|
||||
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')
|
||||
|
||||
/**
|
||||
* Takes a Private Shared Key (psk) and provides a `protect` method
|
||||
* for wrapping existing connections in a private encryption stream
|
||||
*/
|
||||
class Protector {
|
||||
/**
|
||||
* @param {Buffer} keyBuffer The private shared key buffer
|
||||
* @constructor
|
||||
*/
|
||||
constructor (keyBuffer) {
|
||||
const decodedPSK = decodeV1PSK(keyBuffer)
|
||||
this.psk = decodedPSK.psk
|
||||
this.tag = decodedPSK.tag
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a given Connection and creates a private encryption stream
|
||||
* between its two peers from the PSK the Protector instance was
|
||||
* created with.
|
||||
*
|
||||
* @param {Connection} connection The connection to protect
|
||||
* @returns {*} A protected duplex iterable
|
||||
*/
|
||||
async protect (connection) {
|
||||
assert(connection, Errors.NO_HANDSHAKE_CONNECTION)
|
||||
|
||||
// Exchange nonces
|
||||
log('protecting the connection')
|
||||
const localNonce = crypto.randomBytes(NONCE_LENGTH)
|
||||
|
||||
const shake = handshake(connection)
|
||||
shake.write(localNonce)
|
||||
|
||||
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 internal
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Protector
|
||||
module.exports.errors = Errors
|
||||
module.exports.generate = require('./key-generator')
|
22
src/pnet/key-generator.js
Normal file
22
src/pnet/key-generator.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const crypto = require('crypto')
|
||||
const KEY_LENGTH = 32
|
||||
|
||||
/**
|
||||
* Generates a PSK that can be used in a libp2p-pnet private network
|
||||
* @param {Writer} writer An object containing a `write` method
|
||||
* @returns {void}
|
||||
*/
|
||||
function generate (writer) {
|
||||
const psk = crypto.randomBytes(KEY_LENGTH).toString('hex')
|
||||
writer.write('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
|
||||
}
|
||||
|
||||
module.exports = generate
|
||||
module.exports.NONCE_LENGTH = 24
|
||||
module.exports.KEY_LENGTH = KEY_LENGTH
|
||||
|
||||
if (require.main === module) {
|
||||
generate(process.stdout)
|
||||
}
|
132
src/pubsub.js
132
src/pubsub.js
@ -1,100 +1,104 @@
|
||||
'use strict'
|
||||
|
||||
const nextTick = require('async/nextTick')
|
||||
const { messages, codes } = require('./errors')
|
||||
const FloodSub = require('libp2p-floodsub')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { messages, codes } = require('./errors')
|
||||
|
||||
module.exports = (node) => {
|
||||
const floodSub = new FloodSub(node)
|
||||
|
||||
node._floodSub = floodSub
|
||||
module.exports = (node, Pubsub, config) => {
|
||||
const pubsub = new Pubsub(node.peerInfo, node.registrar, config)
|
||||
|
||||
return {
|
||||
subscribe: (topic, options, handler, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = handler
|
||||
handler = options
|
||||
options = {}
|
||||
/**
|
||||
* Subscribe the given handler to a pubsub topic
|
||||
* @param {string} topic
|
||||
* @param {function} handler The handler to subscribe
|
||||
* @returns {void}
|
||||
*/
|
||||
subscribe: (topic, handler) => {
|
||||
if (!node.isStarted() && !pubsub.started) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
|
||||
if (!node.isStarted() && !floodSub.started) {
|
||||
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
|
||||
if (pubsub.listenerCount(topic) === 0) {
|
||||
pubsub.subscribe(topic)
|
||||
}
|
||||
|
||||
function subscribe (cb) {
|
||||
if (floodSub.listenerCount(topic) === 0) {
|
||||
floodSub.subscribe(topic)
|
||||
}
|
||||
|
||||
floodSub.on(topic, handler)
|
||||
nextTick(cb)
|
||||
}
|
||||
|
||||
subscribe(callback)
|
||||
pubsub.on(topic, handler)
|
||||
},
|
||||
|
||||
unsubscribe: (topic, handler, callback) => {
|
||||
if (!node.isStarted() && !floodSub.started) {
|
||||
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
|
||||
/**
|
||||
* Unsubscribes from a pubsub topic
|
||||
* @param {string} topic
|
||||
* @param {function} [handler] The handler to unsubscribe from
|
||||
*/
|
||||
unsubscribe: (topic, handler) => {
|
||||
if (!node.isStarted() && !pubsub.started) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
if (!handler && !callback) {
|
||||
floodSub.removeAllListeners(topic)
|
||||
|
||||
if (!handler) {
|
||||
pubsub.removeAllListeners(topic)
|
||||
} else {
|
||||
floodSub.removeListener(topic, handler)
|
||||
pubsub.removeListener(topic, handler)
|
||||
}
|
||||
|
||||
if (floodSub.listenerCount(topic) === 0) {
|
||||
floodSub.unsubscribe(topic)
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
nextTick(() => callback())
|
||||
if (pubsub.listenerCount(topic) === 0) {
|
||||
pubsub.unsubscribe(topic)
|
||||
}
|
||||
},
|
||||
|
||||
publish: (topic, data, callback) => {
|
||||
if (!node.isStarted() && !floodSub.started) {
|
||||
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
|
||||
/**
|
||||
* Publish messages to the given topics.
|
||||
* @param {Array<string>|string} topic
|
||||
* @param {Buffer} data
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
publish: (topic, data) => {
|
||||
if (!node.isStarted() && !pubsub.started) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(data)) {
|
||||
return nextTick(callback, errCode(new Error('data must be a Buffer'), 'ERR_DATA_IS_NOT_A_BUFFER'))
|
||||
try {
|
||||
data = Buffer.from(data)
|
||||
} catch (err) {
|
||||
throw errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID')
|
||||
}
|
||||
|
||||
floodSub.publish(topic, data, callback)
|
||||
return pubsub.publish(topic, data)
|
||||
},
|
||||
|
||||
ls: (callback) => {
|
||||
if (!node.isStarted() && !floodSub.started) {
|
||||
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
|
||||
/**
|
||||
* Get a list of topics the node is subscribed to.
|
||||
* @returns {Array<String>} topics
|
||||
*/
|
||||
getTopics: () => {
|
||||
if (!node.isStarted() && !pubsub.started) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
|
||||
const subscriptions = Array.from(floodSub.subscriptions)
|
||||
|
||||
nextTick(() => callback(null, subscriptions))
|
||||
return pubsub.getTopics()
|
||||
},
|
||||
|
||||
peers: (topic, callback) => {
|
||||
if (!node.isStarted() && !floodSub.started) {
|
||||
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
|
||||
/**
|
||||
* Get a list of the peer-ids that are subscribed to one topic.
|
||||
* @param {string} topic
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
getPeersSubscribed: (topic) => {
|
||||
if (!node.isStarted() && !pubsub.started) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||
}
|
||||
|
||||
if (typeof topic === 'function') {
|
||||
callback = topic
|
||||
topic = null
|
||||
}
|
||||
|
||||
const peers = Array.from(floodSub.peers.values())
|
||||
.filter((peer) => topic ? peer.topics.has(topic) : true)
|
||||
.map((peer) => peer.info.id.toB58String())
|
||||
|
||||
nextTick(() => callback(null, peers))
|
||||
return pubsub.getPeersSubscribed(topic)
|
||||
},
|
||||
|
||||
setMaxListeners (n) {
|
||||
return floodSub.setMaxListeners(n)
|
||||
}
|
||||
return pubsub.setMaxListeners(n)
|
||||
},
|
||||
|
||||
_pubsub: pubsub,
|
||||
|
||||
start: () => pubsub.start(),
|
||||
|
||||
stop: () => pubsub.stop()
|
||||
}
|
||||
}
|
||||
|
138
src/registrar.js
Normal file
138
src/registrar.js
Normal file
@ -0,0 +1,138 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:peer-store')
|
||||
log.error = debug('libp2p:peer-store:error')
|
||||
|
||||
const Topology = require('libp2p-interfaces/src/topology')
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
/**
|
||||
* Responsible for notifying registered protocols of events in the network.
|
||||
*/
|
||||
class Registrar {
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {PeerStore} props.peerStore
|
||||
* @constructor
|
||||
*/
|
||||
constructor ({ peerStore }) {
|
||||
this.peerStore = peerStore
|
||||
|
||||
/**
|
||||
* Map of connections per peer
|
||||
* TODO: this should be handled by connectionManager
|
||||
* @type {Map<string, Array<conn>>}
|
||||
*/
|
||||
this.connections = new Map()
|
||||
|
||||
/**
|
||||
* Map of topologies
|
||||
*
|
||||
* @type {Map<string, object>}
|
||||
*/
|
||||
this.topologies = new Map()
|
||||
|
||||
this._handle = undefined
|
||||
}
|
||||
|
||||
get handle () {
|
||||
return this._handle
|
||||
}
|
||||
|
||||
set handle (handle) {
|
||||
this._handle = handle
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new connected peer to the record
|
||||
* TODO: this should live in the ConnectionManager
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {Connection} conn
|
||||
* @returns {void}
|
||||
*/
|
||||
onConnect (peerInfo, conn) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')
|
||||
|
||||
const id = peerInfo.id.toB58String()
|
||||
const storedConn = this.connections.get(id)
|
||||
|
||||
if (storedConn) {
|
||||
storedConn.push(conn)
|
||||
} else {
|
||||
this.connections.set(id, [conn])
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a disconnected peer from the record
|
||||
* TODO: this should live in the ConnectionManager
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {Connection} connection
|
||||
* @param {Error} [error]
|
||||
* @returns {void}
|
||||
*/
|
||||
onDisconnect (peerInfo, connection, error) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
const id = peerInfo.id.toB58String()
|
||||
let storedConn = this.connections.get(id)
|
||||
|
||||
if (storedConn && storedConn.length > 1) {
|
||||
storedConn = storedConn.filter((conn) => conn.id === connection.id)
|
||||
} else if (storedConn) {
|
||||
for (const [, topology] of this.topologies) {
|
||||
topology.disconnect(peerInfo, error)
|
||||
}
|
||||
|
||||
this.connections.delete(peerInfo.id.toB58String())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a connection with a peer.
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @returns {Connection}
|
||||
*/
|
||||
getConnection (peerInfo) {
|
||||
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
|
||||
|
||||
// TODO: what should we return
|
||||
return this.connections.get(peerInfo.id.toB58String())[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* Register handlers for a set of multicodecs given
|
||||
* @param {Topology} topology protocol topology
|
||||
* @return {string} registrar identifier
|
||||
*/
|
||||
register (topology) {
|
||||
assert(
|
||||
Topology.isTopology(topology),
|
||||
'topology must be an instance of interfaces/topology')
|
||||
|
||||
// Create topology
|
||||
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
|
||||
|
||||
this.topologies.set(id, topology)
|
||||
|
||||
// Set registrar
|
||||
topology.registrar = this
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister topology.
|
||||
* @param {string} id registrar identifier
|
||||
* @return {boolean} unregistered successfully
|
||||
*/
|
||||
unregister (id) {
|
||||
return this.topologies.delete(id)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Registrar
|
423
src/switch/README.md
Normal file
423
src/switch/README.md
Normal file
@ -0,0 +1,423 @@
|
||||
libp2p-switch JavaScript implementation
|
||||
======================================
|
||||
|
||||
> libp2p-switch is a dialer machine, it leverages the multiple libp2p transports, stream muxers, crypto channels and other connection upgrades to dial to peers in the libp2p network. It also supports Protocol Multiplexing through a multicodec and multistream-select handshake.
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-switch.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [Create a libp2p switch](#create-a-libp2p-switch)
|
||||
- [API](#api)
|
||||
- [`switch.connection`](#switchconnection)
|
||||
- [`switch.dial(peer, protocol, callback)`](#switchdialpeer-protocol-callback)
|
||||
- [`switch.dialFSM(peer, protocol, callback)`](#switchdialfsmpeer-protocol-callback)
|
||||
- [`switch.handle(protocol, handlerFunc, matchFunc)`](#switchhandleprotocol-handlerfunc-matchfunc)
|
||||
- [`switch.hangUp(peer, callback)`](#switchhanguppeer-callback)
|
||||
- [`switch.start(callback)`](#switchstartcallback)
|
||||
- [`switch.stop(callback)`](#switchstopcallback)
|
||||
- [`switch.stats`](#stats-api)
|
||||
- [`switch.unhandle(protocol)`](#switchunhandleprotocol)
|
||||
- [Internal Transports API](#internal-transports-api)
|
||||
- [Design Notes](#design-notes)
|
||||
- [Multitransport](#multitransport)
|
||||
- [Connection upgrades](#connection-upgrades)
|
||||
- [Identify](#identify)
|
||||
- [Notes](#notes)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
> npm install libp2p-switch --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Create a libp2p Switch
|
||||
|
||||
```JavaScript
|
||||
const switch = require('libp2p-switch')
|
||||
|
||||
const sw = new switch(peerInfo , peerBook [, options])
|
||||
```
|
||||
|
||||
If defined, `options` should be an object with the following keys and respective values:
|
||||
|
||||
- `denyTTL`: - number of ms a peer should not be dialable to after it errors. Each successive deny will increase the TTL from the base value. Defaults to 5 minutes
|
||||
- `denyAttempts`: - number of times a peer can be denied before they are permanently denied. Defaults to 5.
|
||||
- `maxParallelDials`: - number of concurrent dials the switch should allow. Defaults to `100`
|
||||
- `maxColdCalls`: - number of queued cold calls that are allowed. Defaults to `50`
|
||||
- `dialTimeout`: - number of ms a dial to a peer should be allowed to run. Defaults to `30000` (30 seconds)
|
||||
- `stats`: an object with the following keys and respective values:
|
||||
- `maxOldPeersRetention`: maximum old peers retention. For when peers disconnect and keeping the stats around in case they reconnect. Defaults to `100`.
|
||||
- `computeThrottleMaxQueueSize`: maximum queue size to perform stats computation throttling. Defaults to `1000`.
|
||||
- `computeThrottleTimeout`: Throttle timeout, in miliseconds. Defaults to `2000`,
|
||||
- `movingAverageIntervals`: Array containin the intervals, in miliseconds, for which moving averages are calculated. Defaults to:
|
||||
|
||||
```js
|
||||
[
|
||||
60 * 1000, // 1 minute
|
||||
5 * 60 * 1000, // 5 minutes
|
||||
15 * 60 * 1000 // 15 minutes
|
||||
]
|
||||
```
|
||||
|
||||
### Private Networks
|
||||
|
||||
libp2p-switch supports private networking. In order to enabled private networks, the `switch.protector` must be
|
||||
set and must contain a `protect` method. You can see an example of this in the [private network
|
||||
tests]([./test/pnet.node.js]).
|
||||
|
||||
## API
|
||||
|
||||
- peerInfo is a [PeerInfo](https://github.com/libp2p/js-peer-info) object that has the peer information.
|
||||
- peerBook is a [PeerBook](https://github.com/libp2p/js-peer-book) object that stores all the known peers.
|
||||
|
||||
### `switch.connection`
|
||||
|
||||
##### `switch.connection.addUpgrade()`
|
||||
|
||||
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) specification.
|
||||
|
||||
> **WIP**
|
||||
|
||||
##### `switch.connection.addStreamMuxer(muxer)`
|
||||
|
||||
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/libp2p/interface-stream-muxer) spec.
|
||||
|
||||
- `muxer`
|
||||
|
||||
##### `switch.connection.reuse()`
|
||||
|
||||
Enable the identify protocol.
|
||||
|
||||
##### `switch.connection.crypto([tag, encrypt])`
|
||||
|
||||
Enable a specified crypto protocol. By default no encryption is used, aka `plaintext`. If called with no arguments it resets to use `plaintext`.
|
||||
|
||||
You can use for example [libp2p-secio](https://github.com/libp2p/js-libp2p-secio) like this
|
||||
|
||||
```js
|
||||
const secio = require('libp2p-secio')
|
||||
switch.connection.crypto(secio.tag, secio.encrypt)
|
||||
```
|
||||
|
||||
##### `switch.connection.enableCircuitRelay(options, callback)`
|
||||
|
||||
Enable circuit relaying.
|
||||
|
||||
- `options`
|
||||
- enabled - activates relay dialing and listening functionality
|
||||
- hop - an object with two properties
|
||||
- enabled - enables circuit relaying
|
||||
- active - is it an active or passive relay (default false)
|
||||
- `callback`
|
||||
|
||||
### `switch.dial(peer, protocol, callback)`
|
||||
|
||||
dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point where we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, then do nothing.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
- `protocol`
|
||||
- `callback`
|
||||
|
||||
### `switch.dialFSM(peer, protocol, callback)`
|
||||
|
||||
works like dial, but calls back with a [Connection State Machine](#connection-state-machine)
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0') to be used
|
||||
- `callback`: Function with signature `function (err, connFSM) {}` where `connFSM` is a [Connection State Machine](#connection-state-machine)
|
||||
|
||||
#### Connection State Machine
|
||||
Connection state machines emit a number of events that can be used to determine the current state of the connection
|
||||
and to received the underlying connection that can be used to transfer data.
|
||||
|
||||
### `switch.dialer.connect(peer, options, callback)`
|
||||
|
||||
a low priority dial to the provided peer. Calls to `dial` and `dialFSM` will take priority. This should be used when an application only wishes to establish connections to new peers, such as during peer discovery when there is a low peer count. Currently, anything greater than the HIGH_PRIORITY (10) will be placed into the cold call queue, and anything less than or equal to the HIGH_PRIORITY will be added to the normal queue.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
- `options`: Optional
|
||||
- `options.priority`: Number of the priority of the dial, defaults to 20.
|
||||
- `options.useFSM`: Boolean of whether or not to callback with a [Connection State Machine](#connection-state-machine)
|
||||
- `callback`: Function with signature `function (err, connFSM) {}` where `connFSM` is a [Connection State Machine](#connection-state-machine)
|
||||
|
||||
##### Events
|
||||
- `error`: emitted whenever a fatal error occurs with the connection; the error will be emitted.
|
||||
- `error:upgrade_failed`: emitted whenever the connection fails to upgrade with a muxer, this is not fatal.
|
||||
- `error:connection_attempt_failed`: emitted whenever a dial attempt fails for a given transport. An array of errors is emitted.
|
||||
- `connection`: emitted whenever a useable connection has been established; the underlying [Connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) will be emitted.
|
||||
- `close`: emitted when the connection has closed.
|
||||
|
||||
### `switch.handle(protocol, handlerFunc, matchFunc)`
|
||||
|
||||
Handle a new protocol.
|
||||
|
||||
- `protocol`
|
||||
- `handlerFunc` - function called when we receive a dial on `protocol. Signature must be `function (protocol, conn) {}`
|
||||
- `matchFunc` - matchFunc for multistream-select
|
||||
|
||||
### `switch.hangUp(peer, callback)`
|
||||
|
||||
Hang up the muxed connection we have with the peer.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
- `callback`
|
||||
|
||||
### `switch.on('error', (err) => {})`
|
||||
|
||||
Emitted when the switch encounters an error.
|
||||
|
||||
- `err`: instance of [Error][]
|
||||
|
||||
### `switch.on('peer-mux-established', (peer) => {})`
|
||||
|
||||
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just established a muxed connection with.
|
||||
|
||||
### `switch.on('peer-mux-closed', (peer) => {})`
|
||||
|
||||
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection with.
|
||||
|
||||
### `switch.on('connection:start', (peer) => {})`
|
||||
This will be triggered anytime a new connection is created.
|
||||
|
||||
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with.
|
||||
|
||||
### `switch.on('connection:end', (peer) => {})`
|
||||
This will be triggered anytime an existing connection, regardless of state, is removed from the switch's internal connection tracking.
|
||||
|
||||
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a connection with.
|
||||
|
||||
### `switch.on('start', () => {})`
|
||||
|
||||
Emitted when the switch has successfully started.
|
||||
|
||||
### `switch.on('stop', () => {})`
|
||||
|
||||
Emitted when the switch has successfully stopped.
|
||||
|
||||
### `switch.start(callback)`
|
||||
|
||||
Start listening on all added transports that are available on the current `peerInfo`.
|
||||
|
||||
### `switch.stop(callback)`
|
||||
|
||||
Close all the listeners and muxers.
|
||||
|
||||
- `callback`
|
||||
|
||||
### Stats API
|
||||
|
||||
##### `switch.stats.emit('update')`
|
||||
|
||||
Every time any stat value changes, this object emits an `update` event.
|
||||
|
||||
#### Global stats
|
||||
|
||||
##### `switch.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
|
||||
|
||||
##### `switch.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` miliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-transport stats
|
||||
|
||||
##### `switch.stats.transports()`
|
||||
|
||||
Returns an array containing the tags (string) for each observed transport.
|
||||
|
||||
##### `switch.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
|
||||
|
||||
##### `switch.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` miliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-protocol stats
|
||||
|
||||
##### `switch.stats.protocols()`
|
||||
|
||||
Returns an array containing the tags (string) for each observed protocol.
|
||||
|
||||
##### `switch.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
|
||||
|
||||
|
||||
##### `switch.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` miliseconds).
|
||||
|
||||
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
|
||||
|
||||
#### Per-peer stats
|
||||
|
||||
##### `switch.stats.peers()`
|
||||
|
||||
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
|
||||
|
||||
##### `switch.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
|
||||
|
||||
|
||||
##### `switch.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` miliseconds).
|
||||
|
||||
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 miliseconds.
|
||||
|
||||
### `switch.unhandle(protocol)`
|
||||
|
||||
Unhandle a protocol.
|
||||
|
||||
- `protocol`
|
||||
|
||||
### Internal Transports API
|
||||
|
||||
##### `switch.transport.add(key, transport, options)`
|
||||
|
||||
libp2p-switch expects transports that implement [interface-transport](https://github.com/libp2p/interface-transport). For example [libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp).
|
||||
|
||||
- `key` - the transport identifier.
|
||||
- `transport` -
|
||||
- `options` -
|
||||
|
||||
##### `switch.transport.dial(key, multiaddrs, callback)`
|
||||
|
||||
Dial to a peer on a specific transport.
|
||||
|
||||
- `key`
|
||||
- `multiaddrs`
|
||||
- `callback`
|
||||
|
||||
##### `switch.transport.listen(key, options, handler, callback)`
|
||||
|
||||
Set a transport to start listening mode.
|
||||
|
||||
- `key`
|
||||
- `options`
|
||||
- `handler`
|
||||
- `callback`
|
||||
|
||||
##### `switch.transport.close(key, callback)`
|
||||
|
||||
Close the listeners of a given transport.
|
||||
|
||||
- `key`
|
||||
- `callback`
|
||||
|
||||
## Design Notes
|
||||
|
||||
### Multitransport
|
||||
|
||||
libp2p is designed to support multiple transports at the same time. While peers are identified by their ID (which are generated from their public keys), the addresses of each pair may vary, depending the device where they are being run or the network in which they are accessible through.
|
||||
|
||||
In order for a transport to be supported, it has to follow the [interface-transport](https://github.com/libp2p/interface-transport) spec.
|
||||
|
||||
### Connection upgrades
|
||||
|
||||
Each connection in libp2p follows the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) spec. This design decision enables libp2p to have upgradable transports.
|
||||
|
||||
We think of `upgrade` as a very important notion when we are talking about connections, we can see mechanisms like: stream multiplexing, congestion control, encrypted channels, multipath, simulcast, etc, as `upgrades` to a connection. A connection can be a simple and with no guarantees, drop a packet on the network with a destination thing, a transport in the other hand can be a connection and or a set of different upgrades that are mounted on top of each other, giving extra functionality to that connection and therefore `upgrading` it.
|
||||
|
||||
Types of upgrades to a connection:
|
||||
|
||||
- encrypted channel (with TLS for e.g)
|
||||
- congestion flow (some transports don't have it by default)
|
||||
- multipath (open several connections and abstract it as a single connection)
|
||||
- simulcast (still really thinking this one through, it might be interesting to send a packet through different connections under some hard network circumstances)
|
||||
- stream-muxer - this a special case, because once we upgrade a connection to a stream-muxer, we can open more streams (multiplex them) on a single stream, also enabling us to reuse the underlying dialed transport
|
||||
|
||||
We also want to enable flexibility when it comes to upgrading a connection, for example, we might that all dialed transports pass through the encrypted channel upgrade, but not the congestion flow, specially when a transport might have already some underlying properties (UDP vs TCP vs WebRTC vs every other transport protocol)
|
||||
|
||||
### Identify
|
||||
|
||||
Identify is a protocol that switchs mounts on top of itself, to identify the connections between any two peers. E.g:
|
||||
|
||||
- a) peer A dials a conn to peer B
|
||||
- b) that conn gets upgraded to a stream multiplexer that both peers agree
|
||||
- c) peer B executes de identify protocol
|
||||
- d) peer B now can open streams to peer A, knowing which is the
|
||||
identity of peer A
|
||||
|
||||
In addition to this, we also share the "observed addresses" by the other peer, which is extremely useful information for different kinds of network topologies.
|
||||
|
||||
### Notes
|
||||
|
||||
To avoid the confusion between connection, stream, transport, and other names that represent an abstraction of data flow between two points, we use terms as:
|
||||
|
||||
- connection - something that implements the transversal expectations of a stream between two peers, including the benefits of using a stream plus having a way to do half duplex, full duplex
|
||||
- transport - something that as a dial/listen interface and return objs that implement a connection interface
|
||||
|
||||
### 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.
|
126
src/switch/connection/base.js
Normal file
126
src/switch/connection/base.js
Normal file
@ -0,0 +1,126 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const debug = require('debug')
|
||||
const withIs = require('class-is')
|
||||
|
||||
class BaseConnection extends EventEmitter {
|
||||
constructor ({ _switch, name }) {
|
||||
super()
|
||||
|
||||
this.switch = _switch
|
||||
this.ourPeerInfo = this.switch._peerInfo
|
||||
this.log = debug(`libp2p:conn:${name}`)
|
||||
this.log.error = debug(`libp2p:conn:${name}:error`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into its disconnecting flow
|
||||
*
|
||||
* @param {Error} err Will be emitted if provided
|
||||
* @returns {void}
|
||||
*/
|
||||
close (err) {
|
||||
if (this._state._state === 'DISCONNECTING') return
|
||||
this.log('closing connection to %s', this.theirB58Id)
|
||||
if (err && this._events.error) {
|
||||
this.emit('error', err)
|
||||
}
|
||||
this._state('disconnect')
|
||||
}
|
||||
|
||||
emit (eventName, ...args) {
|
||||
if (eventName === 'error' && !this._events.error) {
|
||||
this.log.error(...args)
|
||||
} else {
|
||||
super.emit(eventName, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current state of the connection
|
||||
*
|
||||
* @returns {string} The current state of the connection
|
||||
*/
|
||||
getState () {
|
||||
return this._state._state
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into encrypting mode
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
encrypt () {
|
||||
this._state('encrypt')
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into privatizing mode
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
protect () {
|
||||
this._state('privatize')
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into muxing mode
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
upgrade () {
|
||||
this._state('upgrade')
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for disconnected.
|
||||
*
|
||||
* @fires BaseConnection#close
|
||||
* @returns {void}
|
||||
*/
|
||||
_onDisconnected () {
|
||||
this.switch.connection.remove(this)
|
||||
this.log('disconnected from %s', this.theirB58Id)
|
||||
this.emit('close')
|
||||
this.removeAllListeners()
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for privatized
|
||||
*
|
||||
* @fires BaseConnection#private
|
||||
* @returns {void}
|
||||
*/
|
||||
_onPrivatized () {
|
||||
this.emit('private', this.conn)
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps this.conn with the Switch.protector for private connections
|
||||
*
|
||||
* @private
|
||||
* @fires ConnectionFSM#error
|
||||
* @returns {void}
|
||||
*/
|
||||
_onPrivatizing () {
|
||||
if (!this.switch.protector) {
|
||||
return this._state('done')
|
||||
}
|
||||
|
||||
this.conn = this.switch.protector.protect(this.conn, (err) => {
|
||||
if (err) {
|
||||
return this.close(err)
|
||||
}
|
||||
|
||||
this.log('successfully privatized conn to %s', this.theirB58Id)
|
||||
this.conn.setPeerInfo(this.theirPeerInfo)
|
||||
this._state('done')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = withIs(BaseConnection, {
|
||||
className: 'BaseConnection',
|
||||
symbolName: 'libp2p-switch/BaseConnection'
|
||||
})
|
47
src/switch/connection/handler.js
Normal file
47
src/switch/connection/handler.js
Normal file
@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const IncomingConnection = require('./incoming')
|
||||
const observeConn = require('../observe-connection')
|
||||
|
||||
function listener (_switch) {
|
||||
const log = debug('libp2p:switch:listener')
|
||||
|
||||
/**
|
||||
* Takes a transport key and returns a connection handler function
|
||||
*
|
||||
* @param {string} transportKey The key of the transport to handle connections for
|
||||
* @param {function} handler A custom handler to use
|
||||
* @returns {function(Connection)} A connection handler function
|
||||
*/
|
||||
return function (transportKey, handler) {
|
||||
/**
|
||||
* Takes a base connection and manages listening behavior
|
||||
*
|
||||
* @param {Connection} conn The connection to manage
|
||||
* @returns {void}
|
||||
*/
|
||||
return function (conn) {
|
||||
log('received incoming connection for transport %s', transportKey)
|
||||
conn.getPeerInfo((_, peerInfo) => {
|
||||
// Add a transport level observer, if needed
|
||||
const connection = transportKey ? observeConn(transportKey, null, conn, _switch.observer) : conn
|
||||
const connFSM = new IncomingConnection({ connection, _switch, transportKey, peerInfo })
|
||||
|
||||
connFSM.once('error', (err) => log(err))
|
||||
connFSM.once('private', (_conn) => {
|
||||
// Use the custom handler, if it was provided
|
||||
if (handler) {
|
||||
return handler(_conn)
|
||||
}
|
||||
connFSM.encrypt()
|
||||
})
|
||||
connFSM.once('encrypted', () => connFSM.upgrade())
|
||||
|
||||
connFSM.protect()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = listener
|
115
src/switch/connection/incoming.js
Normal file
115
src/switch/connection/incoming.js
Normal file
@ -0,0 +1,115 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const multistream = require('multistream-select')
|
||||
const withIs = require('class-is')
|
||||
|
||||
const BaseConnection = require('./base')
|
||||
|
||||
class IncomingConnectionFSM extends BaseConnection {
|
||||
constructor ({ connection, _switch, transportKey, peerInfo }) {
|
||||
super({
|
||||
_switch,
|
||||
name: `inc:${_switch._peerInfo.id.toB58String().slice(0, 8)}`
|
||||
})
|
||||
this.conn = connection
|
||||
this.theirPeerInfo = peerInfo || null
|
||||
this.theirB58Id = this.theirPeerInfo ? this.theirPeerInfo.id.toB58String() : null
|
||||
this.ourPeerInfo = this.switch._peerInfo
|
||||
this.transportKey = transportKey
|
||||
this.protocolMuxer = this.switch.protocolMuxer(this.transportKey)
|
||||
this.msListener = new multistream.Listener()
|
||||
|
||||
this._state = FSM('DIALED', {
|
||||
DISCONNECTED: {
|
||||
disconnect: 'DISCONNECTED'
|
||||
},
|
||||
DIALED: { // Base connection to peer established
|
||||
privatize: 'PRIVATIZING',
|
||||
encrypt: 'ENCRYPTING'
|
||||
},
|
||||
PRIVATIZING: { // Protecting the base connection
|
||||
done: 'PRIVATIZED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
PRIVATIZED: { // Base connection is protected
|
||||
encrypt: 'ENCRYPTING'
|
||||
},
|
||||
ENCRYPTING: { // Encrypting the base connection
|
||||
done: 'ENCRYPTED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
ENCRYPTED: { // Upgrading could not happen, the connection is encrypted and waiting
|
||||
upgrade: 'UPGRADING',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
UPGRADING: { // Attempting to upgrade the connection with muxers
|
||||
done: 'MUXED'
|
||||
},
|
||||
MUXED: {
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
DISCONNECTING: { // Shutting down the connection
|
||||
done: 'DISCONNECTED'
|
||||
}
|
||||
})
|
||||
|
||||
this._state.on('DISCONNECTED', () => this._onDisconnected())
|
||||
this._state.on('PRIVATIZING', () => this._onPrivatizing())
|
||||
this._state.on('PRIVATIZED', () => this._onPrivatized())
|
||||
this._state.on('ENCRYPTING', () => this._onEncrypting())
|
||||
this._state.on('ENCRYPTED', () => {
|
||||
this.log('successfully encrypted connection to %s', this.theirB58Id || 'unknown peer')
|
||||
this.emit('encrypted', this.conn)
|
||||
})
|
||||
this._state.on('UPGRADING', () => this._onUpgrading())
|
||||
this._state.on('MUXED', () => {
|
||||
this.log('successfully muxed connection to %s', this.theirB58Id || 'unknown peer')
|
||||
this.emit('muxed', this.conn)
|
||||
})
|
||||
this._state.on('DISCONNECTING', () => {
|
||||
this._state('done')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to encrypt `this.conn` with the Switch's crypto.
|
||||
*
|
||||
* @private
|
||||
* @fires IncomingConnectionFSM#error
|
||||
* @returns {void}
|
||||
*/
|
||||
_onEncrypting () {
|
||||
this.log('encrypting connection via %s', this.switch.crypto.tag)
|
||||
|
||||
this.msListener.addHandler(this.switch.crypto.tag, (protocol, _conn) => {
|
||||
this.conn = this.switch.crypto.encrypt(this.ourPeerInfo.id, _conn, undefined, (err) => {
|
||||
if (err) {
|
||||
return this.close(err)
|
||||
}
|
||||
this.conn.getPeerInfo((_, peerInfo) => {
|
||||
this.theirPeerInfo = peerInfo
|
||||
this._state('done')
|
||||
})
|
||||
})
|
||||
}, null)
|
||||
|
||||
// Start handling the connection
|
||||
this.msListener.handle(this.conn, (err) => {
|
||||
if (err) {
|
||||
this.emit('crypto handshaking failed', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_onUpgrading () {
|
||||
this.log('adding the protocol muxer to the connection')
|
||||
this.protocolMuxer(this.conn, this.msListener)
|
||||
this._state('done')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = withIs(IncomingConnectionFSM, {
|
||||
className: 'IncomingConnectionFSM',
|
||||
symbolName: 'libp2p-switch/IncomingConnectionFSM'
|
||||
})
|
498
src/switch/connection/index.js
Normal file
498
src/switch/connection/index.js
Normal file
@ -0,0 +1,498 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const Circuit = require('../../circuit')
|
||||
const multistream = require('multistream-select')
|
||||
const withIs = require('class-is')
|
||||
const BaseConnection = require('./base')
|
||||
const parallel = require('async/parallel')
|
||||
const nextTick = require('async/nextTick')
|
||||
const identify = require('../../identify')
|
||||
const errCode = require('err-code')
|
||||
const { msHandle, msSelect, identifyDialer } = require('../utils')
|
||||
|
||||
const observeConnection = require('../observe-connection')
|
||||
const {
|
||||
CONNECTION_FAILED,
|
||||
DIAL_SELF,
|
||||
INVALID_STATE_TRANSITION,
|
||||
NO_TRANSPORTS_REGISTERED,
|
||||
maybeUnexpectedEnd
|
||||
} = require('../errors')
|
||||
|
||||
/**
|
||||
* @typedef {Object} ConnectionOptions
|
||||
* @property {Switch} _switch Our switch instance
|
||||
* @property {PeerInfo} peerInfo The PeerInfo of the peer to dial
|
||||
* @property {Muxer} muxer Optional - A muxed connection
|
||||
* @property {Connection} conn Optional - The base connection
|
||||
* @property {string} type Optional - identify the connection as incoming or outgoing. Defaults to out.
|
||||
*/
|
||||
|
||||
/**
|
||||
* ConnectionFSM handles the complex logic of managing a connection
|
||||
* between peers. ConnectionFSM is internally composed of a state machine
|
||||
* to help improve the usability and debuggability of connections. The
|
||||
* state machine also helps to improve the ability to handle dial backoff,
|
||||
* coalescing dials and dial locks.
|
||||
*/
|
||||
class ConnectionFSM extends BaseConnection {
|
||||
/**
|
||||
* @param {ConnectionOptions} connectionOptions
|
||||
* @constructor
|
||||
*/
|
||||
constructor ({ _switch, peerInfo, muxer, conn, type = 'out' }) {
|
||||
super({
|
||||
_switch,
|
||||
name: `${type}:${_switch._peerInfo.id.toB58String().slice(0, 8)}`
|
||||
})
|
||||
|
||||
this.theirPeerInfo = peerInfo
|
||||
this.theirB58Id = this.theirPeerInfo.id.toB58String()
|
||||
|
||||
this.conn = conn // The base connection
|
||||
this.muxer = muxer // The upgraded/muxed connection
|
||||
|
||||
let startState = 'DISCONNECTED'
|
||||
if (this.muxer) {
|
||||
startState = 'MUXED'
|
||||
}
|
||||
|
||||
this._state = FSM(startState, {
|
||||
DISCONNECTED: { // No active connections exist for the peer
|
||||
dial: 'DIALING',
|
||||
disconnect: 'DISCONNECTED',
|
||||
done: 'DISCONNECTED'
|
||||
},
|
||||
DIALING: { // Creating an initial connection
|
||||
abort: 'ABORTED',
|
||||
// emit events for different transport dials?
|
||||
done: 'DIALED',
|
||||
error: 'ERRORED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
DIALED: { // Base connection to peer established
|
||||
encrypt: 'ENCRYPTING',
|
||||
privatize: 'PRIVATIZING'
|
||||
},
|
||||
PRIVATIZING: { // Protecting the base connection
|
||||
done: 'PRIVATIZED',
|
||||
abort: 'ABORTED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
PRIVATIZED: { // Base connection is protected
|
||||
encrypt: 'ENCRYPTING'
|
||||
},
|
||||
ENCRYPTING: { // Encrypting the base connection
|
||||
done: 'ENCRYPTED',
|
||||
error: 'ERRORED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
ENCRYPTED: { // Upgrading could not happen, the connection is encrypted and waiting
|
||||
upgrade: 'UPGRADING',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
UPGRADING: { // Attempting to upgrade the connection with muxers
|
||||
stop: 'CONNECTED', // If we cannot mux, stop upgrading
|
||||
done: 'MUXED',
|
||||
error: 'ERRORED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
MUXED: {
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
CONNECTED: { // A non muxed connection is established
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
DISCONNECTING: { // Shutting down the connection
|
||||
done: 'DISCONNECTED',
|
||||
disconnect: 'DISCONNECTING'
|
||||
},
|
||||
ABORTED: { }, // A severe event occurred
|
||||
ERRORED: { // An error occurred, but future dials may be allowed
|
||||
disconnect: 'DISCONNECTING' // There could be multiple options here, but this is a likely action
|
||||
}
|
||||
})
|
||||
|
||||
this._state.on('DISCONNECTED', () => this._onDisconnected())
|
||||
this._state.on('DIALING', () => this._onDialing())
|
||||
this._state.on('DIALED', () => this._onDialed())
|
||||
this._state.on('PRIVATIZING', () => this._onPrivatizing())
|
||||
this._state.on('PRIVATIZED', () => this._onPrivatized())
|
||||
this._state.on('ENCRYPTING', () => this._onEncrypting())
|
||||
this._state.on('ENCRYPTED', () => {
|
||||
this.log('successfully encrypted connection to %s', this.theirB58Id)
|
||||
this.emit('encrypted', this.conn)
|
||||
})
|
||||
this._state.on('UPGRADING', () => this._onUpgrading())
|
||||
this._state.on('MUXED', () => {
|
||||
this.log('successfully muxed connection to %s', this.theirB58Id)
|
||||
delete this.switch.conns[this.theirB58Id]
|
||||
this.emit('muxed', this.muxer)
|
||||
})
|
||||
this._state.on('CONNECTED', () => {
|
||||
this.log('unmuxed connection opened to %s', this.theirB58Id)
|
||||
this.emit('unmuxed', this.conn)
|
||||
})
|
||||
this._state.on('DISCONNECTING', () => this._onDisconnecting())
|
||||
this._state.on('ABORTED', () => this._onAborted())
|
||||
this._state.on('ERRORED', () => this._onErrored())
|
||||
this._state.on('error', (err) => this._onStateError(err))
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into dialing mode
|
||||
*
|
||||
* @fires ConnectionFSM#Error May emit a DIAL_SELF error
|
||||
* @returns {void}
|
||||
*/
|
||||
dial () {
|
||||
if (this.theirB58Id === this.ourPeerInfo.id.toB58String()) {
|
||||
return this.emit('error', DIAL_SELF())
|
||||
} else if (this.getState() === 'DIALING') {
|
||||
return this.log('attempted to dial while already dialing, ignoring')
|
||||
}
|
||||
|
||||
this._state('dial')
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a handshake for the given protocol
|
||||
*
|
||||
* @param {string} protocol The protocol to negotiate
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
shake (protocol, callback) {
|
||||
// If there is no protocol set yet, don't perform the handshake
|
||||
if (!protocol) {
|
||||
return callback(null, null)
|
||||
}
|
||||
|
||||
if (this.muxer && this.muxer.newStream) {
|
||||
return this.muxer.newStream((err, stream) => {
|
||||
if (err) {
|
||||
return callback(err, null)
|
||||
}
|
||||
|
||||
this.log('created new stream to %s', this.theirB58Id)
|
||||
this._protocolHandshake(protocol, stream, callback)
|
||||
})
|
||||
}
|
||||
|
||||
this._protocolHandshake(protocol, this.conn, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the state into muxing mode
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
upgrade () {
|
||||
this._state('upgrade')
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for dialing. Transitions state when successful.
|
||||
*
|
||||
* @private
|
||||
* @fires ConnectionFSM#error
|
||||
* @returns {void}
|
||||
*/
|
||||
_onDialing () {
|
||||
this.log('dialing %s', this.theirB58Id)
|
||||
|
||||
if (!this.switch.hasTransports()) {
|
||||
return this.close(NO_TRANSPORTS_REGISTERED())
|
||||
}
|
||||
|
||||
const tKeys = this.switch.availableTransports(this.theirPeerInfo)
|
||||
|
||||
const circuitEnabled = Boolean(this.switch.transports[Circuit.tag])
|
||||
|
||||
if (circuitEnabled && !tKeys.includes(Circuit.tag)) {
|
||||
tKeys.push(Circuit.tag)
|
||||
}
|
||||
|
||||
const nextTransport = (key) => {
|
||||
const transport = key
|
||||
if (!transport) {
|
||||
if (!circuitEnabled) {
|
||||
return this.close(
|
||||
CONNECTION_FAILED(`Circuit not enabled and all transports failed to dial peer ${this.theirB58Id}!`)
|
||||
)
|
||||
}
|
||||
|
||||
return this.close(
|
||||
CONNECTION_FAILED(`No available transports to dial peer ${this.theirB58Id}!`)
|
||||
)
|
||||
}
|
||||
|
||||
if (transport === Circuit.tag) {
|
||||
this.theirPeerInfo.multiaddrs.add(`/p2p-circuit/p2p/${this.theirB58Id}`)
|
||||
}
|
||||
|
||||
this.log('dialing transport %s', transport)
|
||||
this.switch.transport.dial(transport, this.theirPeerInfo, (errors, _conn) => {
|
||||
if (errors) {
|
||||
this.emit('error:connection_attempt_failed', errors)
|
||||
this.log(errors)
|
||||
return nextTransport(tKeys.shift())
|
||||
}
|
||||
|
||||
this.conn = observeConnection(transport, null, _conn, this.switch.observer)
|
||||
this._state('done')
|
||||
})
|
||||
}
|
||||
|
||||
nextTransport(tKeys.shift())
|
||||
}
|
||||
|
||||
/**
|
||||
* Once a connection has been successfully dialed, the connection
|
||||
* will be privatized or encrypted depending on the presence of the
|
||||
* Switch.protector.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
_onDialed () {
|
||||
this.log('successfully dialed %s', this.theirB58Id)
|
||||
|
||||
this.emit('connected', this.conn)
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for disconnecting. Handles any needed cleanup
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
_onDisconnecting () {
|
||||
this.log('disconnecting from %s', this.theirB58Id, Boolean(this.muxer))
|
||||
|
||||
delete this.switch.conns[this.theirB58Id]
|
||||
|
||||
const tasks = []
|
||||
|
||||
// Clean up stored connections
|
||||
if (this.muxer) {
|
||||
tasks.push((cb) => {
|
||||
this.muxer.end(() => {
|
||||
delete this.muxer
|
||||
cb()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// If we have the base connection, abort it
|
||||
// Ignore abort errors, since we're closing
|
||||
if (this.conn) {
|
||||
try {
|
||||
this.conn.source.abort()
|
||||
} catch (_) { }
|
||||
delete this.conn
|
||||
}
|
||||
|
||||
parallel(tasks, () => {
|
||||
this._state('done')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to encrypt `this.conn` with the Switch's crypto.
|
||||
*
|
||||
* @private
|
||||
* @fires ConnectionFSM#error
|
||||
* @returns {void}
|
||||
*/
|
||||
_onEncrypting () {
|
||||
const msDialer = new multistream.Dialer()
|
||||
msDialer.handle(this.conn, (err) => {
|
||||
if (err) {
|
||||
return this.close(maybeUnexpectedEnd(err))
|
||||
}
|
||||
|
||||
this.log('selecting crypto %s to %s', this.switch.crypto.tag, this.theirB58Id)
|
||||
|
||||
msDialer.select(this.switch.crypto.tag, (err, _conn) => {
|
||||
if (err) {
|
||||
return this.close(maybeUnexpectedEnd(err))
|
||||
}
|
||||
|
||||
const observedConn = observeConnection(null, this.switch.crypto.tag, _conn, this.switch.observer)
|
||||
const encryptedConn = this.switch.crypto.encrypt(this.ourPeerInfo.id, observedConn, this.theirPeerInfo.id, (err) => {
|
||||
if (err) {
|
||||
return this.close(err)
|
||||
}
|
||||
|
||||
this.conn = encryptedConn
|
||||
this.conn.setPeerInfo(this.theirPeerInfo)
|
||||
this._state('done')
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over each Muxer on the Switch and attempts to upgrade
|
||||
* the given `connection`. Successful muxed connections will be stored
|
||||
* on the Switch.muxedConns with `b58Id` as their key for future reference.
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_onUpgrading () {
|
||||
const muxers = Object.keys(this.switch.muxers)
|
||||
this.log('upgrading connection to %s', this.theirB58Id)
|
||||
|
||||
if (muxers.length === 0) {
|
||||
return this._state('stop')
|
||||
}
|
||||
|
||||
const msDialer = new multistream.Dialer()
|
||||
msDialer.handle(this.conn, (err) => {
|
||||
if (err) {
|
||||
return this._didUpgrade(err)
|
||||
}
|
||||
|
||||
// 1. try to handshake in one of the muxers available
|
||||
// 2. if succeeds
|
||||
// - add the muxedConn to the list of muxedConns
|
||||
// - add incomming new streams to connHandler
|
||||
const nextMuxer = (key) => {
|
||||
this.log('selecting %s', key)
|
||||
msDialer.select(key, (err, _conn) => {
|
||||
if (err) {
|
||||
if (muxers.length === 0) {
|
||||
return this._didUpgrade(err)
|
||||
}
|
||||
|
||||
return nextMuxer(muxers.shift())
|
||||
}
|
||||
|
||||
// observe muxed connections
|
||||
const conn = observeConnection(null, key, _conn, this.switch.observer)
|
||||
|
||||
this.muxer = this.switch.muxers[key].dialer(conn)
|
||||
|
||||
this.muxer.once('close', () => {
|
||||
this.close()
|
||||
})
|
||||
|
||||
// For incoming streams, in case identify is on
|
||||
this.muxer.on('stream', (conn) => {
|
||||
this.log('new stream created via muxer to %s', this.theirB58Id)
|
||||
conn.setPeerInfo(this.theirPeerInfo)
|
||||
this.switch.protocolMuxer(null)(conn)
|
||||
})
|
||||
|
||||
this._didUpgrade(null)
|
||||
|
||||
// Run identify on the connection
|
||||
if (this.switch.identify) {
|
||||
this._identify((err, results) => {
|
||||
if (err) {
|
||||
return this.close(err)
|
||||
}
|
||||
this.theirPeerInfo = this.switch._peerBook.put(results.peerInfo)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
nextMuxer(muxers.shift())
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the identify protocol on the connection
|
||||
* @private
|
||||
* @param {function(error, { PeerInfo })} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
_identify (callback) {
|
||||
if (!this.muxer) {
|
||||
return nextTick(callback, errCode('The connection was already closed', 'ERR_CONNECTION_CLOSED'))
|
||||
}
|
||||
this.muxer.newStream(async (err, conn) => {
|
||||
if (err) return callback(err)
|
||||
const ms = new multistream.Dialer()
|
||||
let results
|
||||
try {
|
||||
await msHandle(ms, conn)
|
||||
const msConn = await msSelect(ms, identify.multicodec)
|
||||
results = await identifyDialer(msConn, this.theirPeerInfo)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
callback(null, results)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyses the given error, if it exists, to determine where the state machine
|
||||
* needs to go.
|
||||
*
|
||||
* @param {Error} err
|
||||
* @returns {void}
|
||||
*/
|
||||
_didUpgrade (err) {
|
||||
if (err) {
|
||||
this.log('Error upgrading connection:', err)
|
||||
this.switch.conns[this.theirB58Id] = this
|
||||
this.emit('error:upgrade_failed', err)
|
||||
// Cant upgrade, hold the encrypted connection
|
||||
return this._state('stop')
|
||||
}
|
||||
|
||||
// move the state machine forward
|
||||
this._state('done')
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the protocol handshake for the given protocol
|
||||
* over the given connection. The resulting error or connection
|
||||
* will be returned via the callback.
|
||||
*
|
||||
* @private
|
||||
* @param {string} protocol
|
||||
* @param {Connection} connection
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
_protocolHandshake (protocol, connection, callback) {
|
||||
const msDialer = new multistream.Dialer()
|
||||
msDialer.handle(connection, (err) => {
|
||||
if (err) {
|
||||
return callback(err, null)
|
||||
}
|
||||
|
||||
msDialer.select(protocol, (err, _conn) => {
|
||||
if (err) {
|
||||
this.log('could not perform protocol handshake:', err)
|
||||
return callback(err, null)
|
||||
}
|
||||
|
||||
const conn = observeConnection(null, protocol, _conn, this.switch.observer)
|
||||
this.log('successfully performed handshake of %s to %s', protocol, this.theirB58Id)
|
||||
this.emit('connection', conn)
|
||||
callback(null, conn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for state transition errors
|
||||
*
|
||||
* @param {Error} err
|
||||
* @returns {void}
|
||||
*/
|
||||
_onStateError (err) {
|
||||
this.emit('error', INVALID_STATE_TRANSITION(err))
|
||||
this.log(err)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = withIs(ConnectionFSM, {
|
||||
className: 'ConnectionFSM',
|
||||
symbolName: 'libp2p-switch/ConnectionFSM'
|
||||
})
|
289
src/switch/connection/manager.js
Normal file
289
src/switch/connection/manager.js
Normal file
@ -0,0 +1,289 @@
|
||||
'use strict'
|
||||
|
||||
const identify = require('../../identify')
|
||||
const multistream = require('multistream-select')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch:conn-manager')
|
||||
const once = require('once')
|
||||
const ConnectionFSM = require('../connection')
|
||||
const { msHandle, msSelect, identifyDialer } = require('../utils')
|
||||
|
||||
const Circuit = require('../../circuit')
|
||||
|
||||
const plaintext = require('../plaintext')
|
||||
|
||||
/**
|
||||
* Contains methods for binding handlers to the Switch
|
||||
* in order to better manage its connections.
|
||||
*/
|
||||
class ConnectionManager {
|
||||
constructor (_switch) {
|
||||
this.switch = _switch
|
||||
this.connections = {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the connection for tracking if it's not already added
|
||||
* @private
|
||||
* @param {ConnectionFSM} connection
|
||||
* @returns {void}
|
||||
*/
|
||||
add (connection) {
|
||||
this.connections[connection.theirB58Id] = this.connections[connection.theirB58Id] || []
|
||||
// Only add it if it's not there
|
||||
if (!this.get(connection)) {
|
||||
this.connections[connection.theirB58Id].push(connection)
|
||||
this.switch.emit('connection:start', connection.theirPeerInfo)
|
||||
if (connection.getState() === 'MUXED') {
|
||||
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
|
||||
// Clear the denylist of the peer
|
||||
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
|
||||
} else {
|
||||
connection.once('muxed', () => {
|
||||
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
|
||||
// Clear the denylist of the peer
|
||||
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the connection from the list if it exists
|
||||
* @private
|
||||
* @param {ConnectionFSM} connection
|
||||
* @returns {ConnectionFSM|null} The found connection or null
|
||||
*/
|
||||
get (connection) {
|
||||
if (!this.connections[connection.theirB58Id]) return null
|
||||
|
||||
for (let i = 0; i < this.connections[connection.theirB58Id].length; i++) {
|
||||
if (this.connections[connection.theirB58Id][i] === connection) {
|
||||
return this.connections[connection.theirB58Id][i]
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a connection associated with the given peer
|
||||
* @private
|
||||
* @param {string} peerId The peers id
|
||||
* @returns {ConnectionFSM|null} The found connection or null
|
||||
*/
|
||||
getOne (peerId) {
|
||||
if (this.connections[peerId]) {
|
||||
// Only return muxed connections
|
||||
for (var i = 0; i < this.connections[peerId].length; i++) {
|
||||
if (this.connections[peerId][i].getState() === 'MUXED') {
|
||||
return this.connections[peerId][i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the connection from tracking
|
||||
* @private
|
||||
* @param {ConnectionFSM} connection The connection to remove
|
||||
* @returns {void}
|
||||
*/
|
||||
remove (connection) {
|
||||
// No record of the peer, disconnect it
|
||||
if (!this.connections[connection.theirB58Id]) {
|
||||
if (connection.theirPeerInfo) {
|
||||
connection.theirPeerInfo.disconnect()
|
||||
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.connections[connection.theirB58Id].length; i++) {
|
||||
if (this.connections[connection.theirB58Id][i] === connection) {
|
||||
this.connections[connection.theirB58Id].splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// The peer is fully disconnected
|
||||
if (this.connections[connection.theirB58Id].length === 0) {
|
||||
delete this.connections[connection.theirB58Id]
|
||||
connection.theirPeerInfo.disconnect()
|
||||
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
|
||||
}
|
||||
|
||||
// A tracked connection was closed, let the world know
|
||||
this.switch.emit('connection:end', connection.theirPeerInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all connections being tracked
|
||||
* @private
|
||||
* @returns {ConnectionFSM[]}
|
||||
*/
|
||||
getAll () {
|
||||
let connections = []
|
||||
for (const conns of Object.values(this.connections)) {
|
||||
connections = [...connections, ...conns]
|
||||
}
|
||||
return connections
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all connections being tracked for a given peer id
|
||||
* @private
|
||||
* @param {string} peerId Stringified peer id
|
||||
* @returns {ConnectionFSM[]}
|
||||
*/
|
||||
getAllById (peerId) {
|
||||
return this.connections[peerId] || []
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener for the given `muxer` and creates a handler for it
|
||||
* leveraging the Switch.protocolMuxer handler factory
|
||||
*
|
||||
* @param {Muxer} muxer
|
||||
* @returns {void}
|
||||
*/
|
||||
addStreamMuxer (muxer) {
|
||||
// for dialing
|
||||
this.switch.muxers[muxer.multicodec] = muxer
|
||||
|
||||
// for listening
|
||||
this.switch.handle(muxer.multicodec, (protocol, conn) => {
|
||||
const muxedConn = muxer.listener(conn)
|
||||
|
||||
muxedConn.on('stream', this.switch.protocolMuxer(null))
|
||||
|
||||
// If identify is enabled
|
||||
// 1. overload getPeerInfo
|
||||
// 2. call getPeerInfo
|
||||
// 3. add this conn to the pool
|
||||
if (this.switch.identify) {
|
||||
// Get the peer info from the crypto exchange
|
||||
conn.getPeerInfo((err, cryptoPI) => {
|
||||
if (err || !cryptoPI) {
|
||||
log('crypto peerInfo wasnt found')
|
||||
}
|
||||
|
||||
// overload peerInfo to use Identify instead
|
||||
conn.getPeerInfo = async (callback) => {
|
||||
const conn = muxedConn.newStream()
|
||||
const ms = new multistream.Dialer()
|
||||
callback = once(callback)
|
||||
|
||||
let results
|
||||
try {
|
||||
await msHandle(ms, conn)
|
||||
const msConn = await msSelect(ms, identify.multicodec)
|
||||
results = await identifyDialer(msConn, cryptoPI)
|
||||
} catch (err) {
|
||||
return muxedConn.end(() => {
|
||||
callback(err, null)
|
||||
})
|
||||
}
|
||||
|
||||
const { peerInfo } = results
|
||||
|
||||
if (peerInfo) {
|
||||
conn.setPeerInfo(peerInfo)
|
||||
}
|
||||
callback(null, peerInfo)
|
||||
}
|
||||
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
/* eslint no-warning-comments: off */
|
||||
if (err) {
|
||||
return log('identify not successful')
|
||||
}
|
||||
const b58Str = peerInfo.id.toB58String()
|
||||
peerInfo = this.switch._peerBook.put(peerInfo)
|
||||
|
||||
const connection = new ConnectionFSM({
|
||||
_switch: this.switch,
|
||||
peerInfo,
|
||||
muxer: muxedConn,
|
||||
conn: conn,
|
||||
type: 'inc'
|
||||
})
|
||||
this.switch.connection.add(connection)
|
||||
|
||||
// Only update if it's not already connected
|
||||
if (!peerInfo.isConnected()) {
|
||||
if (peerInfo.multiaddrs.size > 0) {
|
||||
// with incomming conn and through identify, going to pick one
|
||||
// of the available multiaddrs from the other peer as the one
|
||||
// I'm connected to as we really can't be sure at the moment
|
||||
// TODO add this consideration to the connection abstraction!
|
||||
peerInfo.connect(peerInfo.multiaddrs.toArray()[0])
|
||||
} else {
|
||||
// for the case of websockets in the browser, where peers have
|
||||
// no addr, use just their IPFS id
|
||||
peerInfo.connect(`/ipfs/${b58Str}`)
|
||||
}
|
||||
}
|
||||
|
||||
muxedConn.once('close', () => {
|
||||
connection.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return conn
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the `encrypt` handler for the given `tag` and also sets the
|
||||
* Switch's crypto to passed `encrypt` function
|
||||
*
|
||||
* @param {String} tag
|
||||
* @param {function(PeerID, Connection, PeerId, Callback)} encrypt
|
||||
* @returns {void}
|
||||
*/
|
||||
crypto (tag, encrypt) {
|
||||
if (!tag && !encrypt) {
|
||||
tag = plaintext.tag
|
||||
encrypt = plaintext.encrypt
|
||||
}
|
||||
|
||||
this.switch.crypto = { tag, encrypt }
|
||||
}
|
||||
|
||||
/**
|
||||
* If config.enabled is true, a Circuit relay will be added to the
|
||||
* available Switch transports.
|
||||
*
|
||||
* @param {any} config
|
||||
* @returns {void}
|
||||
*/
|
||||
enableCircuitRelay (config) {
|
||||
config = config || {}
|
||||
|
||||
if (config.enabled) {
|
||||
if (!config.hop) {
|
||||
Object.assign(config, { hop: { enabled: false, active: false } })
|
||||
}
|
||||
|
||||
this.switch.transport.add(Circuit.tag, new Circuit(this.switch, config))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets identify to true on the Switch and performs handshakes
|
||||
* for libp2p-identify leveraging the Switch's muxer.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
reuse () {
|
||||
this.switch.identify = true
|
||||
this.switch.handle(identify.multicodec, (protocol, conn) => {
|
||||
identify.listener(conn, this.switch._peerInfo)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConnectionManager
|
12
src/switch/constants.js
Normal file
12
src/switch/constants.js
Normal file
@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
|
||||
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
|
||||
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
|
||||
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
|
||||
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
|
||||
QUARTER_HOUR: 15 * 60e3,
|
||||
PRIORITY_HIGH: 10,
|
||||
PRIORITY_LOW: 20
|
||||
}
|
119
src/switch/dialer/index.js
Normal file
119
src/switch/dialer/index.js
Normal file
@ -0,0 +1,119 @@
|
||||
'use strict'
|
||||
|
||||
const DialQueueManager = require('./queueManager')
|
||||
const { getPeerInfo } = require('../../get-peer-info')
|
||||
const {
|
||||
DENY_ATTEMPTS,
|
||||
DENY_TTL,
|
||||
MAX_COLD_CALLS,
|
||||
MAX_PARALLEL_DIALS,
|
||||
PRIORITY_HIGH,
|
||||
PRIORITY_LOW
|
||||
} = require('../constants')
|
||||
|
||||
module.exports = function (_switch) {
|
||||
const dialQueueManager = new DialQueueManager(_switch)
|
||||
|
||||
_switch.state.on('STARTED:enter', start)
|
||||
_switch.state.on('STOPPING:enter', stop)
|
||||
|
||||
/**
|
||||
* @param {DialRequest} dialRequest
|
||||
* @returns {void}
|
||||
*/
|
||||
function _dial ({ peerInfo, protocol, options, callback }) {
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = null
|
||||
}
|
||||
|
||||
try {
|
||||
peerInfo = getPeerInfo(peerInfo, _switch._peerBook)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// Add it to the queue, it will automatically get executed
|
||||
dialQueueManager.add({ peerInfo, protocol, options, callback })
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the `DialQueueManager`
|
||||
*
|
||||
* @param {function} callback
|
||||
*/
|
||||
function start (callback) {
|
||||
dialQueueManager.start()
|
||||
callback()
|
||||
}
|
||||
|
||||
/**
|
||||
* Aborts all dials that are queued. This should
|
||||
* only be used when the Switch is being stopped
|
||||
*
|
||||
* @param {function} callback
|
||||
*/
|
||||
function stop (callback) {
|
||||
dialQueueManager.stop()
|
||||
callback()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the denylist for a given peer
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
function clearDenylist (peerInfo) {
|
||||
dialQueueManager.clearDenylist(peerInfo)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to establish a connection to the given `peerInfo` at
|
||||
* a lower priority than a standard dial.
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {object} options
|
||||
* @param {boolean} options.useFSM Whether or not to return a `ConnectionFSM`. Defaults to false.
|
||||
* @param {number} options.priority Lowest priority goes first. Defaults to 20.
|
||||
* @param {function(Error, Connection)} callback
|
||||
*/
|
||||
function connect (peerInfo, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = null
|
||||
}
|
||||
options = { useFSM: false, priority: PRIORITY_LOW, ...options }
|
||||
_dial({ peerInfo, protocol: null, options, callback })
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the dial request to the queue for the given `peerInfo`
|
||||
* The request will be added with a high priority (10).
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, Connection)} callback
|
||||
*/
|
||||
function dial (peerInfo, protocol, callback) {
|
||||
_dial({ peerInfo, protocol, options: { useFSM: false, priority: PRIORITY_HIGH }, callback })
|
||||
}
|
||||
|
||||
/**
|
||||
* Behaves like dial, except it calls back with a ConnectionFSM
|
||||
*
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, ConnectionFSM)} callback
|
||||
*/
|
||||
function dialFSM (peerInfo, protocol, callback) {
|
||||
_dial({ peerInfo, protocol, options: { useFSM: true, priority: PRIORITY_HIGH }, callback })
|
||||
}
|
||||
|
||||
return {
|
||||
connect,
|
||||
dial,
|
||||
dialFSM,
|
||||
clearDenylist,
|
||||
DENY_ATTEMPTS: isNaN(_switch._options.denyAttempts) ? DENY_ATTEMPTS : _switch._options.denyAttempts,
|
||||
DENY_TTL: isNaN(_switch._options.denyTTL) ? DENY_TTL : _switch._options.denyTTL,
|
||||
MAX_COLD_CALLS: isNaN(_switch._options.maxColdCalls) ? MAX_COLD_CALLS : _switch._options.maxColdCalls,
|
||||
MAX_PARALLEL_DIALS: isNaN(_switch._options.maxParallelDials) ? MAX_PARALLEL_DIALS : _switch._options.maxParallelDials
|
||||
}
|
||||
}
|
281
src/switch/dialer/queue.js
Normal file
281
src/switch/dialer/queue.js
Normal file
@ -0,0 +1,281 @@
|
||||
'use strict'
|
||||
|
||||
const ConnectionFSM = require('../connection')
|
||||
const { DIAL_ABORTED, ERR_DENIED } = require('../errors')
|
||||
const nextTick = require('async/nextTick')
|
||||
const once = require('once')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch:dial')
|
||||
log.error = debug('libp2p:switch:dial:error')
|
||||
|
||||
/**
|
||||
* Components required to execute a dial
|
||||
* @typedef {Object} DialRequest
|
||||
* @property {PeerInfo} peerInfo - The peer to dial to
|
||||
* @property {string} [protocol] - The protocol to create a stream for
|
||||
* @property {object} options
|
||||
* @property {boolean} options.useFSM - If `callback` should return a ConnectionFSM
|
||||
* @property {number} options.priority - The priority of the dial
|
||||
* @property {function(Error, Connection|ConnectionFSM)} callback
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} NewConnection
|
||||
* @property {ConnectionFSM} connectionFSM
|
||||
* @property {boolean} didCreate
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attempts to create a new connection or stream (when muxed),
|
||||
* via negotiation of the given `protocol`. If no `protocol` is
|
||||
* provided, no action will be taken and `callback` will be called
|
||||
* immediately with no error or values.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {string} options.protocol
|
||||
* @param {ConnectionFSM} options.connection
|
||||
* @param {function(Error, Connection)} options.callback
|
||||
* @returns {void}
|
||||
*/
|
||||
function createConnectionWithProtocol ({ protocol, connection, callback }) {
|
||||
if (!protocol) {
|
||||
return callback()
|
||||
}
|
||||
connection.shake(protocol, (err, conn) => {
|
||||
if (!conn) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
conn.setPeerInfo(connection.theirPeerInfo)
|
||||
callback(null, conn)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience array wrapper for controlling
|
||||
* a per peer queue
|
||||
*
|
||||
* @returns {Queue}
|
||||
*/
|
||||
class Queue {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {string} peerId
|
||||
* @param {Switch} _switch
|
||||
* @param {function(string)} onStopped Called when the queue stops
|
||||
*/
|
||||
constructor (peerId, _switch, onStopped) {
|
||||
this.id = peerId
|
||||
this.switch = _switch
|
||||
this._queue = []
|
||||
this.denylisted = null
|
||||
this.denylistCount = 0
|
||||
this.isRunning = false
|
||||
this.onStopped = onStopped
|
||||
}
|
||||
|
||||
get length () {
|
||||
return this._queue.length
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the dial request to the queue. The queue is not automatically started
|
||||
* @param {string} protocol
|
||||
* @param {boolean} useFSM If callback should use a ConnectionFSM instead
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
add (protocol, useFSM, callback) {
|
||||
if (!this.isDialAllowed()) {
|
||||
return nextTick(callback, ERR_DENIED())
|
||||
}
|
||||
this._queue.push({ protocol, useFSM, callback })
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not dialing is currently allowed
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isDialAllowed () {
|
||||
if (this.denylisted) {
|
||||
// If the deny ttl has passed, reset it
|
||||
if (Date.now() > this.denylisted) {
|
||||
this.denylisted = null
|
||||
return true
|
||||
}
|
||||
// Dial is not allowed
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the queue. If the queue was started `true` will be returned.
|
||||
* If the queue was already running `false` is returned.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
start () {
|
||||
if (!this.isRunning) {
|
||||
log('starting dial queue to %s', this.id)
|
||||
this.isRunning = true
|
||||
this._run()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the queue
|
||||
*/
|
||||
stop () {
|
||||
if (this.isRunning) {
|
||||
log('stopping dial queue to %s', this.id)
|
||||
this.isRunning = false
|
||||
this.onStopped(this.id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the queue and errors the callback for each dial request
|
||||
*/
|
||||
abort () {
|
||||
while (this.length > 0) {
|
||||
const dial = this._queue.shift()
|
||||
dial.callback(DIAL_ABORTED())
|
||||
}
|
||||
this.stop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the queue as denylisted. The queue will be immediately aborted.
|
||||
* @returns {void}
|
||||
*/
|
||||
denylist () {
|
||||
this.denylistCount++
|
||||
|
||||
if (this.denylistCount >= this.switch.dialer.DENY_ATTEMPTS) {
|
||||
this.denylisted = Infinity
|
||||
return
|
||||
}
|
||||
|
||||
let ttl = this.switch.dialer.DENY_TTL * Math.pow(this.denylistCount, 3)
|
||||
const minTTL = ttl * 0.9
|
||||
const maxTTL = ttl * 1.1
|
||||
|
||||
// Add a random jitter of 20% to the ttl
|
||||
ttl = Math.floor(Math.random() * (maxTTL - minTTL) + minTTL)
|
||||
|
||||
this.denylisted = Date.now() + ttl
|
||||
this.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to find a muxed connection for the given peer. If one
|
||||
* isn't found, a new one will be created.
|
||||
*
|
||||
* Returns an array containing two items. The ConnectionFSM and wether
|
||||
* or not the ConnectionFSM was just created. The latter can be used
|
||||
* to determine dialing needs.
|
||||
*
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @returns {NewConnection}
|
||||
*/
|
||||
_getOrCreateConnection (peerInfo) {
|
||||
let connectionFSM = this.switch.connection.getOne(this.id)
|
||||
let didCreate = false
|
||||
|
||||
if (!connectionFSM) {
|
||||
connectionFSM = new ConnectionFSM({
|
||||
_switch: this.switch,
|
||||
peerInfo,
|
||||
muxer: null,
|
||||
conn: null
|
||||
})
|
||||
|
||||
this.switch.connection.add(connectionFSM)
|
||||
|
||||
// Add control events and start the dialer
|
||||
connectionFSM.once('connected', () => connectionFSM.protect())
|
||||
connectionFSM.once('private', () => connectionFSM.encrypt())
|
||||
connectionFSM.once('encrypted', () => connectionFSM.upgrade())
|
||||
|
||||
didCreate = true
|
||||
}
|
||||
|
||||
return { connectionFSM, didCreate }
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the next dial in the queue for the given peer
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_run () {
|
||||
// If we have no items in the queue or we're stopped, exit
|
||||
if (this.length < 1 || !this.isRunning) {
|
||||
log('stopping the queue for %s', this.id)
|
||||
return this.stop()
|
||||
}
|
||||
|
||||
const next = once(() => {
|
||||
log('starting next dial to %s', this.id)
|
||||
this._run()
|
||||
})
|
||||
|
||||
const peerInfo = this.switch._peerBook.get(this.id)
|
||||
const queuedDial = this._queue.shift()
|
||||
const { connectionFSM, didCreate } = this._getOrCreateConnection(peerInfo)
|
||||
|
||||
// If the dial expects a ConnectionFSM, we can provide that back now
|
||||
if (queuedDial.useFSM) {
|
||||
nextTick(queuedDial.callback, null, connectionFSM)
|
||||
}
|
||||
|
||||
// If we can handshake protocols, get a new stream and call run again
|
||||
if (['MUXED', 'CONNECTED'].includes(connectionFSM.getState())) {
|
||||
queuedDial.connection = connectionFSM
|
||||
createConnectionWithProtocol(queuedDial)
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// If we error, error the queued dial
|
||||
// In the future, it may be desired to error the other queued dials,
|
||||
// depending on the error.
|
||||
connectionFSM.once('error', (err) => {
|
||||
queuedDial.callback(err)
|
||||
// Dont denylist peers we have identified and that we are connected to
|
||||
if (peerInfo.protocols.size > 0 && peerInfo.isConnected()) {
|
||||
return
|
||||
}
|
||||
this.denylist()
|
||||
})
|
||||
|
||||
connectionFSM.once('close', () => {
|
||||
next()
|
||||
})
|
||||
|
||||
// If we're not muxed yet, add listeners
|
||||
connectionFSM.once('muxed', () => {
|
||||
this.denylistCount = 0 // reset denylisting on good connections
|
||||
queuedDial.connection = connectionFSM
|
||||
createConnectionWithProtocol(queuedDial)
|
||||
next()
|
||||
})
|
||||
|
||||
connectionFSM.once('unmuxed', () => {
|
||||
this.denylistCount = 0
|
||||
queuedDial.connection = connectionFSM
|
||||
createConnectionWithProtocol(queuedDial)
|
||||
next()
|
||||
})
|
||||
|
||||
// If we have a new connection, start dialing
|
||||
if (didCreate) {
|
||||
connectionFSM.dial()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Queue
|
220
src/switch/dialer/queueManager.js
Normal file
220
src/switch/dialer/queueManager.js
Normal file
@ -0,0 +1,220 @@
|
||||
'use strict'
|
||||
|
||||
const once = require('once')
|
||||
const Queue = require('./queue')
|
||||
const { DIAL_ABORTED } = require('../errors')
|
||||
const nextTick = require('async/nextTick')
|
||||
const retimer = require('retimer')
|
||||
const { QUARTER_HOUR, PRIORITY_HIGH } = require('../constants')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch:dial:manager')
|
||||
const noop = () => {}
|
||||
|
||||
class DialQueueManager {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {Switch} _switch
|
||||
*/
|
||||
constructor (_switch) {
|
||||
this._queue = new Set()
|
||||
this._coldCallQueue = new Set()
|
||||
this._dialingQueues = new Set()
|
||||
this._queues = {}
|
||||
this.switch = _switch
|
||||
this._cleanInterval = retimer(this._clean.bind(this), QUARTER_HOUR)
|
||||
this.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs through all queues, aborts and removes them if they
|
||||
* are no longer valid. A queue that is denylisted indefinitely,
|
||||
* is considered no longer valid.
|
||||
* @private
|
||||
*/
|
||||
_clean () {
|
||||
const queues = Object.values(this._queues)
|
||||
queues.forEach(dialQueue => {
|
||||
// Clear if the queue has reached max denylist
|
||||
if (dialQueue.denylisted === Infinity) {
|
||||
dialQueue.abort()
|
||||
delete this._queues[dialQueue.id]
|
||||
return
|
||||
}
|
||||
|
||||
// Keep track of denylisted queues
|
||||
if (dialQueue.denylisted) return
|
||||
|
||||
// Clear if peer is no longer active
|
||||
// To avoid reallocating memory, dont delete queues of
|
||||
// connected peers, as these are highly likely to leverage the
|
||||
// queues in the immediate term
|
||||
if (!dialQueue.isRunning && dialQueue.length < 1) {
|
||||
let isConnected = false
|
||||
try {
|
||||
const peerInfo = this.switch._peerBook.get(dialQueue.id)
|
||||
isConnected = Boolean(peerInfo.isConnected())
|
||||
} catch (_) {
|
||||
// If we get an error, that means the peerbook doesnt have the peer
|
||||
}
|
||||
|
||||
if (!isConnected) {
|
||||
dialQueue.abort()
|
||||
delete this._queues[dialQueue.id]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
this._cleanInterval.reschedule(QUARTER_HOUR)
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the `DialQueueManager` to execute dials
|
||||
*/
|
||||
start () {
|
||||
this.isRunning = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over all items in the DialerQueue
|
||||
* and executes there callback with an error.
|
||||
*
|
||||
* This causes the entire DialerQueue to be drained
|
||||
*/
|
||||
stop () {
|
||||
this.isRunning = false
|
||||
// Clear the general queue
|
||||
this._queue.clear()
|
||||
// Clear the cold call queue
|
||||
this._coldCallQueue.clear()
|
||||
|
||||
this._cleanInterval.clear()
|
||||
|
||||
// Abort the individual peer queues
|
||||
const queues = Object.values(this._queues)
|
||||
queues.forEach(dialQueue => {
|
||||
dialQueue.abort()
|
||||
delete this._queues[dialQueue.id]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the `dialRequest` to the queue and ensures queue is running
|
||||
*
|
||||
* @param {DialRequest} dialRequest
|
||||
* @returns {void}
|
||||
*/
|
||||
add ({ peerInfo, protocol, options, callback }) {
|
||||
callback = callback ? once(callback) : noop
|
||||
|
||||
// Add the dial to its respective queue
|
||||
const targetQueue = this.getQueue(peerInfo)
|
||||
|
||||
// Cold Call
|
||||
if (options.priority > PRIORITY_HIGH) {
|
||||
// If we have too many cold calls, abort the dial immediately
|
||||
if (this._coldCallQueue.size >= this.switch.dialer.MAX_COLD_CALLS) {
|
||||
return nextTick(callback, DIAL_ABORTED())
|
||||
}
|
||||
|
||||
if (this._queue.has(targetQueue.id)) {
|
||||
return nextTick(callback, DIAL_ABORTED())
|
||||
}
|
||||
}
|
||||
|
||||
targetQueue.add(protocol, options.useFSM, callback)
|
||||
|
||||
// If we're already connected to the peer, start the queue now
|
||||
// While it might cause queues to go over the max parallel amount,
|
||||
// it avoids denying peers we're already connected to
|
||||
if (peerInfo.isConnected()) {
|
||||
targetQueue.start()
|
||||
return
|
||||
}
|
||||
|
||||
// If dialing is not allowed, abort
|
||||
if (!targetQueue.isDialAllowed()) {
|
||||
return
|
||||
}
|
||||
|
||||
// Add the id to its respective queue set if the queue isn't running
|
||||
if (!targetQueue.isRunning) {
|
||||
if (options.priority <= PRIORITY_HIGH) {
|
||||
this._queue.add(targetQueue.id)
|
||||
this._coldCallQueue.delete(targetQueue.id)
|
||||
// Only add it to the cold queue if it's not in the normal queue
|
||||
} else {
|
||||
this._coldCallQueue.add(targetQueue.id)
|
||||
}
|
||||
}
|
||||
|
||||
this.run()
|
||||
}
|
||||
|
||||
/**
|
||||
* Will execute up to `MAX_PARALLEL_DIALS` dials
|
||||
*/
|
||||
run () {
|
||||
if (!this.isRunning) return
|
||||
|
||||
if (this._dialingQueues.size < this.switch.dialer.MAX_PARALLEL_DIALS) {
|
||||
let nextQueue = { done: true }
|
||||
// Check the queue first and fall back to the cold call queue
|
||||
if (this._queue.size > 0) {
|
||||
nextQueue = this._queue.values().next()
|
||||
this._queue.delete(nextQueue.value)
|
||||
} else if (this._coldCallQueue.size > 0) {
|
||||
nextQueue = this._coldCallQueue.values().next()
|
||||
this._coldCallQueue.delete(nextQueue.value)
|
||||
}
|
||||
|
||||
if (nextQueue.done) {
|
||||
return
|
||||
}
|
||||
|
||||
const targetQueue = this._queues[nextQueue.value]
|
||||
|
||||
if (!targetQueue) {
|
||||
log('missing queue %s, maybe it was aborted?', nextQueue.value)
|
||||
return
|
||||
}
|
||||
|
||||
this._dialingQueues.add(targetQueue.id)
|
||||
targetQueue.start()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will remove the `peerInfo` from the dial denylist
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
clearDenylist (peerInfo) {
|
||||
const queue = this.getQueue(peerInfo)
|
||||
queue.denylisted = null
|
||||
queue.denylistCount = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* A handler for when dialing queues stop. This will trigger
|
||||
* `run()` in order to keep the queue processing.
|
||||
* @private
|
||||
* @param {string} id peer id of the queue that stopped
|
||||
*/
|
||||
_onQueueStopped (id) {
|
||||
this._dialingQueues.delete(id)
|
||||
this.run()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `Queue` for the given `peerInfo`
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @returns {Queue}
|
||||
*/
|
||||
getQueue (peerInfo) {
|
||||
const id = peerInfo.id.toB58String()
|
||||
|
||||
this._queues[id] = this._queues[id] || new Queue(id, this.switch, this._onQueueStopped.bind(this))
|
||||
return this._queues[id]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DialQueueManager
|
20
src/switch/errors.js
Normal file
20
src/switch/errors.js
Normal file
@ -0,0 +1,20 @@
|
||||
'use strict'
|
||||
|
||||
const errCode = require('err-code')
|
||||
|
||||
module.exports = {
|
||||
CONNECTION_FAILED: (err) => errCode(err, 'CONNECTION_FAILED'),
|
||||
DIAL_ABORTED: () => errCode('Dial was aborted', 'DIAL_ABORTED'),
|
||||
ERR_DENIED: () => errCode('Dial is currently denied for this peer', 'ERR_DENIED'),
|
||||
DIAL_SELF: () => errCode('A node cannot dial itself', 'DIAL_SELF'),
|
||||
INVALID_STATE_TRANSITION: (err) => errCode(err, 'INVALID_STATE_TRANSITION'),
|
||||
NO_TRANSPORTS_REGISTERED: () => errCode('No transports registered, dial not possible', 'NO_TRANSPORTS_REGISTERED'),
|
||||
PROTECTOR_REQUIRED: () => errCode('No protector provided with private network enforced', 'PROTECTOR_REQUIRED'),
|
||||
UNEXPECTED_END: () => errCode('Unexpected end of input from reader.', 'UNEXPECTED_END'),
|
||||
maybeUnexpectedEnd: (err) => {
|
||||
if (err === true) {
|
||||
return module.exports.UNEXPECTED_END()
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
274
src/switch/index.js
Normal file
274
src/switch/index.js
Normal file
@ -0,0 +1,274 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const each = require('async/each')
|
||||
const eachSeries = require('async/eachSeries')
|
||||
const series = require('async/series')
|
||||
const Circuit = require('../circuit')
|
||||
const TransportManager = require('./transport')
|
||||
const ConnectionManager = require('./connection/manager')
|
||||
const { getPeerInfo } = require('../get-peer-info')
|
||||
const getDialer = require('./dialer')
|
||||
const connectionHandler = require('./connection/handler')
|
||||
const ProtocolMuxer = require('./protocol-muxer')
|
||||
const plaintext = require('./plaintext')
|
||||
const Observer = require('./observer')
|
||||
const Stats = require('./stats')
|
||||
const assert = require('assert')
|
||||
const Errors = require('./errors')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch')
|
||||
log.error = debug('libp2p:switch:error')
|
||||
|
||||
/**
|
||||
* @fires Switch#stop Triggered when the switch has stopped
|
||||
* @fires Switch#start Triggered when the switch has started
|
||||
* @fires Switch#error Triggered whenever an error occurs
|
||||
*/
|
||||
class Switch extends EventEmitter {
|
||||
constructor (peerInfo, peerBook, options) {
|
||||
super()
|
||||
assert(peerInfo, 'You must provide a `peerInfo`')
|
||||
assert(peerBook, 'You must provide a `peerBook`')
|
||||
|
||||
this._peerInfo = peerInfo
|
||||
this._peerBook = peerBook
|
||||
this._options = options || {}
|
||||
|
||||
this.setMaxListeners(Infinity)
|
||||
// transports --
|
||||
// { key: transport }; e.g { tcp: <tcp> }
|
||||
this.transports = {}
|
||||
|
||||
// connections --
|
||||
// { peerIdB58: { conn: <conn> }}
|
||||
this.conns = {}
|
||||
|
||||
// { protocol: handler }
|
||||
this.protocols = {}
|
||||
|
||||
// { muxerCodec: <muxer> } e.g { '/spdy/0.3.1': spdy }
|
||||
this.muxers = {}
|
||||
|
||||
// is the Identify protocol enabled?
|
||||
this.identify = false
|
||||
|
||||
// Crypto details
|
||||
this.crypto = plaintext
|
||||
|
||||
this.protector = this._options.protector || null
|
||||
|
||||
this.transport = new TransportManager(this)
|
||||
this.connection = new ConnectionManager(this)
|
||||
|
||||
this.observer = Observer(this)
|
||||
this.stats = Stats(this.observer, this._options.stats)
|
||||
this.protocolMuxer = ProtocolMuxer(this.protocols, this.observer)
|
||||
|
||||
// All purpose connection handler for managing incoming connections
|
||||
this._connectionHandler = connectionHandler(this)
|
||||
|
||||
// Setup the internal state
|
||||
this.state = new FSM('STOPPED', {
|
||||
STOPPED: {
|
||||
start: 'STARTING',
|
||||
stop: 'STOPPING' // ensures that any transports that were manually started are stopped
|
||||
},
|
||||
STARTING: {
|
||||
done: 'STARTED',
|
||||
stop: 'STOPPING'
|
||||
},
|
||||
STARTED: {
|
||||
stop: 'STOPPING',
|
||||
start: 'STARTED'
|
||||
},
|
||||
STOPPING: {
|
||||
stop: 'STOPPING',
|
||||
done: 'STOPPED'
|
||||
}
|
||||
})
|
||||
this.state.on('STARTING', () => {
|
||||
log('The switch is starting')
|
||||
this._onStarting()
|
||||
})
|
||||
this.state.on('STOPPING', () => {
|
||||
log('The switch is stopping')
|
||||
this._onStopping()
|
||||
})
|
||||
this.state.on('STARTED', () => {
|
||||
log('The switch has started')
|
||||
this.emit('start')
|
||||
})
|
||||
this.state.on('STOPPED', () => {
|
||||
log('The switch has stopped')
|
||||
this.emit('stop')
|
||||
})
|
||||
this.state.on('error', (err) => {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
})
|
||||
|
||||
// higher level (public) API
|
||||
this.dialer = getDialer(this)
|
||||
this.dial = this.dialer.dial
|
||||
this.dialFSM = this.dialer.dialFSM
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the transports peerInfo has addresses for
|
||||
*
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @returns {Array<Transport>}
|
||||
*/
|
||||
availableTransports (peerInfo) {
|
||||
const myAddrs = peerInfo.multiaddrs.toArray()
|
||||
const myTransports = Object.keys(this.transports)
|
||||
|
||||
// Only listen on transports we actually have addresses for
|
||||
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
|
||||
// push Circuit to be the last proto to be dialed, and alphabetize the others
|
||||
.sort((a, b) => {
|
||||
if (a === Circuit.tag) return 1
|
||||
if (b === Circuit.tag) return -1
|
||||
return a < b ? -1 : 1
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the `handlerFunc` and `matchFunc` to the Switch's protocol
|
||||
* handler list for the given `protocol`. If the `matchFunc` returns
|
||||
* true for a protocol check, the `handlerFunc` will be called.
|
||||
*
|
||||
* @param {string} protocol
|
||||
* @param {function(string, Connection)} handlerFunc
|
||||
* @param {function(string, string, function(Error, boolean))} matchFunc
|
||||
* @returns {void}
|
||||
*/
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this.protocols[protocol] = {
|
||||
handlerFunc: handlerFunc,
|
||||
matchFunc: matchFunc
|
||||
}
|
||||
this._peerInfo.protocols.add(protocol)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given protocol from the Switch's protocol list
|
||||
*
|
||||
* @param {string} protocol
|
||||
* @returns {void}
|
||||
*/
|
||||
unhandle (protocol) {
|
||||
if (this.protocols[protocol]) {
|
||||
delete this.protocols[protocol]
|
||||
}
|
||||
this._peerInfo.protocols.delete(protocol)
|
||||
}
|
||||
|
||||
/**
|
||||
* If a muxed Connection exists for the given peer, it will be closed
|
||||
* and its reference on the Switch will be removed.
|
||||
*
|
||||
* @param {PeerInfo|Multiaddr|PeerId} peer
|
||||
* @param {function()} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
hangUp (peer, callback) {
|
||||
const peerInfo = getPeerInfo(peer, this._peerBook)
|
||||
const key = peerInfo.id.toB58String()
|
||||
const conns = [...this.connection.getAllById(key)]
|
||||
each(conns, (conn, cb) => {
|
||||
conn.once('close', cb)
|
||||
conn.close()
|
||||
}, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the switch has any transports
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasTransports () {
|
||||
const transports = Object.keys(this.transports).filter((t) => t !== Circuit.tag)
|
||||
return transports && transports.length > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a start on the Switch state.
|
||||
*
|
||||
* @param {function} callback deprecated: Listening for the `error` and `start` events are recommended
|
||||
* @returns {void}
|
||||
*/
|
||||
start (callback = () => {}) {
|
||||
// Add once listener for deprecated callback support
|
||||
this.once('start', callback)
|
||||
|
||||
this.state('start')
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a stop on the Switch state.
|
||||
*
|
||||
* @param {function} callback deprecated: Listening for the `error` and `stop` events are recommended
|
||||
* @returns {void}
|
||||
*/
|
||||
stop (callback = () => {}) {
|
||||
// Add once listener for deprecated callback support
|
||||
this.once('stop', callback)
|
||||
|
||||
this.state('stop')
|
||||
}
|
||||
|
||||
/**
|
||||
* A listener that will start any necessary services and listeners
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_onStarting () {
|
||||
this.stats.start()
|
||||
eachSeries(this.availableTransports(this._peerInfo), (ts, cb) => {
|
||||
// Listen on the given transport
|
||||
this.transport.listen(ts, {}, null, cb)
|
||||
}, (err) => {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
return this.state('stop')
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A listener that will turn off all running services and listeners
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_onStopping () {
|
||||
this.stats.stop()
|
||||
series([
|
||||
(cb) => {
|
||||
each(this.transports, (transport, cb) => {
|
||||
each(transport.listeners, (listener, cb) => {
|
||||
listener.close((err) => {
|
||||
if (err) log.error(err)
|
||||
cb()
|
||||
})
|
||||
}, cb)
|
||||
}, cb)
|
||||
},
|
||||
(cb) => each(this.connection.getAll(), (conn, cb) => {
|
||||
conn.once('close', cb)
|
||||
conn.close()
|
||||
}, cb)
|
||||
], (_) => {
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Switch
|
||||
module.exports.errors = Errors
|
88
src/switch/limit-dialer/index.js
Normal file
88
src/switch/limit-dialer/index.js
Normal file
@ -0,0 +1,88 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const debug = require('debug')
|
||||
|
||||
const log = debug('libp2p:switch:dialer')
|
||||
|
||||
const DialQueue = require('./queue')
|
||||
|
||||
/**
|
||||
* Track dials per peer and limited them.
|
||||
*/
|
||||
class LimitDialer {
|
||||
/**
|
||||
* Create a new dialer.
|
||||
*
|
||||
* @param {number} perPeerLimit
|
||||
* @param {number} dialTimeout
|
||||
*/
|
||||
constructor (perPeerLimit, dialTimeout) {
|
||||
log('create: %s peer limit, %s dial timeout', perPeerLimit, dialTimeout)
|
||||
this.perPeerLimit = perPeerLimit
|
||||
this.dialTimeout = dialTimeout
|
||||
this.queues = new Map()
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a list of multiaddrs on the given transport.
|
||||
*
|
||||
* @param {PeerId} peer
|
||||
* @param {SwarmTransport} transport
|
||||
* @param {Array<Multiaddr>} addrs
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialMany (peer, transport, addrs, callback) {
|
||||
log('dialMany:start')
|
||||
// we use a token to track if we want to cancel following dials
|
||||
const token = { cancel: false }
|
||||
|
||||
const errors = []
|
||||
const tasks = addrs.map((m) => {
|
||||
return (cb) => this.dialSingle(peer, transport, m, token, (err, result) => {
|
||||
if (err) {
|
||||
errors.push(err)
|
||||
return cb(err)
|
||||
}
|
||||
return cb(null, result)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (_, result) => {
|
||||
if (result && result.conn) {
|
||||
log('dialMany:success')
|
||||
return callback(null, result)
|
||||
}
|
||||
|
||||
log('dialMany:error')
|
||||
callback(errors)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a single multiaddr on the given transport.
|
||||
*
|
||||
* @param {PeerId} peer
|
||||
* @param {SwarmTransport} transport
|
||||
* @param {Multiaddr} addr
|
||||
* @param {CancelToken} token
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialSingle (peer, transport, addr, token, callback) {
|
||||
const ps = peer.toB58String()
|
||||
log('dialSingle: %s:%s', ps, addr.toString())
|
||||
let q
|
||||
if (this.queues.has(ps)) {
|
||||
q = this.queues.get(ps)
|
||||
} else {
|
||||
q = new DialQueue(this.perPeerLimit, this.dialTimeout)
|
||||
this.queues.set(ps, q)
|
||||
}
|
||||
|
||||
q.push(transport, addr, token, callback)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LimitDialer
|
109
src/switch/limit-dialer/queue.js
Normal file
109
src/switch/limit-dialer/queue.js
Normal file
@ -0,0 +1,109 @@
|
||||
'use strict'
|
||||
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const pull = require('pull-stream/pull')
|
||||
const empty = require('pull-stream/sources/empty')
|
||||
const timeout = require('async/timeout')
|
||||
const queue = require('async/queue')
|
||||
const debug = require('debug')
|
||||
const once = require('once')
|
||||
|
||||
const log = debug('libp2p:switch:dialer:queue')
|
||||
log.error = debug('libp2p:switch:dialer:queue:error')
|
||||
|
||||
/**
|
||||
* Queue up the amount of dials to a given peer.
|
||||
*/
|
||||
class DialQueue {
|
||||
/**
|
||||
* Create a new dial queue.
|
||||
*
|
||||
* @param {number} limit
|
||||
* @param {number} dialTimeout
|
||||
*/
|
||||
constructor (limit, dialTimeout) {
|
||||
this.dialTimeout = dialTimeout
|
||||
|
||||
this.queue = queue((task, cb) => {
|
||||
this._doWork(task.transport, task.addr, task.token, cb)
|
||||
}, limit)
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual work done by the queue.
|
||||
*
|
||||
* @param {SwarmTransport} transport
|
||||
* @param {Multiaddr} addr
|
||||
* @param {CancelToken} token
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
* @private
|
||||
*/
|
||||
_doWork (transport, addr, token, callback) {
|
||||
callback = once(callback)
|
||||
log('work:start')
|
||||
this._dialWithTimeout(transport, addr, (err, conn) => {
|
||||
if (err) {
|
||||
log.error(`${transport.constructor.name}:work`, err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
if (token.cancel) {
|
||||
log('work:cancel')
|
||||
// clean up already done dials
|
||||
pull(empty(), conn)
|
||||
// If we can close the connection, do it
|
||||
if (typeof conn.close === 'function') {
|
||||
return conn.close((_) => callback(null))
|
||||
}
|
||||
return callback(null)
|
||||
}
|
||||
|
||||
// one is enough
|
||||
token.cancel = true
|
||||
|
||||
log('work:success')
|
||||
|
||||
const proxyConn = new Connection()
|
||||
proxyConn.setInnerConn(conn)
|
||||
callback(null, { multiaddr: addr, conn: conn })
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial the given transport, timing out with the set timeout.
|
||||
*
|
||||
* @param {SwarmTransport} transport
|
||||
* @param {Multiaddr} addr
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_dialWithTimeout (transport, addr, callback) {
|
||||
timeout((cb) => {
|
||||
const conn = transport.dial(addr, (err) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
cb(null, conn)
|
||||
})
|
||||
}, this.dialTimeout)(callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new work to the queue.
|
||||
*
|
||||
* @param {SwarmTransport} transport
|
||||
* @param {Multiaddr} addr
|
||||
* @param {CancelToken} token
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
push (transport, addr, token, callback) {
|
||||
this.queue.push({ transport, addr, token }, callback)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DialQueue
|
44
src/switch/observe-connection.js
Normal file
44
src/switch/observe-connection.js
Normal file
@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const pull = require('pull-stream/pull')
|
||||
|
||||
/**
|
||||
* Creates a pull stream to run the given Connection stream through
|
||||
* the given Observer. This provides a way to more easily monitor connections
|
||||
* and their metadata. A new Connection will be returned that contains
|
||||
* has the attached Observer.
|
||||
*
|
||||
* @param {Transport} transport
|
||||
* @param {string} protocol
|
||||
* @param {Connection} connection
|
||||
* @param {Observer} observer
|
||||
* @returns {Connection}
|
||||
*/
|
||||
module.exports = (transport, protocol, connection, observer) => {
|
||||
const peerInfo = new Promise((resolve, reject) => {
|
||||
connection.getPeerInfo((err, peerInfo) => {
|
||||
if (!err && peerInfo) {
|
||||
resolve(peerInfo)
|
||||
return
|
||||
}
|
||||
|
||||
const setPeerInfo = connection.setPeerInfo
|
||||
connection.setPeerInfo = (pi) => {
|
||||
setPeerInfo.call(connection, pi)
|
||||
resolve(pi)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const stream = {
|
||||
source: pull(
|
||||
connection,
|
||||
observer.incoming(transport, protocol, peerInfo)),
|
||||
sink: pull(
|
||||
observer.outgoing(transport, protocol, peerInfo),
|
||||
connection)
|
||||
}
|
||||
|
||||
return new Connection(stream, connection)
|
||||
}
|
48
src/switch/observer.js
Normal file
48
src/switch/observer.js
Normal file
@ -0,0 +1,48 @@
|
||||
'use strict'
|
||||
|
||||
const map = require('pull-stream/throughs/map')
|
||||
const EventEmitter = require('events')
|
||||
|
||||
/**
|
||||
* Takes a Switch and returns an Observer that can be used in conjunction with
|
||||
* observe-connection.js. The returned Observer comes with `incoming` and
|
||||
* `outgoing` properties that can be used in pull streams to emit all metadata
|
||||
* for messages that pass through a Connection.
|
||||
*
|
||||
* @param {Switch} swtch
|
||||
* @returns {EventEmitter}
|
||||
*/
|
||||
module.exports = (swtch) => {
|
||||
const observer = Object.assign(new EventEmitter(), {
|
||||
incoming: observe('in'),
|
||||
outgoing: observe('out')
|
||||
})
|
||||
|
||||
swtch.on('peer-mux-established', (peerInfo) => {
|
||||
observer.emit('peer:connected', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
swtch.on('peer-mux-closed', (peerInfo) => {
|
||||
observer.emit('peer:closed', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
return observer
|
||||
|
||||
function observe (direction) {
|
||||
return (transport, protocol, peerInfo) => {
|
||||
return map((buffer) => {
|
||||
willObserve(peerInfo, transport, protocol, direction, buffer.length)
|
||||
return buffer
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function willObserve (peerInfo, transport, protocol, direction, bufferLength) {
|
||||
peerInfo.then((_peerInfo) => {
|
||||
if (_peerInfo) {
|
||||
const peerId = _peerInfo.id.toB58String()
|
||||
observer.emit('message', peerId, transport, protocol, direction, bufferLength)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
20
src/switch/plaintext.js
Normal file
20
src/switch/plaintext.js
Normal file
@ -0,0 +1,20 @@
|
||||
'use strict'
|
||||
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
/**
|
||||
* An encryption stub in the instance that the default crypto
|
||||
* has not been overriden for the Switch
|
||||
*/
|
||||
module.exports = {
|
||||
tag: '/plaintext/1.0.0',
|
||||
encrypt (myId, conn, remoteId, callback) {
|
||||
if (typeof remoteId === 'function') {
|
||||
callback = remoteId
|
||||
remoteId = undefined
|
||||
}
|
||||
|
||||
setImmediate(() => callback())
|
||||
return conn
|
||||
}
|
||||
}
|
48
src/switch/protocol-muxer.js
Normal file
48
src/switch/protocol-muxer.js
Normal file
@ -0,0 +1,48 @@
|
||||
'use strict'
|
||||
|
||||
const multistream = require('multistream-select')
|
||||
const observeConn = require('./observe-connection')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch:protocol-muxer')
|
||||
log.error = debug('libp2p:switch:protocol-muxer:error')
|
||||
|
||||
module.exports = function protocolMuxer (protocols, observer) {
|
||||
return (transport) => (_parentConn, msListener) => {
|
||||
const ms = msListener || new multistream.Listener()
|
||||
let parentConn
|
||||
|
||||
// Only observe the transport if we have one, and there is not already a listener
|
||||
if (transport && !msListener) {
|
||||
parentConn = observeConn(transport, null, _parentConn, observer)
|
||||
} else {
|
||||
parentConn = _parentConn
|
||||
}
|
||||
|
||||
Object.keys(protocols).forEach((protocol) => {
|
||||
if (!protocol) {
|
||||
return
|
||||
}
|
||||
|
||||
const handler = (protocolName, _conn) => {
|
||||
log('registering handler with protocol %s', protocolName)
|
||||
const protocol = protocols[protocolName]
|
||||
if (protocol) {
|
||||
const handlerFunc = protocol && protocol.handlerFunc
|
||||
if (handlerFunc) {
|
||||
const conn = observeConn(null, protocolName, _conn, observer)
|
||||
handlerFunc(protocol, conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ms.addHandler(protocol, handler, protocols[protocol].matchFunc)
|
||||
})
|
||||
|
||||
ms.handle(parentConn, (err) => {
|
||||
if (err) {
|
||||
log.error('multistream handshake failed', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
150
src/switch/stats/index.js
Normal file
150
src/switch/stats/index.js
Normal file
@ -0,0 +1,150 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
|
||||
const Stat = require('./stat')
|
||||
const OldPeers = require('./old-peers')
|
||||
|
||||
const defaultOptions = {
|
||||
computeThrottleMaxQueueSize: 1000,
|
||||
computeThrottleTimeout: 2000,
|
||||
movingAverageIntervals: [
|
||||
60 * 1000, // 1 minute
|
||||
5 * 60 * 1000, // 5 minutes
|
||||
15 * 60 * 1000 // 15 minutes
|
||||
],
|
||||
maxOldPeersRetention: 50
|
||||
}
|
||||
|
||||
const initialCounters = [
|
||||
'dataReceived',
|
||||
'dataSent'
|
||||
]
|
||||
|
||||
const directionToEvent = {
|
||||
in: 'dataReceived',
|
||||
out: 'dataSent'
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds to message events on the given `observer` to generate stats
|
||||
* based on the Peer, Protocol and Transport used for the message. Stat
|
||||
* events will be emitted via the `update` event.
|
||||
*
|
||||
* @param {Observer} observer
|
||||
* @param {any} _options
|
||||
* @returns {Stats}
|
||||
*/
|
||||
module.exports = (observer, _options) => {
|
||||
const options = Object.assign({}, defaultOptions, _options)
|
||||
const globalStats = new Stat(initialCounters, options)
|
||||
|
||||
const stats = Object.assign(new EventEmitter(), {
|
||||
start: start,
|
||||
stop: stop,
|
||||
global: globalStats,
|
||||
peers: () => Array.from(peerStats.keys()),
|
||||
forPeer: (peerId) => {
|
||||
return peerStats.get(peerId) || oldPeers.get(peerId)
|
||||
},
|
||||
transports: () => Array.from(transportStats.keys()),
|
||||
forTransport: (transport) => transportStats.get(transport),
|
||||
protocols: () => Array.from(protocolStats.keys()),
|
||||
forProtocol: (protocol) => protocolStats.get(protocol)
|
||||
})
|
||||
|
||||
globalStats.on('update', propagateChange)
|
||||
|
||||
const oldPeers = OldPeers(options.maxOldPeersRetention)
|
||||
const peerStats = new Map()
|
||||
const transportStats = new Map()
|
||||
const protocolStats = new Map()
|
||||
|
||||
observer.on('peer:closed', (peerId) => {
|
||||
const peer = peerStats.get(peerId)
|
||||
if (peer) {
|
||||
peer.removeListener('update', propagateChange)
|
||||
peer.stop()
|
||||
peerStats.delete(peerId)
|
||||
oldPeers.set(peerId, peer)
|
||||
}
|
||||
})
|
||||
|
||||
return stats
|
||||
|
||||
function onMessage (peerId, transportTag, protocolTag, direction, bufferLength) {
|
||||
const event = directionToEvent[direction]
|
||||
|
||||
if (transportTag) {
|
||||
// because it has a transport tag, this message is at the global level, so we account this
|
||||
// traffic as global.
|
||||
globalStats.push(event, bufferLength)
|
||||
|
||||
// peer stats
|
||||
let peer = peerStats.get(peerId)
|
||||
if (!peer) {
|
||||
peer = oldPeers.get(peerId)
|
||||
if (peer) {
|
||||
oldPeers.delete(peerId)
|
||||
} else {
|
||||
peer = new Stat(initialCounters, options)
|
||||
}
|
||||
peer.on('update', propagateChange)
|
||||
peer.start()
|
||||
peerStats.set(peerId, peer)
|
||||
}
|
||||
peer.push(event, bufferLength)
|
||||
}
|
||||
|
||||
// transport stats
|
||||
if (transportTag) {
|
||||
let transport = transportStats.get(transportTag)
|
||||
if (!transport) {
|
||||
transport = new Stat(initialCounters, options)
|
||||
transport.on('update', propagateChange)
|
||||
transportStats.set(transportTag, transport)
|
||||
}
|
||||
transport.push(event, bufferLength)
|
||||
}
|
||||
|
||||
// protocol stats
|
||||
if (protocolTag) {
|
||||
let protocol = protocolStats.get(protocolTag)
|
||||
if (!protocol) {
|
||||
protocol = new Stat(initialCounters, options)
|
||||
protocol.on('update', propagateChange)
|
||||
protocolStats.set(protocolTag, protocol)
|
||||
}
|
||||
protocol.push(event, bufferLength)
|
||||
}
|
||||
}
|
||||
|
||||
function start () {
|
||||
observer.on('message', onMessage)
|
||||
|
||||
globalStats.start()
|
||||
|
||||
for (const peerStat of peerStats.values()) {
|
||||
peerStat.start()
|
||||
}
|
||||
for (const transportStat of transportStats.values()) {
|
||||
transportStat.start()
|
||||
}
|
||||
}
|
||||
|
||||
function stop () {
|
||||
observer.removeListener('message', onMessage)
|
||||
globalStats.stop()
|
||||
|
||||
for (const peerStat of peerStats.values()) {
|
||||
peerStat.stop()
|
||||
}
|
||||
for (const transportStat of transportStats.values()) {
|
||||
transportStat.stop()
|
||||
}
|
||||
}
|
||||
|
||||
function propagateChange () {
|
||||
stats.emit('update')
|
||||
}
|
||||
}
|
15
src/switch/stats/old-peers.js
Normal file
15
src/switch/stats/old-peers.js
Normal file
@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const LRU = require('hashlru')
|
||||
|
||||
/**
|
||||
* Creates and returns a Least Recently Used Cache
|
||||
*
|
||||
* @param {Number} maxSize
|
||||
* @returns {LRUCache}
|
||||
*/
|
||||
module.exports = (maxSize) => {
|
||||
const patched = LRU(maxSize)
|
||||
patched.delete = patched.remove
|
||||
return patched
|
||||
}
|
239
src/switch/stats/stat.js
Normal file
239
src/switch/stats/stat.js
Normal file
@ -0,0 +1,239 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const Big = require('bignumber.js')
|
||||
const MovingAverage = require('moving-average')
|
||||
const retimer = require('retimer')
|
||||
|
||||
/**
|
||||
* A queue based manager for stat processing
|
||||
*
|
||||
* @param {Array<string>} initialCounters
|
||||
* @param {any} options
|
||||
*/
|
||||
class Stats extends EventEmitter {
|
||||
constructor (initialCounters, options) {
|
||||
super()
|
||||
|
||||
this._options = options
|
||||
this._queue = []
|
||||
this._stats = {}
|
||||
|
||||
this._frequencyLastTime = Date.now()
|
||||
this._frequencyAccumulators = {}
|
||||
this._movingAverages = {}
|
||||
|
||||
this._update = this._update.bind(this)
|
||||
|
||||
const intervals = this._options.movingAverageIntervals
|
||||
|
||||
for (var i = 0; i < initialCounters.length; i++) {
|
||||
var key = initialCounters[i]
|
||||
this._stats[key] = Big(0)
|
||||
this._movingAverages[key] = {}
|
||||
for (var k = 0; k < intervals.length; k++) {
|
||||
var interval = intervals[k]
|
||||
var ma = this._movingAverages[key][interval] = MovingAverage(interval)
|
||||
ma.push(this._frequencyLastTime, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the internal timer if there are items in the queue. This
|
||||
* should only need to be called if `Stats.stop` was previously called, as
|
||||
* `Stats.push` will also start the processing.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
start () {
|
||||
if (this._queue.length) {
|
||||
this._resetComputeTimeout()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops processing and computing of stats by clearing the internal
|
||||
* timer.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
stop () {
|
||||
if (this._timeout) {
|
||||
this._timeout.clear()
|
||||
this._timeout = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a clone of the current stats.
|
||||
*
|
||||
* @returns {Map<string, Stat>}
|
||||
*/
|
||||
get snapshot () {
|
||||
return Object.assign({}, this._stats)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a clone of the internal movingAverages
|
||||
*
|
||||
* @returns {Array<MovingAverage>}
|
||||
*/
|
||||
get movingAverages () {
|
||||
return Object.assign({}, this._movingAverages)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes the given operation data to the queue, along with the
|
||||
* current Timestamp, then resets the update timer.
|
||||
*
|
||||
* @param {string} counter
|
||||
* @param {number} inc
|
||||
* @returns {void}
|
||||
*/
|
||||
push (counter, inc) {
|
||||
this._queue.push([counter, inc, Date.now()])
|
||||
this._resetComputeTimeout()
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the timeout for triggering updates.
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_resetComputeTimeout () {
|
||||
if (this._timeout) {
|
||||
this._timeout.reschedule(this._nextTimeout())
|
||||
} else {
|
||||
this._timeout = retimer(this._update, this._nextTimeout())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates and returns the timeout for the next update based on
|
||||
* the urgency of the update.
|
||||
*
|
||||
* @private
|
||||
* @returns {number}
|
||||
*/
|
||||
_nextTimeout () {
|
||||
// calculate the need for an update, depending on the queue length
|
||||
const urgency = this._queue.length / this._options.computeThrottleMaxQueueSize
|
||||
const timeout = Math.max(this._options.computeThrottleTimeout * (1 - urgency), 0)
|
||||
return timeout
|
||||
}
|
||||
|
||||
/**
|
||||
* If there are items in the queue, they will will be processed and
|
||||
* the frequency for all items will be updated based on the Timestamp
|
||||
* of the last item in the queue. The `update` event will also be emitted
|
||||
* with the latest stats.
|
||||
*
|
||||
* If there are no items in the queue, no action is taken.
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
_update () {
|
||||
this._timeout = null
|
||||
if (this._queue.length) {
|
||||
let last
|
||||
while (this._queue.length) {
|
||||
const op = last = this._queue.shift()
|
||||
this._applyOp(op)
|
||||
}
|
||||
|
||||
this._updateFrequency(last[2]) // contains timestamp of last op
|
||||
|
||||
this.emit('update', this._stats)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For each key in the stats, the frequncy and moving averages
|
||||
* will be updated via Stats._updateFrequencyFor based on the time
|
||||
* difference between calls to this method.
|
||||
*
|
||||
* @private
|
||||
* @param {Timestamp} latestTime
|
||||
* @returns {void}
|
||||
*/
|
||||
_updateFrequency (latestTime) {
|
||||
const timeDiff = latestTime - this._frequencyLastTime
|
||||
|
||||
Object.keys(this._stats).forEach((key) => {
|
||||
this._updateFrequencyFor(key, timeDiff, latestTime)
|
||||
})
|
||||
|
||||
this._frequencyLastTime = latestTime
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the `movingAverages` for the given `key` and also
|
||||
* resets the `frequencyAccumulator` for the `key`.
|
||||
*
|
||||
* @private
|
||||
* @param {string} key
|
||||
* @param {number} timeDiffMS Time in milliseconds
|
||||
* @param {Timestamp} latestTime Time in ticks
|
||||
* @returns {void}
|
||||
*/
|
||||
_updateFrequencyFor (key, timeDiffMS, latestTime) {
|
||||
const count = this._frequencyAccumulators[key] || 0
|
||||
this._frequencyAccumulators[key] = 0
|
||||
// if `timeDiff` is zero, `hz` becomes Infinity, so we fallback to 1ms
|
||||
const safeTimeDiff = timeDiffMS || 1
|
||||
const hz = (count / safeTimeDiff) * 1000
|
||||
|
||||
let movingAverages = this._movingAverages[key]
|
||||
if (!movingAverages) {
|
||||
movingAverages = this._movingAverages[key] = {}
|
||||
}
|
||||
|
||||
const intervals = this._options.movingAverageIntervals
|
||||
|
||||
for (var i = 0; i < intervals.length; i++) {
|
||||
var movingAverageInterval = intervals[i]
|
||||
var movingAverage = movingAverages[movingAverageInterval]
|
||||
if (!movingAverage) {
|
||||
movingAverage = movingAverages[movingAverageInterval] = MovingAverage(movingAverageInterval)
|
||||
}
|
||||
movingAverage.push(latestTime, hz)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For the given operation, `op`, the stats and `frequencyAccumulator`
|
||||
* will be updated or initialized if they don't already exist.
|
||||
*
|
||||
* @private
|
||||
* @param {Array<string, number>} op
|
||||
* @throws {InvalidNumber}
|
||||
* @returns {void}
|
||||
*/
|
||||
_applyOp (op) {
|
||||
const key = op[0]
|
||||
const inc = op[1]
|
||||
|
||||
if (typeof inc !== 'number') {
|
||||
throw new Error('invalid increment number:', inc)
|
||||
}
|
||||
|
||||
let n
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(this._stats, key)) {
|
||||
n = this._stats[key] = Big(0)
|
||||
} else {
|
||||
n = this._stats[key]
|
||||
}
|
||||
this._stats[key] = n.plus(inc)
|
||||
|
||||
if (!this._frequencyAccumulators[key]) {
|
||||
this._frequencyAccumulators[key] = 0
|
||||
}
|
||||
this._frequencyAccumulators[key] += inc
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Stats
|
272
src/switch/transport.js
Normal file
272
src/switch/transport.js
Normal file
@ -0,0 +1,272 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-warning-comments: off */
|
||||
|
||||
const parallel = require('async/parallel')
|
||||
const once = require('once')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:switch:transport')
|
||||
|
||||
const LimitDialer = require('./limit-dialer')
|
||||
const { DIAL_TIMEOUT } = require('./constants')
|
||||
const { uniqueBy } = require('./utils')
|
||||
|
||||
// number of concurrent outbound dials to make per peer, same as go-libp2p-swtch
|
||||
const defaultPerPeerRateLimit = 8
|
||||
|
||||
/**
|
||||
* Manages the transports for the switch. This simplifies dialing and listening across
|
||||
* multiple transports.
|
||||
*/
|
||||
class TransportManager {
|
||||
constructor (_switch) {
|
||||
this.switch = _switch
|
||||
this.dialer = new LimitDialer(defaultPerPeerRateLimit, this.switch._options.dialTimeout || DIAL_TIMEOUT)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a `Transport` to the list of transports on the switch, and assigns it to the given key
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Transport} transport
|
||||
* @returns {void}
|
||||
*/
|
||||
add (key, transport) {
|
||||
log('adding %s', key)
|
||||
if (this.switch.transports[key]) {
|
||||
throw new Error('There is already a transport with this key')
|
||||
}
|
||||
|
||||
this.switch.transports[key] = transport
|
||||
if (!this.switch.transports[key].listeners) {
|
||||
this.switch.transports[key].listeners = []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes connections for the given transport key
|
||||
* and removes it from the switch.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
remove (key, callback) {
|
||||
callback = callback || function () {}
|
||||
|
||||
if (!this.switch.transports[key]) {
|
||||
return callback()
|
||||
}
|
||||
|
||||
this.close(key, (err) => {
|
||||
delete this.switch.transports[key]
|
||||
callback(err)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls `remove` on each transport the switch has
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
removeAll (callback) {
|
||||
const tasks = Object.keys(this.switch.transports).map((key) => {
|
||||
return (cb) => {
|
||||
this.remove(key, cb)
|
||||
}
|
||||
})
|
||||
|
||||
parallel(tasks, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given transport `key`, dial to all that transport multiaddrs
|
||||
*
|
||||
* @param {String} key Key of the `Transport` to dial
|
||||
* @param {PeerInfo} peerInfo
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dial (key, peerInfo, callback) {
|
||||
const transport = this.switch.transports[key]
|
||||
let multiaddrs = peerInfo.multiaddrs.toArray()
|
||||
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
// filter the multiaddrs that are actually valid for this transport
|
||||
multiaddrs = TransportManager.dialables(transport, multiaddrs, this.switch._peerInfo)
|
||||
log('dialing %s', key, multiaddrs.map((m) => m.toString()))
|
||||
|
||||
// dial each of the multiaddrs with the given transport
|
||||
this.dialer.dialMany(peerInfo.id, transport, multiaddrs, (errors, success) => {
|
||||
if (errors) {
|
||||
return callback(errors)
|
||||
}
|
||||
|
||||
peerInfo.connect(success.multiaddr)
|
||||
callback(null, success.conn)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given Transport `key`, listen on all multiaddrs in the switch's `_peerInfo`.
|
||||
* If a `handler` is not provided, the Switch's `protocolMuxer` will be used.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {*} _options Currently ignored
|
||||
* @param {function(Connection)} handler
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
listen (key, _options, handler, callback) {
|
||||
handler = this.switch._connectionHandler(key, handler)
|
||||
|
||||
const transport = this.switch.transports[key]
|
||||
let originalAddrs = this.switch._peerInfo.multiaddrs.toArray()
|
||||
|
||||
// Until TCP can handle distinct addresses on listen, https://github.com/libp2p/interface-transport/issues/41,
|
||||
// make sure we aren't trying to listen on duplicate ports. This also applies to websockets.
|
||||
originalAddrs = uniqueBy(originalAddrs, (addr) => {
|
||||
// Any non 0 port should register as unique
|
||||
const port = Number(addr.toOptions().port)
|
||||
return isNaN(port) || port === 0 ? addr.toString() : port
|
||||
})
|
||||
|
||||
const multiaddrs = TransportManager.dialables(transport, originalAddrs)
|
||||
|
||||
if (!transport.listeners) {
|
||||
transport.listeners = []
|
||||
}
|
||||
|
||||
let freshMultiaddrs = []
|
||||
|
||||
const createListeners = multiaddrs.map((ma) => {
|
||||
return (cb) => {
|
||||
const done = once(cb)
|
||||
const listener = transport.createListener(handler)
|
||||
listener.once('error', done)
|
||||
|
||||
listener.listen(ma, (err) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
listener.removeListener('error', done)
|
||||
listener.getAddrs((err, addrs) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
freshMultiaddrs = freshMultiaddrs.concat(addrs)
|
||||
transport.listeners.push(listener)
|
||||
done()
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
parallel(createListeners, (err) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// cause we can listen on port 0 or 0.0.0.0
|
||||
this.switch._peerInfo.multiaddrs.replace(multiaddrs, freshMultiaddrs)
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the transport with the given key, by closing all of its listeners
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
close (key, callback) {
|
||||
const transport = this.switch.transports[key]
|
||||
|
||||
if (!transport) {
|
||||
return callback(new Error(`Trying to close non existing transport: ${key}`))
|
||||
}
|
||||
|
||||
parallel(transport.listeners.map((listener) => {
|
||||
return (cb) => {
|
||||
listener.close(cb)
|
||||
}
|
||||
}), callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given transport, return its multiaddrs that match the given multiaddrs
|
||||
*
|
||||
* @param {Transport} transport
|
||||
* @param {Array<Multiaddr>} multiaddrs
|
||||
* @param {PeerInfo} peerInfo Optional - a peer whose addresses should not be returned
|
||||
* @returns {Array<Multiaddr>}
|
||||
*/
|
||||
static dialables (transport, multiaddrs, peerInfo) {
|
||||
// If we dont have a proper transport, return no multiaddrs
|
||||
if (!transport || !transport.filter) return []
|
||||
|
||||
const transportAddrs = transport.filter(multiaddrs)
|
||||
if (!peerInfo || !transportAddrs.length) {
|
||||
return transportAddrs
|
||||
}
|
||||
|
||||
const ourAddrs = ourAddresses(peerInfo)
|
||||
|
||||
const result = transportAddrs.filter(transportAddr => {
|
||||
// If our address is in the destination address, filter it out
|
||||
return !ourAddrs.some(a => getDestination(transportAddr).startsWith(a))
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand addresses in peer info into array of addresses with and without peer
|
||||
* ID suffix.
|
||||
*
|
||||
* @param {PeerInfo} peerInfo Our peer info object
|
||||
* @returns {String[]}
|
||||
*/
|
||||
function ourAddresses (peerInfo) {
|
||||
const ourPeerId = peerInfo.id.toB58String()
|
||||
return peerInfo.multiaddrs.toArray()
|
||||
.reduce((ourAddrs, addr) => {
|
||||
const peerId = addr.getPeerId()
|
||||
addr = addr.toString()
|
||||
const otherAddr = peerId
|
||||
? addr.slice(0, addr.lastIndexOf(`/ipfs/${peerId}`))
|
||||
: `${addr}/ipfs/${ourPeerId}`
|
||||
return ourAddrs.concat([addr, otherAddr])
|
||||
}, [])
|
||||
.filter(a => Boolean(a))
|
||||
.concat(`/ipfs/${ourPeerId}`)
|
||||
}
|
||||
|
||||
const RelayProtos = [
|
||||
'p2p-circuit',
|
||||
'p2p-websocket-star',
|
||||
'p2p-webrtc-star',
|
||||
'p2p-stardust'
|
||||
]
|
||||
|
||||
/**
|
||||
* Get the destination address of a (possibly relay) multiaddr as a string
|
||||
*
|
||||
* @param {Multiaddr} addr
|
||||
* @returns {String}
|
||||
*/
|
||||
function getDestination (addr) {
|
||||
const protos = addr.protoNames().reverse()
|
||||
const splitProto = protos.find(p => RelayProtos.includes(p))
|
||||
addr = addr.toString()
|
||||
if (!splitProto) return addr
|
||||
return addr.slice(addr.lastIndexOf(splitProto) + splitProto.length)
|
||||
}
|
||||
|
||||
module.exports = TransportManager
|
60
src/switch/utils.js
Normal file
60
src/switch/utils.js
Normal file
@ -0,0 +1,60 @@
|
||||
'use strict'
|
||||
|
||||
const Identify = require('../identify')
|
||||
|
||||
/**
|
||||
* For a given multistream, registers to handle the given connection
|
||||
* @param {MultistreamDialer} multistream
|
||||
* @param {Connection} connection
|
||||
* @returns {Promise}
|
||||
*/
|
||||
module.exports.msHandle = (multistream, connection) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
multistream.handle(connection, (err) => {
|
||||
if (err) return reject(err)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given multistream, selects the given protocol
|
||||
* @param {MultistreamDialer} multistream
|
||||
* @param {string} protocol
|
||||
* @returns {Promise} Resolves the selected Connection
|
||||
*/
|
||||
module.exports.msSelect = (multistream, protocol) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
multistream.select(protocol, (err, connection) => {
|
||||
if (err) return reject(err)
|
||||
resolve(connection)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs identify for the given connection and verifies it against the
|
||||
* PeerInfo provided
|
||||
* @param {Connection} connection
|
||||
* @param {PeerInfo} cryptoPeerInfo The PeerInfo determined during crypto exchange
|
||||
* @returns {Promise} Resolves {peerInfo, observedAddrs}
|
||||
*/
|
||||
module.exports.identifyDialer = (connection, cryptoPeerInfo) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
Identify.dialer(connection, cryptoPeerInfo, (err, peerInfo, observedAddrs) => {
|
||||
if (err) return reject(err)
|
||||
resolve({ peerInfo, observedAddrs })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique values from `arr` using `getValue` to determine
|
||||
* what is used for uniqueness
|
||||
* @param {Array} arr The array to get unique values for
|
||||
* @param {function(value)} getValue The function to determine what is compared
|
||||
* @returns {Array}
|
||||
*/
|
||||
module.exports.uniqueBy = (arr, getValue) => {
|
||||
return [...new Map(arr.map((i) => [getValue(i), i])).values()]
|
||||
}
|
183
src/transport-manager.js
Normal file
183
src/transport-manager.js
Normal file
@ -0,0 +1,183 @@
|
||||
'use strict'
|
||||
|
||||
const pSettle = require('p-settle')
|
||||
const { codes } = require('./errors')
|
||||
const errCode = require('err-code')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:transports')
|
||||
log.error = debug('libp2p:transports:error')
|
||||
|
||||
class TransportManager {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {Libp2p} options.libp2p The Libp2p instance. It will be passed to the transports.
|
||||
* @param {Upgrader} options.upgrader The upgrader to provide to the transports
|
||||
*/
|
||||
constructor ({ libp2p, upgrader }) {
|
||||
this.libp2p = libp2p
|
||||
this.upgrader = upgrader
|
||||
this._transports = new Map()
|
||||
this._listeners = new Map()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a `Transport` to the manager
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Transport} Transport
|
||||
* @returns {void}
|
||||
*/
|
||||
add (key, Transport) {
|
||||
log('adding %s', key)
|
||||
if (!key) {
|
||||
throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY)
|
||||
}
|
||||
if (this._transports.has(key)) {
|
||||
throw errCode(new Error('There is already a transport with this key'), codes.ERR_DUPLICATE_TRANSPORT)
|
||||
}
|
||||
|
||||
const transport = new Transport({
|
||||
libp2p: this.libp2p,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
|
||||
this._transports.set(key, transport)
|
||||
if (!this._listeners.has(key)) {
|
||||
this._listeners.set(key, [])
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops all listeners
|
||||
* @async
|
||||
*/
|
||||
async close () {
|
||||
const tasks = []
|
||||
for (const [key, listeners] of this._listeners) {
|
||||
log('closing listeners for %s', key)
|
||||
while (listeners.length) {
|
||||
const listener = listeners.pop()
|
||||
tasks.push(listener.close())
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(tasks)
|
||||
log('all listeners closed')
|
||||
this._listeners.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials the given Multiaddr over it's supported transport
|
||||
* @param {Multiaddr} ma
|
||||
* @param {*} options
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async dial (ma, options) {
|
||||
const transport = this.transportForMultiaddr(ma)
|
||||
if (!transport) {
|
||||
throw errCode(new Error(`No transport available for address ${String(ma)}`), codes.ERR_TRANSPORT_UNAVAILABLE)
|
||||
}
|
||||
|
||||
try {
|
||||
return await transport.dial(ma, options)
|
||||
} catch (err) {
|
||||
throw errCode(new Error('Transport dial failed'), codes.ERR_TRANSPORT_DIAL_FAILED, err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all Multiaddr's the listeners are using
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
getAddrs () {
|
||||
let addrs = []
|
||||
for (const listeners of this._listeners.values()) {
|
||||
for (const listener of listeners) {
|
||||
addrs = [...addrs, ...listener.getAddrs()]
|
||||
}
|
||||
}
|
||||
return addrs
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a transport that matches the given Multiaddr
|
||||
* @param {Multiaddr} ma
|
||||
* @returns {Transport|null}
|
||||
*/
|
||||
transportForMultiaddr (ma) {
|
||||
for (const transport of this._transports.values()) {
|
||||
const addrs = transport.filter([ma])
|
||||
if (addrs.length) return transport
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts listeners for each given Multiaddr.
|
||||
* @async
|
||||
* @param {Multiaddr[]} addrs
|
||||
*/
|
||||
async listen (addrs) {
|
||||
for (const [key, transport] of this._transports.entries()) {
|
||||
const supportedAddrs = transport.filter(addrs)
|
||||
const tasks = []
|
||||
|
||||
// For each supported multiaddr, create a listener
|
||||
for (const addr of supportedAddrs) {
|
||||
log('creating listener for %s on %s', key, addr)
|
||||
const listener = transport.createListener({}, this.onConnection)
|
||||
this._listeners.get(key).push(listener)
|
||||
|
||||
// We need to attempt to listen on everything
|
||||
tasks.push(listener.listen(addr))
|
||||
}
|
||||
|
||||
const results = await pSettle(tasks)
|
||||
// If we are listening on at least 1 address, succeed.
|
||||
// TODO: we should look at adding a retry (`p-retry`) here to better support
|
||||
// listening on remote addresses as they may be offline. We could then potentially
|
||||
// just wait for any (`p-any`) listener to succeed on each transport before returning
|
||||
const isListening = results.find(r => r.isFulfilled === true)
|
||||
if (!isListening) {
|
||||
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given transport from the manager.
|
||||
* If a transport has any running listeners, they will be closed.
|
||||
*
|
||||
* @async
|
||||
* @param {string} key
|
||||
*/
|
||||
async remove (key) {
|
||||
log('removing %s', key)
|
||||
if (this._listeners.has(key)) {
|
||||
// Close any running listeners
|
||||
for (const listener of this._listeners.get(key)) {
|
||||
await listener.close()
|
||||
}
|
||||
}
|
||||
|
||||
this._transports.delete(key)
|
||||
this._listeners.delete(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all transports from the manager.
|
||||
* If any listeners are running, they will be closed.
|
||||
* @async
|
||||
*/
|
||||
async removeAll () {
|
||||
const tasks = []
|
||||
for (const key of this._transports.keys()) {
|
||||
tasks.push(this.remove(key))
|
||||
}
|
||||
|
||||
await Promise.all(tasks)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TransportManager
|
366
src/upgrader.js
Normal file
366
src/upgrader.js
Normal file
@ -0,0 +1,366 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:upgrader')
|
||||
log.error = debug('libp2p:upgrader:error')
|
||||
const Multistream = require('multistream-select')
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const PeerId = require('peer-id')
|
||||
const pipe = require('it-pipe')
|
||||
const errCode = require('err-code')
|
||||
|
||||
const { codes } = require('./errors')
|
||||
|
||||
/**
|
||||
* @typedef MultiaddrConnection
|
||||
* @property {function} sink
|
||||
* @property {AsyncIterator} source
|
||||
* @property {*} conn
|
||||
* @property {Multiaddr} remoteAddr
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef CryptoResult
|
||||
* @property {*} conn A duplex iterable
|
||||
* @property {PeerId} remotePeer
|
||||
* @property {string} protocol
|
||||
*/
|
||||
|
||||
class Upgrader {
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {PeerId} options.localPeer
|
||||
* @param {Map<string, Crypto>} options.cryptos
|
||||
* @param {Map<string, Muxer>} options.muxers
|
||||
* @param {function(Connection)} options.onConnection Called when a connection is upgraded
|
||||
* @param {function(Connection)} options.onConnectionEnd
|
||||
*/
|
||||
constructor ({
|
||||
localPeer,
|
||||
cryptos,
|
||||
muxers,
|
||||
onConnectionEnd = () => {},
|
||||
onConnection = () => {}
|
||||
}) {
|
||||
this.localPeer = localPeer
|
||||
this.cryptos = cryptos || new Map()
|
||||
this.muxers = muxers || new Map()
|
||||
this.protector = null
|
||||
this.protocols = new Map()
|
||||
this.onConnection = onConnection
|
||||
this.onConnectionEnd = onConnectionEnd
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrades an inbound connection
|
||||
* @async
|
||||
* @param {MultiaddrConnection} maConn
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async upgradeInbound (maConn) {
|
||||
let encryptedConn
|
||||
let remotePeer
|
||||
let muxedConnection
|
||||
let Muxer
|
||||
let cryptoProtocol
|
||||
|
||||
log('Starting the inbound connection upgrade')
|
||||
|
||||
// Protect
|
||||
let protectedConn = maConn
|
||||
if (this.protector) {
|
||||
protectedConn = await this.protector.protect(maConn)
|
||||
}
|
||||
|
||||
try {
|
||||
// Encrypt the connection
|
||||
({
|
||||
conn: encryptedConn,
|
||||
remotePeer,
|
||||
protocol: cryptoProtocol
|
||||
} = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))
|
||||
|
||||
// Multiplex the connection
|
||||
;({ stream: muxedConnection, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
|
||||
} catch (err) {
|
||||
log.error('Failed to upgrade inbound connection', err)
|
||||
await maConn.close(err)
|
||||
// TODO: We shouldn't throw here, as there isn't anything to catch the failure
|
||||
throw err
|
||||
}
|
||||
|
||||
log('Successfully upgraded inbound connection')
|
||||
|
||||
return this._createConnection({
|
||||
cryptoProtocol,
|
||||
direction: 'inbound',
|
||||
maConn,
|
||||
muxedConnection,
|
||||
Muxer,
|
||||
remotePeer
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrades an outbound connection
|
||||
* @async
|
||||
* @param {MultiaddrConnection} maConn
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async upgradeOutbound (maConn) {
|
||||
let remotePeerId
|
||||
try {
|
||||
remotePeerId = PeerId.createFromB58String(maConn.remoteAddr.getPeerId())
|
||||
} catch (err) {
|
||||
log.error('multiaddr did not contain a valid peer id', err)
|
||||
}
|
||||
|
||||
let encryptedConn
|
||||
let remotePeer
|
||||
let muxedConnection
|
||||
let cryptoProtocol
|
||||
let Muxer
|
||||
|
||||
log('Starting the outbound connection upgrade')
|
||||
|
||||
// Protect
|
||||
let protectedConn = maConn
|
||||
if (this.protector) {
|
||||
protectedConn = await this.protector.protect(maConn)
|
||||
}
|
||||
|
||||
try {
|
||||
// Encrypt the connection
|
||||
({
|
||||
conn: encryptedConn,
|
||||
remotePeer,
|
||||
protocol: cryptoProtocol
|
||||
} = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))
|
||||
|
||||
// Multiplex the connection
|
||||
;({ stream: muxedConnection, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
|
||||
} catch (err) {
|
||||
log.error('Failed to upgrade outbound connection', err)
|
||||
await maConn.close(err)
|
||||
throw err
|
||||
}
|
||||
|
||||
log('Successfully upgraded outbound connection')
|
||||
|
||||
return this._createConnection({
|
||||
cryptoProtocol,
|
||||
direction: 'outbound',
|
||||
maConn,
|
||||
muxedConnection,
|
||||
Muxer,
|
||||
remotePeer
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method for generating a new `Connection`
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {string} cryptoProtocol The crypto protocol that was negotiated
|
||||
* @param {string} direction One of ['inbound', 'outbound']
|
||||
* @param {MultiaddrConnection} maConn The transport layer connection
|
||||
* @param {*} muxedConnection A duplex connection returned from multiplexer selection
|
||||
* @param {Muxer} Muxer The muxer to be used for muxing
|
||||
* @param {PeerId} remotePeer The peer the connection is with
|
||||
* @returns {Connection}
|
||||
*/
|
||||
_createConnection ({
|
||||
cryptoProtocol,
|
||||
direction,
|
||||
maConn,
|
||||
muxedConnection,
|
||||
Muxer,
|
||||
remotePeer
|
||||
}) {
|
||||
// Create the muxer
|
||||
const muxer = new Muxer({
|
||||
// Run anytime a remote stream is created
|
||||
onStream: async muxedStream => {
|
||||
const mss = new Multistream.Listener(muxedStream)
|
||||
try {
|
||||
const { stream, protocol } = await mss.handle(Array.from(this.protocols.keys()))
|
||||
log('%s: incoming stream opened on %s', direction, protocol)
|
||||
connection.addStream(stream, protocol)
|
||||
this._onStream({ connection, stream, protocol })
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
},
|
||||
// Run anytime a stream closes
|
||||
onStreamEnd: muxedStream => {
|
||||
connection.removeStream(muxedStream.id)
|
||||
}
|
||||
})
|
||||
|
||||
const newStream = async protocols => {
|
||||
log('%s: starting new stream on %s', direction, protocols)
|
||||
const muxedStream = muxer.newStream()
|
||||
const mss = new Multistream.Dialer(muxedStream)
|
||||
try {
|
||||
const { stream, protocol } = await mss.select(protocols)
|
||||
return { stream: { ...muxedStream, ...stream }, protocol }
|
||||
} catch (err) {
|
||||
log.error('could not create new stream', err)
|
||||
throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
|
||||
}
|
||||
}
|
||||
|
||||
// Pipe all data through the muxer
|
||||
pipe(muxedConnection, muxer, muxedConnection)
|
||||
|
||||
maConn.timeline.upgraded = Date.now()
|
||||
const timelineProxy = new Proxy(maConn.timeline, {
|
||||
set: (...args) => {
|
||||
if (args[1] === 'close' && args[2]) {
|
||||
this.onConnectionEnd(connection)
|
||||
}
|
||||
|
||||
return Reflect.set(...args)
|
||||
}
|
||||
})
|
||||
|
||||
// Create the connection
|
||||
const connection = new Connection({
|
||||
localAddr: maConn.localAddr,
|
||||
remoteAddr: maConn.remoteAddr,
|
||||
localPeer: this.localPeer,
|
||||
remotePeer: remotePeer,
|
||||
stat: {
|
||||
direction,
|
||||
timeline: timelineProxy,
|
||||
multiplexer: Muxer.multicodec,
|
||||
encryption: cryptoProtocol
|
||||
},
|
||||
newStream,
|
||||
getStreams: () => muxer.streams,
|
||||
close: err => maConn.close(err)
|
||||
})
|
||||
|
||||
this.onConnection(connection)
|
||||
|
||||
return connection
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes incoming streams to the correct handler
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {Connection} options.connection The connection the stream belongs to
|
||||
* @param {Stream} options.stream
|
||||
* @param {string} options.protocol
|
||||
*/
|
||||
_onStream ({ connection, stream, protocol }) {
|
||||
const handler = this.protocols.get(protocol)
|
||||
handler({ connection, stream, protocol })
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to encrypt the incoming `connection` with the provided `cryptos`.
|
||||
* @private
|
||||
* @async
|
||||
* @param {PeerId} localPeer The initiators PeerInfo
|
||||
* @param {*} connection
|
||||
* @param {Map<string, Crypto>} cryptos
|
||||
* @returns {CryptoResult} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
|
||||
*/
|
||||
async _encryptInbound (localPeer, connection, cryptos) {
|
||||
const mss = new Multistream.Listener(connection)
|
||||
const protocols = Array.from(cryptos.keys())
|
||||
log('handling inbound crypto protocol selection', protocols)
|
||||
|
||||
try {
|
||||
const { stream, protocol } = await mss.handle(protocols)
|
||||
const crypto = cryptos.get(protocol)
|
||||
log('encrypting inbound connection...')
|
||||
|
||||
return {
|
||||
...await crypto.secureInbound(localPeer, stream),
|
||||
protocol
|
||||
}
|
||||
} catch (err) {
|
||||
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to encrypt the given `connection` with the provided `cryptos`.
|
||||
* The first `Crypto` module to succeed will be used
|
||||
* @private
|
||||
* @async
|
||||
* @param {PeerId} localPeer The initiators PeerInfo
|
||||
* @param {*} connection
|
||||
* @param {PeerId} remotePeerId
|
||||
* @param {Map<string, Crypto>} cryptos
|
||||
* @returns {CryptoResult} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
|
||||
*/
|
||||
async _encryptOutbound (localPeer, connection, remotePeerId, cryptos) {
|
||||
const mss = new Multistream.Dialer(connection)
|
||||
const protocols = Array.from(cryptos.keys())
|
||||
log('selecting outbound crypto protocol', protocols)
|
||||
|
||||
try {
|
||||
const { stream, protocol } = await mss.select(protocols)
|
||||
const crypto = cryptos.get(protocol)
|
||||
log('encrypting outbound connection to %j', remotePeerId)
|
||||
|
||||
return {
|
||||
...await crypto.secureOutbound(localPeer, stream, remotePeerId),
|
||||
protocol
|
||||
}
|
||||
} catch (err) {
|
||||
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects one of the given muxers via multistream-select. That
|
||||
* muxer will be used for all future streams on the connection.
|
||||
* @private
|
||||
* @async
|
||||
* @param {*} connection A basic duplex connection to multiplex
|
||||
* @param {Map<string, Muxer>} muxers The muxers to attempt multiplexing with
|
||||
* @returns {*} A muxed connection
|
||||
*/
|
||||
async _multiplexOutbound (connection, muxers) {
|
||||
const dialer = new Multistream.Dialer(connection)
|
||||
const protocols = Array.from(muxers.keys())
|
||||
log('outbound selecting muxer %s', protocols)
|
||||
try {
|
||||
const { stream, protocol } = await dialer.select(protocols)
|
||||
log('%s selected as muxer protocol', protocol)
|
||||
const Muxer = muxers.get(protocol)
|
||||
return { stream, Muxer }
|
||||
} catch (err) {
|
||||
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers support for one of the given muxers via multistream-select. The
|
||||
* selected muxer will be used for all future streams on the connection.
|
||||
* @private
|
||||
* @async
|
||||
* @param {*} connection A basic duplex connection to multiplex
|
||||
* @param {Map<string, Muxer>} muxers The muxers to attempt multiplexing with
|
||||
* @returns {*} A muxed connection
|
||||
*/
|
||||
async _multiplexInbound (connection, muxers) {
|
||||
const listener = new Multistream.Listener(connection)
|
||||
const protocols = Array.from(muxers.keys())
|
||||
log('inbound handling muxers %s', protocols)
|
||||
try {
|
||||
const { stream, protocol } = await listener.handle(protocols)
|
||||
const Muxer = muxers.get(protocol)
|
||||
return { stream, Muxer }
|
||||
} catch (err) {
|
||||
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Upgrader
|
@ -30,4 +30,18 @@ function emitFirst (emitter, events, handler) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts BufferList messages to Buffers
|
||||
* @param {*} source
|
||||
* @returns {AsyncGenerator}
|
||||
*/
|
||||
function toBuffer (source) {
|
||||
return (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
module.exports.emitFirst = emitFirst
|
||||
module.exports.toBuffer = toBuffer
|
||||
|
@ -1,4 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
require('./circuit-relay.browser')
|
||||
require('./transports.browser')
|
@ -1,98 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
const tryEcho = require('./utils/try-echo')
|
||||
const echo = require('./utils/echo')
|
||||
|
||||
const {
|
||||
getPeerRelay
|
||||
} = require('./utils/constants')
|
||||
|
||||
function setupNodeWithRelay (addrs, options = {}) {
|
||||
options = {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true
|
||||
},
|
||||
...options.config
|
||||
},
|
||||
...options
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
createNode(addrs, options, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node.handle(echo.multicodec, echo)
|
||||
node.start((err) => {
|
||||
expect(err).to.not.exist()
|
||||
resolve(node)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
describe('circuit relay', () => {
|
||||
let browserNode1
|
||||
let browserNode2
|
||||
let peerRelay
|
||||
|
||||
before('get peer relay', async () => {
|
||||
peerRelay = await new Promise(resolve => {
|
||||
getPeerRelay((err, peer) => {
|
||||
expect(err).to.not.exist()
|
||||
resolve(peer)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
before('create the browser nodes', async () => {
|
||||
[browserNode1, browserNode2] = await Promise.all([
|
||||
setupNodeWithRelay([]),
|
||||
setupNodeWithRelay([])
|
||||
])
|
||||
})
|
||||
|
||||
before('connect to the relay node', async () => {
|
||||
await Promise.all(
|
||||
[browserNode1, browserNode2].map((node) => {
|
||||
return new Promise(resolve => {
|
||||
node.dialProtocol(peerRelay, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
before('give time for HOP support to be determined', async () => {
|
||||
await new Promise(resolve => {
|
||||
setTimeout(resolve, 1e3)
|
||||
})
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
await Promise.all(
|
||||
[browserNode1, browserNode2].map((node) => {
|
||||
return new Promise((resolve) => {
|
||||
node.stop(resolve)
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('should be able to echo over relay', (done) => {
|
||||
browserNode1.dialProtocol(browserNode2.peerInfo, echo.multicodec, (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(conn).to.exist()
|
||||
|
||||
tryEcho(conn, done)
|
||||
})
|
||||
})
|
||||
})
|
@ -1,215 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const sinon = require('sinon')
|
||||
const waterfall = require('async/waterfall')
|
||||
const series = require('async/series')
|
||||
const parallel = require('async/parallel')
|
||||
const Circuit = require('libp2p-circuit')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
const tryEcho = require('./utils/try-echo')
|
||||
const echo = require('./utils/echo')
|
||||
|
||||
describe('circuit relay', () => {
|
||||
let handlerSpies = []
|
||||
let relayNode1
|
||||
let relayNode2
|
||||
let nodeWS1
|
||||
let nodeWS2
|
||||
let nodeTCP1
|
||||
let nodeTCP2
|
||||
|
||||
function setupNode (addrs, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
|
||||
return createNode(addrs, options, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node.handle('/echo/1.0.0', echo)
|
||||
node.start((err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
handlerSpies.push(sinon.spy(
|
||||
node._switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'
|
||||
))
|
||||
|
||||
callback(node)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
before(function (done) {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
waterfall([
|
||||
// set up passive relay
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0/ws',
|
||||
'/ip4/0.0.0.0/tcp/0'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false // passive relay
|
||||
}
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
relayNode1 = node
|
||||
cb()
|
||||
}),
|
||||
// setup active relay
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0/ws',
|
||||
'/ip4/0.0.0.0/tcp/0'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false // passive relay
|
||||
}
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
relayNode2 = node
|
||||
cb()
|
||||
}),
|
||||
// setup node with WS
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0/ws'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
nodeWS1 = node
|
||||
cb()
|
||||
}),
|
||||
// setup node with WS
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0/ws'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
nodeWS2 = node
|
||||
cb()
|
||||
}),
|
||||
// set up node with TCP
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
nodeTCP1 = node
|
||||
cb()
|
||||
}),
|
||||
// set up node with TCP
|
||||
(cb) => setupNode([
|
||||
'/ip4/0.0.0.0/tcp/0'
|
||||
], {
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (node) => {
|
||||
nodeTCP2 = node
|
||||
cb()
|
||||
})
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
series([
|
||||
(cb) => nodeWS1.dial(relayNode1.peerInfo, cb),
|
||||
(cb) => nodeWS1.dial(relayNode2.peerInfo, cb),
|
||||
(cb) => nodeTCP1.dial(relayNode1.peerInfo, cb),
|
||||
(cb) => nodeTCP2.dial(relayNode2.peerInfo, cb)
|
||||
], done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
parallel([
|
||||
(cb) => relayNode1.stop(cb),
|
||||
(cb) => relayNode2.stop(cb),
|
||||
(cb) => nodeWS1.stop(cb),
|
||||
(cb) => nodeWS2.stop(cb),
|
||||
(cb) => nodeTCP1.stop(cb),
|
||||
(cb) => nodeTCP2.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
describe('any relay', function () {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
it('dial from WS1 to TCP1 over any R', (done) => {
|
||||
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(conn).to.exist()
|
||||
tryEcho(conn, done)
|
||||
})
|
||||
})
|
||||
|
||||
it('fail to dial - no R from WS2 to TCP1', (done) => {
|
||||
nodeWS2.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(conn).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('explicit relay', function () {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
it('dial from WS1 to TCP1 over R1', (done) => {
|
||||
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(conn).to.exist()
|
||||
|
||||
tryEcho(conn, () => {
|
||||
const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString()
|
||||
expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('dial from WS1 to TCP2 over R2', (done) => {
|
||||
nodeWS1.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(conn).to.exist()
|
||||
|
||||
tryEcho(conn, () => {
|
||||
const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString()
|
||||
expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -1,332 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const waterfall = require('async/waterfall')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
|
||||
const validateConfig = require('../src/config').validate
|
||||
|
||||
describe('configuration', () => {
|
||||
let peerInfo
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 512 }, cb),
|
||||
(peerId, cb) => PeerInfo.create(peerId, cb),
|
||||
(info, cb) => {
|
||||
peerInfo = info
|
||||
cb()
|
||||
}
|
||||
], () => done())
|
||||
})
|
||||
|
||||
it('should throw an error if peerInfo is missing', () => {
|
||||
expect(() => {
|
||||
validateConfig({
|
||||
modules: {
|
||||
transport: [ WS ]
|
||||
}
|
||||
})
|
||||
}).to.throw()
|
||||
})
|
||||
|
||||
it('should throw an error if modules is missing', () => {
|
||||
expect(() => {
|
||||
validateConfig({
|
||||
peerInfo
|
||||
})
|
||||
}).to.throw()
|
||||
})
|
||||
|
||||
it('should throw an error if there are no transports', () => {
|
||||
expect(() => {
|
||||
validateConfig({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ ]
|
||||
}
|
||||
})
|
||||
}).to.throw('ERROR_EMPTY')
|
||||
})
|
||||
|
||||
it('should add defaults to config', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
dht: DHT
|
||||
}
|
||||
}
|
||||
|
||||
const expected = {
|
||||
peerInfo,
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
},
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabled: false,
|
||||
randomWalk: {
|
||||
enabled: false,
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300000,
|
||||
timeout: 10000
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
active: false,
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(validateConfig(options)).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should add defaults to missing items', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const expected = {
|
||||
peerInfo,
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
},
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
bootstrap: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabled: false,
|
||||
randomWalk: {
|
||||
enabled: false,
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300000,
|
||||
timeout: 10000
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
active: false,
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(validateConfig(options)).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should allow for configuring the switch', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
switch: {
|
||||
blacklistTTL: 60e3,
|
||||
blackListAttempts: 5,
|
||||
maxParallelDials: 100,
|
||||
maxColdCalls: 50,
|
||||
dialTimeout: 30e3
|
||||
},
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ ]
|
||||
}
|
||||
}
|
||||
|
||||
expect(validateConfig(options)).to.deep.include({
|
||||
switch: {
|
||||
blacklistTTL: 60e3,
|
||||
blackListAttempts: 5,
|
||||
maxParallelDials: 100,
|
||||
maxColdCalls: 50,
|
||||
dialTimeout: 30e3
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('should allow for delegated content and peer routing', () => {
|
||||
const peerRouter = new DelegatedPeerRouter()
|
||||
const contentRouter = new DelegatedContentRouter(peerInfo)
|
||||
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
peerRouting: [ peerRouter ],
|
||||
contentRouting: [ contentRouter ],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(validateConfig(options).modules).to.deep.include({
|
||||
peerRouting: [ peerRouter ],
|
||||
contentRouting: [ contentRouter ]
|
||||
})
|
||||
})
|
||||
|
||||
it('should not allow for dht to be enabled without it being provided', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ]
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(() => validateConfig(options)).to.throw()
|
||||
})
|
||||
|
||||
it('should be able to add validators and selectors for dht', () => {
|
||||
const selectors = {}
|
||||
const validators = {}
|
||||
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WS],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
selectors,
|
||||
validators
|
||||
}
|
||||
}
|
||||
}
|
||||
const expected = {
|
||||
peerInfo,
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
},
|
||||
modules: {
|
||||
transport: [WS],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
},
|
||||
peerDiscovery: {
|
||||
autoDial: true
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
active: false,
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
selectors,
|
||||
validators
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(validateConfig(options)).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should support new properties for the dht config', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WS],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabled: false,
|
||||
myNewDHTConfigProperty: true,
|
||||
randomWalk: {
|
||||
enabled: false,
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300000,
|
||||
timeout: 10000
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const expected = {
|
||||
kBucketSize: 20,
|
||||
enabled: false,
|
||||
myNewDHTConfigProperty: true,
|
||||
randomWalk: {
|
||||
enabled: false,
|
||||
queriesPerPeriod: 1,
|
||||
interval: 300000,
|
||||
timeout: 10000
|
||||
}
|
||||
}
|
||||
|
||||
const actual = validateConfig(options).config.dht
|
||||
|
||||
expect(actual).to.deep.equal(expected)
|
||||
})
|
||||
})
|
@ -1,411 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
/* eslint max-nested-callbacks: ["error", 8] */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const parallel = require('async/parallel')
|
||||
const waterfall = require('async/waterfall')
|
||||
const _times = require('lodash.times')
|
||||
const CID = require('cids')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const sinon = require('sinon')
|
||||
const nock = require('nock')
|
||||
const ma = require('multiaddr')
|
||||
const Node = require('./utils/bundle-nodejs')
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
const createPeerInfo = createNode.createPeerInfo
|
||||
|
||||
describe('.contentRouting', () => {
|
||||
describe('via the dht', () => {
|
||||
let nodeA
|
||||
let nodeB
|
||||
let nodeC
|
||||
let nodeD
|
||||
let nodeE
|
||||
|
||||
before(function (done) {
|
||||
this.timeout(5 * 1000)
|
||||
const tasks = _times(5, () => (cb) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
node.start((err) => cb(err, node))
|
||||
})
|
||||
})
|
||||
|
||||
parallel(tasks, (err, nodes) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = nodes[0]
|
||||
nodeB = nodes[1]
|
||||
nodeC = nodes[2]
|
||||
nodeD = nodes[3]
|
||||
nodeE = nodes[4]
|
||||
|
||||
parallel([
|
||||
(cb) => nodeA.dial(nodeB.peerInfo, cb),
|
||||
(cb) => nodeB.dial(nodeC.peerInfo, cb),
|
||||
(cb) => nodeC.dial(nodeD.peerInfo, cb),
|
||||
(cb) => nodeD.dial(nodeE.peerInfo, cb),
|
||||
(cb) => nodeE.dial(nodeA.peerInfo, cb)
|
||||
], done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
parallel([
|
||||
(cb) => nodeA.stop(cb),
|
||||
(cb) => nodeB.stop(cb),
|
||||
(cb) => nodeC.stop(cb),
|
||||
(cb) => nodeD.stop(cb),
|
||||
(cb) => nodeE.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('should use the nodes dht to provide', (done) => {
|
||||
const stub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
|
||||
it('should use the nodes dht to find providers', (done) => {
|
||||
const stub = sinon.stub(nodeA._dht, 'findProviders').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
|
||||
nodeA.contentRouting.findProviders()
|
||||
})
|
||||
|
||||
describe('le ring', () => {
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||
|
||||
it('let kbucket get filled', (done) => {
|
||||
setTimeout(() => done(), 250)
|
||||
})
|
||||
|
||||
it('nodeA.contentRouting.provide', (done) => {
|
||||
nodeA.contentRouting.provide(cid, done)
|
||||
})
|
||||
|
||||
it('nodeE.contentRouting.findProviders for existing record', (done) => {
|
||||
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length.above(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeE.contentRouting.findProviders with limited number of providers', (done) => {
|
||||
parallel([
|
||||
(cb) => nodeA.contentRouting.provide(cid, cb),
|
||||
(cb) => nodeB.contentRouting.provide(cid, cb),
|
||||
(cb) => nodeC.contentRouting.provide(cid, cb)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeE.contentRouting.findProviders(cid, { maxNumProviders: 2 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length(2)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeC.contentRouting.findProviders for non existing record (timeout)', (done) => {
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')
|
||||
|
||||
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => {
|
||||
createPeerInfo(cb)
|
||||
},
|
||||
// Create the node using the delegate
|
||||
(peerInfo, cb) => {
|
||||
delegate = new DelegatedContentRouter(peerInfo.id, {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: 60197
|
||||
}, [
|
||||
ma('/ip4/0.0.0.0/tcp/60194')
|
||||
])
|
||||
nodeA = new Node({
|
||||
peerInfo,
|
||||
modules: {
|
||||
contentRouting: [ delegate ]
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
nodeA.start(cb)
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
afterEach(() => nock.cleanAll())
|
||||
|
||||
describe('provide', () => {
|
||||
it('should use the delegate router to provide', (done) => {
|
||||
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
|
||||
it('should be able to register as a provider', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
// mock the swarm connect
|
||||
.post('/api/v0/swarm/connect')
|
||||
.query({
|
||||
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, {
|
||||
Strings: [`connect ${nodeA.peerInfo.id.toB58String()} success`]
|
||||
}, ['Content-Type', 'application/json'])
|
||||
// mock the refs call
|
||||
.post('/api/v0/refs')
|
||||
.query({
|
||||
recursive: true,
|
||||
arg: cid.toBaseEncodedString(),
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, null, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.provide(cid, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors when registering as a provider', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
// mock the swarm connect
|
||||
.post('/api/v0/swarm/connect')
|
||||
.query({
|
||||
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
|
||||
|
||||
nodeA.contentRouting.provide(cid, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('find providers', () => {
|
||||
it('should use the delegate router to find providers', (done) => {
|
||||
const stub = sinon.stub(delegate, 'findProviders').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.findProviders()
|
||||
})
|
||||
|
||||
it('should be able to find providers', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
.post('/api/v0/dht/findprovs')
|
||||
.query({
|
||||
arg: cid.toBaseEncodedString(),
|
||||
timeout: '1000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":1}\n`, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.findProviders(cid, 1000, (err, response) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(response).to.have.length(1)
|
||||
expect(response[0].id.toB58String()).to.equal(provider)
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors when finding providers', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
.post('/api/v0/dht/findprovs')
|
||||
.query({
|
||||
arg: cid.toBaseEncodedString(),
|
||||
timeout: '30000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(502, 'Bad Gateway', [
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.findProviders(cid, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via the dht and a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => {
|
||||
createPeerInfo(cb)
|
||||
},
|
||||
// Create the node using the delegate
|
||||
(peerInfo, cb) => {
|
||||
delegate = new DelegatedContentRouter(peerInfo.id, {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: 60197
|
||||
}, [
|
||||
ma('/ip4/0.0.0.0/tcp/60194')
|
||||
])
|
||||
nodeA = new Node({
|
||||
peerInfo,
|
||||
modules: {
|
||||
contentRouting: [ delegate ]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
nodeA.start(cb)
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
|
||||
describe('provide', () => {
|
||||
it('should use both the dht and delegate router to provide', (done) => {
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {})
|
||||
const delegateStub = sinon.stub(delegate, 'provide').callsFake(() => {
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.calledOnce).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
})
|
||||
|
||||
describe('findProviders', () => {
|
||||
it('should only use the dht if it finds providers', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, results)
|
||||
const delegateStub = sinon.stub(delegate, 'findProviders').throws(() => {
|
||||
return new Error('the delegate should not have been called')
|
||||
})
|
||||
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.notCalled).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should use the delegate if the dht fails to find providers', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, [])
|
||||
const delegateStub = sinon.stub(delegate, 'findProviders').callsArgWith(2, null, results)
|
||||
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.deep.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.calledOnce).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('no routers', () => {
|
||||
let nodeA
|
||||
before((done) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findProviders should return an error with no options', (done) => {
|
||||
nodeA.contentRouting.findProviders('a cid', (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findProviders should return an error with options', (done) => {
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -1,143 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const series = require('async/series')
|
||||
const createNode = require('./utils/create-node')
|
||||
const sinon = require('sinon')
|
||||
const { createLibp2p } = require('../src')
|
||||
const WS = require('libp2p-websockets')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
describe('libp2p creation', () => {
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('should be able to start and stop successfully', (done) => {
|
||||
createNode([], {
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
},
|
||||
dht: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
let sw = node._switch
|
||||
let cm = node.connectionManager
|
||||
let dht = node._dht
|
||||
let pub = node._floodSub
|
||||
|
||||
sinon.spy(sw, 'start')
|
||||
sinon.spy(cm, 'start')
|
||||
sinon.spy(dht, 'start')
|
||||
sinon.spy(dht.randomWalk, 'start')
|
||||
sinon.spy(pub, 'start')
|
||||
sinon.spy(sw, 'stop')
|
||||
sinon.spy(cm, 'stop')
|
||||
sinon.spy(dht, 'stop')
|
||||
sinon.spy(dht.randomWalk, 'stop')
|
||||
sinon.spy(pub, 'stop')
|
||||
sinon.spy(node, 'emit')
|
||||
|
||||
series([
|
||||
(cb) => node.start(cb),
|
||||
(cb) => {
|
||||
expect(sw.start.calledOnce).to.equal(true)
|
||||
expect(cm.start.calledOnce).to.equal(true)
|
||||
expect(dht.start.calledOnce).to.equal(true)
|
||||
expect(dht.randomWalk.start.calledOnce).to.equal(true)
|
||||
expect(pub.start.calledOnce).to.equal(true)
|
||||
expect(node.emit.calledWith('start')).to.equal(true)
|
||||
|
||||
cb()
|
||||
},
|
||||
(cb) => node.stop(cb)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(sw.stop.calledOnce).to.equal(true)
|
||||
expect(cm.stop.calledOnce).to.equal(true)
|
||||
expect(dht.stop.calledOnce).to.equal(true)
|
||||
expect(dht.randomWalk.stop.called).to.equal(true)
|
||||
expect(pub.stop.calledOnce).to.equal(true)
|
||||
expect(node.emit.calledWith('stop')).to.equal(true)
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should not create disabled modules', (done) => {
|
||||
createNode([], {
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(node._floodSub).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not throw errors from switch if node has no error listeners', (done) => {
|
||||
createNode([], {}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node._switch.emit('error', new Error('bad things'))
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should emit errors from switch if node has error listeners', (done) => {
|
||||
const error = new Error('bad things')
|
||||
createNode([], {}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
node.once('error', (err) => {
|
||||
expect(err).to.eql(error)
|
||||
done()
|
||||
})
|
||||
node._switch.emit('error', error)
|
||||
})
|
||||
})
|
||||
|
||||
it('createLibp2p should create a peerInfo instance', function (done) {
|
||||
this.timeout(10e3)
|
||||
createLibp2p({
|
||||
modules: {
|
||||
transport: [ WS ]
|
||||
}
|
||||
}, (err, libp2p) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(libp2p).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('createLibp2p should allow for a provided peerInfo instance', function (done) {
|
||||
this.timeout(10e3)
|
||||
PeerInfo.create((err, peerInfo) => {
|
||||
expect(err).to.not.exist()
|
||||
sinon.spy(PeerInfo, 'create')
|
||||
createLibp2p({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ]
|
||||
}
|
||||
}, (err, libp2p) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(libp2p).to.exist()
|
||||
expect(PeerInfo.create.callCount).to.eql(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
168
test/dht.node.js
168
test/dht.node.js
@ -1,168 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
|
||||
const MemoryStore = require('interface-datastore').MemoryDatastore
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
|
||||
describe('.dht', () => {
|
||||
describe('enabled', () => {
|
||||
let nodeA
|
||||
const datastore = new MemoryStore()
|
||||
|
||||
before(function (done) {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
datastore
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
|
||||
// Rewrite validators
|
||||
nodeA._dht.validators.v = {
|
||||
func (key, publicKey, callback) {
|
||||
setImmediate(callback)
|
||||
},
|
||||
sign: false
|
||||
}
|
||||
|
||||
// Rewrite selectors
|
||||
nodeA._dht.selectors.v = () => 0
|
||||
|
||||
// Start
|
||||
nodeA.start(done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
nodeA.stop(done)
|
||||
})
|
||||
|
||||
it('should be able to dht.put a value to the DHT', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
const value = Buffer.from('value')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.get a value from the DHT with options', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.get(key, { maxTimeout: 3000 }, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.eql(value)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.get a value from the DHT with no options defined', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.get(key, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.eql(value)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.getMany a value from the DHT with options', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.getMany(key, 1, { maxTimeout: 3000 }, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.getMany a value from the DHT with no options defined', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.getMany(key, 1, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('disabled', () => {
|
||||
let nodeA
|
||||
|
||||
before(function (done) {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
nodeA.start(done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
nodeA.stop(done)
|
||||
})
|
||||
|
||||
it('should receive an error on dht.put if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
const value = Buffer.from('value')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_DHT_DISABLED')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should receive an error on dht.get if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
|
||||
nodeA.dht.get(key, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_DHT_DISABLED')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should receive an error on dht.getMany if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
|
||||
nodeA.dht.getMany(key, 10, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_DHT_DISABLED')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user