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

[WIP] feat: implement relay circuit #1

Merged
merged 16 commits into from
Mar 27, 2017
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Logs
logs
*.log
npm-debug.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
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Vim editor swap files
*.swp

dist

.history
.vscode
93 changes: 93 additions & 0 deletions .history/src/transport/listener_20170305012002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict'

const config = require('../config')
const pull = require('pull-stream')
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const Peer = require('../peer')
const includes = require('lodash/includes')
const lp = require('pull-length-prefixed')
const handshake = require('pull-handshake')
const Connection = require('interface-connection').Connection

const multicodec = config.multicodec

const log = config.log

class Listener {
constructor(libp2p, handler) {
this.libp2p = libp2p
this.peers = new Map()
this.handler = handler

this._onConnection = this._onConnection.bind(this)
}

listen(cb) {
cb = cb || function() {}
this.libp2p.handle(multicodec, this._onConnection)
cb()
}

close(cb) {
cb = cb || function() {}
this.libp2p.unhandle(multicodec)
cb()
}

_onConnection(protocol, conn) {
conn.getPeerInfo((err, peerInfo) => {
if (err) {
log.err('Failed to identify incomming conn', err)
return pull(pull.empty(), conn)
}

const idB58Str = peerInfo.id.toB58String()
let relayPeer = this.peers.get(idB58Str)
if (!relayPeer) {
log('new relay peer', idB58Str)
relayPeer = peerInfo
this.peers.set(idB58Str, new Peer(conn, peerInfo))
}
this._processConnection(relayPeer, conn)
})
}

_processConnection(relayPeer, conn) {
let stream = handshake({ timeout: 1000 * 60 })
let shake = stream.handshake

lp.decodeFromReader(shake, (err, msg) => {
if (err) {
err(err)
return err
}

let addr = multiaddr(msg.toString())
let src
try {
PeerInfo.create(addr.peerId(), (err, peerInfo) => {
if (err) {
log.err(err)
return err
}

if (includes(addr.protoNames(), 'ipfs')) {
addr = addr.decapsulate('ipfs')
}

peerInfo.multiaddr.add(addr)
src = peerInfo
this.handler(new Connection(shake.rest(), peerInfo))
})
} catch (err) {
log.err(err)
}
})

pull(stream, conn, stream)
}

}

module.exports = Listener
35 changes: 35 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
test

# Logs
logs
*.log
npm-debug.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
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
sudo: false
language: node_js

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: "stable"
env:
- CXX=g++-4.8

# Make sure we have new NPM.
before_install:
- npm install -g npm

script:
- npm run lint
- npm test
- npm run coverage

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

addons:
firefox: latest
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 libp2p

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# js-libp2p-circuit
# <topic>

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://github.com/libp2p/libp2p)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

> <description>

<long description>

## Table of Contents

<ToC>

## Install

<install>

## Usage

<usage>

## Lead

- [<lead>](https://github.com/<lead>)

## Contribute

Please contribute! [Look at the issues](https://github.com/libp2p/<repoName>/issues)!

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).

Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

## License

[MIT](LICENSE) © 2016 Protocol Labs Inc.
12 changes: 12 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
1 change: 0 additions & 1 deletion implementation-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,4 @@ In order to minimize nesting, the first example is preferred
- A generic circuit address will be added to the peers multiaddr list
- i.e. `/p2p-circuit/ipfs/QmDest`
- If another transport is available, then use that instead of the relay


69 changes: 69 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "libp2p-circuit",
"version": "0.0.1-alpha.1",
"description": "JavaScript implementation of circuit/switch relaying",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test --env node",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
"pre-commit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-circuit.git"
},
"keywords": [
"IPFS"
],
"author": "Dmitriy Ryajov <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-circuit/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p-circuit#readme",
"eslintConfig": {
"extends": [
"./node_modules/aegir/config/eslintrc.yml"
]
},
"devDependencies": {
"aegir": "^10.0.0",
"chai": "^3.5.0",
"libp2p-ipfs-browser": "^0.19.0",
"libp2p-ipfs-nodejs": "^0.19.0",
"libp2p-mdns": "^0.6.1",
"libp2p-multiplex": "^0.4.1",
"libp2p-railing": "^0.4.1",
"libp2p-secio": "^0.6.7",
"libp2p-spdy": "^0.10.4",
"libp2p-swarm": "^0.26.18",
"libp2p-tcp": "^0.9.3",
"libp2p-webrtc-star": "^0.8.8",
"libp2p-websockets": "^0.9.2",
"peer-id": "^0.8.2",
"peer-info": "^0.8.3",
"pre-commit": "^1.2.2"
},
"contributors": [],
"dependencies": {
"async": "^2.1.5",
"debug": "^2.6.1",
"interface-connection": "^0.3.1",
"lodash": "^4.17.4",
"multiaddr": "^2.2.1",
"multistream-select": "^0.13.4",
"pull-abortable": "^4.1.0",
"pull-handshake": "^1.1.4",
"pull-stream": "^3.5.0",
"setimmediate": "^1.0.5"
}
}
Loading