diff --git a/machine.go b/machine.go index 7b83e44c..1d79cd96 100644 --- a/machine.go +++ b/machine.go @@ -1105,9 +1105,9 @@ func (h *Headscale) EnableAutoApprovedRoutes(machine *Machine) error { } } - for _, approvedRoute := range approvedRoutes { - approvedRoute.Enabled = true - err = h.db.Save(&approvedRoute).Error + for i, approvedRoute := range approvedRoutes { + approvedRoutes[i].Enabled = true + err = h.db.Save(&approvedRoutes[i]).Error if err != nil { log.Err(err). Str("approvedRoute", approvedRoute.String()). diff --git a/routes.go b/routes.go index 7de728de..f1b1913f 100644 --- a/routes.go +++ b/routes.go @@ -106,20 +106,20 @@ func (h *Headscale) processMachineRoutes(machine *Machine) error { advertisedRoutes[prefix] = false } - for _, route := range currentRoutes { + for pos, route := range currentRoutes { if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok { if !route.Advertised { - route.Advertised = true - err := h.db.Save(&route).Error + currentRoutes[pos].Advertised = true + err := h.db.Save(¤tRoutes[pos]).Error if err != nil { return err } } advertisedRoutes[netip.Prefix(route.Prefix)] = true } else if route.Advertised { - route.Advertised = false - route.Enabled = false - err := h.db.Save(&route).Error + currentRoutes[pos].Advertised = false + currentRoutes[pos].Enabled = false + err := h.db.Save(¤tRoutes[pos]).Error if err != nil { return err } @@ -155,7 +155,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error { log.Error().Err(err).Msg("error getting routes") } - for _, route := range routes { + for pos, route := range routes { if route.isExitRoute() { continue } @@ -163,8 +163,8 @@ func (h *Headscale) handlePrimarySubnetFailover() error { if !route.IsPrimary { _, err := h.getPrimaryRoute(netip.Prefix(route.Prefix)) if h.isUniquePrefix(route) || errors.Is(err, gorm.ErrRecordNotFound) { - route.IsPrimary = true - err := h.db.Save(&route).Error + routes[pos].IsPrimary = true + err := h.db.Save(&routes[pos]).Error if err != nil { log.Error().Err(err).Msg("error marking route as primary") @@ -202,9 +202,9 @@ func (h *Headscale) handlePrimarySubnetFailover() error { } var newPrimaryRoute *Route - for _, r := range newPrimaryRoutes { + for pos, r := range newPrimaryRoutes { if r.Machine.isOnline() { - newPrimaryRoute = &r + newPrimaryRoute = &newPrimaryRoutes[pos] break } @@ -226,8 +226,8 @@ func (h *Headscale) handlePrimarySubnetFailover() error { Msgf("found new primary route") // disable the old primary route - route.IsPrimary = false - err = h.db.Save(&route).Error + routes[pos].IsPrimary = false + err = h.db.Save(&routes[pos]).Error if err != nil { log.Error().Err(err).Msg("error disabling old primary route")