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

Commit ebce8cd

Browse files
dryajovdaviddias
authored andcommitted
v0.1.0 (#14)
* initial commit * feat: initial implementation of circuit relaying chore: adding default readme feat: reworking as a transport feat: getting peers communicating over relay (wip) feat: address in swarm [wip] feat: adding onion dial feat: adding onion dialing and tests feat: make circuit a full fledged transport refactor: split transport dialer and circuit logic test: adding dial tests feat: adding passive/active dialing test test: adding relay tests fix: several isues feat: consolidate and cleanup dialing feat: handle listenning circuit addresses correctly feat: make utils a factory feat: adding StreamHandler to aid with pull-stream read/write refactor: clean up and refactor relay and listener tests: adding more relay and listener tests tests: moving long multiaddr to a fixture feat: adding _writeErr to handle returning errors in relay.js fix: cleanup, moving setup code outside of dialer refactor: renaming Relay to Hop chore: fixing license chore: removing custom eslint config refactor: extract listening functionality into Stop handler fix: removing unused priority functionality fix: correct import of safe-buffer lint: remove unused import refactor: use async/setImmediate fix: remove unused constant refactor: move active/passive check out of _readDstAddr feat: initial readme write up fix: output correct circuit addresses feat: initial reimplementation of /hop /stop as a protobufs feat: reworking with protobufs feat: moving SUCCESS response to HOP from STOP feat: removing multihop (onion) dialing 0.0.3 fix: go-js integration test changes feat: adding dialer tests * feat: add getter for tag in circuit transport dialer * feat: options, rename circuit to hop * fix: correctly incapsulate the peer id * fix: correct SUCCESS message return from STOP and HOP * chore: upgrading peer-id and peer-info * doc: adding listener documentation * feat: handle active/passive relays * test: porting to new aegir/jest * chore: updating ci scripts * chore: removing make test from travis yml * test: use new aegir and dirty chai * linting * feat: return error in callback on dial * test: fixing tests based on review feedback
1 parent 0268a5d commit ebce8cd

26 files changed

+2496
-0
lines changed

.gitignore

Lines changed: 41 additions & 0 deletions
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

.npmignore

Lines changed: 35 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
sudo: false
2+
language: node_js
3+
4+
matrix:
5+
include:
6+
- node_js: 6
7+
env: CXX=g++-4.8
8+
- node_js: 8
9+
env: CXX=g++-4.8
10+
# - node_js: stable
11+
# env: CXX=g++-4.8
12+
13+
script:
14+
- npm run lint
15+
- npm run test
16+
- npm run coverage
17+
18+
before_script:
19+
- export DISPLAY=:99.0
20+
- sh -e /etc/init.d/xvfb start
21+
22+
after_success:
23+
- npm run coverage-publish
24+
25+
addons:
26+
firefox: 'latest'
27+
apt:
28+
sources:
29+
- ubuntu-toolchain-r-test
30+
packages:
31+
- g++-4.8

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Protocol Labs Inc
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

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,197 @@
11
# js-libp2p-circuit
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/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
5+
[![Build Status](https://travis-ci.org/libp2p/js-libp2p-circuit.svg?style=flat-square)](https://travis-ci.org/libp2p/js-libp2p-circuit)
6+
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-circuit/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-circuit?branch=master)
7+
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-circuit.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-circuit)
8+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
9+
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
10+
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
11+
12+
![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)
13+
![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)
14+
15+
> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
16+
17+
`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node.
18+
19+
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
20+
21+
### Why?
22+
23+
`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
24+
25+
The applicability of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
26+
- routing traffic between private nets
27+
- circumventing NAT layers
28+
- route mangling for better privacy (matreshka/shallot dialing).
29+
30+
It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluethoot enabled relays and become full p2p nodes.
31+
32+
### libp2p-circuit and IPFS
33+
34+
Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
35+
36+
## Table of Contents
37+
38+
- [Install](#install)
39+
- [npm](#npm)
40+
- [Usage](#usage)
41+
- [Example](#example)
42+
- [This module uses `pull-streams`](#this-module-uses-pull-streams)
43+
- [Converting `pull-streams` to Node.js Streams](#converting-pull-streams-to-nodejs-streams)
44+
- [API](#api)
45+
- [Contribute](#contribute)
46+
- [License](#license)
47+
48+
## Install
49+
50+
### npm
51+
52+
```sh
53+
> npm i libp2p-circuit
54+
```
55+
56+
## Usage
57+
58+
### Example
59+
60+
#### Create dialer/listener
61+
62+
```js
63+
const Circuit = require('libp2p-circuit')
64+
const multiaddr = require('multiaddr')
65+
const pull = require('pull-stream')
66+
67+
const mh1 = multiaddr('/p2p-circuit/ipfs/QmHash') // dial /ipfs/QmHash over any circuit
68+
69+
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
70+
71+
const listener = circuit.createListener(mh1, (connection) => {
72+
console.log('new connection opened')
73+
pull(
74+
pull.values(['hello']),
75+
socket
76+
)
77+
})
78+
79+
listener.listen(() => {
80+
console.log('listening')
81+
82+
pull(
83+
circuit.dial(mh1),
84+
pull.log,
85+
pull.onEnd(() => {
86+
circuit.close()
87+
})
88+
)
89+
})
90+
```
91+
92+
Outputs:
93+
94+
```sh
95+
listening
96+
new connection opened
97+
hello
98+
```
99+
100+
#### Create `relay`
101+
102+
```js
103+
const Relay = require('libp2p-circuit').Realy
104+
105+
const relay = new Relay(options)
106+
107+
relay.mount(swarmInstance) // start relaying traffic
108+
```
109+
110+
### This module uses `pull-streams`
111+
112+
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).
113+
114+
You can learn more about pull-streams at:
115+
116+
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
117+
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
118+
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
119+
- [pull-streams documentation](https://pull-stream.github.io/)
120+
121+
#### Converting `pull-streams` to Node.js Streams
122+
123+
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
124+
125+
```js
126+
const pullToStream = require('pull-stream-to-stream')
127+
128+
const nodeStreamInstance = pullToStream(pullStreamInstance)
129+
// nodeStreamInstance is an instance of a Node.js Stream
130+
```
131+
132+
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
133+
134+
## API
135+
136+
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
137+
138+
`libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
139+
140+
`/p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
141+
142+
Both for dialing and listening.
143+
144+
### Implementation rational
145+
146+
This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p-swarm`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p-swarm` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
147+
148+
```
149+
150+
libp2p libp2p-circuit
151+
+-------------------------------------------------+ +---------------------------------------------------------+
152+
| | | |
153+
| +---------------------------------+ | | |
154+
| | | | | +------------------------+ |
155+
| | | | circuit-relay registers the /hop | | | |
156+
| | libp2p-swarm |<----------------------------------------------------->| circuit-relay | |
157+
| | | | multistream handler with the swarm | | | |
158+
| | | | to handle incomming dial requests | +------------------------+ |
159+
| +---------------------------------+ | from other nodes | transport |
160+
| ^ ^ ^ ^ ^ ^ | | +-------------------------------------------+ |
161+
| | | | | | | | | | +----------------------------------+ | |
162+
| | | | | | | | dialer uses the swarm to dial to a | | | | | |
163+
| | | | +------------------------------------------------------------------------------>| dialer | | |
164+
| | |transports | | relay node listening on the /hop | | | | | |
165+
| | | | | | | multistream endpoint | | +----------------------------------+ | |
166+
| | | | | | | | | | |
167+
| v v | v v | | | | |
168+
|+------------------|----------------------------+| | | +----------------------------------+ | |
169+
|| | | | | || | | | | | |
170+
||libp2p-tcp |libp2p-ws | .... |libp2p-circuit ||listener registers a /stop multistream | | listener | | |
171+
|| | +-------------------------------------------------------------------------------->| | | |
172+
|| | | |pluggs in just ||handler with the swarm to handle | | +----------------------------------+ | |
173+
|| | | |as any other ||incomming relay connections | | | |
174+
|| | | |transport || | +-------------------------------------------+ |
175+
|+-----------------------------------------------+| | |
176+
| | | |
177+
| | | |
178+
+-------------------------------------------------+ +---------------------------------------------------------+
179+
```
180+
181+
182+
183+
## Contribute
184+
185+
Contributions are welcome! The libp2p implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:
186+
187+
- [Check out the existing issues](//github.com/libp2p/js-libp2p-circuit/issues).
188+
- **Perform code reviews**.
189+
- **Add tests**. There can never be enough tests.
190+
191+
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).
192+
193+
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
194+
195+
## License
196+
197+
[MIT](LICENSE) © 2017 Protocol Labs

circle.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
machine:
2+
node:
3+
version: stable
4+
5+
test:
6+
post:
7+
- npm run coverage -- --upload
8+
9+
dependencies:
10+
pre:
11+
- google-chrome --version
12+
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
13+
- sudo dpkg -i google-chrome.deb || true
14+
- sudo apt-get update
15+
- sudo apt-get install -f
16+
- sudo apt-get install --only-upgrade lsb-base
17+
- sudo dpkg -i google-chrome.deb
18+
- google-chrome --version

0 commit comments

Comments
 (0)