Skip to content

Commit 86dd14c

Browse files
dignifiedquiredryajov
authored andcommitted
[WIP] Awesome DHT (#86)
* feat: integrate dht * better interfaces * docs: add documentation for peerRouting, contentRouting, dht * fix: take in passed datastore * fix: update usage of _getPeerInfo * fix: getPeerInfo * docs: update docs * moar feat: correctly handle p2p-circuit addrs when creating a peer info object refactor: rework config options
1 parent ce6b0e3 commit 86dd14c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/index.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const Ping = require('libp2p-ping')
1111
const Swarm = require('libp2p-swarm')
1212
const PeerId = require('peer-id')
1313
const PeerInfo = require('peer-info')
14-
const mafmt = require('mafmt')
1514
const PeerBook = require('peer-book')
1615
const mafmt = require('mafmt')
1716
const multiaddr = require('multiaddr')
@@ -44,23 +43,13 @@ class Node extends EventEmitter {
4443
// If muxer exists, we can use Identify
4544
this.swarm.connection.reuse()
4645

47-
// enable circuit relaying
48-
// TODO: move defaults elsewhere
49-
_options.Relay = Object.assign({
50-
Circuit: {
51-
Enabled: false,
52-
Active: false
53-
},
54-
DialMode: 'onion'
55-
}, _options.Relay)
56-
57-
if (_options.Relay.Circuit.Enabled) {
58-
this.relayCircuit = new Circuit.Relay(_options.Relay.Circuit)
46+
if (_options.relay && _options.relay.circuit) {
47+
this.relayCircuit = new Circuit.Hop(_options.relay.circuit)
5948
this.relayCircuit.mount(this.swarm)
6049
}
6150

6251
// If muxer exists, we can use Relay for listening/dialing
63-
this.swarm.connection.relay(_options.Relay)
52+
this.swarm.connection.enableRelayDialing(_options.relay)
6453

6554
// Received incommind dial and muxer upgrade happened,
6655
// reuse this muxed connection
@@ -267,7 +256,6 @@ class Node extends EventEmitter {
267256

268257
dial (peer, protocol, callback) {
269258
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
270-
const peerInfo = this._getPeerInfo(peer)
271259

272260
if (typeof protocol === 'function') {
273261
callback = protocol
@@ -317,16 +305,24 @@ class Node extends EventEmitter {
317305
// PeerInfo
318306
if (PeerInfo.isPeerInfo(peer)) {
319307
p = peer
320-
// Multiaddr instance (not string)
308+
// Multiaddr instance (not string)
321309
} else if (multiaddr.isMultiaddr(peer)) {
322-
const peerIdB58Str = peer.getPeerId()
310+
let peerId
311+
if (peer.toString().includes('p2p-circuit')) {
312+
// last segment is always the peer ipfs segment
313+
peerId = multiaddr(peer.toString().split('p2p-circuit').pop())
314+
} else {
315+
peerId = peer
316+
}
317+
318+
const peerIdB58Str = peerId.getPeerId()
323319
try {
324320
p = this.peerBook.get(peerIdB58Str)
325321
} catch (err) {
326322
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
327323
}
328324
p.multiaddrs.add(peer)
329-
// PeerId
325+
// PeerId
330326
} else if (PeerId.isPeerId(peer)) {
331327
const peerIdB58Str = peer.toB58String()
332328
try {

0 commit comments

Comments
 (0)