Skip to content

Commit 64a2967

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

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/index.js

+23-3
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) => {
@@ -152,7 +172,7 @@ class Node extends EventEmitter {
152172
this.swarm.transport.add(
153173
transport.tag || transport.constructor.name, transport)
154174
} else if (transport.constructor &&
155-
transport.constructor.name === 'WebSockets') {
175+
transport.constructor.name === 'WebSockets') {
156176
// TODO find a cleaner way to signal that a transport is always
157177
// used for dialing, even if no listener
158178
ws = transport
@@ -274,7 +294,7 @@ class Node extends EventEmitter {
274294
// PeerInfo
275295
if (PeerInfo.isPeerInfo(peer)) {
276296
p = peer
277-
// Multiaddr instance (not string)
297+
// Multiaddr instance (not string)
278298
} else if (multiaddr.isMultiaddr(peer)) {
279299
const peerIdB58Str = peer.getPeerId()
280300
try {
@@ -283,7 +303,7 @@ class Node extends EventEmitter {
283303
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
284304
}
285305
p.multiaddrs.add(peer)
286-
// PeerId
306+
// PeerId
287307
} else if (PeerId.isPeerId(peer)) {
288308
const peerIdB58Str = peer.toB58String()
289309
try {

0 commit comments

Comments
 (0)