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

@ -0,0 +1,46 @@
'use strict'
/* eslint-env mocha */
const Transport = require('libp2p-websockets')
const { NOISE: Crypto } = require('libp2p-noise')
const Libp2p = require('../../src')
const { createPeerId } = require('../utils/creators/peer')
describe('Consume peer record', () => {
let libp2p
beforeEach(async () => {
const [peerId] = await createPeerId()
const config = {
peerId,
modules: {
transport: [Transport],
connEncryption: [Crypto]
}
}
libp2p = await Libp2p.create(config)
})
afterEach(async () => {
await libp2p.stop()
})
it('should consume peer record when observed addrs are added', async () => {
let done
libp2p.peerStore.addressBook.consumePeerRecord = () => {
done()
}
const p = new Promise(resolve => {
done = resolve
})
libp2p.addressManager.addObservedAddr('/ip4/123.123.123.123/tcp/3983')
await p
libp2p.stop()
})
})