feat: Swarm.EnableHolePunching flag (#8562)
* feat: use Swarm.EnableHolePunching flag within libp2p * docs: Swarm.EnableHolePunching Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com>
Marten Seemann committed
Nov 30, 2021 at 23:18 UTC
48a6cac00bd1748fdef9d57b73ba41fa9ce7b865
3 files changed
+29
core/node/groups.go
+1
@@ -154,6 +154,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
154
fx.Invoke(libp2p.SetupDiscovery(cfg.Discovery.MDNS.Enabled, cfg.Discovery.MDNS.Interval)),
155
fx.Provide(libp2p.ForceReachability(cfg.Internal.Libp2pForceReachability)),
156
fx.Provide(libp2p.StaticRelays(cfg.Swarm.RelayClient.StaticRelays)),
157
+ fx.Provide(libp2p.HolePunching(cfg.Swarm.EnableHolePunching, cfg.Swarm.RelayClient.Enabled.WithDefault(false))),
158
159
fx.Provide(libp2p.Security(!bcfg.DisableEncryptedConnections, cfg.Swarm.Transports)),
160
core/node/libp2p/relay.go
+12
@@ -70,3 +70,15 @@ func AutoRelay(addDefaultRelays bool) func() (opts Libp2pOpts, err error) {
70
return
71
}
72
}
73
+
74
+func HolePunching(flag config.Flag, hasRelayClient bool) func() (opts Libp2pOpts, err error) {
75
+ return func() (opts Libp2pOpts, err error) {
76
+ if flag.WithDefault(false) {
77
+ if !hasRelayClient {
78
+ log.Fatal("To enable `Swarm.EnableHolePunching` requires `Swarm.RelayClient.Enabled` to be enabled.")
79
+ }
80
+ opts.Opts = append(opts.Opts, libp2p.EnableHolePunching())
81
+ }
82
+ return
83
+ }
84
+}
docs/config.md
+16
@@ -102,6 +102,7 @@ config file at runtime.
102
- [`Swarm.AddrFilters`](#swarmaddrfilters)
103
- [`Swarm.DisableBandwidthMetrics`](#swarmdisablebandwidthmetrics)
104
- [`Swarm.DisableNatPortMap`](#swarmdisablenatportmap)
105
+ - [`Swarm.EnableHolePunching`](#swarmenableholepunching)
106
- [`Swarm.EnableAutoRelay`](#swarmenableautorelay)
107
- [`Swarm.RelayClient`](#swarmrelayclient)
108
- [`Swarm.RelayClient.Enabled`](#swarmrelayclientenabled)
@@ -1314,6 +1315,21 @@ Default: `false`
1315
1316
Type: `bool`
1317
1318
+### `Swarm.EnableHolePunching`
1319
+
1320
+Enable hole punching for NAT traversal
1321
+when port forwarding is not possible.
1322
+
1323
+When enabled, go-ipfs will coordinate with the counterparty using
1324
+a [relayed connection](https://github.com/libp2p/specs/blob/master/relay/circuit-v2.md),
1325
+to [upgrade to a direct connection](https://github.com/libp2p/specs/blob/master/relay/DCUtR.md)
1326
+through a NAT/firewall whenever possible.
1327
+This feature requires `Swarm.RelayClient.Enabled` to be set to `true`.
1328
+
1329
+Default: `false`
1330
+
1331
+Type: `flag`
1332
+
1333
### `Swarm.EnableAutoRelay`
1334
1335
Deprecated: Set `Swarm.RelayClient.Enabled` to `true`.