hvpn-node3/peer_meta.go

55 lines
986 B
Go
Raw Normal View History

package hvpnnode3
import (
"sync"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// Contians data about peers
type PeerData struct {
addedOn time.Time
deleted bool
}
// Basic implementaion to track peers based on public keys. Thraed safe.
type PeerMeta struct {
data map[wgtypes.Key]PeerData
rwl *sync.RWMutex
}
func NewPeerMeta() *PeerMeta {
meta := new(PeerMeta)
meta.data = make(map[wgtypes.Key]PeerData)
meta.rwl = new(sync.RWMutex)
return meta
}
func (m *PeerMeta) AddPeer(publicKey wgtypes.Key) {
m.rwl.Lock()
m.data[publicKey] = PeerData{
addedOn: time.Now().UTC(),
deleted: false,
}
m.rwl.Unlock()
}
func (m *PeerMeta) DeletePeer(publicKey wgtypes.Key) {
m.rwl.Lock()
d := m.data[publicKey]
d.deleted = true
m.rwl.Unlock()
}
func (m *PeerMeta) TimeAdded(publicKey wgtypes.Key) time.Time {
m.rwl.RLock()
t := m.data[publicKey].addedOn
m.rwl.RUnlock()
return t
}