mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Use NowFromTZEnv to get time
This commit is contained in:
		
							parent
							
								
									e16f1c1cd4
								
							
						
					
					
						commit
						91b1d0d877
					
				
							
								
								
									
										9
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								api.go
									
									
									
									
									
								
							| @ -15,7 +15,6 @@ import ( | |||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/klauspost/compress/zstd" | 	"github.com/klauspost/compress/zstd" | ||||||
| 	"github.com/rs/zerolog/log" | 	"github.com/rs/zerolog/log" | ||||||
| 	"github.com/spf13/viper" |  | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"tailscale.com/tailcfg" | 	"tailscale.com/tailcfg" | ||||||
| 	"tailscale.com/types/key" | 	"tailscale.com/types/key" | ||||||
| @ -121,8 +120,7 @@ func (h *Headscale) RegistrationHandler(ctx *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	location, _ := time.LoadLocation(viper.GetString("TZ")) | 	now := NowFromTZEnv() | ||||||
| 	now := time.Now().In(location) |  | ||||||
| 	machine, err := h.GetMachineByMachineKey(machineKey) | 	machine, err := h.GetMachineByMachineKey(machineKey) | ||||||
| 	if errors.Is(err, gorm.ErrRecordNotFound) { | 	if errors.Is(err, gorm.ErrRecordNotFound) { | ||||||
| 		log.Info().Str("machine", req.Hostinfo.Hostname).Msg("New machine") | 		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) { | 		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)
 | 			// 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
 | 			//   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) | 				h.handleMachineLogOut(ctx, machineKey, *machine) | ||||||
| 
 | 
 | ||||||
| 				return | 				return | ||||||
| @ -603,8 +601,7 @@ func (h *Headscale) handleAuthKey( | |||||||
| 		machine.AuthKeyID = uint(pak.ID) | 		machine.AuthKeyID = uint(pak.ID) | ||||||
| 		h.RefreshMachine(machine, registerRequest.Expiry) | 		h.RefreshMachine(machine, registerRequest.Expiry) | ||||||
| 	} else { | 	} else { | ||||||
| 		location, _ := time.LoadLocation(viper.GetString("TZ")) | 		now := NowFromTZEnv() | ||||||
| 		now := time.Now().In(location) |  | ||||||
| 		machineToRegister := Machine{ | 		machineToRegister := Machine{ | ||||||
| 			Name:           registerRequest.Hostinfo.Hostname, | 			Name:           registerRequest.Hostinfo.Hostname, | ||||||
| 			NamespaceID:    pak.Namespace.ID, | 			NamespaceID:    pak.Namespace.ID, | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								app.go
									
									
									
									
									
								
							| @ -27,7 +27,6 @@ import ( | |||||||
| 	zerolog "github.com/philip-bui/grpc-zerolog" | 	zerolog "github.com/philip-bui/grpc-zerolog" | ||||||
| 	zl "github.com/rs/zerolog" | 	zl "github.com/rs/zerolog" | ||||||
| 	"github.com/rs/zerolog/log" | 	"github.com/rs/zerolog/log" | ||||||
| 	"github.com/spf13/viper" |  | ||||||
| 	ginprometheus "github.com/zsais/go-gin-prometheus" | 	ginprometheus "github.com/zsais/go-gin-prometheus" | ||||||
| 	"golang.org/x/crypto/acme" | 	"golang.org/x/crypto/acme" | ||||||
| 	"golang.org/x/crypto/acme/autocert" | 	"golang.org/x/crypto/acme/autocert" | ||||||
| @ -792,8 +791,7 @@ func (h *Headscale) getTLSSettings() (*tls.Config, error) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (h *Headscale) setLastStateChangeToNow(namespace string) { | func (h *Headscale) setLastStateChangeToNow(namespace string) { | ||||||
| 	location, _ := time.LoadLocation(viper.GetString("TZ")) | 	now := NowFromTZEnv() | ||||||
| 	now := time.Now().In(location) |  | ||||||
| 	lastStateUpdate.WithLabelValues("", "headscale").Set(float64(now.Unix())) | 	lastStateUpdate.WithLabelValues("", "headscale").Set(float64(now.Unix())) | ||||||
| 	h.lastStateChange.Store(namespace, now) | 	h.lastStateChange.Store(namespace, now) | ||||||
| } | } | ||||||
| @ -816,8 +814,7 @@ func (h *Headscale) getLastStateChange(namespaces ...string) time.Time { | |||||||
| 	log.Trace().Msgf("Latest times %#v", times) | 	log.Trace().Msgf("Latest times %#v", times) | ||||||
| 
 | 
 | ||||||
| 	if len(times) == 0 { | 	if len(times) == 0 { | ||||||
| 		location, _ := time.LoadLocation(viper.GetString("TZ")) | 		return NowFromTZEnv() | ||||||
| 		return time.Now().In(location) |  | ||||||
| 	} else { | 	} else { | ||||||
| 		return times[0] | 		return times[0] | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -11,7 +11,6 @@ import ( | |||||||
| 	"github.com/fatih/set" | 	"github.com/fatih/set" | ||||||
| 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||||
| 	"github.com/rs/zerolog/log" | 	"github.com/rs/zerolog/log" | ||||||
| 	"github.com/spf13/viper" |  | ||||||
| 	"google.golang.org/protobuf/types/known/timestamppb" | 	"google.golang.org/protobuf/types/known/timestamppb" | ||||||
| 	"inet.af/netaddr" | 	"inet.af/netaddr" | ||||||
| 	"tailscale.com/tailcfg" | 	"tailscale.com/tailcfg" | ||||||
| @ -117,9 +116,8 @@ func (machine Machine) isExpired() bool { | |||||||
| 	if machine.Expiry == nil || machine.Expiry.IsZero() { | 	if machine.Expiry == nil || machine.Expiry.IsZero() { | ||||||
| 		return false | 		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 { | func containsAddresses(inputs []string, addrs []string) bool { | ||||||
|  | |||||||
							
								
								
									
										16
									
								
								poll.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								poll.go
									
									
									
									
									
								
							| @ -10,7 +10,6 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| 	"github.com/rs/zerolog/log" | 	"github.com/rs/zerolog/log" | ||||||
| 	"github.com/spf13/viper" |  | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"tailscale.com/tailcfg" | 	"tailscale.com/tailcfg" | ||||||
| 	"tailscale.com/types/key" | 	"tailscale.com/types/key" | ||||||
| @ -98,8 +97,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | |||||||
| 	machine.Name = hname | 	machine.Name = hname | ||||||
| 	machine.HostInfo = HostInfo(*req.Hostinfo) | 	machine.HostInfo = HostInfo(*req.Hostinfo) | ||||||
| 	machine.DiscoKey = DiscoPublicKeyStripPrefix(req.DiscoKey) | 	machine.DiscoKey = DiscoPublicKeyStripPrefix(req.DiscoKey) | ||||||
| 	location, _ := time.LoadLocation(viper.GetString("TZ")) | 	now := NowFromTZEnv() | ||||||
| 	now := time.Now().In(location) |  | ||||||
| 
 | 
 | ||||||
| 	// update ACLRules with peer informations (to update server tags if necessary)
 | 	// update ACLRules with peer informations (to update server tags if necessary)
 | ||||||
| 	if h.aclPolicy != nil { | 	if h.aclPolicy != nil { | ||||||
| @ -341,8 +339,7 @@ func (h *Headscale) PollNetMapStream( | |||||||
| 				// since the stream opened, terminate connection.
 | 				// since the stream opened, terminate connection.
 | ||||||
| 				return false | 				return false | ||||||
| 			} | 			} | ||||||
| 			location, _ := time.LoadLocation(viper.GetString("TZ")) | 			now := NowFromTZEnv() | ||||||
| 			now := time.Now().In(location) |  | ||||||
| 			machine.LastSeen = &now | 			machine.LastSeen = &now | ||||||
| 
 | 
 | ||||||
| 			lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | 			lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | ||||||
| @ -408,8 +405,7 @@ func (h *Headscale) PollNetMapStream( | |||||||
| 				// since the stream opened, terminate connection.
 | 				// since the stream opened, terminate connection.
 | ||||||
| 				return false | 				return false | ||||||
| 			} | 			} | ||||||
| 			location, _ := time.LoadLocation(viper.GetString("TZ")) | 			now := NowFromTZEnv() | ||||||
| 			now := time.Now().In(location) |  | ||||||
| 			machine.LastSeen = &now | 			machine.LastSeen = &now | ||||||
| 			err = h.TouchMachine(machine) | 			err = h.TouchMachine(machine) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| @ -499,8 +495,7 @@ func (h *Headscale) PollNetMapStream( | |||||||
| 					// since the stream opened, terminate connection.
 | 					// since the stream opened, terminate connection.
 | ||||||
| 					return false | 					return false | ||||||
| 				} | 				} | ||||||
| 				location, _ := time.LoadLocation(viper.GetString("TZ")) | 				now := NowFromTZEnv() | ||||||
| 				now := time.Now().In(location) |  | ||||||
| 
 | 
 | ||||||
| 				lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | 				lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | ||||||
| 					Set(float64(now.Unix())) | 					Set(float64(now.Unix())) | ||||||
| @ -552,8 +547,7 @@ func (h *Headscale) PollNetMapStream( | |||||||
| 				return false | 				return false | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			location, _ := time.LoadLocation(viper.GetString("TZ")) | 			now := NowFromTZEnv() | ||||||
| 			now := time.Now().In(location) |  | ||||||
| 			machine.LastSeen = &now | 			machine.LastSeen = &now | ||||||
| 			err = h.TouchMachine(machine) | 			err = h.TouchMachine(machine) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
|  | |||||||
| @ -8,7 +8,6 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||||
| 	"github.com/spf13/viper" |  | ||||||
| 	"google.golang.org/protobuf/types/known/timestamppb" | 	"google.golang.org/protobuf/types/known/timestamppb" | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| ) | ) | ||||||
| @ -46,8 +45,7 @@ func (h *Headscale) CreatePreAuthKey( | |||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	location, _ := time.LoadLocation(viper.GetString("TZ")) | 	now := NowFromTZEnv() | ||||||
| 	now := time.Now().In(location) |  | ||||||
| 	kstr, err := h.generateKey() | 	kstr, err := h.generateKey() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user