Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
fix: libp2p/js-libp2p#189 Prevent self-dial
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 authored and Jacob Heun committed May 22, 2018
1 parent 36bc50b commit 5cdec34
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const observeConnection = require('./observe-connection')
* @param {function(Error, Connection)} callback
*/
class Dialer {
constructor (_switch, peerInfo, protocol, callback) {
constructor (_switch, peerInfo, ourPeerInfo, protocol, callback) {
this.switch = _switch
this.peerInfo = peerInfo
this.ourPeerInfo = ourPeerInfo
this.protocol = protocol
this.callback = callback
}
Expand Down Expand Up @@ -72,6 +73,9 @@ class Dialer {
_establishConnection (callback) {
const b58Id = this.peerInfo.id.toB58String()
log('dialing %s', b58Id)
if (b58Id === this.ourPeerInfo.id.toB58String()) {
return callback(new Error('A node cannot dial itself'))
}

waterfall([
(cb) => {
Expand Down Expand Up @@ -385,7 +389,7 @@ function dial (_switch) {
callback = callback || function noop () {}

const peerInfo = getPeerInfo(peer, _switch._peerBook)
const dialer = new Dialer(_switch, peerInfo, protocol, callback)
const dialer = new Dialer(_switch, peerInfo, _switch._peerInfo, protocol, callback)

return dialer.dial()
}
Expand Down

0 comments on commit 5cdec34

Please sign in to comment.