Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autonat: fix interaction with autorelay #2967

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -432,16 +433,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 @@ -514,17 +505,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 @@ -561,7 +543,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 @@ -577,11 +559,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 @@ -664,7 +655,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
Loading