Skip to content

Commit 335c75a

Browse files
authored
Merge branch 'master' into uci-golangci-lint
2 parents 50f065b + dbf7e1b commit 335c75a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3377
-531
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ linters:
1212
- revive
1313
- unused
1414
- prealloc
15+
disable:
16+
- errcheck
17+
- staticcheck
1518

1619
disable:
1720
- errcheck

config/config.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
3434
"github.com/libp2p/go-libp2p/p2p/net/swarm"
3535
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
36+
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2"
3637
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
3738
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
3839
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
@@ -379,8 +380,28 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
379380
fxopts = append(fxopts, cfg.QUICReuse...)
380381
} else {
381382
fxopts = append(fxopts,
382-
fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
383-
var opts []quicreuse.Option
383+
fx.Provide(func(key quic.StatelessResetKey, tokenGenerator quic.TokenGeneratorKey, rcmgr network.ResourceManager, lifecycle fx.Lifecycle) (*quicreuse.ConnManager, error) {
384+
opts := []quicreuse.Option{
385+
quicreuse.ConnContext(func(ctx context.Context, clientInfo *quic.ClientInfo) (context.Context, error) {
386+
// even if creating the quic maddr fails, let the rcmgr decide what to do with the connection
387+
addr, err := quicreuse.ToQuicMultiaddr(clientInfo.RemoteAddr, quic.Version1)
388+
if err != nil {
389+
addr = nil
390+
}
391+
scope, err := rcmgr.OpenConnection(network.DirInbound, false, addr)
392+
if err != nil {
393+
return ctx, err
394+
}
395+
ctx = network.WithConnManagementScope(ctx, scope)
396+
context.AfterFunc(ctx, func() {
397+
scope.Done()
398+
})
399+
return ctx, nil
400+
}),
401+
quicreuse.VerifySourceAddress(func(addr net.Addr) bool {
402+
return rcmgr.VerifySourceAddress(addr)
403+
}),
404+
}
384405
if !cfg.DisableMetrics {
385406
opts = append(opts, quicreuse.EnableMetrics(cfg.PrometheusRegisterer))
386407
}
@@ -413,15 +434,7 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
413434
return fxopts, nil
414435
}
415436

416-
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.BasicHost, error) {
417-
var autonatv2Dialer host.Host
418-
if cfg.EnableAutoNATv2 {
419-
ah, err := cfg.makeAutoNATV2Host()
420-
if err != nil {
421-
return nil, err
422-
}
423-
autonatv2Dialer = ah
424-
}
437+
func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus, an *autonatv2.AutoNAT) (*bhost.BasicHost, error) {
425438
h, err := bhost.NewHost(swrm, &bhost.HostOpts{
426439
EventBus: eventBus,
427440
ConnManager: cfg.ConnManager,
@@ -437,8 +450,7 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
437450
EnableMetrics: !cfg.DisableMetrics,
438451
PrometheusRegisterer: cfg.PrometheusRegisterer,
439452
DisableIdentifyAddressDiscovery: cfg.DisableIdentifyAddressDiscovery,
440-
EnableAutoNATv2: cfg.EnableAutoNATv2,
441-
AutoNATv2Dialer: autonatv2Dialer,
453+
AutoNATv2: an,
442454
})
443455
if err != nil {
444456
return nil, err
@@ -517,6 +529,24 @@ func (cfg *Config) NewNode() (host.Host, error) {
517529
})
518530
return sw, nil
519531
}),
532+
fx.Provide(func() (*autonatv2.AutoNAT, error) {
533+
if !cfg.EnableAutoNATv2 {
534+
return nil, nil
535+
}
536+
ah, err := cfg.makeAutoNATV2Host()
537+
if err != nil {
538+
return nil, err
539+
}
540+
var mt autonatv2.MetricsTracer
541+
if !cfg.DisableMetrics {
542+
mt = autonatv2.NewMetricsTracer(cfg.PrometheusRegisterer)
543+
}
544+
autoNATv2, err := autonatv2.New(ah, autonatv2.WithMetricsTracer(mt))
545+
if err != nil {
546+
return nil, fmt.Errorf("failed to create autonatv2: %w", err)
547+
}
548+
return autoNATv2, nil
549+
}),
520550
fx.Provide(cfg.newBasicHost),
521551
fx.Provide(func(bh *bhost.BasicHost) identify.IDService {
522552
return bh.IDService()

core/event/reachability.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package event
22

33
import (
44
"github.com/libp2p/go-libp2p/core/network"
5+
ma "github.com/multiformats/go-multiaddr"
56
)
67

78
// EvtLocalReachabilityChanged is an event struct to be emitted when the local's
@@ -11,3 +12,13 @@ import (
1112
type EvtLocalReachabilityChanged struct {
1213
Reachability network.Reachability
1314
}
15+
16+
// EvtHostReachableAddrsChanged is sent when host's reachable or unreachable addresses change
17+
// Reachable, Unreachable, and Unknown only contain Public IP or DNS addresses
18+
//
19+
// Experimental: This API is unstable. Any changes to this event will be done without a deprecation notice.
20+
type EvtHostReachableAddrsChanged struct {
21+
Reachable []ma.Multiaddr
22+
Unreachable []ma.Multiaddr
23+
Unknown []ma.Multiaddr
24+
}

core/network/mocks/mock_resource_manager.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/network/rcmgr.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package network
22

33
import (
4+
"context"
5+
"errors"
6+
"net"
7+
48
"github.com/libp2p/go-libp2p/core/peer"
59
"github.com/libp2p/go-libp2p/core/protocol"
610
"github.com/multiformats/go-multiaddr"
@@ -87,6 +91,10 @@ type ResourceManager interface {
8791
// the end of the scope's span.
8892
OpenConnection(dir Direction, usefd bool, endpoint multiaddr.Multiaddr) (ConnManagementScope, error)
8993

94+
// VerifySourceAddress tells the transport to verify the source address for an incoming connection
95+
// before gating the connection with OpenConnection.
96+
VerifySourceAddress(addr net.Addr) bool
97+
9098
// OpenStream creates a new stream scope, initially unnegotiated.
9199
// An unnegotiated stream will be initially unattached to any protocol scope
92100
// and constrained by the transient scope.
@@ -269,9 +277,30 @@ type ScopeStat struct {
269277
Memory int64
270278
}
271279

280+
// connManagementScopeKey is the key to store Scope in contexts
281+
type connManagementScopeKey struct{}
282+
283+
func WithConnManagementScope(ctx context.Context, scope ConnManagementScope) context.Context {
284+
return context.WithValue(ctx, connManagementScopeKey{}, scope)
285+
}
286+
287+
func UnwrapConnManagementScope(ctx context.Context) (ConnManagementScope, error) {
288+
v := ctx.Value(connManagementScopeKey{})
289+
if v == nil {
290+
return nil, errors.New("context has no ConnManagementScope")
291+
}
292+
scope, ok := v.(ConnManagementScope)
293+
if !ok {
294+
return nil, errors.New("context has no ConnManagementScope")
295+
}
296+
return scope, nil
297+
}
298+
272299
// NullResourceManager is a stub for tests and initialization of default values
273300
type NullResourceManager struct{}
274301

302+
var _ ResourceManager = (*NullResourceManager)(nil)
303+
275304
var _ ResourceScope = (*NullScope)(nil)
276305
var _ ResourceScopeSpan = (*NullScope)(nil)
277306
var _ ServiceScope = (*NullScope)(nil)
@@ -306,6 +335,10 @@ func (n *NullResourceManager) OpenConnection(_ Direction, _ bool, _ multiaddr.Mu
306335
func (n *NullResourceManager) OpenStream(_ peer.ID, _ Direction) (StreamManagementScope, error) {
307336
return &NullScope{}, nil
308337
}
338+
func (*NullResourceManager) VerifySourceAddress(_ net.Addr) bool {
339+
return false
340+
}
341+
309342
func (n *NullResourceManager) Close() error {
310343
return nil
311344
}
@@ -324,3 +357,4 @@ func (n *NullScope) ProtocolScope() ProtocolScope { return &NullScop
324357
func (n *NullScope) SetProtocol(_ protocol.ID) error { return nil }
325358
func (n *NullScope) ServiceScope() ServiceScope { return &NullScope{} }
326359
func (n *NullScope) SetService(_ string) error { return nil }
360+
func (n *NullScope) VerifySourceAddress(_ net.Addr) bool { return false }

go.mod

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ require (
5353
github.com/pion/webrtc/v4 v4.0.14
5454
github.com/prometheus/client_golang v1.21.0
5555
github.com/prometheus/client_model v0.6.1
56-
github.com/quic-go/quic-go v0.50.0
56+
github.com/quic-go/quic-go v0.52.0
5757
github.com/quic-go/webtransport-go v0.8.1-0.20241018022711-4ac2c9250e66
5858
github.com/stretchr/testify v1.10.0
5959
go.uber.org/fx v1.23.0
6060
go.uber.org/goleak v1.3.0
61-
go.uber.org/mock v0.5.0
61+
go.uber.org/mock v0.5.2
6262
go.uber.org/zap v1.27.0
63-
golang.org/x/crypto v0.35.0
64-
golang.org/x/sync v0.11.0
65-
golang.org/x/sys v0.30.0
63+
golang.org/x/crypto v0.37.0
64+
golang.org/x/sync v0.14.0
65+
golang.org/x/sys v0.33.0
6666
golang.org/x/time v0.11.0
67-
golang.org/x/tools v0.30.0
67+
golang.org/x/tools v0.32.0
6868
google.golang.org/protobuf v1.36.5
6969
)
7070

@@ -74,7 +74,7 @@ require (
7474
github.com/davecgh/go-spew v1.1.1 // indirect
7575
github.com/francoispqt/gojay v1.2.13 // indirect
7676
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
77-
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 // indirect
77+
github.com/google/pprof v0.0.0-20250501235452-c0086092b71a // indirect
7878
github.com/google/uuid v1.6.0 // indirect
7979
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
8080
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -83,7 +83,7 @@ require (
8383
github.com/minio/sha256-simd v1.0.1 // indirect
8484
github.com/multiformats/go-base36 v0.2.0 // indirect
8585
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
86-
github.com/onsi/ginkgo/v2 v2.22.2 // indirect
86+
github.com/onsi/ginkgo/v2 v2.23.4 // indirect
8787
github.com/pion/dtls/v2 v2.2.12 // indirect
8888
github.com/pion/dtls/v3 v3.0.4 // indirect
8989
github.com/pion/interceptor v0.1.37 // indirect
@@ -103,12 +103,13 @@ require (
103103
github.com/quic-go/qpack v0.5.1 // indirect
104104
github.com/spaolacci/murmur3 v1.1.0 // indirect
105105
github.com/wlynxg/anet v0.0.5 // indirect
106+
go.uber.org/automaxprocs v1.6.0 // indirect
106107
go.uber.org/dig v1.18.0 // indirect
107108
go.uber.org/multierr v1.11.0 // indirect
108109
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
109-
golang.org/x/mod v0.23.0 // indirect
110-
golang.org/x/net v0.35.0 // indirect
111-
golang.org/x/text v0.22.0 // indirect
110+
golang.org/x/mod v0.24.0 // indirect
111+
golang.org/x/net v0.39.0 // indirect
112+
golang.org/x/text v0.24.0 // indirect
112113
gopkg.in/yaml.v3 v3.0.1 // indirect
113114
lukechampine.com/blake3 v1.4.0 // indirect
114115
)

0 commit comments

Comments
 (0)