Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

feat: enable upnp nat hole punching #3426

Merged
merged 8 commits into from
Jan 29, 2021
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
10 changes: 9 additions & 1 deletion docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The js-ipfs config file is a JSON document located in the root directory of the
- [`Enabled`](#enabled)
- [`Swarm`](#swarm-1)
- [`ConnMgr`](#connmgr)
- [`DisableNatPortMap`](#disablenatportmap)
- [Example](#example)
- [`API`](#api-1)
- [`HTTPHeaders`](#httpheaders)
Expand Down Expand Up @@ -269,6 +270,12 @@ The "basic" connection manager tries to keep between `LowWater` and `HighWater`
1. Keeping all connections until `HighWater` connections is reached.
2. Once `HighWater` is reached, it closes connections until `LowWater` is reached.

### `DisableNatPortMap`

By default when running under nodejs, libp2p will try to use [UPnP](https://en.wikipedia.org/wiki/Universal_Plug_and_Play) to open a random high port on your router for any TCP connections you have configured.

Set `DisableNatPortMap` to `false` to disable this behaviour.

### Example

```json
Expand All @@ -278,7 +285,8 @@ The "basic" connection manager tries to keep between `LowWater` and `HighWater`
"LowWater": 100,
"HighWater": 200,
}
}
},
"DisableNatPortMap": false
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"dependencies": {
"ipfs": "^0.53.2",
"libp2p": "^0.30.0",
"libp2p": "^0.30.6",
"libp2p-bootstrap": "^0.12.1",
"libp2p-kad-dht": "^0.20.1",
"libp2p-mdns": "^0.15.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"it-first": "^1.0.4",
"it-last": "^1.0.4",
"it-pipe": "^1.1.0",
"libp2p": "^0.30.0",
"libp2p": "^0.30.6",
"libp2p-bootstrap": "^0.12.1",
"libp2p-crypto": "^0.19.0",
"libp2p-floodsub": "^0.24.1",
Expand Down
21 changes: 19 additions & 2 deletions packages/ipfs-core/src/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,35 @@ async function listProfiles (_options) { // eslint-disable-line require-await

const profiles = {
server: {
description: 'Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery in local networks.',
description: 'Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery and UPnP in local networks.',
/**
* @param {IPFSConfig} config
* @returns {IPFSConfig}
*/
transform: (config) => {
config.Discovery.MDNS.Enabled = false
config.Discovery.webRTCStar.Enabled = false
config.Swarm = {
...(config.Swarm || {}),
DisableNatPortMap: true
}

return config
}
},
'local-discovery': {
description: 'Sets default values to fields affected by `server` profile, enables discovery in local networks.',
description: 'Sets default values to fields affected by `server` profile, enables discovery and UPnP in local networks.',
/**
* @param {IPFSConfig} config
* @returns {IPFSConfig}
*/
transform: (config) => {
config.Discovery.MDNS.Enabled = true
config.Discovery.webRTCStar.Enabled = true
config.Swarm = {
...(config.Swarm || {}),
DisableNatPortMap: false
}

return config
}
Expand All @@ -149,6 +157,10 @@ const profiles = {
config.Bootstrap = []
config.Discovery.MDNS.Enabled = false
config.Discovery.webRTCStar.Enabled = false
config.Swarm = {
...(config.Swarm || {}),
DisableNatPortMap: true
}

return config
}
Expand All @@ -169,6 +181,10 @@ const profiles = {
config.Bootstrap = defaultConfig.Bootstrap
config.Discovery.MDNS.Enabled = defaultConfig.Discovery.MDNS.Enabled
config.Discovery.webRTCStar.Enabled = defaultConfig.Discovery.webRTCStar.Enabled
config.Swarm = {
...(config.Swarm || {}),
DisableNatPortMap: false
}

return config
}
Expand Down Expand Up @@ -483,6 +499,7 @@ module.exports.profiles = profiles
* @typedef {Object} SwarmConfig
* Options for configuring the swarm.
* @property {ConnMgrConfig} [ConnMgr]
* @property {boolean} [DisableNatPortMap]
*
* @typedef {Object} ConnMgrConfig
* The connection manager determines which and how many connections to keep and
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/src/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = ({ peerId, network }) => {
if (net) {
const { libp2p } = net
// only available while the node is running
addresses = libp2p.transportManager.getAddrs()
addresses = libp2p.multiaddrs
protocols = Array.from(libp2p.upgrader.protocols.keys())
}

Expand Down
10 changes: 10 additions & 0 deletions packages/ipfs-core/src/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
pubsub: {
enabled: get(options, 'config.Pubsub.Enabled',
get(config, 'Pubsub.Enabled', true))
},
nat: {
enabled: get(options, 'nat.enabled', !get(config, 'Swarm.DisableNatPortMap', false)),
ttl: get(options, 'nat.ttl', 7200),
autoUpdate: get(options, 'nat.autoUpdate', true),
gateway: get(options, 'nat.gateway'),
externalIp: get(options, 'nat.externalIp'),
pmp: {
enabled: get(options, 'nat.pmp.enabled', false)
}
}
},
addresses: {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/src/components/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Network {

await libp2p.start()

for (const ma of libp2p.transportManager.getAddrs()) {
for (const ma of libp2p.multiaddrs) {
print(`Swarm listening on ${ma}/p2p/${peerId.toB58String()}`)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-core/src/runtime/config-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module.exports = () => ({
ConnMgr: {
LowWater: 200,
HighWater: 500
}
},
DisableNatPortMap: true
},
Routing: {
Type: 'none'
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-core/src/runtime/config-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module.exports = () => ({
ConnMgr: {
LowWater: 200,
HighWater: 500
}
},
DisableNatPortMap: false
},
Routing: {
Type: 'none'
Expand Down
3 changes: 3 additions & 0 deletions packages/ipfs-core/src/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ module.exports = () => {
pubsub: {
enabled: true,
emitSelf: true
},
nat: {
enabled: false
}
},
metrics: {
Expand Down
5 changes: 5 additions & 0 deletions packages/ipfs-core/src/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const GossipSub = require('libp2p-gossipsub')
const Multiplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const ipnsUtils = require('../ipns/routing/utils')
const os = require('os')

module.exports = () => {
return {
Expand Down Expand Up @@ -63,6 +64,10 @@ module.exports = () => {
pubsub: {
enabled: true,
emitSelf: true
},
nat: {
enabled: true,
description: `ipfs@${os.hostname()}`
}
},
metrics: {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"ipfs-http-server": "^0.2.2",
"ipfs-utils": "^5.0.0",
"just-safe-set": "^2.1.0",
"libp2p": "^0.30.0",
"libp2p": "^0.30.6",
"libp2p-delegated-content-routing": "^0.8.0",
"libp2p-delegated-peer-routing": "^0.8.0",
"libp2p-webrtc-star": "^0.20.1",
Expand Down