From e024655a40df067749ee81459ea90296cb553f46 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 14 Nov 2021 14:48:21 +0400 Subject: [PATCH 1/3] introduce options to configure the AutoRelay --- config/config.go | 19 +++++++++++++++---- p2p/host/autorelay/autorelay.go | 33 ++++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 9606de5015..0b7a156218 100644 --- a/config/config.go +++ b/config/config.go @@ -252,7 +252,11 @@ func (cfg *Config) NewNode() (host.Host, error) { } if len(cfg.StaticRelays) > 0 { - ar = autorelay.NewAutoRelay(h, nil, router, cfg.StaticRelays) + var err error + ar, err = autorelay.NewAutoRelay(h, router, autorelay.WithStaticRelays(cfg.StaticRelays)) + if err != nil { + return nil, err + } } else { if router == nil { h.Close() @@ -263,9 +267,16 @@ 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) + var err error + ar, err = autorelay.NewAutoRelay( + h, + router, + autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)), + autorelay.WithStaticRelays(cfg.StaticRelays), + ) + if err != nil { + return nil, err + } } } diff --git a/p2p/host/autorelay/autorelay.go b/p2p/host/autorelay/autorelay.go index db0a676546..0123e50b86 100644 --- a/p2p/host/autorelay/autorelay.go +++ b/p2p/host/autorelay/autorelay.go @@ -2,6 +2,7 @@ package autorelay import ( "context" + "errors" "fmt" "math/rand" "sync" @@ -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", @@ -52,6 +53,25 @@ var DefaultRelays = []string{ "/ip4/147.75.70.221/udp/4001/quic/p2p/Qme8g49gm3q4Acp7xWBKg3nAa9fxZ1YmyDJdyGgoG6LsXh", } +type Option func(*AutoRelay) error + +func WithStaticRelays(static []peer.AddrInfo) Option { + 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 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 @@ -74,24 +94,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 { From 9734b8d822d9c8ee305c58d7556e6927f8d39662 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 14 Nov 2021 14:53:56 +0400 Subject: [PATCH 2/3] add a WithDefaultStaticRelays AutoRelay option --- options.go | 6 +----- p2p/host/autorelay/autorelay.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/options.go b/options.go index 3d20c975cc..b92148876d 100644 --- a/options.go +++ b/options.go @@ -271,11 +271,7 @@ func StaticRelays(relays []peer.AddrInfo) Option { 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) + pi, err := peer.AddrInfoFromString(addr) if err != nil { return err } diff --git a/p2p/host/autorelay/autorelay.go b/p2p/host/autorelay/autorelay.go index 0123e50b86..a97de138f5 100644 --- a/p2p/host/autorelay/autorelay.go +++ b/p2p/host/autorelay/autorelay.go @@ -53,6 +53,18 @@ 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 func WithStaticRelays(static []peer.AddrInfo) Option { @@ -65,6 +77,10 @@ func WithStaticRelays(static []peer.AddrInfo) Option { } } +func WithDefaultStaticRelays() Option { + return WithStaticRelays(defaultStaticRelays) +} + func WithDiscoverer(discover discovery.Discoverer) Option { return func(r *AutoRelay) error { r.discover = discover From 4141cac6f9f2d64b6e876a354fbd8c707f393260 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 14 Nov 2021 15:33:59 +0400 Subject: [PATCH 3/3] pass static relays to EnableAutoRelay --- config/config.go | 26 +++++++++----------------- options.go | 28 +++++++++++++++++----------- p2p/host/autorelay/autorelay.go | 5 +++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/config/config.go b/config/config.go index 0b7a156218..47459b8b27 100644 --- a/config/config.go +++ b/config/config.go @@ -97,7 +97,7 @@ type Config struct { EnableAutoRelay bool AutoNATConfig - StaticRelays []peer.AddrInfo + StaticRelayOpt autorelay.StaticRelayOption EnableHolePunching bool HolePunchingOptions []holepunch.Option @@ -251,12 +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 { - var err error - ar, err = autorelay.NewAutoRelay(h, router, autorelay.WithStaticRelays(cfg.StaticRelays)) - if err != nil { - return nil, err - } + var opts []autorelay.Option + if cfg.StaticRelayOpt != nil { + opts = append(opts, autorelay.Option(cfg.StaticRelayOpt)) } else { if router == nil { h.Close() @@ -267,16 +264,11 @@ func (cfg *Config) NewNode() (host.Host, error) { h.Close() return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery") } - var err error - ar, err = autorelay.NewAutoRelay( - h, - router, - autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)), - autorelay.WithStaticRelays(cfg.StaticRelays), - ) - if err != nil { - return nil, err - } + opts = append(opts, autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter))) + } + ar, err = autorelay.NewAutoRelay(h, router, opts...) + if err != nil { + return nil, err } } diff --git a/options.go b/options.go index b92148876d..863d519597 100644 --- a/options.go +++ b/options.go @@ -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 } @@ -260,26 +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 { - pi, err := peer.AddrInfoFromString(addr) - 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, diff --git a/p2p/host/autorelay/autorelay.go b/p2p/host/autorelay/autorelay.go index a97de138f5..5a028a450e 100644 --- a/p2p/host/autorelay/autorelay.go +++ b/p2p/host/autorelay/autorelay.go @@ -66,8 +66,9 @@ func init() { } type Option func(*AutoRelay) error +type StaticRelayOption Option -func WithStaticRelays(static []peer.AddrInfo) 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") @@ -77,7 +78,7 @@ func WithStaticRelays(static []peer.AddrInfo) Option { } } -func WithDefaultStaticRelays() Option { +func WithDefaultStaticRelays() StaticRelayOption { return WithStaticRelays(defaultStaticRelays) }