From f406dd50dbef2716ead74c624864f08ad0322620 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Mon, 12 Nov 2018 17:56:47 +0100 Subject: [PATCH] fix: dont dial an address that we have --- src/transport.js | 22 +++++++++++++++++++--- test/dialSelf.spec.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/transport.js b/src/transport.js index db11567..5635531 100644 --- a/src/transport.js +++ b/src/transport.js @@ -97,7 +97,7 @@ class TransportManager { } // filter the multiaddrs that are actually valid for this transport - multiaddrs = TransportManager.dialables(transport, multiaddrs) + multiaddrs = TransportManager.dialables(transport, multiaddrs, this.switch._peerInfo) log('dialing %s', key, multiaddrs.map((m) => m.toString())) // dial each of the multiaddrs with the given transport @@ -197,10 +197,26 @@ class TransportManager { * * @param {Transport} transport * @param {Array} multiaddrs + * @param {PeerInfo} peerInfo Optional - a peer whose addresses should not be returned * @returns {Array} */ - static dialables (transport, multiaddrs) { - return transport.filter(multiaddrs) + static dialables (transport, multiaddrs, peerInfo) { + const transportAddrs = transport.filter(multiaddrs) + if (!peerInfo) { + return transportAddrs + } + + return transportAddrs.filter((addr) => { + // If our address is in the destination address, filter it out + return !peerInfo.multiaddrs.toArray().find((pAddr) => { + try { + addr.decapsulate(pAddr) + } catch (err) { + return false + } + return true + }) + }) } } diff --git a/test/dialSelf.spec.js b/test/dialSelf.spec.js index 86f20e4..844979e 100644 --- a/test/dialSelf.spec.js +++ b/test/dialSelf.spec.js @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +const { EventEmitter } = require('events') const PeerBook = require('peer-book') const Duplex = require('pull-pair/duplex') @@ -14,8 +15,9 @@ const utils = require('./utils') const createInfos = utils.createInfos const Swarm = require('../src') -class MockTransport { +class MockTransport extends EventEmitter { constructor () { + super() this.conn = Duplex() } dial (addr, cb) { @@ -34,13 +36,20 @@ class MockTransport { describe(`dial self`, () => { let swarmA + let peerInfos - before((done) => createInfos(3, (err, infos) => { + before((done) => createInfos(2, (err, infos) => { expect(err).to.not.exist() - const peerA = infos[0] + const peerA = infos.shift() + peerInfos = infos peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') + peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/ipfs/${peerA.id.toB58String()}`) + peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`) + peerA.multiaddrs.add('/ip4/0.0.0.0/tcp/9001') + peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerA.id.toB58String()}`) + peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`) swarmA = new Swarm(peerA, new PeerBook()) @@ -59,4 +68,18 @@ describe(`dial self`, () => { done() }) }) + + it('node should not be able to dial another peers address that matches its own', (done) => { + const peerB = peerInfos.shift() + peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') + peerB.multiaddrs.add('/ip4/0.0.0.0/tcp/9001') + peerB.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerB.id.toB58String()}`) + + swarmA.dial(peerB, (err, conn) => { + expect(err).to.exist() + expect(err.code).to.eql('CONNECTION_FAILED') + expect(conn).to.not.exist() + done() + }) + }) })