Skip to content

Commit

Permalink
WIP relay config
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 21, 2021
1 parent 03af870 commit cfe9509
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 38 deletions.
17 changes: 9 additions & 8 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
blockstore "github.com/ipfs/go-ipfs-blockstore"
config "github.com/ipfs/go-ipfs-config"
util "github.com/ipfs/go-ipfs-util"
log "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"

"github.com/ipfs/go-ipfs/core/node/libp2p"
Expand Down Expand Up @@ -109,17 +109,17 @@ 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.Relay` isn't, use the former.
enableRelay := cfg.Swarm.Transports.Network.Relay.WithDefault(!cfg.Swarm.DisableRelay) //nolint
// 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

// Warn about a deprecated option.
//nolint
if cfg.Swarm.DisableRelay {
logger.Error("The `Swarm.DisableRelay' config field is deprecated.")
if enableRelay {
logger.Error("`Swarm.DisableRelay' has been overridden by `Swarm.Transports.Network.Relay'")
if enableRelayTransport {
logger.Error("`Swarm.DisableRelay' has been overridden by `Swarm.Transports.Network.RelayTransport'")
} else {
logger.Error("Use the `Swarm.Transports.Network.Relay' config field instead")
logger.Error("Use the `Swarm.Transports.Network.RelayTransport' config field instead")
}
}

Expand All @@ -130,7 +130,8 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)),
fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.NoAnnounce)),
fx.Provide(libp2p.SmuxTransport(cfg.Swarm.Transports)),
fx.Provide(libp2p.Relay(enableRelay, cfg.Swarm.EnableRelayHop)),
fx.Provide(libp2p.RelayTransport(enableRelayTransport)),
fx.Provide(libp2p.RelayService(!cfg.Swarm.DisableRelayService, cfg.Swarm.RelayServiceOpts)),
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)),
Expand Down
45 changes: 43 additions & 2 deletions core/node/libp2p/relay.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package libp2p

import (
"time"

config "github.com/ipfs/go-ipfs-config"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
)

func Relay(enableRelay, enableHop bool) func() (opts Libp2pOpts, err error) {
func RelayTransport(enableRelay bool) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
if enableRelay {
// TODO: enable relay v2
opts.Opts = append(opts.Opts, libp2p.EnableRelay())
} else {
opts.Opts = append(opts.Opts, libp2p.DisableRelay())
Expand All @@ -16,4 +20,41 @@ func Relay(enableRelay, enableHop bool) func() (opts Libp2pOpts, err error) {
}
}

func RelayService(enable bool, relayOpts config.RelayResources) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
if enable {
r := relay.DefaultResources()
if relayOpts.Limit.Data > 0 {
r.Limit.Data = relayOpts.Limit.Data
}
if relayOpts.Limit.Duration > 0 {
r.Limit.Duration = time.Duration(relayOpts.Limit.Duration)
}
if relayOpts.MaxCircuits > 0 {
r.MaxCircuits = relayOpts.MaxCircuits
}
if relayOpts.BufferSize > 0 {
r.BufferSize = relayOpts.BufferSize
}
if relayOpts.ReservationTTL > 0 {
r.ReservationTTL = time.Duration(relayOpts.ReservationTTL)
}
if relayOpts.MaxReservations > 0 {
r.MaxReservations = relayOpts.MaxReservations
}
if relayOpts.MaxReservationsPerIP > 0 {
r.MaxReservationsPerIP = relayOpts.MaxReservationsPerIP
}
if relayOpts.MaxReservationsPerPeer > 0 {
r.MaxReservationsPerPeer = relayOpts.MaxReservationsPerPeer
}
if relayOpts.MaxReservationsPerASN > 0 {
r.MaxReservationsPerASN = relayOpts.MaxReservationsPerASN
}
opts.Opts = append(opts.Opts, libp2p.EnableRelayService(relay.WithResources(r)))
}
return
}
}

