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

Commit 5cdec34

Browse files
mkg20001Jacob Heun
authored andcommitted
fix: libp2p/js-libp2p#189 Prevent self-dial
1 parent 36bc50b commit 5cdec34

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/dial.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ const observeConnection = require('./observe-connection')
2323
* @param {function(Error, Connection)} callback
2424
*/
2525
class Dialer {
26-
constructor (_switch, peerInfo, protocol, callback) {
26+
constructor (_switch, peerInfo, ourPeerInfo, protocol, callback) {
2727
this.switch = _switch
2828
this.peerInfo = peerInfo
29+
this.ourPeerInfo = ourPeerInfo
2930
this.protocol = protocol
3031
this.callback = callback
3132
}
@@ -72,6 +73,9 @@ class Dialer {
7273
_establishConnection (callback) {
7374
const b58Id = this.peerInfo.id.toB58String()
7475
log('dialing %s', b58Id)
76+
if (b58Id === this.ourPeerInfo.id.toB58String()) {
77+
return callback(new Error('A node cannot dial itself'))
78+
}
7579

7680
waterfall([
7781
(cb) => {
@@ -385,7 +389,7 @@ function dial (_switch) {
385389
callback = callback || function noop () {}
386390

387391
const peerInfo = getPeerInfo(peer, _switch._peerBook)
388-
const dialer = new Dialer(_switch, peerInfo, protocol, callback)
392+
const dialer = new Dialer(_switch, peerInfo, _switch._peerInfo, protocol, callback)
389393

390394
return dialer.dial()
391395
}

0 commit comments

Comments
 (0)