mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-29 18:33:05 +00:00
refactor(verify-client): simplify error handling
This commit is contained in:
parent
bb79471139
commit
dfebd176df
1 changed files with 19 additions and 22 deletions
|
@ -71,37 +71,34 @@ func (h *Headscale) VerifyHandler(
|
|||
Str("handler", "/verify").
|
||||
Msg("verify client")
|
||||
|
||||
body, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("handler", "/verify").
|
||||
Err(err).
|
||||
Msg("Cannot read request body")
|
||||
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
doVerify := func() (bool, error) {
|
||||
body, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("cannot read request body: %w", err)
|
||||
}
|
||||
|
||||
var derpAdmitClientRequest tailcfg.DERPAdmitClientRequest
|
||||
if err := json.Unmarshal(body, &derpAdmitClientRequest); err != nil {
|
||||
return false, fmt.Errorf("cannot parse derpAdmitClientRequest: %w", err)
|
||||
}
|
||||
|
||||
nodes, err := h.db.ListNodes()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("cannot list nodes: %w", err)
|
||||
}
|
||||
|
||||
return nodes.ContainsNodeKey(derpAdmitClientRequest.NodePublic), nil
|
||||
}
|
||||
|
||||
var derpAdmitClientRequest tailcfg.DERPAdmitClientRequest
|
||||
if err := json.Unmarshal(body, &derpAdmitClientRequest); err != nil {
|
||||
log.Error().
|
||||
Caller().
|
||||
Err(err).
|
||||
Msg("Cannot parse derpAdmitClientRequest")
|
||||
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
nodes, err := h.db.ListNodes()
|
||||
allow, err := doVerify()
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Caller().
|
||||
Err(err).
|
||||
Msg("Cannot list nodes")
|
||||
Msg("Failed to verify client")
|
||||
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
allow := nodes.ContainsNodeKey(derpAdmitClientRequest.NodePublic)
|
||||
|
||||
resp := tailcfg.DERPAdmitClientResponse{
|
||||
Allow: allow,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue