mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-07 21:01:36 +00:00
Compare commits
89 Commits
Author | SHA1 | Date | |
---|---|---|---|
66c8adb8bc | |||
d0c68f63fe | |||
6a1e6493cb | |||
89ed15e9d9 | |||
f3ee2f069b | |||
01f8ee1574 | |||
bb244ce392 | |||
ef04b79983 | |||
308cd9ab2c | |||
b8d2b3d651 | |||
c7eac39012 | |||
9d5f2a35ae | |||
7477c5c4a4 | |||
ba275558bb | |||
9b32d25de9 | |||
3c16dbe93e | |||
2dbcc4b126 | |||
27481e7edc | |||
aa199362c2 | |||
3694e02eaa | |||
b216471f44 | |||
10f4cb969d | |||
f3fd734a2a | |||
28d915ebb6 | |||
fb59bf2bf5 | |||
d0ae8b6267 | |||
4b01fbebf9 | |||
4f095a0543 | |||
aba09a4f30 | |||
90e88742b0 | |||
fc492c6739 | |||
21d74401ed | |||
d5374f0167 | |||
f73fe2502b | |||
2d6c0ed3f7 | |||
ae0cd81ed9 | |||
4ac25c9f32 | |||
52fdf7b40c | |||
8ca5f60f42 | |||
b8f77ea5ed | |||
7e6f3f2e22 | |||
f9a35dd8a4 | |||
a2d2c953bc | |||
e836bb7ff0 | |||
f686a3a010 | |||
92946a59c3 | |||
65e8fd5f24 | |||
f07b35f4d4 | |||
85f601f809 | |||
bb6d3dfc36 | |||
4312acf49a | |||
45c0e957c3 | |||
7d4294b279 | |||
228c2b09de | |||
faa5433f8d | |||
de53a00e4d | |||
cf45341754 | |||
8c75a1fe3c | |||
79f8ccb6fe | |||
4b1b8cdf49 | |||
ac77cbfa16 | |||
0764da9485 | |||
d94c205b94 | |||
02483376b4 | |||
ee22cec1a6 | |||
78704d8f0a | |||
533d36a161 | |||
38171cd396 | |||
cf48d49c59 | |||
945ac3b520 | |||
414a449ec1 | |||
2d527b6244 | |||
08a883c153 | |||
3224e9faa8 | |||
9bcc5055a2 | |||
c34f2a17df | |||
ce0f6fd73f | |||
2b0477c5b3 | |||
79f4edf9e2 | |||
1af8355a88 | |||
82bc0f2171 | |||
3b751c2247 | |||
cf1a4c90e8 | |||
413f50fbbe | |||
1885c773c5 | |||
74748d91fa | |||
4ccea11e6c | |||
cc8b68b659 | |||
db6808113c |
16
.aegir.js
16
.aegir.js
@ -1,16 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
|
||||
module.exports = {
|
||||
webpack: {
|
||||
resolve: {
|
||||
alias: {
|
||||
'node-forge': path.resolve(
|
||||
path.dirname(require.resolve('libp2p-crypto')),
|
||||
'../vendor/forge.bundle.js'
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
43
.gitignore
vendored
43
.gitignore
vendored
@ -1,34 +1,13 @@
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
docs
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
|
||||
coverage
|
||||
|
||||
**/node_modules
|
||||
**/*.log
|
||||
test/setup/tmp-disposable-nodes-addrs.json
|
||||
dist
|
||||
lib
|
||||
coverage
|
||||
**/*.swp
|
||||
examples/sub-module/**/bundle.js
|
||||
examples/sub-module/**/*-minified.js
|
||||
examples/sub-module/*-bundle.js
|
||||
|
54
.travis.yml
54
.travis.yml
@ -1,25 +1,43 @@
|
||||
|
||||
sudo: false
|
||||
language: node_js
|
||||
cache: npm
|
||||
stages:
|
||||
- check
|
||||
- test
|
||||
- cov
|
||||
|
||||
node_js:
|
||||
- 4
|
||||
- 5
|
||||
- '10'
|
||||
|
||||
# Make sure we have new NPM.
|
||||
before_install:
|
||||
- npm install -g npm
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
script:
|
||||
- npm run lint
|
||||
- npm test
|
||||
- npm run coverage
|
||||
script: npx nyc -s npm run test:node -- --bail
|
||||
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
|
||||
|
||||
addons:
|
||||
firefox: 'latest'
|
||||
jobs:
|
||||
include:
|
||||
- os: windows
|
||||
cache: false
|
||||
|
||||
before_script:
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
- stage: check
|
||||
script:
|
||||
- npx aegir commitlint --travis
|
||||
- npx aegir dep-check
|
||||
- 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
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
after_success:
|
||||
- npm run coverage-publish
|
161
CHANGELOG.md
Normal file
161
CHANGELOG.md
Normal file
@ -0,0 +1,161 @@
|
||||
<a name="0.7.6"></a>
|
||||
## [0.7.6](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.5...v0.7.6) (2019-03-20)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add protocol support to identify ([#51](https://github.com/libp2p/js-libp2p-identify/issues/51)) ([89ed15e](https://github.com/libp2p/js-libp2p-identify/commit/89ed15e))
|
||||
|
||||
|
||||
|
||||
<a name="0.7.5"></a>
|
||||
## [0.7.5](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.4...v0.7.5) (2019-01-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* reduce bundle size ([b8d2b3d](https://github.com/libp2p/js-libp2p-identify/commit/b8d2b3d))
|
||||
|
||||
|
||||
|
||||
<a name="0.7.4"></a>
|
||||
## [0.7.4](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.3...v0.7.4) (2019-01-04)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.3"></a>
|
||||
## [0.7.3](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.2...v0.7.3) (2018-11-26)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.2"></a>
|
||||
## [0.7.2](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.1...v0.7.2) (2018-07-23)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add the ability to verify expected peer data ([aa19936](https://github.com/libp2p/js-libp2p-identify/commit/aa19936))
|
||||
|
||||
|
||||
|
||||
<a name="0.7.1"></a>
|
||||
## [0.7.1](https://github.com/libp2p/js-libp2p-identify/compare/v0.7.0...v0.7.1) (2018-04-30)
|
||||
|
||||
|
||||
|
||||
<a name="0.7.0"></a>
|
||||
# [0.7.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.6.3...v0.7.0) (2018-04-05)
|
||||
|
||||
|
||||
|
||||
<a name="0.6.3"></a>
|
||||
## [0.6.3](https://github.com/libp2p/js-libp2p-identify/compare/v0.6.2...v0.6.3) (2018-01-28)
|
||||
|
||||
|
||||
|
||||
<a name="0.6.2"></a>
|
||||
## [0.6.2](https://github.com/libp2p/js-libp2p-identify/compare/v0.6.1...v0.6.2) (2018-01-07)
|
||||
|
||||
|
||||
|
||||
<a name="0.6.1"></a>
|
||||
## [0.6.1](https://github.com/libp2p/js-libp2p-identify/compare/v0.6.0...v0.6.1) (2017-09-07)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* replace protocol-buffers with protons ([#42](https://github.com/libp2p/js-libp2p-identify/issues/42)) ([4ac25c9](https://github.com/libp2p/js-libp2p-identify/commit/4ac25c9))
|
||||
|
||||
|
||||
|
||||
<a name="0.6.0"></a>
|
||||
# [0.6.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.5.0...v0.6.0) (2017-09-03)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* p2p addrs situation ([#41](https://github.com/libp2p/js-libp2p-identify/issues/41)) ([b8f77ea](https://github.com/libp2p/js-libp2p-identify/commit/b8f77ea))
|
||||
|
||||
|
||||
|
||||
<a name="0.5.0"></a>
|
||||
# [0.5.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.4.2...v0.5.0) (2017-07-22)
|
||||
|
||||
|
||||
|
||||
<a name="0.4.2"></a>
|
||||
## [0.4.2](https://github.com/libp2p/js-libp2p-identify/compare/v0.4.1...v0.4.2) (2017-07-08)
|
||||
|
||||
|
||||
|
||||
<a name="0.4.1"></a>
|
||||
## [0.4.1](https://github.com/libp2p/js-libp2p-identify/compare/v0.4.0...v0.4.1) (2017-04-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* if conn was closed, don't crash just because identify didn't get any data ([#40](https://github.com/libp2p/js-libp2p-identify/issues/40)) ([bb6d3df](https://github.com/libp2p/js-libp2p-identify/commit/bb6d3df))
|
||||
|
||||
|
||||
|
||||
<a name="0.4.0"></a>
|
||||
# [0.4.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.3.3...v0.4.0) (2017-03-30)
|
||||
|
||||
|
||||
|
||||
<a name="0.3.3"></a>
|
||||
## [0.3.3](https://github.com/libp2p/js-libp2p-identify/compare/v0.3.2...v0.3.3) (2017-03-21)
|
||||
|
||||
|
||||
|
||||
<a name="0.3.2"></a>
|
||||
## [0.3.2](https://github.com/libp2p/js-libp2p-identify/compare/v0.3.1...v0.3.2) (2017-02-09)
|
||||
|
||||
|
||||
|
||||
<a name="0.3.1"></a>
|
||||
## [0.3.1](https://github.com/libp2p/js-libp2p-identify/compare/v0.3.0...v0.3.1) (2017-01-11)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove deprecation notice from identify protobuf ([38171cd](https://github.com/libp2p/js-libp2p-identify/commit/38171cd))
|
||||
|
||||
|
||||
|
||||
<a name="0.3.0"></a>
|
||||
# [0.3.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.2.0...v0.3.0) (2016-11-03)
|
||||
|
||||
|
||||
|
||||
<a name="0.2.0"></a>
|
||||
# [0.2.0](https://github.com/libp2p/js-libp2p-identify/compare/v0.1.3...v0.2.0) (2016-09-06)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **api:** move to pull-streams ([3b751c2](https://github.com/libp2p/js-libp2p-identify/commit/3b751c2))
|
||||
* **readme:** documentationa and cleanup of code ([82bc0f2](https://github.com/libp2p/js-libp2p-identify/commit/82bc0f2))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.3"></a>
|
||||
## [0.1.3](https://github.com/libp2p/js-libp2p-identify/compare/v0.1.2...v0.1.3) (2016-08-03)
|
||||
|
||||
|
||||
|
||||
<a name="0.1.2"></a>
|
||||
## [0.1.2](https://github.com/libp2p/js-libp2p-identify/compare/v0.1.1...v0.1.2) (2016-06-27)
|
||||
|
||||
|
||||
|
||||
<a name="0.1.1"></a>
|
||||
## [0.1.1](https://github.com/libp2p/js-libp2p-identify/compare/v0.1.0...v0.1.1) (2016-06-27)
|
||||
|
||||
|
||||
|
||||
<a name="0.1.0"></a>
|
||||
# 0.1.0 (2016-06-27)
|
||||
|
||||
|
||||
|
49
README.md
49
README.md
@ -1,4 +1,49 @@
|
||||
libp2p-identify
|
||||
===============
|
||||
# js-libp2p-identify
|
||||
|
||||
[](http://protocol.ai)
|
||||
[](http://libp2p.io/)
|
||||
[](http://webchat.freenode.net/?channels=%23libp2p)
|
||||
[](https://codecov.io/gh/libp2p/js-libp2p-identify)
|
||||
[](https://travis-ci.com/libp2p/js-libp2p-identify)
|
||||
[](https://david-dm.org/libp2p/js-libp2p-identify)
|
||||
[](https://github.com/feross/standard)
|
||||

|
||||

|
||||
|
||||
> libp2p Identify Protocol
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[Jacob Heun](https://github.com/jacobheun)
|
||||
|
||||
## Description
|
||||
|
||||
Identify is a STUN protocol, used by libp2p-swarm in order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
|
||||
|
||||
## How does it work
|
||||
|
||||
Best way to understand the current design is through this issue: https://github.com/libp2p/js-libp2p-swarm/issues/78
|
||||
|
||||
### This module uses `pull-streams`
|
||||
|
||||
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||
|
||||
You can learn more about pull-streams at:
|
||||
|
||||
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||
|
||||
#### Converting `pull-streams` to Node.js Streams
|
||||
|
||||
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
|
||||
|
||||
```js
|
||||
const pullToStream = require('pull-stream-to-stream')
|
||||
|
||||
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||
// nodeStreamInstance is an instance of a Node.js Stream
|
||||
```
|
||||
|
||||
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
|
||||
|
13
circle.yml
13
circle.yml
@ -1,13 +0,0 @@
|
||||
|
||||
machine:
|
||||
node:
|
||||
version: stable
|
||||
|
||||
dependencies:
|
||||
pre:
|
||||
- google-chrome --version
|
||||
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
||||
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
|
||||
- sudo apt-get update
|
||||
- sudo apt-get --only-upgrade install google-chrome-stable
|
||||
- google-chrome --version
|
67
package.json
67
package.json
@ -1,27 +1,31 @@
|
||||
{
|
||||
"name": "libp2p-identify",
|
||||
"version": "0.1.2",
|
||||
"version": "0.7.6",
|
||||
"description": "libp2p Identify Protocol",
|
||||
"main": "lib/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "aegir-lint",
|
||||
"build": "aegir-build",
|
||||
"test": "aegir-test",
|
||||
"test:node": "aegir-test test:node",
|
||||
"test:browser": "aegir-test test:browser",
|
||||
"release": "aegir-release",
|
||||
"release-minor": "aegir-release --type minor",
|
||||
"release-major": "aegir-release --type major",
|
||||
"coverage": "aegir-coverage",
|
||||
"coverage-publish": "aegir-coverage publish"
|
||||
"lint": "aegir lint",
|
||||
"build": "aegir build",
|
||||
"test": "aegir test",
|
||||
"test:node": "aegir test -t node",
|
||||
"test:browser": "aegir test -t browser",
|
||||
"release": "aegir release",
|
||||
"release-minor": "aegir release --type minor",
|
||||
"release-major": "aegir release --type major",
|
||||
"coverage": "aegir coverage"
|
||||
},
|
||||
"pre-commit": [
|
||||
"pre-push": [
|
||||
"lint",
|
||||
"test"
|
||||
],
|
||||
"engines": {
|
||||
"node": "^4.3.0"
|
||||
"node": ">=6.0.0",
|
||||
"npm": ">=3.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -31,25 +35,36 @@
|
||||
"IPFS",
|
||||
"libp2p"
|
||||
],
|
||||
"author": "David Dias <daviddias@ipfs.io>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/libp2p/js-libp2p-identify/issues"
|
||||
},
|
||||
"homepage": "https://github.com/libp2p/js-libp2p-identify#readme",
|
||||
"devDependencies": {
|
||||
"aegir": "^3.2.0",
|
||||
"pre-commit": "^1.1.3",
|
||||
"stream-pair": "^1.0.3"
|
||||
"aegir": "^18.1.0",
|
||||
"chai": "^4.2.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"pull-pair": "^1.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"length-prefixed-stream": "^1.5.0",
|
||||
"multiaddr": "^2.0.2",
|
||||
"peer-id": "^0.7.0",
|
||||
"peer-info": "^0.7.0",
|
||||
"protocol-buffers": "^3.1.6"
|
||||
"multiaddr": "^6.0.4",
|
||||
"peer-id": "~0.12.2",
|
||||
"peer-info": "~0.15.1",
|
||||
"protons": "^1.0.1",
|
||||
"pull-length-prefixed": "^1.3.1",
|
||||
"pull-stream": "^3.6.9"
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>"
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Greenkeeper <support@greenkeeper.io>",
|
||||
"Hugo Dias <hugomrdias@gmail.com>",
|
||||
"Jacob Heun <jacobheun@gmail.com>",
|
||||
"Jacob Heun <jake@andyet.net>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||
"Yusef Napora <yusef@protocol.ai>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
86
src/dialer.js
Normal file
86
src/dialer.js
Normal file
@ -0,0 +1,86 @@
|
||||
'use strict'
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream/pull')
|
||||
const take = require('pull-stream/throughs/take')
|
||||
const collect = require('pull-stream/sinks/collect')
|
||||
const lp = require('pull-length-prefixed')
|
||||
|
||||
const msg = require('./message')
|
||||
|
||||
module.exports = (conn, expectedPeerInfo, callback) => {
|
||||
if (typeof expectedPeerInfo === 'function') {
|
||||
callback = expectedPeerInfo
|
||||
expectedPeerInfo = null
|
||||
console.warn('WARNING: no expected peer info was given, identify will not be able to verify peer integrity')
|
||||
}
|
||||
|
||||
pull(
|
||||
conn,
|
||||
lp.decode(),
|
||||
take(1),
|
||||
collect((err, data) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// connection got closed graciously
|
||||
if (data.length === 0) {
|
||||
return callback(new Error('conn was closed, did not receive data'))
|
||||
}
|
||||
|
||||
const input = msg.decode(data[0])
|
||||
|
||||
PeerId.createFromPubKey(input.publicKey, (err, id) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
const peerInfo = new PeerInfo(id)
|
||||
if (expectedPeerInfo && expectedPeerInfo.id.toB58String() !== id.toB58String()) {
|
||||
return callback(new Error('invalid peer'))
|
||||
}
|
||||
|
||||
try {
|
||||
input.listenAddrs
|
||||
.map(multiaddr)
|
||||
.forEach((ma) => peerInfo.multiaddrs.add(ma))
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
let observedAddr
|
||||
|
||||
try {
|
||||
observedAddr = getObservedAddrs(input)
|
||||
} catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
// Copy the protocols
|
||||
peerInfo.protocols = new Set(input.protocols)
|
||||
|
||||
callback(null, peerInfo, observedAddr)
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
function getObservedAddrs (input) {
|
||||
if (!hasObservedAddr(input)) {
|
||||
return []
|
||||
}
|
||||
|
||||
let addrs = input.observedAddr
|
||||
|
||||
if (!Array.isArray(addrs)) {
|
||||
addrs = [addrs]
|
||||
}
|
||||
|
||||
return addrs.map((oa) => multiaddr(oa))
|
||||
}
|
||||
|
||||
function hasObservedAddr (input) {
|
||||
return input.observedAddr && input.observedAddr.length > 0
|
||||
}
|
88
src/index.js
88
src/index.js
@ -1,89 +1,7 @@
|
||||
/*
|
||||
* Identify is one of the protocols swarms speaks in order to
|
||||
* broadcast and learn about the ip:port pairs a specific peer
|
||||
* is available through and to know when a new stream muxer is
|
||||
* established, so a conn can be reused
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
const bl = require('bl')
|
||||
|
||||
const lpstream = require('length-prefixed-stream')
|
||||
const protobuf = require('protocol-buffers')
|
||||
const schema = fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
||||
const idPb = protobuf(schema)
|
||||
|
||||
exports = module.exports
|
||||
exports.multicodec = '/ipfs/id/1.0.0'
|
||||
|
||||
exports.exec = (conn, callback) => {
|
||||
const decode = lpstream.decode()
|
||||
|
||||
conn
|
||||
.pipe(decode)
|
||||
.pipe(bl((err, data) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
const msg = idPb.Identify.decode(data)
|
||||
let observedAddrs = []
|
||||
if (hasObservedAddr(msg)) {
|
||||
if (!Array.isArray(msg.observedAddr)) {
|
||||
msg.observedAddr = [msg.observedAddr]
|
||||
}
|
||||
observedAddrs = msg.observedAddr.map((oa) => {
|
||||
return multiaddr(oa)
|
||||
})
|
||||
}
|
||||
|
||||
const pId = PeerId.createFromPubKey(msg.publicKey)
|
||||
const pInfo = new PeerInfo(pId)
|
||||
msg.listenAddrs.forEach((ma) => {
|
||||
pInfo.multiaddr.add(multiaddr(ma))
|
||||
})
|
||||
|
||||
callback(null, pInfo, observedAddrs)
|
||||
}))
|
||||
|
||||
conn.end()
|
||||
}
|
||||
|
||||
exports.handler = (pInfoSelf) => {
|
||||
return (conn) => {
|
||||
// send what I see from the other + my Info
|
||||
const encode = lpstream.encode()
|
||||
|
||||
encode.pipe(conn)
|
||||
|
||||
conn.getObservedAddrs((err, observedAddrs) => {
|
||||
if (err) { return }
|
||||
observedAddrs = observedAddrs[0]
|
||||
|
||||
let publicKey = new Buffer(0)
|
||||
if (pInfoSelf.id.pubKey) {
|
||||
publicKey = pInfoSelf.id.pubKey.bytes
|
||||
}
|
||||
|
||||
const msgSend = idPb.Identify.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: publicKey,
|
||||
listenAddrs: pInfoSelf.multiaddrs.map((ma) => ma.buffer),
|
||||
observedAddr: observedAddrs ? observedAddrs.buffer : new Buffer('')
|
||||
})
|
||||
|
||||
encode.write(msgSend)
|
||||
encode.end()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function hasObservedAddr (msg) {
|
||||
return msg.observedAddr && msg.observedAddr.length > 0
|
||||
}
|
||||
exports.listener = require('./listener')
|
||||
exports.dialer = require('./dialer')
|
||||
exports.message = require('./message')
|
||||
|
35
src/listener.js
Normal file
35
src/listener.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const values = require('pull-stream/sources/values')
|
||||
const lp = require('pull-length-prefixed')
|
||||
|
||||
const msg = require('./message')
|
||||
|
||||
module.exports = (conn, pInfoSelf) => {
|
||||
// send what I see from the other + my Info
|
||||
conn.getObservedAddrs((err, observedAddrs) => {
|
||||
if (err) { return }
|
||||
observedAddrs = observedAddrs[0]
|
||||
|
||||
let publicKey = Buffer.alloc(0)
|
||||
if (pInfoSelf.id.pubKey) {
|
||||
publicKey = pInfoSelf.id.pubKey.bytes
|
||||
}
|
||||
|
||||
const msgSend = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: publicKey,
|
||||
listenAddrs: pInfoSelf.multiaddrs.toArray().map((ma) => ma.buffer),
|
||||
observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from(''),
|
||||
protocols: Array.from(pInfoSelf.protocols)
|
||||
})
|
||||
|
||||
pull(
|
||||
values([msgSend]),
|
||||
lp.encode(),
|
||||
conn
|
||||
)
|
||||
})
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
message Identify {
|
||||
'use strict'
|
||||
|
||||
const protons = require('protons')
|
||||
const schema = `
|
||||
message Identify {
|
||||
// protocolVersion determines compatibility between peers
|
||||
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
|
||||
|
||||
@ -20,6 +23,8 @@ message Identify {
|
||||
// determine whether its connection to the local peer goes through NAT.
|
||||
optional bytes observedAddr = 4;
|
||||
|
||||
// (DEPRECATED) protocols are the services this node is running
|
||||
// repeated string protocols = 3;
|
||||
repeated string protocols = 3;
|
||||
}
|
||||
`
|
||||
|
||||
module.exports = protons(schema).Identify
|
15
test/basic.spec.js
Normal file
15
test/basic.spec.js
Normal file
@ -0,0 +1,15 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const identify = require('../src')
|
||||
|
||||
describe('basic', () => {
|
||||
it('multicodec', () => {
|
||||
expect(identify.multicodec).to.eql('/ipfs/id/1.0.0')
|
||||
})
|
||||
})
|
184
test/dialer.spec.js
Normal file
184
test/dialer.spec.js
Normal file
@ -0,0 +1,184 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const values = require('pull-stream/sources/values')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const pair = require('pull-pair/duplex')
|
||||
const PeerInfo = require('peer-info')
|
||||
const lp = require('pull-length-prefixed')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const msg = require('../src/message')
|
||||
const identify = require('../src')
|
||||
|
||||
describe('identify.dialer', () => {
|
||||
let original
|
||||
beforeEach(function (done) {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
PeerInfo.create((err, info) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
original = info
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('works', (done) => {
|
||||
const p = pair()
|
||||
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
original.protocols.add('/echo/1.0.0')
|
||||
original.protocols.add('/ping/1.0.0')
|
||||
|
||||
const input = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: original.id.pubKey.bytes,
|
||||
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
|
||||
protocols: Array.from(original.protocols)
|
||||
})
|
||||
|
||||
pull(
|
||||
values([input]),
|
||||
lp.encode(),
|
||||
p[0]
|
||||
)
|
||||
|
||||
identify.dialer(p[1], (err, info, observedAddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(info.id.pubKey.bytes)
|
||||
.to.eql(original.id.pubKey.bytes)
|
||||
|
||||
expect(info.multiaddrs.toArray())
|
||||
.to.eql(original.multiaddrs.toArray())
|
||||
|
||||
expect(observedAddrs)
|
||||
.to.eql([multiaddr('/ip4/127.0.0.1/tcp/5001')])
|
||||
|
||||
expect(info.protocols).to.eql(original.protocols)
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle missing protocols', (done) => {
|
||||
const p = pair()
|
||||
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
|
||||
const input = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: original.id.pubKey.bytes,
|
||||
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
|
||||
protocols: Array.from(original.protocols)
|
||||
})
|
||||
|
||||
pull(
|
||||
values([input]),
|
||||
lp.encode(),
|
||||
p[0]
|
||||
)
|
||||
|
||||
identify.dialer(p[1], (err, info, observedAddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(info.id.pubKey.bytes)
|
||||
.to.eql(original.id.pubKey.bytes)
|
||||
|
||||
expect(info.multiaddrs.toArray())
|
||||
.to.eql(original.multiaddrs.toArray())
|
||||
|
||||
expect(observedAddrs)
|
||||
.to.eql([multiaddr('/ip4/127.0.0.1/tcp/5001')])
|
||||
|
||||
expect(Array.from(info.protocols)).to.eql([])
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('does not crash with invalid listen addresses', (done) => {
|
||||
const p = pair()
|
||||
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
const input = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: original.id.pubKey.bytes,
|
||||
listenAddrs: [Buffer.from('ffac010203')],
|
||||
observedAddr: Buffer.from('ffac010203')
|
||||
})
|
||||
|
||||
pull(
|
||||
values([input]),
|
||||
lp.encode(),
|
||||
p[0]
|
||||
)
|
||||
|
||||
identify.dialer(p[1], (err, info, observedAddrs) => {
|
||||
expect(err).to.exist()
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('does not crash with invalid observed address', (done) => {
|
||||
const p = pair()
|
||||
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
const input = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: original.id.pubKey.bytes,
|
||||
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||
observedAddr: Buffer.from('ffac010203')
|
||||
})
|
||||
|
||||
pull(
|
||||
values([input]),
|
||||
lp.encode(),
|
||||
p[0]
|
||||
)
|
||||
|
||||
identify.dialer(p[1], (err, info, observedAddrs) => {
|
||||
expect(err).to.exist()
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should return an error with mismatched peerInfo data', (done) => {
|
||||
const p = pair()
|
||||
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
const input = msg.encode({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: original.id.pubKey.bytes,
|
||||
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer
|
||||
})
|
||||
|
||||
PeerInfo.create((err, info) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
pull(
|
||||
values([input]),
|
||||
lp.encode(),
|
||||
p[0]
|
||||
)
|
||||
|
||||
identify.dialer(p[1], info, (err, peerInfo) => {
|
||||
expect(err).to.exist()
|
||||
expect(peerInfo).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -1,4 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
describe('identify', () => {})
|
70
test/listener.spec.js
Normal file
70
test/listener.spec.js
Normal file
@ -0,0 +1,70 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream/pull')
|
||||
const collect = require('pull-stream/sinks/collect')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const pair = require('pull-pair/duplex')
|
||||
const PeerInfo = require('peer-info')
|
||||
const lp = require('pull-length-prefixed')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const msg = require('../src/message')
|
||||
const identify = require('../src')
|
||||
|
||||
describe('identify.listener', () => {
|
||||
let info
|
||||
|
||||
beforeEach(function (done) {
|
||||
this.timeout(20 * 1000)
|
||||
|
||||
PeerInfo.create((err, _info) => {
|
||||
if (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
_info.protocols.add('/echo/1.0.0')
|
||||
_info.protocols.add('/chat/1.0.0')
|
||||
|
||||
info = _info
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('works', (done) => {
|
||||
const p = pair()
|
||||
|
||||
info.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||
|
||||
pull(
|
||||
p[1],
|
||||
lp.decode(),
|
||||
collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
const input = msg.decode(result[0])
|
||||
expect(
|
||||
input
|
||||
).to.be.eql({
|
||||
protocolVersion: 'ipfs/0.1.0',
|
||||
agentVersion: 'na',
|
||||
publicKey: info.id.pubKey.bytes,
|
||||
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
|
||||
protocols: ['/echo/1.0.0', '/chat/1.0.0']
|
||||
})
|
||||
done()
|
||||
})
|
||||
)
|
||||
|
||||
const conn = p[0]
|
||||
conn.getObservedAddrs = (cb) => {
|
||||
cb(null, [multiaddr('/ip4/127.0.0.1/tcp/5001')])
|
||||
}
|
||||
|
||||
identify.listener(conn, info)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user