mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-07 21:01:36 +00:00
Compare commits
113 Commits
Author | SHA1 | Date | |
---|---|---|---|
8e1743cac4 | |||
3ea95ce642 | |||
c4cae29ef3 | |||
a7128f07ec | |||
c3e147df6b | |||
b89445274d | |||
b9e3bcd91e | |||
f5c1cd1fb0 | |||
975e4b0fe0 | |||
9504f1951a | |||
8e1fc78353 | |||
8895a092b6 | |||
f2f361998d | |||
5f702f3481 | |||
03b34cac7d | |||
9c67364caa | |||
a1424826e7 | |||
3d5bba070b | |||
3f314d5e90 | |||
4ee3e1973b | |||
fc6558b897 | |||
3e302570e5 | |||
a34d2bbcc3 | |||
9941414a91 | |||
46cb46188a | |||
1af8472dc6 | |||
f6a4cad827 | |||
b1079474de | |||
a150ea60c5 | |||
aec8e3d3bb | |||
3abf4aeb35 | |||
a36b2112aa | |||
8d3b61710a | |||
5dbbeef311 | |||
3e7594f697 | |||
ce2a624a09 | |||
a64c02838c | |||
74d07e5e8c | |||
eeda056883 | |||
f06e06a006 | |||
28f52bbf75 | |||
ed5f8f853f | |||
0a6bc0d101 | |||
b5c9e48b68 | |||
9942cbd50c | |||
037c965a67 | |||
748b552876 | |||
961b48bb8d | |||
000826db21 | |||
45c33675a7 | |||
a28c878f4a | |||
67067c97d5 | |||
f45cd1c4b5 | |||
0a02207116 | |||
0b854a949f | |||
9014ea657a | |||
f40697975e | |||
6c41e30456 | |||
77e8273a64 | |||
d60922b799 | |||
42b51d8f01 | |||
d19401aa4c | |||
24bb8df521 | |||
58d4f9a915 | |||
239413e331 | |||
01d43a7b60 | |||
37d66fd88c | |||
21e8ced81a | |||
9ae1b758e9 | |||
408868655c | |||
c5f61ac05f | |||
5d0ac529e4 | |||
bc05083207 | |||
169bb806a7 | |||
7809e6444e | |||
f7e1426b9e | |||
7d76ba1367 | |||
b538ebdc0a | |||
baedf3fe5a | |||
4ebcdb085c | |||
4448de8432 | |||
585ad52b4c | |||
e50c6abcf2 | |||
49fffda23c | |||
689c35ed1c | |||
1a13e2c6ca | |||
5758db8ea9 | |||
ef9d3ca2c6 | |||
97e3633f47 | |||
e36b67a212 | |||
e977039c8a | |||
a5337c1797 | |||
ee23fb9508 | |||
11a46ea71e | |||
5c72424e57 | |||
0bf0b7cf89 | |||
55020056ee | |||
bb83cacb5a | |||
447d0ed0dd | |||
43eda43f06 | |||
7b93ece7f2 | |||
74bdfd1024 | |||
4d1fcdb3d2 | |||
caf66ea143 | |||
48656712ea | |||
1a5ae74741 | |||
8691465a52 | |||
6350a187c7 | |||
8e3bb09279 | |||
73204958ee | |||
e9e4b731a5 | |||
d0a9fada32 | |||
824a444f56 |
@ -31,6 +31,9 @@ const before = async () => {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
nat: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -45,7 +48,7 @@ const after = async () => {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
bundlesize: { maxSize: '225kB' },
|
||||
bundlesize: { maxSize: '223kB' },
|
||||
hooks: {
|
||||
pre: before,
|
||||
post: after
|
||||
|
144
.github/workflows/main.yml
vendored
Normal file
144
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
name: ci
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- '**'
|
||||
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: yarn lint
|
||||
- uses: gozala/typescript-error-reporter-action@v1.0.8
|
||||
- run: yarn build
|
||||
- run: yarn aegir dep-check
|
||||
- uses: ipfs/aegir/actions/bundle-size@master
|
||||
name: size
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
test-node:
|
||||
needs: check
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [windows-latest, ubuntu-latest, macos-latest]
|
||||
node: [14]
|
||||
fail-fast: true
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node }}
|
||||
- run: yarn
|
||||
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
|
||||
- uses: codecov/codecov-action@v1
|
||||
test-chrome:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: npx aegir test -t browser -t webworker --bail
|
||||
test-firefox:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
|
||||
test-interop:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail
|
||||
test-auto-relay-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- auto-relay
|
||||
test-chat-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- chat
|
||||
test-connection-encryption-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- connection-encryption
|
||||
test-discovery-mechanisms-example:
|
||||
needs: check
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- discovery-mechanisms
|
||||
test-echo-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- echo
|
||||
test-libp2p-in-the-browser-example:
|
||||
needs: check
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- libp2p-in-the-browser
|
||||
test-peer-and-content-routing-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- peer-and-content-routing
|
||||
test-pnet-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- pnet
|
||||
test-protocol-and-stream-muxing-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- protocol-and-stream-muxing
|
||||
test-pubsub-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- pubsub
|
||||
test-transports-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- transports
|
||||
test-webrtc-direct-example:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- run: yarn
|
||||
- run: cd examples && yarn && npm run test -- webrtc-direct
|
50
.travis.yml
50
.travis.yml
@ -1,50 +0,0 @@
|
||||
language: node_js
|
||||
cache: npm
|
||||
stages:
|
||||
- check
|
||||
- test
|
||||
- cov
|
||||
|
||||
node_js:
|
||||
- 'lts/*'
|
||||
- '14'
|
||||
|
||||
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
|
||||
# Remove pull libs once ping is async
|
||||
- npx aegir dep-check -- -i pull-handshake -i pull-stream
|
||||
- npm run lint
|
||||
|
||||
- stage: test
|
||||
name: chrome
|
||||
addons:
|
||||
chrome: stable
|
||||
script:
|
||||
- npx aegir test -t browser -t webworker
|
||||
|
||||
- stage: test
|
||||
name: firefox
|
||||
addons:
|
||||
firefox: latest
|
||||
script:
|
||||
- npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
|
||||
|
||||
- stage: test
|
||||
name: interop
|
||||
script:
|
||||
- cd node_modules/interop-libp2p
|
||||
- npm install
|
||||
- LIBP2P_JS=${TRAVIS_BUILD_DIR}/src/index.js npx aegir test -t node --bail
|
||||
|
||||
notifications:
|
||||
email: false
|
191
CHANGELOG.md
191
CHANGELOG.md
@ -1,3 +1,194 @@
|
||||
## [0.30.12](https://github.com/libp2p/js-libp2p/compare/v0.30.11...v0.30.12) (2021-03-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* the API of es6-promisify is not the same as promisify-es6 ([#905](https://github.com/libp2p/js-libp2p/issues/905)) ([a7128f0](https://github.com/libp2p/js-libp2p/commit/a7128f07ec8d4b729145ecfc6ad1d585ffddea46))
|
||||
|
||||
|
||||
|
||||
## [0.30.11](https://github.com/libp2p/js-libp2p/compare/v0.30.10...v0.30.11) (2021-03-23)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* connection direction should be only inbound or outbound ([9504f19](https://github.com/libp2p/js-libp2p/commit/9504f1951a3cca55bb7b4e25e4934e4024034ee8))
|
||||
* interface-datastore update ([f5c1cd1](https://github.com/libp2p/js-libp2p/commit/f5c1cd1fb07bc73cf9d9da3c2eb4327bed4279a4))
|
||||
|
||||
|
||||
|
||||
## [0.30.10](https://github.com/libp2p/js-libp2p/compare/v0.30.9...v0.30.10) (2021-03-09)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* conn mgr access to moving averages record object ([#897](https://github.com/libp2p/js-libp2p/issues/897)) ([5f702f3](https://github.com/libp2p/js-libp2p/commit/5f702f3481afd4ad4fbc89f0e9b75a6d56b03520))
|
||||
|
||||
|
||||
|
||||
## [0.30.9](https://github.com/libp2p/js-libp2p/compare/v0.30.8...v0.30.9) (2021-02-25)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* transport manager fault tolerance should include tolerance to transport listen fail ([#893](https://github.com/libp2p/js-libp2p/issues/893)) ([3f314d5](https://github.com/libp2p/js-libp2p/commit/3f314d5e90f74583b721386d0c9c5d8363cd4de7))
|
||||
|
||||
|
||||
|
||||
## [0.30.8](https://github.com/libp2p/js-libp2p/compare/v0.30.7...v0.30.8) (2021-02-11)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* routers should only use dht if enabled ([#885](https://github.com/libp2p/js-libp2p/issues/885)) ([a34d2bb](https://github.com/libp2p/js-libp2p/commit/a34d2bbcc3d69ec3006137a909a7e8c53b9d378e))
|
||||
|
||||
|
||||
|
||||
## [0.30.7](https://github.com/libp2p/js-libp2p/compare/v0.30.6...v0.30.7) (2021-02-01)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* do not add observed address received from peers ([#882](https://github.com/libp2p/js-libp2p/issues/882)) ([a36b211](https://github.com/libp2p/js-libp2p/commit/a36b2112aafcee309a02de0cff5440cf69cd53a7))
|
||||
|
||||
|
||||
|
||||
## [0.30.6](https://github.com/libp2p/js-libp2p/compare/v0.30.5...v0.30.6) (2021-01-29)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* peer discovery type in config ([#878](https://github.com/libp2p/js-libp2p/issues/878)) ([3e7594f](https://github.com/libp2p/js-libp2p/commit/3e7594f69733bf374b374a6065458fa6cae81c5f))
|
||||
* unref nat manager retries ([#877](https://github.com/libp2p/js-libp2p/issues/877)) ([ce2a624](https://github.com/libp2p/js-libp2p/commit/ce2a624a09b3107c0b2b4752e666804ecea54fb5))
|
||||
|
||||
|
||||
|
||||
## [0.30.5](https://github.com/libp2p/js-libp2p/compare/v0.30.4...v0.30.5) (2021-01-28)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* create has optional peer id type ([#875](https://github.com/libp2p/js-libp2p/issues/875)) ([eeda056](https://github.com/libp2p/js-libp2p/commit/eeda05688330c17b810bf47544ef977386623317))
|
||||
|
||||
|
||||
|
||||
## [0.30.4](https://github.com/libp2p/js-libp2p/compare/v0.30.3...v0.30.4) (2021-01-27)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add UPnP NAT manager ([#810](https://github.com/libp2p/js-libp2p/issues/810)) ([0a6bc0d](https://github.com/libp2p/js-libp2p/commit/0a6bc0d1013dfd80ab600e8f74c1544b433ece29))
|
||||
|
||||
|
||||
|
||||
## [0.30.3](https://github.com/libp2p/js-libp2p/compare/v0.30.2...v0.30.3) (2021-01-27)
|
||||
|
||||
|
||||
|
||||
## [0.30.2](https://github.com/libp2p/js-libp2p/compare/v0.30.1...v0.30.2) (2021-01-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* store multiaddrs during content and peer routing queries ([#865](https://github.com/libp2p/js-libp2p/issues/865)) ([45c3367](https://github.com/libp2p/js-libp2p/commit/45c33675a7412c66d0fd4e113ef8506077b6f492))
|
||||
|
||||
|
||||
|
||||
## [0.30.1](https://github.com/libp2p/js-libp2p/compare/v0.30.0...v0.30.1) (2021-01-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* event emitter types with local types ([#864](https://github.com/libp2p/js-libp2p/issues/864)) ([6c41e30](https://github.com/libp2p/js-libp2p/commit/6c41e3045608bcae8061d20501be5751dad8157a))
|
||||
|
||||
|
||||
|
||||
# [0.30.0](https://github.com/libp2p/js-libp2p/compare/v0.29.4...v0.30.0) (2020-12-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove test/dialing/utils extra file ([689c35e](https://github.com/libp2p/js-libp2p/commit/689c35ed1c68e514293a9895d496e2e8440454e9))
|
||||
* types from ipfs integration ([#832](https://github.com/libp2p/js-libp2p/issues/832)) ([9ae1b75](https://github.com/libp2p/js-libp2p/commit/9ae1b758e99e3fc9067e26b4eae4c15ccb1ba303))
|
||||
|
||||
|
||||
### chore
|
||||
|
||||
* update pubsub ([#801](https://github.com/libp2p/js-libp2p/issues/801)) ([e50c6ab](https://github.com/libp2p/js-libp2p/commit/e50c6abcf2ebc80ebf2dfadd015ab21a20cffadc))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* auto relay ([#723](https://github.com/libp2p/js-libp2p/issues/723)) ([caf66ea](https://github.com/libp2p/js-libp2p/commit/caf66ea1439f6b75a0c321a16bd5c5d7d6a2bd47))
|
||||
* auto relay network query for new relays ([0bf0b7c](https://github.com/libp2p/js-libp2p/commit/0bf0b7cf8968d55002ac4c559ffb59985feeb092))
|
||||
* custom announce filter ([ef9d3ca](https://github.com/libp2p/js-libp2p/commit/ef9d3ca2c6f35d692d6079e74088c5146d46eebe))
|
||||
* custom dialer addr sorter ([#792](https://github.com/libp2p/js-libp2p/issues/792)) ([585ad52](https://github.com/libp2p/js-libp2p/commit/585ad52b4c71dd7514e99a287e0318b2b837ec48))
|
||||
* discover and connect to closest peers ([#798](https://github.com/libp2p/js-libp2p/issues/798)) ([baedf3f](https://github.com/libp2p/js-libp2p/commit/baedf3fe5ab946e938db1415d1662452cdfc0cc1))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* pubsub signing policy properties were changed according to libp2p-interfaces changes to a single property. The emitSelf option default value was also modified to match the routers value
|
||||
|
||||
|
||||
|
||||
# [0.30.0-rc.2](https://github.com/libp2p/js-libp2p/compare/v0.30.0-rc.1...v0.30.0-rc.2) (2020-12-15)
|
||||
|
||||
|
||||
|
||||
# [0.30.0-rc.1](https://github.com/libp2p/js-libp2p/compare/v0.30.0-rc.0...v0.30.0-rc.1) (2020-12-11)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* types from ipfs integration ([#832](https://github.com/libp2p/js-libp2p/issues/832)) ([216eb97](https://github.com/libp2p/js-libp2p/commit/216eb9730ef473f73a974c3dbaf306ecdc815c8b))
|
||||
|
||||
|
||||
|
||||
# [0.30.0-rc.0](https://github.com/libp2p/js-libp2p/compare/v0.29.4...v0.30.0-rc.0) (2020-12-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove test/dialing/utils extra file ([3f1dc20](https://github.com/libp2p/js-libp2p/commit/3f1dc20caf1c80078f403deb9174cd06d08567ab))
|
||||
|
||||
|
||||
### chore
|
||||
|
||||
* update pubsub ([#801](https://github.com/libp2p/js-libp2p/issues/801)) ([9205fce](https://github.com/libp2p/js-libp2p/commit/9205fce34d0cd8dd5d32988be34c110fc0a5b6e2))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* auto relay ([#723](https://github.com/libp2p/js-libp2p/issues/723)) ([65ec267](https://github.com/libp2p/js-libp2p/commit/65ec267e7f4826caacd042213c3fbacce589ab5b))
|
||||
* auto relay network query for new relays ([9faf1bf](https://github.com/libp2p/js-libp2p/commit/9faf1bfcf61581acc715b9be78b71dc14501835a))
|
||||
* custom announce filter ([48476d5](https://github.com/libp2p/js-libp2p/commit/48476d504a98b7b51b3e2dc64eab93670fde0c7b))
|
||||
* custom dialer addr sorter ([#792](https://github.com/libp2p/js-libp2p/issues/792)) ([91b15b6](https://github.com/libp2p/js-libp2p/commit/91b15b6790952b4db11264961d9c6f2a96d1fe43))
|
||||
* discover and connect to closest peers ([#798](https://github.com/libp2p/js-libp2p/issues/798)) ([b73106e](https://github.com/libp2p/js-libp2p/commit/b73106eba2d559621f427f7aa788e9b0ef47d135))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* pubsub signing policy properties were changed according to libp2p-interfaces changes to a single property. The emitSelf option default value was also modified to match the routers value
|
||||
|
||||
|
||||
|
||||
<a name="0.29.4"></a>
|
||||
## [0.29.4](https://github.com/libp2p/js-libp2p/compare/v0.29.3...v0.29.4) (2020-12-09)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* dial self ([#826](https://github.com/libp2p/js-libp2p/issues/826)) ([6350a18](https://github.com/libp2p/js-libp2p/commit/6350a18))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* custom and store self agent version + store self protocol version ([#800](https://github.com/libp2p/js-libp2p/issues/800)) ([d0a9fad](https://github.com/libp2p/js-libp2p/commit/d0a9fad))
|
||||
* support custom listener options ([#822](https://github.com/libp2p/js-libp2p/issues/822)) ([8691465](https://github.com/libp2p/js-libp2p/commit/8691465))
|
||||
|
||||
|
||||
|
||||
<a name="0.29.3"></a>
|
||||
## [0.29.3](https://github.com/libp2p/js-libp2p/compare/v0.29.2...v0.29.3) (2020-11-04)
|
||||
|
||||
|
@ -16,8 +16,8 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<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://github.com/libp2p/js-libp2p/actions?query=branch%3Amaster+workflow%3Aci+"><img src="https://img.shields.io/github/workflow/status/libp2p/js-libp2p/ci?label=ci&style=flat-square" /></a>
|
||||
<a href="https://codecov.io/gh/libp2p/js-libp2p"><img src="https://img.shields.io/codecov/c/github/libp2p/js-libp2p/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>
|
||||
@ -147,7 +147,6 @@ List of packages currently in existence for libp2p
|
||||
| [`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) |
|
||||
| **secure channels** |
|
||||
| [`libp2p-noise`](//github.com/NodeFactoryIo/js-libp2p-noise) | [](//github.com/NodeFactoryIo/js-libp2p-noise/releases) | [](https://david-dm.org/NodeFactoryIo/js-libp2p-noise) | [](https://travis-ci.com/NodeFactoryIo/js-libp2p-noise) | [](https://codecov.io/gh/NodeFactoryIo/js-libp2p-noise) | N/A |
|
||||
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | [](https://travis-ci.com/libp2p/js-libp2p-secio) | [](https://codecov.io/gh/libp2p/js-libp2p-secio) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| **stream multiplexers** |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **peer discovery** |
|
||||
|
193
doc/API.md
193
doc/API.md
@ -13,14 +13,14 @@
|
||||
* [`ping`](#ping)
|
||||
* [`multiaddrs`](#multiaddrs)
|
||||
* [`addressManager.getListenAddrs`](#addressmanagergetlistenaddrs)
|
||||
* [`addressmger.getAnnounceAddrs`](#addressmanagergetannounceaddrs)
|
||||
* [`addressManager.getNoAnnounceAddrs`](#addressmanagergetnoannounceaddrs)
|
||||
* [`addressManager.getAnnounceAddrs`](#addressmanagergetannounceaddrs)
|
||||
* [`contentRouting.findProviders`](#contentroutingfindproviders)
|
||||
* [`contentRouting.provide`](#contentroutingprovide)
|
||||
* [`contentRouting.put`](#contentroutingput)
|
||||
* [`contentRouting.get`](#contentroutingget)
|
||||
* [`contentRouting.getMany`](#contentroutinggetmany)
|
||||
* [`peerRouting.findPeer`](#peerroutingfindpeer)
|
||||
* [`peerRouting.getClosestPeers`](#peerroutinggetclosestpeers)
|
||||
* [`peerStore.addressBook.add`](#peerstoreaddressbookadd)
|
||||
* [`peerStore.addressBook.delete`](#peerstoreaddressbookdelete)
|
||||
* [`peerStore.addressBook.get`](#peerstoreaddressbookget)
|
||||
@ -37,6 +37,7 @@
|
||||
* [`peerStore.protoBook.add`](#peerstoreprotobookadd)
|
||||
* [`peerStore.protoBook.delete`](#peerstoreprotobookdelete)
|
||||
* [`peerStore.protoBook.get`](#peerstoreprotobookget)
|
||||
* [`peerStore.protoBook.remove`](#peerstoreprotobookremove)
|
||||
* [`peerStore.protoBook.set`](#peerstoreprotobookset)
|
||||
* [`peerStore.delete`](#peerstoredelete)
|
||||
* [`peerStore.get`](#peerstoreget)
|
||||
@ -90,8 +91,9 @@ Creates an instance of Libp2p.
|
||||
|------|------|-------------|
|
||||
| options | `object` | libp2p options |
|
||||
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use |
|
||||
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network |
|
||||
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, announceFilter: (ma: Array<multiaddr>) => Array<multiaddr> }` | Addresses for transport listening and to advertise to the network |
|
||||
| [options.config] | `object` | libp2p modules configuration and core configuration |
|
||||
| [options.host] | `{ agentVersion: string }` | libp2p host options |
|
||||
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) |
|
||||
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration](./CONFIGURATION.md#configuring-transport-manager) |
|
||||
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
|
||||
@ -99,6 +101,7 @@ Creates an instance of Libp2p.
|
||||
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain [configuration](./CONFIGURATION.md#setup-with-keychain) |
|
||||
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics [configuration](./CONFIGURATION.md#configuring-metrics) |
|
||||
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
|
||||
| [options.peerRouting] | [`object`](./CONFIGURATION.md#setup-with-content-and-peer-routing) | libp2p Peer routing service [configuration](./CONFIGURATION.md#setup-with-content-and-peer-routing) |
|
||||
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore [configuration](./CONFIGURATION.md#configuring-peerstore) |
|
||||
|
||||
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
|
||||
@ -113,12 +116,25 @@ For Libp2p configurations and modules details read the [Configuration Document](
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
// specify options
|
||||
const options = {}
|
||||
async function main () {
|
||||
// specify options
|
||||
const options = {
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
}
|
||||
|
||||
// create libp2p
|
||||
const libp2p = await Libp2p.create(options)
|
||||
// create libp2p
|
||||
const libp2p = await Libp2p.create(options)
|
||||
}
|
||||
|
||||
main()
|
||||
```
|
||||
|
||||
Note: The [`PeerId`][peer-id] option is not required and will be generated if it is not provided.
|
||||
@ -130,12 +146,30 @@ As an alternative, it is possible to create a Libp2p instance with the construct
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
// specify options
|
||||
const options = {}
|
||||
async function main () {
|
||||
const peerId = await PeerId.create();
|
||||
|
||||
// create libp2p
|
||||
const libp2p = new Libp2p(options)
|
||||
// specify options
|
||||
// peerId is required when Libp2p is instantiated via the constructor
|
||||
const options = {
|
||||
peerId,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
}
|
||||
}
|
||||
|
||||
// create libp2p
|
||||
const libp2p = new Libp2p(options)
|
||||
}
|
||||
|
||||
main()
|
||||
```
|
||||
|
||||
Required keys in the `options` object:
|
||||
@ -482,26 +516,6 @@ const announceMa = libp2p.addressManager.getAnnounceAddrs()
|
||||
// [ <Multiaddr 047f00000106f9ba - /dns4/peer.io/...> ]
|
||||
```
|
||||
|
||||
### addressManager.getNoAnnounceAddrs
|
||||
|
||||
Get the multiaddrs that were provided to not announce to the network.
|
||||
|
||||
`libp2p.addressManager.getNoAnnounceAddrs()`
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `Array<Multiaddr>` | Provided noAnnounce multiaddrs |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// ...
|
||||
const noAnnounceMa = libp2p.addressManager.getNoAnnounceAddrs()
|
||||
// [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]
|
||||
```
|
||||
|
||||
### transportManager.getAddrs
|
||||
|
||||
Get the multiaddrs that libp2p transports are using to listen on.
|
||||
@ -665,7 +679,7 @@ Queries the DHT for the n values stored for the given key (without sorting).
|
||||
// ...
|
||||
|
||||
const key = '/key'
|
||||
const { from, val } = await libp2p.contentRouting.get(key)
|
||||
const records = await libp2p.contentRouting.getMany(key, 2)
|
||||
```
|
||||
|
||||
### peerRouting.findPeer
|
||||
@ -695,6 +709,36 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e
|
||||
const peer = await libp2p.peerRouting.findPeer(peerId, options)
|
||||
```
|
||||
|
||||
### peerRouting.getClosestPeers
|
||||
|
||||
Iterates over all content routers in series to get the closest peers of the given key.
|
||||
Once a content router succeeds, the iteration will stop. If the DHT is enabled, it will be queried first.
|
||||
|
||||
`libp2p.peerRouting.getClosestPeers(cid, options)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| key | `Uint8Array` | A CID like key |
|
||||
| options | `object` | operation options |
|
||||
| options.timeout | `number` | How long the query can take (ms). |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }` | Async iterator for peer data |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
// Iterate over the closest peers found for the given key
|
||||
for await (const peer of libp2p.peerRouting.getClosestPeers(key)) {
|
||||
console.log(peer.id, peer.multiaddrs)
|
||||
}
|
||||
```
|
||||
|
||||
### peerStore.addressBook.add
|
||||
|
||||
Adds known `multiaddrs` of a given peer. If the peer is not known, it will be set with the provided multiaddrs.
|
||||
@ -843,32 +887,6 @@ Consider using `addressBook.add()` if you're not sure this is what you want to d
|
||||
peerStore.addressBook.add(peerId, multiaddr)
|
||||
```
|
||||
|
||||
### peerStore.protoBook.add
|
||||
|
||||
Add known `protocols` of a given peer.
|
||||
|
||||
`peerStore.protoBook.add(peerId, protocols)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | [`PeerId`][peer-id] | peerId to set |
|
||||
| protocols | `Array<string>` | protocols to add |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `ProtoBook` | Returns the Proto Book component |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.protoBook.add(peerId, protocols)
|
||||
```
|
||||
|
||||
|
||||
### peerStore.keyBook.delete
|
||||
|
||||
Delete the provided peer from the book.
|
||||
@ -1091,6 +1109,31 @@ Set known metadata of a given `peerId`.
|
||||
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
|
||||
```
|
||||
|
||||
### peerStore.protoBook.add
|
||||
|
||||
Add known `protocols` of a given peer.
|
||||
|
||||
`peerStore.protoBook.add(peerId, protocols)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | [`PeerId`][peer-id] | peerId to set |
|
||||
| protocols | `Array<string>` | protocols to add |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `ProtoBook` | Returns the Proto Book component |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.protoBook.add(peerId, protocols)
|
||||
```
|
||||
|
||||
### peerStore.protoBook.delete
|
||||
|
||||
Delete the provided peer from the book.
|
||||
@ -1147,6 +1190,31 @@ peerStore.protoBook.get(peerId)
|
||||
// [ '/proto/1.0.0', '/proto/1.1.0' ]
|
||||
```
|
||||
|
||||
### peerStore.protoBook.remove
|
||||
|
||||
Remove given `protocols` of a given peer.
|
||||
|
||||
`peerStore.protoBook.remove(peerId, protocols)`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Name | Type | Description |
|
||||
|------|------|-------------|
|
||||
| peerId | [`PeerId`][peer-id] | peerId to set |
|
||||
| protocols | `Array<string>` | protocols to remove |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `ProtoBook` | Returns the Proto Book component |
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
peerStore.protoBook.remove(peerId, protocols)
|
||||
```
|
||||
|
||||
### peerStore.protoBook.set
|
||||
|
||||
Set known `protocols` of a given peer.
|
||||
@ -1987,6 +2055,15 @@ This event will be triggered anytime we are disconnected from another peer, rega
|
||||
- `peerId`: instance of [`PeerId`][peer-id]
|
||||
- `protocols`: array of known, supported protocols for the peer (string identifiers)
|
||||
|
||||
### libp2p.addressManager
|
||||
|
||||
#### Our addresses have changed
|
||||
|
||||
This could be in response to a peer telling us about addresses they have observed, or
|
||||
the NatManager performing NAT hole punching.
|
||||
|
||||
`libp2p.addressManager.on('change:addresses', () => {})`
|
||||
|
||||
## Types
|
||||
|
||||
### Stats
|
||||
|
@ -20,6 +20,7 @@
|
||||
- [Customizing DHT](#customizing-dht)
|
||||
- [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing)
|
||||
- [Setup with Relay](#setup-with-relay)
|
||||
- [Setup with Auto Relay](#setup-with-auto-relay)
|
||||
- [Setup with Keychain](#setup-with-keychain)
|
||||
- [Configuring Dialing](#configuring-dialing)
|
||||
- [Configuring Connection Manager](#configuring-connection-manager)
|
||||
@ -27,6 +28,9 @@
|
||||
- [Configuring Metrics](#configuring-metrics)
|
||||
- [Configuring PeerStore](#configuring-peerstore)
|
||||
- [Customizing Transports](#customizing-transports)
|
||||
- [Configuring the NAT Manager](#configuring-the-nat-manager)
|
||||
- [Browser support](#browser-support)
|
||||
- [UPnP and NAT-PMP](#upnp-and-nat-pmp)
|
||||
- [Configuration examples](#configuration-examples)
|
||||
|
||||
## Overview
|
||||
@ -210,10 +214,10 @@ Besides the `modules` and `config`, libp2p allows other internal options and con
|
||||
- This is used in modules such as the DHT. If it is not provided, `js-libp2p` will use an in memory datastore.
|
||||
- `peerId`: the identity of the node, an instance of [libp2p/js-peer-id](https://github.com/libp2p/js-peer-id).
|
||||
- This is particularly useful if you want to reuse the same `peer-id`, as well as for modules like `libp2p-delegated-content-routing`, which need a `peer-id` in their instantiation.
|
||||
- `addresses`: an object containing `listen`, `announce` and `noAnnounce` properties with `Array<string>`:
|
||||
- `addresses`: an object containing `listen`, `announce` and `announceFilter`:
|
||||
- `listen` addresses will be provided to the libp2p underlying transports for listening on them.
|
||||
- `announce` addresses will be used to compute the advertises that the node should advertise to the network.
|
||||
- `noAnnounce` addresses will be used as a filter to compute the advertises that the node should advertise to the network.
|
||||
- `announceFilter`: filter function used to filter announced addresses programmatically: `(ma: Array<multiaddr>) => Array<multiaddr>`. Default: returns all addresses. [`libp2p-utils`](https://github.com/libp2p/js-libp2p-utils) provides useful [multiaddr utilities](https://github.com/libp2p/js-libp2p-utils/blob/master/API.md#multiaddr-isloopbackma) to create your filters.
|
||||
|
||||
### Examples
|
||||
|
||||
@ -260,13 +264,14 @@ const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [MulticastDNS]
|
||||
peerDiscovery: [MulticastDNS, Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
@ -276,6 +281,15 @@ const node = await Libp2p.create({
|
||||
[MulticastDNS.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag:] {
|
||||
list: [ // A list of bootstrap peers to connect to starting up the node
|
||||
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
||||
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
||||
],
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
// .. other discovery module options.
|
||||
}
|
||||
@ -321,6 +335,8 @@ const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
|
||||
const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
@ -331,9 +347,8 @@ const node = await Libp2p.create({
|
||||
config: {
|
||||
pubsub: { // The pubsub options (and defaults) can be found in the pubsub router documentation
|
||||
enabled: true,
|
||||
emitSelf: true, // whether the node should emit to self on publish
|
||||
signMessages: true, // if messages should be signed
|
||||
strictSigning: true // if message signing should be required
|
||||
emitSelf: false, // whether the node should emit to self on publish
|
||||
globalSignaturePolicy: SignaturePolicy.StrictSign // message signing policy
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -376,6 +391,7 @@ const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const ipfsHttpClient = require('ipfs-http-client')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const PeerId = require('peer-id')
|
||||
@ -383,19 +399,34 @@ const PeerId = require('peer-id')
|
||||
// create a peerId
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
const delegatedPeerRouting = new DelegatedPeerRouter(ipfsHttpClient({
|
||||
host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates
|
||||
protocol: 'https',
|
||||
port: 443
|
||||
}))
|
||||
|
||||
const delegatedContentRouting = new DelegatedContentRouter(peerId, ipfsHttpClient({
|
||||
host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates
|
||||
protocol: 'https',
|
||||
port: 443
|
||||
}))
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE],
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerId)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter()
|
||||
],
|
||||
contentRouting: [delegatedContentRouting],
|
||||
peerRouting: [delegatedPeerRouting],
|
||||
},
|
||||
peerId
|
||||
peerId,
|
||||
peerRouting: { // Peer routing configuration
|
||||
refreshManager: { // Refresh known and connected closest peers
|
||||
enabled: true, // Should find the closest peers.
|
||||
interval: 6e5, // Interval for getting the new for closest peers of 10min
|
||||
bootDelay: 10e3 // Delay for the initial query for closest peers
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
@ -419,6 +450,37 @@ const node = await Libp2p.create({
|
||||
hop: {
|
||||
enabled: true, // Allows you to be a relay for other peers
|
||||
active: true // You will attempt to dial destination peers if you are not connected to them
|
||||
},
|
||||
advertise: {
|
||||
bootDelay: 15 * 60 * 1000, // Delay before HOP relay service is advertised on the network
|
||||
enabled: true, // Allows you to disable the advertise of the Hop service
|
||||
ttl: 30 * 60 * 1000 // Delay Between HOP relay service advertisements on the network
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Setup with Auto Relay
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
|
||||
enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay.
|
||||
autoRelay: {
|
||||
enabled: true, // Allows you to bind to relays with HOP enabled for improving node dialability
|
||||
maxListeners: 2 // Configure maximum number of HOP relays to use
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -466,6 +528,7 @@ Dialing in libp2p can be configured to limit the rate of dialing, and how long d
|
||||
| maxDialsPerPeer | `number` | How many multiaddrs we can dial per peer, in parallel. |
|
||||
| dialTimeout | `number` | Second dial timeout per peer in ms. |
|
||||
| resolvers | `object` | Dial [Resolvers](https://github.com/multiformats/js-multiaddr/blob/master/src/resolvers/index.js) for resolving multiaddrs |
|
||||
| addressSorter | `(Array<Address>) => Array<Address>` | Sort the known addresses of a peer before trying to dial. |
|
||||
|
||||
The below configuration example shows how the dialer should be configured, with the current defaults:
|
||||
|
||||
@ -476,6 +539,7 @@ const MPLEX = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const { dnsaddrResolver } = require('multiaddr/src/resolvers')
|
||||
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
@ -489,7 +553,8 @@ const node = await Libp2p.create({
|
||||
dialTimeout: 30e3,
|
||||
resolvers: {
|
||||
dnsaddr: dnsaddrResolver
|
||||
}
|
||||
},
|
||||
addressSorter: publicAddressesFirst
|
||||
}
|
||||
```
|
||||
|
||||
@ -651,6 +716,69 @@ const node = await Libp2p.create({
|
||||
})
|
||||
```
|
||||
|
||||
During Libp2p startup, transport listeners will be created for the configured listen multiaddrs. Some transports support custom listener options and you can set them using the `listenerOptions` in the transport configuration. For example, [libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star) transport listener supports the configuration of its underlying [simple-peer](https://github.com/feross/simple-peer) ice server(STUN/TURN) config as follows:
|
||||
|
||||
```js
|
||||
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebRTCStar],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/dns4/your-wrtc-star.pub/tcp/443/wss/p2p-webrtc-star'] // your webrtc dns multiaddr
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
listenerOptions: {
|
||||
config: {
|
||||
iceServers: [
|
||||
{"urls": ["turn:YOUR.TURN.SERVER:3478"], "username": "YOUR.USER", "credential": "YOUR.PASSWORD"},
|
||||
{"urls": ["stun:YOUR.STUN.SERVER:3478"], "username": "", "credential": ""}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Configuring the NAT Manager
|
||||
|
||||
Network Address Translation (NAT) is a function performed by your router to enable multiple devices on your local network to share a single IPv4 address. It's done transparently for outgoing connections, ensuring the correct response traffic is routed to your computer, but if you wish to accept incoming connections some configuration is necessary.
|
||||
|
||||
The NAT manager can be configured as follows:
|
||||
|
||||
```js
|
||||
const node = await Libp2p.create({
|
||||
config: {
|
||||
nat: {
|
||||
description: 'my-node', // set as the port mapping description on the router, defaults the current libp2p version and your peer id
|
||||
enabled: true, // defaults to true
|
||||
gateway: '192.168.1.1', // leave unset to auto-discover
|
||||
externalIp: '80.1.1.1', // leave unset to auto-discover
|
||||
ttl: 7200, // TTL for port mappings (min 20 minutes)
|
||||
keepAlive: true, // Refresh port mapping after TTL expires
|
||||
pmp: {
|
||||
enabled: false, // defaults to false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
##### Browser support
|
||||
|
||||
Browsers cannot open TCP ports or send the UDP datagrams necessary to configure external port mapping - to accept incoming connections in the browser please use a WebRTC transport.
|
||||
|
||||
##### UPnP and NAT-PMP
|
||||
|
||||
By default under nodejs libp2p will attempt to use [UPnP](https://en.wikipedia.org/wiki/Universal_Plug_and_Play) to configure your router to allow incoming connections to any TCP transports that have been configured.
|
||||
|
||||
[NAT-PMP](http://miniupnp.free.fr/nat-pmp.html) is a feature of some modern routers which performs a similar job to UPnP. NAT-PMP is disabled by default, if enabled libp2p will try to use NAT-PMP and will fall back to UPnP if it fails.
|
||||
|
||||
## Configuration examples
|
||||
|
||||
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
|
||||
|
185
doc/migrations/v0.29-v0.30.md
Normal file
185
doc/migrations/v0.29-v0.30.md
Normal file
@ -0,0 +1,185 @@
|
||||
<!--Specify versions for migration below-->
|
||||
# Migrating to libp2p@30
|
||||
|
||||
A migration guide for refactoring your application code from libp2p v0.29.x to v0.30.0.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [API](#api)
|
||||
- [Development and Testing](#development-and-testing)
|
||||
- [Module Updates](#module-updates)
|
||||
|
||||
## API
|
||||
|
||||
### Pubsub
|
||||
|
||||
`js-libp2p` nodes prior to this version were emitting to self on publish by default.
|
||||
This default value was changed on the pubsub router layer in the past, but we kept it overwritten in libp2p to avoid an upstream breaking change.
|
||||
Now `js-libp2p` does not overwrite the pubsub router options anymore. Upstream projects that want this feature should enable it on their libp2p configuration.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
The [Pubsub interface](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/pubsub) was updated on its message signing properties, taking into account the Gossipsub spec updates on [libp2p/specs#294](https://github.com/libp2p/specs/pull/294) and [libp2p/specs#299](https://github.com/libp2p/specs/pull/299)
|
||||
|
||||
The signing property is now based on a `globalSignaturePolicy` option instead of the previous `signMessages` and `strictSigning` options. The default to strict signing pubsub messages was kept, but if you would like to disable it, the properties should be changed as follows:
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
signMessages: false,
|
||||
strictSigning: false
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
// ... Add required modules according to the Configuration docs
|
||||
pubsub: Gossipsub
|
||||
},
|
||||
config: {
|
||||
pubsub: {
|
||||
globalSignaturePolicy: SignaturePolicy.StrictNoSign
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Addresses
|
||||
|
||||
Libp2p has supported `noAnnounce` addresses configuration for some time now. However, it did not provide the best developer experience. In this release, we dropped the `noAnnounce` configuration property in favor of an `announceFilter` property function.
|
||||
|
||||
**Before**
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
noAnnounce: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
},
|
||||
// ... additional configuration per the Configuration docs
|
||||
})
|
||||
```
|
||||
|
||||
**After**
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
// Libp2p utils has several multiaddr utils you can leverage
|
||||
const isPrivate = require('libp2p-utils/src/multiaddr/is-private')
|
||||
|
||||
const libp2p = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/8000/ws'],
|
||||
// Filter function: (ma: Array<multiaddr>) => Array<multiaddr>
|
||||
announceFilter: (multiaddrs) => multiaddrs.filter(m => !isPrivate(m))
|
||||
},
|
||||
// ... additional configuration per the Configuration docs
|
||||
})
|
||||
```
|
||||
|
||||
It is important pointing out another change regarding address advertising. This is not an API breaking change, but it might have influence on your libp2p setup.
|
||||
Previously, when using the addresses `announce` property, its multiaddrs were concatenated with the `listen` multiaddrs and then they were filtered out by the `noAnnounce` multiaddrs, in order to create the list of multiaddrs to advertise.
|
||||
In `libp2p@0.30` the logic now operates as follows:
|
||||
|
||||
- If `announce` addresses are provided, only they will be announced (no filters are applied)
|
||||
- If `announce` is not provided, the transport addresses will be filtered (if a filter is provided)
|
||||
- if the `announceFilter` is provide it will be passed the transport addresses
|
||||
|
||||
## Development and Testing
|
||||
|
||||
While this is not an API breaking change, there was a behavioral breaking change on the Websockets transport when in a browser environment. This change might create issues on local test setups.
|
||||
`libp2p-websockets` has allowed `TCP` and `DNS` addresses, both with `ws` or `wss` to be used for dial purposes. Taking into account security (and browser policies), we are now restricting addresses to `DNS` + `wss` in the browser
|
||||
With this new behavior, if you need to use non DNS addresses, you can configure your libp2p node as follows:
|
||||
|
||||
```js
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const filters = require('libp2p-websockets/src/filters')
|
||||
const Libp2p = require('libp2p')
|
||||
|
||||
const transportKey = Websockets.prototype[Symbol.toStringTag]
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets]
|
||||
// ... Add required modules according to the Configuration docs
|
||||
},
|
||||
config: {
|
||||
transport: {
|
||||
[transportKey]: {
|
||||
filter: filters.all
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Module Updates
|
||||
|
||||
With this release you should update the following libp2p modules if you are relying on them:
|
||||
|
||||
<!--Specify module versions in JSON for migration below.
|
||||
It's recommended to check package.json changes for this:
|
||||
`git diff <release> <prev> -- package.json`
|
||||
-->
|
||||
|
||||
```json
|
||||
"libp2p-delegated-content-routing": "^0.8.0",
|
||||
"libp2p-delegated-peer-routing": "^0.8.0",
|
||||
"libp2p-floodsub": "^0.24.0",
|
||||
"libp2p-gossipsub": "^0.7.0",
|
||||
"libp2p-websockets": "^0.15.0",
|
||||
```
|
||||
|
||||
Note that some of them do not need to be updated for this libp2p version to work as expected, but we suggest you to keep them updated as part of this release.
|
3
doc/production/DELEGATE_NODES.md
Normal file
3
doc/production/DELEGATE_NODES.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Delegate Nodes
|
||||
|
||||
[TODO](https://github.com/libp2p/js-libp2p/pull/718)
|
65
doc/production/README.md
Normal file
65
doc/production/README.md
Normal file
@ -0,0 +1,65 @@
|
||||
# Production
|
||||
|
||||
Nowadays, you can run JavaScript code in several different environments, some of them with their own particularities. Moreover, you can use `js-libp2p` for a wide range of use cases. Different environments and different use cases mean different configurations and challenges in the network.
|
||||
|
||||
Libp2p nodes can vary from nodes behind an application, to infrastructure nodes that enable the network to operate and to be efficient. In this context, the Libp2p project provides public infrastructure to boost the network, enable nodes connectivity and improve constrained nodes performance. This public infrastructure should be leveraged for learning the concepts and experimenting. When an application on top of libp2p aims to move into production, its own infrastructure should be setup as the public nodes will be intensively used by others and its availability is not guaranteed.
|
||||
|
||||
This guide aims to guide you from using the public infrastructure into setting up your own.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Joining the Network](#joining-the-network)
|
||||
* [Connecting to Nodes with connectivity limitations](#connecting-to-nodes-with-connectivity-limitations)
|
||||
* [`webrtc-star` servers](#webrtc-star-servers)
|
||||
* [Circuit Relay](#circuit-relay)
|
||||
* [Querying the network from the browser](#querying-the-network-from-the-browser)
|
||||
* [Others](#others)
|
||||
* [SSL](#ssl)
|
||||
|
||||
## Joining the Network
|
||||
|
||||
Once a libp2p node stars, it will need to connect to a set of peers in order to establish its overlay network.
|
||||
|
||||
Currently `js-libp2p` is not the best choice for being a bootstrap node. Its DHT needs to be improved, in order to become an effective server to enable other nodes to properly bootstrap their network.
|
||||
|
||||
Setting up a fleet of [`go-libp2p`](https://github.com/libp2p/go-libp2p) nodes is the recommended way to proceed here.
|
||||
|
||||
## Connecting to Nodes with connectivity limitations
|
||||
|
||||
While the libp2p core codebase aims to work in multiple environments, there are some limitations that are not possible to overcome at the time of writing. These limitations include browser nodes, nodes behind NAT, reverse proxies, firewalls, or lack of compatible transports.
|
||||
|
||||
In the browser, libp2p supports two transports: `websockets` and `webrtc-star`. Nowadays, browsers do not support listening for connections, but only to dial known addresses. `webrtc-star` servers can be used to enable libp2p nodes to discover other nodes running on the browser and to help them establish a connection.
|
||||
|
||||
For nodes that cannot be dialed (including browser), circuit relay nodes should be used.
|
||||
|
||||
### `webrtc-star` servers
|
||||
|
||||
Regarding `webRTC` connections, a set of star servers are needed to act as a rendezvous point, where peers can learn about other peers (`peer-discovery`), as well as exchange their SDP offers (signaling data).
|
||||
|
||||
You can read on how to setup your own star servers in [libp2p/js-libp2p-webrtc-star/DEPLOYMENT.md](https://github.com/libp2p/js-libp2p-webrtc-star/blob/master/DEPLOYMENT.md).
|
||||
|
||||
It is worth pointing out that with new discovery protocols on the way, as well as support for distributed signaling, the star servers should be deprecated on the long run.
|
||||
|
||||
### Circuit Relay
|
||||
|
||||
Libp2p nodes acting as circuit relay aim to establish connectivity between libp2p nodes (e.g. IPFS nodes) that wouldn't otherwise be able to establish a direct connection to each other.
|
||||
|
||||
A relay is needed in situations where nodes are behind NAT, reverse proxies, firewalls and/or simply don't support the same transports (e.g. go-libp2p vs. browser-libp2p). The circuit relay protocol exists to overcome those scenarios. Nodes with the `auto-relay` feature enabled can automatically bind themselves on a relay to listen for connections on their behalf.
|
||||
|
||||
You can use [libp2p/js-libp2p-relay-server](https://github.com/libp2p/js-libp2p-relay-server) to setup your own relay server. This also includes an easy to customize Docker setup for a HOP Relay.
|
||||
|
||||
## Querying the network from the browser
|
||||
|
||||
Libp2p nodes in scenarios such as browser environment and constrained devices will not be an efficient node in the libp2p DHT overlay, as a consequence of their known limitations regarding connectivity and performance.
|
||||
|
||||
Aiming to support these type of nodes to find other peers and content in the network, delegate nodes can be setup. With a set of well known IPFS delegate nodes, nodes with limitations in the network can leverage them to perform peer and content routing queries.
|
||||
|
||||
Currently, delegate nodes must be IPFS nodes as the IPFS HTTP API is leveraged by them to make routing queries.
|
||||
|
||||
You can read on how to setup your own set of delegated nodes in [DELEGATE_NODES.md](./DELEGATE_NODES.md).
|
||||
|
||||
## Others
|
||||
|
||||
### SSL
|
||||
|
||||
TODO
|
@ -8,7 +8,7 @@ Let us know if you find any issues, or if you want to contribute and add a new t
|
||||
|
||||
- [Transports](./transports)
|
||||
- [Protocol and Stream Muxing](./protocol-and-stream-muxing)
|
||||
- [Encrypted Communications](./encrypted-communications)
|
||||
- [Connection Encryption](./connection-encryption)
|
||||
- [Discovery Mechanisms](./discovery-mechanisms)
|
||||
- [Peer and Content Routing](./peer-and-content-routing)
|
||||
- [PubSub](./pubsub)
|
||||
|
192
examples/auto-relay/README.md
Normal file
192
examples/auto-relay/README.md
Normal file
@ -0,0 +1,192 @@
|
||||
# Auto relay
|
||||
|
||||
Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s).
|
||||
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
|
||||
|
||||
## 0. Setup the example
|
||||
|
||||
Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example. Once the install finishes, you should move into the example folder with `cd examples/auto-relay`.
|
||||
|
||||
This example comes with 3 main files. A `relay.js` file to be used in the first step, a `listener.js` file to be used in the second step and a `dialer.js` file to be used on the third step. All of these scripts will run their own libp2p node, which will interact with the previous ones. All nodes must be running in order for you to proceed.
|
||||
|
||||
## 1. Set up a relay node
|
||||
|
||||
In the first step of this example, we need to configure and run a relay node in order for our target node to bind to for accepting inbound connections.
|
||||
|
||||
The relay node will need to have its relay subsystem enabled, as well as its HOP capability. It can be configured as follows:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
// TODO check "What is next?" section
|
||||
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true
|
||||
},
|
||||
advertise: {
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
console.log('Listening on:')
|
||||
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
|
||||
```
|
||||
|
||||
The Relay HOP advertise functionality is **NOT** required to be enabled. However, if you are interested in advertising on the network that this node is available to be used as a HOP Relay you can enable it. A content router module or Rendezvous needs to be configured to leverage this option.
|
||||
|
||||
You should now run the following to start the relay node:
|
||||
|
||||
```sh
|
||||
node relay.js
|
||||
```
|
||||
|
||||
This should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started with id QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
Listening on:
|
||||
/ip4/127.0.0.1/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
/ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
```
|
||||
|
||||
## 2. Set up a listener node with Auto Relay Enabled
|
||||
|
||||
One of the typical use cases for Auto Relay is nodes behind a NAT or browser nodes due to their inability to expose a public address. For running a libp2p node that automatically binds itself to connected HOP relays, you can see the following:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const relayAddr = process.argv[2]
|
||||
if (!relayAddr) {
|
||||
throw new Error('the relay address needs to be specified as a parameter')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
autoRelay: {
|
||||
enabled: true,
|
||||
maxListeners: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(relayAddr)
|
||||
|
||||
// Wait for connection and relay to be bind for the example purpose
|
||||
await new Promise((resolve) => {
|
||||
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
// Updated self multiaddrs?
|
||||
if (peerId.equals(node.peerId)) {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
|
||||
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
|
||||
```
|
||||
|
||||
As you can see in the code, we need to provide the relay address, `relayAddr`, as a process argument. This node will dial the provided relay address and automatically bind to it.
|
||||
|
||||
You should now run the following to start the node running Auto Relay:
|
||||
|
||||
```sh
|
||||
node listener.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
|
||||
```
|
||||
|
||||
This should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started with id QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
Connected to the HOP relay QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
|
||||
Advertising with a relay address of /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
```
|
||||
|
||||
Per the address, it is possible to verify that the auto relay node is listening on the circuit relay node address.
|
||||
|
||||
Instead of dialing this relay manually, you could set up this node with the Bootstrap module and provide it in the bootstrap list. Moreover, you can use other `peer-discovery` modules to discover peers in the network and the node will automatically bind to the relays that support HOP until reaching the maximum number of listeners.
|
||||
|
||||
## 3. Set up a dialer node for testing connectivity
|
||||
|
||||
Now that you have a relay node and a node bound to that relay, you can test connecting to the auto relay node via the relay.
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
const autoRelayNodeAddr = process.argv[2]
|
||||
if (!autoRelayNodeAddr) {
|
||||
throw new Error('the auto relay node address needs to be specified')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(autoRelayNodeAddr)
|
||||
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
|
||||
```
|
||||
|
||||
You should now run the following to start the relay node using the listen address from step 2:
|
||||
|
||||
```sh
|
||||
node dialer.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
|
||||
```
|
||||
|
||||
Once you start your test node, it should print out something similar to the following:
|
||||
|
||||
```sh
|
||||
Node started: Qme7iEzDxFoFhhkrsrkHkMnM11aPYjysaehP4NZeUfVMKG
|
||||
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
|
||||
```
|
||||
|
||||
As you can see from the output, the remote address of the established connection uses the relayed connection.
|
||||
|
||||
## 4. What is next?
|
||||
|
||||
Before moving into production, there are a few things that you should take into account.
|
||||
|
||||
A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate.
|
29
examples/auto-relay/dialer.js
Normal file
29
examples/auto-relay/dialer.js
Normal file
@ -0,0 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const autoRelayNodeAddr = process.argv[2]
|
||||
if (!autoRelayNodeAddr) {
|
||||
throw new Error('the auto relay node address needs to be specified')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(autoRelayNodeAddr)
|
||||
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
|
||||
}
|
||||
|
||||
main()
|
47
examples/auto-relay/listener.js
Normal file
47
examples/auto-relay/listener.js
Normal file
@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const relayAddr = process.argv[2]
|
||||
if (!relayAddr) {
|
||||
throw new Error('the relay address needs to be specified as a parameter')
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
autoRelay: {
|
||||
enabled: true,
|
||||
maxListeners: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
|
||||
const conn = await node.dial(relayAddr)
|
||||
|
||||
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
|
||||
|
||||
// Wait for connection and relay to be bind for the example purpose
|
||||
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
// Updated self multiaddrs?
|
||||
if (peerId.equals(node.peerId)) {
|
||||
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
main()
|
40
examples/auto-relay/relay.js
Normal file
40
examples/auto-relay/relay.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
|
||||
async function main () {
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [Websockets],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX]
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
// TODO check "What is next?" section
|
||||
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true
|
||||
},
|
||||
advertise: {
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log(`Node started with id ${node.peerId.toB58String()}`)
|
||||
console.log('Listening on:')
|
||||
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
|
||||
}
|
||||
|
||||
main()
|
94
examples/auto-relay/test.js
Normal file
94
examples/auto-relay/test.js
Normal file
@ -0,0 +1,94 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess (name, args = []) {
|
||||
return execa('node', [path.join(__dirname, name), ...args], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
let output1 = ''
|
||||
let output2 = ''
|
||||
let output3 = ''
|
||||
let relayAddr
|
||||
let autoRelayAddr
|
||||
|
||||
const proc1Ready = pDefer()
|
||||
const proc2Ready = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('relay.js\n')
|
||||
|
||||
const proc1 = startProcess('relay.js')
|
||||
proc1.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output1 += uint8ArrayToString(data)
|
||||
|
||||
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
|
||||
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
|
||||
proc1Ready.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await proc1Ready.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('listener.js\n')
|
||||
|
||||
const proc2 = startProcess('listener.js', [relayAddr])
|
||||
proc2.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output2 += uint8ArrayToString(data)
|
||||
|
||||
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
|
||||
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1]
|
||||
proc2Ready.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await proc2Ready.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 3 process
|
||||
process.stdout.write('dialer.js\n')
|
||||
|
||||
const proc3 = startProcess('dialer.js', [autoRelayAddr])
|
||||
proc3.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output3 += uint8ArrayToString(data)
|
||||
|
||||
if (output3.includes('Connected to the auto relay node via')) {
|
||||
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
|
||||
|
||||
if (remoteAddr === autoRelayAddr) {
|
||||
proc3.kill()
|
||||
proc2.kill()
|
||||
proc1.kill()
|
||||
} else {
|
||||
throw new Error('dialer did not dial through the relay')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
proc1,
|
||||
proc2,
|
||||
proc3
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -3,7 +3,7 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
async function run() {
|
||||
@ -13,7 +13,7 @@ async function run() {
|
||||
])
|
||||
|
||||
// Create a new libp2p node on localhost with a randomly chosen port
|
||||
const nodeDialer = new Node({
|
||||
const nodeDialer = await createLibp2p({
|
||||
peerId: idDialer,
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
|
@ -1,27 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: [ mplex ],
|
||||
connEncryption: [ NOISE ]
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
22
examples/chat/src/libp2p.js
Normal file
22
examples/chat/src/libp2p.js
Normal file
@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
async function createLibp2p(_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [TCP, WS],
|
||||
streamMuxer: [mplex],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
}
|
||||
|
||||
return libp2p.create(defaultsDeep(_options, defaults))
|
||||
}
|
||||
|
||||
module.exports = createLibp2p
|
@ -2,13 +2,13 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const Node = require('./libp2p-bundle.js')
|
||||
const createLibp2p = require('./libp2p.js')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
|
||||
async function run() {
|
||||
// Create a new libp2p node with the given multi-address
|
||||
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
const nodeListener = new Node({
|
||||
const nodeListener = await createLibp2p({
|
||||
peerId: idListener,
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/10333']
|
||||
|
77
examples/chat/test.js
Normal file
77
examples/chat/test.js
Normal file
@ -0,0 +1,77 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess(name) {
|
||||
return execa('node', [path.join(__dirname, name)], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const message = 'test message'
|
||||
let listenerOutput = ''
|
||||
let dialerOutput = ''
|
||||
|
||||
let isListening = false
|
||||
let messageSent = false
|
||||
const listenerReady = pDefer()
|
||||
const dialerReady = pDefer()
|
||||
const messageReceived = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('node listener.js\n')
|
||||
const listenerProc = startProcess('src/listener.js')
|
||||
listenerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
listenerOutput += uint8ArrayToString(data)
|
||||
|
||||
if (!isListening && listenerOutput.includes('Listener ready, listening on')) {
|
||||
listenerReady.resolve()
|
||||
isListening = true
|
||||
} else if (isListening && listenerOutput.includes(message)) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await listenerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('node dialer.js\n')
|
||||
const dialerProc = startProcess('src/dialer.js')
|
||||
dialerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
dialerOutput += uint8ArrayToString(data)
|
||||
|
||||
if (!messageSent && dialerOutput.includes('Type a message and see what happens')) {
|
||||
dialerReady.resolve()
|
||||
dialerProc.stdin.write(message)
|
||||
dialerProc.stdin.write('\n')
|
||||
messageSent = true
|
||||
}
|
||||
})
|
||||
|
||||
await dialerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
await messageReceived.promise
|
||||
process.stdout.write('chat message received\n')
|
||||
|
||||
listenerProc.kill()
|
||||
dialerProc.kill()
|
||||
await Promise.all([
|
||||
listenerProc,
|
||||
dialerProc
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
@ -1,4 +1,4 @@
|
||||
# Encrypted Communications
|
||||
# Connection Encryption
|
||||
|
||||
libp2p can leverage the encrypted communications from the transports it uses (i.e WebRTC). To ensure that every connection is encrypted, independently of how it was set up, libp2p also supports a set of modules that encrypt every communication established.
|
||||
|
30
examples/connection-encryption/test.js
Normal file
30
examples/connection-encryption/test.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const messageReceived = pDefer()
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const s = uint8ArrayToString(data)
|
||||
if (s.includes('This information is sent out encrypted to the other peer')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -7,18 +7,7 @@ const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
// 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/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'
|
||||
]
|
||||
const bootstrapers = require('./bootstrapers')
|
||||
|
||||
;(async () => {
|
||||
const node = await Libp2p.create({
|
||||
|
68
examples/discovery-mechanisms/3.js
Normal file
68
examples/discovery-mechanisms/3.js
Normal file
@ -0,0 +1,68 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const PubsubPeerDiscovery = require('libp2p-pubsub-peer-discovery')
|
||||
|
||||
const createRelayServer = require('libp2p-relay-server')
|
||||
|
||||
const createNode = async (bootstrapers) => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub,
|
||||
peerDiscovery: [Bootstrap, PubsubPeerDiscovery]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[PubsubPeerDiscovery.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const relay = await createRelayServer({
|
||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0']
|
||||
})
|
||||
console.log(`libp2p relay starting with id: ${relay.peerId.toB58String()}`)
|
||||
await relay.start()
|
||||
const relayMultiaddrs = relay.multiaddrs.map((m) => `${m.toString()}/p2p/${relay.peerId.toB58String()}`)
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(relayMultiaddrs),
|
||||
createNode(relayMultiaddrs)
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node1.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
node2.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node2.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
;[node1, node2].forEach((node, index) => console.log(`Node ${index} starting with id: ${node.peerId.toB58String()}`))
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
})();
|
@ -2,13 +2,13 @@
|
||||
|
||||
A Peer Discovery module enables libp2p to find peers to connect to. Think of these mechanisms as ways to join the rest of the network, as railing points.
|
||||
|
||||
With these system, a libp2p node can both have a set of nodes to always connect on boot (bootstraper nodes), discover nodes through locality (e.g connected in the same LAN) or through serendipity (random walks on a DHT).
|
||||
With this system, a libp2p node can both have a set of nodes to always connect on boot (bootstraper nodes), discover nodes through locality (e.g connected in the same LAN) or through serendipity (random walks on a DHT).
|
||||
|
||||
These mechanisms save configuration and enable a node to operate without any explicit dials, it will just work. Once new peers are discovered, their known data is stored in the peer's PeerStore.
|
||||
|
||||
## 1. Bootstrap list of Peers when booting a node
|
||||
|
||||
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, mplex and NOISE. You can see the complete example at [1.js](./1.js).
|
||||
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, mplex, and NOISE. You can see the complete example at [1.js](./1.js).
|
||||
|
||||
First, we create our libp2p node.
|
||||
|
||||
@ -16,7 +16,7 @@ First, we create our libp2p node.
|
||||
const Libp2p = require('libp2p')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
const node = Libp2p.create({
|
||||
const node = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
@ -40,14 +40,11 @@ In this configuration, we use a `bootstrappers` array listing peers to connect _
|
||||
```JavaScript
|
||||
const bootstrapers = [
|
||||
'/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'
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
|
||||
]
|
||||
```
|
||||
|
||||
@ -93,23 +90,17 @@ From running [1.js](./1.js), you should see the following:
|
||||
```bash
|
||||
> node 1.js
|
||||
Discovered: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
|
||||
Discovered: QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z
|
||||
Discovered: QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM
|
||||
Discovered: QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm
|
||||
Discovered: QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu
|
||||
Discovered: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
|
||||
Discovered: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
|
||||
Discovered: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
|
||||
Discovered: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
|
||||
Discovered: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
|
||||
Discovered: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
|
||||
Discovered: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
|
||||
Discovered: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
|
||||
Discovered: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
|
||||
Connection established to: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
|
||||
Connection established to: QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z
|
||||
Connection established to: QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM
|
||||
Connection established to: QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm
|
||||
Connection established to: QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu
|
||||
Connection established to: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
|
||||
Connection established to: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
|
||||
Connection established to: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
|
||||
Connection established to: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
|
||||
Connection established to: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
|
||||
Connection established to: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
|
||||
Connection established to: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
|
||||
Connection established to: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
|
||||
Connection established to: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
|
||||
```
|
||||
|
||||
## 2. MulticastDNS to find other peers in the network
|
||||
@ -165,10 +156,103 @@ Discovered: QmSSbQpuKrxkoXHm1v4Pi35hPN5hUHMZoBoawEs2Nhvi8m
|
||||
Discovered: QmRcXXhtG8vTqwVBRonKWtV4ovDoC1Fe56WYtcrw694eiJ
|
||||
```
|
||||
|
||||
## 3. Where to find other Peer Discovery Mechanisms
|
||||
## 3. Pubsub based Peer Discovery
|
||||
|
||||
For this example, we need [`libp2p-pubsub-peer-discovery`](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/), go ahead and `npm install` it. You also need to spin up a set of [`libp2p-relay-servers`](https://github.com/libp2p/js-libp2p-relay-server). These servers act as relay servers and a peer discovery source.
|
||||
|
||||
In the context of this example, we will create and run the `libp2p-relay-server` in the same code snippet. You can find the complete solution at [3.js](./3.js).
|
||||
|
||||
You can create your libp2p nodes as follows:
|
||||
|
||||
```js
|
||||
const Libp2p = require('libp2p')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const PubsubPeerDiscovery = require('libp2p-pubsub-peer-discovery')
|
||||
|
||||
const createNode = async (bootstrapers) => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub,
|
||||
peerDiscovery: [Bootstrap, PubsubPeerDiscovery]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[PubsubPeerDiscovery.tag]: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
},
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: bootstrapers
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
We will use the `libp2p-relay-server` as bootstrap nodes for the libp2p nodes, so that they establish a connection with the relay after starting. As a result, after they establish a connection with the relay, the pubsub discovery will kick in and the relay will advertise them.
|
||||
|
||||
```js
|
||||
const relay = await createRelayServer({
|
||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0']
|
||||
})
|
||||
console.log(`libp2p relay starting with id: ${relay.peerId.toB58String()}`)
|
||||
await relay.start()
|
||||
const relayMultiaddrs = relay.multiaddrs.map((m) => `${m.toString()}/p2p/${relay.peerId.toB58String()}`)
|
||||
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(relayMultiaddrs),
|
||||
createNode(relayMultiaddrs)
|
||||
])
|
||||
|
||||
node1.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node1.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
node2.on('peer:discovery', (peerId) => {
|
||||
console.log(`Peer ${node2.peerId.toB58String()} discovered: ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
;[node1, node2].forEach((node, index) => console.log(`Node ${index} starting with id: ${node.peerId.toB58String()}`))
|
||||
await Promise.all([
|
||||
node1.start(),
|
||||
node2.start()
|
||||
])
|
||||
```
|
||||
|
||||
If you run this example, you will see the other peers being discovered.
|
||||
|
||||
```bash
|
||||
> node 3.js
|
||||
libp2p relay starting with id: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Node 0 starting with id: QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N
|
||||
Node 1 starting with id: QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv
|
||||
Peer QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N discovered: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Peer QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv discovered: QmW6FqVV6RsyoGC5zaeFGW9gSWA3LcBRVZrjkKMruh38Bo
|
||||
Peer QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv discovered: QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N
|
||||
Peer QmezqDTmEjZ5BfMgVqjSpLY19mVVLTQ9bE9mRpZwtGxL8N discovered: QmYWeom2odTkm79DzB68NHULqVHDaNDqHhoyqLdcV1fqdv
|
||||
```
|
||||
|
||||
Taking into account the output, after the relay and both libp2p nodes start, both libp2p nodes will discover the bootstrap node (relay) and connect with it. After establishing a connection with the relay, they will discover each other.
|
||||
|
||||
This is really useful when running libp2p in constrained environments like a browser. You can run a set of `libp2p-relay-server` nodes that will be responsible for both relaying websocket connections between browser nodes and for discovering other browser peers.
|
||||
|
||||
## 4. Where to find other Peer Discovery Mechanisms
|
||||
|
||||
There are plenty more Peer Discovery Mechanisms out there, you can:
|
||||
|
||||
- Find one in [libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star). Yes, a transport with discovery capabilities! This happens because WebRTC requires a rendezvous point for peers to exchange [SDP](https://tools.ietf.org/html/rfc4317) offer, which means we have one or more points that can introduce peers to each other. Think of it as MulticastDNS for the Web, as in MulticastDNS only works in LAN.
|
||||
- Any DHT will offer you a discovery capability. You can simple _random-walk_ the routing tables to find other peers to connect to. For example [libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht) can be used for peer discovery. An example how to configure it to enable random walks can be found [here](https://github.com/libp2p/js-libp2p/blob/v0.28.4/doc/CONFIGURATION.md#customizing-dht).
|
||||
- Any DHT will offer you a discovery capability. You can simple _random-walk_ the routing tables to find other peers to connect to. For example [libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht) can be used for peer discovery. An example of how to configure it to enable random walks can be found [here](https://github.com/libp2p/js-libp2p/blob/v0.28.4/doc/CONFIGURATION.md#customizing-dht).
|
||||
- You can create your own Discovery service, a registry, a list, a radio beacon, you name it!
|
||||
|
13
examples/discovery-mechanisms/bootstrapers.js
vendored
Normal file
13
examples/discovery-mechanisms/bootstrapers.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core/src/runtime/config-nodejs.js
|
||||
const bootstrapers = [
|
||||
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
|
||||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
|
||||
]
|
||||
|
||||
module.exports = bootstrapers
|
42
examples/discovery-mechanisms/test-1.js
Normal file
42
examples/discovery-mechanisms/test-1.js
Normal file
@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const bootstrapers = require('./bootstrapers')
|
||||
|
||||
const discoveredCopy = 'Discovered:'
|
||||
const connectedCopy = 'Connection established to:'
|
||||
|
||||
async function test () {
|
||||
const discoveredNodes = []
|
||||
const connectedNodes = []
|
||||
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered or Connected
|
||||
if (line.includes(discoveredCopy)) {
|
||||
const id = line.trim().split(discoveredCopy)[1]
|
||||
discoveredNodes.push(id)
|
||||
} else if (line.includes(connectedCopy)) {
|
||||
const id = line.trim().split(connectedCopy)[1]
|
||||
connectedNodes.push(id)
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoveredNodes.length === bootstrapers.length && connectedNodes.length === bootstrapers.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
35
examples/discovery-mechanisms/test-2.js
Normal file
35
examples/discovery-mechanisms/test-2.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const discoveredCopy = 'Discovered:'
|
||||
|
||||
async function test() {
|
||||
const discoveredNodes = []
|
||||
|
||||
process.stdout.write('2.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes(discoveredCopy)) {
|
||||
const id = line.trim().split(discoveredCopy)[1]
|
||||
discoveredNodes.push(id)
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoveredNodes.length === 2)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
35
examples/discovery-mechanisms/test-3.js
Normal file
35
examples/discovery-mechanisms/test-3.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const discoveredCopy = 'discovered:'
|
||||
|
||||
async function test() {
|
||||
let discoverCount = 0
|
||||
|
||||
process.stdout.write('3.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '3.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered or Connected
|
||||
if (line.includes(discoveredCopy)) {
|
||||
discoverCount++
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => discoverCount === 4)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/discovery-mechanisms/test.js
Normal file
13
examples/discovery-mechanisms/test.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
const test3 = require('./test-3')
|
||||
|
||||
async function test () {
|
||||
await test1()
|
||||
await test2()
|
||||
await test3()
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -5,9 +5,8 @@
|
||||
* Dialer Node
|
||||
*/
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async function run() {
|
||||
@ -17,7 +16,7 @@ async function run() {
|
||||
])
|
||||
|
||||
// Dialer
|
||||
const dialerNode = new Node({
|
||||
const dialerNode = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
|
@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: [ mplex ],
|
||||
connEncryption: [ NOISE ]
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
23
examples/echo/src/libp2p.js
Normal file
23
examples/echo/src/libp2p.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('libp2p-noise')
|
||||
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
async function createLibp2p(_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [TCP, WS],
|
||||
streamMuxer: [mplex],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
}
|
||||
|
||||
return libp2p.create(defaultsDeep(_options, defaults))
|
||||
}
|
||||
|
||||
module.exports = createLibp2p
|
@ -6,14 +6,14 @@
|
||||
*/
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async function run() {
|
||||
const listenerId = await PeerId.createFromJSON(require('./id-l'))
|
||||
|
||||
// Listener libp2p node
|
||||
const listenerNode = new Node({
|
||||
const listenerNode = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/10333']
|
||||
},
|
||||
|
61
examples/echo/test.js
Normal file
61
examples/echo/test.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
function startProcess(name) {
|
||||
return execa('node', [path.join(__dirname, name)], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
const listenerReady = pDefer()
|
||||
const messageReceived = pDefer()
|
||||
|
||||
// Step 1 process
|
||||
process.stdout.write('node listener.js\n')
|
||||
const listenerProc = startProcess('src/listener.js')
|
||||
listenerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const s = uint8ArrayToString(data)
|
||||
|
||||
if (s.includes('Listener ready, listening on:')) {
|
||||
listenerReady.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await listenerReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2 process
|
||||
process.stdout.write('node dialer.js\n')
|
||||
const dialerProc = startProcess('src/dialer.js')
|
||||
dialerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const s = uint8ArrayToString(data)
|
||||
|
||||
if (s.includes('received echo:')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
process.stdout.write('echo message received\n')
|
||||
|
||||
listenerProc.kill()
|
||||
dialerProc.kill()
|
||||
await Promise.all([
|
||||
listenerProc,
|
||||
dialerProc
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -8,13 +8,14 @@
|
||||
],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "parcel build index.html",
|
||||
"start": "parcel index.html"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@babel/preset-env": "^7.8.3",
|
||||
"@babel/preset-env": "^7.13.0",
|
||||
"libp2p": "../../",
|
||||
"libp2p-bootstrap": "^0.12.1",
|
||||
"libp2p-mplex": "^0.10.0",
|
||||
@ -23,11 +24,11 @@
|
||||
"libp2p-websockets": "^0.14.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.8.3",
|
||||
"@babel/core": "^7.8.3",
|
||||
"@babel/cli": "^7.13.10",
|
||||
"@babel/core": "^7.13.0",
|
||||
"babel-plugin-syntax-async-functions": "^6.13.0",
|
||||
"babel-plugin-transform-regenerator": "^6.26.0",
|
||||
"babel-polyfill": "^6.26.0",
|
||||
"parcel-bundler": "^1.12.4"
|
||||
"parcel-bundler": "1.12.3"
|
||||
}
|
||||
}
|
||||
|
52
examples/libp2p-in-the-browser/test.js
Normal file
52
examples/libp2p-in-the-browser/test.js
Normal file
@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const execa = require('execa')
|
||||
const { chromium } = require('playwright');
|
||||
|
||||
async function run() {
|
||||
let url = ''
|
||||
const proc = execa('parcel', ['./index.html'], {
|
||||
preferLocal: true,
|
||||
localDir: __dirname,
|
||||
cwd: __dirname,
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (chunk) => {
|
||||
/**@type {string} */
|
||||
const out = chunk.toString()
|
||||
|
||||
if (out.includes('Server running at')) {
|
||||
url = out.replace('Server running at ', '')
|
||||
}
|
||||
|
||||
if (out.includes('✨ Built in ')) {
|
||||
try {
|
||||
const browser = await chromium.launch();
|
||||
const page = await browser.newPage();
|
||||
await page.goto(url);
|
||||
await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
|
||||
await page.waitForFunction(
|
||||
selector => {
|
||||
const text = document.querySelector(selector).innerText
|
||||
return text.includes('libp2p id is') &&
|
||||
text.includes('Found peer') &&
|
||||
text.includes('Connected to')
|
||||
},
|
||||
'#output',
|
||||
{ timeout: 5000 }
|
||||
)
|
||||
await browser.close();
|
||||
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
} finally {
|
||||
proc.cancel()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
module.exports = run
|
21
examples/package.json
Normal file
21
examples/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "libp2p-examples",
|
||||
"version": "1.0.0",
|
||||
"description": "Examples of how to use libp2p",
|
||||
"scripts": {
|
||||
"test": "node ./test.js",
|
||||
"test:all": "node ./test-all.js"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"execa": "^2.1.0",
|
||||
"fs-extra": "^8.1.0",
|
||||
"libp2p-pubsub-peer-discovery": "^3.0.0",
|
||||
"libp2p-relay-server": "^0.1.2",
|
||||
"p-defer": "^3.0.0",
|
||||
"which": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"playwright": "^1.7.1"
|
||||
}
|
||||
}
|
36
examples/peer-and-content-routing/test-1.js
Normal file
36
examples/peer-and-content-routing/test-1.js
Normal file
@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const addrs = []
|
||||
let foundIt = false
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
// Discovered peer
|
||||
if (!foundIt && line.includes('Found it, multiaddrs are:')) {
|
||||
foundIt = true
|
||||
}
|
||||
|
||||
addrs.push(line)
|
||||
})
|
||||
|
||||
await pWaitFor(() => addrs.length === 2)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
40
examples/peer-and-content-routing/test-2.js
Normal file
40
examples/peer-and-content-routing/test-2.js
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const providedCopy = 'is providing'
|
||||
const foundCopy = 'Found provider:'
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('2.js\n')
|
||||
const providedDefer = pDefer()
|
||||
const foundDefer = pDefer()
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes(providedCopy)) {
|
||||
providedDefer.resolve()
|
||||
} else if (line.includes(foundCopy)) {
|
||||
foundDefer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
providedDefer.promise,
|
||||
foundDefer.promise
|
||||
])
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
11
examples/peer-and-content-routing/test.js
Normal file
11
examples/peer-and-content-routing/test.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await test2()
|
||||
}
|
||||
|
||||
module.exports = test
|
30
examples/pnet/test.js
Normal file
30
examples/pnet/test.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const messageReceived = pDefer()
|
||||
process.stdout.write('index.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, 'index.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const s = uint8ArrayToString(data)
|
||||
if (s.includes('This message is sent on a private network')) {
|
||||
messageReceived.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageReceived.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
31
examples/protocol-and-stream-muxing/test-1.js
Normal file
31
examples/protocol-and-stream-muxing/test-1.js
Normal file
@ -0,0 +1,31 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test() {
|
||||
const messageDefer = pDefer()
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes('my own protocol, wow!')) {
|
||||
messageDefer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await messageDefer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
38
examples/protocol-and-stream-muxing/test-2.js
Normal file
38
examples/protocol-and-stream-muxing/test-2.js
Normal file
@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const messages = [
|
||||
'protocol (a)',
|
||||
'protocol (b)',
|
||||
'another stream on protocol (b)'
|
||||
]
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('2.js\n')
|
||||
|
||||
let count = 0
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (messages.find((m) => line.includes(m))) {
|
||||
count += 1
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => count === messages.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
37
examples/protocol-and-stream-muxing/test-3.js
Normal file
37
examples/protocol-and-stream-muxing/test-3.js
Normal file
@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const messages = [
|
||||
'from 1 to 2',
|
||||
'from 2 to 1'
|
||||
]
|
||||
|
||||
async function test() {
|
||||
process.stdout.write('3.js\n')
|
||||
|
||||
let count = 0
|
||||
const proc = execa('node', [path.join(__dirname, '3.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (messages.find((m) => line.includes(m))) {
|
||||
count += 1
|
||||
}
|
||||
})
|
||||
|
||||
await pWaitFor(() => count === messages.length)
|
||||
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/protocol-and-stream-muxing/test.js
Normal file
13
examples/protocol-and-stream-muxing/test.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
const test3 = require('./test-3')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await test2()
|
||||
await test3()
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -43,6 +43,7 @@ const createNode = async () => {
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
|
@ -44,7 +44,6 @@ const node2 = nodes[1]
|
||||
|
||||
// Add node's 2 data to the PeerStore
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => {
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
// Will not receive own published messages by default
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
@ -68,25 +68,34 @@ The output of the program should look like:
|
||||
```
|
||||
> node 1.js
|
||||
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.
|
||||
You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages.
|
||||
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: false
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The output of the program should look like:
|
||||
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
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!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
## 2. Future work
|
||||
|
||||
libp2p/IPFS PubSub is enabling a whole set of Distributed Real Time applications using CRDT (Conflict-Free Replicated Data Types). It is still going through heavy research (and hacking) and we invite you to join the conversation at [research-CRDT](https://github.com/ipfs/research-CRDT). Here is a list of some of the exciting examples:
|
||||
|
@ -44,6 +44,7 @@ const createNode = async () => {
|
||||
|
||||
//subscribe
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
// Will not receive own published messages by default
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
@ -97,15 +97,12 @@ Result
|
||||
```
|
||||
> node 1.js
|
||||
############## fruit banana ##############
|
||||
node1 received: banana
|
||||
node2 received: banana
|
||||
node3 received: banana
|
||||
############## fruit apple ##############
|
||||
node1 received: apple
|
||||
node2 received: apple
|
||||
node3 received: apple
|
||||
############## fruit car ##############
|
||||
node1 received: car
|
||||
############## fruit orange ##############
|
||||
node1 received: orange
|
||||
node2 received: orange
|
||||
|
67
examples/pubsub/message-filtering/test.js
Normal file
67
examples/pubsub/message-filtering/test.js
Normal file
@ -0,0 +1,67 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const stdout = [
|
||||
{
|
||||
topic: 'banana',
|
||||
messageCount: 2
|
||||
},
|
||||
{
|
||||
topic: 'apple',
|
||||
messageCount: 2
|
||||
},
|
||||
{
|
||||
topic: 'car',
|
||||
messageCount: 0
|
||||
},
|
||||
{
|
||||
topic: 'orange',
|
||||
messageCount: 2
|
||||
},
|
||||
]
|
||||
|
||||
async function test () {
|
||||
const defer = pDefer()
|
||||
let topicCount = 0
|
||||
let topicMessageCount = 0
|
||||
|
||||
process.stdout.write('message-filtering/1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
// End
|
||||
if (topicCount === stdout.length) {
|
||||
defer.resolve()
|
||||
proc.all.removeAllListeners('data')
|
||||
}
|
||||
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (stdout[topicCount] && line.includes(stdout[topicCount].topic)) {
|
||||
// Validate previous number of messages
|
||||
if (topicCount > 0 && topicMessageCount > stdout[topicCount - 1].messageCount) {
|
||||
defer.reject()
|
||||
throw new Error(`topic ${stdout[topicCount - 1].topic} had ${topicMessageCount} messages instead of ${stdout[topicCount - 1].messageCount}`)
|
||||
}
|
||||
|
||||
topicCount++
|
||||
topicMessageCount = 0
|
||||
} else {
|
||||
topicMessageCount++
|
||||
}
|
||||
})
|
||||
|
||||
await defer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
30
examples/pubsub/test-1.js
Normal file
30
examples/pubsub/test-1.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const defer = pDefer()
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes('node1 received: Bird bird bird, bird is the word!')) {
|
||||
defer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await defer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
11
examples/pubsub/test.js
Normal file
11
examples/pubsub/test.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const testMessageFiltering = require('./message-filtering/test')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await testMessageFiltering()
|
||||
}
|
||||
|
||||
module.exports = test
|
33
examples/test-all.js
Normal file
33
examples/test-all.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
process.on('unhandedRejection', (err) => {
|
||||
console.error(err)
|
||||
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const {
|
||||
waitForOutput
|
||||
} = require('./utils')
|
||||
|
||||
async function testAll () {
|
||||
for (const dir of fs.readdirSync(__dirname)) {
|
||||
if (dir === 'node_modules' || dir === 'tests_output') {
|
||||
continue
|
||||
}
|
||||
|
||||
const stats = fs.statSync(path.join(__dirname, dir))
|
||||
|
||||
if (!stats.isDirectory()) {
|
||||
continue
|
||||
}
|
||||
|
||||
await waitForOutput('npm info ok', 'npm', ['test', '--', dir], {
|
||||
cwd: __dirname
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
testAll()
|
94
examples/test.js
Normal file
94
examples/test.js
Normal file
@ -0,0 +1,94 @@
|
||||
'use strict'
|
||||
|
||||
process.env.NODE_ENV = 'test'
|
||||
process.env.CI = true // needed for some "clever" build tools
|
||||
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const dir = path.join(__dirname, process.argv[2])
|
||||
|
||||
testExample(dir)
|
||||
.then(() => {}, (err) => {
|
||||
if (err.exitCode) {
|
||||
process.exit(err.exitCode)
|
||||
}
|
||||
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
async function testExample (dir) {
|
||||
await installDeps(dir)
|
||||
await build(dir)
|
||||
await runTest(dir)
|
||||
}
|
||||
|
||||
async function installDeps (dir) {
|
||||
if (!fs.existsSync(path.join(dir, 'package.json'))) {
|
||||
console.info('Nothing to install in', dir)
|
||||
return
|
||||
}
|
||||
|
||||
if (fs.existsSync(path.join(dir, 'node_modules'))) {
|
||||
console.info('Dependencies already installed in', dir)
|
||||
return
|
||||
}
|
||||
|
||||
const proc = execa.command('npm install', {
|
||||
cwd: dir
|
||||
})
|
||||
proc.all.on('data', (data) => {
|
||||
process.stdout.write(data)
|
||||
})
|
||||
|
||||
await proc
|
||||
}
|
||||
|
||||
async function build (dir) {
|
||||
const pkgJson = path.join(dir, 'package.json')
|
||||
|
||||
if (!fs.existsSync(pkgJson)) {
|
||||
console.info('Nothing to build in', dir)
|
||||
return
|
||||
}
|
||||
|
||||
const pkg = require(pkgJson)
|
||||
let build
|
||||
|
||||
if (pkg.scripts.bundle) {
|
||||
build = 'bundle'
|
||||
}
|
||||
|
||||
if (pkg.scripts.build) {
|
||||
build = 'build'
|
||||
}
|
||||
|
||||
if (!build) {
|
||||
console.info('No "build" or "bundle" script in', pkgJson)
|
||||
return
|
||||
}
|
||||
|
||||
const proc = execa('npm', ['run', build], {
|
||||
cwd: dir
|
||||
})
|
||||
proc.all.on('data', (data) => {
|
||||
process.stdout.write(data)
|
||||
})
|
||||
|
||||
await proc
|
||||
}
|
||||
|
||||
async function runTest (dir) {
|
||||
console.info('Running node tests in', dir)
|
||||
const testFile = path.join(dir, 'test.js')
|
||||
|
||||
if (!fs.existsSync(testFile)) {
|
||||
console.info('Nothing to test in', dir)
|
||||
return
|
||||
}
|
||||
|
||||
const test = require(testFile)
|
||||
|
||||
await test()
|
||||
}
|
38
examples/transports/test-1.js
Normal file
38
examples/transports/test-1.js
Normal file
@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const deferStarted = pDefer()
|
||||
const deferListen = pDefer()
|
||||
|
||||
process.stdout.write('1.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '1.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
|
||||
if (line.includes('node has started (true/false): true')) {
|
||||
deferStarted.resolve()
|
||||
} else if (line.includes('p2p')) {
|
||||
deferListen.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
deferStarted.promise,
|
||||
deferListen.promise
|
||||
])
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
30
examples/transports/test-2.js
Normal file
30
examples/transports/test-2.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const defer = pDefer()
|
||||
process.stdout.write('2.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '2.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes('Hello p2p world!')) {
|
||||
defer.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await defer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
41
examples/transports/test-3.js
Normal file
41
examples/transports/test-3.js
Normal file
@ -0,0 +1,41 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
async function test () {
|
||||
const deferNode1 = pDefer()
|
||||
const deferNode2 = pDefer()
|
||||
const deferNode3 = pDefer()
|
||||
|
||||
process.stdout.write('3.js\n')
|
||||
|
||||
const proc = execa('node', [path.join(__dirname, '3.js')], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
|
||||
proc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
const line = uint8ArrayToString(data)
|
||||
|
||||
if (line.includes('node 1 dialed to node 2 successfully')) {
|
||||
deferNode1.resolve()
|
||||
} else if (line.includes('node 2 dialed to node 3 successfully')) {
|
||||
deferNode2.resolve()
|
||||
} else if (line.includes('node 3 failed to dial to node 1 with:')) {
|
||||
deferNode3.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
deferNode1.promise,
|
||||
deferNode2.promise,
|
||||
deferNode3.promise
|
||||
])
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
13
examples/transports/test.js
Normal file
13
examples/transports/test.js
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const test1 = require('./test-1')
|
||||
const test2 = require('./test-2')
|
||||
const test3 = require('./test-3')
|
||||
|
||||
async function test() {
|
||||
await test1()
|
||||
await test2()
|
||||
await test3()
|
||||
}
|
||||
|
||||
module.exports = test
|
61
examples/utils.js
Normal file
61
examples/utils.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict'
|
||||
|
||||
const execa = require('execa')
|
||||
const fs = require('fs-extra')
|
||||
const which = require('which')
|
||||
|
||||
async function isExecutable (command) {
|
||||
try {
|
||||
await fs.access(command, fs.constants.X_OK)
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
return isExecutable(await which(command))
|
||||
}
|
||||
|
||||
if (err.code === 'EACCES') {
|
||||
return false
|
||||
}
|
||||
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
|
||||
if (!await isExecutable(command)) {
|
||||
args.unshift(command)
|
||||
command = 'node'
|
||||
}
|
||||
|
||||
const proc = execa(command, args, opts)
|
||||
let output = ''
|
||||
let time = 120000
|
||||
|
||||
let timeout = setTimeout(() => {
|
||||
throw new Error(`Did not see "${expectedOutput}" in output from "${[command].concat(args).join(' ')}" after ${time/1000}s`)
|
||||
}, time)
|
||||
|
||||
proc.all.on('data', (data) => {
|
||||
process.stdout.write(data)
|
||||
|
||||
output += data.toString('utf8')
|
||||
|
||||
if (output.includes(expectedOutput)) {
|
||||
clearTimeout(timeout)
|
||||
proc.kill()
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
await proc
|
||||
} catch (err) {
|
||||
if (!err.killed) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
waitForOutput
|
||||
}
|
33
examples/webrtc-direct/README.md
Normal file
33
examples/webrtc-direct/README.md
Normal file
@ -0,0 +1,33 @@
|
||||
### Webrtc-direct example
|
||||
|
||||
An example that uses [js-libp2p-webrtc-direct](https://github.com/libp2p/js-libp2p-webrtc-direct) for connecting
|
||||
nodejs libp2p and browser libp2p clients. To run the example:
|
||||
|
||||
## 0. Run a nodejs libp2p listener
|
||||
|
||||
When in the root folder of this example, type `node listener.js` in terminal. You should see an address that listens for
|
||||
incoming connections. Below is just an example of such address. In your case the suffix hash (`peerId`) will be different.
|
||||
|
||||
```bash
|
||||
$ node listener.js
|
||||
Listening on:
|
||||
/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct/p2p/QmUKQCzEUhhhobcNSrXU5uzxTqbvF1BjMCGNGZzZU14Kgd
|
||||
```
|
||||
|
||||
## 1. Prepare a browser libp2p dialer
|
||||
Confirm that the above address is the same as the field `list` in `public/dialer.js`:
|
||||
```js
|
||||
peerDiscovery: {
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
// paste the address into `list`
|
||||
list: ['/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct/p2p/QmUKQCzEUhhhobcNSrXU5uzxTqbvF1BjMCGNGZzZU14Kgd']
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Run a browser libp2p dialer
|
||||
When in the root folder of this example, type `npm run dev` in terminal. You should see an address where you can browse
|
||||
the running client. Open this address in your browser. In console
|
||||
logs you should see logs about successful connection with the node client. In the output of node client you should see
|
||||
a log message about successful connection as well.
|
57
examples/webrtc-direct/dialer.js
Normal file
57
examples/webrtc-direct/dialer.js
Normal file
@ -0,0 +1,57 @@
|
||||
import 'babel-polyfill'
|
||||
const Libp2p = require('libp2p')
|
||||
const WebRTCDirect = require('libp2p-webrtc-direct')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const {NOISE} = require('libp2p-noise')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
// use the same peer id as in `listener.js` to avoid copy-pasting of listener's peer id into `peerDiscovery`
|
||||
const hardcodedPeerId = '12D3KooWCuo3MdXfMgaqpLC5Houi1TRoFqgK9aoxok4NK5udMu8m'
|
||||
const libp2p = await Libp2p.create({
|
||||
modules: {
|
||||
transport: [WebRTCDirect],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
peerDiscovery: [Bootstrap]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[Bootstrap.tag]: {
|
||||
enabled: true,
|
||||
list: [`/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct/p2p/${hardcodedPeerId}`]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const status = document.getElementById('status')
|
||||
const output = document.getElementById('output')
|
||||
|
||||
output.textContent = ''
|
||||
|
||||
function log (txt) {
|
||||
console.info(txt)
|
||||
output.textContent += `${txt.trim()}\n`
|
||||
}
|
||||
|
||||
// Listen for new peers
|
||||
libp2p.on('peer:discovery', (peerId) => {
|
||||
log(`Found peer ${peerId.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for new connections to peers
|
||||
libp2p.connectionManager.on('peer:connect', (connection) => {
|
||||
log(`Connected to ${connection.remotePeer.toB58String()}`)
|
||||
})
|
||||
|
||||
// Listen for peers disconnecting
|
||||
libp2p.connectionManager.on('peer:disconnect', (connection) => {
|
||||
log(`Disconnected from ${connection.remotePeer.toB58String()}`)
|
||||
})
|
||||
|
||||
await libp2p.start()
|
||||
status.innerText = 'libp2p started!'
|
||||
log(`libp2p id is ${libp2p.peerId.toB58String()}`)
|
||||
|
||||
})
|
17
examples/webrtc-direct/index.html
Normal file
17
examples/webrtc-direct/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>js-libp2p parcel.js browser example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1 id="status">Starting libp2p...</h1>
|
||||
</header>
|
||||
<main>
|
||||
<pre id="output"></pre>
|
||||
</main>
|
||||
<script src="./dialer.js"></script>
|
||||
</body>
|
||||
</html>
|
44
examples/webrtc-direct/listener.js
Normal file
44
examples/webrtc-direct/listener.js
Normal file
@ -0,0 +1,44 @@
|
||||
const Libp2p = require('libp2p')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const WebRTCDirect = require('libp2p-webrtc-direct')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const {NOISE} = require('libp2p-noise')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
;(async () => {
|
||||
// hardcoded peer id to avoid copy-pasting of listener's peer id into the dialer's bootstrap list
|
||||
// generated with cmd `peer-id --type=ed25519`
|
||||
const hardcodedPeerId = await PeerId.createFromJSON({
|
||||
"id": "12D3KooWCuo3MdXfMgaqpLC5Houi1TRoFqgK9aoxok4NK5udMu8m",
|
||||
"privKey": "CAESQAG6Ld7ev6nnD0FKPs033/j0eQpjWilhxnzJ2CCTqT0+LfcWoI2Vr+zdc1vwk7XAVdyoCa2nwUR3RJebPWsF1/I=",
|
||||
"pubKey": "CAESIC33FqCNla/s3XNb8JO1wFXcqAmtp8FEd0SXmz1rBdfy"
|
||||
})
|
||||
const node = await Libp2p.create({
|
||||
peerId: hardcodedPeerId,
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct']
|
||||
},
|
||||
modules: {
|
||||
transport: [WebRTCDirect],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
[Bootstrap.tag]: {
|
||||
enabled: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
node.connectionManager.on('peer:connect', (connection) => {
|
||||
console.info(`Connected to ${connection.remotePeer.toB58String()}!`)
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log('Listening on:')
|
||||
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
|
||||
|
||||
})()
|
31
examples/webrtc-direct/package.json
Normal file
31
examples/webrtc-direct/package.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "webrtc-direct",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "parcel build index.html",
|
||||
"start": "parcel index.html"
|
||||
},
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.13.10",
|
||||
"@babel/core": "^7.13.10",
|
||||
"babel-plugin-syntax-async-functions": "^6.13.0",
|
||||
"babel-plugin-transform-regenerator": "^6.26.0",
|
||||
"babel-polyfill": "^6.26.0",
|
||||
"parcel-bundler": "1.12.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"libp2p": "../../",
|
||||
"libp2p-bootstrap": "^0.12.1",
|
||||
"libp2p-mplex": "^0.10.1",
|
||||
"libp2p-noise": "^2.0.1",
|
||||
"libp2p-webrtc-direct": "^0.5.0",
|
||||
"peer-id": "^0.14.3"
|
||||
},
|
||||
"browser": {
|
||||
"ipfs": "ipfs/dist/index.min.js"
|
||||
}
|
||||
}
|
93
examples/webrtc-direct/test.js
Normal file
93
examples/webrtc-direct/test.js
Normal file
@ -0,0 +1,93 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const { chromium } = require('playwright');
|
||||
|
||||
function startNode (name, args = []) {
|
||||
return execa('node', [path.join(__dirname, name), ...args], {
|
||||
cwd: path.resolve(__dirname),
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
function startBrowser (name, args = []) {
|
||||
return execa('parcel', [path.join(__dirname, name), ...args], {
|
||||
preferLocal: true,
|
||||
localDir: __dirname,
|
||||
cwd: __dirname,
|
||||
all: true
|
||||
})
|
||||
}
|
||||
|
||||
async function test () {
|
||||
// Step 1, listener process
|
||||
const listenerProcReady = pDefer()
|
||||
let listenerOutput = ''
|
||||
process.stdout.write('listener.js\n')
|
||||
const listenerProc = startNode('listener.js')
|
||||
|
||||
listenerProc.all.on('data', async (data) => {
|
||||
process.stdout.write(data)
|
||||
listenerOutput += uint8ArrayToString(data)
|
||||
if (listenerOutput.includes('Listening on:') && listenerOutput.includes('12D3KooWCuo3MdXfMgaqpLC5Houi1TRoFqgK9aoxok4NK5udMu8m')) {
|
||||
listenerProcReady.resolve()
|
||||
}
|
||||
})
|
||||
|
||||
await listenerProcReady.promise
|
||||
process.stdout.write('==================================================================\n')
|
||||
|
||||
// Step 2, dialer process
|
||||
process.stdout.write('dialer.js\n')
|
||||
let dialerUrl = ''
|
||||
const dialerProc = startBrowser('index.html')
|
||||
|
||||
dialerProc.all.on('data', async (chunk) => {
|
||||
/**@type {string} */
|
||||
const out = chunk.toString()
|
||||
|
||||
if (out.includes('Server running at')) {
|
||||
dialerUrl = out.replace('Server running at ', '')
|
||||
}
|
||||
|
||||
if (out.includes('✨ Built in ')) {
|
||||
try {
|
||||
const browser = await chromium.launch();
|
||||
const page = await browser.newPage();
|
||||
await page.goto(dialerUrl);
|
||||
await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
|
||||
await page.waitForFunction(
|
||||
selector => {
|
||||
const text = document.querySelector(selector).innerText
|
||||
return text.includes('libp2p id is') &&
|
||||
text.includes('Found peer') &&
|
||||
text.includes('Connected to')
|
||||
},
|
||||
'#output',
|
||||
{ timeout: 10000 }
|
||||
)
|
||||
await browser.close();
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
} finally {
|
||||
dialerProc.cancel()
|
||||
listenerProc.kill()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all([
|
||||
listenerProc,
|
||||
dialerProc,
|
||||
]).catch((err) => {
|
||||
if (err.signal !== 'SIGTERM') {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = test
|
@ -23,7 +23,6 @@
|
||||
|
||||
"secure channels",
|
||||
["NodeFactoryIo/js-libp2p-noise", "libp2p-noise"],
|
||||
["libp2p/js-libp2p-secio", "libp2p-secio"],
|
||||
|
||||
"stream multiplexers",
|
||||
["libp2p/js-libp2p-mplex", "libp2p-mplex"],
|
||||
|
129
package.json
129
package.json
@ -1,9 +1,18 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.29.3",
|
||||
"version": "0.30.12",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"src/*": [
|
||||
"dist/src/*",
|
||||
"dist/src/*/index"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
@ -14,6 +23,7 @@
|
||||
"test": "npm run test:node && npm run test:browser",
|
||||
"test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
|
||||
"test:browser": "aegir test -t browser",
|
||||
"test:examples": "cd examples && npm run test:all",
|
||||
"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",
|
||||
@ -40,86 +50,99 @@
|
||||
"node": ">=12.0.0",
|
||||
"npm": ">=6.0.0"
|
||||
},
|
||||
"browser": {
|
||||
"@motrix/nat-api": false
|
||||
},
|
||||
"dependencies": {
|
||||
"@motrix/nat-api": "^0.3.1",
|
||||
"abort-controller": "^3.0.0",
|
||||
"aggregate-error": "^3.0.1",
|
||||
"any-signal": "^1.1.0",
|
||||
"bignumber.js": "^9.0.0",
|
||||
"aggregate-error": "^3.1.0",
|
||||
"any-signal": "^2.1.1",
|
||||
"bignumber.js": "^9.0.1",
|
||||
"cids": "^1.1.5",
|
||||
"class-is": "^1.1.0",
|
||||
"debug": "^4.1.1",
|
||||
"debug": "^4.3.1",
|
||||
"err-code": "^2.0.0",
|
||||
"events": "^3.1.0",
|
||||
"es6-promisify": "^6.1.1",
|
||||
"events": "^3.2.0",
|
||||
"hashlru": "^2.3.0",
|
||||
"interface-datastore": "^2.0.0",
|
||||
"ipfs-utils": "^2.2.0",
|
||||
"it-all": "^1.0.1",
|
||||
"interface-datastore": "^3.0.3",
|
||||
"ipfs-utils": "^6.0.0",
|
||||
"it-all": "^1.0.4",
|
||||
"it-buffer": "^0.1.2",
|
||||
"it-handshake": "^1.0.1",
|
||||
"it-length-prefixed": "^3.0.1",
|
||||
"it-drain": "^1.0.3",
|
||||
"it-filter": "^1.0.1",
|
||||
"it-first": "^1.0.4",
|
||||
"it-handshake": "^1.0.2",
|
||||
"it-length-prefixed": "^3.1.0",
|
||||
"it-map": "^1.0.4",
|
||||
"it-merge": "1.0.0",
|
||||
"it-pipe": "^1.1.0",
|
||||
"it-protocol-buffers": "^0.2.0",
|
||||
"libp2p-crypto": "^0.18.0",
|
||||
"libp2p-interfaces": "^0.5.1",
|
||||
"libp2p-utils": "^0.2.0",
|
||||
"it-take": "1.0.0",
|
||||
"libp2p-crypto": "^0.19.0",
|
||||
"libp2p-interfaces": "^0.8.1",
|
||||
"libp2p-utils": "^0.2.2",
|
||||
"mafmt": "^8.0.0",
|
||||
"merge-options": "^2.0.0",
|
||||
"merge-options": "^3.0.4",
|
||||
"moving-average": "^1.0.0",
|
||||
"multiaddr": "^8.1.0",
|
||||
"multicodec": "^2.0.0",
|
||||
"multicodec": "^2.1.0",
|
||||
"multihashing-async": "^2.0.1",
|
||||
"multistream-select": "^1.0.0",
|
||||
"mutable-proxy": "^1.0.0",
|
||||
"node-forge": "^0.9.1",
|
||||
"node-forge": "^0.10.0",
|
||||
"p-any": "^3.0.0",
|
||||
"p-fifo": "^1.0.0",
|
||||
"p-retry": "^4.2.0",
|
||||
"p-settle": "^4.0.1",
|
||||
"peer-id": "^0.14.2",
|
||||
"private-ip": "^2.0.0",
|
||||
"protons": "^2.0.0",
|
||||
"retimer": "^2.0.0",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
"set-delayed-interval": "^1.0.0",
|
||||
"streaming-iterables": "^5.0.2",
|
||||
"timeout-abort-controller": "^1.1.1",
|
||||
"varint": "^5.0.0",
|
||||
"varint": "^6.0.0",
|
||||
"xsalsa20": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"@types/es6-promisify": "^6.0.0",
|
||||
"abortable-iterator": "^3.0.0",
|
||||
"aegir": "^27.0.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"aegir": "^29.2.0",
|
||||
"chai-bytes": "^0.1.2",
|
||||
"chai-string": "^1.5.0",
|
||||
"cids": "^1.0.0",
|
||||
"delay": "^4.3.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"delay": "^4.4.0",
|
||||
"interop-libp2p": "^0.3.0",
|
||||
"ipfs-http-client": "^47.0.1",
|
||||
"into-stream": "^6.0.0",
|
||||
"ipfs-http-client": "^48.2.2",
|
||||
"it-concat": "^1.0.0",
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pushable": "^1.4.0",
|
||||
"libp2p": ".",
|
||||
"libp2p-bootstrap": "^0.12.0",
|
||||
"libp2p-delegated-content-routing": "^0.7.0",
|
||||
"libp2p-delegated-peer-routing": "^0.7.0",
|
||||
"libp2p-floodsub": "^0.23.0",
|
||||
"libp2p-gossipsub": "^0.6.0",
|
||||
"libp2p-kad-dht": "^0.20.0",
|
||||
"libp2p-delegated-content-routing": "^0.9.0",
|
||||
"libp2p-delegated-peer-routing": "^0.8.0",
|
||||
"libp2p-floodsub": "^0.24.0",
|
||||
"libp2p-gossipsub": "^0.8.0",
|
||||
"libp2p-kad-dht": "^0.20.5",
|
||||
"libp2p-mdns": "^0.15.0",
|
||||
"libp2p-mplex": "^0.10.1",
|
||||
"libp2p-noise": "^2.0.0",
|
||||
"libp2p-secio": "^0.13.1",
|
||||
"libp2p-tcp": "^0.15.1",
|
||||
"libp2p-webrtc-star": "^0.20.0",
|
||||
"libp2p-websockets": "^0.14.0",
|
||||
"libp2p-websockets": "^0.15.0",
|
||||
"multihashes": "^3.0.1",
|
||||
"nock": "^13.0.3",
|
||||
"p-defer": "^3.0.0",
|
||||
"p-times": "^3.0.0",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"promisify-es6": "^1.0.3",
|
||||
"p-wait-for": "^3.2.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"sinon": "^9.0.2",
|
||||
"uint8arrays": "^1.1.0"
|
||||
"sinon": "^9.2.4",
|
||||
"uint8arrays": "^2.0.5"
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
@ -132,41 +155,49 @@
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Hugo Dias <mail@hugodias.me>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"Volker Mische <volker.mische@gmail.com>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"a1300 <matthias-knopp@gmx.net>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
"Andrew Nesbitt <andrewnez@gmail.com>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
"Ryan Bell <ryan@piing.net>",
|
||||
"Thomas Eizinger <thomas@eizinger.io>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
|
||||
"Samlior <samlior@foxmail.com>",
|
||||
"Didrik Nordström <didrik@betamos.se>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Fei Liu <liu.feiwood@gmail.com>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>",
|
||||
"Ethan Lam <elmemphis2000@gmail.com>",
|
||||
"Joel Gustafson <joelg@mit.edu>",
|
||||
"Julien Bouquillon <contact@revolunet.com>",
|
||||
"Kevin Kwok <antimatter15@gmail.com>",
|
||||
"Kevin Lacker <lacker@gmail.com>",
|
||||
"Miguel Mota <miguelmota2@gmail.com>",
|
||||
"Nuno Nogueira <nunofmn@gmail.com>",
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Philipp Muens <raute1337@gmx.de>",
|
||||
"RasmusErik Voel Jensen <github@solsort.com>",
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"robertkiel <robert.kiel@validitylabs.org>",
|
||||
"Smite Chow <xiaopengyou@live.com>",
|
||||
"Soeren <nikorpoulsen@gmail.com>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"TJKoury <TJKoury@gmail.com>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"Cindy Wu <ciindy.wu@gmail.com>",
|
||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"Bernd Strehl <bernd.strehl@gmail.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"Ethan Lam <elmemphis2000@gmail.com>",
|
||||
"isan_rivkin <isanrivkin@gmail.com>",
|
||||
"robertkiel <robert.kiel@validitylabs.org>",
|
||||
"Aleksei <vozhdb@gmail.com>",
|
||||
"Fei Liu <liu.feiwood@gmail.com>",
|
||||
"Felipe Martins <felipebrasil93@gmail.com>",
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Francis Gulotta <wizard@roborooter.com>",
|
||||
"Felipe Martins <felipebrasil93@gmail.com>"
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>",
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"Joel Gustafson <joelg@mit.edu>"
|
||||
]
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Address Manager
|
||||
|
||||
The Address manager is responsible for keeping an updated register of the peer's addresses. It includes 3 different types of Addresses: `Listen Addresses`, `Announce Addresses` and `No Announce Addresses`.
|
||||
The Address manager is responsible for keeping an updated register of the peer's addresses. It includes 2 different types of Addresses: `Listen Addresses` and `Announce Addresses`.
|
||||
|
||||
These Addresses should be specified in your libp2p [configuration](../../doc/CONFIGURATION.md) when you create your node.
|
||||
|
||||
@ -20,17 +20,11 @@ Scenarios for Announce Addresses include:
|
||||
- when you setup a libp2p node in your private network at home, but you need to announce your public IP Address to the outside world;
|
||||
- when you want to announce a DNS address, which maps to your public IP Address.
|
||||
|
||||
## No Announce Addresses
|
||||
|
||||
While we need to add Announce Addresses to enable peers' connectivity, we should also avoid announcing addresses that will not be reachable. No Announce Addresses should be specified so that they are filtered from the advertised multiaddrs.
|
||||
|
||||
As stated in the Listen Addresses section, Listen Addresses might be modified by libp2p transports after the successfully bind to those addresses. Libp2p should also take these changes into account so that they can be matched when No Announce Addresses are being filtered out of the advertised multiaddrs.
|
||||
|
||||
## Implementation
|
||||
|
||||
When a libp2p node is created, the Address Manager will be populated from the provided addresses through the libp2p configuration. Once the node is started, the Transport Manager component will gather the listen addresses from the Address Manager, so that the libp2p transports can attempt to bind to them.
|
||||
|
||||
Libp2p will use the the Address Manager as the source of truth when advertising the peers addresses. After all transports are ready, other libp2p components/subsystems will kickoff, namely the Identify Service and the DHT. Both of them will announce the node addresses to the other peers in the network. The announce and noAnnounce addresses will have an important role here and will be gathered by libp2p to compute its current addresses to advertise everytime it is needed.
|
||||
Libp2p will use the the Address Manager as the source of truth when advertising the peers addresses. After all transports are ready, other libp2p components/subsystems will kickoff, namely the Identify Service and the DHT. Both of them will announce the node addresses to the other peers in the network. The announce addresses will have an important role here and will be gathered by libp2p to compute its current addresses to advertise everytime it is needed.
|
||||
|
||||
## Future Considerations
|
||||
|
||||
|
@ -1,36 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:addresses')
|
||||
log.error = debug('libp2p:addresses:error')
|
||||
|
||||
/** @typedef {import('../types').EventEmitterFactory} Events */
|
||||
/** @type Events */
|
||||
const EventEmitter = require('events')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
/**
|
||||
* Responsible for managing this peers addresses.
|
||||
* Peers can specify their listen, announce and noAnnounce addresses.
|
||||
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
||||
* while the announce an noAnnounce addresses will be combined with the listen addresses for
|
||||
* address adverstising to other peers in the network.
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
*/
|
||||
class AddressManager {
|
||||
|
||||
/**
|
||||
* @typedef {Object} AddressManagerOptions
|
||||
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
|
||||
* @property {string[]} [announce = []] - list of multiaddrs string representation to announce.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fires AddressManager#change:addresses Emitted when a addresses change.
|
||||
*/
|
||||
class AddressManager extends EventEmitter {
|
||||
/**
|
||||
* Responsible for managing the peer addresses.
|
||||
* Peers can specify their listen and announce addresses.
|
||||
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
||||
* while the announce addresses will be used for the peer addresses' to other peers in the network.
|
||||
*
|
||||
* @class
|
||||
* @param {PeerId} peerId - The Peer ID of the node
|
||||
* @param {object} [options]
|
||||
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
|
||||
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
|
||||
* @param {Array<string>} [options.noAnnounce = []] - list of multiaddrs string representation to not announce.
|
||||
*/
|
||||
constructor ({ listen = [], announce = [], noAnnounce = [] } = {}) {
|
||||
this.listen = new Set(listen)
|
||||
this.announce = new Set(announce)
|
||||
this.noAnnounce = new Set(noAnnounce)
|
||||
constructor (peerId, { listen = [], announce = [] } = {}) {
|
||||
super()
|
||||
|
||||
this.peerId = peerId
|
||||
this.listen = new Set(listen.map(ma => ma.toString()))
|
||||
this.announce = new Set(announce.map(ma => ma.toString()))
|
||||
this.observed = new Set()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get peer listen multiaddrs.
|
||||
*
|
||||
* @returns {Array<Multiaddr>}
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
getListenAddrs () {
|
||||
return Array.from(this.listen).map((a) => multiaddr(a))
|
||||
@ -39,19 +53,49 @@ class AddressManager {
|
||||
/**
|
||||
* Get peer announcing multiaddrs.
|
||||
*
|
||||
* @returns {Array<Multiaddr>}
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
getAnnounceAddrs () {
|
||||
return Array.from(this.announce).map((a) => multiaddr(a))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get peer noAnnouncing multiaddrs.
|
||||
* Get observed multiaddrs.
|
||||
*
|
||||
* @returns {Array<Multiaddr>}
|
||||
*/
|
||||
getNoAnnounceAddrs () {
|
||||
return Array.from(this.noAnnounce).map((a) => multiaddr(a))
|
||||
getObservedAddrs () {
|
||||
return Array.from(this.observed).map((a) => multiaddr(a))
|
||||
}
|
||||
|
||||
/**
|
||||
* Add peer observed addresses
|
||||
*
|
||||
* @param {string | Multiaddr} addr
|
||||
*/
|
||||
addObservedAddr (addr) {
|
||||
let ma = multiaddr(addr)
|
||||
const remotePeer = ma.getPeerId()
|
||||
|
||||
// strip our peer id if it has been passed
|
||||
if (remotePeer) {
|
||||
const remotePeerId = PeerId.createFromB58String(remotePeer)
|
||||
|
||||
// use same encoding for comparison
|
||||
if (remotePeerId.equals(this.peerId)) {
|
||||
ma = ma.decapsulate(multiaddr(`/p2p/${this.peerId}`))
|
||||
}
|
||||
}
|
||||
|
||||
const addrString = ma.toString()
|
||||
|
||||
// do not trigger the change:addresses event if we already know about this address
|
||||
if (this.observed.has(addrString)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.observed.add(addrString)
|
||||
this.emit('change:addresses')
|
||||
}
|
||||
}
|
||||
|
||||
|
268
src/circuit/auto-relay.js
Normal file
268
src/circuit/auto-relay.js
Normal file
@ -0,0 +1,268 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = Object.assign(debug('libp2p:auto-relay'), {
|
||||
error: debug('libp2p:auto-relay:err')
|
||||
})
|
||||
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const { relay: multicodec } = require('./multicodec')
|
||||
const { canHop } = require('./circuit/hop')
|
||||
const { namespaceToCid } = require('./utils')
|
||||
const {
|
||||
CIRCUIT_PROTO_CODE,
|
||||
HOP_METADATA_KEY,
|
||||
HOP_METADATA_VALUE,
|
||||
RELAY_RENDEZVOUS_NS
|
||||
} = require('./constants')
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('../peer-store/address-book').Address} Address
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} AutoRelayProperties
|
||||
* @property {import('../')} libp2p
|
||||
*
|
||||
* @typedef {Object} AutoRelayOptions
|
||||
* @property {number} [maxListeners = 1] - maximum number of relays to listen.
|
||||
*/
|
||||
|
||||
class AutoRelay {
|
||||
/**
|
||||
* Creates an instance of AutoRelay.
|
||||
*
|
||||
* @class
|
||||
* @param {AutoRelayProperties & AutoRelayOptions} props
|
||||
*/
|
||||
constructor ({ libp2p, maxListeners = 1 }) {
|
||||
this._libp2p = libp2p
|
||||
this._peerId = libp2p.peerId
|
||||
this._peerStore = libp2p.peerStore
|
||||
this._connectionManager = libp2p.connectionManager
|
||||
this._transportManager = libp2p.transportManager
|
||||
this._addressSorter = libp2p.dialer.addressSorter
|
||||
|
||||
this.maxListeners = maxListeners
|
||||
|
||||
/**
|
||||
* @type {Set<string>}
|
||||
*/
|
||||
this._listenRelays = new Set()
|
||||
|
||||
this._onProtocolChange = this._onProtocolChange.bind(this)
|
||||
this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
|
||||
|
||||
this._peerStore.on('change:protocols', this._onProtocolChange)
|
||||
this._connectionManager.on('peer:disconnect', this._onPeerDisconnected)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a peer supports the relay protocol.
|
||||
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
||||
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
||||
* inside the threshold.
|
||||
*
|
||||
* @param {Object} props
|
||||
* @param {PeerId} props.peerId
|
||||
* @param {string[]} props.protocols
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _onProtocolChange ({ peerId, protocols }) {
|
||||
const id = peerId.toB58String()
|
||||
|
||||
// Check if it has the protocol
|
||||
const hasProtocol = protocols.find(protocol => protocol === multicodec)
|
||||
|
||||
// If no protocol, check if we were keeping the peer before as a listenRelay
|
||||
if (!hasProtocol && this._listenRelays.has(id)) {
|
||||
this._removeListenRelay(id)
|
||||
return
|
||||
} else if (!hasProtocol || this._listenRelays.has(id)) {
|
||||
return
|
||||
}
|
||||
|
||||
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
||||
try {
|
||||
const connection = this._connectionManager.get(peerId)
|
||||
if (!connection) {
|
||||
return
|
||||
}
|
||||
|
||||
// Do not hop on a relayed connection
|
||||
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
||||
log(`relayed connection to ${id} will not be used to hop on`)
|
||||
return
|
||||
}
|
||||
|
||||
const supportsHop = await canHop({ connection })
|
||||
|
||||
if (supportsHop) {
|
||||
this._peerStore.metadataBook.set(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
|
||||
await this._addListenRelay(connection, id)
|
||||
}
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Peer disconnects.
|
||||
*
|
||||
* @param {Connection} connection - connection to the peer
|
||||
* @returns {void}
|
||||
*/
|
||||
_onPeerDisconnected (connection) {
|
||||
const peerId = connection.remotePeer
|
||||
const id = peerId.toB58String()
|
||||
|
||||
// Not listening on this relay
|
||||
if (!this._listenRelays.has(id)) {
|
||||
return
|
||||
}
|
||||
|
||||
this._removeListenRelay(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to listen on the given relay connection.
|
||||
*
|
||||
* @private
|
||||
* @param {Connection} connection - connection to the peer
|
||||
* @param {string} id - peer identifier string
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _addListenRelay (connection, id) {
|
||||
// Check if already listening on enough relays
|
||||
if (this._listenRelays.size >= this.maxListeners) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get peer known addresses and sort them per public addresses first
|
||||
const remoteAddrs = this._peerStore.addressBook.getMultiaddrsForPeer(
|
||||
connection.remotePeer, this._addressSorter
|
||||
)
|
||||
|
||||
if (!remoteAddrs || !remoteAddrs.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
|
||||
this._listenRelays.add(id)
|
||||
|
||||
// Attempt to listen on relay
|
||||
try {
|
||||
await this._transportManager.listen([multiaddr(listenAddr)])
|
||||
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
this._listenRelays.delete(id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove listen relay.
|
||||
*
|
||||
* @private
|
||||
* @param {string} id - peer identifier string.
|
||||
* @returns {void}
|
||||
*/
|
||||
_removeListenRelay (id) {
|
||||
if (this._listenRelays.delete(id)) {
|
||||
// TODO: this should be responsibility of the connMgr
|
||||
this._listenOnAvailableHopRelays([id])
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to listen on available hop relay connections.
|
||||
* The following order will happen while we do not have enough relays.
|
||||
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
||||
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
||||
* 3. Search the network.
|
||||
*
|
||||
* @param {string[]} [peersToIgnore]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _listenOnAvailableHopRelays (peersToIgnore = []) {
|
||||
// TODO: The peer redial issue on disconnect should be handled by connection gating
|
||||
// Check if already listening on enough relays
|
||||
if (this._listenRelays.size >= this.maxListeners) {
|
||||
return
|
||||
}
|
||||
|
||||
const knownHopsToDial = []
|
||||
|
||||
// Check if we have known hop peers to use and attempt to listen on the already connected
|
||||
for (const [id, metadataMap] of this._peerStore.metadataBook.data.entries()) {
|
||||
// Continue to next if listening on this or peer to ignore
|
||||
if (this._listenRelays.has(id) || peersToIgnore.includes(id)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const supportsHop = metadataMap.get(HOP_METADATA_KEY)
|
||||
|
||||
// Continue to next if it does not support Hop
|
||||
if (!supportsHop || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
|
||||
continue
|
||||
}
|
||||
|
||||
const peerId = PeerId.createFromCID(id)
|
||||
const connection = this._connectionManager.get(peerId)
|
||||
|
||||
// If not connected, store for possible later use.
|
||||
if (!connection) {
|
||||
knownHopsToDial.push(peerId)
|
||||
continue
|
||||
}
|
||||
|
||||
await this._addListenRelay(connection, id)
|
||||
|
||||
// Check if already listening on enough relays
|
||||
if (this._listenRelays.size >= this.maxListeners) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Try to listen on known peers that are not connected
|
||||
for (const peerId of knownHopsToDial) {
|
||||
const connection = await this._libp2p.dial(peerId)
|
||||
await this._addListenRelay(connection, peerId.toB58String())
|
||||
|
||||
// Check if already listening on enough relays
|
||||
if (this._listenRelays.size >= this.maxListeners) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Try to find relays to hop on the network
|
||||
try {
|
||||
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
||||
for await (const provider of this._libp2p.contentRouting.findProviders(cid)) {
|
||||
if (!provider.multiaddrs.length) {
|
||||
continue
|
||||
}
|
||||
const peerId = provider.id
|
||||
|
||||
this._peerStore.addressBook.add(peerId, provider.multiaddrs)
|
||||
const connection = await this._libp2p.dial(peerId)
|
||||
|
||||
await this._addListenRelay(connection, peerId.toB58String())
|
||||
|
||||
// Check if already listening on enough relays
|
||||
if (this._listenRelays.size >= this.maxListeners) {
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AutoRelay
|
@ -1,22 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:hop')
|
||||
log.error = debug('libp2p:circuit:hop:error')
|
||||
const log = Object.assign(debug('libp2p:circuit:hop'), {
|
||||
error: debug('libp2p:circuit:hop:err')
|
||||
})
|
||||
const errCode = require('err-code')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const { validateAddrs } = require('./utils')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
const { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
const pipe = require('it-pipe')
|
||||
const errCode = require('err-code')
|
||||
const { pipe } = require('it-pipe')
|
||||
const { codes: Errors } = require('../../errors')
|
||||
|
||||
const { stop } = require('./stop')
|
||||
|
||||
const multicodec = require('./../multicodec')
|
||||
|
||||
module.exports.handleHop = async function handleHop ({
|
||||
/**
|
||||
* @typedef {import('../../types').CircuitRequest} CircuitRequest
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('./stream-handler')<CircuitRequest>} StreamHandlerT
|
||||
* @typedef {import('../transport')} Transport
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} HopRequest
|
||||
* @property {Connection} connection
|
||||
* @property {CircuitRequest} request
|
||||
* @property {StreamHandlerT} streamHandler
|
||||
* @property {Transport} circuit
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {HopRequest} options
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function handleHop ({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
@ -51,6 +71,9 @@ module.exports.handleHop = async function handleHop ({
|
||||
}
|
||||
|
||||
// TODO: Handle being an active relay
|
||||
if (!destinationConnection) {
|
||||
return
|
||||
}
|
||||
|
||||
// Handle the incoming HOP request by performing a STOP request
|
||||
const stopRequest = {
|
||||
@ -63,8 +86,7 @@ module.exports.handleHop = async function handleHop ({
|
||||
try {
|
||||
destinationStream = await stop({
|
||||
connection: destinationConnection,
|
||||
request: stopRequest,
|
||||
circuit
|
||||
request: stopRequest
|
||||
})
|
||||
} catch (err) {
|
||||
return log.error(err)
|
||||
@ -91,10 +113,10 @@ module.exports.handleHop = async function handleHop ({
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {Connection} options.connection - Connection to the relay
|
||||
* @param {*} options.request
|
||||
* @param {CircuitRequest} options.request
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
module.exports.hop = async function hop ({
|
||||
async function hop ({
|
||||
connection,
|
||||
request
|
||||
}) {
|
||||
@ -116,16 +138,44 @@ module.exports.hop = async function hop ({
|
||||
throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a CAN_HOP request to a relay peer, in order to understand its capabilities.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {Connection} options.connection - Connection to the relay
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async function canHop ({
|
||||
connection
|
||||
}) {
|
||||
// Create a new stream to the relay
|
||||
const { stream } = await connection.newStream([multicodec.relay])
|
||||
// Send the HOP request
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
streamHandler.write({
|
||||
type: CircuitPB.Type.CAN_HOP
|
||||
})
|
||||
|
||||
const response = await streamHandler.read()
|
||||
await streamHandler.close()
|
||||
|
||||
if (response.code !== CircuitPB.Status.SUCCESS) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an unencoded CAN_HOP response based on the Circuits configuration
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {Connection} options.connection
|
||||
* @param {StreamHandler} options.streamHandler
|
||||
* @param {Circuit} options.circuit
|
||||
* @param {StreamHandlerT} options.streamHandler
|
||||
* @param {Transport} options.circuit
|
||||
* @private
|
||||
*/
|
||||
module.exports.handleCanHop = function handleCanHop ({
|
||||
function handleCanHop ({
|
||||
connection,
|
||||
streamHandler,
|
||||
circuit
|
||||
@ -137,3 +187,10 @@ module.exports.handleCanHop = function handleCanHop ({
|
||||
code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
handleHop,
|
||||
hop,
|
||||
canHop,
|
||||
handleCanHop
|
||||
}
|
||||
|
@ -1,23 +1,31 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = Object.assign(debug('libp2p:circuit:stop'), {
|
||||
error: debug('libp2p:circuit:stop:err')
|
||||
})
|
||||
|
||||
const { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
const multicodec = require('../multicodec')
|
||||
const StreamHandler = require('./stream-handler')
|
||||
const { validateAddrs } = require('./utils')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:stop')
|
||||
log.error = debug('libp2p:circuit:stop:error')
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
* @typedef {import('../../types').CircuitRequest} CircuitRequest
|
||||
* @typedef {import('./stream-handler')<CircuitRequest>} StreamHandlerT
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handles incoming STOP requests
|
||||
*
|
||||
* @private
|
||||
* @param {*} options
|
||||
* @param {Object} options
|
||||
* @param {Connection} options.connection
|
||||
* @param {*} options.request - The CircuitRelay protobuf request (unencoded)
|
||||
* @param {StreamHandler} options.streamHandler
|
||||
* @returns {Promise<*>} Resolves a duplex iterable
|
||||
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
|
||||
* @param {StreamHandlerT} options.streamHandler
|
||||
* @returns {Promise<MuxedStream>|void} Resolves a duplex iterable
|
||||
*/
|
||||
module.exports.handleStop = function handleStop ({
|
||||
connection,
|
||||
@ -44,10 +52,10 @@ module.exports.handleStop = function handleStop ({
|
||||
* Creates a STOP request
|
||||
*
|
||||
* @private
|
||||
* @param {*} options
|
||||
* @param {Object} options
|
||||
* @param {Connection} options.connection
|
||||
* @param {*} options.request - The CircuitRelay protobuf request (unencoded)
|
||||
* @returns {Promise<*>} Resolves a duplex iterable
|
||||
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
|
||||
* @returns {Promise<MuxedStream|void>} Resolves a duplex iterable
|
||||
*/
|
||||
module.exports.stop = async function stop ({
|
||||
connection,
|
||||
|
@ -1,20 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = Object.assign(debug('libp2p:circuit:stream-handler'), {
|
||||
error: debug('libp2p:circuit:stream-handler:err')
|
||||
})
|
||||
|
||||
const lp = require('it-length-prefixed')
|
||||
const handshake = require('it-handshake')
|
||||
const { CircuitRelay: CircuitPB } = require('../protocol')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:stream-handler')
|
||||
log.error = debug('libp2p:circuit:stream-handler:error')
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
class StreamHandler {
|
||||
/**
|
||||
* Create a stream handler for connection
|
||||
*
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {*} options.stream - A duplex iterable
|
||||
* @param {number} options.maxLength - max bytes length of message
|
||||
* @param {MuxedStream} options.stream - A duplex iterable
|
||||
* @param {number} [options.maxLength = 4096] - max bytes length of message
|
||||
*/
|
||||
constructor ({ stream, maxLength = 4096 }) {
|
||||
this.stream = stream
|
||||
@ -27,7 +36,7 @@ class StreamHandler {
|
||||
* Read and decode message
|
||||
*
|
||||
* @async
|
||||
* @returns {void}
|
||||
* @returns {Promise<T|undefined>}
|
||||
*/
|
||||
async read () {
|
||||
const msg = await this.decoder.next()
|
||||
@ -45,10 +54,12 @@ class StreamHandler {
|
||||
/**
|
||||
* Encode and write array of buffers
|
||||
*
|
||||
* @param {*} msg - An unencoded CircuitRelay protobuf message
|
||||
* @param {CircuitPB} msg - An unencoded CircuitRelay protobuf message
|
||||
* @returns {void}
|
||||
*/
|
||||
write (msg) {
|
||||
log('write message type %s', msg.type)
|
||||
// @ts-ignore lp.encode expects type type 'Buffer | BufferList', not 'Uint8Array'
|
||||
this.shake.write(lp.encode.single(CircuitPB.encode(msg)))
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,16 @@
|
||||
const multiaddr = require('multiaddr')
|
||||
const { CircuitRelay } = require('../protocol')
|
||||
|
||||
/**
|
||||
* @typedef {import('./stream-handler')} StreamHandler
|
||||
* @typedef {import('../../types').CircuitStatus} CircuitStatus
|
||||
*/
|
||||
|
||||
/**
|
||||
* Write a response
|
||||
*
|
||||
* @param {StreamHandler} streamHandler
|
||||
* @param {CircuitRelay.Status} status
|
||||
* @param {CircuitStatus} status
|
||||
*/
|
||||
function writeResponse (streamHandler, status) {
|
||||
streamHandler.write({
|
||||
|
12
src/circuit/constants.js
Normal file
12
src/circuit/constants.js
Normal file
@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const minute = 60 * 1000
|
||||
|
||||
module.exports = {
|
||||
ADVERTISE_BOOT_DELAY: 15 * minute, // Delay before HOP relay service is advertised on the network
|
||||
ADVERTISE_TTL: 30 * minute, // Delay Between HOP relay service advertisements on the network
|
||||
CIRCUIT_PROTO_CODE: 290, // Multicodec code
|
||||
HOP_METADATA_KEY: 'hop_relay', // PeerStore metadaBook key for HOP relay service
|
||||
HOP_METADATA_VALUE: 'true', // PeerStore metadaBook value for HOP relay service
|
||||
RELAY_RENDEZVOUS_NS: '/libp2p/relay' // Relay HOP relay service namespace for discovery
|
||||
}
|
@ -1,187 +1,109 @@
|
||||
'use strict'
|
||||
|
||||
const mafmt = require('mafmt')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const withIs = require('class-is')
|
||||
const { CircuitRelay: CircuitPB } = require('./protocol')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit')
|
||||
log.error = debug('libp2p:circuit:error')
|
||||
const toConnection = require('libp2p-utils/src/stream-to-ma-conn')
|
||||
const log = Object.assign(debug('libp2p:relay'), {
|
||||
error: debug('libp2p:relay:err')
|
||||
})
|
||||
|
||||
const { relay: multicodec } = require('./multicodec')
|
||||
const createListener = require('./listener')
|
||||
const { handleCanHop, handleHop, hop } = require('./circuit/hop')
|
||||
const { handleStop } = require('./circuit/stop')
|
||||
const StreamHandler = require('./circuit/stream-handler')
|
||||
const {
|
||||
setDelayedInterval,
|
||||
clearDelayedInterval
|
||||
} = require('set-delayed-interval')
|
||||
|
||||
class Circuit {
|
||||
const AutoRelay = require('./auto-relay')
|
||||
const { namespaceToCid } = require('./utils')
|
||||
const {
|
||||
ADVERTISE_BOOT_DELAY,
|
||||
ADVERTISE_TTL,
|
||||
RELAY_RENDEZVOUS_NS
|
||||
} = require('./constants')
|
||||
|
||||
/**
|
||||
* @typedef {import('../')} Libp2p
|
||||
*
|
||||
* @typedef {Object} RelayAdvertiseOptions
|
||||
* @property {number} [bootDelay = ADVERTISE_BOOT_DELAY]
|
||||
* @property {boolean} [enabled = true]
|
||||
* @property {number} [ttl = ADVERTISE_TTL]
|
||||
*
|
||||
* @typedef {Object} HopOptions
|
||||
* @property {boolean} [enabled = false]
|
||||
* @property {boolean} [active = false]
|
||||
*
|
||||
* @typedef {Object} AutoRelayOptions
|
||||
* @property {number} [maxListeners = 2] - maximum number of relays to listen.
|
||||
* @property {boolean} [enabled = false]
|
||||
*/
|
||||
|
||||
class Relay {
|
||||
/**
|
||||
* Creates an instance of Circuit.
|
||||
* Creates an instance of Relay.
|
||||
*
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {Libp2p} options.libp2p
|
||||
* @param {Upgrader} options.upgrader
|
||||
* @param {Libp2p} libp2p
|
||||
*/
|
||||
constructor ({ libp2p, upgrader }) {
|
||||
this._dialer = libp2p.dialer
|
||||
this._registrar = libp2p.registrar
|
||||
this._connectionManager = libp2p.connectionManager
|
||||
this._upgrader = upgrader
|
||||
this._options = libp2p._config.relay
|
||||
constructor (libp2p) {
|
||||
this._libp2p = libp2p
|
||||
this.peerId = libp2p.peerId
|
||||
this._registrar.handle(multicodec, this._onProtocol.bind(this))
|
||||
}
|
||||
|
||||
async _onProtocol ({ connection, stream, protocol }) {
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
const request = await streamHandler.read()
|
||||
const circuit = this
|
||||
let virtualConnection
|
||||
|
||||
switch (request.type) {
|
||||
case CircuitPB.Type.CAN_HOP: {
|
||||
log('received CAN_HOP request from %s', connection.remotePeer.toB58String())
|
||||
await handleCanHop({ circuit, connection, streamHandler })
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.HOP: {
|
||||
log('received HOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleHop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
})
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.STOP: {
|
||||
log('received STOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleStop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
})
|
||||
break
|
||||
}
|
||||
default: {
|
||||
log('Request of type %s not supported', request.type)
|
||||
}
|
||||
this._options = {
|
||||
advertise: {
|
||||
bootDelay: ADVERTISE_BOOT_DELAY,
|
||||
enabled: true,
|
||||
ttl: ADVERTISE_TTL,
|
||||
...libp2p._config.relay.advertise
|
||||
},
|
||||
...libp2p._config.relay
|
||||
}
|
||||
|
||||
if (virtualConnection) {
|
||||
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
|
||||
const localAddr = multiaddr(request.srcPeer.addrs[0])
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr,
|
||||
localAddr
|
||||
})
|
||||
const type = CircuitPB.Type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
||||
log('new %s connection %s', type, maConn.remoteAddr)
|
||||
// Create autoRelay if enabled
|
||||
this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay })
|
||||
|
||||
const conn = await this._upgrader.upgradeInbound(maConn)
|
||||
log('%s connection %s upgraded', type, maConn.remoteAddr)
|
||||
this.handler && this.handler(conn)
|
||||
this._advertiseService = this._advertiseService.bind(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Relay service.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
start () {
|
||||
// Advertise service if HOP enabled
|
||||
const canHop = this._options.hop.enabled
|
||||
|
||||
if (canHop && this._options.advertise.enabled) {
|
||||
this._timeout = setDelayedInterval(
|
||||
this._advertiseService, this._options.advertise.ttl, this._options.advertise.bootDelay
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
* Stop Relay service.
|
||||
*
|
||||
* @param {multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Object} options - dial options
|
||||
* @param {AbortSignal} [options.signal] - An optional abort signal
|
||||
* @returns {Connection} - the connection
|
||||
* @returns {void}
|
||||
*/
|
||||
async dial (ma, options) {
|
||||
// Check the multiaddr to see if it contains a relay and a destination peer
|
||||
const addrs = ma.toString().split('/p2p-circuit')
|
||||
const relayAddr = multiaddr(addrs[0])
|
||||
const destinationAddr = multiaddr(addrs[addrs.length - 1])
|
||||
const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
|
||||
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
|
||||
|
||||
let disconnectOnFailure = false
|
||||
let relayConnection = this._connectionManager.get(relayPeer)
|
||||
if (!relayConnection) {
|
||||
relayConnection = await this._dialer.connectToPeer(relayAddr, options)
|
||||
disconnectOnFailure = true
|
||||
}
|
||||
stop () {
|
||||
clearDelayedInterval(this._timeout)
|
||||
}
|
||||
|
||||
/**
|
||||
* Advertise hop relay service in the network.
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _advertiseService () {
|
||||
try {
|
||||
const virtualConnection = await hop({
|
||||
connection: relayConnection,
|
||||
circuit: this,
|
||||
request: {
|
||||
type: CircuitPB.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.peerId.toBytes(),
|
||||
addrs: this._libp2p.multiaddrs.map(addr => addr.bytes)
|
||||
},
|
||||
dstPeer: {
|
||||
id: destinationPeer.toBytes(),
|
||||
addrs: [multiaddr(destinationAddr).bytes]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`)
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr: ma,
|
||||
localAddr
|
||||
})
|
||||
log('new outbound connection %s', maConn.remoteAddr)
|
||||
|
||||
return this._upgrader.upgradeOutbound(maConn)
|
||||
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
||||
await this._libp2p.contentRouting.provide(cid)
|
||||
} catch (err) {
|
||||
log.error('Circuit relay dial failed', err)
|
||||
disconnectOnFailure && await relayConnection.close()
|
||||
throw err
|
||||
if (err.code === 'NO_ROUTERS_AVAILABLE') {
|
||||
log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
|
||||
// Stop the advertise
|
||||
this.stop()
|
||||
} else {
|
||||
log.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener
|
||||
*
|
||||
* @param {any} options
|
||||
* @param {Function} handler
|
||||
* @returns {listener}
|
||||
*/
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
// Called on successful HOP and STOP requests
|
||||
this.handler = handler
|
||||
|
||||
return createListener(this, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter check for all Multiaddrs that this transport can dial on
|
||||
*
|
||||
* @param {Array<Multiaddr>} multiaddrs
|
||||
* @returns {Array<Multiaddr>}
|
||||
*/
|
||||
filter (multiaddrs) {
|
||||
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
|
||||
|
||||
return multiaddrs.filter((ma) => {
|
||||
return mafmt.Circuit.matches(ma)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Circuit}
|
||||
*/
|
||||
module.exports = withIs(Circuit, { className: 'Circuit', symbolName: '@libp2p/js-libp2p-circuit/circuit' })
|
||||
module.exports = Relay
|
||||
|
@ -1,43 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const { EventEmitter } = require('events')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:circuit:listener')
|
||||
log.err = debug('libp2p:circuit:error:listener')
|
||||
/**
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
* @typedef {import('libp2p-interfaces/src/transport/types').Listener} Listener
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {*} circuit
|
||||
* @param {import('../')} libp2p
|
||||
* @returns {Listener} a transport listener
|
||||
*/
|
||||
module.exports = (circuit) => {
|
||||
const listener = new EventEmitter()
|
||||
module.exports = (libp2p) => {
|
||||
const listeningAddrs = new Map()
|
||||
|
||||
/**
|
||||
* Add swarm handler and listen for incoming connections
|
||||
*
|
||||
* @param {Multiaddr} addr
|
||||
* @returns {void}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
listener.listen = async (addr) => {
|
||||
async function listen (addr) {
|
||||
const addrString = String(addr).split('/p2p-circuit').find(a => a !== '')
|
||||
|
||||
const relayConn = await circuit._dialer.connectToPeer(multiaddr(addrString))
|
||||
const relayConn = await libp2p.dial(multiaddr(addrString))
|
||||
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
||||
|
||||
listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)
|
||||
listener.emit('listening')
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Remove the peers from our topology
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
listener.close = () => {}
|
||||
|
||||
/**
|
||||
* Get fixed up multiaddrs
|
||||
*
|
||||
@ -54,7 +47,7 @@ module.exports = (circuit) => {
|
||||
*
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
listener.getAddrs = () => {
|
||||
function getAddrs () {
|
||||
const addrs = []
|
||||
for (const addr of listeningAddrs.values()) {
|
||||
addrs.push(addr)
|
||||
@ -62,5 +55,22 @@ module.exports = (circuit) => {
|
||||
return addrs
|
||||
}
|
||||
|
||||
/** @type Listener */
|
||||
const listener = Object.assign(new EventEmitter(), {
|
||||
close: () => Promise.resolve(),
|
||||
listen,
|
||||
getAddrs
|
||||
})
|
||||
|
||||
// Remove listeningAddrs when a peer disconnects
|
||||
libp2p.connectionManager.on('peer:disconnect', (connection) => {
|
||||
const deleted = listeningAddrs.delete(connection.remotePeer.toB58String())
|
||||
|
||||
if (deleted) {
|
||||
// Announce listen addresses change
|
||||
listener.emit('close')
|
||||
}
|
||||
})
|
||||
|
||||
return listener
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
'use strict'
|
||||
const protobuf = require('protons')
|
||||
|
||||
/** @type {{CircuitRelay: import('../../types').CircuitMessageProto}} */
|
||||
module.exports = protobuf(`
|
||||
message CircuitRelay {
|
||||
|
||||
|
218
src/circuit/transport.js
Normal file
218
src/circuit/transport.js
Normal file
@ -0,0 +1,218 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = Object.assign(debug('libp2p:circuit'), {
|
||||
error: debug('libp2p:circuit:err')
|
||||
})
|
||||
|
||||
const mafmt = require('mafmt')
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const { CircuitRelay: CircuitPB } = require('./protocol')
|
||||
|
||||
const toConnection = require('libp2p-utils/src/stream-to-ma-conn')
|
||||
|
||||
const { relay: multicodec } = require('./multicodec')
|
||||
const createListener = require('./listener')
|
||||
const { handleCanHop, handleHop, hop } = require('./circuit/hop')
|
||||
const { handleStop } = require('./circuit/stop')
|
||||
const StreamHandler = require('./circuit/stream-handler')
|
||||
|
||||
const transportSymbol = Symbol.for('@libp2p/js-libp2p-circuit/circuit')
|
||||
|
||||
/**
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
* @typedef {import('../types').CircuitRequest} CircuitRequest
|
||||
*/
|
||||
|
||||
class Circuit {
|
||||
/**
|
||||
* Creates an instance of the Circuit Transport.
|
||||
*
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {import('../')} options.libp2p
|
||||
* @param {import('../upgrader')} options.upgrader
|
||||
*/
|
||||
constructor ({ libp2p, upgrader }) {
|
||||
this._dialer = libp2p.dialer
|
||||
this._registrar = libp2p.registrar
|
||||
this._connectionManager = libp2p.connectionManager
|
||||
this._upgrader = upgrader
|
||||
this._options = libp2p._config.relay
|
||||
this._libp2p = libp2p
|
||||
this.peerId = libp2p.peerId
|
||||
|
||||
this._registrar.handle(multicodec, this._onProtocol.bind(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {Connection} props.connection
|
||||
* @param {MuxedStream} props.stream
|
||||
*/
|
||||
async _onProtocol ({ connection, stream }) {
|
||||
/** @type {import('./circuit/stream-handler')<CircuitRequest>} */
|
||||
const streamHandler = new StreamHandler({ stream })
|
||||
const request = await streamHandler.read()
|
||||
|
||||
if (!request) {
|
||||
return
|
||||
}
|
||||
|
||||
const circuit = this
|
||||
let virtualConnection
|
||||
|
||||
switch (request.type) {
|
||||
case CircuitPB.Type.CAN_HOP: {
|
||||
log('received CAN_HOP request from %s', connection.remotePeer.toB58String())
|
||||
await handleCanHop({ circuit, connection, streamHandler })
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.HOP: {
|
||||
log('received HOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleHop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler,
|
||||
circuit
|
||||
})
|
||||
break
|
||||
}
|
||||
case CircuitPB.Type.STOP: {
|
||||
log('received STOP request from %s', connection.remotePeer.toB58String())
|
||||
virtualConnection = await handleStop({
|
||||
connection,
|
||||
request,
|
||||
streamHandler
|
||||
})
|
||||
break
|
||||
}
|
||||
default: {
|
||||
log('Request of type %s not supported', request.type)
|
||||
}
|
||||
}
|
||||
|
||||
if (virtualConnection) {
|
||||
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
|
||||
const localAddr = multiaddr(request.srcPeer.addrs[0])
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr,
|
||||
localAddr
|
||||
})
|
||||
const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
||||
log('new %s connection %s', type, maConn.remoteAddr)
|
||||
|
||||
const conn = await this._upgrader.upgradeInbound(maConn)
|
||||
log('%s connection %s upgraded', type, maConn.remoteAddr)
|
||||
this.handler && this.handler(conn)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dial a peer over a relay
|
||||
*
|
||||
* @param {Multiaddr} ma - the multiaddr of the peer to dial
|
||||
* @param {Object} options - dial options
|
||||
* @param {AbortSignal} [options.signal] - An optional abort signal
|
||||
* @returns {Promise<Connection>} - the connection
|
||||
*/
|
||||
async dial (ma, options) {
|
||||
// Check the multiaddr to see if it contains a relay and a destination peer
|
||||
const addrs = ma.toString().split('/p2p-circuit')
|
||||
const relayAddr = multiaddr(addrs[0])
|
||||
const destinationAddr = multiaddr(addrs[addrs.length - 1])
|
||||
const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
|
||||
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
|
||||
|
||||
let disconnectOnFailure = false
|
||||
let relayConnection = this._connectionManager.get(relayPeer)
|
||||
if (!relayConnection) {
|
||||
relayConnection = await this._dialer.connectToPeer(relayAddr, options)
|
||||
disconnectOnFailure = true
|
||||
}
|
||||
|
||||
try {
|
||||
const virtualConnection = await hop({
|
||||
connection: relayConnection,
|
||||
request: {
|
||||
type: CircuitPB.Type.HOP,
|
||||
srcPeer: {
|
||||
id: this.peerId.toBytes(),
|
||||
addrs: this._libp2p.multiaddrs.map(addr => addr.bytes)
|
||||
},
|
||||
dstPeer: {
|
||||
id: destinationPeer.toBytes(),
|
||||
addrs: [multiaddr(destinationAddr).bytes]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`)
|
||||
const maConn = toConnection({
|
||||
stream: virtualConnection,
|
||||
remoteAddr: ma,
|
||||
localAddr
|
||||
})
|
||||
log('new outbound connection %s', maConn.remoteAddr)
|
||||
|
||||
return this._upgrader.upgradeOutbound(maConn)
|
||||
} catch (err) {
|
||||
log.error('Circuit relay dial failed', err)
|
||||
disconnectOnFailure && await relayConnection.close()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener
|
||||
*
|
||||
* @param {any} options
|
||||
* @param {Function} handler
|
||||
* @returns {import('libp2p-interfaces/src/transport/types').Listener}
|
||||
*/
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
// Called on successful HOP and STOP requests
|
||||
this.handler = handler
|
||||
|
||||
return createListener(this._libp2p)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter check for all Multiaddrs that this transport can dial on
|
||||
*
|
||||
* @param {Multiaddr[]} multiaddrs
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
filter (multiaddrs) {
|
||||
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
|
||||
|
||||
return multiaddrs.filter((ma) => {
|
||||
return mafmt.Circuit.matches(ma)
|
||||
})
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag] () {
|
||||
return 'Circuit'
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value is a Transport instance.
|
||||
*
|
||||
* @param {any} other
|
||||
* @returns {other is Transport}
|
||||
*/
|
||||
static isTransport (other) {
|
||||
return Boolean(other && other[transportSymbol])
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Circuit
|
19
src/circuit/utils.js
Normal file
19
src/circuit/utils.js
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const CID = require('cids')
|
||||
const multihashing = require('multihashing-async')
|
||||
|
||||
const TextEncoder = require('ipfs-utils/src/text-encoder')
|
||||
|
||||
/**
|
||||
* Convert a namespace string into a cid.
|
||||
*
|
||||
* @param {string} namespace
|
||||
* @returns {Promise<CID>}
|
||||
*/
|
||||
module.exports.namespaceToCid = async (namespace) => {
|
||||
const bytes = new TextEncoder().encode(namespace)
|
||||
const hash = await multihashing(bytes, 'sha2-256')
|
||||
|
||||
return new CID(hash)
|
||||
}
|
@ -4,7 +4,10 @@ const mergeOptions = require('merge-options')
|
||||
const { dnsaddrResolver } = require('multiaddr/src/resolvers')
|
||||
|
||||
const Constants = require('./constants')
|
||||
const { AGENT_VERSION } = require('./identify/consts')
|
||||
const RelayConstants = require('./circuit/constants')
|
||||
|
||||
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
||||
const { FaultTolerance } = require('./transport-manager')
|
||||
|
||||
const DefaultConfig = {
|
||||
@ -25,7 +28,11 @@ const DefaultConfig = {
|
||||
dialTimeout: Constants.DIAL_TIMEOUT,
|
||||
resolvers: {
|
||||
dnsaddr: dnsaddrResolver
|
||||
}
|
||||
},
|
||||
addressSorter: publicAddressesFirst
|
||||
},
|
||||
host: {
|
||||
agentVersion: AGENT_VERSION
|
||||
},
|
||||
metrics: {
|
||||
enabled: false
|
||||
@ -34,6 +41,13 @@ const DefaultConfig = {
|
||||
persistence: false,
|
||||
threshold: 5
|
||||
},
|
||||
peerRouting: {
|
||||
refreshManager: {
|
||||
enabled: true,
|
||||
interval: 6e5,
|
||||
bootDelay: 10e3
|
||||
}
|
||||
},
|
||||
config: {
|
||||
dht: {
|
||||
enabled: false,
|
||||
@ -45,20 +59,36 @@ const DefaultConfig = {
|
||||
timeout: 10e3
|
||||
}
|
||||
},
|
||||
nat: {
|
||||
enabled: true,
|
||||
ttl: 7200,
|
||||
keepAlive: true,
|
||||
gateway: null,
|
||||
externalIp: null,
|
||||
pmp: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
peerDiscovery: {
|
||||
autoDial: true
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true,
|
||||
signMessages: true,
|
||||
strictSigning: true
|
||||
enabled: true
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
advertise: {
|
||||
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
||||
enabled: false,
|
||||
ttl: RelayConstants.ADVERTISE_TTL
|
||||
},
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
},
|
||||
autoRelay: {
|
||||
enabled: false,
|
||||
maxListeners: 2
|
||||
}
|
||||
},
|
||||
transport: {}
|
||||
|
@ -1,20 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:connection-manager')
|
||||
log.error = debug('libp2p:connection-manager:error')
|
||||
const log = Object.assign(debug('libp2p:connection-manager'), {
|
||||
error: debug('libp2p:connection-manager:err')
|
||||
})
|
||||
|
||||
const errcode = require('err-code')
|
||||
const mergeOptions = require('merge-options')
|
||||
const LatencyMonitor = require('./latency-monitor')
|
||||
const retimer = require('retimer')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
/** @typedef {import('../types').EventEmitterFactory} Events */
|
||||
/** @type Events */
|
||||
const EventEmitter = require('events')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const {
|
||||
ERR_INVALID_PARAMETERS
|
||||
codes: { ERR_INVALID_PARAMETERS }
|
||||
} = require('../errors')
|
||||
|
||||
const defaultOptions = {
|
||||
@ -31,29 +34,39 @@ const defaultOptions = {
|
||||
}
|
||||
|
||||
/**
|
||||
* Responsible for managing known connections.
|
||||
* @typedef {import('../')} Libp2p
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ConnectionManagerOptions
|
||||
* @property {number} [maxConnections = Infinity] - The maximum number of connections allowed.
|
||||
* @property {number} [minConnections = 0] - The minimum number of connections to avoid pruning.
|
||||
* @property {number} [maxData = Infinity] - The max data (in and out), per average interval to allow.
|
||||
* @property {number} [maxSentData = Infinity] - The max outgoing data, per average interval to allow.
|
||||
* @property {number} [maxReceivedData = Infinity] - The max incoming data, per average interval to allow.
|
||||
* @property {number} [maxEventLoopDelay = Infinity] - The upper limit the event loop can take to run.
|
||||
* @property {number} [pollInterval = 2000] - How often, in milliseconds, metrics and latency should be checked.
|
||||
* @property {number} [movingAverageInterval = 60000] - How often, in milliseconds, to compute averages.
|
||||
* @property {number} [defaultPeerValue = 1] - The value of the peer.
|
||||
* @property {boolean} [autoDial = true] - Should preemptively guarantee connections are above the low watermark.
|
||||
* @property {number} [autoDialInterval = 10000] - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
|
||||
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
|
||||
*/
|
||||
class ConnectionManager extends EventEmitter {
|
||||
/**
|
||||
* Responsible for managing known connections.
|
||||
*
|
||||
* @class
|
||||
* @param {Libp2p} libp2p
|
||||
* @param {object} options
|
||||
* @param {number} options.maxConnections - The maximum number of connections allowed. Default=Infinity
|
||||
* @param {number} options.minConnections - The minimum number of connections to avoid pruning. Default=0
|
||||
* @param {number} options.maxData - The max data (in and out), per average interval to allow. Default=Infinity
|
||||
* @param {number} options.maxSentData - The max outgoing data, per average interval to allow. Default=Infinity
|
||||
* @param {number} options.maxReceivedData - The max incoming data, per average interval to allow.. Default=Infinity
|
||||
* @param {number} options.maxEventLoopDelay - The upper limit the event loop can take to run. Default=Infinity
|
||||
* @param {number} options.pollInterval - How often, in milliseconds, metrics and latency should be checked. Default=2000
|
||||
* @param {number} options.movingAverageInterval - How often, in milliseconds, to compute averages. Default=60000
|
||||
* @param {number} options.defaultPeerValue - The value of the peer. Default=1
|
||||
* @param {boolean} options.autoDial - Should preemptively guarantee connections are above the low watermark. Default=true
|
||||
* @param {number} options.autoDialInterval - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark. Default=10000
|
||||
* @param {ConnectionManagerOptions} options
|
||||
*/
|
||||
constructor (libp2p, options) {
|
||||
constructor (libp2p, options = {}) {
|
||||
super()
|
||||
|
||||
this._libp2p = libp2p
|
||||
@ -66,8 +79,6 @@ class ConnectionManager extends EventEmitter {
|
||||
|
||||
log('options: %j', this._options)
|
||||
|
||||
this._libp2p = libp2p
|
||||
|
||||
/**
|
||||
* Map of peer identifiers to their peer value for pruning connections.
|
||||
*
|
||||
@ -78,7 +89,7 @@ class ConnectionManager extends EventEmitter {
|
||||
/**
|
||||
* Map of connections per peer
|
||||
*
|
||||
* @type {Map<string, Array<conn>>}
|
||||
* @type {Map<string, Connection[]>}
|
||||
*/
|
||||
this.connections = new Map()
|
||||
|
||||
@ -149,7 +160,7 @@ class ConnectionManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
await tasks
|
||||
await Promise.all(tasks)
|
||||
this.connections.clear()
|
||||
}
|
||||
|
||||
@ -159,15 +170,13 @@ class ConnectionManager extends EventEmitter {
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @param {number} value - A number between 0 and 1
|
||||
* @returns {void}
|
||||
*/
|
||||
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)
|
||||
this._peerValues.set(peerId.toB58String(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,21 +186,24 @@ class ConnectionManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
_checkMetrics () {
|
||||
const movingAverages = this._libp2p.metrics.global.movingAverages
|
||||
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkMaxLimit('maxReceivedData', received)
|
||||
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkMaxLimit('maxSentData', sent)
|
||||
const total = received + sent
|
||||
this._checkMaxLimit('maxData', total)
|
||||
log('metrics update', total)
|
||||
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
|
||||
if (this._libp2p.metrics) {
|
||||
const movingAverages = this._libp2p.metrics.global.movingAverages
|
||||
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkMaxLimit('maxReceivedData', received)
|
||||
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
||||
this._checkMaxLimit('maxSentData', sent)
|
||||
const total = received + sent
|
||||
this._checkMaxLimit('maxData', total)
|
||||
log('metrics update', total)
|
||||
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks the incoming connection and check the connection limit
|
||||
*
|
||||
* @param {Connection} connection
|
||||
* @returns {void}
|
||||
*/
|
||||
onConnect (connection) {
|
||||
const peerId = connection.remotePeer
|
||||
@ -218,6 +230,7 @@ class ConnectionManager extends EventEmitter {
|
||||
* Removes the connection from tracking
|
||||
*
|
||||
* @param {Connection} connection
|
||||
* @returns {void}
|
||||
*/
|
||||
onDisconnect (connection) {
|
||||
const peerId = connection.remotePeer.toB58String()
|
||||
@ -237,7 +250,7 @@ class ConnectionManager extends EventEmitter {
|
||||
* Get a connection with a peer.
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Connection}
|
||||
* @returns {Connection|null}
|
||||
*/
|
||||
get (peerId) {
|
||||
const connections = this.getAll(peerId)
|
||||
@ -251,7 +264,7 @@ class ConnectionManager extends EventEmitter {
|
||||
* Get all open connections with a peer.
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Array<Connection>}
|
||||
* @returns {Connection[]}
|
||||
*/
|
||||
getAll (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
|
@ -1,11 +1,12 @@
|
||||
// @ts-nocheck
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
||||
*/
|
||||
|
||||
/* global window */
|
||||
const globalThis = require('ipfs-utils/src/globalthis')
|
||||
/** @typedef {import('../types').EventEmitterFactory} Events */
|
||||
/** @type Events */
|
||||
const EventEmitter = require('events')
|
||||
const VisibilityChangeEmitter = require('./visibility-change-emitter')
|
||||
const debug = require('debug')('latency-monitor:LatencyMonitor')
|
||||
@ -17,6 +18,12 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
|
||||
* @property {number} maxMS What was the max time for a cb to be called
|
||||
* @property {number} avgMs What was the average time for a cb to be called
|
||||
* @property {number} lengthMs How long this interval was in ms
|
||||
*
|
||||
* @typedef {Object} LatencyMonitorOptions
|
||||
* @property {number} [latencyCheckIntervalMs=500] - How often to add a latency check event (ms)
|
||||
* @property {number} [dataEmitIntervalMs=5000] - How often to summarize latency check events. null or 0 disables event firing
|
||||
* @property {Function} [asyncTestFn] - What cb-style async function to use
|
||||
* @property {number} [latencyRandomPercentage=5] - What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -24,6 +31,8 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
|
||||
* the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this.
|
||||
* This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.
|
||||
*
|
||||
* @extends {EventEmitter}
|
||||
*
|
||||
* The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop
|
||||
* and timing how long it takes to get back.
|
||||
*
|
||||
@ -37,11 +46,8 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
|
||||
*/
|
||||
class LatencyMonitor extends EventEmitter {
|
||||
/**
|
||||
* @param {object} [options]
|
||||
* @param {number} [options.latencyCheckIntervalMs=500] - How often to add a latency check event (ms)
|
||||
* @param {number} [options.dataEmitIntervalMs=5000] - How often to summarize latency check events. null or 0 disables event firing
|
||||
* @param {Function} [options.asyncTestFn] - What cb-style async function to use
|
||||
* @param {number} [options.latencyRandomPercentage=5] - What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
|
||||
* @class
|
||||
* @param {LatencyMonitorOptions} [options]
|
||||
*/
|
||||
constructor ({ latencyCheckIntervalMs, dataEmitIntervalMs, asyncTestFn, latencyRandomPercentage } = {}) {
|
||||
super()
|
||||
@ -66,9 +72,9 @@ class LatencyMonitor extends EventEmitter {
|
||||
that.asyncTestFn = asyncTestFn // If there is no asyncFn, we measure latency
|
||||
|
||||
// If process: use high resolution timer
|
||||
if (globalThis.process && globalThis.process.hrtime) {
|
||||
if (globalThis.process && globalThis.process.hrtime) { // eslint-disable-line no-undef
|
||||
debug('Using process.hrtime for timing')
|
||||
that.now = globalThis.process.hrtime
|
||||
that.now = globalThis.process.hrtime // eslint-disable-line no-undef
|
||||
that.getDeltaMS = (startTime) => {
|
||||
const hrtime = that.now(startTime)
|
||||
return (hrtime[0] * 1000) + (hrtime[1] / 1000000)
|
||||
@ -91,6 +97,7 @@ class LatencyMonitor extends EventEmitter {
|
||||
// See: http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs
|
||||
if (isBrowser()) {
|
||||
that._visibilityChangeEmitter = new VisibilityChangeEmitter()
|
||||
|
||||
that._visibilityChangeEmitter.on('visibilityChange', (pageInFocus) => {
|
||||
if (pageInFocus) {
|
||||
that._startTimers()
|
||||
|
@ -1,9 +1,13 @@
|
||||
// @ts-nocheck
|
||||
/* global document */
|
||||
|
||||
/**
|
||||
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
||||
*/
|
||||
'use strict'
|
||||
|
||||
/** @typedef {import('../types').EventEmitterFactory} Events */
|
||||
/** @type Events */
|
||||
const EventEmitter = require('events')
|
||||
|
||||
const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
|
||||
@ -29,12 +33,12 @@ const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
|
||||
* });
|
||||
* // To access the visibility state directly, call:
|
||||
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
|
||||
*
|
||||
* @class VisibilityChangeEmitter
|
||||
*/
|
||||
module.exports = class VisibilityChangeEmitter extends EventEmitter {
|
||||
class VisibilityChangeEmitter extends EventEmitter {
|
||||
/**
|
||||
* Creates a VisibilityChangeEmitter
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
constructor () {
|
||||
super()
|
||||
@ -119,3 +123,5 @@ module.exports = class VisibilityChangeEmitter extends EventEmitter {
|
||||
this.emit('visibilityChange', visible)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VisibilityChangeEmitter
|
||||
|
@ -1,116 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { messages, codes } = require('./errors')
|
||||
|
||||
const all = require('it-all')
|
||||
const pAny = require('p-any')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.contentRouting || []
|
||||
const dht = node._dht
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (dht) {
|
||||
routers.unshift(dht)
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* 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.timeout] - How long the query should run
|
||||
* @param {number} [options.maxNumProviders] - maximum number of providers to find
|
||||
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
||||
*/
|
||||
async * findProviders (key, options) {
|
||||
if (!routers.length) {
|
||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
const result = await pAny(
|
||||
routers.map(async (router) => {
|
||||
const provs = await all(router.findProviders(key, options))
|
||||
|
||||
if (!provs || !provs.length) {
|
||||
throw errCode(new Error('not found'), 'NOT_FOUND')
|
||||
}
|
||||
return provs
|
||||
})
|
||||
)
|
||||
|
||||
for (const peer of result) {
|
||||
yield peer
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async provide (key) { // eslint-disable-line require-await
|
||||
if (!routers.length) {
|
||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
return Promise.all(routers.map((router) => router.provide(key)))
|
||||
},
|
||||
|
||||
/**
|
||||
* Store the given key/value pair in the DHT.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {Uint8Array} value
|
||||
* @param {Object} [options] - put options
|
||||
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async put (key, value, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.put(key, value, options)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the value to the given key.
|
||||
* Times out after 1 minute by default.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<{from: PeerId, val: Uint8Array}>}
|
||||
*/
|
||||
async get (key, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.get(key, options)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the `n` values to the given key without sorting.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {number} nVals
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<Array<{from: PeerId, val: Uint8Array}>>}
|
||||
*/
|
||||
async getMany (key, nVals, options) { // eslint-disable-line require-await
|
||||
if (!node.isStarted() || !dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return dht.getMany(key, nVals, options)
|
||||
}
|
||||
}
|
||||
}
|
135
src/content-routing/index.js
Normal file
135
src/content-routing/index.js
Normal file
@ -0,0 +1,135 @@
|
||||
'use strict'
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { messages, codes } = require('../errors')
|
||||
const {
|
||||
storeAddresses,
|
||||
uniquePeers,
|
||||
requirePeers,
|
||||
maybeLimitSource
|
||||
} = require('./utils')
|
||||
|
||||
const merge = require('it-merge')
|
||||
const { pipe } = require('it-pipe')
|
||||
|
||||
/**
|
||||
* @typedef {import('peer-id')} PeerId
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
* @typedef {import('cids')} CID
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} GetData
|
||||
* @property {PeerId} from
|
||||
* @property {Uint8Array} val
|
||||
*/
|
||||
|
||||
class ContentRouting {
|
||||
/**
|
||||
* @class
|
||||
* @param {import('..')} libp2p
|
||||
*/
|
||||
constructor (libp2p) {
|
||||
this.libp2p = libp2p
|
||||
this.routers = libp2p._modules.contentRouting || []
|
||||
this.dht = libp2p._dht
|
||||
|
||||
// If we have the dht, add it to the available content routers
|
||||
if (this.dht && libp2p._config.dht.enabled) {
|
||||
this.routers.push(this.dht)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over all content routers in parallel to find providers of the given key.
|
||||
*
|
||||
* @param {CID} key - The CID key of the content to find
|
||||
* @param {object} [options]
|
||||
* @param {number} [options.timeout] - How long the query should run
|
||||
* @param {number} [options.maxNumProviders] - maximum number of providers to find
|
||||
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
||||
*/
|
||||
async * findProviders (key, options = {}) {
|
||||
if (!this.routers.length) {
|
||||
throw errCode(new Error('No content this.routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
yield * pipe(
|
||||
merge(
|
||||
...this.routers.map(router => router.findProviders(key, options))
|
||||
),
|
||||
(source) => storeAddresses(source, this.libp2p.peerStore),
|
||||
(source) => uniquePeers(source),
|
||||
(source) => maybeLimitSource(source, options.maxNumProviders),
|
||||
(source) => requirePeers(source)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async provide (key) {
|
||||
if (!this.routers.length) {
|
||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
||||
}
|
||||
|
||||
await Promise.all(this.routers.map((router) => router.provide(key)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the given key/value pair in the DHT.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {Uint8Array} value
|
||||
* @param {Object} [options] - put options
|
||||
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
put (key, value, options) {
|
||||
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return this.dht.put(key, value, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value to the given key.
|
||||
* Times out after 1 minute by default.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<GetData>}
|
||||
*/
|
||||
get (key, options) {
|
||||
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return this.dht.get(key, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the `n` values to the given key without sorting.
|
||||
*
|
||||
* @param {Uint8Array} key
|
||||
* @param {number} nVals
|
||||
* @param {Object} [options] - get options
|
||||
* @param {number} [options.timeout] - optional timeout (default: 60000)
|
||||
* @returns {Promise<GetData[]>}
|
||||
*/
|
||||
async getMany (key, nVals, options) { // eslint-disable-line require-await
|
||||
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
|
||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
||||
}
|
||||
|
||||
return this.dht.getMany(key, nVals, options)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ContentRouting
|
89
src/content-routing/utils.js
Normal file
89
src/content-routing/utils.js
Normal file
@ -0,0 +1,89 @@
|
||||
'use strict'
|
||||
|
||||
const errCode = require('err-code')
|
||||
const filter = require('it-filter')
|
||||
const map = require('it-map')
|
||||
const take = require('it-take')
|
||||
|
||||
/**
|
||||
* @typedef {import('peer-id')} PeerId
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
*/
|
||||
|
||||
/**
|
||||
* Store the multiaddrs from every peer in the passed peer store
|
||||
*
|
||||
* @param {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} source
|
||||
* @param {import('../peer-store')} peerStore
|
||||
*/
|
||||
function storeAddresses (source, peerStore) {
|
||||
return map(source, (peer) => {
|
||||
// ensure we have the addresses for a given peer
|
||||
peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
||||
|
||||
return peer
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter peers by unique peer id
|
||||
*
|
||||
* @param {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} source
|
||||
*/
|
||||
function uniquePeers (source) {
|
||||
/** @type Set<string> */
|
||||
const seen = new Set()
|
||||
|
||||
return filter(source, (peer) => {
|
||||
// dedupe by peer id
|
||||
if (seen.has(peer.id.toString())) {
|
||||
return false
|
||||
}
|
||||
|
||||
seen.add(peer.id.toString())
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Require at least `min` peers to be yielded from `source`
|
||||
*
|
||||
* @param {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} source
|
||||
* @param {number} min
|
||||
*/
|
||||
async function * requirePeers (source, min = 1) {
|
||||
let seen = 0
|
||||
|
||||
for await (const peer of source) {
|
||||
seen++
|
||||
|
||||
yield peer
|
||||
}
|
||||
|
||||
if (seen < min) {
|
||||
throw errCode(new Error('not found'), 'NOT_FOUND')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If `max` is passed, only take that number of peers from the source
|
||||
* otherwise take all the peers
|
||||
*
|
||||
* @param {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} source
|
||||
* @param {number} [max]
|
||||
*/
|
||||
function maybeLimitSource (source, max) {
|
||||
if (max) {
|
||||
return take(source, max)
|
||||
}
|
||||
|
||||
return source
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
storeAddresses,
|
||||
uniquePeers,
|
||||
requirePeers,
|
||||
maybeLimitSource
|
||||
}
|
@ -1,14 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const AbortController = require('abort-controller')
|
||||
const anySignal = require('any-signal')
|
||||
const debug = require('debug')
|
||||
const errCode = require('err-code')
|
||||
const log = debug('libp2p:dialer:request')
|
||||
log.error = debug('libp2p:dialer:request:error')
|
||||
const AbortController = require('abort-controller').default
|
||||
const { anySignal } = require('any-signal')
|
||||
const FIFO = require('p-fifo')
|
||||
const pAny = require('p-any')
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('./')} Dialer
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} DialOptions
|
||||
* @property {AbortSignal} signal
|
||||
*
|
||||
* @typedef {Object} DialRequestOptions
|
||||
* @property {Multiaddr[]} addrs
|
||||
* @property {(m: Multiaddr, options: DialOptions) => Promise<Connection>} dialAction
|
||||
* @property {Dialer} dialer
|
||||
*/
|
||||
|
||||
class DialRequest {
|
||||
/**
|
||||
* Manages running the `dialAction` on multiple provided `addrs` in parallel
|
||||
@ -17,10 +30,8 @@ class DialRequest {
|
||||
* started using `DialRequest.run(options)`. Once a single dial has succeeded,
|
||||
* all other dials in the request will be cancelled.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {Multiaddr[]} options.addrs
|
||||
* @param {function(Multiaddr):Promise<Connection>} options.dialAction
|
||||
* @param {Dialer} options.dialer
|
||||
* @class
|
||||
* @param {DialRequestOptions} options
|
||||
*/
|
||||
constructor ({
|
||||
addrs,
|
||||
@ -34,11 +45,11 @@ class DialRequest {
|
||||
|
||||
/**
|
||||
* @async
|
||||
* @param {object} options
|
||||
* @param {AbortSignal} options.signal - An AbortController signal
|
||||
* @returns {Connection}
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal] - An AbortController signal
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
async run (options) {
|
||||
async run (options = {}) {
|
||||
const tokens = this.dialer.getTokens(this.addrs.length)
|
||||
// If no tokens are available, throw
|
||||
if (tokens.length < 1) {
|
||||
@ -56,7 +67,7 @@ class DialRequest {
|
||||
let conn
|
||||
try {
|
||||
const signal = dialAbortControllers[i].signal
|
||||
conn = await this.dialAction(addr, { ...options, signal: anySignal([signal, options.signal]) })
|
||||
conn = await this.dialAction(addr, { ...options, signal: options.signal ? anySignal([signal, options.signal]) : signal })
|
||||
// Remove the successful AbortController so it is not aborted
|
||||
dialAbortControllers.splice(i, 1)
|
||||
} finally {
|
||||
@ -78,4 +89,4 @@ class DialRequest {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.DialRequest = DialRequest
|
||||
module.exports = DialRequest
|
||||
|
@ -1,14 +1,16 @@
|
||||
'use strict'
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const errCode = require('err-code')
|
||||
const TimeoutController = require('timeout-abort-controller')
|
||||
const anySignal = require('any-signal')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:dialer')
|
||||
log.error = debug('libp2p:dialer:error')
|
||||
const log = Object.assign(debug('libp2p:dialer'), {
|
||||
error: debug('libp2p:dialer:err')
|
||||
})
|
||||
const errCode = require('err-code')
|
||||
const multiaddr = require('multiaddr')
|
||||
const TimeoutController = require('timeout-abort-controller')
|
||||
const { anySignal } = require('any-signal')
|
||||
|
||||
const { DialRequest } = require('./dial-request')
|
||||
const DialRequest = require('./dial-request')
|
||||
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
||||
const getPeer = require('../get-peer')
|
||||
|
||||
const { codes } = require('../errors')
|
||||
@ -18,20 +20,49 @@ const {
|
||||
MAX_PER_PEER_DIALS
|
||||
} = require('../constants')
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
* @typedef {import('peer-id')} PeerId
|
||||
* @typedef {import('../peer-store')} PeerStore
|
||||
* @typedef {import('../peer-store/address-book').Address} Address
|
||||
* @typedef {import('../transport-manager')} TransportManager
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} DialerProperties
|
||||
* @property {PeerStore} peerStore
|
||||
* @property {TransportManager} transportManager
|
||||
*
|
||||
* @typedef {(addr:Multiaddr) => Promise<string[]>} Resolver
|
||||
*
|
||||
* @typedef {Object} DialerOptions
|
||||
* @property {(addresses: Address[]) => Address[]} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
|
||||
* @property {number} [concurrency = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
|
||||
* @property {number} [perPeerLimit = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
|
||||
* @property {number} [timeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
|
||||
* @property {Record<string, Resolver>} [resolvers = {}] - multiaddr resolvers to use when dialing
|
||||
*
|
||||
* @typedef DialTarget
|
||||
* @property {string} id
|
||||
* @property {Multiaddr[]} addrs
|
||||
*
|
||||
* @typedef PendingDial
|
||||
* @property {DialRequest} dialRequest
|
||||
* @property {TimeoutController} controller
|
||||
* @property {Promise} promise
|
||||
* @property {function():void} destroy
|
||||
*/
|
||||
|
||||
class Dialer {
|
||||
/**
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {TransportManager} options.transportManager
|
||||
* @param {Peerstore} options.peerStore
|
||||
* @param {number} [options.concurrency = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
|
||||
* @param {number} [options.perPeerLimit = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
|
||||
* @param {number} [options.timeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
|
||||
* @param {object} [options.resolvers = {}] - multiaddr resolvers to use when dialing
|
||||
* @param {DialerProperties & DialerOptions} options
|
||||
*/
|
||||
constructor ({
|
||||
transportManager,
|
||||
peerStore,
|
||||
addressSorter = publicAddressesFirst,
|
||||
concurrency = MAX_PARALLEL_DIALS,
|
||||
timeout = DIAL_TIMEOUT,
|
||||
perPeerLimit = MAX_PER_PEER_DIALS,
|
||||
@ -39,6 +70,7 @@ class Dialer {
|
||||
}) {
|
||||
this.transportManager = transportManager
|
||||
this.peerStore = peerStore
|
||||
this.addressSorter = addressSorter
|
||||
this.concurrency = concurrency
|
||||
this.timeout = timeout
|
||||
this.perPeerLimit = perPeerLimit
|
||||
@ -98,12 +130,6 @@ class Dialer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef DialTarget
|
||||
* @property {string} id
|
||||
* @property {Multiaddr[]} addrs
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a DialTarget. The DialTarget is used to create and track
|
||||
* the DialRequest to a given peer.
|
||||
@ -120,7 +146,7 @@ class Dialer {
|
||||
this.peerStore.addressBook.add(id, multiaddrs)
|
||||
}
|
||||
|
||||
let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id) || []
|
||||
let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
|
||||
|
||||
// If received a multiaddr to dial, it should be the first to use
|
||||
// But, if we know other multiaddrs for the peer, we should try them too.
|
||||
@ -141,14 +167,6 @@ class Dialer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef PendingDial
|
||||
* @property {DialRequest} dialRequest
|
||||
* @property {TimeoutController} controller
|
||||
* @property {Promise} promise
|
||||
* @property {function():void} destroy
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a PendingDial that wraps the underlying DialRequest
|
||||
*
|
||||
@ -158,7 +176,7 @@ class Dialer {
|
||||
* @param {AbortSignal} [options.signal] - An AbortController signal
|
||||
* @returns {PendingDial}
|
||||
*/
|
||||
_createPendingDial (dialTarget, options) {
|
||||
_createPendingDial (dialTarget, options = {}) {
|
||||
const dialAction = (addr, options) => {
|
||||
if (options.signal.aborted) throw errCode(new Error('already aborted'), codes.ERR_ALREADY_ABORTED)
|
||||
return this.transportManager.dial(addr, options)
|
||||
@ -207,7 +225,7 @@ class Dialer {
|
||||
* Resolve multiaddr recursively.
|
||||
*
|
||||
* @param {Multiaddr} ma
|
||||
* @returns {Promise<Array<Multiaddr>>}
|
||||
* @returns {Promise<Multiaddr[]>}
|
||||
*/
|
||||
async _resolve (ma) {
|
||||
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
|
||||
@ -224,19 +242,20 @@ class Dialer {
|
||||
return this._resolve(nm)
|
||||
}))
|
||||
|
||||
return recursiveMultiaddrs.flat().reduce((array, newM) => {
|
||||
const addrs = recursiveMultiaddrs.flat()
|
||||
return addrs.reduce((array, newM) => {
|
||||
if (!array.find(m => m.equals(newM))) {
|
||||
array.push(newM)
|
||||
}
|
||||
return array
|
||||
}, []) // Unique addresses
|
||||
}, /** @type {Multiaddr[]} */([]))
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a given multiaddr. If this fails, an empty array will be returned
|
||||
*
|
||||
* @param {Multiaddr} ma
|
||||
* @returns {Promise<Array<Multiaddr>>}
|
||||
* @returns {Promise<Multiaddr[]>}
|
||||
*/
|
||||
async _resolveRecord (ma) {
|
||||
try {
|
||||
|
@ -16,6 +16,7 @@ exports.codes = {
|
||||
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
||||
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
|
||||
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
|
||||
ERR_DIALED_SELF: 'ERR_DIALED_SELF',
|
||||
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
|
||||
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
||||
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
||||
|
@ -6,12 +6,16 @@ const errCode = require('err-code')
|
||||
|
||||
const { codes } = require('./errors')
|
||||
|
||||
/**
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts the given `peer` to a `Peer` object.
|
||||
* If a multiaddr is received, the addressBook is updated.
|
||||
*
|
||||
* @param {PeerId|Multiaddr|string} peer
|
||||
* @returns {{ id: PeerId, multiaddrs: Array<Multiaddr> }}
|
||||
* @returns {{ id: PeerId, multiaddrs: Multiaddr[]|undefined }}
|
||||
*/
|
||||
function getPeer (peer) {
|
||||
if (typeof peer === 'string') {
|
||||
|
@ -1,5 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
// @ts-ignore file not listed within the file list of projects
|
||||
const libp2pVersion = require('../../package.json').version
|
||||
|
||||
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
|
||||
|
@ -1,13 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:identify')
|
||||
log.error = debug('libp2p:identify:error')
|
||||
|
||||
const log = Object.assign(debug('libp2p:identify'), {
|
||||
error: debug('libp2p:identify:err')
|
||||
})
|
||||
const errCode = require('err-code')
|
||||
const pb = require('it-protocol-buffers')
|
||||
const lp = require('it-length-prefixed')
|
||||
const pipe = require('it-pipe')
|
||||
const { pipe } = require('it-pipe')
|
||||
const { collect, take, consume } = require('streaming-iterables')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
@ -29,72 +29,65 @@ const {
|
||||
|
||||
const { codes } = require('../errors')
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
*/
|
||||
|
||||
class IdentifyService {
|
||||
/**
|
||||
* Takes the `addr` and converts it to a Multiaddr if possible
|
||||
*
|
||||
* @param {Uint8Array | string} addr
|
||||
* @returns {Multiaddr|null}
|
||||
*/
|
||||
static getCleanMultiaddr (addr) {
|
||||
if (addr && addr.length > 0) {
|
||||
try {
|
||||
return multiaddr(addr)
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {Libp2p} options.libp2p
|
||||
* @param {Map<string, handler>} options.protocols - A reference to the protocols we support
|
||||
* @param {Object} options
|
||||
* @param {import('../')} options.libp2p
|
||||
*/
|
||||
constructor ({ libp2p, protocols }) {
|
||||
/**
|
||||
* @property {PeerStore}
|
||||
*/
|
||||
constructor ({ libp2p }) {
|
||||
this._libp2p = libp2p
|
||||
this.peerStore = libp2p.peerStore
|
||||
|
||||
/**
|
||||
* @property {ConnectionManager}
|
||||
*/
|
||||
this.addressManager = libp2p.addressManager
|
||||
this.connectionManager = libp2p.connectionManager
|
||||
|
||||
this.connectionManager.on('peer:connect', (connection) => {
|
||||
const peerId = connection.remotePeer
|
||||
|
||||
this.identify(connection, peerId).catch(log.error)
|
||||
})
|
||||
|
||||
/**
|
||||
* @property {PeerId}
|
||||
*/
|
||||
this.peerId = libp2p.peerId
|
||||
|
||||
/**
|
||||
* @property {AddressManager}
|
||||
*/
|
||||
this._libp2p = libp2p
|
||||
|
||||
this._protocols = protocols
|
||||
|
||||
this.handleMessage = this.handleMessage.bind(this)
|
||||
|
||||
// Store self host metadata
|
||||
this._host = {
|
||||
agentVersion: AGENT_VERSION,
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
...libp2p._options.host
|
||||
}
|
||||
|
||||
this.peerStore.metadataBook.set(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
|
||||
this.peerStore.metadataBook.set(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
|
||||
// When a new connection happens, trigger identify
|
||||
this.connectionManager.on('peer:connect', (connection) => {
|
||||
this.identify(connection).catch(log.error)
|
||||
})
|
||||
|
||||
// When self multiaddrs change, trigger identify-push
|
||||
this.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
if (peerId.toString() === this.peerId.toString()) {
|
||||
this.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
|
||||
// When self protocols change, trigger identify-push
|
||||
this.peerStore.on('change:protocols', ({ peerId }) => {
|
||||
if (peerId.toString() === this.peerId.toString()) {
|
||||
this.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an Identify Push update to the list of connections
|
||||
*
|
||||
* @param {Array<Connection>} connections
|
||||
* @returns {Promise<void>}
|
||||
* @param {Connection[]} connections
|
||||
* @returns {Promise<void[]>}
|
||||
*/
|
||||
async push (connections) {
|
||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
|
||||
const protocols = Array.from(this._protocols.keys())
|
||||
const protocols = this.peerStore.protoBook.get(this.peerId) || []
|
||||
|
||||
const pushes = connections.map(async connection => {
|
||||
try {
|
||||
@ -122,12 +115,17 @@ class IdentifyService {
|
||||
/**
|
||||
* Calls `push` for all peers in the `peerStore` that are connected
|
||||
*
|
||||
* @param {PeerStore} peerStore
|
||||
* @returns {void}
|
||||
*/
|
||||
pushToPeerStore (peerStore) {
|
||||
pushToPeerStore () {
|
||||
// Do not try to push if libp2p node is not running
|
||||
if (!this._libp2p.isStarted()) {
|
||||
return
|
||||
}
|
||||
|
||||
const connections = []
|
||||
let connection
|
||||
for (const peer of peerStore.peers.values()) {
|
||||
for (const peer of this.peerStore.peers.values()) {
|
||||
if (peer.protocols.includes(MULTICODEC_IDENTIFY_PUSH) && (connection = this.connectionManager.get(peer.id))) {
|
||||
connections.push(connection)
|
||||
}
|
||||
@ -204,18 +202,19 @@ class IdentifyService {
|
||||
this.peerStore.protoBook.set(id, protocols)
|
||||
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
|
||||
|
||||
// TODO: Track our observed address so that we can score it
|
||||
// TODO: Add and score our observed addr
|
||||
log('received observed address of %s', observedAddr)
|
||||
// this.addressManager.addObservedAddr(observedAddr)
|
||||
}
|
||||
|
||||
/**
|
||||
* A handler to register with Libp2p to process identify messages.
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {string} options.protocol
|
||||
* @param {*} options.stream
|
||||
* @param {Object} options
|
||||
* @param {Connection} options.connection
|
||||
* @returns {Promise<void>}
|
||||
* @param {MuxedStream} options.stream
|
||||
* @param {string} options.protocol
|
||||
* @returns {Promise<void>|undefined}
|
||||
*/
|
||||
handleMessage ({ connection, stream, protocol }) {
|
||||
switch (protocol) {
|
||||
@ -233,9 +232,10 @@ class IdentifyService {
|
||||
* to the requesting peer over the given `connection`
|
||||
*
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {*} options.stream
|
||||
* @param {Object} options
|
||||
* @param {MuxedStream} options.stream
|
||||
* @param {Connection} options.connection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _handleIdentify ({ connection, stream }) {
|
||||
let publicKey = new Uint8Array(0)
|
||||
@ -243,16 +243,17 @@ class IdentifyService {
|
||||
publicKey = this.peerId.pubKey.bytes
|
||||
}
|
||||
|
||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
const protocols = this.peerStore.protoBook.get(this.peerId) || []
|
||||
|
||||
const message = Message.encode({
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
agentVersion: AGENT_VERSION,
|
||||
protocolVersion: this._host.protocolVersion,
|
||||
agentVersion: this._host.agentVersion,
|
||||
publicKey,
|
||||
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
|
||||
signedPeerRecord,
|
||||
observedAddr: connection.remoteAddr.bytes,
|
||||
protocols: Array.from(this._protocols.keys())
|
||||
protocols
|
||||
})
|
||||
|
||||
try {
|
||||
@ -272,8 +273,9 @@ class IdentifyService {
|
||||
*
|
||||
* @private
|
||||
* @param {object} options
|
||||
* @param {*} options.stream
|
||||
* @param {MuxedStream} options.stream
|
||||
* @param {Connection} options.connection
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _handlePush ({ connection, stream }) {
|
||||
let message
|
||||
@ -315,42 +317,34 @@ class IdentifyService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get self signed peer record raw envelope.
|
||||
* Takes the `addr` and converts it to a Multiaddr if possible
|
||||
*
|
||||
* @returns {Uint8Array}
|
||||
* @param {Uint8Array | string} addr
|
||||
* @returns {multiaddr|null}
|
||||
*/
|
||||
async _getSelfPeerRecord () {
|
||||
const selfSignedPeerRecord = this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
|
||||
// TODO: support invalidation when dynamic multiaddrs are supported
|
||||
if (selfSignedPeerRecord) {
|
||||
return selfSignedPeerRecord
|
||||
}
|
||||
|
||||
try {
|
||||
const peerRecord = new PeerRecord({
|
||||
peerId: this.peerId,
|
||||
multiaddrs: this._libp2p.multiaddrs
|
||||
})
|
||||
const envelope = await Envelope.seal(peerRecord, this.peerId)
|
||||
this.peerStore.addressBook.consumePeerRecord(envelope)
|
||||
|
||||
return this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
} catch (err) {
|
||||
log.error('failed to get self peer record')
|
||||
static getCleanMultiaddr (addr) {
|
||||
if (addr && addr.length > 0) {
|
||||
try {
|
||||
return multiaddr(addr)
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.IdentifyService = IdentifyService
|
||||
/**
|
||||
* The protocols the IdentifyService supports
|
||||
*
|
||||
* @property multicodecs
|
||||
*/
|
||||
module.exports.multicodecs = {
|
||||
const multicodecs = {
|
||||
IDENTIFY: MULTICODEC_IDENTIFY,
|
||||
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
|
||||
}
|
||||
module.exports.Message = Message
|
||||
|
||||
IdentifyService.multicodecs = multicodecs
|
||||
IdentifyService.Messsage = Message
|
||||
|
||||
module.exports = IdentifyService
|
||||
|
266
src/index.js
266
src/index.js
@ -1,23 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const debug = require('debug')
|
||||
const globalThis = require('ipfs-utils/src/globalthis')
|
||||
const log = debug('libp2p')
|
||||
log.error = debug('libp2p:error')
|
||||
const log = Object.assign(debug('libp2p'), {
|
||||
error: debug('libp2p:err')
|
||||
})
|
||||
/** @typedef {import('./types').EventEmitterFactory} Events */
|
||||
/** @type Events */
|
||||
const EventEmitter = require('events')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const peerRouting = require('./peer-routing')
|
||||
const contentRouting = require('./content-routing')
|
||||
const PeerRouting = require('./peer-routing')
|
||||
const ContentRouting = require('./content-routing')
|
||||
const getPeer = require('./get-peer')
|
||||
const { validate: validateConfig } = require('./config')
|
||||
const { codes, messages } = require('./errors')
|
||||
|
||||
const AddressManager = require('./address-manager')
|
||||
const ConnectionManager = require('./connection-manager')
|
||||
const Circuit = require('./circuit')
|
||||
const Circuit = require('./circuit/transport')
|
||||
const Relay = require('./circuit')
|
||||
const Dialer = require('./dialer')
|
||||
const Keychain = require('./keychain')
|
||||
const Metrics = require('./metrics')
|
||||
@ -28,22 +32,98 @@ const PubsubAdapter = require('./pubsub-adapter')
|
||||
const PersistentPeerStore = require('./peer-store/persistent')
|
||||
const Registrar = require('./registrar')
|
||||
const ping = require('./ping')
|
||||
const {
|
||||
IdentifyService,
|
||||
multicodecs: IDENTIFY_PROTOCOLS
|
||||
} = require('./identify')
|
||||
const IdentifyService = require('./identify')
|
||||
const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
|
||||
const NatManager = require('./nat-manager')
|
||||
const { updateSelfPeerRecord } = require('./record/utils')
|
||||
|
||||
/**
|
||||
* @typedef {import('multiaddr')} Multiaddr
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
||||
* @typedef {import('libp2p-interfaces/src/transport/types').TransportFactory} TransportFactory
|
||||
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory
|
||||
* @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
|
||||
* @typedef {import('libp2p-interfaces/src/pubsub')} Pubsub
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} PeerStoreOptions
|
||||
* @property {boolean} persistence
|
||||
*
|
||||
* @typedef {Object} RelayOptions
|
||||
* @property {boolean} enabled
|
||||
* @property {import('./circuit').RelayAdvertiseOptions} advertise
|
||||
* @property {import('./circuit').HopOptions} hop
|
||||
* @property {import('./circuit').AutoRelayOptions} autoRelay
|
||||
*
|
||||
* @typedef {Object} Libp2pConfig
|
||||
* @property {Object} [dht] dht module options
|
||||
* @property {Object} [peerDiscovery]
|
||||
* @property {Pubsub} [pubsub] pubsub module options
|
||||
* @property {RelayOptions} [relay]
|
||||
* @property {Record<string, Object>} [transport] transport options indexed by transport key
|
||||
*
|
||||
* @typedef {Object} Libp2pModules
|
||||
* @property {TransportFactory[]} transport
|
||||
* @property {MuxerFactory[]} streamMuxer
|
||||
* @property {Crypto[]} connEncryption
|
||||
*
|
||||
* @typedef {Object} Libp2pOptions
|
||||
* @property {Libp2pModules} modules libp2p modules to use
|
||||
* @property {import('./address-manager').AddressManagerOptions} [addresses]
|
||||
* @property {import('./connection-manager').ConnectionManagerOptions} [connectionManager]
|
||||
* @property {import('./dialer').DialerOptions} [dialer]
|
||||
* @property {import('./metrics').MetricsOptions} [metrics]
|
||||
* @property {Object} [keychain]
|
||||
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
|
||||
* @property {PeerStoreOptions & import('./peer-store/persistent').PersistentPeerStoreOptions} [peerStore]
|
||||
* @property {Libp2pConfig} [config]
|
||||
*
|
||||
* @typedef {Object} constructorOptions
|
||||
* @property {PeerId} peerId
|
||||
*
|
||||
* @typedef {Object} CreateOptions
|
||||
* @property {PeerId} [peerId]
|
||||
*
|
||||
* @extends {EventEmitter}
|
||||
* @fires Libp2p#error Emitted when an error occurs
|
||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
||||
*/
|
||||
class Libp2p extends EventEmitter {
|
||||
/**
|
||||
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||
* instance if one is not provided in options.
|
||||
*
|
||||
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
||||
* @returns {Promise<Libp2p>}
|
||||
*/
|
||||
static async create (options) {
|
||||
if (options.peerId) {
|
||||
// @ts-ignore 'Libp2pOptions & CreateOptions' is not assignable to 'Libp2pOptions & constructorOptions'
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
options.peerId = peerId
|
||||
// @ts-ignore 'Libp2pOptions & CreateOptions' is not assignable to 'Libp2pOptions & constructorOptions'
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Libp2p node.
|
||||
*
|
||||
* @class
|
||||
* @param {Libp2pOptions & constructorOptions} _options
|
||||
*/
|
||||
constructor (_options) {
|
||||
super()
|
||||
// validateConfig will ensure the config is correct,
|
||||
// and add default values where appropriate
|
||||
this._options = validateConfig(_options)
|
||||
|
||||
/** @type {PeerId} */
|
||||
this.peerId = this._options.peerId
|
||||
this.datastore = this._options.datastore
|
||||
|
||||
@ -57,7 +137,14 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
// Addresses {listen, announce, noAnnounce}
|
||||
this.addresses = this._options.addresses
|
||||
this.addressManager = new AddressManager(this._options.addresses)
|
||||
this.addressManager = new AddressManager(this.peerId, this._options.addresses)
|
||||
|
||||
// when addresses change, update our peer record
|
||||
this.addressManager.on('change:addresses', () => {
|
||||
updateSelfPeerRecord(this).catch(err => {
|
||||
log.error('Error updating self peer record', err)
|
||||
})
|
||||
})
|
||||
|
||||
this._modules = this._options.modules
|
||||
this._config = this._options.config
|
||||
@ -111,6 +198,14 @@ class Libp2p extends EventEmitter {
|
||||
faultTolerance: this._options.transportManager.faultTolerance
|
||||
})
|
||||
|
||||
// Create the Nat Manager
|
||||
this.natManager = new NatManager({
|
||||
peerId: this.peerId,
|
||||
addressManager: this.addressManager,
|
||||
transportManager: this.transportManager,
|
||||
...this._options.config.nat
|
||||
})
|
||||
|
||||
// Create the Registrar
|
||||
this.registrar = new Registrar({
|
||||
peerStore: this.peerStore,
|
||||
@ -135,7 +230,8 @@ class Libp2p extends EventEmitter {
|
||||
concurrency: this._options.dialer.maxParallelDials,
|
||||
perPeerLimit: this._options.dialer.maxDialsPerPeer,
|
||||
timeout: this._options.dialer.dialTimeout,
|
||||
resolvers: this._options.dialer.resolvers
|
||||
resolvers: this._options.dialer.resolvers,
|
||||
addressSorter: this._options.dialer.addressSorter
|
||||
})
|
||||
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
@ -145,7 +241,9 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
|
||||
if (this._config.relay.enabled) {
|
||||
// @ts-ignore Circuit prototype
|
||||
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
|
||||
this.relay = new Relay(this)
|
||||
}
|
||||
|
||||
// Attach stream multiplexers
|
||||
@ -156,17 +254,14 @@ class Libp2p extends EventEmitter {
|
||||
})
|
||||
|
||||
// Add the identify service since we can multiplex
|
||||
this.identifyService = new IdentifyService({
|
||||
libp2p: this,
|
||||
protocols: this.upgrader.protocols
|
||||
})
|
||||
this.identifyService = new IdentifyService({ libp2p: this })
|
||||
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
|
||||
}
|
||||
|
||||
// Attach private network protector
|
||||
if (this._modules.connProtector) {
|
||||
this.upgrader.protector = this._modules.connProtector
|
||||
} else if (globalThis.process !== undefined && globalThis.process.env && globalThis.process.env.LIBP2P_FORCE_PNET) {
|
||||
} else if (globalThis.process !== undefined && globalThis.process.env && globalThis.process.env.LIBP2P_FORCE_PNET) { // eslint-disable-line no-undef
|
||||
throw new Error('Private network is enforced, but no protector was provided')
|
||||
}
|
||||
|
||||
@ -188,13 +283,14 @@ class Libp2p extends EventEmitter {
|
||||
if (this._modules.pubsub) {
|
||||
const Pubsub = this._modules.pubsub
|
||||
// using pubsub adapter with *DEPRECATED* handlers functionality
|
||||
/** @type {Pubsub} */
|
||||
this.pubsub = PubsubAdapter(Pubsub, this, 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.peerRouting = new PeerRouting(this)
|
||||
this.contentRouting = new ContentRouting(this)
|
||||
|
||||
// Mount default protocols
|
||||
ping.mount(this)
|
||||
@ -208,13 +304,16 @@ class Libp2p extends EventEmitter {
|
||||
*
|
||||
* @param {string} eventName
|
||||
* @param {...any} args
|
||||
* @returns {void}
|
||||
* @returns {boolean}
|
||||
*/
|
||||
emit (eventName, ...args) {
|
||||
// TODO: do we still need this?
|
||||
// @ts-ignore _events does not exist in libp2p
|
||||
if (eventName === 'error' && !this._events.error) {
|
||||
log.error(...args)
|
||||
log.error(args)
|
||||
return false
|
||||
} else {
|
||||
super.emit(eventName, ...args)
|
||||
return super.emit(eventName, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,12 +341,17 @@ class Libp2p extends EventEmitter {
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*
|
||||
* @async
|
||||
* @returns {void}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async stop () {
|
||||
log('libp2p is stopping')
|
||||
|
||||
try {
|
||||
this._isStarted = false
|
||||
|
||||
this.relay && this.relay.stop()
|
||||
this.peerRouting.stop()
|
||||
|
||||
for (const service of this._discovery.values()) {
|
||||
service.removeListener('peer', this._onDiscoveryPeer)
|
||||
}
|
||||
@ -265,6 +369,7 @@ class Libp2p extends EventEmitter {
|
||||
this.metrics && this.metrics.stop()
|
||||
])
|
||||
|
||||
await this.natManager.stop()
|
||||
await this.transportManager.close()
|
||||
|
||||
ping.unmount(this)
|
||||
@ -275,7 +380,6 @@ class Libp2p extends EventEmitter {
|
||||
this.emit('error', err)
|
||||
}
|
||||
}
|
||||
this._isStarted = false
|
||||
log('libp2p has stopped')
|
||||
}
|
||||
|
||||
@ -284,9 +388,13 @@ class Libp2p extends EventEmitter {
|
||||
* Imports the private key as 'self', if needed.
|
||||
*
|
||||
* @async
|
||||
* @returns {void}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async loadKeychain () {
|
||||
if (!this.keychain) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await this.keychain.findKeyByName('self')
|
||||
} catch (err) {
|
||||
@ -313,12 +421,12 @@ class Libp2p extends EventEmitter {
|
||||
* peer will be added to the nodes `peerStore`
|
||||
*
|
||||
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
||||
* @param {object} options
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal]
|
||||
* @returns {Promise<Connection>}
|
||||
*/
|
||||
dial (peer, options) {
|
||||
return this.dialProtocol(peer, null, options)
|
||||
return this.dialProtocol(peer, [], options)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -329,12 +437,17 @@ class Libp2p extends EventEmitter {
|
||||
* @async
|
||||
* @param {PeerId|Multiaddr|string} peer - The peer to dial
|
||||
* @param {string[]|string} protocols
|
||||
* @param {object} options
|
||||
* @param {object} [options]
|
||||
* @param {AbortSignal} [options.signal]
|
||||
* @returns {Promise<Connection|*>}
|
||||
*/
|
||||
async dialProtocol (peer, protocols, options) {
|
||||
const { id, multiaddrs } = getPeer(peer)
|
||||
|
||||
if (id.equals(this.peerId)) {
|
||||
throw errCode(new Error('Cannot dial self'), codes.ERR_DIALED_SELF)
|
||||
}
|
||||
|
||||
let connection = this.connectionManager.get(id)
|
||||
|
||||
if (!connection) {
|
||||
@ -344,7 +457,7 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
|
||||
// If a protocol was provided, create a new stream
|
||||
if (protocols) {
|
||||
if (protocols && protocols.length) {
|
||||
return connection.newStream(protocols)
|
||||
}
|
||||
|
||||
@ -352,38 +465,38 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get peer advertising multiaddrs by concating the addresses used
|
||||
* by transports to listen with the announce addresses.
|
||||
* Duplicated addresses and noAnnounce addresses are filtered out.
|
||||
* Get a deduplicated list of peer advertising multiaddrs by concatenating
|
||||
* the listen addresses used by transports with any configured
|
||||
* announce addresses as well as observed addresses reported by peers.
|
||||
*
|
||||
* @returns {Array<Multiaddr>}
|
||||
* If Announce addrs are specified, configured listen addresses will be
|
||||
* ignored though observed addresses will still be included.
|
||||
*
|
||||
* @returns {Multiaddr[]}
|
||||
*/
|
||||
get multiaddrs () {
|
||||
// Filter noAnnounce multiaddrs
|
||||
const filterMa = this.addressManager.getNoAnnounceAddrs()
|
||||
let addrs = this.addressManager.getAnnounceAddrs().map(ma => ma.toString())
|
||||
|
||||
if (!addrs.length) {
|
||||
// no configured announce addrs, add configured listen addresses
|
||||
addrs = this.transportManager.getAddrs().map(ma => ma.toString())
|
||||
}
|
||||
|
||||
addrs = addrs.concat(this.addressManager.getObservedAddrs().map(ma => ma.toString()))
|
||||
|
||||
const announceFilter = this._options.addresses.announceFilter || ((multiaddrs) => multiaddrs)
|
||||
|
||||
// dedupe multiaddrs
|
||||
const addrSet = new Set(addrs)
|
||||
|
||||
// Create advertising list
|
||||
return this.transportManager.getAddrs()
|
||||
.concat(this.addressManager.getAnnounceAddrs())
|
||||
.filter((ma, index, array) => {
|
||||
// Filter out if repeated
|
||||
if (array.findIndex((otherMa) => otherMa.equals(ma)) !== index) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Filter out if in noAnnounceMultiaddrs
|
||||
if (filterMa.find((fm) => fm.equals(ma))) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
return announceFilter(Array.from(addrSet).map(str => multiaddr(str)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects all connections to the given `peer`
|
||||
*
|
||||
* @param {PeerId|multiaddr|string} peer - the peer to close connections to
|
||||
* @param {PeerId|Multiaddr|string} peer - the peer to close connections to
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async hangUp (peer) {
|
||||
@ -423,7 +536,7 @@ class Libp2p extends EventEmitter {
|
||||
* Registers the `handler` for each protocol
|
||||
*
|
||||
* @param {string[]|string} protocols
|
||||
* @param {function({ connection:*, stream:*, protocol:string })} handler
|
||||
* @param {({ connection: Connection, stream: MuxedStream, protocol: string }) => void} handler
|
||||
*/
|
||||
handle (protocols, handler) {
|
||||
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
||||
@ -431,10 +544,8 @@ class Libp2p extends EventEmitter {
|
||||
this.upgrader.protocols.set(protocol, handler)
|
||||
})
|
||||
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted() && this.identifyService) {
|
||||
this.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
// Add new protocols to self protocols in the Protobook
|
||||
this.peerStore.protoBook.add(this.peerId, protocols)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -449,15 +560,17 @@ class Libp2p extends EventEmitter {
|
||||
this.upgrader.protocols.delete(protocol)
|
||||
})
|
||||
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted() && this.identifyService) {
|
||||
this.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
// Remove protocols from self protocols in the Protobook
|
||||
this.peerStore.protoBook.remove(this.peerId, protocols)
|
||||
}
|
||||
|
||||
async _onStarting () {
|
||||
// Listen on the provided transports
|
||||
await this.transportManager.listen()
|
||||
// Listen on the provided transports for the provided addresses
|
||||
const addrs = this.addressManager.getListenAddrs()
|
||||
await this.transportManager.listen(addrs)
|
||||
|
||||
// Manage your NATs
|
||||
this.natManager.start()
|
||||
|
||||
// Start PeerStore
|
||||
await this.peerStore.start()
|
||||
@ -502,6 +615,11 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
// Peer discovery
|
||||
await this._setupPeerDiscovery()
|
||||
|
||||
// Relay
|
||||
this.relay && this.relay.start()
|
||||
|
||||
this.peerRouting.start()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -509,7 +627,7 @@ class Libp2p extends EventEmitter {
|
||||
* Known peers may be emitted.
|
||||
*
|
||||
* @private
|
||||
* @param {{ id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string> }} peer
|
||||
* @param {{ id: PeerId, multiaddrs: Multiaddr[], protocols: string[] }} peer
|
||||
*/
|
||||
_onDiscoveryPeer (peer) {
|
||||
if (peer.id.toB58String() === this.peerId.toB58String()) {
|
||||
@ -587,7 +705,9 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
// Transport modules with discovery
|
||||
for (const Transport of this.transportManager.getTransports()) {
|
||||
// @ts-ignore Transport interface does not include discovery
|
||||
if (Transport.discovery) {
|
||||
// @ts-ignore Transport interface does not include discovery
|
||||
setupService(Transport.discovery)
|
||||
}
|
||||
}
|
||||
@ -596,22 +716,4 @@ class Libp2p extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||
* instance if one is not provided in options.
|
||||
*
|
||||
* @param {object} options - Libp2p configuration options
|
||||
* @returns {Libp2p}
|
||||
*/
|
||||
Libp2p.create = async function create (options = {}) {
|
||||
if (options.peerId) {
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
const peerId = await PeerId.create()
|
||||
|
||||
options.peerId = peerId
|
||||
return new Libp2p(options)
|
||||
}
|
||||
|
||||
module.exports = Libp2p
|
||||
|
@ -1,21 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = Object.assign(debug('libp2p:plaintext'), {
|
||||
error: debug('libp2p:plaintext:err')
|
||||
})
|
||||
const handshake = require('it-handshake')
|
||||
const lp = require('it-length-prefixed')
|
||||
const PeerId = require('peer-id')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:plaintext')
|
||||
log.error = debug('libp2p:plaintext:error')
|
||||
const { UnexpectedPeerError, InvalidCryptoExchangeError } = require('libp2p-interfaces/src/crypto/errors')
|
||||
|
||||
const { Exchange, KeyType } = require('./proto')
|
||||
const protocol = '/plaintext/2.0.0'
|
||||
|
||||
/**
|
||||
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
||||
*/
|
||||
|
||||
function lpEncodeExchange (exchange) {
|
||||
const pb = Exchange.encode(exchange)
|
||||
return lp.encode.single(pb)
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt connection.
|
||||
*
|
||||
* @param {PeerId} localId
|
||||
* @param {Connection} conn
|
||||
* @param {PeerId} [remoteId]
|
||||
*/
|
||||
async function encrypt (localId, conn, remoteId) {
|
||||
const shake = handshake(conn)
|
||||
|
||||
|
@ -8,6 +8,8 @@ const errcode = require('err-code')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
|
||||
const privates = new WeakMap()
|
||||
|
||||
/**
|
||||
* Cryptographic Message Syntax (aka PKCS #7)
|
||||
*
|
||||
@ -21,14 +23,16 @@ class CMS {
|
||||
/**
|
||||
* Creates a new instance with a keychain
|
||||
*
|
||||
* @param {Keychain} keychain - the available keys
|
||||
* @param {import('./index')} keychain - the available keys
|
||||
* @param {string} dek
|
||||
*/
|
||||
constructor (keychain) {
|
||||
constructor (keychain, dek) {
|
||||
if (!keychain) {
|
||||
throw errcode(new Error('keychain is required'), 'ERR_KEYCHAIN_REQUIRED')
|
||||
}
|
||||
|
||||
this.keychain = keychain
|
||||
privates.set(this, { dek })
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,7 +42,7 @@ class CMS {
|
||||
*
|
||||
* @param {string} name - The local key name.
|
||||
* @param {Uint8Array} plain - The data to encrypt.
|
||||
* @returns {undefined}
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
async encrypt (name, plain) {
|
||||
if (!(plain instanceof Uint8Array)) {
|
||||
@ -47,7 +51,9 @@ class CMS {
|
||||
|
||||
const key = await this.keychain.findKeyByName(name)
|
||||
const pem = await this.keychain._getPrivateKey(name)
|
||||
const privateKey = forge.pki.decryptRsaPrivateKey(pem, this.keychain._())
|
||||
/** @type {string} */
|
||||
const dek = privates.get(this).dek
|
||||
const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
|
||||
const certificate = await certificateForKey(key, privateKey)
|
||||
|
||||
// create a p7 enveloped message
|
||||
@ -68,7 +74,7 @@ class CMS {
|
||||
* exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
|
||||
*
|
||||
* @param {Uint8Array} cmsData - The CMS encrypted data to decrypt.
|
||||
* @returns {undefined}
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
async decrypt (cmsData) {
|
||||
if (!(cmsData instanceof Uint8Array)) {
|
||||
@ -114,8 +120,14 @@ class CMS {
|
||||
}
|
||||
|
||||
const key = await this.keychain.findKeyById(r.keyId)
|
||||
|
||||
if (!key) {
|
||||
throw errcode(new Error('No key available to decrypto'), 'ERR_NO_KEY')
|
||||
}
|
||||
|
||||
const pem = await this.keychain._getPrivateKey(key.name)
|
||||
const privateKey = forge.pki.decryptRsaPrivateKey(pem, this.keychain._())
|
||||
const dek = privates.get(this).dek
|
||||
const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
|
||||
cms.decrypt(r.recipient, privateKey)
|
||||
return uint8ArrayFromString(cms.content.getBytes(), 'ascii')
|
||||
}
|
||||
|
@ -4,17 +4,22 @@
|
||||
const sanitize = require('sanitize-filename')
|
||||
const mergeOptions = require('merge-options')
|
||||
const crypto = require('libp2p-crypto')
|
||||
const DS = require('interface-datastore')
|
||||
const { Key } = require('interface-datastore')
|
||||
const CMS = require('./cms')
|
||||
const errcode = require('err-code')
|
||||
const { Number } = require('ipfs-utils/src/globalthis')
|
||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
require('node-forge/lib/sha512')
|
||||
|
||||
/**
|
||||
* @typedef {import('peer-id')} PeerId
|
||||
* @typedef {import('interface-datastore').Datastore} Datastore
|
||||
*/
|
||||
|
||||
const keyPrefix = '/pkcs8/'
|
||||
const infoPrefix = '/info/'
|
||||
const privates = new WeakMap()
|
||||
|
||||
// NIST SP 800-132
|
||||
const NIST = {
|
||||
@ -45,7 +50,8 @@ function validateKeyName (name) {
|
||||
* This assumes than an error indicates that the keychain is under attack. Delay returning an
|
||||
* error to make brute force attacks harder.
|
||||
*
|
||||
* @param {string | Error} err - The error
|
||||
* @param {string|Error} err - The error
|
||||
* @returns {Promise<never>}
|
||||
* @private
|
||||
*/
|
||||
async function throwDelayed (err) {
|
||||
@ -61,29 +67,28 @@ async function throwDelayed (err) {
|
||||
* Converts a key name into a datastore name.
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {DS.Key}
|
||||
* @returns {Key}
|
||||
* @private
|
||||
*/
|
||||
function DsName (name) {
|
||||
return new DS.Key(keyPrefix + name)
|
||||
return new Key(keyPrefix + name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a key name into a datastore info name.
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {DS.Key}
|
||||
* @returns {Key}
|
||||
* @private
|
||||
*/
|
||||
function DsInfoName (name) {
|
||||
return new DS.Key(infoPrefix + name)
|
||||
return new Key(infoPrefix + name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Information about a key.
|
||||
*
|
||||
* @typedef {Object} KeyInfo
|
||||
*
|
||||
* @property {string} id - The universally unique key id.
|
||||
* @property {string} name - The local key name.
|
||||
*/
|
||||
@ -100,8 +105,9 @@ class Keychain {
|
||||
/**
|
||||
* Creates a new instance of a key chain.
|
||||
*
|
||||
* @param {DS} store - where the key are.
|
||||
* @param {object} options - ???
|
||||
* @param {Datastore} store - where the key are.
|
||||
* @param {object} options
|
||||
* @class
|
||||
*/
|
||||
constructor (store, options) {
|
||||
if (!store) {
|
||||
@ -132,7 +138,7 @@ class Keychain {
|
||||
this.opts.dek.keyLength,
|
||||
this.opts.dek.hash) : ''
|
||||
|
||||
Object.defineProperty(this, '_', { value: () => dek })
|
||||
privates.set(this, { dek })
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,13 +152,13 @@ class Keychain {
|
||||
* @returns {CMS}
|
||||
*/
|
||||
get cms () {
|
||||
return new CMS(this)
|
||||
return new CMS(this, privates.get(this).dek)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the options for a keychain. A random salt is produced.
|
||||
*
|
||||
* @returns {object}
|
||||
* @returns {Object}
|
||||
*/
|
||||
static generateOptions () {
|
||||
const options = Object.assign({}, defaultOptions)
|
||||
@ -165,7 +171,7 @@ class Keychain {
|
||||
* Gets an object that can encrypt/decrypt protected data.
|
||||
* The default options for a keychain.
|
||||
*
|
||||
* @returns {object}
|
||||
* @returns {Object}
|
||||
*/
|
||||
static get options () {
|
||||
return defaultOptions
|
||||
@ -176,10 +182,10 @@ class Keychain {
|
||||
*
|
||||
* @param {string} name - The local key name; cannot already exist.
|
||||
* @param {string} type - One of the key types; 'rsa'.
|
||||
* @param {int} [size] - The key size in bits. Used for rsa keys only.
|
||||
* @returns {KeyInfo}
|
||||
* @param {number} [size = 2048] - The key size in bits. Used for rsa keys only.
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async createKey (name, type, size) {
|
||||
async createKey (name, type, size = 2048) {
|
||||
const self = this
|
||||
|
||||
if (!validateKeyName(name) || name === 'self') {
|
||||
@ -206,9 +212,12 @@ class Keychain {
|
||||
|
||||
let keyInfo
|
||||
try {
|
||||
// @ts-ignore Differences between several crypto return types need to be fixed in libp2p-crypto
|
||||
const keypair = await crypto.keys.generateKeyPair(type, size)
|
||||
const kid = await keypair.id()
|
||||
const pem = await keypair.export(this._())
|
||||
/** @type {string} */
|
||||
const dek = privates.get(this).dek
|
||||
const pem = await keypair.export(dek)
|
||||
keyInfo = {
|
||||
name: name,
|
||||
id: kid
|
||||
@ -228,7 +237,7 @@ class Keychain {
|
||||
/**
|
||||
* List all the keys.
|
||||
*
|
||||
* @returns {KeyInfo[]}
|
||||
* @returns {Promise<KeyInfo[]>}
|
||||
*/
|
||||
async listKeys () {
|
||||
const self = this
|
||||
@ -248,7 +257,7 @@ class Keychain {
|
||||
* Find a key by it's id.
|
||||
*
|
||||
* @param {string} id - The universally unique key identifier.
|
||||
* @returns {KeyInfo}
|
||||
* @returns {Promise<KeyInfo|undefined>}
|
||||
*/
|
||||
async findKeyById (id) {
|
||||
try {
|
||||
@ -263,7 +272,7 @@ class Keychain {
|
||||
* Find a key by it's name.
|
||||
*
|
||||
* @param {string} name - The local key name.
|
||||
* @returns {KeyInfo}
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async findKeyByName (name) {
|
||||
if (!validateKeyName(name)) {
|
||||
@ -283,7 +292,7 @@ class Keychain {
|
||||
* Remove an existing key.
|
||||
*
|
||||
* @param {string} name - The local key name; must already exist.
|
||||
* @returns {KeyInfo}
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async removeKey (name) {
|
||||
const self = this
|
||||
@ -304,7 +313,7 @@ class Keychain {
|
||||
*
|
||||
* @param {string} oldName - The old local key name; must already exist.
|
||||
* @param {string} newName - The new local key name; must not already exist.
|
||||
* @returns {KeyInfo}
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async renameKey (oldName, newName) {
|
||||
const self = this
|
||||
@ -345,7 +354,7 @@ class Keychain {
|
||||
*
|
||||
* @param {string} name - The local key name; must already exist.
|
||||
* @param {string} password - The password
|
||||
* @returns {string}
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async exportKey (name, password) {
|
||||
if (!validateKeyName(name)) {
|
||||
@ -359,7 +368,9 @@ class Keychain {
|
||||
try {
|
||||
const res = await this.store.get(dsname)
|
||||
const pem = uint8ArrayToString(res)
|
||||
const privateKey = await crypto.keys.import(pem, this._())
|
||||
/** @type {string} */
|
||||
const dek = privates.get(this).dek
|
||||
const privateKey = await crypto.keys.import(pem, dek)
|
||||
return privateKey.export(password)
|
||||
} catch (err) {
|
||||
return throwDelayed(err)
|
||||
@ -372,7 +383,7 @@ class Keychain {
|
||||
* @param {string} name - The local key name; must not already exist.
|
||||
* @param {string} pem - The PEM encoded PKCS #8 string
|
||||
* @param {string} password - The password.
|
||||
* @returns {KeyInfo}
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async importKey (name, pem, password) {
|
||||
const self = this
|
||||
@ -396,7 +407,9 @@ class Keychain {
|
||||
let kid
|
||||
try {
|
||||
kid = await privateKey.id()
|
||||
pem = await privateKey.export(this._())
|
||||
/** @type {string} */
|
||||
const dek = privates.get(this).dek
|
||||
pem = await privateKey.export(dek)
|
||||
} catch (err) {
|
||||
return throwDelayed(err)
|
||||
}
|
||||
@ -413,6 +426,13 @@ class Keychain {
|
||||
return keyInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a peer key
|
||||
*
|
||||
* @param {string} name - The local key name; must not already exist.
|
||||
* @param {PeerId} peer - The PEM encoded PKCS #8 string
|
||||
* @returns {Promise<KeyInfo>}
|
||||
*/
|
||||
async importPeer (name, peer) {
|
||||
const self = this
|
||||
if (!validateKeyName(name)) {
|
||||
@ -429,7 +449,9 @@ class Keychain {
|
||||
|
||||
try {
|
||||
const kid = await privateKey.id()
|
||||
const pem = await privateKey.export(this._())
|
||||
/** @type {string} */
|
||||
const dek = privates.get(this).dek
|
||||
const pem = await privateKey.export(dek)
|
||||
const keyInfo = {
|
||||
name: name,
|
||||
id: kid
|
||||
@ -448,8 +470,7 @@ class Keychain {
|
||||
* Gets the private key as PEM encoded PKCS #8 string.
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {string}
|
||||
* @private
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async _getPrivateKey (name) {
|
||||
if (!validateKeyName(name)) {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user