@@ -32,6 +32,8 @@ import (
3232 "github.com/libp2p/go-libp2p/p2p/discovery/mdns"
3333 drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
3434 "github.com/libp2p/go-libp2p/p2p/net/conngater"
35+ "github.com/libp2p/go-libp2p/p2p/net/connmgr"
36+ "github.com/libp2p/go-libp2p/p2p/transport/tcp"
3537 "github.com/multiformats/go-multiaddr"
3638 manet "github.com/multiformats/go-multiaddr/net"
3739)
@@ -232,6 +234,39 @@ func createPrivateIPConnectionGater(log logger, cancel context.CancelFunc) (*con
232234func buildHostOptions (config Config , log logger , cancel context.CancelFunc ) ([]libp2p.Option , error ) {
233235 hostOpts := []libp2p.Option {libp2p .Identity (config .PrivateKey )}
234236
237+ // Explicitly configure only TCP transport to prevent WebRTC's mDNS usage
238+ // WebRTC transport uses mDNS (port 5353) for ICE candidate discovery
239+ // By only enabling TCP, we avoid any unwanted mDNS traffic
240+ hostOpts = append (hostOpts , libp2p .Transport (tcp .NewTCPTransport ))
241+ log .Infof ("Configured TCP-only transport (WebRTC disabled to prevent mDNS)" )
242+
243+ // Configure connection manager to limit total connections
244+ maxConns := config .MaxConnections
245+ if maxConns == 0 {
246+ maxConns = 35 // Default high water mark
247+ }
248+ minConns := config .MinConnections
249+ if minConns == 0 {
250+ minConns = 25 // Default low water mark
251+ }
252+ gracePeriod := config .ConnectionGracePeriod
253+ if gracePeriod == 0 {
254+ gracePeriod = 20 * time .Second // Default grace period
255+ }
256+
257+ connMgr , err := connmgr .NewConnManager (
258+ minConns ,
259+ maxConns ,
260+ connmgr .WithGracePeriod (gracePeriod ),
261+ )
262+ if err != nil {
263+ cancel ()
264+ return nil , fmt .Errorf ("failed to create connection manager: %w" , err )
265+ }
266+
267+ hostOpts = append (hostOpts , libp2p .ConnectionManager (connMgr ))
268+ log .Infof ("Connection manager configured: min=%d, max=%d, grace=%v" , minConns , maxConns , gracePeriod )
269+
235270 // Add connection gater to block private IPs if AllowPrivateIPs is false (default)
236271 if ! config .AllowPrivateIPs {
237272 ipFilter , err := createPrivateIPConnectionGater (log , cancel )
@@ -494,6 +529,10 @@ func (c *client) Subscribe(topic string) <-chan Message {
494529 peerID := conn .RemotePeer ()
495530 topicPeers := t .ListPeers ()
496531 if slices .Contains (topicPeers , peerID ) {
532+ // Tag topic peers with high value to protect from connection manager pruning
533+ c .host .ConnManager ().TagPeer (peerID , fmt .Sprintf ("topic:%s" , topic ), 100 )
534+ c .logger .Debugf ("Tagged topic peer %s for protection" , peerID )
535+
497536 name := c .peerTracker .getName (peerID )
498537 addr := conn .RemoteMultiaddr ().String ()
499538 c .logger .Infof ("[CONNECTED] Topic peer %s [%s] %s" , peerID .String (), name , addr )
@@ -507,6 +546,8 @@ func (c *client) Subscribe(topic string) <-chan Message {
507546 peerID := conn .RemotePeer ()
508547 topicPeers := t .ListPeers ()
509548 if slices .Contains (topicPeers , peerID ) {
549+ // Untag peer when they disconnect from topic
550+ c .host .ConnManager ().UntagPeer (peerID , fmt .Sprintf ("topic:%s" , topic ))
510551 c .logger .Infof ("[DISCONNECTED] Lost connection to topic peer %s" , peerID .String ()[:16 ])
511552 }
512553 },
0 commit comments