Skip to content

Commit

Permalink
refactor(config): cleanup relay handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Apr 19, 2022
1 parent e395f42 commit 8a72aa0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
47 changes: 24 additions & 23 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,39 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
autonat = fx.Provide(libp2p.AutoNATService(cfg.AutoNAT.Throttle))
}

// If `cfg.Swarm.DisableRelay` is set and `Network.RelayTransport` isn't, use the former.
enableRelayTransport := cfg.Swarm.Transports.Network.Relay.WithDefault(!cfg.Swarm.DisableRelay) // nolint
enableRelayTransport := cfg.Swarm.Transports.Network.Relay.WithDefault(true) // nolint
enableRelayService := cfg.Swarm.RelayService.Enabled.WithDefault(enableRelayTransport)
enableRelayClient := cfg.Swarm.RelayClient.Enabled.WithDefault(enableRelayTransport)

// Log error when relay subsystem could not be initialized due to missing dependency
if !enableRelayTransport {
if enableRelayService {
logger.Fatal("Failed to enable `Swarm.RelayService`, it requires `Swarm.Transports.Network.Relay` to be true.")
}
if enableRelayClient {
logger.Fatal("Failed to enable `Swarm.RelayClient`, it requires `Swarm.Transports.Network.Relay` to be true.")
}
}

// Warn about a deprecated option.
// Force users to migrate old config.
// nolint
if cfg.Swarm.DisableRelay {
logger.Error("The 'Swarm.DisableRelay' config field is deprecated.")
if enableRelayTransport {
logger.Error("'Swarm.DisableRelay' has been overridden by 'Swarm.Transports.Network.Relay'")
} else {
logger.Error("Use the 'Swarm.Transports.Network.Relay' config field instead")
}
logger.Fatal("The 'Swarm.DisableRelay' config field was removed." +
"Use the 'Swarm.Transports.Network.Relay' instead.")
}
// nolint
if cfg.Swarm.EnableAutoRelay {
logger.Error("The 'Swarm.EnableAutoRelay' config field is deprecated.")
if cfg.Swarm.RelayClient.Enabled == config.Default {
logger.Error("Use the 'Swarm.AutoRelay.Enabled' config field instead")
} else {
logger.Error("'Swarm.EnableAutoRelay' has been overridden by 'Swarm.AutoRelay.Enabled'")
}
logger.Fatal("The 'Swarm.EnableAutoRelay' config field was removed." +
"Use the 'Swarm.RelayClient.Enabled' instead.")
}
// nolint
if cfg.Swarm.EnableRelayHop {
logger.Fatal("The `Swarm.EnableRelayHop` config field is ignored.\n" +
logger.Fatal("The `Swarm.EnableRelayHop` config field was removed.\n" +
"Use `Swarm.RelayService` to configure the circuit v2 relay.\n" +
"If you want to continue running a circuit v1 relay, please use the standalone relay daemon: https://github.com/libp2p/go-libp2p-relay-daemon (with RelayV1.Enabled: true)")
"If you want to continue running a circuit v1 relay, please use the standalone relay daemon: https://dist.ipfs.io/#libp2p-relay-daemon (with RelayV1.Enabled: true)")
}

peerChan := make(chan peer.AddrInfo)
usesRelayClient := cfg.Swarm.RelayClient.Enabled.WithDefault(true)
// Gather all the options
opts := fx.Options(
BaseLibP2P,
Expand All @@ -156,12 +158,12 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.AppendAnnounce, cfg.Addresses.NoAnnounce)),
fx.Provide(libp2p.SmuxTransport(cfg.Swarm.Transports)),
fx.Provide(libp2p.RelayTransport(enableRelayTransport)),
fx.Provide(libp2p.RelayService(cfg.Swarm.RelayService.Enabled.WithDefault(true), cfg.Swarm.RelayService)),
fx.Provide(libp2p.RelayService(enableRelayService, cfg.Swarm.RelayService)),
fx.Provide(libp2p.Transports(cfg.Swarm.Transports)),
fx.Invoke(libp2p.StartListening(cfg.Addresses.Swarm)),
fx.Invoke(libp2p.SetupDiscovery(cfg.Discovery.MDNS.Enabled, cfg.Discovery.MDNS.Interval)),
fx.Provide(libp2p.ForceReachability(cfg.Internal.Libp2pForceReachability)),
fx.Provide(libp2p.HolePunching(cfg.Swarm.EnableHolePunching, cfg.Swarm.RelayClient.Enabled.WithDefault(false))),
fx.Provide(libp2p.HolePunching(cfg.Swarm.EnableHolePunching, enableRelayClient)),

fx.Provide(libp2p.Security(!bcfg.DisableEncryptedConnections, cfg.Swarm.Transports)),

Expand All @@ -171,9 +173,8 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

