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: confirm dns mappings with ip mappings #2861

Merged
merged 1 commit into from
Nov 29, 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
47 changes: 38 additions & 9 deletions packages/libp2p/src/address-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
externalPort: number
}

interface DNSMapping {
domain: string
confident: boolean
}

export class AddressManager implements AddressManagerInterface {
private readonly log: Logger
private readonly components: AddressManagerComponents
Expand All @@ -96,7 +101,7 @@
private readonly appendAnnounce: Set<string>
private readonly observed: Map<string, ObservedAddressMetadata>
private readonly announceFilter: AddressFilter
private readonly ipDomainMappings: Map<string, string>
private readonly ipDomainMappings: Map<string, DNSMapping>
private readonly publicAddressMappings: Map<string, PublicAddressMapping[]>

/**
Expand Down Expand Up @@ -269,7 +274,7 @@
return
}

mappings.forEach(mapping => {
for (const mapping of mappings) {
tuples[0][0] = isIPv4(mapping.externalIp) ? CODEC_IP4 : CODEC_IP6
tuples[0][1] = mapping.externalIp
tuples[1][1] = `${mapping.externalPort}`
Expand All @@ -284,7 +289,7 @@
}).join('/')
}`)
)
})
}
})
multiaddrs = multiaddrs.concat(ipMappedMultiaddrs)

Expand All @@ -294,21 +299,25 @@
const tuples = ma.stringTuples()
let mappedIp = false

for (const [ip, domain] of this.ipDomainMappings.entries()) {
for (const [ip, mapping] of this.ipDomainMappings.entries()) {
if (!mapping.confident) {
continue
}

for (let i = 0; i < tuples.length; i++) {
if (tuples[i][1] !== ip) {
continue
}

if (tuples[i][0] === CODEC_IP4) {
tuples[i][0] = CODEC_DNS4
tuples[i][1] = domain
tuples[i][1] = mapping.domain
mappedIp = true
}

if (tuples[i][0] === CODEC_IP6) {
tuples[i][0] = CODEC_DNS6
tuples[i][1] = domain
tuples[i][1] = mapping.domain
mappedIp = true
}
}
Expand Down Expand Up @@ -366,14 +375,23 @@
addDNSMapping (domain: string, addresses: string[]): void {
addresses.forEach(ip => {
this.log('add DNS mapping %s to %s', ip, domain)
this.ipDomainMappings.set(ip, domain)

// check ip/public ip mappings to see if we think we are contactable
const confident = [...this.publicAddressMappings.entries()].some(([key, mappings]) => {
return mappings.some(mapping => mapping.externalIp === ip)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L381 was not covered by tests
})

this.ipDomainMappings.set(ip, {
domain,
confident
})
})
this._updatePeerStoreAddresses()
}

removeDNSMapping (domain: string): void {
for (const [key, value] of this.ipDomainMappings.entries()) {
if (value === domain) {
for (const [key, mapping] of this.ipDomainMappings.entries()) {
if (mapping.domain === domain) {
this.log('remove DNS mapping for %s', domain)
this.ipDomainMappings.delete(key)
}
Expand All @@ -390,6 +408,17 @@
})

this.publicAddressMappings.set(key, mappings)

// update domain mappings to indicate we are now confident that any matching
// ip/domain combination can now be resolved externally
for (const [key, mapping] of this.ipDomainMappings.entries()) {
if (key === externalIp) {
mapping.confident = true

this.ipDomainMappings.set(key, mapping)
}
}

this._updatePeerStoreAddresses()
}

Expand Down
121 changes: 85 additions & 36 deletions packages/libp2p/test/addresses/address-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,97 +257,146 @@ describe('Address Manager', () => {
})

it('should add an IPv4 DNS mapping', () => {
const transportManager = stubInterface<TransportManager>()

const am = new AddressManager({
peerId,
transportManager: stubInterface<TransportManager>({
getAddrs: () => []
}),
transportManager,
peerStore,
events,
logger: defaultLogger()
})

expect(am.getAddresses()).to.be.empty()

const externalIp = '81.12.12.1'
const externalAddress = multiaddr(`/ip4/${externalIp}/tcp/1234`)
const internalIp = '192.168.1.123'
const internalPort = 1234
const protocol = 'tcp'

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

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

const domain = 'example.com'
const externalIp = '81.12.12.1'
const externalPort = 4566

am.addDNSMapping(domain, [externalIp])

// have not verified DNS mapping so it is not included
expect(am.getAddresses()).to.deep.equal([
externalAddress.encapsulate(`/p2p/${peerId.toString()}`),
multiaddr(`/dns4/${domain}/tcp/1234/p2p/${peerId.toString()}`)
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`)
])

// public address mapping confirms DNS mapping
am.addPublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${externalIp}/tcp/${externalPort}/p2p/${peerId}`),
multiaddr(`/dns4/${domain}/tcp/${externalPort}/p2p/${peerId}`)
])
})

it('should add an IPv6 DNS mapping', () => {
const transportManager = stubInterface<TransportManager>()

const am = new AddressManager({
peerId,
transportManager: stubInterface<TransportManager>({
getAddrs: () => []
}),
transportManager,
peerStore,
events,
logger: defaultLogger()
})

expect(am.getAddresses()).to.be.empty()

const externalIp = 'fe80::7c98:a9ff:fe94'
const externalAddress = multiaddr(`/ip6/${externalIp}/tcp/1234`)
const internalIp = '192.168.1.123'
const internalPort = 1234
const protocol = 'tcp'

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

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

const domain = 'example.com'
const externalIp = '2a00:23c6:14b1:7e00:c010:8ecf:2a25:dcd1'
const externalPort = 4566

am.addDNSMapping(domain, [externalIp])

// have not verified DNS mapping so it is not included
expect(am.getAddresses()).to.deep.equal([
externalAddress.encapsulate(`/p2p/${peerId.toString()}`),
multiaddr(`/dns6/${domain}/tcp/1234/p2p/${peerId.toString()}`)
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`)
])

// public address mapping confirms DNS mapping
am.addPublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip6/${externalIp}/tcp/${externalPort}/p2p/${peerId}`),
multiaddr(`/dns6/${domain}/tcp/${externalPort}/p2p/${peerId}`)
])
})

it('should remove add a DNS mapping', () => {
it('should remove a DNS mapping', () => {
const transportManager = stubInterface<TransportManager>()

const am = new AddressManager({
peerId,
transportManager: stubInterface<TransportManager>({
getAddrs: () => []
}),
transportManager,
peerStore,
events,
logger: defaultLogger()
})

expect(am.getAddresses()).to.be.empty()

const externalIp = '81.12.12.1'
const externalAddress = multiaddr(`/ip4/${externalIp}/tcp/1234`)

am.confirmObservedAddr(externalAddress)
const internalIp = '192.168.1.123'
const internalPort = 1234
const protocol = 'tcp'

expect(am.getAddresses()).to.deep.equal([externalAddress.encapsulate(`/p2p/${peerId.toString()}`)])
// one loopback, one LAN address
transportManager.getAddrs.returns([
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}`)
])

const domain = 'example.com'
const externalIp = '81.12.12.1'
const externalPort = 4566

am.addDNSMapping(domain, [externalIp])
am.addPublicAddressMapping(internalIp, internalPort, externalIp, externalPort, protocol)

expect(am.getAddresses()).to.deep.equal([
externalAddress.encapsulate(`/p2p/${peerId.toString()}`),
multiaddr(`/dns4/${domain}/tcp/1234/p2p/${peerId.toString()}`)
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${externalIp}/tcp/${externalPort}/p2p/${peerId}`),
multiaddr(`/dns4/${domain}/tcp/${externalPort}/p2p/${peerId}`)
])

// public address mapping confirms DNS mapping
am.removeDNSMapping(domain)

expect(am.getAddresses()).to.deep.equal([externalAddress.encapsulate(`/p2p/${peerId.toString()}`)])
expect(am.getAddresses()).to.deep.equal([
multiaddr(`/ip4/127.0.0.1/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${internalIp}/${protocol}/${internalPort}/p2p/${peerId}`),
multiaddr(`/ip4/${externalIp}/tcp/${externalPort}/p2p/${peerId}`)
])
})

it('should add a public IPv4 address mapping', () => {
Expand Down
Loading