Skip to content

Commit

Permalink
Merge pull request #1239 from libp2p/autorelay-options
Browse files Browse the repository at this point in the history
pass static relays to EnableAutoRelay, deprecate libp2p.StaticRelays and libp2p.DefaultStaticRelays
  • Loading branch information
marten-seemann authored Nov 14, 2021
2 parents 7a35d0d + 4141cac commit 3c4b16f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
15 changes: 9 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Config struct {

EnableAutoRelay bool
AutoNATConfig
StaticRelays []peer.AddrInfo
StaticRelayOpt autorelay.StaticRelayOption

EnableHolePunching bool
HolePunchingOptions []holepunch.Option
Expand Down Expand Up @@ -251,8 +251,9 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}

if len(cfg.StaticRelays) > 0 {
ar = autorelay.NewAutoRelay(h, nil, router, cfg.StaticRelays)
var opts []autorelay.Option
if cfg.StaticRelayOpt != nil {
opts = append(opts, autorelay.Option(cfg.StaticRelayOpt))
} else {
if router == nil {
h.Close()
Expand All @@ -263,9 +264,11 @@ func (cfg *Config) NewNode() (host.Host, error) {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}

discovery := discovery.NewRoutingDiscovery(crouter)
ar = autorelay.NewAutoRelay(h, discovery, router, cfg.StaticRelays)
opts = append(opts, autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)))
}
ar, err = autorelay.NewAutoRelay(h, router, opts...)
if err != nil {
return nil, err
}
}

Expand Down
32 changes: 17 additions & 15 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,14 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
func EnableAutoRelay() Option {
func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
return func(cfg *Config) error {
if len(opts) > 0 {
if len(opts) > 1 {
return errors.New("only expected a single static relay configuration option")
}
cfg.StaticRelayOpt = opts[0]
}
cfg.EnableAutoRelay = true
return nil
}
Expand All @@ -260,30 +266,26 @@ func EnableAutoRelay() Option {
// StaticRelays configures known relays for autorelay; when this option is enabled
// then the system will use the configured relays instead of querying the DHT to
// discover relays.
// Deprecated: pass an autorelay.WithStaticRelays option to EnableAutoRelay.
func StaticRelays(relays []peer.AddrInfo) Option {
return func(cfg *Config) error {
cfg.StaticRelays = append(cfg.StaticRelays, relays...)
cfg.StaticRelayOpt = autorelay.WithStaticRelays(relays)
return nil
}
}

// DefaultStaticRelays configures the static relays to use the known PL-operated relays.
// Deprecated: pass autorelay.WithDefaultStaticRelays to EnableAutoRelay.
func DefaultStaticRelays() Option {
return func(cfg *Config) error {
for _, addr := range autorelay.DefaultRelays {
a, err := ma.NewMultiaddr(addr)
if err != nil {
return err
}
pi, err := peer.AddrInfoFromP2pAddr(a)
if err != nil {
return err
}
cfg.StaticRelays = append(cfg.StaticRelays, *pi)
relays := make([]peer.AddrInfo, 0, len(autorelay.DefaultRelays))
for _, addr := range autorelay.DefaultRelays {
pi, err := peer.AddrInfoFromString(addr)
if err != nil {
panic(fmt.Sprintf("failed to initialize default static relays: %s", err))
}

return nil
relays = append(relays, *pi)
}
return StaticRelays(relays)
}

// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
Expand Down
50 changes: 45 additions & 5 deletions p2p/host/autorelay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package autorelay

import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
Expand Down Expand Up @@ -42,7 +43,7 @@ var (
BootDelay = 20 * time.Second
)

// These are the known PL-operated v1 relays; will be decommissioned in 2022.
// DefaultRelays are the known PL-operated v1 relays; will be decommissioned in 2022.
var DefaultRelays = []string{
"/ip4/147.75.80.110/tcp/4001/p2p/QmbFgm5zan8P6eWWmeyfncR5feYEMPbht5b1FW1C37aQ7y",
"/ip4/147.75.80.110/udp/4001/quic/p2p/QmbFgm5zan8P6eWWmeyfncR5feYEMPbht5b1FW1C37aQ7y",
Expand All @@ -52,6 +53,42 @@ var DefaultRelays = []string{
"/ip4/147.75.70.221/udp/4001/quic/p2p/Qme8g49gm3q4Acp7xWBKg3nAa9fxZ1YmyDJdyGgoG6LsXh",
}

var defaultStaticRelays []peer.AddrInfo

func init() {
for _, s := range DefaultRelays {
pi, err := peer.AddrInfoFromString(s)
if err != nil {
panic(fmt.Sprintf("failed to initialize default static relays: %s", err))
}
defaultStaticRelays = append(defaultStaticRelays, *pi)
}
}

type Option func(*AutoRelay) error
type StaticRelayOption Option

func WithStaticRelays(static []peer.AddrInfo) StaticRelayOption {
return func(r *AutoRelay) error {
if len(r.static) > 0 {
return errors.New("can't set static relays, static relays already configured")
}
r.static = static
return nil
}
}

func WithDefaultStaticRelays() StaticRelayOption {
return WithStaticRelays(defaultStaticRelays)
}

func WithDiscoverer(discover discovery.Discoverer) Option {
return func(r *AutoRelay) error {
r.discover = discover
return nil
}
}

// AutoRelay is a Host that uses relays for connectivity when a NAT is detected.
type AutoRelay struct {
host *basic.BasicHost
Expand All @@ -74,24 +111,27 @@ type AutoRelay struct {
cachedAddrsExpiry time.Time
}

func NewAutoRelay(bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting, static []peer.AddrInfo) *AutoRelay {
func NewAutoRelay(bhost *basic.BasicHost, router routing.PeerRouting, opts ...Option) (*AutoRelay, error) {
ctx, cancel := context.WithCancel(context.Background())
ar := &AutoRelay{
ctxCancel: cancel,
host: bhost,
discover: discover,
router: router,
addrsF: bhost.AddrsFactory,
static: static,
relays: make(map[peer.ID]*circuitv2.Reservation),
disconnect: make(chan struct{}, 1),
status: network.ReachabilityUnknown,
}
for _, opt := range opts {
if err := opt(ar); err != nil {
return nil, err
}
}
bhost.AddrsFactory = ar.hostAddrs
bhost.Network().Notify(ar)
ar.refCount.Add(1)
go ar.background(ctx)
return ar
return ar, nil
}

func (ar *AutoRelay) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
Expand Down

0 comments on commit 3c4b16f

Please sign in to comment.