Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 71ad80c

Browse files
authored
Merge pull request #1 from dryajov/master
feat: implement relay circuit
2 parents 0268a5d + d6f1268 commit 71ad80c

18 files changed

+1106
-2
lines changed

.gitignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history
34+
35+
# Vim editor swap files
36+
*.swp
37+
38+
dist
39+
40+
.history
41+
.vscode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict'
2+
3+
const config = require('../config')
4+
const pull = require('pull-stream')
5+
const multiaddr = require('multiaddr')
6+
const PeerInfo = require('peer-info')
7+
const Peer = require('../peer')
8+
const includes = require('lodash/includes')
9+
const lp = require('pull-length-prefixed')
10+
const handshake = require('pull-handshake')
11+
const Connection = require('interface-connection').Connection
12+
13+
const multicodec = config.multicodec
14+
15+
const log = config.log
16+
17+
class Listener {
18+
constructor(libp2p, handler) {
19+
this.libp2p = libp2p
20+
this.peers = new Map()
21+
this.handler = handler
22+
23+
this._onConnection = this._onConnection.bind(this)
24+
}
25+
26+
listen(cb) {
27+
cb = cb || function() {}
28+
this.libp2p.handle(multicodec, this._onConnection)
29+
cb()
30+
}
31+
32+
close(cb) {
33+
cb = cb || function() {}
34+
this.libp2p.unhandle(multicodec)
35+
cb()
36+
}
37+
38+
_onConnection(protocol, conn) {
39+
conn.getPeerInfo((err, peerInfo) => {
40+
if (err) {
41+
log.err('Failed to identify incomming conn', err)
42+
return pull(pull.empty(), conn)
43+
}
44+
45+
const idB58Str = peerInfo.id.toB58String()
46+
let relayPeer = this.peers.get(idB58Str)
47+
if (!relayPeer) {
48+
log('new relay peer', idB58Str)
49+
relayPeer = peerInfo
50+
this.peers.set(idB58Str, new Peer(conn, peerInfo))
51+
}
52+
this._processConnection(relayPeer, conn)
53+
})
54+
}
55+
56+
_processConnection(relayPeer, conn) {
57+
let stream = handshake({ timeout: 1000 * 60 })
58+
let shake = stream.handshake
59+
60+
lp.decodeFromReader(shake, (err, msg) => {
61+
if (err) {
62+
err(err)
63+
return err
64+
}
65+
66+
let addr = multiaddr(msg.toString())
67+
let src
68+
try {
69+
PeerInfo.create(addr.peerId(), (err, peerInfo) => {
70+
if (err) {
71+
log.err(err)
72+
return err
73+
}
74+
75+
if (includes(addr.protoNames(), 'ipfs')) {
76+
addr = addr.decapsulate('ipfs')
77+
}
78+
79+
peerInfo.multiaddr.add(addr)
80+
src = peerInfo
81+
this.handler(new Connection(shake.rest(), peerInfo))
82+
})
83+
} catch (err) {
84+
log.err(err)
85+
}
86+
})
87+
88+
pull(stream, conn, stream)
89+
}
90+
91+
}
92+
93+
module.exports = Listener

.npmignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
test
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
13+
# Directory for instrumented libs generated by jscoverage/JSCover
14+
lib-cov
15+
16+
# Coverage directory used by tools like istanbul
17+
coverage
18+
19+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
20+
.grunt
21+
22+
# node-waf configuration
23+
.lock-wscript
24+
25+
# Compiled binary addons (http://nodejs.org/api/addons.html)
26+
build/Release
27+
28+
# Dependency directory
29+
node_modules
30+
31+
# Optional npm cache directory
32+
.npm
33+
34+
# Optional REPL history
35+
.node_repl_history

