Lint fixes 4/n

This commit is contained in:
Juan Font Alonso 2022-06-26 12:06:25 +02:00
parent c859bea0cf
commit 03ced0ecfe

87
poll.go
View file

@ -34,16 +34,16 @@ const machineNameContextKey = contextKey("machineName")
// //
// At this moment the updates are sent in a quite horrendous way, but they kinda work. // At this moment the updates are sent in a quite horrendous way, but they kinda work.
func (h *Headscale) PollNetMapHandler( func (h *Headscale) PollNetMapHandler(
w http.ResponseWriter, writer http.ResponseWriter,
r *http.Request, req *http.Request,
) { ) {
vars := mux.Vars(r) vars := mux.Vars(req)
machineKeyStr, ok := vars["mkey"] machineKeyStr, ok := vars["mkey"]
if !ok || machineKeyStr == "" { if !ok || machineKeyStr == "" {
log.Error(). log.Error().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Msg("No machine key in request") Msg("No machine key in request")
http.Error(w, "No machine key in request", http.StatusBadRequest) http.Error(writer, "No machine key in request", http.StatusBadRequest)
return return
} }
@ -51,7 +51,7 @@ func (h *Headscale) PollNetMapHandler(
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Str("id", machineKeyStr). Str("id", machineKeyStr).
Msg("PollNetMapHandler called") Msg("PollNetMapHandler called")
body, _ := io.ReadAll(r.Body) body, _ := io.ReadAll(req.Body)
var machineKey key.MachinePublic var machineKey key.MachinePublic
err := machineKey.UnmarshalText([]byte(MachinePublicKeyEnsurePrefix(machineKeyStr))) err := machineKey.UnmarshalText([]byte(MachinePublicKeyEnsurePrefix(machineKeyStr)))
@ -61,18 +61,18 @@ func (h *Headscale) PollNetMapHandler(
Err(err). Err(err).
Msg("Cannot parse client key") Msg("Cannot parse client key")
http.Error(w, "Cannot parse client key", http.StatusBadRequest) http.Error(writer, "Cannot parse client key", http.StatusBadRequest)
return return
} }
req := tailcfg.MapRequest{} mapRequest := tailcfg.MapRequest{}
err = decode(body, &req, &machineKey, h.privateKey) err = decode(body, &req, &machineKey, h.privateKey)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Err(err). Err(err).
Msg("Cannot decode message") Msg("Cannot decode message")
http.Error(w, "Cannot decode message", http.StatusBadRequest) http.Error(writer, "Cannot decode message", http.StatusBadRequest)
return return
} }
@ -84,14 +84,14 @@ func (h *Headscale) PollNetMapHandler(
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Msgf("Ignoring request, cannot find machine with key %s", machineKey.String()) Msgf("Ignoring request, cannot find machine with key %s", machineKey.String())
http.Error(w, "", http.StatusUnauthorized) http.Error(writer, "", http.StatusUnauthorized)
return return
} }
log.Error(). log.Error().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Msgf("Failed to fetch machine from the database with Machine key: %s", machineKey.String()) Msgf("Failed to fetch machine from the database with Machine key: %s", machineKey.String())
http.Error(w, "", http.StatusInternalServerError) http.Error(writer, "", http.StatusInternalServerError)
return return
} }
@ -101,9 +101,9 @@ func (h *Headscale) PollNetMapHandler(
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Msg("Found machine in database") Msg("Found machine in database")
machine.Hostname = req.Hostinfo.Hostname machine.Hostname = mapRequest.Hostinfo.Hostname
machine.HostInfo = HostInfo(*req.Hostinfo) machine.HostInfo = HostInfo(*mapRequest.Hostinfo)
machine.DiscoKey = DiscoPublicKeyStripPrefix(req.DiscoKey) machine.DiscoKey = DiscoPublicKeyStripPrefix(mapRequest.DiscoKey)
now := time.Now().UTC() now := time.Now().UTC()
// update ACLRules with peer informations (to update server tags if necessary) // update ACLRules with peer informations (to update server tags if necessary)
@ -125,8 +125,8 @@ func (h *Headscale) PollNetMapHandler(
// //
// The intended use is for clients to discover the DERP map at start-up // The intended use is for clients to discover the DERP map at start-up
// before their first real endpoint update. // before their first real endpoint update.
if !req.ReadOnly { if !mapRequest.ReadOnly {
machine.Endpoints = req.Endpoints machine.Endpoints = mapRequest.Endpoints
machine.LastSeen = &now machine.LastSeen = &now
} }
@ -138,13 +138,13 @@ func (h *Headscale) PollNetMapHandler(
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Err(err). Err(err).
Msg("Failed to persist/update machine in the database") Msg("Failed to persist/update machine in the database")
http.Error(w, "", http.StatusInternalServerError) http.Error(writer, "", http.StatusInternalServerError)
return return
} }
} }
data, err := h.getMapResponse(machineKey, req, machine) data, err := h.getMapResponse(machineKey, mapRequest, machine)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
@ -152,7 +152,7 @@ func (h *Headscale) PollNetMapHandler(
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Err(err). Err(err).
Msg("Failed to get Map response") Msg("Failed to get Map response")
http.Error(w, "", http.StatusInternalServerError) http.Error(writer, "", http.StatusInternalServerError)
return return
} }
@ -166,20 +166,20 @@ func (h *Headscale) PollNetMapHandler(
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Str("id", machineKeyStr). Str("id", machineKeyStr).
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Bool("readOnly", req.ReadOnly). Bool("readOnly", mapRequest.ReadOnly).
Bool("omitPeers", req.OmitPeers). Bool("omitPeers", mapRequest.OmitPeers).
Bool("stream", req.Stream). Bool("stream", mapRequest.Stream).
Msg("Client map request processed") Msg("Client map request processed")
if req.ReadOnly { if mapRequest.ReadOnly {
log.Info(). log.Info().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Msg("Client is starting up. Probably interested in a DERP map") Msg("Client is starting up. Probably interested in a DERP map")
w.Header().Set("Content-Type", "application/json; charset=utf-8") writer.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
w.Write(data) writer.Write(data)
return return
} }
@ -206,14 +206,14 @@ func (h *Headscale) PollNetMapHandler(
keepAliveChan := make(chan []byte) keepAliveChan := make(chan []byte)
if req.OmitPeers && !req.Stream { if mapRequest.OmitPeers && !mapRequest.Stream {
log.Info(). log.Info().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Msg("Client sent endpoint update and is ok with a response without peer list") Msg("Client sent endpoint update and is ok with a response without peer list")
w.Header().Set("Content-Type", "application/json; charset=utf-8") writer.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
w.Write(data) writer.Write(data)
// It sounds like we should update the nodes when we have received a endpoint update // 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. // even tho the comments in the tailscale code dont explicitly say so.
updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "endpoint-update"). updateRequestsFromNode.WithLabelValues(machine.Namespace.Name, machine.Hostname, "endpoint-update").
@ -221,12 +221,12 @@ func (h *Headscale) PollNetMapHandler(
updateChan <- struct{}{} updateChan <- struct{}{}
return return
} else if req.OmitPeers && req.Stream { } else if mapRequest.OmitPeers && mapRequest.Stream {
log.Warn(). log.Warn().
Str("handler", "PollNetMap"). Str("handler", "PollNetMap").
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Msg("Ignoring request, don't know how to handle it") Msg("Ignoring request, don't know how to handle it")
http.Error(w, "", http.StatusBadRequest) http.Error(writer, "", http.StatusBadRequest)
return return
} }
@ -250,10 +250,10 @@ func (h *Headscale) PollNetMapHandler(
updateChan <- struct{}{} updateChan <- struct{}{}
h.PollNetMapStream( h.PollNetMapStream(
w, writer,
r,
machine,
req, req,
machine,
mapRequest,
machineKey, machineKey,
pollDataChan, pollDataChan,
keepAliveChan, keepAliveChan,
@ -270,8 +270,8 @@ func (h *Headscale) PollNetMapHandler(
// stream logic, ensuring we communicate updates and data // stream logic, ensuring we communicate updates and data
// to the connected clients. // to the connected clients.
func (h *Headscale) PollNetMapStream( func (h *Headscale) PollNetMapStream(
w http.ResponseWriter, writer http.ResponseWriter,
r *http.Request, req *http.Request,
machine *Machine, machine *Machine,
mapRequest tailcfg.MapRequest, mapRequest tailcfg.MapRequest,
machineKey key.MachinePublic, machineKey key.MachinePublic,
@ -279,7 +279,7 @@ func (h *Headscale) PollNetMapStream(
keepAliveChan chan []byte, keepAliveChan chan []byte,
updateChan chan struct{}, updateChan chan struct{},
) { ) {
ctx := context.WithValue(r.Context(), machineNameContextKey, machine.Hostname) ctx := context.WithValue(req.Context(), machineNameContextKey, machine.Hostname)
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -312,7 +312,7 @@ func (h *Headscale) PollNetMapStream(
Str("channel", "pollData"). Str("channel", "pollData").
Int("bytes", len(data)). Int("bytes", len(data)).
Msg("Sending data received via pollData channel") Msg("Sending data received via pollData channel")
_, err := w.Write(data) _, err := writer.Write(data)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -323,7 +323,7 @@ func (h *Headscale) PollNetMapStream(
return return
} }
w.(http.Flusher).Flush() writer.(http.Flusher).Flush()
log.Trace(). log.Trace().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -380,7 +380,7 @@ func (h *Headscale) PollNetMapStream(
Str("channel", "keepAlive"). Str("channel", "keepAlive").
Int("bytes", len(data)). Int("bytes", len(data)).
Msg("Sending keep alive message") Msg("Sending keep alive message")
_, err := w.Write(data) _, err := writer.Write(data)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -391,7 +391,7 @@ func (h *Headscale) PollNetMapStream(
return return
} }
w.(http.Flusher).Flush() writer.(http.Flusher).Flush()
log.Trace(). log.Trace().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -467,7 +467,7 @@ func (h *Headscale) PollNetMapStream(
return return
} }
_, err = w.Write(data) _, err = writer.Write(data)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -480,7 +480,7 @@ func (h *Headscale) PollNetMapStream(
return return
} }
w.(http.Flusher).Flush() writer.(http.Flusher).Flush()
log.Trace(). log.Trace().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
@ -581,6 +581,7 @@ func (h *Headscale) PollNetMapStream(
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
Str("machine", machine.Hostname). Str("machine", machine.Hostname).
Msg("The long-poll handler is shutting down") Msg("The long-poll handler is shutting down")
return return
} }
} }