Skip to content
Merged
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
16 changes: 13 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,19 @@ func createHost(_ context.Context, hostOpts []libp2p.Option, config Config, rela
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.Port),
fmt.Sprintf("/ip6/::/tcp/%d", config.Port),
),
libp2p.NATPortMap(),
libp2p.EnableNATService(),
libp2p.EnableHolePunching(),
)

// Only enable NAT traversal features if not disabled
// NAT features can cause data races in tests due to libp2p's NAT manager using non-thread-safe global state
if !config.DisableNAT {
hostOpts = append(hostOpts,
libp2p.NATPortMap(),
libp2p.EnableNATService(),
libp2p.EnableHolePunching(),
)
}

hostOpts = append(hostOpts,
libp2p.EnableRelay(),
libp2p.EnableAutoRelayWithStaticRelays(relayPeers),
)
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ type Config struct {
// Recommended: 6-24 hours for production to reduce CPU overhead.
// The cleanup frequency trades off between memory usage (stale records) and CPU usage.
DHTCleanupInterval time.Duration

// DisableNAT disables NAT traversal features (UPnP/NAT-PMP port mapping, NAT service, hole punching).
// Set to true in test environments where NAT traversal is not needed and can cause data races
// due to libp2p's NAT manager using non-thread-safe global state.
// Default: false (NAT features enabled)
DisableNAT bool
}
Loading