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

Commit 65dc161

Browse files
authored
feat: enable upnp nat hole punching (#3426)
On by default, set [Swarm.DisableNatPortMap](https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#swarmdisablenatportmap) to `true` to disable.
1 parent c7110db commit 65dc161

File tree

12 files changed

+55
-10
lines changed

12 files changed

+55
-10
lines changed

docs/CONFIG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The js-ipfs config file is a JSON document located in the root directory of the
2626
- [`Enabled`](#enabled)
2727
- [`Swarm`](#swarm-1)
2828
- [`ConnMgr`](#connmgr)
29+
- [`DisableNatPortMap`](#disablenatportmap)
2930
- [Example](#example)
3031
- [`API`](#api-1)
3132
- [`HTTPHeaders`](#httpheaders)
@@ -269,6 +270,12 @@ The "basic" connection manager tries to keep between `LowWater` and `HighWater`
269270
1. Keeping all connections until `HighWater` connections is reached.
270271
2. Once `HighWater` is reached, it closes connections until `LowWater` is reached.
271272

273+
### `DisableNatPortMap`
274+
275+
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.
276+
277+
Set `DisableNatPortMap` to `false` to disable this behaviour.
278+
272279
### Example
273280

274281
```json
@@ -278,7 +285,8 @@ The "basic" connection manager tries to keep between `LowWater` and `HighWater`
278285
"LowWater": 100,
279286
"HighWater": 200,
280287
}
281-
}
288+
},
289+
"DisableNatPortMap": false
282290
}
283291
```
284292

examples/custom-libp2p/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"license": "MIT",
1212
"dependencies": {
1313
"ipfs": "^0.53.2",
14-
"libp2p": "^0.30.0",
14+
"libp2p": "^0.30.6",
1515
"libp2p-bootstrap": "^0.12.1",
1616
"libp2p-kad-dht": "^0.20.1",
1717
"libp2p-mdns": "^0.15.0",

packages/ipfs-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"it-first": "^1.0.4",
9090
"it-last": "^1.0.4",
9191
"it-pipe": "^1.1.0",
92-
"libp2p": "^0.30.0",
92+
"libp2p": "^0.30.6",
9393
"libp2p-bootstrap": "^0.12.1",
9494
"libp2p-crypto": "^0.19.0",
9595
"libp2p-floodsub": "^0.24.1",

packages/ipfs-core/src/components/config.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,35 @@ async function listProfiles (_options) { // eslint-disable-line require-await
108108

109109
const profiles = {
110110
server: {
111-
description: 'Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery in local networks.',
111+
description: 'Recommended for nodes with public IPv4 address (servers, VPSes, etc.), disables host and content discovery and UPnP in local networks.',
112112
/**
113113
* @param {IPFSConfig} config
114114
* @returns {IPFSConfig}
115115
*/
116116
transform: (config) => {
117117
config.Discovery.MDNS.Enabled = false
118118
config.Discovery.webRTCStar.Enabled = false
119+
config.Swarm = {
120+
...(config.Swarm || {}),
121+
DisableNatPortMap: true
122+
}
119123

120124
return config
121125
}
122126
},
123127
'local-discovery': {
124-
description: 'Sets default values to fields affected by `server` profile, enables discovery in local networks.',
128+
description: 'Sets default values to fields affected by `server` profile, enables discovery and UPnP in local networks.',
125129
/**
126130
* @param {IPFSConfig} config
127131
* @returns {IPFSConfig}
128132
*/
129133
transform: (config) => {
130134
config.Discovery.MDNS.Enabled = true
131135
config.Discovery.webRTCStar.Enabled = true
136+
config.Swarm = {
137+
...(config.Swarm || {}),
138+
DisableNatPortMap: false
139+
}
132140

133141
return config
134142
}
@@ -149,6 +157,10 @@ const profiles = {
149157
config.Bootstrap = []
150158
config.Discovery.MDNS.Enabled = false
151159
config.Discovery.webRTCStar.Enabled = false
160+
config.Swarm = {
161+
...(config.Swarm || {}),
162+
DisableNatPortMap: true
163+
}
152164

153165
return config
154166
}
@@ -169,6 +181,10 @@ const profiles = {
169181
config.Bootstrap = defaultConfig.Bootstrap
170182
config.Discovery.MDNS.Enabled = defaultConfig.Discovery.MDNS.Enabled
171183
config.Discovery.webRTCStar.Enabled = defaultConfig.Discovery.webRTCStar.Enabled
184+
config.Swarm = {
185+
...(config.Swarm || {}),
186+
DisableNatPortMap: false
187+
}
172188

173189
return config
174190
}
@@ -483,6 +499,7 @@ module.exports.profiles = profiles
483499
* @typedef {Object} SwarmConfig
484500
* Options for configuring the swarm.
485501
* @property {ConnMgrConfig} [ConnMgr]
502+
* @property {boolean} [DisableNatPortMap]
486503
*
487504
* @typedef {Object} ConnMgrConfig
488505
* The connection manager determines which and how many connections to keep and

packages/ipfs-core/src/components/id.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = ({ peerId, network }) => {
3232
if (net) {
3333
const { libp2p } = net
3434
// only available while the node is running
35-
addresses = libp2p.transportManager.getAddrs()
35+
addresses = libp2p.multiaddrs
3636
protocols = Array.from(libp2p.upgrader.protocols.keys())
3737
}
3838

packages/ipfs-core/src/components/libp2p.js

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
108108
pubsub: {
109109
enabled: get(options, 'config.Pubsub.Enabled',
110110
get(config, 'Pubsub.Enabled', true))
111+
},
112+
nat: {
113+
enabled: get(options, 'nat.enabled', !get(config, 'Swarm.DisableNatPortMap', false)),
114+
ttl: get(options, 'nat.ttl', 7200),
115+
autoUpdate: get(options, 'nat.autoUpdate', true),
116+
gateway: get(options, 'nat.gateway'),
117+
externalIp: get(options, 'nat.externalIp'),
118+
pmp: {
119+
enabled: get(options, 'nat.pmp.enabled', false)
120+
}
111121
}
112122
},
113123
addresses: {

packages/ipfs-core/src/components/network.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Network {
4444

4545
await libp2p.start()
4646

47-
for (const ma of libp2p.transportManager.getAddrs()) {
47+
for (const ma of libp2p.multiaddrs) {
4848
print(`Swarm listening on ${ma}/p2p/${peerId.toB58String()}`)
4949
}
5050

packages/ipfs-core/src/runtime/config-browser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ module.exports = () => ({
4242
ConnMgr: {
4343
LowWater: 200,
4444
HighWater: 500
45-
}
45+
},
46+
DisableNatPortMap: true
4647
},
4748
Routing: {
4849
Type: 'none'

packages/ipfs-core/src/runtime/config-nodejs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = () => ({
4747
ConnMgr: {
4848
LowWater: 200,
4949
HighWater: 500
50-
}
50+
},
51+
DisableNatPortMap: false
5152
},
5253
Routing: {
5354
Type: 'none'

packages/ipfs-core/src/runtime/libp2p-browser.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module.exports = () => {
5959
pubsub: {
6060
enabled: true,
6161
emitSelf: true
62+
},
63+
nat: {
64+
enabled: false
6265
}
6366
},
6467
metrics: {

packages/ipfs-core/src/runtime/libp2p-nodejs.js

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const GossipSub = require('libp2p-gossipsub')
88
const Multiplex = require('libp2p-mplex')
99
const { NOISE } = require('libp2p-noise')
1010
const ipnsUtils = require('../ipns/routing/utils')
11+
const os = require('os')
1112

1213
module.exports = () => {
1314
return {
@@ -63,6 +64,10 @@ module.exports = () => {
6364
pubsub: {
6465
enabled: true,
6566
emitSelf: true
67+
},
68+
nat: {
69+
enabled: true,
70+
description: `ipfs@${os.hostname()}`
6671
}
6772
},
6873
metrics: {

packages/ipfs-daemon/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"ipfs-http-server": "^0.2.2",
3939
"ipfs-utils": "^5.0.0",
4040
"just-safe-set": "^2.1.0",
41-
"libp2p": "^0.30.0",
41+
"libp2p": "^0.30.6",
4242
"libp2p-delegated-content-routing": "^0.8.0",
4343
"libp2p-delegated-peer-routing": "^0.8.0",
4444
"libp2p-webrtc-star": "^0.20.1",

0 commit comments

Comments
 (0)