feat: add UPnP NAT manager (#810)

* feat: add uPnP nat manager

Adds a really basic nat manager that attempts to use UPnP to punch
a hole through your router for any IPV4 tcp addresses you have
configured.

Adds any configured addresses to the node's observed addresses list
and adds observed addresses to `libp2p.multiaddrs` so we exchange
them with peers when performing `identify` and people can dial you.

Adds configuration options under `config.nat`

Hole punching is async to not affect start up time.

Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
This commit is contained in:
Alex Potsides
2021-01-27 13:55:26 +00:00
committed by GitHub
parent b5c9e48b68
commit 0a6bc0d101
16 changed files with 742 additions and 22 deletions

View File

@ -3,23 +3,32 @@
const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const AddressManager = require('../../src/address-manager')
const peerUtils = require('../utils/creators/peer')
const Peers = require('../fixtures/peers')
const listenAddresses = ['/ip4/127.0.0.1/tcp/15006/ws', '/ip4/127.0.0.1/tcp/15008/ws']
const announceAddreses = ['/dns4/peer.io']
describe('Address Manager', () => {
let peerId
before(async () => {
peerId = await PeerId.createFromJSON(Peers[0])
})
it('should not need any addresses', () => {
const am = new AddressManager()
const am = new AddressManager(peerId)
expect(am.listen.size).to.equal(0)
expect(am.announce.size).to.equal(0)
})
it('should return listen multiaddrs on get', () => {
const am = new AddressManager({
const am = new AddressManager(peerId, {
listen: listenAddresses
})
@ -33,7 +42,7 @@ describe('Address Manager', () => {
})
it('should return announce multiaddrs on get', () => {
const am = new AddressManager({
const am = new AddressManager(peerId, {
listen: listenAddresses,
announce: announceAddreses
})
@ -45,6 +54,75 @@ describe('Address Manager', () => {
expect(announceMultiaddrs.length).to.equal(1)
expect(announceMultiaddrs[0].equals(multiaddr(announceAddreses[0]))).to.equal(true)
})
it('should add observed addresses', () => {
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr('/ip4/123.123.123.123/tcp/39201')
expect(am.observed).to.have.property('size', 1)
})
it('should dedupe added observed addresses', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
expect(am.observed).to.have.property('size', 1)
expect(am.observed).to.include(ma)
})
it('should only emit one change:addresses event', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
let eventCount = 0
am.on('change:addresses', () => {
eventCount++
})
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`)
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`)
expect(eventCount).to.equal(1)
})
it('should strip our peer address from added observed addresses', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`)
expect(am.observed).to.have.property('size', 1)
expect(am.observed).to.include(ma)
})
it('should strip our peer address from added observed addresses in difference formats', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`) // base32 CID
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`) // base58btc
expect(am.observed).to.have.property('size', 1)
expect(am.observed).to.include(ma)
})
})
describe('libp2p.addressManager', () => {