@@ -14,6 +14,7 @@ const PeerInfo = require('peer-info')
14
14
const PeerBook = require ( 'peer-book' )
15
15
const mafmt = require ( 'mafmt' )
16
16
const multiaddr = require ( 'multiaddr' )
17
+ const Circuit = require ( 'libp2p-circuit' )
17
18
18
19
exports = module . exports
19
20
@@ -29,6 +30,7 @@ class Node extends EventEmitter {
29
30
this . peerInfo = _peerInfo
30
31
this . peerBook = _peerBook || new PeerBook ( )
31
32
this . isOnline = false
33
+ this . relayCircuit = null
32
34
33
35
this . swarm = new Swarm ( this . peerInfo , this . peerBook )
34
36
@@ -41,6 +43,24 @@ class Node extends EventEmitter {
41
43
// If muxer exists, we can use Identify
42
44
this . swarm . connection . reuse ( )
43
45
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
+
44
64
// Received incommind dial and muxer upgrade happened,
45
65
// reuse this muxed connection
46
66
this . swarm . on ( 'peer-mux-established' , ( peerInfo ) => {
@@ -152,7 +172,7 @@ class Node extends EventEmitter {
152
172
this . swarm . transport . add (
153
173
transport . tag || transport . constructor . name , transport )
154
174
} else if ( transport . constructor &&
155
- transport . constructor . name === 'WebSockets' ) {
175
+ transport . constructor . name === 'WebSockets' ) {
156
176
// TODO find a cleaner way to signal that a transport is always
157
177
// used for dialing, even if no listener
158
178
ws = transport
@@ -274,7 +294,7 @@ class Node extends EventEmitter {
274
294
// PeerInfo
275
295
if ( PeerInfo . isPeerInfo ( peer ) ) {
276
296
p = peer
277
- // Multiaddr instance (not string)
297
+ // Multiaddr instance (not string)
278
298
} else if ( multiaddr . isMultiaddr ( peer ) ) {
279
299
const peerIdB58Str = peer . getPeerId ( )
280
300
try {
@@ -283,7 +303,7 @@ class Node extends EventEmitter {
283
303
p = new PeerInfo ( PeerId . createFromB58String ( peerIdB58Str ) )
284
304
}
285
305
p . multiaddrs . add ( peer )
286
- // PeerId
306
+ // PeerId
287
307
} else if ( PeerId . isPeerId ( peer ) ) {
288
308
const peerIdB58Str = peer . toB58String ( )
289
309
try {
0 commit comments