Use helper methods in Machine

This commit is contained in:
Juan Font 2022-11-24 22:41:11 +00:00
parent 3da872bb30
commit 940dbc7706

View file

@ -138,6 +138,17 @@ func (machine Machine) isExpired() bool {
return time.Now().UTC().After(*machine.Expiry) return time.Now().UTC().After(*machine.Expiry)
} }
// isOnline returns if the machine is connected to Headscale.
// This is really a naive implementation, as we don't really see
// if there is a working connection between the client and the server.
func (machine *Machine) isOnline() bool {
if machine.LastSeen == nil {
return false
}
return machine.LastSeen.After(time.Now().Add(-keepAliveInterval))
}
func containsAddresses(inputs []string, addrs []string) bool { func containsAddresses(inputs []string, addrs []string) bool {
for _, addr := range addrs { for _, addr := range addrs {
if contains(inputs, addr) { if contains(inputs, addr) {
@ -708,9 +719,7 @@ func (h *Headscale) toNode(
hostInfo := machine.GetHostInfo() hostInfo := machine.GetHostInfo()
// A node is Online if it is connected to the control server, online := machine.isOnline()
// and we now we update LastSeen every keepAliveInterval duration at least.
online := machine.LastSeen.After(time.Now().Add(-keepAliveInterval))
node := tailcfg.Node{ node := tailcfg.Node{
ID: tailcfg.NodeID(machine.ID), // this is the actual ID ID: tailcfg.NodeID(machine.ID), // this is the actual ID
@ -1023,7 +1032,7 @@ func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error {
// Mark already as primary if there is only this node offering this subnet // Mark already as primary if there is only this node offering this subnet
// (and is not an exit route) // (and is not an exit route)
if prefix != ExitRouteV4 && prefix != ExitRouteV6 { if !route.isExitRoute() {
route.IsPrimary = h.isUniquePrefix(route) route.IsPrimary = h.isUniquePrefix(route)
} }