mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 13:21:34 +00:00
Compare commits
154 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
10811e9ced | |||
9c2789bc15 | |||
24be691bc1 | |||
9433c6c398 | |||
04faf1806c | |||
b06ca1b3c7 | |||
bde30cac45 | |||
28c054c21e | |||
c346e8066b | |||
40978a1940 | |||
71dcaafcac | |||
5319e065ec | |||
f3801f0e6c | |||
51cc993876 | |||
a800c1ad91 | |||
54c474de98 | |||
f28dffb268 | |||
c049074cb5 | |||
1bde70f1b5 | |||
cfa4df6e11 | |||
eb5aa03232 | |||
4cb541ddae | |||
aa1d9b273a | |||
7313f781fc | |||
313b1eae20 | |||
01aa44724e | |||
8b627797e2 | |||
e5f19e860b | |||
5204da73f7 | |||
66130ccba8 | |||
aa1bb3ab75 | |||
813a59b9ce | |||
eee60ed37d | |||
e52ce66ab7 | |||
da52af704e | |||
282ce62703 | |||
ef3238bdee | |||
4c06c54fc5 | |||
bb0c45d704 | |||
eb10b5c6d2 | |||
a282fbe139 | |||
6cfb0b2692 | |||
de84ee473c | |||
f28ecb2483 | |||
bba6f7eff8 | |||
9f5f07269e | |||
5f92acd5bb | |||
dfe8f632f7 | |||
9f47100713 | |||
d497961938 | |||
6e76aade7f | |||
ec7d0761de | |||
59fe9732d7 | |||
4ed5c039fc | |||
9e7a080a5c | |||
558e5987be | |||
dcf59a8468 | |||
ab028a2be3 | |||
91e60d4253 | |||
679d446daa | |||
8047fb76fa | |||
c4cab007af | |||
ebaab3e47f | |||
b31690c8e6 | |||
3bde9c8bed | |||
14e12ee1f1 | |||
2374929990 | |||
26de739bb1 | |||
0f8d6afd8f | |||
daa26859e0 | |||
fdfb7b4e86 | |||
15bdb795a4 | |||
7d78728f54 | |||
53ed3bdb99 | |||
ae513887f5 | |||
7c78faa171 | |||
7d12eb9e26 | |||
581a1de472 | |||
288ac17954 | |||
2e4459b315 | |||
2a5232b541 | |||
44915b3723 | |||
64bba57255 | |||
88ebd1fc09 | |||
92cd591da4 | |||
320d84f541 | |||
970deec2a4 | |||
714b6ec2b9 | |||
f71fdfdf35 | |||
e92053da9a | |||
17b5f73b3d | |||
c18d2a4147 | |||
f1baa7e0b1 | |||
4abc868ab3 | |||
40e840d5fd | |||
9518eb44b3 | |||
0b75f99d75 | |||
686379efb0 | |||
a95389a28e | |||
3226632d83 | |||
dd934b9690 | |||
cef3c8b5cc | |||
eedb20e9a3 | |||
e51260434c | |||
7e6c9eeb38 | |||
c537140fbc | |||
3b7c4b5eb0 | |||
4460e8246c | |||
a63432e24b | |||
69f7264123 | |||
e052021397 | |||
fdd714ee60 | |||
65e7223ce0 | |||
eddec7d2e4 | |||
7b6c921d36 | |||
05c16e4262 | |||
c8a86db310 | |||
ce29902691 | |||
0b729621db |
152
.aegir.js
152
.aegir.js
@ -1,76 +1,134 @@
|
||||
'use strict'
|
||||
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
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 promisify = require('promisify-es6')
|
||||
const mplex = require('pull-mplex')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const PeerBook = require('peer-book')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const path = require('path')
|
||||
const Switch = require('./src/switch')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
|
||||
const rawPeer = require('./test/fixtures/test-peer.json')
|
||||
const Node = require('./test/utils/bundle-nodejs.js')
|
||||
const {
|
||||
getPeerRelay,
|
||||
WRTC_RENDEZVOUS_MULTIADDR,
|
||||
WS_RENDEZVOUS_MULTIADDR
|
||||
} = require('./test/utils/constants')
|
||||
|
||||
let wrtcRendezvous
|
||||
let wsRendezvous
|
||||
let node
|
||||
let peerInfo
|
||||
let switchA
|
||||
let switchB
|
||||
|
||||
const before = (done) => {
|
||||
parallel([
|
||||
(cb) => {
|
||||
sigServer.start({
|
||||
port: 15555
|
||||
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
|
||||
}, (err, server) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
function echo (protocol, conn) { pull(conn, conn) }
|
||||
function idJSON (id) {
|
||||
const p = path.join(__dirname, `./test/switch/test-data/id-${id}.json`)
|
||||
return require(p)
|
||||
}
|
||||
wrtcRendezvous = server
|
||||
cb()
|
||||
|
||||
function createSwitchA () {
|
||||
return new Promise((resolve, reject) => {
|
||||
PeerId.createFromJSON(idJSON(1), (err, id) => {
|
||||
if (err) { return reject(err) }
|
||||
|
||||
const peerA = new PeerInfo(id)
|
||||
const maA = '/ip4/127.0.0.1/tcp/15337/ws'
|
||||
|
||||
peerA.multiaddrs.add(maA)
|
||||
const sw = new Switch(peerA, new PeerBook())
|
||||
|
||||
sw.transport.add('ws', new WebSockets())
|
||||
sw.start((err) => {
|
||||
if (err) { return reject(err) }
|
||||
resolve(sw)
|
||||
})
|
||||
},
|
||||
(cb) => {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function createSwitchB () {
|
||||
return new Promise((resolve, reject) => {
|
||||
PeerId.createFromJSON(idJSON(2), (err, id) => {
|
||||
if (err) { return reject(err) }
|
||||
|
||||
const peerB = new PeerInfo(id)
|
||||
const maB = '/ip4/127.0.0.1/tcp/15347/ws'
|
||||
|
||||
peerB.multiaddrs.add(maB)
|
||||
const sw = new Switch(peerB, new PeerBook())
|
||||
|
||||
sw.transport.add('ws', new WebSockets())
|
||||
sw.connection.addStreamMuxer(mplex)
|
||||
sw.connection.addStreamMuxer(spdy)
|
||||
sw.connection.reuse()
|
||||
sw.handle('/echo/1.0.0', echo)
|
||||
sw.start((err) => {
|
||||
if (err) { return reject(err) }
|
||||
resolve(sw)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const before = async () => {
|
||||
[
|
||||
wrtcRendezvous,
|
||||
wsRendezvous,
|
||||
peerInfo,
|
||||
switchA,
|
||||
switchB
|
||||
] = await Promise.all([
|
||||
sigServer.start({
|
||||
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
|
||||
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
|
||||
}),
|
||||
WebSocketStarRendezvous.start({
|
||||
port: 14444,
|
||||
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
|
||||
refreshPeerListIntervalMS: 1000,
|
||||
strictMultiaddr: false,
|
||||
cryptoChallenge: true
|
||||
}, (err, _server) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
wsRendezvous = _server
|
||||
cb()
|
||||
})
|
||||
},
|
||||
(cb) => {
|
||||
PeerId.createFromJSON(rawPeer, (err, peerId) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
const peer = new PeerInfo(peerId)
|
||||
|
||||
peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')
|
||||
}),
|
||||
getPeerRelay(),
|
||||
createSwitchA(),
|
||||
createSwitchB()
|
||||
])
|
||||
|
||||
node = new Node({
|
||||
peerInfo: peer
|
||||
})
|
||||
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
node.start(cb)
|
||||
})
|
||||
peerInfo,
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: true
|
||||
}
|
||||
], done)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
await node.start()
|
||||
}
|
||||
|
||||
const after = (done) => {
|
||||
setTimeout(() =>
|
||||
parallel(
|
||||
[node, wrtcRendezvous, wsRendezvous].map((s) => (cb) => s.stop(cb)),
|
||||
done),
|
||||
2000)
|
||||
const after = () => {
|
||||
return Promise.all([
|
||||
wrtcRendezvous.stop(),
|
||||
wsRendezvous.stop(),
|
||||
node.stop(),
|
||||
promisify(switchA.stop, { context: switchA })(),
|
||||
promisify(switchB.stop, { context: switchB })()
|
||||
])
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
bundlesize: { maxSize: '220kB' },
|
||||
hooks: {
|
||||
pre: before,
|
||||
post: after
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -9,6 +9,7 @@ logs
|
||||
*.log
|
||||
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
@ -41,3 +42,4 @@ test/test-data/go-ipfs-repo/LOG.old
|
||||
|
||||
# while testing npm5
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
|
37
.npmignore
37
.npmignore
@ -1,37 +0,0 @@
|
||||
**/node_modules/
|
||||
**/*.log
|
||||
test/repo-tests*
|
||||
img
|
||||
docs
|
||||
examples
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
coverage
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
build
|
||||
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
test
|
44
.travis.yml
Normal file
44
.travis.yml
Normal file
@ -0,0 +1,44 @@
|
||||
language: node_js
|
||||
cache: npm
|
||||
stages:
|
||||
- check
|
||||
- test
|
||||
- cov
|
||||
|
||||
node_js:
|
||||
- '10'
|
||||
- '12'
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
script: npx nyc -s npm run test:node -- --bail
|
||||
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: check
|
||||
script:
|
||||
- npx aegir build --bundlesize
|
||||
- npx aegir dep-check -- -i wrtc -i electron-webrtc
|
||||
- npm run lint
|
||||
|
||||
- stage: test
|
||||
name: chrome
|
||||
addons:
|
||||
chrome: stable
|
||||
script:
|
||||
- npx aegir test -t browser
|
||||
- npx aegir test -t webworker
|
||||
|
||||
- stage: test
|
||||
name: firefox
|
||||
addons:
|
||||
firefox: latest
|
||||
script:
|
||||
- npx aegir test -t browser -- --browsers FirefoxHeadless
|
||||
- npx aegir test -t webworker -- --browsers FirefoxHeadless
|
||||
|
||||
notifications:
|
||||
email: false
|
278
CHANGELOG.md
278
CHANGELOG.md
@ -1,3 +1,281 @@
|
||||
<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)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add createLibp2p to generate a PeerInfo instance ([#367](https://github.com/libp2p/js-libp2p/issues/367)) ([04faf18](https://github.com/libp2p/js-libp2p/commit/04faf18))
|
||||
* pass libp2p as option to transport creation ([#363](https://github.com/libp2p/js-libp2p/issues/363)) ([b06ca1b](https://github.com/libp2p/js-libp2p/commit/b06ca1b))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.3"></a>
|
||||
## [0.25.3](https://github.com/libp2p/js-libp2p/compare/v0.25.2...v0.25.3) (2019-05-07)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* sign pubsub messages ([#362](https://github.com/libp2p/js-libp2p/issues/362)) ([40978a1](https://github.com/libp2p/js-libp2p/commit/40978a1))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.2"></a>
|
||||
## [0.25.2](https://github.com/libp2p/js-libp2p/compare/v0.25.1...v0.25.2) (2019-04-17)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* dht config ([#359](https://github.com/libp2p/js-libp2p/issues/359)) ([f3801f0](https://github.com/libp2p/js-libp2p/commit/f3801f0))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.1"></a>
|
||||
## [0.25.1](https://github.com/libp2p/js-libp2p/compare/v0.25.0...v0.25.1) (2019-04-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* bail when discovering self ([#357](https://github.com/libp2p/js-libp2p/issues/357)) ([f28dffb](https://github.com/libp2p/js-libp2p/commit/f28dffb))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0"></a>
|
||||
# [0.25.0](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.6...v0.25.0) (2019-04-12)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* allow switch to be configured ([#354](https://github.com/libp2p/js-libp2p/issues/354)) ([eb5aa03](https://github.com/libp2p/js-libp2p/commit/eb5aa03))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.6"></a>
|
||||
# [0.25.0-rc.6](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.5...v0.25.0-rc.6) (2019-04-11)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* connection emits ([#352](https://github.com/libp2p/js-libp2p/issues/352)) ([313b1ea](https://github.com/libp2p/js-libp2p/commit/313b1ea))
|
||||
* remove unneeded peerbook puts ([#348](https://github.com/libp2p/js-libp2p/issues/348)) ([e5f19e8](https://github.com/libp2p/js-libp2p/commit/e5f19e8))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* auto dial discovered peers ([#349](https://github.com/libp2p/js-libp2p/issues/349)) ([01aa447](https://github.com/libp2p/js-libp2p/commit/01aa447))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.5"></a>
|
||||
# [0.25.0-rc.5](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.4...v0.25.0-rc.5) (2019-03-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* disable dht by default [#338](https://github.com/libp2p/js-libp2p/issues/338) ([#339](https://github.com/libp2p/js-libp2p/issues/339)) ([e52ce66](https://github.com/libp2p/js-libp2p/commit/e52ce66))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* update to the latest switch ([#336](https://github.com/libp2p/js-libp2p/issues/336)) ([eee60ed](https://github.com/libp2p/js-libp2p/commit/eee60ed))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.4"></a>
|
||||
# [0.25.0-rc.4](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.3...v0.25.0-rc.4) (2019-03-06)
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.3"></a>
|
||||
# [0.25.0-rc.3](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.2...v0.25.0-rc.3) (2019-02-26)
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.2"></a>
|
||||
# [0.25.0-rc.2](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.1...v0.25.0-rc.2) (2019-02-26)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* make the config less restrictive ([#329](https://github.com/libp2p/js-libp2p/issues/329)) ([5f92acd](https://github.com/libp2p/js-libp2p/commit/5f92acd))
|
||||
|
||||
|
||||
|
||||
<a name="0.25.0-rc.1"></a>
|
||||
# [0.25.0-rc.1](https://github.com/libp2p/js-libp2p/compare/v0.25.0-rc.0...v0.25.0-rc.1) (2019-02-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* bundle-size ([#298](https://github.com/libp2p/js-libp2p/issues/298)) ([d497961](https://github.com/libp2p/js-libp2p/commit/d497961))
|
||||
* emit peer discovery for dht discovery ([9e7a080](https://github.com/libp2p/js-libp2p/commit/9e7a080))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* support unsubscribe all for pubsub ([#321](https://github.com/libp2p/js-libp2p/issues/321)) ([6e76aad](https://github.com/libp2p/js-libp2p/commit/6e76aad))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.4"></a>
|
||||
## [0.24.4](https://github.com/libp2p/js-libp2p/compare/v0.24.3...v0.24.4) (2019-01-04)
|
||||
|
||||
|
||||
|
||||
<a name="0.24.3"></a>
|
||||
## [0.24.3](https://github.com/libp2p/js-libp2p/compare/v0.24.2...v0.24.3) (2018-12-14)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* not started yet ([#297](https://github.com/libp2p/js-libp2p/issues/297)) ([fdfb7b4](https://github.com/libp2p/js-libp2p/commit/fdfb7b4))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.2"></a>
|
||||
## [0.24.2](https://github.com/libp2p/js-libp2p/compare/v0.24.1...v0.24.2) (2018-12-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* use symbol instead of constructor name ([#292](https://github.com/libp2p/js-libp2p/issues/292)) ([53ed3bd](https://github.com/libp2p/js-libp2p/commit/53ed3bd))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.1"></a>
|
||||
## [0.24.1](https://github.com/libp2p/js-libp2p/compare/v0.24.0...v0.24.1) (2018-12-03)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* allow configurable validators and selectors to the dht ([#288](https://github.com/libp2p/js-libp2p/issues/288)) ([7d12eb9](https://github.com/libp2p/js-libp2p/commit/7d12eb9))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.0"></a>
|
||||
# [0.24.0](https://github.com/libp2p/js-libp2p/compare/v0.24.0-rc.3...v0.24.0) (2018-11-16)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add maxtimeout to dht get ([#248](https://github.com/libp2p/js-libp2p/issues/248)) ([69f7264](https://github.com/libp2p/js-libp2p/commit/69f7264))
|
||||
* dht get options ([4460e82](https://github.com/libp2p/js-libp2p/commit/4460e82))
|
||||
* dont call callback before it's properly set ([17b5f73](https://github.com/libp2p/js-libp2p/commit/17b5f73))
|
||||
* improve get peer info errors ([714b6ec](https://github.com/libp2p/js-libp2p/commit/714b6ec))
|
||||
* start kad dht random walk ([#251](https://github.com/libp2p/js-libp2p/issues/251)) ([dd934b9](https://github.com/libp2p/js-libp2p/commit/dd934b9))
|
||||
|
||||
### Features
|
||||
|
||||
* add datastore to config ([40e840d](https://github.com/libp2p/js-libp2p/commit/40e840d))
|
||||
* add delegated peer and content routing support ([#242](https://github.com/libp2p/js-libp2p/issues/242)) ([a95389a](https://github.com/libp2p/js-libp2p/commit/a95389a))
|
||||
* add maxNumProviders to findprovs ([#283](https://github.com/libp2p/js-libp2p/issues/283)) ([970deec](https://github.com/libp2p/js-libp2p/commit/970deec))
|
||||
* conditionally emit errors ([f71fdfd](https://github.com/libp2p/js-libp2p/commit/f71fdfd))
|
||||
* enable relay by default (no hop) ([#254](https://github.com/libp2p/js-libp2p/issues/254)) ([686379e](https://github.com/libp2p/js-libp2p/commit/686379e))
|
||||
* make libp2p a state machine ([#257](https://github.com/libp2p/js-libp2p/issues/257)) ([0b75f99](https://github.com/libp2p/js-libp2p/commit/0b75f99))
|
||||
* use package-table vs custom script ([a63432e](https://github.com/libp2p/js-libp2p/commit/a63432e))
|
||||
|
||||
<a name="0.23.1"></a>
|
||||
## [0.23.1](https://github.com/libp2p/js-libp2p/compare/v0.23.0...v0.23.1) (2018-08-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* callback with error for invalid or non-peer multiaddr ([#232](https://github.com/libp2p/js-libp2p/issues/232)) ([c8a86db](https://github.com/libp2p/js-libp2p/commit/c8a86db))
|
||||
|
||||
|
||||
|
||||
<a name="0.23.0"></a>
|
||||
# [0.23.0](https://github.com/libp2p/js-libp2p/compare/v0.22.0...v0.23.0) (2018-07-27)
|
||||
|
||||
|
@ -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.
|
||||
|
||||
|
@ -39,5 +39,5 @@ One of following:
|
||||
This is for you! Please read, and then delete this text before posting it.
|
||||
The js-ipfs issues are only for bug reports and directly actionable features.
|
||||
|
||||
Read https://github.com/ipfs/community/blob/master/contributing.md#reporting-issues if your issue doesn't fit either of those categories.
|
||||
Read https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#reporting-issues if your issue doesn't fit either of those categories.
|
||||
-->
|
||||
|
36
MGMT.md
36
MGMT.md
@ -1,36 +0,0 @@
|
||||
# Core Dev Team Work Tracking & Managment
|
||||
|
||||
## How work gets organized (a tl;dr;)
|
||||
|
||||
The js-ipfs core working group follows the OKR structure established for the IPFS project to set the quarterly targets. Within each quarter, work gets tracked using Github and Waffle.
|
||||
|
||||
- Github is used for discussions and track current endeavours.
|
||||
- Waffle gives us a [Kanban](https://en.wikipedia.org/wiki/Kanban) view over the work at hand.
|
||||
|
||||

|
||||
|
||||
In the Waffle board, we have 4 columns:
|
||||
|
||||
- **Inbox** - New issues or PRs that haven't been evaluated yet
|
||||
- **Backlog** - Issues that are blocked or discussion threads that are not currently active
|
||||
- **Ready** - Issues Ready to be worked on
|
||||
- **In Progress** - Issues that someone is already tackling. Contributors should focus on a few things rather than many at once.
|
||||
- **Done** - Issues are automatically moved here when the issue is closed or the PR merged.
|
||||
|
||||
We track work for the JavaScript implementation of the IPFS protocol in 3 separate waffle boards:
|
||||
|
||||
- [js-ipfs](http://waffle.io/ipfs/js-ipfs)
|
||||
- [js-libp2p](http://waffle.io/libp2p/js-libp2p)
|
||||
- [js-ipld](http://waffle.io/ipld/js-ipld)
|
||||
|
||||
## Issue labels and how to use filters
|
||||
|
||||
We use labels to tag urgency and the difficulty of an issue. The current label system has:
|
||||
|
||||
- `difficulty:{easy, moderate, hard}` - This is an instinctive measure give by the project lead or leads. It is a subjective best guess, however the current golden rule is that an issue with difficulty:easy should not require more than a morning (3~4 hours) to do and it should not require having to mess with multiple modules to complete. Issues with difficulty moderate or hard might require some discussion around the problem or even request that another team (i.e go-ipfs) makes some changes. The length of moderate or hard issue might be a day to ad-aeternum.
|
||||
- `priority (P0, P1, P2, P3, P4)` - P0 is the most important while P4 is the least.
|
||||
- `help wanted` - Issues perfect for new contributors. They will have the information necessary or the pointers for a new contributor to figure out what is required. These issues are never blocked on some other issue be done first.
|
||||
|
||||
## Weekly Core Dev Team Calls
|
||||
|
||||
[⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽](https://github.com/ipfs/pm/issues/650)
|
10
OKR.md
10
OKR.md
@ -2,9 +2,17 @@
|
||||
|
||||
We try to frame our ongoing work using a process based on quarterly Objectives and Key Results (OKRs). Objectives reflect outcomes that are challenging, but realistic. Results are tangible and measurable.
|
||||
|
||||
## 2019 Q1
|
||||
|
||||
Find the js-libp2p OKRs for 2019 Q1 at the [2019 Q1 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/11GKG1DBRIIAiQnHvLD7_IqWxDGsVdaZFpxJM6NWtXe8/edit#gid=1271182838)
|
||||
|
||||
## 2018 Q4
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q4 at the [2018 Q4 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1BYwmbVicgo6_tOHAbgiUXWge8Ej0qR1M_gAUulazmrg/edit#gid=1241853194)
|
||||
|
||||
## 2018 Q3
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q2 at the [2018 Q3 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1HTXfgR5FyPTFhsTkFPRThkeMvHvCgJOaAs7BSl_vQ_0/edit#gid=1241853194)
|
||||
Find the js-libp2p OKRs for 2018 Q3 at the [2018 Q3 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1HTXfgR5FyPTFhsTkFPRThkeMvHvCgJOaAs7BSl_vQ_0/edit#gid=1241853194)
|
||||
|
||||
## Previous Quarters
|
||||
|
||||
|
60
PEER_DISCOVERY.md
Normal file
60
PEER_DISCOVERY.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Peer Discovery and Auto Dial
|
||||
|
||||
**Synopsis**:
|
||||
* All peers discovered are emitted via `peer:discovery` so applications can take any desired action.
|
||||
* Libp2p defaults to automatically connecting to new peers, when under the [ConnectionManager](https://github.com/libp2p/js-libp2p-connection-manager) low watermark (minimum peers).
|
||||
* Applications can disable this via the `peerDiscovery.autoDial` config property, and handle connections themselves.
|
||||
* Applications who have not disabled this should **never** connect on peer discovery. Applications should use the `peer:connect` event if they wish to take a specific action on new peers.
|
||||
|
||||
## Scenarios
|
||||
In any scenario, if a peer is discovered it should be added to the PeerBook. This ensures that even if we don't dial to a node when we discover it, we know about it in the event that it becomes known as a provider for something we need. The scenarios listed below detail what actions the auto dialer will take when peers are discovered.
|
||||
|
||||
### 1. Joining the network
|
||||
The node is new and needs to join the network. It currently has 0 peers.
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery)
|
||||
|
||||
### Action to take
|
||||
Connect to discovered peers. This should have some degree of concurrency limiting. While the case should be low, if we immediately discover more peers than our high watermark we should avoid dialing them all.
|
||||
|
||||
### 2. Connected to some
|
||||
The node is connected to other nodes. The current number of connections is less than the desired low watermark.
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
Connect to discovered peers. This should have some degree of concurrency limiting. The concurrency may need to be modified to reflect the current number of peers connected. The more peers we have, the lower the concurrency may need to be.
|
||||
|
||||
### 3. Connected to enough
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
None. If we are connected to enough peers, the low watermark, we should not connect to discovered peers. As other peers discover us, they may connect to us based on their current scenario.
|
||||
|
||||
For example, a long running node with adequate peers is on an MDNS network. A new peer joins the network and both become aware of each other. The new peer should be the peer that dials, as it has too few peers. The existing node has no reason to dial the new peer, but should keep a record of it in case it later becomes an important node due to its contents/capabilities.
|
||||
|
||||
Avoiding dials above the low watermark also allows for a pool of connections to be reserved for application specific actions, such as connecting to a specific content provider via a DHT query to find that content (ipfs-bitswap).
|
||||
|
||||
### 4. Connected to too many
|
||||
The node has more connections than it wants. The current number of connections is greater than the high watermark.
|
||||
|
||||
[WIP Connection Manager v2 spec](https://github.com/libp2p/specs/pull/161)
|
||||
**Discovery Mechanisms**: [Ambient Discovery](#ambient-discovery) and [Active Discovery](#active-discovery)
|
||||
|
||||
### Action to take
|
||||
None, the `ConnectionManager` will automatically prune connections.
|
||||
|
||||
## Discovery Mechanisms
|
||||
Means of which a libp2p node discovers other peers.
|
||||
|
||||
### Active Discovery
|
||||
Through active use of the libp2p network, a node may discovery peers.
|
||||
|
||||
* Content/Peer routing (DHT, delegated, etc) provider and peer queries
|
||||
* DHT random walk
|
||||
* Rendezvous servers
|
||||
|
||||
### Ambient Discovery
|
||||
Leveraging known addresses, or network discovery mechanisms, a node may discover peers outside of the bounds of the libp2p network.
|
||||
|
||||
* Bootstrap
|
||||
* MDNS
|
||||
* proximity based (bluetooth, sound, etc)
|
307
README.md
307
README.md
@ -8,12 +8,14 @@
|
||||
<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://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">
|
||||
<a href="https://ci.ipfs.team/job/libp2p/job/js-libp2p/job/master/"><img src="https://ci.ipfs.team/buildStatus/icon?job=libp2p/js-libp2p/master" /></a>
|
||||
<a href="https://codecov.io/gh/libp2p/js-libp2p"><img src="https://codecov.io/gh/libp2p/js-libp2p/branch/master/graph/badge.svg"></a>
|
||||
<a href="https://travis-ci.com/libp2p/js-libp2p"><img src="https://flat.badgen.net/travis/libp2p/js-libp2p" /></a>
|
||||
<a href="https://codecov.io/gh/libp2p/js-libp2p"><img src="https://img.shields.io/codecov/c/github/ipfs/js-ipfs-multipart/master.svg?style=flat-square"></a>
|
||||
<a href="https://bundlephobia.com/result?p=ipfsd-ctl"><img src="https://flat.badgen.net/bundlephobia/minzip/ipfsd-ctl"></a>
|
||||
<br>
|
||||
<a href="https://david-dm.org/libp2p/js-libp2p"><img src="https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" /></a>
|
||||
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
|
||||
@ -39,7 +41,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[David Dias](https://github.com/diasdavid/)
|
||||
[Jacob Heun](https://github.com/jacobheun/)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@ -49,6 +51,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [API](#api)
|
||||
- [Events](#events)
|
||||
- [Development](#development)
|
||||
- [Tests](#tests)
|
||||
- [Packages](#packages)
|
||||
@ -62,7 +65,9 @@ libp2p is the product of a long and arduous quest to understand the evolution of
|
||||
We are in the process of writing better documentation, blog posts, tutorials and a formal specification. Today you can find:
|
||||
|
||||
- [libp2p.io](https://libp2p.io)
|
||||
- [docs.libp2p.io](https://docs.libp2p.io)
|
||||
- [Specification (WIP)](https://github.com/libp2p/specs)
|
||||
- [Discussion Forums](https://discuss.libp2p.io)
|
||||
- Talks
|
||||
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [📼 video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [📼 demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [📼 demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
|
||||
- Articles
|
||||
@ -89,13 +94,15 @@ 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.
|
||||
|
||||
### Extending libp2p skeleton
|
||||
### Creating your own libp2p bundle
|
||||
|
||||
libp2p becomes very simple and basically acts as a glue for every module that compose this library. Since it can be highly customized, it requires some setup. What we recommend is to have a libp2p build for the system you are developing taking into account in your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built and minified version that browsers can require).
|
||||
The libp2p module acts as a glue for every libp2p module that you can use to create your own libp2p bundle. Creating your own libp2p bundle gives you a lot of freedom when it comes to customize it with features and default setup. We recommend creating your own libp2p bundle for the app you are developing that takes in account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built one that leverages the Browser transports).
|
||||
|
||||
**Example:**
|
||||
|
||||
@ -106,7 +113,7 @@ libp2p becomes very simple and basically acts as a glue for every module that co
|
||||
// crypto-channel: secio
|
||||
// discovery: multicast-dns
|
||||
|
||||
const libp2p = require('libp2p')
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
@ -114,15 +121,16 @@ 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')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_peerInfo, _peerBook, _options) {
|
||||
class Node extends Libp2p {
|
||||
constructor (_options) {
|
||||
const peerInfo = _options.peerInfo
|
||||
const defaults = {
|
||||
peerInfo: _peerInfo, // The Identity of your Peer
|
||||
peerBook: _peerBook, // Where peers get tracked, if undefined libp2p will create one instance
|
||||
|
||||
// The libp2p modules for this libp2p bundle
|
||||
modules: {
|
||||
transport: [
|
||||
@ -136,44 +144,58 @@ class Node extends libp2p {
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
connProtector: new Protector(/*protector specific opts*/),
|
||||
/** Encryption for private networks. Needs additional private key to work **/
|
||||
// connProtector: new Protector(/*protector specific opts*/),
|
||||
/** Enable custom content routers, such as delegated routing **/
|
||||
// contentRouting: [
|
||||
// new DelegatedContentRouter(peerInfo.id)
|
||||
// ],
|
||||
/** Enable custom peer routers, such as delegated routing **/
|
||||
// peerRouting: [
|
||||
// new DelegatedPeerRouter()
|
||||
// ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS
|
||||
],
|
||||
peerRouting: {}, // Currently both peerRouting and contentRouting are patched through the DHT,
|
||||
contentRouting: {}, // this will change once we factor that into two modules, for now do the following line:
|
||||
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)
|
||||
config: { // The config object is the part of the config that can go into a file, config.json.
|
||||
peerDiscovery: {
|
||||
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
|
||||
mdns: { // mdns options
|
||||
interval: 1000 // ms
|
||||
interval: 1000, // ms
|
||||
enabled: true
|
||||
},
|
||||
webrtcStar: { // webrtc-star options
|
||||
interval: 1000 // ms
|
||||
interval: 1000, // ms
|
||||
enabled: false
|
||||
}
|
||||
// .. other discovery module options.
|
||||
},
|
||||
peerRouting: {},
|
||||
contentRouting: {},
|
||||
relay: { // Circuit Relay options
|
||||
enabled: false,
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
kBucketSize: 20,
|
||||
enabled: true,
|
||||
randomWalk: {
|
||||
enabled: true, // Allows to disable discovery (enabled by default)
|
||||
interval: 300e3,
|
||||
timeout: 10e3
|
||||
}
|
||||
},
|
||||
// Enable/Disable Experimental features
|
||||
EXPERIMENTAL: { // Experimental features ("behind a flag")
|
||||
pubsub: false,
|
||||
dht: 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -188,34 +210,52 @@ class Node extends libp2p {
|
||||
|
||||
### API
|
||||
|
||||
#### Create a Node - `new libp2p.Node([peerInfo, peerBook, options])`
|
||||
**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.
|
||||
|
||||
> Creates an instance of the libp2p.Node.
|
||||
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node. Optional.
|
||||
- `peerBook`: instance of [PeerBook][] that contains the [PeerInfo][] of known peers. Optional.
|
||||
- `options`: Object containing custom options for the bundle.
|
||||
> 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
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- `options`: Object of libp2p configuration options
|
||||
- `callback`: Function with signature `function (Error, Libp2p) {}`
|
||||
|
||||
#### Create a Node alternative - `new Libp2p(options)`
|
||||
|
||||
> Creates an instance of Libp2p with a custom `PeerInfo` provided via `options.peerInfo`.
|
||||
|
||||
Required keys in the `options` object:
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
|
||||
- `modules.transport`: An array that must include at least 1 transport, such as `libp2p-tcp`.
|
||||
|
||||
#### `libp2p.start(callback)`
|
||||
|
||||
> Start the libp2p Node.
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case starting the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case starting the node fails.
|
||||
|
||||
#### `libp2p.stop(callback)`
|
||||
|
||||
> Stop the libp2p Node.
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.dial(peer, callback)`
|
||||
|
||||
> Dials to another peer in the network, establishes the connection.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
|
||||
`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
- `callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
|
||||
#### `libp2p.dialProtocol(peer, protocol, callback)`
|
||||
|
||||
@ -223,9 +263,17 @@ class Node extends libp2p {
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `callback`: Function with signature `function (err, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
|
||||
`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
`callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||
|
||||
#### `libp2p.dialFSM(peer, protocol, callback)`
|
||||
|
||||
> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: following signature `function (err, connFSM) {}`, where `connFSM` is a [Connection State Machine](https://github.com/libp2p/js-libp2p-switch#connection-state-machine)
|
||||
|
||||
#### `libp2p.hangUp(peer, callback)`
|
||||
|
||||
@ -233,30 +281,33 @@ class Node extends libp2p {
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.peerRouting.findPeer(id, callback)`
|
||||
#### `libp2p.peerRouting.findPeer(id, options, callback)`
|
||||
|
||||
> Looks up for multiaddrs of a peer in the DHT
|
||||
|
||||
- `id`: instance of [PeerId][]
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.contentRouting.findProviders(key, timeout, callback)`
|
||||
#### `libp2p.contentRouting.findProviders(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `timeout`: Number miliseconds
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
- `options.maxNumProviders` maximum number of providers to find
|
||||
|
||||
#### `libp2p.contentRouting.provide(key, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
|
||||
|
||||
#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`
|
||||
|
||||
> Handle new protocol
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `handlerFunc`: Function with signature `function (protocol, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `handlerFunc`: following signature `function (protocol, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.
|
||||
|
||||
#### `libp2p.unhandle(protocol)`
|
||||
@ -265,21 +316,52 @@ class Node extends libp2p {
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
|
||||
#### `libp2p.on('peer:discovery', (peer) => {})`
|
||||
#### Events
|
||||
|
||||
##### `libp2p.on('start', () => {})`
|
||||
|
||||
> Libp2p has started, along with all its services.
|
||||
|
||||
##### `libp2p.on('stop', () => {})`
|
||||
|
||||
> Libp2p has stopped, along with all its services.
|
||||
|
||||
##### `libp2p.on('error', (err) => {})`
|
||||
|
||||
> An error has occurred
|
||||
|
||||
- `err`: instance of `Error`
|
||||
|
||||
##### `libp2p.on('peer:discovery', (peer) => {})`
|
||||
|
||||
> Peer has been discovered.
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
#### `libp2p.on('peer:connect', (peer) => {})`
|
||||
|
||||
> We connected to a new peer
|
||||
If `autoDial` is `true`, applications should **not** attempt to connect to the peer
|
||||
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
#### `libp2p.on('peer:disconnect', (peer) => {})`
|
||||
##### `libp2p.on('peer:connect', (peer) => {})`
|
||||
|
||||
> We disconnected from Peer
|
||||
> We have a new muxed connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('peer:disconnect', (peer) => {})`
|
||||
|
||||
> We have closed a connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('connection:start', (peer) => {})`
|
||||
|
||||
> We created a new connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
##### `libp2p.on('connection:end', (peer) => {})`
|
||||
|
||||
> We closed a connection to a peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
@ -312,14 +394,18 @@ class Node extends libp2p {
|
||||
- `key`: Buffer
|
||||
- `value`: Buffer
|
||||
|
||||
#### `libp2p.dht.get(key, callback)`
|
||||
#### `libp2p.dht.get(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.dht.getMany(key, nVals, callback)`
|
||||
#### `libp2p.dht.getMany(key, nVals, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `nVals`: Number
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
[PeerInfo]: https://github.com/libp2p/js-peer-info
|
||||
[PeerId]: https://github.com/libp2p/js-peer-id
|
||||
@ -469,86 +555,75 @@ Some available network protectors:
|
||||
> npm run test:browser
|
||||
```
|
||||
|
||||
#### Run interop tests
|
||||
|
||||
```sh
|
||||
N/A
|
||||
```
|
||||
|
||||
#### Run benchmark tests
|
||||
|
||||
```sh
|
||||
N/A
|
||||
```
|
||||
|
||||
### Packages
|
||||
|
||||
List of packages currently in existence for libp2p
|
||||
|
||||
| Package | Version | Deps | CI | Coverage |
|
||||
| ---------|---------|---------|---------|--------- |
|
||||
> This table is generated using the module `package-table` with `package-table --data=package-list.json`.
|
||||
|
||||
| Package | Version | Deps | CI | Coverage | Lead Maintainer |
|
||||
| ---------|---------|---------|---------|---------|--------- |
|
||||
| **Libp2p** |
|
||||
| [`interface-libp2p`](//github.com/libp2p/interface-libp2p) | [](//github.com/libp2p/interface-libp2p/releases) | [](https://david-dm.org/libp2p/interface-libp2p) | [](https://ci.ipfs.team/job/libp2p/job/interface-libp2p/job/master/) | [](https://codecov.io/gh/libp2p/interface-libp2p) |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p) |
|
||||
| [`interface-libp2p`](//github.com/libp2p/interface-libp2p) | [](//github.com/libp2p/interface-libp2p/releases) | [](https://david-dm.org/libp2p/interface-libp2p) | [](https://travis-ci.com/libp2p/interface-libp2p) | [](https://codecov.io/gh/libp2p/interface-libp2p) | N/A |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://travis-ci.com/libp2p/js-libp2p) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Connection** |
|
||||
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://ci.ipfs.team/job/libp2p/job/interface-connection/job/master/) | [](https://codecov.io/gh/libp2p/interface-connection) |
|
||||
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://travis-ci.com/libp2p/interface-connection) | [](https://codecov.io/gh/libp2p/interface-connection) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Transport** |
|
||||
| [`interface-transport`](//github.com/libp2p/interface-transport) | [](//github.com/libp2p/interface-transport/releases) | [](https://david-dm.org/libp2p/interface-transport) | [](https://ci.ipfs.team/job/libp2p/job/interface-transport/job/master/) | [](https://codecov.io/gh/libp2p/interface-transport) |
|
||||
| [`libp2p-circuit`](//github.com/libp2p/js-libp2p-circuit) | [](//github.com/libp2p/js-libp2p-circuit/releases) | [](https://david-dm.org/libp2p/js-libp2p-circuit) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-circuit/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-circuit) |
|
||||
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-tcp/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) |
|
||||
| [`libp2p-udp`](//github.com/libp2p/js-libp2p-udp) | [](//github.com/libp2p/js-libp2p-udp/releases) | [](https://david-dm.org/libp2p/js-libp2p-udp) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-udp/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-udp) |
|
||||
| [`libp2p-udt`](//github.com/libp2p/js-libp2p-udt) | [](//github.com/libp2p/js-libp2p-udt/releases) | [](https://david-dm.org/libp2p/js-libp2p-udt) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-udt/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-udt) |
|
||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-utp/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-utp) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-direct/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-websockets/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-websocket-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-websocket-star-rendezvous/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star-rendezvous) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-secio/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-secio) |
|
||||
| [`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** |
|
||||
| [`interface-stream-muxer`](//github.com/libp2p/interface-stream-muxer) | [](//github.com/libp2p/interface-stream-muxer/releases) | [](https://david-dm.org/libp2p/interface-stream-muxer) | [](https://ci.ipfs.team/job/libp2p/job/interface-stream-muxer/job/master/) | [](https://codecov.io/gh/libp2p/interface-stream-muxer) |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-mplex/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-spdy/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) |
|
||||
| [`interface-stream-muxer`](//github.com/libp2p/interface-stream-muxer) | [](//github.com/libp2p/interface-stream-muxer/releases) | [](https://david-dm.org/libp2p/interface-stream-muxer) | [](https://travis-ci.com/libp2p/interface-stream-muxer) | [](https://codecov.io/gh/libp2p/interface-stream-muxer) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://travis-ci.com/libp2p/js-libp2p-spdy) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Discovery** |
|
||||
| [`interface-peer-discovery`](//github.com/libp2p/interface-peer-discovery) | [](//github.com/libp2p/interface-peer-discovery/releases) | [](https://david-dm.org/libp2p/interface-peer-discovery) | [](https://ci.ipfs.team/job/libp2p/job/interface-peer-discovery/job/master/) | [](https://codecov.io/gh/libp2p/interface-peer-discovery) |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-bootstrap/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-mdns/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) |
|
||||
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-rendezvous/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-websocket-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) |
|
||||
| [`interface-peer-discovery`](//github.com/libp2p/interface-peer-discovery) | [](//github.com/libp2p/interface-peer-discovery/releases) | [](https://david-dm.org/libp2p/interface-peer-discovery) | [](https://travis-ci.com/libp2p/interface-peer-discovery) | [](https://codecov.io/gh/libp2p/interface-peer-discovery) | N/A |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://travis-ci.com/libp2p/js-libp2p-bootstrap) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://travis-ci.com/libp2p/js-libp2p-mdns) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [](https://travis-ci.com/libp2p/js-libp2p-rendezvous) | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | N/A |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **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://ci.ipfs.team/job/libp2p/job/js-libp2p-circuit/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-circuit) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-nat-mngr/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mngr) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-peer-book/job/master/) | [](https://codecov.io/gh/libp2p/js-peer-book) |
|
||||
| [`peer-id`](//github.com/libp2p/js-peer-id) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://ci.ipfs.team/job/libp2p/job/js-peer-id/job/master/) | [](https://codecov.io/gh/libp2p/js-peer-id) |
|
||||
| [`peer-info`](//github.com/libp2p/js-peer-info) | [](//github.com/libp2p/js-peer-info/releases) | [](https://david-dm.org/libp2p/js-peer-info) | [](https://ci.ipfs.team/job/libp2p/job/js-peer-info/job/master/) | [](https://codecov.io/gh/libp2p/js-peer-info) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/interface-content-routing/job/master/) | [](https://codecov.io/gh/libp2p/interface-content-routing) |
|
||||
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-delegated-content-routing/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) |
|
||||
| [`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) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **Peer Routing** |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/interface-peer-routing/job/master/) | [](https://codecov.io/gh/libp2p/interface-peer-routing) |
|
||||
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-delegated-peer-routing/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/interface-record-store/job/master/) | [](https://codecov.io/gh/libp2p/interface-record-store) |
|
||||
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [](//github.com/libp2p/js-libp2p-record/releases) | [](https://david-dm.org/libp2p/js-libp2p-record) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-record/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-record) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-connection-manager/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-connection-manager) |
|
||||
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-crypto/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-libp2p-crypto-secp256k1/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) |
|
||||
| [`libp2p-switch`](//github.com/libp2p/js-libp2p-switch) | [](//github.com/libp2p/js-libp2p-switch/releases) | [](https://david-dm.org/libp2p/js-libp2p-switch) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-switch/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-switch) |
|
||||
| [`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 |
|
||||
| [`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) |
|
||||
| **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://ci.ipfs.team/job/libp2p/job/js-libp2p-floodsub/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) |
|
||||
| [`libp2p-identify`](//github.com/libp2p/js-libp2p-identify) | [](//github.com/libp2p/js-libp2p-identify/releases) | [](https://david-dm.org/libp2p/js-libp2p-identify) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-identify/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-identify) |
|
||||
| [`libp2p-keychain`](//github.com/libp2p/js-libp2p-keychain) | [](//github.com/libp2p/js-libp2p-keychain/releases) | [](https://david-dm.org/libp2p/js-libp2p-keychain) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-keychain/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-keychain) |
|
||||
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [](//github.com/libp2p/js-libp2p-ping/releases) | [](https://david-dm.org/libp2p/js-libp2p-ping) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-ping/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-ping) |
|
||||
| [`libp2p-pnet`](//github.com/libp2p/js-libp2p-pnet) | [](//github.com/libp2p/js-libp2p-pnet/releases) | [](https://david-dm.org/libp2p/js-libp2p-pnet) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-pnet/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-pnet) |
|
||||
| [`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://ci.ipfs.team/job/libp2p/job/js-p2pcat/job/master/) | [](https://codecov.io/gh/libp2p/js-p2pcat) |
|
||||
| [`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
|
||||
|
||||
@ -560,4 +635,4 @@ The libp2p implementation in JavaScript is a work in progress. As such, there ar
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © David Dias
|
||||
[MIT](LICENSE) © Protocol Labs
|
||||
|
25
RELEASE.md
25
RELEASE.md
@ -13,6 +13,14 @@
|
||||
- Robustness and quality
|
||||
- [ ] Ensure that all tests are passing, this includes:
|
||||
- [ ] unit
|
||||
- [ ] Publish a release candidate to npm
|
||||
```sh
|
||||
# Minor prerelease (e.g. 0.24.1 -> 0.25.0-rc.0)
|
||||
$ npx aegir release --type preminor -t node -t browser --preid rc --dist-tag next
|
||||
|
||||
# Increment prerelease (e.g. 0.25.0-rc.0 -> 0.25.0-rc.1)
|
||||
$ npx aegir release --type prerelease -t node -t browser --preid rc --dist-tag next
|
||||
```
|
||||
- [ ] Run tests of the following projects with the new release:
|
||||
- [ ] [js-ipfs](https://github.com/ipfs/js-ipfs)
|
||||
- Documentation
|
||||
@ -20,22 +28,31 @@
|
||||
- [ ] Ensure that all the examples run
|
||||
- Communication
|
||||
- [ ] Create the release issue
|
||||
- [ ] Take a snapshot between of everyone that has contributed to this release (including its subdeps in IPFS, libp2p, IPLD and multiformats) using [`name-your-contributors`](https://www.npmjs.com/package/name-your-contributors). Generate a nice markdown list with [this script](https://gist.github.com/jacobheun/d2ff479ca991733c13cdcf688a1317e5)
|
||||
- [ ] Announcements (both pre-release and post-release)
|
||||
- [ ] Twitter
|
||||
- [ ] IRC
|
||||
- [ ] Reddit
|
||||
- [ ] [discuss.libp2p.io](https://discuss.libp2p.io/c/news)
|
||||
- [ ] Blog post
|
||||
- [ ] Copy release notes to the [GitHub Release description](https://github.com/libp2p/js-libp2p/releases)
|
||||
|
||||
# ❤️ Huge thank you to everyone that made this release possible
|
||||
|
||||
In alphabetical order, here are all the humans that contributed to the release:
|
||||
|
||||
- ...
|
||||
|
||||
# 🙌🏽 Want to contribute?
|
||||
|
||||
Would you like to contribute to the libp2p project and don't know how? Well, there are a few places you can get started:
|
||||
|
||||
- Check the issues with the `help wanted` label at the Ready column in our waffle board - https://waffle.io/libp2p/js-libp2p?label=help%20wanted
|
||||
- Join an IPFS All Hands, introduce yourself and let us know where you would like to contribute - https://github.com/ipfs/pm/#all-hands-call
|
||||
- Check the issues with the `help wanted` label in the [libp2p repo](https://github.com/libp2p/js-libp2p/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22)
|
||||
- Join an IPFS All Hands, introduce yourself and let us know where you would like to contribute - https://github.com/ipfs/team-mgmt#all-hands-call
|
||||
- Hack with IPFS and show us what you made! The All Hands call is also the perfect venue for demos, join in and show us what you built
|
||||
- Join the discussion at http://discuss.ipfs.io/ and help users finding their answers.
|
||||
- Join the [⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽 ](https://github.com/ipfs/pm/issues/650) and be part of the Sprint action!
|
||||
- Join the [⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽 ](https://github.com/ipfs/team-mgmt/issues/650) and be part of the Sprint action!
|
||||
|
||||
# ⁉️ Do you have questions?
|
||||
|
||||
The best place to ask your questions about libp2p, how it works and what you can do with it is at [discuss.ipfs.io](http://discuss.ipfs.io). We are also available at the #libp2p channel on Freenode.
|
||||
The best place to ask your questions about libp2p, how it works and what you can do with it is at [discuss.libp2p.io](https://discuss.libp2p.io). We are also available at the #libp2p channel on Freenode.
|
||||
|
29
appveyor.yml
29
appveyor.yml
@ -1,29 +0,0 @@
|
||||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
version: "{build}"
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: "6"
|
||||
- nodejs_version: "8"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
|
||||
install:
|
||||
# Install Node.js
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
||||
# Upgrade npm
|
||||
- npm install -g npm
|
||||
|
||||
# Output our current versions for debugging
|
||||
- node --version
|
||||
- npm --version
|
||||
|
||||
# Install our package dependencies
|
||||
- npm install
|
||||
|
||||
test_script:
|
||||
- npm run test:node
|
||||
|
||||
build: off
|
2
ci/Jenkinsfile
vendored
2
ci/Jenkinsfile
vendored
@ -1,2 +0,0 @@
|
||||
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
javascript()
|
22
circle.yml
22
circle.yml
@ -1,22 +0,0 @@
|
||||
machine:
|
||||
node:
|
||||
version: 8.11.3
|
||||
|
||||
test:
|
||||
post:
|
||||
- npm run coverage -- --upload --providers coveralls
|
||||
|
||||
dependencies:
|
||||
pre:
|
||||
- google-chrome --version
|
||||
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||||
- for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb
|
||||
- sudo dpkg -i google-chrome.deb || true
|
||||
- sudo dpkg -i libnss3*.deb || true
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -f || true
|
||||
- sudo dpkg -i libnss3*.deb
|
||||
- sudo apt-get install -f
|
||||
- sudo apt-get install --only-upgrade lsb-base
|
||||
- sudo dpkg -i google-chrome.deb
|
||||
- google-chrome --version
|
@ -10,8 +10,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
|
||||
- [Protocol and Stream Muxing](./protocol-and-stream-muxing)
|
||||
- [Encrypted Communications](./encrypted-communications)
|
||||
- [Discovery Mechanisms](./discovery-mechanisms)
|
||||
- [Peer Routing](./peer-and-content-routing)
|
||||
- [Content Routing](./peer-and-content-routing)
|
||||
- [Peer and Content Routing](./peer-and-content-routing)
|
||||
- [PubSub](./pubsub)
|
||||
- [NAT Traversal](./nat-traversal)
|
||||
- Circuit Relay (future)
|
||||
|
@ -3,11 +3,11 @@
|
||||
This example creates a simple chat app in your terminal.
|
||||
|
||||
## Setup
|
||||
1. Install the modules, `npm install`.
|
||||
2. Open 2 terminal windows in the `./src` directory.
|
||||
1. Install the modules in the libp2p root directory, `npm install`.
|
||||
2. Open 2 terminal windows in the `./examples/chat/src` directory.
|
||||
|
||||
## Running
|
||||
1. Run the listener in window 1, `node listener.js`
|
||||
2. Run the dialer in window 2, `node dialer.js`
|
||||
3. Type a message in either window and hit _enter_
|
||||
4. Tell youself secrets to your hearts content!
|
||||
4. Tell yourself secrets to your hearts content!
|
||||
|
@ -46,7 +46,7 @@ async.parallel([
|
||||
console.log('Dialer ready, listening on:')
|
||||
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
|
||||
nodeDialer.dialProtocol(peerListener, '/chat/1.0.0', (err, conn) => {
|
||||
|
@ -3,7 +3,7 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
|
@ -50,7 +50,7 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
peerListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
49
examples/delegated-routing/README.md
Normal file
49
examples/delegated-routing/README.md
Normal file
@ -0,0 +1,49 @@
|
||||
# Delegated Routing with Libp2p and IPFS
|
||||
|
||||
This example shows how to use delegated peer and content routing. The [Peer and Content Routing Example](../peer-and-content-routing) focuses
|
||||
on the DHT implementation. This example takes that a step further and introduces delegated routing. Delegated routing is
|
||||
especially useful when your libp2p node will have limited resources, making running a DHT impractical. It's
|
||||
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
|
||||
to provide content on your behalf.
|
||||
|
||||
The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
|
||||
Once you've completed the example, you should try enabled the DHT and see what kind of results you get! You can also enable the
|
||||
various Peer Discovery modules and see the impact it has on your Peer count.
|
||||
|
||||
## Prerequisite
|
||||
**NOTE**: This example is currently dependent on a clone of the [delegated routing support branch of go-ipfs](https://github.com/ipfs/go-ipfs/pull/4595).
|
||||
|
||||
## Running this example
|
||||
|
||||
1. Install IPFS locally if you dont already have it. [Install Guide](https://docs.ipfs.io/introduction/install/)
|
||||
2. Run the IPFS daemon: `ipfs daemon`
|
||||
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
|
||||
4. In another window output the addresses of the node: `ipfs id`. Make note of the websocket address, it will contain `/ws/` in the address.
|
||||
5. In `./src/libp2p-bundle.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
|
||||
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
|
||||
7. Start this example:
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm start
|
||||
```
|
||||
|
||||
This should open your browser to http://localhost:3000. If it does not, go ahead and do that now.
|
||||
|
||||
8. Your browser should show you connected to at least 1 peer.
|
||||
|
||||
### Finding Content via the Delegate
|
||||
1. Add a file to your IPFS node. From this example root you can do `ipfs add ./README.md` to add the example readme.
|
||||
2. Copy the hash from line 5, it will look something like *Qmf33vz4HJFkqgH7XPP1uA6atYKTX1BWQEQthzpKcAdeyZ*.
|
||||
3. In the browser, paste the hash into the *Hash* field and hit `Find`. The readme contents should display.
|
||||
|
||||
This will do a few things:
|
||||
* The delegate nodes api will be queried to find providers of the content
|
||||
* The content will be fetched from the providers
|
||||
* Since we now have the content, we tell the delegate node to fetch the content from us and become a provider
|
||||
|
||||
### Finding Peers via the Delegate
|
||||
1. Get a list of your delegate nodes peer by querying the IPFS daemon: `ipfs swarm peers`
|
||||
2. Copy one of the CIDs from the list of peer addresses, this will be the last portion of the address and will look something like `QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8`.
|
||||
3. In your browser, paste the CID into the *Peer* field and hit `Find`.
|
||||
4. You should see information about the peer including its addresses.
|
29
examples/delegated-routing/package.json
Normal file
29
examples/delegated-routing/package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "delegated-routing-example",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"ipfs": "~0.34.4",
|
||||
"libp2p": "github:libp2p/js-libp2p#master",
|
||||
"libp2p-delegated-content-routing": "~0.2.2",
|
||||
"libp2p-delegated-peer-routing": "~0.2.2",
|
||||
"libp2p-kad-dht": "~0.14.12",
|
||||
"libp2p-mplex": "~0.8.5",
|
||||
"libp2p-secio": "~0.11.1",
|
||||
"libp2p-webrtc-star": "~0.15.8",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websockets": "~0.12.2",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-scripts": "2.1.8"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start"
|
||||
},
|
||||
"browserslist": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not ie <= 11",
|
||||
"not op_mini all"
|
||||
]
|
||||
}
|
BIN
examples/delegated-routing/public/favicon.ico
Normal file
BIN
examples/delegated-routing/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
16
examples/delegated-routing/public/index.html
Normal file
16
examples/delegated-routing/public/index.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="theme-color" content="#000000">
|
||||
<title>Delegated Routing</title>
|
||||
<link rel="stylesheet" type="text/css" href="main.css">
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
67
examples/delegated-routing/public/main.css
Normal file
67
examples/delegated-routing/public/main.css
Normal file
@ -0,0 +1,67 @@
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
|
||||
section * {
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: #222;
|
||||
height: 150px;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.center {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: bisque;
|
||||
min-height: 100px;
|
||||
margin: 0px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.loader {
|
||||
text-align: center;
|
||||
height: 64px;
|
||||
margin-bottom: -64px;
|
||||
}
|
||||
|
||||
.loading .lds-ripple {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
}
|
||||
.loading .lds-ripple div {
|
||||
position: absolute;
|
||||
border: 4px solid #000;
|
||||
opacity: 1;
|
||||
border-radius: 50%;
|
||||
animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
|
||||
margin: auto;
|
||||
}
|
||||
.loading .lds-ripple div:nth-child(2) {
|
||||
animation-delay: -0.5s;
|
||||
}
|
||||
@keyframes lds-ripple {
|
||||
0% {
|
||||
top: 28px;
|
||||
left: 28px;
|
||||
width: 0;
|
||||
height: 0;
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
top: -1px;
|
||||
left: -1px;
|
||||
width: 58px;
|
||||
height: 58px;
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
152
examples/delegated-routing/src/App.js
Normal file
152
examples/delegated-routing/src/App.js
Normal file
@ -0,0 +1,152 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
import React from 'react'
|
||||
import Ipfs from 'ipfs'
|
||||
import libp2pBundle from './libp2p-bundle'
|
||||
const Component = React.Component
|
||||
|
||||
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
|
||||
|
||||
class App extends Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
peers: 0,
|
||||
// This hash is the IPFS readme
|
||||
hash: 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB',
|
||||
// This peer is one of the Bootstrap nodes for IPFS
|
||||
peer: 'QmV6kA2fB8kTr6jc3pL5zbNsjKbmPUHAPKKHRBYe1kDEyc',
|
||||
isLoading: 0
|
||||
}
|
||||
this.peerInterval = null
|
||||
|
||||
this.handleHashChange = this.handleHashChange.bind(this)
|
||||
this.handleHashSubmit = this.handleHashSubmit.bind(this)
|
||||
this.handlePeerChange = this.handlePeerChange.bind(this)
|
||||
this.handlePeerSubmit = this.handlePeerSubmit.bind(this)
|
||||
}
|
||||
|
||||
handleHashChange (event) {
|
||||
this.setState({
|
||||
hash: event.target.value
|
||||
})
|
||||
}
|
||||
handlePeerChange (event) {
|
||||
this.setState({
|
||||
peer: event.target.value
|
||||
})
|
||||
}
|
||||
|
||||
handleHashSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.files.cat(this.state.hash, (err, data) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: data.toString(),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
handlePeerSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: JSON.stringify(results, null, 2),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
componentDidMount () {
|
||||
window.ipfs = this.ipfs = new Ipfs({
|
||||
config: {
|
||||
Addresses: {
|
||||
Swarm: []
|
||||
},
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: false
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
},
|
||||
Bootstrap: [
|
||||
BootstrapNode
|
||||
]
|
||||
},
|
||||
preload: {
|
||||
enabled: false
|
||||
},
|
||||
libp2p: libp2pBundle
|
||||
})
|
||||
this.ipfs.on('ready', () => {
|
||||
if (this.peerInterval) {
|
||||
clearInterval(this.peerInterval)
|
||||
}
|
||||
|
||||
this.ipfs.swarm.connect(BootstrapNode, (err) => {
|
||||
if (err) {
|
||||
console.log('Error connecting to the node', err)
|
||||
}
|
||||
console.log('Connected!')
|
||||
})
|
||||
|
||||
this.peerInterval = setInterval(() => {
|
||||
this.ipfs.swarm.peers((err, peers) => {
|
||||
if (err) console.log(err)
|
||||
if (peers) this.setState({peers: peers.length})
|
||||
})
|
||||
}, 2500)
|
||||
})
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<header className="center">
|
||||
<h1>Delegated Routing</h1>
|
||||
<h2>There are currently {this.state.peers} peers.</h2>
|
||||
</header>
|
||||
<section className="center">
|
||||
<form onSubmit={this.handleHashSubmit}>
|
||||
<label>
|
||||
Hash:
|
||||
<input type="text" value={this.state.hash} onChange={this.handleHashChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
<form onSubmit={this.handlePeerSubmit}>
|
||||
<label>
|
||||
Peer:
|
||||
<input type="text" value={this.state.peer} onChange={this.handlePeerChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
</section>
|
||||
<section className={[this.state.isLoading > 0 ? 'loading' : '', 'loader'].join(' ')}>
|
||||
<div className="lds-ripple"><div></div><div></div></div>
|
||||
</section>
|
||||
<section>
|
||||
<pre>
|
||||
{this.state.response}
|
||||
</pre>
|
||||
</section>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default App
|
8
examples/delegated-routing/src/index.js
Normal file
8
examples/delegated-routing/src/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
import React from 'react' // eslint-disable-line no-unused-vars
|
||||
import ReactDOM from 'react-dom'
|
||||
import App from './App' // eslint-disable-line no-unused-vars
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
76
examples/delegated-routing/src/libp2p-bundle.js
Normal file
76
examples/delegated-routing/src/libp2p-bundle.js
Normal file
@ -0,0 +1,76 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
export default function Libp2pBundle ({peerInfo, peerBook}) {
|
||||
const wrtcstar = new WebRTCStar({id: peerInfo.id})
|
||||
const wsstar = new WebSocketStar({id: peerInfo.id})
|
||||
const delegatedApiOptions = {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: '8080'
|
||||
}
|
||||
|
||||
return new Libp2p({
|
||||
peerInfo,
|
||||
peerBook,
|
||||
// Lets limit the connection managers peers and have it check peer health less frequently
|
||||
connectionManager: {
|
||||
maxPeers: 10,
|
||||
pollInterval: 5000
|
||||
},
|
||||
modules: {
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter(delegatedApiOptions)
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcstar.discovery,
|
||||
wsstar.discovery
|
||||
],
|
||||
transport: [
|
||||
wrtcstar,
|
||||
wsstar,
|
||||
Websockets
|
||||
],
|
||||
streamMuxer: [
|
||||
MPLEX
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false,
|
||||
webrtcStar: {
|
||||
enabled: false
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -5,21 +6,21 @@ const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
|
||||
const bootstrapers = [
|
||||
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
|
||||
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
|
||||
]
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
@ -33,8 +34,9 @@ class MyBundle extends libp2p {
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
bootstrap: {
|
||||
interval: 2000,
|
||||
interval: 20e3,
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
@ -61,8 +63,8 @@ waterfall([
|
||||
if (err) { throw err }
|
||||
|
||||
node.on('peer:discovery', (peer) => {
|
||||
// No need to dial, autoDial is on
|
||||
console.log('Discovered:', peer.id.toB58String())
|
||||
node.dial(peer, () => {})
|
||||
})
|
||||
|
||||
node.on('peer:connect', (peer) => {
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -22,7 +23,7 @@ class MyBundle extends libp2p {
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 1000,
|
||||
interval: 20e3,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
|
@ -43,15 +43,15 @@ In this bundle, we use a `bootstrappers` array listing peers to connect _on boot
|
||||
|
||||
```JavaScript
|
||||
const bootstrapers = [
|
||||
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
|
||||
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
|
||||
]
|
||||
```
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
This example performs a simple echo from the listener to the dialer.
|
||||
|
||||
## Setup
|
||||
1. Install the modules, `npm install`.
|
||||
1. Install the modules from libp2p root, `npm install`.
|
||||
2. Open 2 terminal windows in the `./src` directory.
|
||||
|
||||
## Running
|
||||
|
@ -28,7 +28,7 @@ async.parallel([
|
||||
// Peer to Dial
|
||||
const listenerPeerInfo = new PeerInfo(ids[1])
|
||||
const listenerId = ids[1]
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/ipfs/' +
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' +
|
||||
listenerId.toB58String()
|
||||
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
|
||||
|
||||
@ -37,7 +37,7 @@ async.parallel([
|
||||
|
||||
console.log('Dialer ready, listening on:')
|
||||
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
|
||||
'/ipfs/' + dialerId.toB58String()))
|
||||
'/p2p/' + dialerId.toB58String()))
|
||||
|
||||
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
||||
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
|
@ -3,7 +3,7 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
|
@ -41,6 +41,6 @@ series([
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/ipfs/' + listenerId.toB58String())
|
||||
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
|
||||
})
|
||||
})
|
||||
|
@ -37,4 +37,4 @@ And that's it, from now on, all your libp2p communications are encrypted. Try ru
|
||||
|
||||
If you want to want to learn more about how SECIO works, you can read the [great write up done by Dominic Tarr](https://github.com/auditdrivencrypto/secure-channel/blob/master/prior-art.md#ipfss-secure-channel).
|
||||
|
||||
Importante note: SECIO hasn't been audited and so, we do not recommend to trust its security. We intent to move to TLS 1.3 once the specification is finalized and an implementation exists that we can use.
|
||||
Important note: SECIO hasn't been audited and so, we do not recommend to trust its security. We intent to move to TLS 1.3 once the specification is finalized and an implementation exists that we can use.
|
||||
|
@ -17,12 +17,16 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"detect-dom-ready": "^1.0.2",
|
||||
"libp2p-mplex": "~0.8.0",
|
||||
"libp2p-railing": "~0.9.1",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-spdy": "~0.12.1",
|
||||
"libp2p-webrtc-star": "~0.15.3",
|
||||
"libp2p-websockets": "~0.12.0",
|
||||
"peer-info": "~0.14.1"
|
||||
"libp2p": "../../../",
|
||||
"libp2p-bootstrap": "~0.9.7",
|
||||
"libp2p-gossipsub": "~0.0.4",
|
||||
"libp2p-kad-dht": "^0.15.3",
|
||||
"libp2p-mplex": "~0.8.5",
|
||||
"libp2p-secio": "~0.11.1",
|
||||
"libp2p-spdy": "~0.13.3",
|
||||
"libp2p-webrtc-star": "~0.15.8",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websockets": "~0.12.2",
|
||||
"peer-info": "~0.15.1"
|
||||
}
|
||||
}
|
||||
|
@ -2,36 +2,40 @@
|
||||
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../../../')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const libp2p = require('libp2p')
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
|
||||
const bootstrapers = [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/sfo-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/dns4/sfo-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
|
||||
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
|
||||
const bootstrapList = [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/sfo-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
'/dns4/sfo-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
|
||||
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
|
||||
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
|
||||
]
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
|
||||
constructor ({ peerInfo }) {
|
||||
const wrtcStar = new WebRTCStar({ id: peerInfo.id })
|
||||
const wsstar = new WebSocketStar({ id: peerInfo.id })
|
||||
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
wrtcStar,
|
||||
new WebSockets()
|
||||
WebSockets,
|
||||
wsstar
|
||||
],
|
||||
streamMuxer: [
|
||||
Mplex,
|
||||
@ -42,11 +46,15 @@ class Node extends libp2p {
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcStar.discovery,
|
||||
wsstar.discovery,
|
||||
Bootstrap
|
||||
]
|
||||
],
|
||||
dht: DHT,
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
webRTCStar: {
|
||||
enabled: true
|
||||
},
|
||||
@ -54,26 +62,32 @@ class Node extends libp2p {
|
||||
enabled: true
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: bootstrapers
|
||||
interval: 20e3,
|
||||
enabled: true,
|
||||
list: bootstrapList
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: false,
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: false,
|
||||
pubsub: false
|
||||
dht: {
|
||||
enabled: false
|
||||
},
|
||||
pubsub: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
connectionManager: {
|
||||
minPeers: 10,
|
||||
maxPeers: 50
|
||||
}
|
||||
}
|
||||
|
||||
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/ipfs/${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
|
||||
|
@ -1,3 +1,5 @@
|
||||
/* eslint no-console: ["error", { allow: ["log"] }] */
|
||||
/* eslint max-nested-callbacks: ["error", 5] */
|
||||
'use strict'
|
||||
|
||||
const domReady = require('detect-dom-ready')
|
||||
@ -13,13 +15,7 @@ domReady(() => {
|
||||
}
|
||||
|
||||
node.on('peer:discovery', (peerInfo) => {
|
||||
console.log('Discovered a peer')
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
console.log('Discovered: ' + idStr)
|
||||
|
||||
node.dial(peerInfo, (err, conn) => {
|
||||
if (err) { return console.log('Failed to dial:', idStr) }
|
||||
})
|
||||
console.log('Discovered a peer:', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
node.on('peer:connect', (peerInfo) => {
|
||||
@ -33,13 +29,13 @@ domReady(() => {
|
||||
|
||||
node.on('peer:disconnect', (peerInfo) => {
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
console.log('Lost connection to: ' + idStr)
|
||||
document.getElementById(idStr).remove()
|
||||
const el = document.getElementById(idStr)
|
||||
el && el.remove()
|
||||
})
|
||||
|
||||
node.start((err) => {
|
||||
if (err) {
|
||||
return console.log('WebRTC not supported')
|
||||
return console.log(err)
|
||||
}
|
||||
|
||||
const idStr = node.peerInfo.id.toB58String()
|
||||
@ -50,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) => {})
|
||||
|
@ -4,7 +4,15 @@ One of the primary goals with libp2p P2P was to get it fully working in the brow
|
||||
|
||||
# 1. Setting up a simple app that lists connections to other nodes
|
||||
|
||||
Simple go into the folder [1](./1) and execute the following
|
||||
Start by installing libp2p's dependencies.
|
||||
|
||||
```bash
|
||||
> cd ../../
|
||||
> npm install
|
||||
> cd examples/libp2p-in-the-browser
|
||||
```
|
||||
|
||||
Then simply go into the folder [1](./1) and execute the following
|
||||
|
||||
```bash
|
||||
> cd 1
|
||||
@ -12,5 +20,3 @@ Simple 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,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -22,10 +23,8 @@ class MyBundle extends libp2p {
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -23,10 +24,8 @@ class MyBundle extends libp2p {
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,11 +25,9 @@ class MyBundle extends libp2p {
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
// dht must be enabled
|
||||
dht: true
|
||||
enabled: true,
|
||||
kBucketSize: 20
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -69,8 +67,8 @@ You should see the output being something like:
|
||||
```Bash
|
||||
> node 1.js
|
||||
Found it, multiaddrs are:
|
||||
/ip4/127.0.0.1/tcp/63617/ipfs/QmWrFXvZr9S4iDqycyoyc2zDdrT1jg9wpdenUTdd1LTar6
|
||||
/ip4/192.168.86.41/tcp/63617/ipfs/QmWrFXvZr9S4iDqycyoyc2zDdrT1jg9wpdenUTdd1LTar6
|
||||
/ip4/127.0.0.1/tcp/63617/p2p/QmWrFXvZr9S4iDqycyoyc2zDdrT1jg9wpdenUTdd1LTar6
|
||||
/ip4/192.168.86.41/tcp/63617/p2p/QmWrFXvZr9S4iDqycyoyc2zDdrT1jg9wpdenUTdd1LTar6
|
||||
```
|
||||
|
||||
You have successfully used Peer Routing to find a peer that you were not directly connected. Now all you have to do is to dial to the multiaddrs you discovered.
|
||||
|
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
|
||||
}, '')
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -169,7 +169,7 @@ You can see this working on example [3.js](./3.js). The result should look like
|
||||
> node 3.js
|
||||
from 1 to 2
|
||||
Addresses by which both peers are connected
|
||||
node 1 to node 2: /ip4/127.0.0.1/tcp/50629/ipfs/QmZwMKTo6wG4Te9A6M2eJnWDpR8uhsGed4YRegnV5DcKiv
|
||||
node 2 to node 1: /ip4/127.0.0.1/tcp/50630/ipfs/QmRgormJQeDyXhDKma11eUtksoh8vWmeBoxghVt4meauW9
|
||||
node 1 to node 2: /ip4/127.0.0.1/tcp/50629/p2p/QmZwMKTo6wG4Te9A6M2eJnWDpR8uhsGed4YRegnV5DcKiv
|
||||
node 2 to node 1: /ip4/127.0.0.1/tcp/50630/p2p/QmRgormJQeDyXhDKma11eUtksoh8vWmeBoxghVt4meauW9
|
||||
from 2 to 1
|
||||
```
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -6,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')
|
||||
@ -18,7 +20,8 @@ class MyBundle extends libp2p {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ SECIO ],
|
||||
peerDiscovery: [ MulticastDNS ]
|
||||
peerDiscovery: [ MulticastDNS ],
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
@ -27,8 +30,9 @@ class MyBundle extends libp2p {
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,25 +65,39 @@ parallel([
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
|
||||
series([
|
||||
(cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)),
|
||||
(cb) => setTimeout(cb, 500)
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
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!'),
|
||||
() => {}
|
||||
)
|
||||
}, 1000)
|
||||
(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,29 +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, PubSub will be enabled by default.
|
||||
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
|
||||
series([
|
||||
(cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)),
|
||||
(cb) => setTimeout(cb, 500)
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
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!'),
|
||||
() => {}
|
||||
)
|
||||
}, 1000)
|
||||
(err) => {
|
||||
if (err) { throw err }
|
||||
}
|
||||
)
|
||||
}, 1000)
|
||||
cb()
|
||||
},
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
@ -42,11 +56,30 @@ The output of the program should look like:
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
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!
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
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
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -88,8 +88,8 @@ Running this should result in something like:
|
||||
> node 1.js
|
||||
node has started (true/false): true
|
||||
listening on:
|
||||
/ip4/127.0.0.1/tcp/61329/ipfs/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
|
||||
/ip4/192.168.2.156/tcp/61329/ipfs/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
|
||||
/ip4/127.0.0.1/tcp/61329/p2p/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
|
||||
/ip4/192.168.2.156/tcp/61329/p2p/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
|
||||
```
|
||||
|
||||
That `QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ` is the PeerId that was created during the PeerInfo generation.
|
||||
@ -175,11 +175,11 @@ The result should be look like:
|
||||
```bash
|
||||
> node 2.js
|
||||
node 1 is listening on:
|
||||
/ip4/127.0.0.1/tcp/62279/ipfs/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
|
||||
/ip4/192.168.2.156/tcp/62279/ipfs/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
|
||||
/ip4/127.0.0.1/tcp/62279/p2p/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
|
||||
/ip4/192.168.2.156/tcp/62279/p2p/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
|
||||
node 2 is listening on:
|
||||
/ip4/127.0.0.1/tcp/62278/ipfs/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
|
||||
/ip4/192.168.2.156/tcp/62278/ipfs/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
|
||||
/ip4/127.0.0.1/tcp/62278/p2p/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
|
||||
/ip4/192.168.2.156/tcp/62278/p2p/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
|
||||
Hello p2p world!
|
||||
```
|
||||
|
||||
@ -304,14 +304,14 @@ If everything was set correctly, you now should see the following after you run
|
||||
```Bash
|
||||
> node 3.js
|
||||
node 1 is listening on:
|
||||
/ip4/127.0.0.1/tcp/62620/ipfs/QmWpWmcVJkF6EpmAaVDauku8g1uFGuxPsGP35XZp9GYEqs
|
||||
/ip4/192.168.2.156/tcp/62620/ipfs/QmWpWmcVJkF6EpmAaVDauku8g1uFGuxPsGP35XZp9GYEqs
|
||||
/ip4/127.0.0.1/tcp/62620/p2p/QmWpWmcVJkF6EpmAaVDauku8g1uFGuxPsGP35XZp9GYEqs
|
||||
/ip4/192.168.2.156/tcp/62620/p2p/QmWpWmcVJkF6EpmAaVDauku8g1uFGuxPsGP35XZp9GYEqs
|
||||
node 2 is listening on:
|
||||
/ip4/127.0.0.1/tcp/10000/ws/ipfs/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
/ip4/127.0.0.1/tcp/62619/ipfs/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
/ip4/192.168.2.156/tcp/62619/ipfs/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
/ip4/127.0.0.1/tcp/10000/ws/p2p/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
/ip4/127.0.0.1/tcp/62619/p2p/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
/ip4/192.168.2.156/tcp/62619/p2p/QmWAQtWdzWXibgfyc7WRHhhv6MdqVKzXvyfSTnN2aAvixX
|
||||
node 3 is listening on:
|
||||
/ip4/127.0.0.1/tcp/20000/ws/ipfs/QmVq1PWh3VSDYdFqYMtqp4YQyXcrH27N7968tGdM1VQPj1
|
||||
/ip4/127.0.0.1/tcp/20000/ws/p2p/QmVq1PWh3VSDYdFqYMtqp4YQyXcrH27N7968tGdM1VQPj1
|
||||
node 3 failed to dial to node 1 with: No available transport to dial to
|
||||
node 1 dialed to node 2 successfully
|
||||
node 2 dialed to node 3 successfully
|
||||
|
86
package-list.json
Normal file
86
package-list.json
Normal file
@ -0,0 +1,86 @@
|
||||
{
|
||||
"columns": [
|
||||
"Package",
|
||||
"Version",
|
||||
"Deps",
|
||||
"CI/Travis",
|
||||
"Coverage",
|
||||
"Lead Maintainer"
|
||||
],
|
||||
"rows": [
|
||||
"Libp2p",
|
||||
["libp2p/interface-libp2p", "interface-libp2p"],
|
||||
["libp2p/js-libp2p", "libp2p"],
|
||||
|
||||
"Connection",
|
||||
["libp2p/interface-connection", "interface-connection"],
|
||||
|
||||
"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"],
|
||||
|
||||
"Stream Muxers",
|
||||
["libp2p/interface-stream-muxer", "interface-stream-muxer"],
|
||||
["libp2p/js-libp2p-mplex", "libp2p-mplex"],
|
||||
["libp2p/js-libp2p-spdy", "libp2p-spdy"],
|
||||
|
||||
"Discovery",
|
||||
["libp2p/interface-peer-discovery", "interface-peer-discovery"],
|
||||
["libp2p/js-libp2p-bootstrap", "libp2p-bootstrap"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
["libp2p/js-libp2p-mdns", "libp2p-mdns"],
|
||||
["libp2p/js-libp2p-rendezvous", "libp2p-rendezvous"],
|
||||
["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"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
|
||||
"Peer Routing",
|
||||
["libp2p/interface-peer-routing", "interface-peer-routing"],
|
||||
["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"],
|
||||
["libp2p/js-libp2p-crypto", "libp2p-crypto"],
|
||||
["libp2p/js-libp2p-crypto-secp256k1", "libp2p-crypto-secp256k1"],
|
||||
["libp2p/js-libp2p-switch", "libp2p-switch"],
|
||||
|
||||
"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"]
|
||||
]
|
||||
}
|
140
package.json
140
package.json
@ -1,9 +1,13 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.23.0",
|
||||
"description": "JavaScript base class for libp2p bundles",
|
||||
"leadMaintainer": "David Dias <daviddias@ipfs.io>",
|
||||
"version": "0.26.1",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "aegir lint",
|
||||
"build": "aegir build",
|
||||
@ -13,72 +17,106 @@
|
||||
"release": "aegir release -t node -t browser",
|
||||
"release-minor": "aegir release --type minor -t node -t browser",
|
||||
"release-major": "aegir release --type major -t node -t browser",
|
||||
"coverage": "aegir coverage",
|
||||
"coverage-publish": "aegir coverage --provider coveralls"
|
||||
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/libp2p/js-libp2p.git"
|
||||
},
|
||||
"keywords": [
|
||||
"libp2p",
|
||||
"network",
|
||||
"p2p",
|
||||
"peer",
|
||||
"peer-to-peer",
|
||||
"IPFS"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=6.0.0",
|
||||
"npm": ">=3.0.0"
|
||||
},
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/libp2p/js-libp2p/issues"
|
||||
},
|
||||
"homepage": "https://github.com/libp2p/js-libp2p",
|
||||
"homepage": "https://libp2p.io",
|
||||
"license": "MIT",
|
||||
"browser": {
|
||||
"joi": "joi-browser",
|
||||
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0",
|
||||
"npm": ">=6.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"joi": "^13.4.0",
|
||||
"joi-browser": "^13.4.0",
|
||||
"libp2p-connection-manager": "~0.0.2",
|
||||
"libp2p-floodsub": "~0.15.0",
|
||||
"libp2p-ping": "~0.8.0",
|
||||
"libp2p-switch": "~0.40.7",
|
||||
"libp2p-websockets": "~0.12.0",
|
||||
"mafmt": "^6.0.0",
|
||||
"multiaddr": "^5.0.0",
|
||||
"peer-book": "~0.8.0",
|
||||
"peer-id": "~0.11.0",
|
||||
"peer-info": "~0.14.1"
|
||||
"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",
|
||||
"hashlru": "^2.3.0",
|
||||
"interface-connection": "~0.3.3",
|
||||
"latency-monitor": "~0.2.1",
|
||||
"libp2p-crypto": "~0.16.1",
|
||||
"libp2p-websockets": "^0.12.2",
|
||||
"mafmt": "^6.0.7",
|
||||
"merge-options": "^1.0.1",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^6.1.0",
|
||||
"multistream-select": "~0.14.6",
|
||||
"once": "^1.4.0",
|
||||
"peer-book": "^0.9.1",
|
||||
"peer-id": "^0.12.2",
|
||||
"peer-info": "~0.15.1",
|
||||
"pull-cat": "^1.1.11",
|
||||
"pull-defer": "~0.2.3",
|
||||
"pull-handshake": "^1.1.4",
|
||||
"pull-reader": "^1.3.1",
|
||||
"pull-stream": "^3.6.9",
|
||||
"promisify-es6": "^1.0.3",
|
||||
"protons": "^1.0.1",
|
||||
"retimer": "^2.0.0",
|
||||
"superstruct": "^0.6.0",
|
||||
"xsalsa20": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"aegir": "^15.1.0",
|
||||
"chai": "^4.1.2",
|
||||
"cids": "~0.5.3",
|
||||
"aegir": "^20.0.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"cids": "^0.7.1",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"electron-webrtc": "~0.3.0",
|
||||
"libp2p-circuit": "~0.2.0",
|
||||
"libp2p-kad-dht": "~0.10.1",
|
||||
"libp2p-mdns": "~0.12.0",
|
||||
"libp2p-mplex": "~0.8.0",
|
||||
"libp2p-railing": "~0.9.2",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-spdy": "~0.12.1",
|
||||
"libp2p-tcp": "~0.12.0",
|
||||
"libp2p-webrtc-star": "~0.15.3",
|
||||
"libp2p-websocket-star": "~0.8.1",
|
||||
"libp2p-websocket-star-rendezvous": "~0.2.3",
|
||||
"electron-webrtc": "^0.3.0",
|
||||
"interface-datastore": "^0.6.0",
|
||||
"libp2p-bootstrap": "^0.9.7",
|
||||
"libp2p-delegated-content-routing": "^0.2.2",
|
||||
"libp2p-delegated-peer-routing": "^0.2.2",
|
||||
"libp2p-floodsub": "~0.17.0",
|
||||
"libp2p-gossipsub": "~0.0.4",
|
||||
"libp2p-kad-dht": "^0.15.3",
|
||||
"libp2p-mdns": "^0.12.3",
|
||||
"libp2p-mplex": "^0.8.4",
|
||||
"libp2p-pnet": "~0.1.0",
|
||||
"libp2p-secio": "^0.11.1",
|
||||
"libp2p-spdy": "^0.13.2",
|
||||
"libp2p-tcp": "^0.13.0",
|
||||
"libp2p-webrtc-star": "^0.16.1",
|
||||
"libp2p-websocket-star": "~0.10.2",
|
||||
"libp2p-websocket-star-rendezvous": "~0.4.1",
|
||||
"lodash.times": "^4.3.2",
|
||||
"nock": "^10.0.6",
|
||||
"portfinder": "^1.0.20",
|
||||
"pull-goodbye": "0.0.2",
|
||||
"pull-serializer": "~0.3.2",
|
||||
"pull-stream": "^3.6.8",
|
||||
"sinon": "^6.1.4",
|
||||
"webrtcsupport": "^2.2.0",
|
||||
"wrtc": "~0.1.6"
|
||||
"pull-length-prefixed": "^1.3.3",
|
||||
"pull-mplex": "^0.1.2",
|
||||
"pull-pair": "^1.1.0",
|
||||
"pull-protocol-buffers": "~0.1.2",
|
||||
"pull-serializer": "^0.3.2",
|
||||
"sinon": "^7.2.7",
|
||||
"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>",
|
||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||
@ -89,6 +127,9 @@
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
"Guy Sviry <32539816+guysv@users.noreply.github.com>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Hugo Dias <mail@hugodias.me>",
|
||||
"Hugo Dias <hugomrdias@gmail.com>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>",
|
||||
"Jacob Heun <jacobheun@gmail.com>",
|
||||
@ -98,17 +139,26 @@
|
||||
"Kevin Kwok <antimatter15@gmail.com>",
|
||||
"Lars Gierth <lgierth@users.noreply.github.com>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Marcin Tojek <mtojek@users.noreply.github.com>",
|
||||
"Nuno Nogueira <nunofmn@gmail.com>",
|
||||
"Pedro Teixeira <pedro@protocol.ai>",
|
||||
"Pedro Teixeira <i@pgte.me>",
|
||||
"RasmusErik Voel Jensen <github@solsort.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Ryan Bell <ryan@piing.net>",
|
||||
"Soeren <nikorpoulsen@gmail.com>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Thomas Eizinger <thomas@eizinger.io>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"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>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
|
||||
]
|
||||
|
@ -59,7 +59,7 @@ test('story 1 - peerA', (t) => {
|
||||
t.ifErr(err, 'created Node successfully')
|
||||
t.ok(node.isStarted(), 'PeerA is Running')
|
||||
|
||||
const peerBAddr = `/ip4/127.0.0.1/tcp/10001/ipfs/${PeerB.id}`
|
||||
const peerBAddr = `/ip4/127.0.0.1/tcp/10001/p2p/${PeerB.id}`
|
||||
|
||||
node.handle('/time/1.0.0', (protocol, conn) => {
|
||||
pull(
|
||||
|
@ -59,7 +59,7 @@ test('story 1 - peerA', (t) => {
|
||||
t.ifErr(err, 'created Node successfully')
|
||||
t.ok(node.isStarted(), 'PeerB is Running')
|
||||
|
||||
const peerAAddr = `/ip4/127.0.0.1/tcp/10000/ipfs/${PeerA.id}`
|
||||
const peerAAddr = `/ip4/127.0.0.1/tcp/10000/p2p/${PeerA.id}`
|
||||
|
||||
node.handle('/echo/1.0.0', (protocol, conn) => {
|
||||
pull(
|
||||
|
@ -33,7 +33,7 @@ test('story 1 - peerA', (t) => {
|
||||
t.ifErr(err, 'created Node')
|
||||
t.ok(node.isStarted(), 'PeerA is running')
|
||||
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/ipfs/${PeerB.id}`
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/p2p/${PeerB.id}`
|
||||
|
||||
node.dial(PeerBAddr, '/echo/1.0.0', (err, conn) => {
|
||||
t.ifErr(err, 'dial successful')
|
||||
|
@ -33,7 +33,7 @@ test('story 2 - peerA', (t) => {
|
||||
t.ifErr(err, 'created Node')
|
||||
t.ok(node.isStarted(), 'PeerA is running')
|
||||
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/ws/ipfs/${PeerB.id}`
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/p2p/${PeerB.id}`
|
||||
|
||||
node.dial(PeerBAddr, '/echo/1.0.0', (err, conn) => {
|
||||
t.ifErr(err, 'dial successful')
|
||||
|
@ -32,7 +32,7 @@ test('story 3 - peerA', (t) => {
|
||||
t.ifErr(err, 'created Node')
|
||||
t.ok(node.isStarted(), 'PeerA is running')
|
||||
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/ws/ipfs/${PeerB.id}`
|
||||
const PeerBAddr = `/ip4/127.0.0.1/tcp/10001/ws/p2p/${PeerB.id}`
|
||||
|
||||
setTimeout(() => node.dial(PeerBAddr, '/echo/1.0.0', (err, conn) => {
|
||||
t.ok(err, 'dial failed')
|
||||
|
@ -32,7 +32,7 @@ test('story 3 - peerB', (t) => {
|
||||
t.ifErr(err, 'created Node')
|
||||
t.ok(node.isStarted(), 'PeerA is running')
|
||||
|
||||
const PeerAAddr = `/ip4/127.0.0.1/tcp/10000/ws/ipfs/${PeerA.id}`
|
||||
const PeerAAddr = `/ip4/127.0.0.1/tcp/10000/ws/p2p/${PeerA.id}`
|
||||
|
||||
setTimeout(() => node.dial(PeerAAddr, '/echo/1.0.0', (err, conn) => {
|
||||
t.ok(err, 'dial failed')
|
||||
|
@ -1,146 +0,0 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
'use strict'
|
||||
|
||||
// This script generates the table of packages you can see in the readme
|
||||
|
||||
// Columns to show at the header of the table
|
||||
const columns = [
|
||||
'Package',
|
||||
'Version',
|
||||
'Deps',
|
||||
'CI',
|
||||
'Coverage'
|
||||
]
|
||||
|
||||
// Headings are a string
|
||||
// Arrays are packages. Index 0 is the GitHub repo and index 1 is the npm package
|
||||
const rows = [
|
||||
'Libp2p',
|
||||
['libp2p/interface-libp2p', 'interface-libp2p'],
|
||||
['libp2p/js-libp2p', 'libp2p'],
|
||||
|
||||
'Connection',
|
||||
['libp2p/interface-connection', 'interface-connection'],
|
||||
|
||||
'Transport',
|
||||
['libp2p/interface-transport', 'interface-transport'],
|
||||
['libp2p/js-libp2p-circuit', 'libp2p-circuit'], // should this be NAT Traversal only?
|
||||
['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'],
|
||||
|
||||
'Stream Muxers',
|
||||
['libp2p/interface-stream-muxer', 'interface-stream-muxer'],
|
||||
['libp2p/js-libp2p-mplex', 'libp2p-mplex'],
|
||||
['libp2p/js-libp2p-spdy', 'libp2p-spdy'],
|
||||
|
||||
'Discovery',
|
||||
['libp2p/interface-peer-discovery', 'interface-peer-discovery'],
|
||||
['libp2p/js-libp2p-bootstrap', 'libp2p-bootstrap'],
|
||||
['libp2p/js-libp2p-kad-dht', 'libp2p-kad-dht'], // should this be here?
|
||||
['libp2p/js-libp2p-mdns', 'libp2p-mdns'],
|
||||
['libp2p/js-libp2p-rendezvous', 'libp2p-rendezvous'],
|
||||
['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'],
|
||||
['libp2p/js-libp2p-kad-dht', 'libp2p-kad-dht'],
|
||||
|
||||
'Peer Routing',
|
||||
['libp2p/interface-peer-routing', 'interface-peer-routing'],
|
||||
['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'],
|
||||
['libp2p/js-libp2p-crypto', 'libp2p-crypto'],
|
||||
['libp2p/js-libp2p-crypto-secp256k1', 'libp2p-crypto-secp256k1'],
|
||||
['libp2p/js-libp2p-switch', 'libp2p-switch'],
|
||||
|
||||
'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']
|
||||
]
|
||||
|
||||
const isItemPackage = (item) => {
|
||||
return Array.isArray(item)
|
||||
}
|
||||
|
||||
const packageBadges = [
|
||||
// Package
|
||||
(gh, npm) => `[\`${npm}\`](//github.com/${gh})`,
|
||||
// Version
|
||||
(gh, npm) => `[](//github.com/${gh}/releases)`,
|
||||
// Deps
|
||||
(gh, npm) => `[](https://david-dm.org/${gh})`,
|
||||
// CI
|
||||
(gh, npm) => {
|
||||
// Need to fix the path for jenkins links, as jenkins adds `/job/` between everything
|
||||
const jenkinsPath = gh.split('/').join('/job/')
|
||||
return `[](https://ci.ipfs.team/job/${jenkinsPath}/job/master/)`
|
||||
},
|
||||
// Coverage
|
||||
(gh, npm) => `[](https://codecov.io/gh/${gh})`
|
||||
]
|
||||
|
||||
// Creates the table row for a package
|
||||
const generatePackageRow = (item) => {
|
||||
const row = packageBadges.map((func) => {
|
||||
// First string is GitHub path, second is npm package name
|
||||
return func(item[0], item[1])
|
||||
}).join(' | ')
|
||||
const fullRow = `| ${row} |`
|
||||
return fullRow
|
||||
}
|
||||
|
||||
// Generates a row for the table, depending if it's a package or a heading
|
||||
const generateRow = (item) => {
|
||||
if (isItemPackage(item)) {
|
||||
return generatePackageRow(item)
|
||||
} else {
|
||||
return `| **${item}** |`
|
||||
}
|
||||
}
|
||||
|
||||
const header = `| ${columns.join(' | ')} |`
|
||||
const hr = `| ${columns.map(() => '---------').join('|')} |`
|
||||
|
||||
const toPrint = [
|
||||
header,
|
||||
hr,
|
||||
rows.map((row) => generateRow(row)).join('\n')
|
||||
]
|
||||
|
||||
toPrint.forEach((t) => console.log(t))
|
||||
|
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
|
||||
|
||||
|
156
src/circuit/README.md
Normal file
156
src/circuit/README.md
Normal file
@ -0,0 +1,156 @@
|
||||
# js-libp2p-circuit
|
||||
|
||||
> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit.
|
||||
|
||||
`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node.
|
||||
|
||||
This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
|
||||
|
||||
### Why?
|
||||
|
||||
`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
|
||||
|
||||
The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
|
||||
- routing traffic between private nets and circumventing NAT layers
|
||||
- route mangling for better privacy (matreshka/shallot dialing).
|
||||
|
||||
It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
|
||||
|
||||
### libp2p-circuit and IPFS
|
||||
|
||||
Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Install](#install)
|
||||
- [npm](#npm)
|
||||
- [Usage](#usage)
|
||||
- [Example](#example)
|
||||
- [This module uses `pull-streams`](#this-module-uses-pull-streams)
|
||||
- [Converting `pull-streams` to Node.js Streams](#converting-pull-streams-to-nodejs-streams)
|
||||
- [API](#api)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## Usage
|
||||
|
||||
### Example
|
||||
|
||||
#### Create dialer/listener
|
||||
|
||||
```js
|
||||
const Circuit = require('libp2p-circuit')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
const mh1 = multiaddr('/p2p-circuit/ipfs/QmHash') // dial /ipfs/QmHash over any circuit
|
||||
|
||||
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
|
||||
|
||||
const listener = circuit.createListener(mh1, (connection) => {
|
||||
console.log('new connection opened')
|
||||
pull(
|
||||
pull.values(['hello']),
|
||||
socket
|
||||
)
|
||||
})
|
||||
|
||||
listener.listen(() => {
|
||||
console.log('listening')
|
||||
|
||||
pull(
|
||||
circuit.dial(mh1),
|
||||
pull.log,
|
||||
pull.onEnd(() => {
|
||||
circuit.close()
|
||||
})
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
Outputs:
|
||||
|
||||
```sh
|
||||
listening
|
||||
new connection opened
|
||||
hello
|
||||
```
|
||||
|
||||
#### Create `relay`
|
||||
|
||||
```js
|
||||
const Relay = require('libp2p-circuit').Relay
|
||||
|
||||
const relay = new Relay(options)
|
||||
|
||||
relay.mount(swarmInstance) // start relaying traffic
|
||||
```
|
||||
|
||||
### This module uses `pull-streams`
|
||||
|
||||
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||
|
||||
You can learn more about pull-streams at:
|
||||
|
||||
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||
|
||||
#### Converting `pull-streams` to Node.js Streams
|
||||
|
||||
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
|
||||
|
||||
```js
|
||||
const pullToStream = require('pull-stream-to-stream')
|
||||
|
||||
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||
// nodeStreamInstance is an instance of a Node.js Stream
|
||||
```
|
||||
|
||||
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
|
||||
|
||||
## API
|
||||
|
||||
[](https://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('interface-connection').Connection
|
||||
|
||||
const utilsFactory = require('./utils')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:dialer')
|
||||
log.err = debug('libp2p:circuit:error:dialer')
|
||||
|
||||
const multicodec = require('../multicodec')
|
||||
const proto = require('../protocol')
|
||||
|
||||
class Dialer {
|
||||
/**
|
||||
* Creates an instance of Dialer.
|
||||
* @param {Swarm} swarm - the swarm
|
||||
* @param {any} options - config options
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
constructor (swarm, options) {
|
||||
this.swarm = swarm
|
||||
this.relayPeers = new Map()
|
||||
this.relayConns = new Map()
|
||||
this.options = options
|
||||
this.utils = utilsFactory(swarm)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper that returns a relay connection
|
||||
*
|
||||
* @param {*} relay
|
||||
* @param {*} callback
|
||||
* @returns {Function} - callback
|
||||
*/
|
||||
_dialRelayHelper (relay, callback) {
|
||||
if (this.relayConns.has(relay.id.toB58String())) {
|
||||
return callback(null, this.relayConns.get(relay.id.toB58String()))
|
||||
}
|
||||
|
||||
return this._dialRelay(relay, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
*
|
||||
* @param {multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Function} cb - a callback called once dialed
|
||||
* @returns {Connection} - the connection
|
||||
*
|
||||
*/
|
||||
dial (ma, cb) {
|
||||
cb = cb || (() => { })
|
||||
const strMa = ma.toString()
|
||||
if (!strMa.includes('/p2p-circuit')) {
|
||||
log.err('invalid circuit address')
|
||||
return cb(new Error('invalid circuit address'))
|
||||
}
|
||||
|
||||
const addr = strMa.split('p2p-circuit') // extract relay address if any
|
||||
const relay = addr[0] === '/' ? null : multiaddr(addr[0])
|
||||
const peer = multiaddr(addr[1] || addr[0])
|
||||
|
||||
const dstConn = new Connection()
|
||||
setImmediate(
|
||||
this._dialPeer.bind(this),
|
||||
peer,
|
||||
relay,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
dstConn.setInnerConn(conn)
|
||||
cb(null, dstConn)
|
||||
})
|
||||
|
||||
return dstConn
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the peer support the HOP protocol
|
||||
*
|
||||
* @param {PeerInfo} peer
|
||||
* @param {Function} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
canHop (peer, callback) {
|
||||
callback = once(callback || (() => { }))
|
||||
|
||||
this._dialRelayHelper(peer, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const sh = new StreamHandler(conn)
|
||||
waterfall([
|
||||
(cb) => sh.write(proto.CircuitRelay.encode({
|
||||
type: proto.CircuitRelay.Type.CAN_HOP
|
||||
}), cb),
|
||||
(cb) => sh.read(cb)
|
||||
], (err, msg) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const response = proto.CircuitRelay.decode(msg)
|
||||
|
||||
if (response.code !== proto.CircuitRelay.Status.SUCCESS) {
|
||||
const err = new Error(`HOP not supported, skipping - ${this.utils.getB58String(peer)}`)
|
||||
log(err)
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
log('HOP supported adding as relay - %s', this.utils.getB58String(peer))
|
||||
this.relayPeers.set(this.utils.getB58String(peer), peer)
|
||||
sh.close()
|
||||
callback()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial the destination peer over a relay
|
||||
*
|
||||
* @param {multiaddr} dstMa
|
||||
* @param {Connection|PeerInfo} relay
|
||||
* @param {Function} cb
|
||||
* @return {Function|void}
|
||||
* @private
|
||||
*/
|
||||
_dialPeer (dstMa, relay, cb) {
|
||||
if (typeof relay === 'function') {
|
||||
cb = relay
|
||||
relay = null
|
||||
}
|
||||
|
||||
if (!cb) {
|
||||
cb = () => {}
|
||||
}
|
||||
|
||||
dstMa = multiaddr(dstMa)
|
||||
// if no relay provided, dial on all available relays until one succeeds
|
||||
if (!relay) {
|
||||
const relays = Array.from(this.relayPeers.values())
|
||||
const next = (nextRelay) => {
|
||||
if (!nextRelay) {
|
||||
const err = `no relay peers were found or all relays failed to dial`
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return this._negotiateRelay(
|
||||
nextRelay,
|
||||
dstMa,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return next(relays.shift())
|
||||
}
|
||||
cb(null, conn)
|
||||
})
|
||||
}
|
||||
next(relays.shift())
|
||||
} else {
|
||||
return this._negotiateRelay(
|
||||
relay,
|
||||
dstMa,
|
||||
(err, conn) => {
|
||||
if (err) {
|
||||
log.err('An error has occurred negotiating the relay connection', err)
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
return cb(null, conn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Negotiate the relay connection
|
||||
*
|
||||
* @param {Multiaddr|PeerInfo|Connection} relay - the Connection or PeerInfo of the relay
|
||||
* @param {multiaddr} dstMa - the multiaddr of the peer to relay the connection for
|
||||
* @param {Function} callback - a callback which gets the negotiated relay connection
|
||||
* @returns {void}
|
||||
* @private
|
||||
*
|
||||
* @memberOf Dialer
|
||||
*/
|
||||
_negotiateRelay (relay, dstMa, callback) {
|
||||
dstMa = multiaddr(dstMa)
|
||||
relay = this.utils.peerInfoFromMa(relay)
|
||||
const srcMas = this.swarm._peerInfo.multiaddrs.toArray()
|
||||
this._dialRelayHelper(relay, (err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return callback(err)
|
||||
}
|
||||
const sh = new StreamHandler(conn)
|
||||
waterfall([
|
||||
(cb) => {
|
||||
log('negotiating relay for peer %s', dstMa.getPeerId())
|
||||
let dstPeerId
|
||||
try {
|
||||
dstPeerId = PeerId.createFromB58String(dstMa.getPeerId()).id
|
||||
} catch (err) {
|
||||
return cb(err)
|
||||
}
|
||||
sh.write(
|
||||
proto.CircuitRelay.encode({
|
||||
type: proto.CircuitRelay.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.swarm._peerInfo.id.id,
|
||||
addrs: srcMas.map((addr) => addr.buffer)
|
||||
},
|
||||
dstPeer: {
|
||||
id: dstPeerId,
|
||||
addrs: [dstMa.buffer]
|
||||
}
|
||||
}), cb)
|
||||
},
|
||||
(cb) => sh.read(cb)
|
||||
], (err, msg) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const message = proto.CircuitRelay.decode(msg)
|
||||
if (message.type !== proto.CircuitRelay.Type.STATUS) {
|
||||
return callback(new Error(`Got invalid message type - ` +
|
||||
`expected ${proto.CircuitRelay.Type.STATUS} got ${message.type}`))
|
||||
}
|
||||
|
||||
if (message.code !== proto.CircuitRelay.Status.SUCCESS) {
|
||||
return callback(new Error(`Got ${message.code} error code trying to dial over relay`))
|
||||
}
|
||||
|
||||
callback(null, new Connection(sh.rest()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a relay peer by its PeerInfo
|
||||
*
|
||||
* @param {PeerInfo} peer - the PeerInfo of the relay peer
|
||||
* @param {Function} cb - a callback with the connection to the relay peer
|
||||
* @returns {void}
|
||||
* @private
|
||||
*/
|
||||
_dialRelay (peer, cb) {
|
||||
cb = once(cb || (() => { }))
|
||||
|
||||
this.swarm.dial(
|
||||
peer,
|
||||
multicodec.relay,
|
||||
once((err, conn) => {
|
||||
if (err) {
|
||||
log.err(err)
|
||||
return cb(err)
|
||||
}
|
||||
cb(null, conn)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dialer
|
283
src/circuit/circuit/hop.js
Normal file
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('interface-connection').Connection
|
||||
const utilsFactory = require('./utils')
|
||||
const PeerInfo = require('peer-info')
|
||||
const proto = require('../protocol').CircuitRelay
|
||||
const series = require('async/series')
|
||||
|
||||
const debug = require('debug')
|
||||
|
||||
const log = debug('libp2p:circuit:stop')
|
||||
log.err = debug('libp2p:circuit:error:stop')
|
||||
|
||||
class Stop extends EE {
|
||||
constructor (swarm) {
|
||||
super()
|
||||
this.swarm = swarm
|
||||
this.utils = utilsFactory(swarm)
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the incoming STOP message
|
||||
*
|
||||
* @param {{}} msg - the parsed protobuf message
|
||||
* @param {StreamHandler} sh - the stream handler wrapped connection
|
||||
* @param {Function} callback - callback
|
||||
* @returns {undefined}
|
||||
*/
|
||||
handle (msg, sh, callback) {
|
||||
callback = callback || (() => {})
|
||||
|
||||
series([
|
||||
(cb) => this.utils.validateAddrs(msg, sh, proto.Type.STOP, cb),
|
||||
(cb) => this.utils.writeResponse(sh, proto.Status.Success, cb)
|
||||
], (err) => {
|
||||
if (err) {
|
||||
// we don't return the error here,
|
||||
// since multistream select don't expect one
|
||||
callback()
|
||||
return log(err)
|
||||
}
|
||||
|
||||
const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
|
||||
msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
|
||||
const newConn = new Connection(sh.rest())
|
||||
newConn.setPeerInfo(peerInfo)
|
||||
setImmediate(() => this.emit('connection', newConn))
|
||||
callback(newConn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Stop
|
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
|
||||
}
|
||||
`)
|
130
src/config.js
130
src/config.js
@ -1,49 +1,103 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const mergeOptions = require('merge-options')
|
||||
const { struct, superstruct } = require('superstruct')
|
||||
const { optional, list } = struct
|
||||
|
||||
const ModuleSchema = Joi.alternatives().try(Joi.func(), Joi.object())
|
||||
const DefaultConfig = {
|
||||
connectionManager: {
|
||||
minPeers: 25
|
||||
},
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const OptionsSchema = Joi.object({
|
||||
// TODO: create proper validators for the generics
|
||||
connectionManager: Joi.object(),
|
||||
peerInfo: Joi.object().required(),
|
||||
peerBook: Joi.object(),
|
||||
modules: Joi.object().keys({
|
||||
transport: Joi.array().items(ModuleSchema).min(1).required(),
|
||||
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
|
||||
connEncryption: Joi.array().items(ModuleSchema).allow(null),
|
||||
connProtector: Joi.object().keys({
|
||||
protect: Joi.func().required()
|
||||
}).unknown(),
|
||||
peerDiscovery: Joi.array().items(ModuleSchema).allow(null),
|
||||
dht: ModuleSchema.allow(null)
|
||||
}).required(),
|
||||
config: Joi.object().keys({
|
||||
peerDiscovery: Joi.object().allow(null),
|
||||
relay: Joi.object().keys({
|
||||
enabled: Joi.boolean().default(false),
|
||||
hop: Joi.object().keys({
|
||||
enabled: Joi.boolean().default(false),
|
||||
active: Joi.boolean().default(false)
|
||||
// Define custom types
|
||||
const s = superstruct({
|
||||
types: {
|
||||
transport: value => {
|
||||
if (value.length === 0) return 'ERROR_EMPTY'
|
||||
value.forEach(i => {
|
||||
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
|
||||
})
|
||||
}).default(),
|
||||
dht: Joi.object().keys({
|
||||
kBucketSize: Joi.number().allow(null)
|
||||
}),
|
||||
EXPERIMENTAL: Joi.object().keys({
|
||||
dht: Joi.boolean().default(false),
|
||||
pubsub: Joi.boolean().default(false)
|
||||
}).default()
|
||||
}).default()
|
||||
return true
|
||||
},
|
||||
protector: value => {
|
||||
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
module.exports.validate = (options) => {
|
||||
options = Joi.attempt(options, OptionsSchema)
|
||||
const modulesSchema = s({
|
||||
connEncryption: optional(list([s('object|function')])),
|
||||
// this is hacky to simulate optional because interface doesnt work correctly with it
|
||||
// change to optional when fixed upstream
|
||||
connProtector: s('undefined|protector'),
|
||||
contentRouting: optional(list(['object'])),
|
||||
dht: optional(s('null|function|object')),
|
||||
pubsub: optional(s('null|function|object')),
|
||||
peerDiscovery: optional(list([s('object|function')])),
|
||||
peerRouting: optional(list(['object'])),
|
||||
streamMuxer: optional(list([s('object|function')])),
|
||||
transport: 'transport'
|
||||
})
|
||||
|
||||
// Ensure dht is correct
|
||||
if (options.config.EXPERIMENTAL.dht) {
|
||||
Joi.assert(options.modules.dht, ModuleSchema.required())
|
||||
const configSchema = s({
|
||||
peerDiscovery: 'object?',
|
||||
relay: 'object?',
|
||||
dht: 'object?',
|
||||
pubsub: 'object?'
|
||||
})
|
||||
|
||||
const optionsSchema = s({
|
||||
switch: 'object?',
|
||||
connectionManager: 'object?',
|
||||
datastore: 'object?',
|
||||
peerInfo: 'object',
|
||||
peerBook: 'object?',
|
||||
modules: modulesSchema,
|
||||
config: configSchema
|
||||
})
|
||||
|
||||
module.exports.validate = (opts) => {
|
||||
opts = mergeOptions(DefaultConfig, opts)
|
||||
const [error, options] = optionsSchema.validate(opts)
|
||||
|
||||
// Improve errors throwed, reduce stack by throwing here and add reason to the message
|
||||
if (error) {
|
||||
throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`)
|
||||
} else {
|
||||
// Throw when dht is enabled but no dht module provided
|
||||
if (options.config.dht.enabled) {
|
||||
s('function|object')(options.modules.dht)
|
||||
}
|
||||
}
|
||||
|
||||
return options
|
||||
|
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()
|
||||
}
|
@ -1,20 +1,84 @@
|
||||
'use strict'
|
||||
|
||||
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 || []
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (node._dht) {
|
||||
routers.unshift(node._dht)
|
||||
}
|
||||
|
||||
return {
|
||||
findProviders: (key, timeout, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
/**
|
||||
* Iterates over all content routers in series to find providers of the given key.
|
||||
* Once a content router succeeds, iteration will stop.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {number} options.maxNumProviders - maximum number of providers to find
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findProviders: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
} else if (typeof options === 'number') { // This can be deprecated in a future release
|
||||
options = {
|
||||
maxTimeout: options
|
||||
}
|
||||
}
|
||||
|
||||
node._dht.findProviders(key, timeout, callback)
|
||||
},
|
||||
provide: (key, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
node._dht.provide(key, callback)
|
||||
}
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findProviders(key, options, (err, results) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
// If we don't have any results, we need to provide an error to keep trying
|
||||
if (!results || Object.keys(results).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
}
|
||||
|
||||
cb(null, results)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err && err.code !== 'NOT_FOUND') {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Iterates over all content routers in parallel to notify it is
|
||||
* a provider of the given key.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
provide: promisify((key, callback) => {
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
parallel(routers.map((router) => {
|
||||
return (cb) => router.provide(key, cb)
|
||||
}), callback)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
40
src/dht.js
40
src/dht.js
@ -1,27 +1,43 @@
|
||||
'use strict'
|
||||
|
||||
const nextTick = require('async/nextTick')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
const { messages, codes } = require('./errors')
|
||||
|
||||
module.exports = (node) => {
|
||||
return {
|
||||
put: (key, value, callback) => {
|
||||
put: promisify((key, value, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.put(key, value, callback)
|
||||
},
|
||||
get: (key, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
}),
|
||||
get: promisify((key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
node._dht.get(key, callback)
|
||||
},
|
||||
getMany (key, nVals, callback) {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.getMany(key, nVals, callback)
|
||||
}
|
||||
node._dht.get(key, options, callback)
|
||||
}),
|
||||
getMany: promisify((key, nVals, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!node._dht) {
|
||||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
|
||||
}
|
||||
|
||||
node._dht.getMany(key, nVals, options, callback)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
exports.NOT_STARTED_YET = 'The libp2p node is not started yet'
|
13
src/errors.js
Normal file
13
src/errors.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
exports.messages = {
|
||||
NOT_STARTED_YET: 'The libp2p node is not started yet',
|
||||
DHT_DISABLED: 'DHT is not available'
|
||||
}
|
||||
|
||||
exports.codes = {
|
||||
DHT_DISABLED: 'ERR_DHT_DISABLED',
|
||||
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
||||
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
||||
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF'
|
||||
}
|
@ -3,46 +3,74 @@
|
||||
const PeerId = require('peer-id')
|
||||
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
|
||||
/**
|
||||
* Converts the given `peer` to a `PeerInfo` instance.
|
||||
* The `PeerBook` will be checked for the resulting peer, and
|
||||
* the peer will be updated in the `PeerBook`.
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @param {PeerBook} peerBook
|
||||
* @returns {PeerInfo}
|
||||
*/
|
||||
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') {
|
||||
function getPeerInfo (peer, peerBook) {
|
||||
if (typeof peer === 'string') {
|
||||
peer = multiaddr(peer)
|
||||
}
|
||||
|
||||
const peerIdB58Str = peer.getPeerId()
|
||||
if (!peerIdB58Str) {
|
||||
throw new Error(`peer multiaddr instance or string must include peerId`)
|
||||
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'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (PeerId.isPeerId(peer)) {
|
||||
peer = new PeerInfo(peer)
|
||||
}
|
||||
|
||||
addr && peer.multiaddrs.add(addr)
|
||||
|
||||
return peerBook ? peerBook.put(peer) : peer
|
||||
}
|
||||
|
||||
/**
|
||||
* If `getPeerInfo` does not return a peer with multiaddrs,
|
||||
* the `libp2p` PeerRouter will be used to attempt to find the peer.
|
||||
*
|
||||
* @async
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer
|
||||
* @param {Libp2p} libp2p
|
||||
* @returns {Promise<PeerInfo>}
|
||||
*/
|
||||
function getPeerInfoRemote (peer, libp2p) {
|
||||
let peerInfo
|
||||
|
||||
try {
|
||||
p = node.peerBook.get(peerIdB58Str)
|
||||
peerInfo = getPeerInfo(peer, libp2p.peerBook)
|
||||
} 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 setImmediate(() => callback(new Error('peer type not recognized')))
|
||||
return Promise.reject(errCode(
|
||||
new Error(`${peer} is not a valid peer type`),
|
||||
'ERR_INVALID_PEER_TYPE'
|
||||
))
|
||||
}
|
||||
|
||||
setImmediate(() => callback(null, p))
|
||||
// If we don't have an address for the peer, attempt to find it
|
||||
if (peerInfo.multiaddrs.size < 1) {
|
||||
return libp2p.peerRouting.findPeer(peerInfo.id)
|
||||
}
|
||||
|
||||
return Promise.resolve(peerInfo)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getPeerInfoRemote,
|
||||
getPeerInfo
|
||||
}
|
||||
|
37
src/identify/README.md
Normal file
37
src/identify/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# js-libp2p-identify
|
||||
|
||||
> libp2p Identify Protocol
|
||||
|
||||
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-identify.
|
||||
|
||||
## Description
|
||||
|
||||
Identify is a STUN protocol, used by libp2p-swarm in order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
|
||||
|
||||
## How does it work
|
||||
|
||||
Best way to understand the current design is through this issue: https://github.com/libp2p/js-libp2p-swarm/issues/78
|
||||
|
||||
### This module uses `pull-streams`
|
||||
|
||||
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||
|
||||
You can learn more about pull-streams at:
|
||||
|
||||
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||
|
||||
#### Converting `pull-streams` to Node.js Streams
|
||||
|
||||
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
|
||||
|
||||
```js
|
||||
const pullToStream = require('pull-stream-to-stream')
|
||||
|
||||
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||
// nodeStreamInstance is an instance of a Node.js Stream
|
||||
```
|
||||
|
||||
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
|
87
src/identify/dialer.js
Normal file
87
src/identify/dialer.js
Normal file
@ -0,0 +1,87 @@
|
||||
'use strict'
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream/pull')
|
||||
const take = require('pull-stream/throughs/take')
|
||||
const collect = require('pull-stream/sinks/collect')
|
||||
const lp = require('pull-length-prefixed')
|
||||
|
||||
const msg = require('./message')
|
||||
|
||||
module.exports = (conn, expectedPeerInfo, callback) => {
|
||||
if (typeof expectedPeerInfo === 'function') {
|
||||
callback = expectedPeerInfo
|
||||
expectedPeerInfo = null
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('WARNING: no expected peer info was given, identify will not be able to verify peer integrity')
|
||||
}
|
||||
|
||||
pull(
|
||||
conn,
|
||||
lp.decode(),
|
||||
take(1),
|
||||
collect((err, data) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// connection got closed graciously
|
||||
if (data.length === 0) {
|
||||
return callback(new Error('conn was closed, did not receive data'))
|
||||
}
|
||||
|
||||
const input = msg.decode(data[0])
|
||||
|
||||
PeerId.createFromPubKey(input.publicKey, (err, id) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const peerInfo = new PeerInfo(id)
|
||||
if (expectedPeerInfo && expectedPeerInfo.id.toB58String() !== id.toB58String()) {
|
||||
return callback(new Error('invalid peer'))
|
||||
}
|
||||
|
||||
try {
|
||||
input.listenAddrs
|
||||
.map(multiaddr)
|
||||
.forEach((ma) => peerInfo.multiaddrs.add(ma))
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
let observedAddr
|
||||
|
||||
try {
|
||||
observedAddr = getObservedAddrs(input)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// Copy the protocols
|
||||
peerInfo.protocols = new Set(input.protocols)
|
||||
|
||||
callback(null, peerInfo, observedAddr)
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
function getObservedAddrs (input) {
|
||||
if (!hasObservedAddr(input)) {
|
||||
return []
|
||||
}
|
||||
|
||||
let addrs = input.observedAddr
|
||||
|
||||
if (!Array.isArray(addrs)) {
|
||||
addrs = [addrs]
|
||||
}
|
||||
|
||||
return addrs.map((oa) => multiaddr(oa))
|
||||
}
|
||||
|
||||
function hasObservedAddr (input) {
|
||||
return input.observedAddr && input.observedAddr.length > 0
|
||||
}
|
7
src/identify/index.js
Normal file
7
src/identify/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
exports = module.exports
|
||||
exports.multicodec = '/ipfs/id/1.0.0'
|
||||
exports.listener = require('./listener')
|
||||
exports.dialer = require('./dialer')
|
||||
exports.message = require('./message')
|
35
src/identify/listener.js
Normal file
35
src/identify/listener.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const values = require('pull-stream/sources/values')
|
||||
const lp = require('pull-length-prefixed')
|
||||
|
||||
const msg = require('./message')
|
||||
|
||||
module.exports = (conn, pInfoSelf) => {
|
||||
// send what I see from the other + my Info
|
||||
conn.getObservedAddrs((err, observedAddrs) => {
|
||||
if (err) { return }
|
||||
observedAddrs = observedAddrs[0]
|
||||
|
||||
let publicKey = Buffer.alloc(0)
|
||||
if (pInfoSelf.id.pubKey) {
|
||||
publicKey = pInfoSelf.id.pubKey.bytes
|
||||
}
|
||||
|
||||
const msgSend = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: publicKey,
|
||||
listenAddrs: pInfoSelf.multiaddrs.toArray().map((ma) => ma.buffer),
|
||||
observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from(''),
|
||||
protocols: Array.from(pInfoSelf.protocols)
|
||||
})
|
||||
|
||||
pull(
|
||||
values([msgSend]),
|
||||
lp.encode(),
|
||||
conn
|
||||
)
|
||||
})
|
||||
}
|
30
src/identify/message.js
Normal file
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
|
587
src/index.js
587
src/index.js
@ -1,52 +1,75 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const assert = require('assert')
|
||||
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 Switch = require('libp2p-switch')
|
||||
const Ping = require('libp2p-ping')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Switch = require('./switch')
|
||||
const Ping = require('./ping')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const ConnectionManager = require('libp2p-connection-manager')
|
||||
const ConnectionManager = require('./connection-manager')
|
||||
|
||||
const { emitFirst } = require('./util')
|
||||
const peerRouting = require('./peer-routing')
|
||||
const contentRouting = require('./content-routing')
|
||||
const dht = require('./dht')
|
||||
const pubsub = require('./pubsub')
|
||||
const getPeerInfo = require('./get-peer-info')
|
||||
const { getPeerInfoRemote } = require('./get-peer-info')
|
||||
const validateConfig = require('./config').validate
|
||||
const { codes } = require('./errors')
|
||||
|
||||
exports = module.exports
|
||||
const notStarted = (action, state) => {
|
||||
return errCode(
|
||||
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
|
||||
codes.ERR_NODE_NOT_STARTED
|
||||
)
|
||||
}
|
||||
|
||||
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
|
||||
|
||||
class Node extends EventEmitter {
|
||||
/**
|
||||
* @fires Libp2p#error Emitted when an error occurs
|
||||
* @fires Libp2p#peer:connect Emitted when a peer is connected to this node
|
||||
* @fires Libp2p#peer:disconnect Emitted when a peer disconnects from this node
|
||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
||||
* @fires Libp2p#start Emitted when the node and its services has started
|
||||
* @fires Libp2p#stop Emitted when the node and its services has stopped
|
||||
*/
|
||||
class Libp2p extends EventEmitter {
|
||||
constructor (_options) {
|
||||
super()
|
||||
// validateConfig will ensure the config is correct,
|
||||
// and add default values where appropriate
|
||||
_options = validateConfig(_options)
|
||||
this._options = validateConfig(_options)
|
||||
|
||||
this.peerInfo = _options.peerInfo
|
||||
this.peerBook = _options.peerBook || new PeerBook()
|
||||
this.datastore = this._options.datastore
|
||||
this.peerInfo = this._options.peerInfo
|
||||
this.peerBook = this._options.peerBook || new PeerBook()
|
||||
|
||||
this._modules = _options.modules
|
||||
this._config = _options.config
|
||||
this._isStarted = false
|
||||
this._modules = this._options.modules
|
||||
this._config = this._options.config
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = [] // Discovery service instances/references
|
||||
|
||||
this._switch = new Switch(this.peerInfo, this.peerBook, _options.switch)
|
||||
// create the switch, and listen for errors
|
||||
this._switch = new Switch(this.peerInfo, this.peerBook, this._options.switch)
|
||||
this._switch.on('error', (...args) => this.emit('error', ...args))
|
||||
|
||||
this.stats = this._switch.stats
|
||||
this.connectionManager = new ConnectionManager(this, _options.connectionManager)
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
|
||||
// Attach stream multiplexers
|
||||
if (this._modules.streamMuxer) {
|
||||
let muxers = this._modules.streamMuxer
|
||||
const muxers = this._modules.streamMuxer
|
||||
muxers.forEach((muxer) => this._switch.connection.addStreamMuxer(muxer))
|
||||
|
||||
// If muxer exists
|
||||
@ -59,7 +82,6 @@ class Node extends EventEmitter {
|
||||
// reuse this muxed connection
|
||||
this._switch.on('peer-mux-established', (peerInfo) => {
|
||||
this.emit('peer:connect', peerInfo)
|
||||
this.peerBook.put(peerInfo)
|
||||
})
|
||||
|
||||
this._switch.on('peer-mux-closed', (peerInfo) => {
|
||||
@ -67,9 +89,17 @@ class Node extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
// 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)
|
||||
})
|
||||
|
||||
// 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)
|
||||
})
|
||||
@ -83,39 +113,233 @@ class Node extends EventEmitter {
|
||||
}
|
||||
|
||||
// dht provided components (peerRouting, contentRouting, dht)
|
||||
if (this._config.EXPERIMENTAL.dht) {
|
||||
if (this._config.dht.enabled) {
|
||||
const DHT = this._modules.dht
|
||||
|
||||
this._dht = new DHT(this._switch, {
|
||||
kBucketSize: this._config.dht.kBucketSize || 20,
|
||||
// TODO make datastore an option of libp2p itself so
|
||||
// that other things can use it as well
|
||||
datastore: dht.datastore
|
||||
datastore: this.datastore,
|
||||
...this._config.dht
|
||||
})
|
||||
}
|
||||
|
||||
// enable/disable pubsub
|
||||
if (this._config.EXPERIMENTAL.pubsub) {
|
||||
this.pubsub = pubsub(this)
|
||||
// start pubsub
|
||||
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
|
||||
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
|
||||
}
|
||||
|
||||
// Attach remaining APIs
|
||||
// peer and content routing will automatically get modules from _modules and _dht
|
||||
this.peerRouting = peerRouting(this)
|
||||
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'
|
||||
},
|
||||
STARTING: {
|
||||
done: 'STARTED',
|
||||
abort: 'STOPPED',
|
||||
stop: 'STOPPING'
|
||||
},
|
||||
STARTED: {
|
||||
stop: 'STOPPING',
|
||||
start: 'STARTED'
|
||||
},
|
||||
STOPPING: {
|
||||
stop: 'STOPPING',
|
||||
done: 'STOPPED'
|
||||
}
|
||||
})
|
||||
this.state.on('STARTING', () => {
|
||||
log('libp2p is starting')
|
||||
this._onStarting()
|
||||
})
|
||||
this.state.on('STOPPING', () => {
|
||||
log('libp2p is stopping')
|
||||
this._onStopping()
|
||||
})
|
||||
this.state.on('STARTED', () => {
|
||||
log('libp2p has started')
|
||||
this.emit('start')
|
||||
})
|
||||
this.state.on('STOPPED', () => {
|
||||
log('libp2p has stopped')
|
||||
this.emit('stop')
|
||||
})
|
||||
this.state.on('error', (err) => {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
})
|
||||
|
||||
// Once we start, emit and dial any peers we may have already discovered
|
||||
this.state.on('STARTED', () => {
|
||||
this.peerBook.getAllArray().forEach((peerInfo) => {
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
})
|
||||
})
|
||||
|
||||
this._peerDiscovered = this._peerDiscovered.bind(this)
|
||||
|
||||
// promisify all instance methods
|
||||
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
|
||||
this[method] = promisify(this[method], { context: this })
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
* Start the libp2p node
|
||||
* - create listeners on the multiaddrs the Peer wants to listen
|
||||
/**
|
||||
* Overrides EventEmitter.emit to conditionally emit errors
|
||||
* if there is a handler. If not, errors will be logged.
|
||||
* @param {string} eventName
|
||||
* @param {...any} args
|
||||
* @returns {void}
|
||||
*/
|
||||
start (callback) {
|
||||
emit (eventName, ...args) {
|
||||
if (eventName === 'error' && !this._events.error) {
|
||||
log.error(...args)
|
||||
} else {
|
||||
super.emit(eventName, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the libp2p node and all sub services
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
start (callback = () => {}) {
|
||||
emitFirst(this, ['error', 'start'], callback)
|
||||
this.state('start')
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
stop (callback = () => {}) {
|
||||
emitFirst(this, ['error', 'stop'], callback)
|
||||
this.state('stop')
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
return this.state ? this.state._state === 'STARTED' : false
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials to the provided peer. If successful, the `PeerInfo` of the
|
||||
* peer will be added to the nodes `PeerBook`
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dial (peer, callback) {
|
||||
this.dialProtocol(peer, null, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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`,
|
||||
* and the `Connection` will be sent in the callback
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialProtocol (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.dial(peerInfo, protocol, callback)
|
||||
}, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `dial` and `dialProtocol`, but the callback will contain a
|
||||
* Connection State Machine.
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, ConnectionFSM)} 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
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
this._switch.dialFSM(peerInfo, protocol, callback)
|
||||
}, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the given peer
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
hangUp (peer, 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))
|
||||
}
|
||||
|
||||
getPeerInfoRemote(peer, this)
|
||||
.then(peerInfo => {
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
}, callback)
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
|
||||
_onStarting () {
|
||||
if (!this._modules.transport) {
|
||||
return callback(new Error('no transports were present'))
|
||||
this.emit('error', new Error('no transports were present'))
|
||||
return this.state('abort')
|
||||
}
|
||||
|
||||
let ws
|
||||
@ -126,7 +350,7 @@ class Node extends EventEmitter {
|
||||
this.peerInfo.multiaddrs.toArray().forEach((ma) => {
|
||||
if (!ma.getPeerId()) {
|
||||
maOld.push(ma)
|
||||
maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()))
|
||||
maNew.push(ma.encapsulate('/p2p/' + this.peerInfo.id.toB58String()))
|
||||
}
|
||||
})
|
||||
this.peerInfo.multiaddrs.replace(maOld, maNew)
|
||||
@ -137,13 +361,13 @@ class Node extends EventEmitter {
|
||||
let t
|
||||
|
||||
if (typeof Transport === 'function') {
|
||||
t = new Transport()
|
||||
t = new Transport({ libp2p: this })
|
||||
} else {
|
||||
t = Transport
|
||||
}
|
||||
|
||||
if (t.filter(multiaddrs).length > 0) {
|
||||
this._switch.transport.add(t.tag || t.constructor.name, t)
|
||||
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
|
||||
@ -163,62 +387,6 @@ class Node extends EventEmitter {
|
||||
this._switch.transport.add(ws.tag || ws.constructor.name, ws)
|
||||
}
|
||||
|
||||
// all transports need to be setup before discover starts
|
||||
if (this._modules.peerDiscovery) {
|
||||
each(this._modules.peerDiscovery, (D, _cb) => {
|
||||
let config = {}
|
||||
|
||||
if (D.tag &&
|
||||
this._config.peerDiscovery &&
|
||||
this._config.peerDiscovery[D.tag]) {
|
||||
config = this._config.peerDiscovery[D.tag]
|
||||
}
|
||||
|
||||
// If not configured to be enabled/disabled then enable by default
|
||||
const enabled = config.enabled == null ? true : config.enabled
|
||||
|
||||
// If enabled then start it
|
||||
if (enabled) {
|
||||
let d
|
||||
|
||||
if (typeof D === 'function') {
|
||||
d = new D(Object.assign({}, config, { peerInfo: this.peerInfo }))
|
||||
} else {
|
||||
d = D
|
||||
}
|
||||
|
||||
d.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
|
||||
this._discovery.push(d)
|
||||
d.start(_cb)
|
||||
} else {
|
||||
_cb()
|
||||
}
|
||||
}, cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
(cb) => {
|
||||
// TODO: chicken-and-egg problem #1:
|
||||
// have to set started here because DHT requires libp2p is already started
|
||||
this._isStarted = true
|
||||
if (this._dht) {
|
||||
this._dht.start(cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
(cb) => {
|
||||
// TODO: chicken-and-egg problem #2:
|
||||
// have to set started here because FloodSub requires libp2p is already started
|
||||
if (this._floodSub) {
|
||||
this._floodSub.start(cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
|
||||
(cb) => {
|
||||
// detect which multiaddrs we don't have a transport for and remove them
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
|
||||
@ -231,37 +399,63 @@ class Node extends EventEmitter {
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
this.emit('start')
|
||||
if (this._dht) {
|
||||
this._dht.start(() => {
|
||||
this._dht.on('peer', this._peerDiscovered)
|
||||
cb()
|
||||
})
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
], callback)
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*/
|
||||
stop (callback) {
|
||||
series([
|
||||
},
|
||||
(cb) => {
|
||||
if (this._modules.peerDiscovery) {
|
||||
// stop all discoveries before continuing with shutdown
|
||||
return parallel(
|
||||
this._discovery.map((d) => {
|
||||
return (_cb) => d.stop(() => { _cb() })
|
||||
}),
|
||||
cb
|
||||
)
|
||||
if (this.pubsub) {
|
||||
return this.pubsub.start(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
// Peer Discovery
|
||||
(cb) => {
|
||||
if (this._floodSub) {
|
||||
return this._floodSub.stop(cb)
|
||||
if (this._modules.peerDiscovery) {
|
||||
this._setupPeerDiscovery(cb)
|
||||
} else {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
], (err) => {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
return this.state('stop')
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
_onStopping () {
|
||||
series([
|
||||
(cb) => {
|
||||
// stop all discoveries before continuing with shutdown
|
||||
parallel(
|
||||
this._discovery.map((d) => {
|
||||
d.removeListener('peer', this._peerDiscovered)
|
||||
return (_cb) => d.stop((err) => {
|
||||
log.error('an error occurred stopping the discovery service', err)
|
||||
_cb()
|
||||
})
|
||||
}),
|
||||
cb
|
||||
)
|
||||
},
|
||||
(cb) => {
|
||||
if (this.pubsub) {
|
||||
return this.pubsub.stop(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
if (this._dht) {
|
||||
this._dht.removeListener('peer', this._peerDiscovered)
|
||||
return this._dht.stop(cb)
|
||||
}
|
||||
cb()
|
||||
@ -271,82 +465,119 @@ class Node extends EventEmitter {
|
||||
this._switch.stop(cb)
|
||||
},
|
||||
(cb) => {
|
||||
this.emit('stop')
|
||||
cb()
|
||||
// Ensures idempotent restarts, ignore any errors
|
||||
// from removeAll, they're not useful at this point
|
||||
this._switch.transport.removeAll(() => cb())
|
||||
}
|
||||
], (err) => {
|
||||
this._isStarted = false
|
||||
callback(err)
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
return this._isStarted
|
||||
/**
|
||||
* Handles discovered peers. Each discovered peer will be emitted via
|
||||
* the `peer:discovery` event. If auto dial is enabled for libp2p
|
||||
* and the current connection count is under the low watermark, the
|
||||
* peer will be dialed.
|
||||
*
|
||||
* TODO: If `peerBook.put` becomes centralized, https://github.com/libp2p/js-libp2p/issues/345,
|
||||
* it would be ideal if only new peers were emitted. Currently, with
|
||||
* other modules adding peers to the `PeerBook` we have no way of knowing
|
||||
* if a peer is new or not, so it has to be emitted.
|
||||
*
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
_peerDiscovered (peerInfo) {
|
||||
if (peerInfo.id.toB58String() === this.peerInfo.id.toB58String()) {
|
||||
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
||||
return
|
||||
}
|
||||
peerInfo = this.peerBook.put(peerInfo)
|
||||
|
||||
if (!this.isStarted()) return
|
||||
|
||||
this.emit('peer:discovery', peerInfo)
|
||||
this._maybeConnect(peerInfo)
|
||||
}
|
||||
|
||||
dial (peer, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dial(peerInfo, (err) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this.peerBook.put(peerInfo)
|
||||
callback()
|
||||
})
|
||||
/**
|
||||
* Will dial to the given `peerInfo` if the current number of
|
||||
* connected peers is less than the configured `ConnectionManager`
|
||||
* minPeers.
|
||||
* @private
|
||||
* @param {PeerInfo} peerInfo
|
||||
*/
|
||||
_maybeConnect (peerInfo) {
|
||||
// If auto dialing is on, check if we should dial
|
||||
if (this._config.peerDiscovery.autoDial === true && !peerInfo.isConnected()) {
|
||||
const minPeers = this._options.connectionManager.minPeers || 0
|
||||
if (minPeers > Object.keys(this._switch.connection.connections).length) {
|
||||
log('connecting to discovered peer')
|
||||
this._switch.dialer.connect(peerInfo, (err) => {
|
||||
err && log.error('could not connect to discovered peer', err)
|
||||
})
|
||||
}
|
||||
|
||||
dialProtocol (peer, protocol, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
/**
|
||||
* Initializes and starts peer discovery services
|
||||
*
|
||||
* @private
|
||||
* @param {function(Error)} callback
|
||||
*/
|
||||
_setupPeerDiscovery (callback) {
|
||||
for (const DiscoveryService of this._modules.peerDiscovery) {
|
||||
let config = {
|
||||
enabled: true // on by default
|
||||
}
|
||||
|
||||
this._switch.dial(peerInfo, protocol, (err, conn) => {
|
||||
if (err) { return callback(err) }
|
||||
this.peerBook.put(peerInfo)
|
||||
callback(null, conn)
|
||||
if (DiscoveryService.tag &&
|
||||
this._config.peerDiscovery &&
|
||||
this._config.peerDiscovery[DiscoveryService.tag]) {
|
||||
config = { ...config, ...this._config.peerDiscovery[DiscoveryService.tag] }
|
||||
}
|
||||
|
||||
if (config.enabled) {
|
||||
let discoveryService
|
||||
|
||||
if (typeof DiscoveryService === 'function') {
|
||||
discoveryService = new DiscoveryService(Object.assign({}, config, { peerInfo: this.peerInfo }))
|
||||
} else {
|
||||
discoveryService = DiscoveryService
|
||||
}
|
||||
|
||||
discoveryService.on('peer', this._peerDiscovered)
|
||||
this._discovery.push(discoveryService)
|
||||
}
|
||||
}
|
||||
|
||||
each(this._discovery, (d, cb) => {
|
||||
d.start(cb)
|
||||
}, callback)
|
||||
}
|
||||
}
|
||||
|
||||
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}
|
||||
*/
|
||||
module.exports.createLibp2p = promisify((options, callback) => {
|
||||
if (options.peerInfo) {
|
||||
return nextTick(callback, null, new Libp2p(options))
|
||||
}
|
||||
PeerInfo.create((err, peerInfo) => {
|
||||
if (err) return callback(err)
|
||||
options.peerInfo = peerInfo
|
||||
callback(null, new Libp2p(options))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
hangUp (peer, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
})
|
||||
}
|
||||
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(new Error(NOT_STARTED_ERROR_MESSAGE))
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
})
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
||||
|
@ -1,13 +1,59 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const errCode = require('err-code')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
module.exports = (node) => {
|
||||
return {
|
||||
findPeer: (id, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
const routers = node._modules.peerRouting || []
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (node._dht) {
|
||||
routers.unshift(node._dht)
|
||||
}
|
||||
|
||||
node._dht.findPeer(id, callback)
|
||||
}
|
||||
return {
|
||||
/**
|
||||
* Iterates over all peer routers in series to find the given peer.
|
||||
*
|
||||
* @param {String} id The id of the peer to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findPeer: promisify((id, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!routers.length) {
|
||||
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findPeer(id, options, (err, result) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
// If we don't have a result, we need to provide an error to keep trying
|
||||
if (!result || Object.keys(result).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
}
|
||||
|
||||
cb(null, result)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
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)
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user