mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-26 20:21:26 +08:00
basichost: use autonatv2 to verify reachability (#3231)
This introduces addrsReachabilityTracker that tracks reachability on a set of addresses. It probes reachability for addresses periodically and has an exponential backoff in case there are too many errors or we don't have any valid autonatv2 peer. There's no smartness in the address selection logic currently. We just test all provided addresses. It also doesn't use the addresses provided by `AddrsFactory`, so currently there's no way to get a user provided address tested for reachability, something that would be a problem for dns addresses. I intend to introduce an alternative to `AddrsFactory`, something like, `AnnounceAddrs(addrs []ma.Multiaddr)` that's just appended to the set of addresses that we have, and check reachability for those addresses. There's only one method exposed in the BasicHost right now that's `ReachableAddrs() []ma.Multiadd`r that returns the host's reachable addrs. Users can also use the event `EvtHostReachableAddrsChanged` to be notified when any addrs reachability changes.
This commit is contained in:
@@ -33,6 +33,7 @@ import (
|
||||
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
|
||||
"github.com/libp2p/go-libp2p/p2p/net/swarm"
|
||||
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
|
||||
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2"
|
||||
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
|
||||
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
|
||||
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
|
||||
@@ -413,15 +414,7 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
|
||||
return fxopts, nil
|
||||
}
|
||||
|
||||
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.BasicHost, error) {
|
||||
var autonatv2Dialer host.Host
|
||||
if cfg.EnableAutoNATv2 {
|
||||
ah, err := cfg.makeAutoNATV2Host()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
autonatv2Dialer = ah
|
||||
}
|
||||
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus, an *autonatv2.AutoNAT) (*bhost.BasicHost, error) {
|
||||
h, err := bhost.NewHost(swrm, &bhost.HostOpts{
|
||||
EventBus: eventBus,
|
||||
ConnManager: cfg.ConnManager,
|
||||
@@ -437,8 +430,7 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
|
||||
EnableMetrics: !cfg.DisableMetrics,
|
||||
PrometheusRegisterer: cfg.PrometheusRegisterer,
|
||||
DisableIdentifyAddressDiscovery: cfg.DisableIdentifyAddressDiscovery,
|
||||
EnableAutoNATv2: cfg.EnableAutoNATv2,
|
||||
AutoNATv2Dialer: autonatv2Dialer,
|
||||
AutoNATv2: an,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -517,6 +509,24 @@ func (cfg *Config) NewNode() (host.Host, error) {
|
||||
})
|
||||
return sw, nil
|
||||
}),
|
||||
fx.Provide(func() (*autonatv2.AutoNAT, error) {
|
||||
if !cfg.EnableAutoNATv2 {
|
||||
return nil, nil
|
||||
}
|
||||
ah, err := cfg.makeAutoNATV2Host()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var mt autonatv2.MetricsTracer
|
||||
if !cfg.DisableMetrics {
|
||||
mt = autonatv2.NewMetricsTracer(cfg.PrometheusRegisterer)
|
||||
}
|
||||
autoNATv2, err := autonatv2.New(ah, autonatv2.WithMetricsTracer(mt))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create autonatv2: %w", err)
|
||||
}
|
||||
return autoNATv2, nil
|
||||
}),
|
||||
fx.Provide(cfg.newBasicHost),
|
||||
fx.Provide(func(bh *bhost.BasicHost) identify.IDService {
|
||||
return bh.IDService()
|
||||
|
Reference in New Issue
Block a user