Update neighbours if node is expired or refreshed

In addition, only pass the map of registered and not expired nodes to
clients.
This commit is contained in:
Kristoffer Dalby 2021-11-22 19:51:16 +00:00
parent caf1b1cabc
commit 68dc2a70db
2 changed files with 23 additions and 1 deletions

2
api.go
View file

@ -186,7 +186,7 @@ func (h *Headscale) getMapResponse(
return nil, err return nil, err
} }
peers, err := h.getPeers(machine) peers, err := h.getValidPeers(machine)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("func", "getMapResponse"). Str("func", "getMapResponse").

View file

@ -207,6 +207,23 @@ func (h *Headscale) getPeers(machine *Machine) (Machines, error) {
return peers, nil return peers, nil
} }
func (h *Headscale) getValidPeers(machine *Machine) (Machines, error) {
validPeers := make(Machines, 0)
peers, err := h.getPeers(machine)
if err != nil {
return Machines{}, err
}
for _, peer := range peers {
if peer.isRegistered() && !peer.isExpired() {
validPeers = append(validPeers, peer)
}
}
return validPeers, nil
}
func (h *Headscale) ListMachines() ([]Machine, error) { func (h *Headscale) ListMachines() ([]Machine, error) {
machines := []Machine{} machines := []Machine{}
if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Find(&machines).Error; err != nil { if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Find(&machines).Error; err != nil {
@ -267,6 +284,8 @@ func (h *Headscale) ExpireMachine(machine *Machine) {
now := time.Now() now := time.Now()
machine.Expiry = &now machine.Expiry = &now
h.setLastStateChangeToNow(machine.Namespace.Name)
h.db.Save(machine) h.db.Save(machine)
} }
@ -276,6 +295,9 @@ func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) {
machine.LastSuccessfulUpdate = &now machine.LastSuccessfulUpdate = &now
machine.Expiry = &expiry machine.Expiry = &expiry
h.setLastStateChangeToNow(machine.Namespace.Name)
h.db.Save(machine) h.db.Save(machine)
} }