mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-26 01:53:42 +01:00
0c039274a4
This update adds new relay integration for NetBird clients. The new relay is based on web sockets and listens on a single port. - Adds new relay implementation with websocket with single port relaying mechanism - refactor peer connection logic, allowing upgrade and downgrade from/to P2P connection - peer connections are faster since it connects first to relay and then upgrades to P2P - maintains compatibility with old clients by not using the new relay - updates infrastructure scripts with new relay service
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// Store is a thread-safe store of peers
|
|
// It is used to store the peers that are connected to the relay server
|
|
type Store struct {
|
|
peers map[string]*Peer // consider to use [32]byte as key. The Peer(id string) would be faster
|
|
peersLock sync.RWMutex
|
|
}
|
|
|
|
// NewStore creates a new Store instance
|
|
func NewStore() *Store {
|
|
return &Store{
|
|
peers: make(map[string]*Peer),
|
|
}
|
|
}
|
|
|
|
// AddPeer adds a peer to the store
|
|
// todo: consider to close peer conn if the peer already exists
|
|
func (s *Store) AddPeer(peer *Peer) {
|
|
s.peersLock.Lock()
|
|
defer s.peersLock.Unlock()
|
|
s.peers[peer.String()] = peer
|
|
}
|
|
|
|
// DeletePeer deletes a peer from the store
|
|
func (s *Store) DeletePeer(peer *Peer) {
|
|
s.peersLock.Lock()
|
|
defer s.peersLock.Unlock()
|
|
|
|
dp, ok := s.peers[peer.String()]
|
|
if !ok {
|
|
return
|
|
}
|
|
if dp != peer {
|
|
return
|
|
}
|
|
|
|
delete(s.peers, peer.String())
|
|
}
|
|
|
|
// Peer returns a peer by its ID
|
|
func (s *Store) Peer(id string) (*Peer, bool) {
|
|
s.peersLock.RLock()
|
|
defer s.peersLock.RUnlock()
|
|
|
|
p, ok := s.peers[id]
|
|
return p, ok
|
|
}
|
|
|
|
// Peers returns all the peers in the store
|
|
func (s *Store) Peers() []*Peer {
|
|
s.peersLock.RLock()
|
|
defer s.peersLock.RUnlock()
|
|
|
|
peers := make([]*Peer, 0, len(s.peers))
|
|
for _, p := range s.peers {
|
|
peers = append(peers, p)
|
|
}
|
|
return peers
|
|
}
|