Skip to content

Commit

Permalink
autonat: fix interaction with autorelay (#2967)
Browse files Browse the repository at this point in the history
* autonat: fix interaction with autorelay

* Fix race in test

* Use deadline from context if available for DialBack

* Return hasNewAddrs correctly

* nit: cleanup contains check

* Shuffle peers

* nits

* Change comment to indicate the bug

* holepuncher: pass address function in constructor (#2979)

* holepunch: pass address function in constructor

* nit

* Remove getPublicAddrs

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>

* Make a copy of the multiaddr slice in Addrs()

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
  • Loading branch information
sukunrt and MarcoPolo authored Oct 17, 2024
1 parent af2042c commit b9cb861
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 272 deletions.
41 changes: 16 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"slices"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -430,16 +431,6 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
if err != nil {
return nil, err
}
if cfg.Relay {
// If we've enabled the relay, we should filter out relay
// addresses by default.
//
// TODO: We shouldn't be doing this here.
originalAddrFactory := h.AddrsFactory
h.AddrsFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr {
return originalAddrFactory(autorelay.Filter(addrs))
}
}
return h, nil
}

Expand Down Expand Up @@ -512,17 +503,8 @@ func (cfg *Config) NewNode() (host.Host, error) {
)
}

// originalAddrFactory is the AddrFactory before it's modified by autorelay
// we need this for checking reachability via autonat
originalAddrFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr {
return addrs
}

// enable autorelay
fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost) {
originalAddrFactory = h.AddrsFactory
}),
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) error {
if cfg.EnableAutoRelay {
if !cfg.DisableMetrics {
Expand Down Expand Up @@ -559,7 +541,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, err
}

if err := cfg.addAutoNAT(bh, originalAddrFactory); err != nil {
if err := cfg.addAutoNAT(bh); err != nil {
app.Stop(context.Background())
if cfg.Routing != nil {
rh.Close()
Expand All @@ -575,11 +557,20 @@ func (cfg *Config) NewNode() (host.Host, error) {
return &closableBasicHost{App: app, BasicHost: bh}, nil
}

func (cfg *Config) addAutoNAT(h *bhost.BasicHost, addrF AddrsFactory) error {
func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
// Only use public addresses for autonat
addrFunc := func() []ma.Multiaddr {
return slices.DeleteFunc(h.AllAddrs(), func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
}
if cfg.AddrsFactory != nil {
addrFunc = func() []ma.Multiaddr {
return slices.DeleteFunc(
cfg.AddrsFactory(h.AllAddrs()),
func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
}
}
autonatOpts := []autonat.Option{
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
}),
autonat.UsingAddresses(addrFunc),
}
if !cfg.DisableMetrics {
autonatOpts = append(autonatOpts, autonat.WithMetricsTracer(
Expand Down Expand Up @@ -662,7 +653,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost, addrF AddrsFactory) error {

autonat, err := autonat.New(h, autonatOpts...)
if err != nil {
return fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
return fmt.Errorf("autonat init failed: %w", err)
}
h.SetAutoNat(autonat)
return nil
Expand Down
2 changes: 1 addition & 1 deletion core/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Host interface {
// given peer.ID. Connect will absorb the addresses in pi into its internal
// peerstore. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is
// returned. // TODO: Relay + NAT.
// returned.
Connect(ctx context.Context, pi peer.AddrInfo) error

// SetStreamHandler sets the protocol handler on the Host's Mux.
Expand Down
Loading

0 comments on commit b9cb861

Please sign in to comment.