netbird/relay/client/manager.go

220 lines
5.9 KiB
Go
Raw Normal View History

2024-05-23 13:24:02 +02:00
package client
import (
"context"
2024-05-29 16:40:26 +02:00
"fmt"
2024-05-26 22:14:33 +02:00
"net"
"sync"
"time"
2024-06-01 11:48:15 +02:00
log "github.com/sirupsen/logrus"
2024-05-23 13:24:02 +02:00
)
var (
relayCleanupInterval = 60 * time.Second
errRelayClientNotConnected = fmt.Errorf("relay client not connected")
)
// RelayTrack hold the relay clients for the foregin relay servers.
// With the mutex can ensure we can open new connection in case the relay connection has been established with
// the relay server.
2024-06-01 11:48:15 +02:00
type RelayTrack struct {
sync.RWMutex
relayClient *Client
}
func NewRelayTrack() *RelayTrack {
return &RelayTrack{}
}
// Manager is a manager for the relay client. It establish one persistent connection to the given relay server. In case
// of network error the manager will try to reconnect to the server.
// The manager also manage temproary relay connection. If a client wants to communicate with an another client on a
// different relay server, the manager will establish a new connection to the relay server. The connection with these
// relay servers will be closed if there is no active connection. Periodically the manager will check if there is any
// unused relay connection and close it.
2024-05-23 13:24:02 +02:00
type Manager struct {
ctx context.Context
srvAddress string
peerID string
relayClient *Client
reconnectGuard *Guard
2024-05-23 13:24:02 +02:00
2024-06-01 11:48:15 +02:00
relayClients map[string]*RelayTrack
relayClientsMutex sync.RWMutex
2024-05-23 13:24:02 +02:00
}
func NewManager(ctx context.Context, serverAddress string, peerID string) *Manager {
return &Manager{
2024-05-29 16:40:26 +02:00
ctx: ctx,
srvAddress: serverAddress,
peerID: peerID,
2024-06-01 11:48:15 +02:00
relayClients: make(map[string]*RelayTrack),
2024-05-23 13:24:02 +02:00
}
}
// Serve starts the manager. It will establish a connection to the relay server and start the relay cleanup loop.
// todo: consider to return an error if the initial connection to the relay server is not established.
func (m *Manager) Serve() error {
if m.relayClient != nil {
return fmt.Errorf("manager already serving")
}
2024-05-29 16:40:26 +02:00
m.relayClient = NewClient(m.ctx, m.srvAddress, m.peerID)
err := m.relayClient.Connect()
if err != nil {
log.Errorf("failed to connect to relay server: %s", err)
return err
2024-05-23 13:24:02 +02:00
}
m.reconnectGuard = NewGuard(m.ctx, m.relayClient)
m.relayClient.SetOnDisconnectListener(m.reconnectGuard.OnDisconnected)
m.startCleanupLoop()
return nil
2024-05-26 22:14:33 +02:00
}
// OpenConn opens a connection to the given peer key. If the peer is on the same relay server, the connection will be
// established via the relay server. If the peer is on a different relay server, the manager will establish a new
// connection to the relay server.
func (m *Manager) OpenConn(serverAddress, peerKey string) (net.Conn, error) {
2024-05-29 16:40:26 +02:00
if m.relayClient == nil {
return nil, errRelayClientNotConnected
2024-05-29 16:40:26 +02:00
}
foreign, err := m.isForeignServer(serverAddress)
2024-05-29 16:40:26 +02:00
if err != nil {
return nil, err
2024-05-29 16:40:26 +02:00
}
2024-06-01 11:48:15 +02:00
if !foreign {
2024-06-20 18:17:30 +02:00
log.Debugf("open peer connection via permanent server: %s", peerKey)
return m.relayClient.OpenConn(peerKey)
2024-06-01 11:48:15 +02:00
} else {
2024-06-20 18:17:30 +02:00
log.Debugf("open peer connection via foreign server: %s", serverAddress)
2024-06-01 11:48:15 +02:00
return m.openConnVia(serverAddress, peerKey)
}
2024-05-26 22:14:33 +02:00
}
// RelayAddress returns the address of the permanent relay server. It could change if the network connection is lost.
// This address will be sent to the target peer to choose the common relay server for the communication.
func (m *Manager) RelayAddress() (net.Addr, error) {
2024-05-29 16:40:26 +02:00
if m.relayClient == nil {
return nil, errRelayClientNotConnected
2024-05-29 16:40:26 +02:00
}
return m.relayClient.RelayRemoteAddress()
}
2024-05-29 16:40:26 +02:00
2024-06-18 11:10:17 +02:00
func (m *Manager) HasRelayAddress() bool {
return m.srvAddress != ""
}
func (m *Manager) openConnVia(serverAddress, peerKey string) (net.Conn, error) {
// check if already has a connection to the desired relay server
2024-06-01 11:48:15 +02:00
m.relayClientsMutex.RLock()
rt, ok := m.relayClients[serverAddress]
2024-05-29 16:40:26 +02:00
if ok {
rt.RLock()
2024-06-01 11:48:15 +02:00
m.relayClientsMutex.RUnlock()
defer rt.RUnlock()
return rt.relayClient.OpenConn(peerKey)
2024-05-29 16:40:26 +02:00
}
2024-06-01 11:48:15 +02:00
m.relayClientsMutex.RUnlock()
// if not, establish a new connection but check it again (because changed the lock type) before starting the
// connection
2024-06-01 11:48:15 +02:00
m.relayClientsMutex.Lock()
rt, ok = m.relayClients[serverAddress]
if ok {
rt.RLock()
m.relayClientsMutex.Unlock()
defer rt.RUnlock()
return rt.relayClient.OpenConn(peerKey)
}
// create a new relay client and store it in the relayClients map
rt = NewRelayTrack()
rt.Lock()
2024-06-01 11:48:15 +02:00
m.relayClients[serverAddress] = rt
m.relayClientsMutex.Unlock()
2024-05-29 16:40:26 +02:00
2024-06-01 11:48:15 +02:00
relayClient := NewClient(m.ctx, serverAddress, m.peerID)
err := relayClient.Connect()
2024-05-29 16:40:26 +02:00
if err != nil {
2024-06-01 11:48:15 +02:00
rt.Unlock()
m.relayClientsMutex.Lock()
delete(m.relayClients, serverAddress)
m.relayClientsMutex.Unlock()
2024-05-29 16:40:26 +02:00
return nil, err
}
// if connection closed then delete the relay client from the list
relayClient.SetOnDisconnectListener(func() {
m.deleteRelayConn(serverAddress)
})
rt.relayClient = relayClient
2024-06-01 11:48:15 +02:00
rt.Unlock()
2024-05-29 16:40:26 +02:00
conn, err := relayClient.OpenConn(peerKey)
if err != nil {
return nil, err
}
return conn, nil
2024-05-23 13:24:02 +02:00
}
func (m *Manager) deleteRelayConn(address string) {
2024-06-01 11:48:15 +02:00
log.Infof("deleting relay client for %s", address)
m.relayClientsMutex.Lock()
delete(m.relayClients, address)
m.relayClientsMutex.Unlock()
}
func (m *Manager) isForeignServer(address string) (bool, error) {
rAddr, err := m.relayClient.RelayRemoteAddress()
if err != nil {
return false, fmt.Errorf("relay client not connected")
}
return rAddr.String() != address, nil
}
func (m *Manager) startCleanupLoop() {
if m.ctx.Err() != nil {
return
}
ticker := time.NewTicker(relayCleanupInterval)
go func() {
defer ticker.Stop()
for {
select {
case <-m.ctx.Done():
return
case <-ticker.C:
m.cleanUpUnusedRelays()
}
}
}()
}
func (m *Manager) cleanUpUnusedRelays() {
m.relayClientsMutex.Lock()
defer m.relayClientsMutex.Unlock()
for addr, rt := range m.relayClients {
rt.Lock()
if rt.relayClient.HasConns() {
rt.Unlock()
continue
}
rt.relayClient.SetOnDisconnectListener(nil)
go func() {
_ = rt.relayClient.Close()
}()
log.Debugf("clean up relay client: %s", addr)
delete(m.relayClients, addr)
rt.Unlock()
}
}