maybeProvide(libp2p.BandwidthCounter, !cfg.Swarm.DisableBandwidthMetrics),
maybeProvide(libp2p.NatPortMap, !cfg.Swarm.DisableNatPortMap),
maybeProvide(libp2p.AutoRelay(cfg.Swarm.RelayClient.StaticRelays, peerChan), usesRelayClient),

maybeInvoke(libp2p.AutoRelayFeeder, usesRelayClient),
maybeProvide(libp2p.AutoRelay(cfg.Swarm.RelayClient.StaticRelays, peerChan), enableRelayClient),
maybeInvoke(libp2p.AutoRelayFeeder, enableRelayClient),
autonat,
connmgr,
ps,
Expand Down
39 changes: 10 additions & 29 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ config file at runtime.
- [`Swarm.DisableBandwidthMetrics`](#swarmdisablebandwidthmetrics)
- [`Swarm.DisableNatPortMap`](#swarmdisablenatportmap)
- [`Swarm.EnableHolePunching`](#swarmenableholepunching)
- [`Swarm.EnableAutoRelay`](#swarmenableautorelay)
- [`Swarm.RelayClient`](#swarmrelayclient)
- [`Swarm.RelayClient.Enabled`](#swarmrelayclientenabled)
- [`Swarm.RelayClient.StaticRelays`](#swarmrelayclientstaticrelays)
Expand All @@ -122,7 +121,6 @@ config file at runtime.
- [`Swarm.RelayService.MaxReservationsPerPeer`](#swarmrelayservicemaxreservationsperpeer)
- [`Swarm.RelayService.MaxReservationsPerIP`](#swarmrelayservicemaxreservationsperip)
- [`Swarm.RelayService.MaxReservationsPerASN`](#swarmrelayservicemaxreservationsperasn)
- [`Swarm.DisableRelay`](#swarmdisablerelay)
- [`Swarm.EnableAutoNATService`](#swarmenableautonatservice)
- [`Swarm.ConnMgr`](#swarmconnmgr)
- [`Swarm.ConnMgr.Type`](#swarmconnmgrtype)
Expand Down Expand Up @@ -1370,27 +1368,15 @@ to [upgrade to a direct connection](https://github.com/libp2p/specs/blob/master/
through a NAT/firewall whenever possible.
This feature requires `Swarm.RelayClient.Enabled` to be set to `true`.

Default: `false`
Default: `true`

Type: `flag`

### `Swarm.EnableAutoRelay`

Deprecated: Set `Swarm.RelayClient.Enabled` to `true`.

Enables "automatic relay user" mode for this node.

Your node will automatically _use_ public relays from the network if it detects
that it cannot be reached from the public internet (e.g., it's behind a
firewall) and get a `/p2p-circuit` address from a public relay.

This is likely the feature you're looking for, but see also:
- [`Swarm.RelayService.Enabled`](#swarmrelayserviceenabled) if your node should act as a limited relay for other peers
- Docs: [Libp2p Circuit Relay](https://docs.libp2p.io/concepts/circuit-relay/)
**REMOVED**

Default: `true`

Type: `bool`
See `Swarm.RelayClient` instead.

### `Swarm.RelayClient`

Expand All @@ -1414,8 +1400,8 @@ Type: `bool`

#### `Swarm.RelayClient.StaticRelays`

Your node will use these statically configured relay servers instead of
discovering public relays from the network.
Your node will use these statically configured relay servers (V1 or V2)
instead of discovering public relays V2 from the network.

Default: `[]`

Expand Down Expand Up @@ -1536,15 +1522,9 @@ Replaced with [`Swarm.RelayService.Enabled`](#swarmrelayserviceenabled).

### `Swarm.DisableRelay`

Deprecated: Set `Swarm.Transports.Network.Relay` to `false`.

Disables the p2p-circuit relay transport. This will prevent this node from
connecting to nodes behind relays, or accepting connections from nodes behind
relays.

Default: `false`
**REMOVED**

Type: `bool`
Set `Swarm.Transports.Network.Relay` to `false` instead.

### `Swarm.EnableAutoNATService`

Expand Down Expand Up @@ -1765,8 +1745,9 @@ NATs.

See also:
- Docs: [Libp2p Circuit Relay](https://docs.libp2p.io/concepts/circuit-relay/)
- [`Swarm.EnableAutoRelay`](#swarmenableautorelay) for getting a public
`/p2p-circuit` address when behind a firewall.
- [`Swarm.RelayClient.Enabled`](#swarmrelayclientenabled) for getting a public
- `/p2p-circuit` address when behind a firewall.
- [`Swarm.EnableHolePunching`](#swarmenableholepunching) for direct connection upgrade through relay
- [`Swarm.RelayService.Enabled`](#swarmrelayserviceenabled) for becoming a
limited relay for other peers

Expand Down

0 comments on commit 8a72aa0

Please sign in to comment.