diff --git a/api.go b/api.go index f88fa480..fa2e681f 100644 --- a/api.go +++ b/api.go @@ -15,7 +15,6 @@ import ( "github.com/gin-gonic/gin" "github.com/klauspost/compress/zstd" "github.com/rs/zerolog/log" - "github.com/spf13/viper" "gorm.io/gorm" "tailscale.com/tailcfg" "tailscale.com/types/key" @@ -121,8 +120,7 @@ func (h *Headscale) RegistrationHandler(ctx *gin.Context) { return } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() machine, err := h.GetMachineByMachineKey(machineKey) if errors.Is(err, gorm.ErrRecordNotFound) { log.Info().Str("machine", req.Hostinfo.Hostname).Msg("New machine") @@ -191,7 +189,7 @@ func (h *Headscale) RegistrationHandler(ctx *gin.Context) { if machine.NodeKey == NodePublicKeyStripPrefix(req.NodeKey) { // The client sends an Expiry in the past if the client is requesting to expire the key (aka logout) // https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L648 - if !req.Expiry.IsZero() && req.Expiry.In(location).Before(now) { + if !req.Expiry.IsZero() && req.Expiry.Before(NowFromTZEnv()) { h.handleMachineLogOut(ctx, machineKey, *machine) return @@ -603,8 +601,7 @@ func (h *Headscale) handleAuthKey( machine.AuthKeyID = uint(pak.ID) h.RefreshMachine(machine, registerRequest.Expiry) } else { - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() machineToRegister := Machine{ Name: registerRequest.Hostinfo.Hostname, NamespaceID: pak.Namespace.ID, diff --git a/app.go b/app.go index 687b80eb..94d73b28 100644 --- a/app.go +++ b/app.go @@ -27,7 +27,6 @@ import ( zerolog "github.com/philip-bui/grpc-zerolog" zl "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/spf13/viper" ginprometheus "github.com/zsais/go-gin-prometheus" "golang.org/x/crypto/acme" "golang.org/x/crypto/acme/autocert" @@ -792,8 +791,7 @@ func (h *Headscale) getTLSSettings() (*tls.Config, error) { } func (h *Headscale) setLastStateChangeToNow(namespace string) { - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() lastStateUpdate.WithLabelValues("", "headscale").Set(float64(now.Unix())) h.lastStateChange.Store(namespace, now) } @@ -816,8 +814,7 @@ func (h *Headscale) getLastStateChange(namespaces ...string) time.Time { log.Trace().Msgf("Latest times %#v", times) if len(times) == 0 { - location, _ := time.LoadLocation(viper.GetString("TZ")) - return time.Now().In(location) + return NowFromTZEnv() } else { return times[0] } diff --git a/machine.go b/machine.go index 360a7d77..2481a643 100644 --- a/machine.go +++ b/machine.go @@ -11,7 +11,6 @@ import ( "github.com/fatih/set" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/rs/zerolog/log" - "github.com/spf13/viper" "google.golang.org/protobuf/types/known/timestamppb" "inet.af/netaddr" "tailscale.com/tailcfg" @@ -117,9 +116,8 @@ func (machine Machine) isExpired() bool { if machine.Expiry == nil || machine.Expiry.IsZero() { return false } - location, _ := time.LoadLocation(viper.GetString("TZ")) - return time.Now().In(location).After(*machine.Expiry) + return NowFromTZEnv().After(*machine.Expiry) } func containsAddresses(inputs []string, addrs []string) bool { diff --git a/poll.go b/poll.go index ed538158..70b52955 100644 --- a/poll.go +++ b/poll.go @@ -10,7 +10,6 @@ import ( "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" - "github.com/spf13/viper" "gorm.io/gorm" "tailscale.com/tailcfg" "tailscale.com/types/key" @@ -98,8 +97,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { machine.Name = hname machine.HostInfo = HostInfo(*req.Hostinfo) machine.DiscoKey = DiscoPublicKeyStripPrefix(req.DiscoKey) - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() // update ACLRules with peer informations (to update server tags if necessary) if h.aclPolicy != nil { @@ -341,8 +339,7 @@ func (h *Headscale) PollNetMapStream( // since the stream opened, terminate connection. return false } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() machine.LastSeen = &now lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). @@ -408,8 +405,7 @@ func (h *Headscale) PollNetMapStream( // since the stream opened, terminate connection. return false } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() machine.LastSeen = &now err = h.TouchMachine(machine) if err != nil { @@ -499,8 +495,7 @@ func (h *Headscale) PollNetMapStream( // since the stream opened, terminate connection. return false } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). Set(float64(now.Unix())) @@ -552,8 +547,7 @@ func (h *Headscale) PollNetMapStream( return false } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() machine.LastSeen = &now err = h.TouchMachine(machine) if err != nil { diff --git a/preauth_keys.go b/preauth_keys.go index 482e3b51..73f846df 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -8,7 +8,6 @@ import ( "time" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" - "github.com/spf13/viper" "google.golang.org/protobuf/types/known/timestamppb" "gorm.io/gorm" ) @@ -46,8 +45,7 @@ func (h *Headscale) CreatePreAuthKey( return nil, err } - location, _ := time.LoadLocation(viper.GetString("TZ")) - now := time.Now().In(location) + now := NowFromTZEnv() kstr, err := h.generateKey() if err != nil { return nil, err