Skip to content

Commit

Permalink
[WIP] Awesome DHT (#86)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dignifiedquire authored and dryajov committed Jul 9, 2017
1 parent ce6b0e3 commit 86dd14c
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const Ping = require('libp2p-ping')
const Swarm = require('libp2p-swarm')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const mafmt = require('mafmt')
const PeerBook = require('peer-book')
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
Expand Down Expand Up @@ -44,23 +43,13 @@ 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)
if (_options.relay && _options.relay.circuit) {
this.relayCircuit = new Circuit.Hop(_options.relay.circuit)
this.relayCircuit.mount(this.swarm)
}

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

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

dial (peer, protocol, callback) {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)

if (typeof protocol === 'function') {
callback = protocol
Expand Down Expand Up @@ -317,16 +305,24 @@ class Node extends EventEmitter {
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance (not string)
// Multiaddr instance (not string)
} else if (multiaddr.isMultiaddr(peer)) {
const peerIdB58Str = peer.getPeerId()
let peerId
if (peer.toString().includes('p2p-circuit')) {
// last segment is always the peer ipfs segment
peerId = multiaddr(peer.toString().split('p2p-circuit').pop())
} else {
peerId = peer
}

const peerIdB58Str = peerId.getPeerId()
try {
p = this.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)
// PeerId
// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
Expand Down

0 comments on commit 86dd14c

Please sign in to comment.