Skip to content

Commit

Permalink
Lint fixes 5/n
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Jun 26, 2022
1 parent 03ced0e commit c810b24
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 56 deletions.
89 changes: 78 additions & 11 deletions api.go
Expand Up @@ -38,7 +38,13 @@ func (h *Headscale) KeyHandler(
) {
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write([]byte(MachinePublicKeyStripPrefix(h.privateKey.Public())))
_, err := writer.Write([]byte(MachinePublicKeyStripPrefix(h.privateKey.Public())))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

type registerWebAPITemplateConfig struct {
Expand Down Expand Up @@ -72,7 +78,13 @@ func (h *Headscale) RegisterWebAPI(
if machineKeyStr == "" {
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte("Wrong params"))
_, err := writer.Write([]byte("Wrong params"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand All @@ -87,14 +99,26 @@ func (h *Headscale) RegisterWebAPI(
Msg("Could not render register web API template")
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("Could not render register web API template"))
_, err = writer.Write([]byte("Could not render register web API template"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}

writer.Header().Set("Content-Type", "text/html; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(content.Bytes())
_, err := writer.Write(content.Bytes())
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

// RegistrationHandler handles the actual registration process of a machine
Expand Down Expand Up @@ -407,7 +431,13 @@ func (h *Headscale) handleMachineLogOut(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

func (h *Headscale) handleMachineValidRegistration(
Expand Down Expand Up @@ -445,7 +475,13 @@ func (h *Headscale) handleMachineValidRegistration(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

func (h *Headscale) handleMachineExpired(
Expand Down Expand Up @@ -493,7 +529,13 @@ func (h *Headscale) handleMachineExpired(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

func (h *Headscale) handleMachineRefreshKey(
Expand Down Expand Up @@ -535,7 +577,13 @@ func (h *Headscale) handleMachineRefreshKey(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

func (h *Headscale) handleMachineRegistrationNew(
Expand Down Expand Up @@ -574,7 +622,13 @@ func (h *Headscale) handleMachineRegistrationNew(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

// TODO: check if any locks are needed around IP allocation.
Expand Down Expand Up @@ -618,7 +672,13 @@ func (h *Headscale) handleAuthKey(

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusUnauthorized)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

log.Error().
Caller().
Expand Down Expand Up @@ -721,7 +781,14 @@ func (h *Headscale) handleAuthKey(
Inc()
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
writer.Write(respBody)
_, err = writer.Write(respBody)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

log.Info().
Str("func", "handleAuthKey").
Str("machine", registerRequest.Hostinfo.Hostname).
Expand Down
32 changes: 28 additions & 4 deletions app.go
Expand Up @@ -348,7 +348,13 @@ func (h *Headscale) httpAuthenticationMiddleware(next http.Handler) http.Handler
Str("client_address", req.RemoteAddr).
Msg(`missing "Bearer " prefix in "Authorization" header`)
writer.WriteHeader(http.StatusUnauthorized)
writer.Write([]byte("Unauthorized"))
_, err := writer.Write([]byte("Unauthorized"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand All @@ -362,7 +368,13 @@ func (h *Headscale) httpAuthenticationMiddleware(next http.Handler) http.Handler
Msg("failed to validate token")

writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("Unauthorized"))
_, err := writer.Write([]byte("Unauthorized"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand All @@ -373,7 +385,13 @@ func (h *Headscale) httpAuthenticationMiddleware(next http.Handler) http.Handler
Msg("invalid token")

writer.WriteHeader(http.StatusUnauthorized)
writer.Write([]byte("Unauthorized"))
_, err := writer.Write([]byte("Unauthorized"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand Down Expand Up @@ -409,7 +427,13 @@ func (h *Headscale) createRouter(grpcMux *runtime.ServeMux) *mux.Router {
"/health",
func(writer http.ResponseWriter, req *http.Request) {
writer.WriteHeader(http.StatusOK)
writer.Write([]byte("{\"healthy\": \"ok\"}"))
_, err := writer.Write([]byte("{\"healthy\": \"ok\"}"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}).Methods(http.MethodGet)

router.HandleFunc("/key", h.KeyHandler).Methods(http.MethodGet)
Expand Down
40 changes: 35 additions & 5 deletions derp_server.go
Expand Up @@ -105,7 +105,13 @@ func (h *Headscale) DERPHandler(
}
writer.Header().Set("Content-Type", "text/plain")
writer.WriteHeader(http.StatusUpgradeRequired)
writer.Write([]byte("DERP requires connection upgrade"))
_, err := writer.Write([]byte("DERP requires connection upgrade"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand All @@ -117,7 +123,13 @@ func (h *Headscale) DERPHandler(
log.Error().Caller().Msg("DERP requires Hijacker interface from Gin")
writer.Header().Set("Content-Type", "text/plain")
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("HTTP does not support general TCP support"))
_, err := writer.Write([]byte("HTTP does not support general TCP support"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand All @@ -127,7 +139,13 @@ func (h *Headscale) DERPHandler(
log.Error().Caller().Err(err).Msgf("Hijack failed")
writer.Header().Set("Content-Type", "text/plain")
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("HTTP does not support general TCP support"))
_, err = writer.Write([]byte("HTTP does not support general TCP support"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}

return
}
Expand Down Expand Up @@ -160,7 +178,13 @@ func (h *Headscale) DERPProbeHandler(
writer.WriteHeader(http.StatusOK)
default:
writer.WriteHeader(http.StatusMethodNotAllowed)
writer.Write([]byte("bogus probe method"))
_, err := writer.Write([]byte("bogus probe method"))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}
}

Expand Down Expand Up @@ -196,7 +220,13 @@ func (h *Headscale) DERPBootstrapDNSHandler(
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
json.NewEncoder(writer).Encode(dnsEntries)
err := json.NewEncoder(writer).Encode(dnsEntries)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
}

// ServeSTUN starts a STUN server on the configured addr.
Expand Down

0 comments on commit c810b24

Please sign in to comment.