1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-08-28 13:49:04 +02:00

Use NowFromTZEnv to get time

This commit is contained in:
Deon Thomas 2022-05-10 14:18:10 -04:00
parent e16f1c1cd4
commit 91b1d0d877
No known key found for this signature in database
GPG Key ID: 0FF7DAE12CA62D67
5 changed files with 12 additions and 28 deletions

9
api.go
View File

@ -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,

7
app.go
View File

@ -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]
}

View File

@ -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 {

16
poll.go
View File

@ -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 {

View File

@ -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