mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-26 20:21:26 +08:00
Merge branch 'master' into uci-golangci-lint
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"
|
||||
@@ -379,8 +380,28 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
|
||||
fxopts = append(fxopts, cfg.QUICReuse...)
|
||||
} else {
|
||||
fxopts = append(fxopts,
|
||||
fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
|
||||
var opts []quicreuse.Option
|
||||
fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, rcmgr network.ResourceManager, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
|
||||
opts := []quicreuse.Option{
|
||||
quicreuse.ConnContext(func(ctx context.Context, clientInfo *quic.ClientInfo) (context.Context, error) {
|
||||
// even if creating the quic maddr fails, let the rcmgr decide what to do with the connection
|
||||
addr, err := quicreuse.ToQuicMultiaddr(clientInfo.RemoteAddr, quic.Version1)
|
||||
if err != nil {
|
||||
addr = nil
|
||||
}
|
||||
scope, err := rcmgr.OpenConnection(network.DirInbound, false, addr)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
ctx = network.WithConnManagementScope(ctx, scope)
|
||||
context.AfterFunc(ctx, func() {
|
||||
scope.Done()
|
||||
})
|
||||
return ctx, nil
|
||||
}),
|
||||
quicreuse.VerifySourceAddress(func(addr net.Addr) bool {
|
||||
return rcmgr.VerifySourceAddress(addr)
|
||||
}),
|
||||
}
|
||||
if !cfg.DisableMetrics {
|
||||
opts = append(opts, quicreuse.EnableMetrics(cfg.PrometheusRegisterer))
|
||||
}
|
||||
@@ -413,15 +434,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 +450,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 +529,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