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

Commit

Permalink
fix: dont dial an address that we have
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Nov 15, 2018
1 parent e757cf6 commit f406dd5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,10 +197,26 @@ class TransportManager {
*
* @param {Transport} transport
* @param {Array<Multiaddr>} multiaddrs
* @param {PeerInfo} peerInfo Optional - a peer whose addresses should not be returned
* @returns {Array<Multiaddr>}
*/
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
})
})
}
}

Expand Down
29 changes: 26 additions & 3 deletions test/dialSelf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ 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')

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) {
Expand All @@ -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())

Expand All @@ -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()
})
})
})

0 comments on commit f406dd5

Please sign in to comment.