var AutoRelay = simpleOpt(libp2p.ChainOptions(libp2p.EnableAutoRelay(), libp2p.DefaultStaticRelays()))
150 changes: 129 additions & 21 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ config file at runtime.
- [`Swarm.DisableBandwidthMetrics`](#swarmdisablebandwidthmetrics)
- [`Swarm.DisableNatPortMap`](#swarmdisablenatportmap)
- [`Swarm.DisableRelay`](#swarmdisablerelay)
- [`Swarm.EnableRelayHop`](#swarmenablerelayhop)
- [`Swarm.DisableRelayService`](#swarmdisablerelayservice)
- [`Swarm.RelayServiceOpts`](#swarmrelayserviceopts)
- [`Swarm.RelayServiceOpts.Limit`](#swarmrelayserviceoptslimit)
- [`Swarm.RelayServiceOpts.Limit.Duration`](#swarmrelayserviceoptslimitduration)
- [`Swarm.RelayServiceOpts.Limit.Data`](#swarmrelayserviceoptslimitdata)
- [`Swarm.RelayServiceOpts.ReservationTTL`](#swarmrelayserviceoptsreservationttl)
- [`Swarm.RelayServiceOpts.MaxReservations`](#swarmrelayserviceoptsmaxreservations)
- [`Swarm.RelayServiceOpts.MaxCircuits`](#swarmrelayserviceoptsmaxcircuits)
- [`Swarm.RelayServiceOpts.BufferSize`](#swarmrelayserviceoptsbuffersize)
- [`Swarm.RelayServiceOpts.MaxReservationsPerPeer`](#swarmrelayserviceoptsmaxreservationsperpeer)
- [`Swarm.RelayServiceOpts.MaxReservationsPerIP`](#swarmrelayserviceoptsmaxreservationsperip)
- [`Swarm.RelayServiceOpts.MaxReservationsPerASN`](#swarmrelayserviceoptsmaxreservationsperasn)
- [`Swarm.EnableAutoRelay`](#swarmenableautorelay)
- [Mode 1: `EnableRelayHop` is `false`](#mode-1-enablerelayhop-is-false)
- [Mode 2: `EnableRelayHop` is `true`](#mode-2-enablerelayhop-is-true)
Expand Down Expand Up @@ -1280,18 +1291,128 @@ Default: `false`

Type: `bool`

### `Swarm.EnableRelayHop`

Configures this node to act as a relay "hop". A relay "hop" relays traffic for other peers.
### `Swarm.DisableRelayService`

WARNING: Do not enable this option unless you know what you're doing. Other
peers will randomly decide to use your node as a relay and consume _all_
available bandwidth. There is _no_ rate-limiting.
Disables the p2p-circuit v2 relay service. This will prevent this node from
running as a relay server.

Default: `false`

Type: `bool`

### `Swarm.RelayServiceOpts`

Configuration options for the relay service.

Default: `{}`

Type: `object`

#### `Swarm.RelayServiceOpts.Limit`

Limits applied to every relayed connection.

Default: `{}`

Type: `object[string -> string]`

##### `Swarm.RelayServiceOpts.Limit.Duration`

Time limit before a relayed connection is reset.

Default: `2m`

Type: `string`


##### `Swarm.RelayServiceOpts.Limit.Duration`

Time limit before a relayed connection is reset.

Default: `2m`

Type: `string`


##### `Swarm.RelayServiceOpts.Limit.Data`

Limit of data relayed (in each direction) before a relayed connection is reset.

Default: `131072` (128 kb)

Type: `integer`


#### `Swarm.RelayServiceOpts.ReservationTTL`

Duration of a new or refreshed reservation.

Default: `1h`

Type: `string`


#### `Swarm.RelayServiceOpts.MaxReservations`

Maximum number of active relay slots.

Default: `128`

Type: `integer`


#### `Swarm.RelayServiceOpts.MaxReservations`

Maximum number of open relay connections for each peer.

Default: `16`

Type: `integer`


#### `Swarm.RelayServiceOpts.BufferSize`

Size of the relayed connection buffers.

Default: `2048`

Type: `integer`


#### `Swarm.RelayServiceOpts.MaxReservationsPerPeer`

Maximum number of reservations originating from the same peer.

Default: `4`

Type: `integer`


#### `Swarm.RelayServiceOpts.MaxReservationsPerIP`

Maximum number of reservations originating from the same IP.

Default: `8`

Type: `integer`


#### `Swarm.RelayServiceOpts.MaxReservationsPerASN`

Maximum number of reservations originating from the same ASN.

Default: `32`

Type: `integer`


### `Swarm.EnableRelayHop`

**REMOVED**

Please use [`Swarm.DisableRelayService`][].


### `Swarm.EnableAutoRelay`

Enables "automatic relay" mode for this node. This option does two _very_
Expand All @@ -1302,23 +1423,10 @@ Default: `false`

Type: `bool`

#### Mode 1: `EnableRelayHop` is `false`

If `Swarm.EnableAutoRelay` is enabled and `Swarm.EnableRelayHop` is disabled,
your node will automatically _use_ public relays from the network if it detects
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). This is likely the feature you're looking for.

If you enable `EnableAutoRelay`, you should almost certainly disable
`EnableRelayHop`.

#### Mode 2: `EnableRelayHop` is `true`

If `EnableAutoRelay` is enabled and `EnableRelayHop` is enabled, your node will
_act_ as a public relay for the network. Furthermore, in addition to simply
relaying traffic, your node will advertise itself as a public relay. Unless you
have the bandwidth of a small ISP, do not enable both of these options at the
same time.

### `Swarm.EnableAutoNATService`

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/go-ipfs-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/ipfs/go-ipfs v0.9.1
github.com/ipfs/go-ipfs-config v0.16.0
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22
github.com/ipfs/go-ipfs-files v0.0.9
github.com/ipfs/interface-go-ipfs-core v0.5.1
github.com/libp2p/go-libp2p-core v0.11.0
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/go-ipfs-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ github.com/ipfs/go-ipfs-chunker v0.0.1/go.mod h1:tWewYK0we3+rMbOh7pPFGDyypCtvGcB
github.com/ipfs/go-ipfs-chunker v0.0.5 h1:ojCf7HV/m+uS2vhUGWcogIIxiO5ubl5O57Q7NapWLY8=
github.com/ipfs/go-ipfs-chunker v0.0.5/go.mod h1:jhgdF8vxRHycr00k13FM8Y0E+6BoalYeobXmUyTreP8=
github.com/ipfs/go-ipfs-cmds v0.6.0/go.mod h1:ZgYiWVnCk43ChwoH8hAmI1IRbuVtq3GSTHwtRB/Kqhk=
github.com/ipfs/go-ipfs-config v0.16.0 h1:CBtIYyp/iWIczCv83bmfge8EA2KqxOOfqmETs3tUnnU=
github.com/ipfs/go-ipfs-config v0.16.0/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22 h1:oaJFYaZpyJ3U6GSnn3+M3yH/++78jrns2xSZp4kWWe4=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/ipfs/go-ipfs-blockstore v0.1.6
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-cmds v0.6.0
github.com/ipfs/go-ipfs-config v0.16.0
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.9
Expand Down Expand Up @@ -66,7 +66,6 @@ require (
github.com/jbenet/goprocess v0.1.4
github.com/libp2p/go-doh-resolver v0.3.1
github.com/libp2p/go-libp2p v0.15.0-rc.1.0.20211021081216-db8f9c6fddb5
github.com/libp2p/go-libp2p-circuit v0.4.0
github.com/libp2p/go-libp2p-connmgr v0.2.4
github.com/libp2p/go-libp2p-core v0.11.0
github.com/libp2p/go-libp2p-discovery v0.5.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ github.com/ipfs/go-ipfs-chunker v0.0.5 h1:ojCf7HV/m+uS2vhUGWcogIIxiO5ubl5O57Q7Na
github.com/ipfs/go-ipfs-chunker v0.0.5/go.mod h1:jhgdF8vxRHycr00k13FM8Y0E+6BoalYeobXmUyTreP8=
github.com/ipfs/go-ipfs-cmds v0.6.0 h1:yAxdowQZzoFKjcLI08sXVNnqVj3jnABbf9smrPQmBsw=
github.com/ipfs/go-ipfs-cmds v0.6.0/go.mod h1:ZgYiWVnCk43ChwoH8hAmI1IRbuVtq3GSTHwtRB/Kqhk=
github.com/ipfs/go-ipfs-config v0.16.0 h1:CBtIYyp/iWIczCv83bmfge8EA2KqxOOfqmETs3tUnnU=
github.com/ipfs/go-ipfs-config v0.16.0/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22 h1:oaJFYaZpyJ3U6GSnn3+M3yH/++78jrns2xSZp4kWWe4=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211018130411-0226122eaf22/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
Expand Down

0 comments on commit cfe9509

Please sign in to comment.