mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-31 16:11:03 +01:00 
			
		
		
		
	Use new logic and fields for dns
This commit is contained in:
		
							parent
							
								
									6b79679cb4
								
							
						
					
					
						commit
						14994cb6cc
					
				| @ -237,6 +237,7 @@ func (n *Namespace) toProto() *v1.Namespace { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // TODO(kradalby): We need these fields to be unique, we need to add a hash or something at the end. | ||||
| // NormalizeToFQDNRules will replace forbidden chars in namespace | ||||
| // it can also return an error if the namespace doesn't respect RFC 952 and 1123. | ||||
| func NormalizeToFQDNRules(name string, stripEmailDomain bool) (string, error) { | ||||
|  | ||||
							
								
								
									
										2
									
								
								oidc.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								oidc.go
									
									
									
									
									
								
							| @ -231,7 +231,7 @@ func (h *Headscale) OIDCCallback(ctx *gin.Context) { | ||||
| 	if machine != nil { | ||||
| 		log.Trace(). | ||||
| 			Caller(). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msg("machine already registered, reauthenticating") | ||||
| 
 | ||||
| 		h.RefreshMachine(machine, time.Time{}) | ||||
|  | ||||
							
								
								
									
										115
									
								
								poll.go
									
									
									
									
									
								
							
							
						
						
									
										115
									
								
								poll.go
									
									
									
									
									
								
							| @ -80,21 +80,10 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	log.Trace(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("id", ctx.Param("id")). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Found machine in database") | ||||
| 
 | ||||
| 	hname, err := NormalizeToFQDNRules( | ||||
| 		req.Hostinfo.Hostname, | ||||
| 		h.cfg.OIDC.StripEmaildomain, | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		log.Error(). | ||||
| 			Caller(). | ||||
| 			Str("func", "handleAuthKey"). | ||||
| 			Str("hostinfo.name", req.Hostinfo.Hostname). | ||||
| 			Err(err) | ||||
| 	} | ||||
| 	machine.Name = hname | ||||
| 	machine.Hostname = req.Hostinfo.Hostname | ||||
| 	machine.HostInfo = HostInfo(*req.Hostinfo) | ||||
| 	machine.DiscoKey = DiscoPublicKeyStripPrefix(req.DiscoKey) | ||||
| 	now := time.Now().UTC() | ||||
| @ -106,7 +95,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 			log.Error(). | ||||
| 				Caller(). | ||||
| 				Str("func", "handleAuthKey"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Err(err) | ||||
| 		} | ||||
| 	} | ||||
| @ -129,7 +118,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 		log.Error(). | ||||
| 			Str("handler", "PollNetMap"). | ||||
| 			Str("id", ctx.Param("id")). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Err(err). | ||||
| 			Msg("Failed to get Map response") | ||||
| 		ctx.String(http.StatusInternalServerError, ":(") | ||||
| @ -145,7 +134,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	log.Debug(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("id", ctx.Param("id")). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Bool("readOnly", req.ReadOnly). | ||||
| 		Bool("omitPeers", req.OmitPeers). | ||||
| 		Bool("stream", req.Stream). | ||||
| @ -154,7 +143,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	if req.ReadOnly { | ||||
| 		log.Info(). | ||||
| 			Str("handler", "PollNetMap"). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msg("Client is starting up. Probably interested in a DERP map") | ||||
| 		ctx.Data(http.StatusOK, "application/json; charset=utf-8", data) | ||||
| 
 | ||||
| @ -172,27 +161,27 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	log.Trace(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("id", ctx.Param("id")). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Loading or creating update channel") | ||||
| 
 | ||||
| 	const chanSize = 8 | ||||
| 	updateChan := make(chan struct{}, chanSize) | ||||
| 
 | ||||
| 	pollDataChan := make(chan []byte, chanSize) | ||||
| 	defer closeChanWithLog(pollDataChan, machine.Name, "pollDataChan") | ||||
| 	defer closeChanWithLog(pollDataChan, machine.Hostname, "pollDataChan") | ||||
| 
 | ||||
| 	keepAliveChan := make(chan []byte) | ||||
| 
 | ||||
| 	if req.OmitPeers && !req.Stream { | ||||
| 		log.Info(). | ||||
| 			Str("handler", "PollNetMap"). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msg("Client sent endpoint update and is ok with a response without peer list") | ||||
| 		ctx.Data(http.StatusOK, "application/json; charset=utf-8", data) | ||||
| 
 | ||||
| 		// It sounds like we should update the nodes when we have received a endpoint update | ||||
| 		// even tho the comments in the tailscale code dont explicitly say so. | ||||
| 		updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Name, "endpoint-update"). | ||||
| 		updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "endpoint-update"). | ||||
| 			Inc() | ||||
| 		updateChan <- struct{}{} | ||||
| 
 | ||||
| @ -200,7 +189,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	} else if req.OmitPeers && req.Stream { | ||||
| 		log.Warn(). | ||||
| 			Str("handler", "PollNetMap"). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msg("Ignoring request, don't know how to handle it") | ||||
| 		ctx.String(http.StatusBadRequest, "") | ||||
| 
 | ||||
| @ -209,19 +198,19 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 
 | ||||
| 	log.Info(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Client is ready to access the tailnet") | ||||
| 	log.Info(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Sending initial map") | ||||
| 	pollDataChan <- data | ||||
| 
 | ||||
| 	log.Info(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Notifying peers") | ||||
| 	updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Name, "full-update"). | ||||
| 	updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "full-update"). | ||||
| 		Inc() | ||||
| 	updateChan <- struct{}{} | ||||
| 
 | ||||
| @ -237,7 +226,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) { | ||||
| 	log.Trace(). | ||||
| 		Str("handler", "PollNetMap"). | ||||
| 		Str("id", ctx.Param("id")). | ||||
| 		Str("machine", machine.Name). | ||||
| 		Str("machine", machine.Hostname). | ||||
| 		Msg("Finished stream, closing PollNetMap session") | ||||
| } | ||||
| 
 | ||||
| @ -272,7 +261,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		ctx := context.WithValue(ctx.Request.Context(), "machineName", machine.Name) | ||||
| 		ctx := context.WithValue(ctx.Request.Context(), "machineName", machine.Hostname) | ||||
| 
 | ||||
| 		ctx, cancel := context.WithCancel(ctx) | ||||
| 		defer cancel() | ||||
| @ -290,19 +279,19 @@ func (h *Headscale) PollNetMapStream( | ||||
| 	ctx.Stream(func(writer io.Writer) bool { | ||||
| 		log.Trace(). | ||||
| 			Str("handler", "PollNetMapStream"). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msg("Waiting for data to stream...") | ||||
| 
 | ||||
| 		log.Trace(). | ||||
| 			Str("handler", "PollNetMapStream"). | ||||
| 			Str("machine", machine.Name). | ||||
| 			Str("machine", machine.Hostname). | ||||
| 			Msgf("pollData is %#v, keepAliveChan is %#v, updateChan is %#v", pollDataChan, keepAliveChan, updateChan) | ||||
| 
 | ||||
| 		select { | ||||
| 		case data := <-pollDataChan: | ||||
| 			log.Trace(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Str("channel", "pollData"). | ||||
| 				Int("bytes", len(data)). | ||||
| 				Msg("Sending data received via pollData channel") | ||||
| @ -310,7 +299,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "pollData"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot write data") | ||||
| @ -319,7 +308,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			} | ||||
| 			log.Trace(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Str("channel", "pollData"). | ||||
| 				Int("bytes", len(data)). | ||||
| 				Msg("Data from pollData channel written successfully") | ||||
| @ -330,7 +319,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "pollData"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine from database") | ||||
| @ -342,7 +331,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			now := time.Now().UTC() | ||||
| 			machine.LastSeen = &now | ||||
| 
 | ||||
| 			lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | ||||
| 			lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Hostname). | ||||
| 				Set(float64(now.Unix())) | ||||
| 			machine.LastSuccessfulUpdate = &now | ||||
| 
 | ||||
| @ -350,14 +339,14 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "pollData"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine LastSuccessfulUpdate") | ||||
| 			} else { | ||||
| 				log.Trace(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "pollData"). | ||||
| 					Int("bytes", len(data)). | ||||
| 					Msg("Machine entry in database updated successfully after sending pollData") | ||||
| @ -368,7 +357,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 		case data := <-keepAliveChan: | ||||
| 			log.Trace(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Str("channel", "keepAlive"). | ||||
| 				Int("bytes", len(data)). | ||||
| 				Msg("Sending keep alive message") | ||||
| @ -376,7 +365,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "keepAlive"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot write keep alive message") | ||||
| @ -385,7 +374,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			} | ||||
| 			log.Trace(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Str("channel", "keepAlive"). | ||||
| 				Int("bytes", len(data)). | ||||
| 				Msg("Keep alive sent successfully") | ||||
| @ -396,7 +385,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "keepAlive"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine from database") | ||||
| @ -411,14 +400,14 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "keepAlive"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine LastSeen") | ||||
| 			} else { | ||||
| 				log.Trace(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "keepAlive"). | ||||
| 					Int("bytes", len(data)). | ||||
| 					Msg("Machine updated successfully after sending keep alive") | ||||
| @ -429,10 +418,10 @@ func (h *Headscale) PollNetMapStream( | ||||
| 		case <-updateChan: | ||||
| 			log.Trace(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Str("channel", "update"). | ||||
| 				Msg("Received a request for update") | ||||
| 			updateRequestsReceivedOnChannel.WithLabelValues(machine.Namespace.Name, machine.Name). | ||||
| 			updateRequestsReceivedOnChannel.WithLabelValues(machine.Namespace.Name, machine.Hostname). | ||||
| 				Inc() | ||||
| 			if h.isOutdated(machine) { | ||||
| 				var lastUpdate time.Time | ||||
| @ -441,15 +430,15 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				} | ||||
| 				log.Debug(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Time("last_successful_update", lastUpdate). | ||||
| 					Time("last_state_change", h.getLastStateChange(machine.Namespace.Name)). | ||||
| 					Msgf("There has been updates since the last successful update to %s", machine.Name) | ||||
| 					Msgf("There has been updates since the last successful update to %s", machine.Hostname) | ||||
| 				data, err := h.getMapResponse(machineKey, mapRequest, machine) | ||||
| 				if err != nil { | ||||
| 					log.Error(). | ||||
| 						Str("handler", "PollNetMapStream"). | ||||
| 						Str("machine", machine.Name). | ||||
| 						Str("machine", machine.Hostname). | ||||
| 						Str("channel", "update"). | ||||
| 						Err(err). | ||||
| 						Msg("Could not get the map update") | ||||
| @ -458,21 +447,21 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				if err != nil { | ||||
| 					log.Error(). | ||||
| 						Str("handler", "PollNetMapStream"). | ||||
| 						Str("machine", machine.Name). | ||||
| 						Str("machine", machine.Hostname). | ||||
| 						Str("channel", "update"). | ||||
| 						Err(err). | ||||
| 						Msg("Could not write the map response") | ||||
| 					updateRequestsSentToNode.WithLabelValues(machine.Namespace.Name, machine.Name, "failed"). | ||||
| 					updateRequestsSentToNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "failed"). | ||||
| 						Inc() | ||||
| 
 | ||||
| 					return false | ||||
| 				} | ||||
| 				log.Trace(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "update"). | ||||
| 					Msg("Updated Map has been sent") | ||||
| 				updateRequestsSentToNode.WithLabelValues(machine.Namespace.Name, machine.Name, "success"). | ||||
| 				updateRequestsSentToNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "success"). | ||||
| 					Inc() | ||||
| 
 | ||||
| 				// Keep track of the last successful update, | ||||
| @ -486,7 +475,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				if err != nil { | ||||
| 					log.Error(). | ||||
| 						Str("handler", "PollNetMapStream"). | ||||
| 						Str("machine", machine.Name). | ||||
| 						Str("machine", machine.Hostname). | ||||
| 						Str("channel", "update"). | ||||
| 						Err(err). | ||||
| 						Msg("Cannot update machine from database") | ||||
| @ -497,7 +486,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				} | ||||
| 				now := time.Now().UTC() | ||||
| 
 | ||||
| 				lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Name). | ||||
| 				lastStateUpdate.WithLabelValues(machine.Namespace.Name, machine.Hostname). | ||||
| 					Set(float64(now.Unix())) | ||||
| 				machine.LastSuccessfulUpdate = &now | ||||
| 
 | ||||
| @ -505,7 +494,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				if err != nil { | ||||
| 					log.Error(). | ||||
| 						Str("handler", "PollNetMapStream"). | ||||
| 						Str("machine", machine.Name). | ||||
| 						Str("machine", machine.Hostname). | ||||
| 						Str("channel", "update"). | ||||
| 						Err(err). | ||||
| 						Msg("Cannot update machine LastSuccessfulUpdate") | ||||
| @ -517,10 +506,10 @@ func (h *Headscale) PollNetMapStream( | ||||
| 				} | ||||
| 				log.Trace(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Time("last_successful_update", lastUpdate). | ||||
| 					Time("last_state_change", h.getLastStateChange(machine.Namespace.Name)). | ||||
| 					Msgf("%s is up to date", machine.Name) | ||||
| 					Msgf("%s is up to date", machine.Hostname) | ||||
| 			} | ||||
| 
 | ||||
| 			return true | ||||
| @ -528,7 +517,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 		case <-ctx.Request.Context().Done(): | ||||
| 			log.Info(). | ||||
| 				Str("handler", "PollNetMapStream"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Msg("The client has closed the connection") | ||||
| 				// TODO: Abstract away all the database calls, this can cause race conditions | ||||
| 				// when an outdated machine object is kept alive, e.g. db is update from | ||||
| @ -537,7 +526,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "Done"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine from database") | ||||
| @ -552,7 +541,7 @@ func (h *Headscale) PollNetMapStream( | ||||
| 			if err != nil { | ||||
| 				log.Error(). | ||||
| 					Str("handler", "PollNetMapStream"). | ||||
| 					Str("machine", machine.Name). | ||||
| 					Str("machine", machine.Hostname). | ||||
| 					Str("channel", "Done"). | ||||
| 					Err(err). | ||||
| 					Msg("Cannot update machine LastSeen") | ||||
| @ -603,16 +592,16 @@ func (h *Headscale) scheduledPollWorker( | ||||
| 
 | ||||
| 			log.Debug(). | ||||
| 				Str("func", "keepAlive"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Msg("Sending keepalive") | ||||
| 			keepAliveChan <- data | ||||
| 
 | ||||
| 		case <-updateCheckerTicker.C: | ||||
| 			log.Debug(). | ||||
| 				Str("func", "scheduledPollWorker"). | ||||
| 				Str("machine", machine.Name). | ||||
| 				Str("machine", machine.Hostname). | ||||
| 				Msg("Sending update request") | ||||
| 			updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Name, "scheduled-update"). | ||||
| 			updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "scheduled-update"). | ||||
| 				Inc() | ||||
| 			updateChan <- struct{}{} | ||||
| 		} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user