.travis.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
sudo: false
2+
language: node_js
3+
4+
matrix:
5+
include:
6+
- node_js: 4
7+
env: CXX=g++-4.8
8+
- node_js: 6
9+
env:
10+
- SAUCE=true
11+
- CXX=g++-4.8
12+
- node_js: "stable"
13+
env:
14+
- CXX=g++-4.8
15+
16+
# Make sure we have new NPM.
17+
before_install:
18+
- npm install -g npm
19+
20+
script:
21+
- npm run lint
22+
- npm test
23+
- npm run coverage
24+
25+
before_script:
26+
- export DISPLAY=:99.0
27+
- sh -e /etc/init.d/xvfb start
28+
29+
after_success:
30+
- npm run coverage-publish
31+
32+
addons:
33+
firefox: latest
34+
apt:
35+
sources:
36+
- ubuntu-toolchain-r-test
37+
packages:
38+
- g++-4.8

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 libp2p
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
# js-libp2p-circuit
1+
# <topic>
2+
3+
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
4+
[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://github.com/libp2p/libp2p)
5+
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6+
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
7+
8+
> <description>
9+
10+
<long description>
11+
12+
## Table of Contents
13+
14+
<ToC>
15+
16+
## Install
17+
18+
<install>
19+
20+
## Usage
21+
22+
<usage>
23+
24+
## Lead
25+
26+
- [<lead>](https://github.com/<lead>)
27+
28+
## Contribute
29+
30+
Please contribute! [Look at the issues](https://github.com/libp2p/<repoName>/issues)!
31+
32+
Check out our [contributing document](https://github.com/libp2p/community/blob/master/CONTRIBUTE.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to libp2p are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
33+
34+
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
35+
36+
## License
37+
38+
[MIT](LICENSE) © 2016 Protocol Labs Inc.

circle.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
machine:
2+
node:
3+
version: stable
4+
5+
dependencies:
6+
pre:
7+
- google-chrome --version
8+
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
9+
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
10+
- sudo apt-get update
11+
- sudo apt-get --only-upgrade install google-chrome-stable
12+
- google-chrome --version

implementation-notes.md

-1
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,4 @@ In order to minimize nesting, the first example is preferred
124124
- A generic circuit address will be added to the peers multiaddr list
125125
- i.e. `/p2p-circuit/ipfs/QmDest`
126126
- If another transport is available, then use that instead of the relay
127-
128127

package.json

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "libp2p-circuit",
3+
"version": "0.0.1-alpha.1",
4+
"description": "JavaScript implementation of circuit/switch relaying",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"lint": "aegir-lint",
8+
"build": "aegir-build",
9+
"test": "aegir-test --env node",
10+
"release": "aegir-release",
11+
"release-minor": "aegir-release --type minor",
12+
"release-major": "aegir-release --type major",
13+
"coverage": "aegir-coverage",
14+
"coverage-publish": "aegir-coverage publish"
15+
},
16+
"pre-commit": [
17+
"lint",
18+
"test"
19+
],
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/libp2p/js-libp2p-circuit.git"
23+
},
24+
"keywords": [
25+
"IPFS"
26+
],
27+
"author": "Dmitriy Ryajov <[email protected]>",
28+
"license": "MIT",
29+
"bugs": {
30+
"url": "https://github.com/libp2p/js-libp2p-circuit/issues"
31+
},
32+
"homepage": "https://github.com/libp2p/js-libp2p-circuit#readme",
33+
"eslintConfig": {
34+
"extends": [
35+
"./node_modules/aegir/config/eslintrc.yml"
36+
]
37+
},
38+
"devDependencies": {
39+
"aegir": "^10.0.0",
40+
"chai": "^3.5.0",
41+
"libp2p-ipfs-browser": "^0.19.0",
42+
"libp2p-ipfs-nodejs": "^0.19.0",
43+
"libp2p-mdns": "^0.6.1",
44+
"libp2p-multiplex": "^0.4.1",
45+
"libp2p-railing": "^0.4.1",
46+
"libp2p-secio": "^0.6.7",
47+
"libp2p-spdy": "^0.10.4",
48+
"libp2p-swarm": "^0.26.18",
49+
"libp2p-tcp": "^0.9.3",
50+
"libp2p-webrtc-star": "^0.8.8",
51+
"libp2p-websockets": "^0.9.2",
52+
"peer-id": "^0.8.2",
53+
"peer-info": "^0.8.3",
54+
"pre-commit": "^1.2.2"
55+
},
56+
"contributors": [],
57+
"dependencies": {
58+
"async": "^2.1.5",
59+
"debug": "^2.6.1",
60+
"interface-connection": "^0.3.1",
61+
"lodash": "^4.17.4",
62+
"multiaddr": "^2.2.1",
63+
"multistream-select": "^0.13.4",
64+
"pull-abortable": "^4.1.0",
65+
"pull-handshake": "^1.1.4",
66+
"pull-stream": "^3.5.0",
67+
"setimmediate": "^1.0.5"
68+
}
69+
}

0 commit comments

Comments
 (0)