Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle router mappings of mixed IP version #2858

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"test:webkit": "aegir test -t browser -- --browser webkit"
},
"dependencies": {
"@chainsafe/is-ip": "^2.0.2",
"@libp2p/crypto": "^5.0.7",
"@libp2p/interface": "^2.2.1",
"@libp2p/interface-internal": "^2.1.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/libp2p/src/address-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isIPv4 } from '@chainsafe/is-ip'
import { peerIdFromString } from '@libp2p/peer-id'
import { debounce } from '@libp2p/utils/debounce'
import { multiaddr, protocols } from '@multiformats/multiaddr'
Expand Down Expand Up @@ -149,7 +150,9 @@
this.components.peerStore.patch(this.components.peerId, {
multiaddrs: addrs
})
.catch(err => { this.log.error('error updating addresses', err) })
.catch(err => {
this.log.error('error updating addresses', err)

Check warning on line 154 in packages/libp2p/src/address-manager.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/address-manager.ts#L154

Added line #L154 was not covered by tests
})
}

/**
Expand Down Expand Up @@ -267,6 +270,7 @@
}

mappings.forEach(mapping => {
tuples[0][0] = isIPv4(mapping.externalIp) ? CODEC_IP4 : CODEC_IP6
tuples[0][1] = mapping.externalIp
tuples[1][1] = `${mapping.externalPort}`

Expand Down
72 changes: 72 additions & 0 deletions packages/libp2p/test/addresses/address-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,76 @@ describe('Address Manager', () => {
multiaddr(`/ip6/${internalIp}/${protocol}/${internalPort}/p2p/${peerId.toString()}`)
])
})

it('should add a public IPv4 address mapping when only local IPv6 addresses are present', () => {
const transportManager = stubInterface<TransportManager>()
const am = new AddressManager({
peerId,
transportManager,
peerStore,
events,
logger: defaultLogger()
})

const internalIp = '2a00:23c6:14b1:7e00:28b8:30d:944e:27f3'
const internalPort = 4567
const externalIp = '81.12.12.1'
const externalPort = 8910
const protocol = 'tcp'

am.addPublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

// one loopback, one LAN address
transportManager.getAddrs.returns([
multiaddr(`/ip6/${internalIp}/${protocol}/${internalPort}`)
])

// should have mapped the LAN address to the external IP
expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip6/${internalIp}/${protocol}/${internalPort}/p2p/${peerId.toString()}`),
multiaddr(`/ip4/${externalIp}/${protocol}/${externalPort}/p2p/${peerId.toString()}`)
])

am.removePublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip6/${internalIp}/${protocol}/${internalPort}/p2p/${peerId.toString()}`)
])
})

it('should add a public IPv6 address mapping when only local IPv4 addresses are present', () => {
const transportManager = stubInterface<TransportManager>()
const am = new AddressManager({
peerId,
transportManager,
peerStore,
events,
logger: defaultLogger()
})

const internalIp = '192.168.1.123'
const internalPort = 4567
const externalIp = '2a00:23c6:14b1:7e00:28b8:30d:944e:27f3'
const externalPort = 8910
const protocol = 'tcp'

am.addPublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

// one loopback, one LAN address
transportManager.getAddrs.returns([
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}`)
])

// should have mapped the LAN address to the external IP
expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId.toString()}`),
multiaddr(`/ip6/${externalIp}/${protocol}/${externalPort}/p2p/${peerId.toString()}`)
])

am.removePublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId.toString()}`)
])
})
})
Loading