Skip to content

Commit c85c5c2

Browse files
committed
feat: adding circuit relaying
1 parent a5b7de1 commit c85c5c2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const PeerInfo = require('peer-info')
1414
const PeerBook = require('peer-book')
1515
const mafmt = require('mafmt')
1616
const multiaddr = require('multiaddr')
17+
const Circuit = require('libp2p-circuit')
1718

1819
exports = module.exports
1920

@@ -29,6 +30,7 @@ class Node extends EventEmitter {
2930
this.peerInfo = _peerInfo
3031
this.peerBook = _peerBook || new PeerBook()
3132
this.isOnline = false
33+
this.relayCircuit = null
3234

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

@@ -41,6 +43,24 @@ class Node extends EventEmitter {
4143
// If muxer exists, we can use Identify
4244
this.swarm.connection.reuse()
4345

46+
// enable circuit relaying
47+
// TODO: move defaults elsewhere
48+
_options.Relay = Object.assign({
49+
Circuit: {
50+
Enabled: false,
51+
Active: false
52+
},
53+
DialMode: 'onion'
54+
}, _options.Relay)
55+
56+
if (_options.Relay.Circuit.Enabled) {
57+
this.relayCircuit = new Circuit.Relay(_options.Relay.Circuit)
58+
this.relayCircuit.mount(this.swarm)
59+
}
60+
61+
// If muxer exists, we can use Relay for listening/dialing
62+
this.swarm.connection.relay(_options.Relay)
63+
4464
// Received incommind dial and muxer upgrade happened,
4565
// reuse this muxed connection
4666
this.swarm.on('peer-mux-established', (peerInfo) => {

0 commit comments

Comments
 (0)