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

Commit f406dd5

Browse files
committed
fix: dont dial an address that we have
1 parent e757cf6 commit f406dd5

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/transport.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class TransportManager {
9797
}
9898

9999
// filter the multiaddrs that are actually valid for this transport
100-
multiaddrs = TransportManager.dialables(transport, multiaddrs)
100+
multiaddrs = TransportManager.dialables(transport, multiaddrs, this.switch._peerInfo)
101101
log('dialing %s', key, multiaddrs.map((m) => m.toString()))
102102

103103
// dial each of the multiaddrs with the given transport
@@ -197,10 +197,26 @@ class TransportManager {
197197
*
198198
* @param {Transport} transport
199199
* @param {Array<Multiaddr>} multiaddrs
200+
* @param {PeerInfo} peerInfo Optional - a peer whose addresses should not be returned
200201
* @returns {Array<Multiaddr>}
201202
*/
202-
static dialables (transport, multiaddrs) {
203-
return transport.filter(multiaddrs)
203+
static dialables (transport, multiaddrs, peerInfo) {
204+
const transportAddrs = transport.filter(multiaddrs)
205+
if (!peerInfo) {
206+
return transportAddrs
207+
}
208+
209+
return transportAddrs.filter((addr) => {
210+
// If our address is in the destination address, filter it out
211+
return !peerInfo.multiaddrs.toArray().find((pAddr) => {
212+
try {
213+
addr.decapsulate(pAddr)
214+
} catch (err) {
215+
return false
216+
}
217+
return true
218+
})
219+
})
204220
}
205221
}
206222

test/dialSelf.spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99

10+
const { EventEmitter } = require('events')
1011
const PeerBook = require('peer-book')
1112
const Duplex = require('pull-pair/duplex')
1213

1314
const utils = require('./utils')
1415
const createInfos = utils.createInfos
1516
const Swarm = require('../src')
1617

17-
class MockTransport {
18+
class MockTransport extends EventEmitter {
1819
constructor () {
20+
super()
1921
this.conn = Duplex()
2022
}
2123
dial (addr, cb) {
@@ -34,13 +36,20 @@ class MockTransport {
3436

3537
describe(`dial self`, () => {
3638
let swarmA
39+
let peerInfos
3740

38-
before((done) => createInfos(3, (err, infos) => {
41+
before((done) => createInfos(2, (err, infos) => {
3942
expect(err).to.not.exist()
4043

41-
const peerA = infos[0]
44+
const peerA = infos.shift()
45+
peerInfos = infos
4246

4347
peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001')
48+
peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/ipfs/${peerA.id.toB58String()}`)
49+
peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`)
50+
peerA.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
51+
peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerA.id.toB58String()}`)
52+
peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`)
4453

4554
swarmA = new Swarm(peerA, new PeerBook())
4655

@@ -59,4 +68,18 @@ describe(`dial self`, () => {
5968
done()
6069
})
6170
})
71+
72+
it('node should not be able to dial another peers address that matches its own', (done) => {
73+
const peerB = peerInfos.shift()
74+
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9001')
75+
peerB.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
76+
peerB.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerB.id.toB58String()}`)
77+
78+
swarmA.dial(peerB, (err, conn) => {
79+
expect(err).to.exist()
80+
expect(err.code).to.eql('CONNECTION_FAILED')
81+
expect(conn).to.not.exist()
82+
done()
83+
})
84+
})
6285
})

0 commit comments

Comments
 (0)