mirror of
https://github.com/netbirdio/netbird.git
synced 2025-07-21 16:28:16 +02:00
With the lazy connection feature, the peer will connect to target peers on-demand. The trigger can be any IP traffic. This feature can be enabled with the NB_ENABLE_EXPERIMENTAL_LAZY_CONN environment variable. When the engine receives a network map, it binds a free UDP port for every remote peer, and the system configures WireGuard endpoints for these ports. When traffic appears on a UDP socket, the system removes this listener and starts the peer connection procedure immediately. Key changes Fix slow netbird status -d command Move from engine.go file to conn_mgr.go the peer connection related code Refactor the iface interface usage and moved interface file next to the engine code Add new command line flag and UI option to enable feature The peer.Conn struct is reusable after it has been closed. Change connection states Connection states Idle: The peer is not attempting to establish a connection. This typically means it's in a lazy state or the remote peer is expired. Connecting: The peer is actively trying to establish a connection. This occurs when the peer has entered an active state and is continuously attempting to reach the remote peer. Connected: A successful peer-to-peer connection has been established and communication is active.
115 lines
2.1 KiB
Go
115 lines
2.1 KiB
Go
package peerstore
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"sync"
|
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
|
)
|
|
|
|
// Store is a thread-safe store for peer connections.
|
|
type Store struct {
|
|
peerConns map[string]*peer.Conn
|
|
peerConnsMu sync.RWMutex
|
|
}
|
|
|
|
func NewConnStore() *Store {
|
|
return &Store{
|
|
peerConns: make(map[string]*peer.Conn),
|
|
}
|
|
}
|
|
|
|
func (s *Store) AddPeerConn(pubKey string, conn *peer.Conn) bool {
|
|
s.peerConnsMu.Lock()
|
|
defer s.peerConnsMu.Unlock()
|
|
|
|
_, ok := s.peerConns[pubKey]
|
|
if ok {
|
|
return false
|
|
}
|
|
|
|
s.peerConns[pubKey] = conn
|
|
return true
|
|
}
|
|
|
|
func (s *Store) Remove(pubKey string) (*peer.Conn, bool) {
|
|
s.peerConnsMu.Lock()
|
|
defer s.peerConnsMu.Unlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
delete(s.peerConns, pubKey)
|
|
return p, true
|
|
}
|
|
|
|
func (s *Store) AllowedIPs(pubKey string) ([]netip.Prefix, bool) {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return p.WgConfig().AllowedIps, true
|
|
}
|
|
|
|
func (s *Store) AllowedIP(pubKey string) (netip.Addr, bool) {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return netip.Addr{}, false
|
|
}
|
|
return p.AllowedIP(), true
|
|
}
|
|
|
|
func (s *Store) PeerConn(pubKey string) (*peer.Conn, bool) {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return p, true
|
|
}
|
|
|
|
func (s *Store) PeerConnOpen(ctx context.Context, pubKey string) {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return
|
|
}
|
|
// this can be blocked because of the connect open limiter semaphore
|
|
if err := p.Open(ctx); err != nil {
|
|
p.Log.Errorf("failed to open peer connection: %v", err)
|
|
}
|
|
|
|
}
|
|
|
|
func (s *Store) PeerConnClose(pubKey string) {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
p, ok := s.peerConns[pubKey]
|
|
if !ok {
|
|
return
|
|
}
|
|
p.Close()
|
|
}
|
|
|
|
func (s *Store) PeersPubKey() []string {
|
|
s.peerConnsMu.RLock()
|
|
defer s.peerConnsMu.RUnlock()
|
|
|
|
return maps.Keys(s.peerConns)
|
|
}
|