Skip to content

Commit

Permalink
feat: adding circuit relaying
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Oct 23, 2017
1 parent 2504cbe commit ad1e6ff
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 0 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@
"pull-serializer": "^0.3.2",
"pull-stream": "^3.6.1",
"safe-buffer": "^5.1.1",
"sinon": "^2.3.6",
"electron-webrtc": "^0.3.0",
"wrtc": "0.0.62"
},
"dependencies": {
"async": "^2.5.0",
"libp2p-ping": "~0.4.0",
"libp2p-swarm": "~0.29.1",
"libp2p-circuit": "~0.0.1",
"mafmt": "^2.1.8",
"multiaddr": "^2.3.0",
"peer-book": "~0.4.0",
"peer-id": "~0.8.7",
"peer-info": "~0.9.2"
},
"contributors": [
"Chris Bratlien <chrisbratlien@gmail.com>",
"Daijiro Wachi <daijiro.wachi@gmail.com>",
Expand Down
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
const Circuit = require('libp2p-circuit')

exports = module.exports

Expand All @@ -31,6 +32,7 @@ class Node extends EventEmitter {
_options = _options || {}

this._isStarted = false
this.relayCircuit = null

this.swarm = new Swarm(this.peerInfo, this.peerBook)

Expand All @@ -43,6 +45,24 @@ class Node extends EventEmitter {
// If muxer exists, we can use Identify
this.swarm.connection.reuse()

// enable circuit relaying
// TODO: move defaults elsewhere
_options.Relay = Object.assign({
Circuit: {
Enabled: false,
Active: false
},
DialMode: 'onion'
}, _options.Relay)

if (_options.Relay.Circuit.Enabled) {
this.relayCircuit = new Circuit.Relay(_options.Relay.Circuit)
this.relayCircuit.mount(this.swarm)
}

// If muxer exists, we can use Relay for listening/dialing
this.swarm.connection.relay(_options.Relay)

// Received incommind dial and muxer upgrade happened,
// reuse this muxed connection
this.swarm.on('peer-mux-established', (peerInfo) => {
Expand Down
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ require('./nodejs-bundle/stream-muxing')
require('./nodejs-bundle/discovery')
require('./nodejs-bundle/peer-routing')
require('./nodejs-bundle/content-routing')
require('./nodejs-bundle/circuit/circuit')
Loading

0 comments on commit ad1e6ff

Please sign in to comment.