headscale/protocol_legacy.go

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

2022-11-04 10:26:33 +00:00
//go:build ts2019
2022-08-14 19:16:29 +00:00
package headscale
import (
"io"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
)
2023-05-01 14:52:03 +00:00
// RegistrationHandler handles the actual registration process of a node
2022-08-14 19:16:29 +00:00
// Endpoint /machine/:mkey.
func (h *Headscale) RegistrationHandler(
writer http.ResponseWriter,
req *http.Request,
) {
vars := mux.Vars(req)
machineKeyStr, ok := vars["mkey"]
if !ok || machineKeyStr == "" {
log.Error().
Str("handler", "RegistrationHandler").
Msg("No machine ID in request")
http.Error(writer, "No machine ID in request", http.StatusBadRequest)
return
}
body, _ := io.ReadAll(req.Body)
var machineKey key.MachinePublic
err := machineKey.UnmarshalText([]byte(MachinePublicKeyEnsurePrefix(machineKeyStr)))
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Cannot parse machine key")
2023-05-01 14:52:03 +00:00
nodeRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
2022-08-14 19:16:29 +00:00
http.Error(writer, "Cannot parse machine key", http.StatusBadRequest)
return
}
registerRequest := tailcfg.RegisterRequest{}
err = decode(body, &registerRequest, &machineKey, h.privateKey)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Cannot decode message")
2023-05-01 14:52:03 +00:00
nodeRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
2022-08-14 19:16:29 +00:00
http.Error(writer, "Cannot decode message", http.StatusBadRequest)
return
}
h.handleRegisterCommon(writer, req, registerRequest, machineKey, false)
2022-08-14 19:16:29 +00:00
}