2023-09-04 11:37:39 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-09-04 11:37:39 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-12-11 12:58:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/store"
|
|
|
|
"github.com/netbirdio/netbird/management/server/types"
|
2023-09-04 11:37:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ephemeralLifeTime = 10 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
timeNow = time.Now
|
|
|
|
)
|
|
|
|
|
|
|
|
type ephemeralPeer struct {
|
|
|
|
id string
|
2024-12-11 12:58:45 +01:00
|
|
|
account *types.Account
|
2023-09-04 11:37:39 +02:00
|
|
|
deadline time.Time
|
|
|
|
next *ephemeralPeer
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: consider to remove peer from ephemeral list when the peer has been deleted via API. If we do not do it
|
|
|
|
// in worst case we will get invalid error message in this manager.
|
|
|
|
|
|
|
|
// EphemeralManager keep a list of ephemeral peers. After ephemeralLifeTime inactivity the peer will be deleted
|
|
|
|
// automatically. Inactivity means the peer disconnected from the Management server.
|
|
|
|
type EphemeralManager struct {
|
2024-12-11 12:58:45 +01:00
|
|
|
store store.Store
|
2023-09-04 11:37:39 +02:00
|
|
|
accountManager AccountManager
|
|
|
|
|
|
|
|
headPeer *ephemeralPeer
|
|
|
|
tailPeer *ephemeralPeer
|
|
|
|
peersLock sync.Mutex
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEphemeralManager instantiate new EphemeralManager
|
2024-12-11 12:58:45 +01:00
|
|
|
func NewEphemeralManager(store store.Store, accountManager AccountManager) *EphemeralManager {
|
2023-09-04 11:37:39 +02:00
|
|
|
return &EphemeralManager{
|
|
|
|
store: store,
|
|
|
|
accountManager: accountManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadInitialPeers load from the database the ephemeral type of peers and schedule a cleanup procedure to the head
|
|
|
|
// of the linked list (to the most deprecated peer). At the end of cleanup it schedules the next cleanup to the new
|
|
|
|
// head.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (e *EphemeralManager) LoadInitialPeers(ctx context.Context) {
|
2023-09-04 11:37:39 +02:00
|
|
|
e.peersLock.Lock()
|
|
|
|
defer e.peersLock.Unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
e.loadEphemeralPeers(ctx)
|
2023-09-04 11:37:39 +02:00
|
|
|
if e.headPeer != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
e.timer = time.AfterFunc(ephemeralLifeTime, func() {
|
|
|
|
e.cleanup(ctx)
|
|
|
|
})
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop timer
|
|
|
|
func (e *EphemeralManager) Stop() {
|
|
|
|
e.peersLock.Lock()
|
|
|
|
defer e.peersLock.Unlock()
|
|
|
|
|
|
|
|
if e.timer != nil {
|
|
|
|
e.timer.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPeerConnected remove the peer from the linked list of ephemeral peers. Because it has been called when the peer
|
|
|
|
// is active the manager will not delete it while it is active.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (e *EphemeralManager) OnPeerConnected(ctx context.Context, peer *nbpeer.Peer) {
|
2023-09-04 11:37:39 +02:00
|
|
|
if !peer.Ephemeral {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Tracef("remove peer from ephemeral list: %s", peer.ID)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
|
|
|
e.peersLock.Lock()
|
|
|
|
defer e.peersLock.Unlock()
|
|
|
|
|
|
|
|
e.removePeer(peer.ID)
|
|
|
|
|
|
|
|
// stop the unnecessary timer
|
|
|
|
if e.headPeer == nil && e.timer != nil {
|
|
|
|
e.timer.Stop()
|
|
|
|
e.timer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPeerDisconnected add the peer to the linked list of ephemeral peers. Because of the peer
|
|
|
|
// is inactive it will be deleted after the ephemeralLifeTime period.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (e *EphemeralManager) OnPeerDisconnected(ctx context.Context, peer *nbpeer.Peer) {
|
2023-09-04 11:37:39 +02:00
|
|
|
if !peer.Ephemeral {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Tracef("add peer to ephemeral list: %s", peer.ID)
|
2023-09-04 11:37:39 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
a, err := e.store.GetAccountByPeerID(context.Background(), peer.ID)
|
2023-09-04 11:37:39 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to add peer to ephemeral list: %s", err)
|
2023-09-04 11:37:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.peersLock.Lock()
|
|
|
|
defer e.peersLock.Unlock()
|
|
|
|
|
|
|
|
if e.isPeerOnList(peer.ID) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.addPeer(peer.ID, a, newDeadLine())
|
|
|
|
if e.timer == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
e.timer = time.AfterFunc(e.headPeer.deadline.Sub(timeNow()), func() {
|
|
|
|
e.cleanup(ctx)
|
|
|
|
})
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (e *EphemeralManager) loadEphemeralPeers(ctx context.Context) {
|
|
|
|
accounts := e.store.GetAllAccounts(context.Background())
|
2023-09-04 11:37:39 +02:00
|
|
|
t := newDeadLine()
|
|
|
|
count := 0
|
|
|
|
for _, a := range accounts {
|
|
|
|
for id, p := range a.Peers {
|
|
|
|
if p.Ephemeral {
|
|
|
|
count++
|
|
|
|
e.addPeer(id, a, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("loaded ephemeral peer(s): %d", count)
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (e *EphemeralManager) cleanup(ctx context.Context) {
|
2023-09-04 11:37:39 +02:00
|
|
|
log.Tracef("on ephemeral cleanup")
|
|
|
|
deletePeers := make(map[string]*ephemeralPeer)
|
|
|
|
|
|
|
|
e.peersLock.Lock()
|
|
|
|
now := timeNow()
|
|
|
|
for p := e.headPeer; p != nil; p = p.next {
|
|
|
|
if now.Before(p.deadline) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
deletePeers[p.id] = p
|
|
|
|
e.headPeer = p.next
|
|
|
|
if p.next == nil {
|
|
|
|
e.tailPeer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.headPeer != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
e.timer = time.AfterFunc(e.headPeer.deadline.Sub(timeNow()), func() {
|
|
|
|
e.cleanup(ctx)
|
|
|
|
})
|
2023-09-04 11:37:39 +02:00
|
|
|
} else {
|
|
|
|
e.timer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
e.peersLock.Unlock()
|
|
|
|
|
|
|
|
for id, p := range deletePeers {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("delete ephemeral peer: %s", id)
|
|
|
|
err := e.accountManager.DeletePeer(ctx, p.account.Id, id, activity.SystemInitiator)
|
2023-09-04 11:37:39 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to delete ephemeral peer: %s", err)
|
2023-09-04 11:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
func (e *EphemeralManager) addPeer(id string, account *types.Account, deadline time.Time) {
|
2023-09-04 11:37:39 +02:00
|
|
|
ep := &ephemeralPeer{
|
|
|
|
id: id,
|
|
|
|
account: account,
|
|
|
|
deadline: deadline,
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.headPeer == nil {
|
|
|
|
e.headPeer = ep
|
|
|
|
}
|
|
|
|
if e.tailPeer != nil {
|
|
|
|
e.tailPeer.next = ep
|
|
|
|
}
|
|
|
|
e.tailPeer = ep
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EphemeralManager) removePeer(id string) {
|
|
|
|
if e.headPeer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.headPeer.id == id {
|
|
|
|
e.headPeer = e.headPeer.next
|
|
|
|
if e.tailPeer.id == id {
|
|
|
|
e.tailPeer = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for p := e.headPeer; p.next != nil; p = p.next {
|
|
|
|
if p.next.id == id {
|
|
|
|
// if we remove the last element from the chain then set the last-1 as tail
|
|
|
|
if e.tailPeer.id == id {
|
|
|
|
e.tailPeer = p
|
|
|
|
}
|
|
|
|
p.next = p.next.next
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EphemeralManager) isPeerOnList(id string) bool {
|
|
|
|
for p := e.headPeer; p != nil; p = p.next {
|
|
|
|
if p.id == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDeadLine() time.Time {
|
|
|
|
return timeNow().Add(ephemeralLifeTime)
|
|
|
|
}
|