2022-11-04 06:26:33 -04:00
|
|
|
//go:build ts2019
|
|
|
|
|
2023-05-10 03:24:05 -04:00
|
|
|
package hscontrol
|
2022-08-14 15:16:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-05-11 03:09:18 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-08-14 15:16:29 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegistrationHandler handles the actual registration process of a machine
|
|
|
|
// 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
|
2023-05-11 03:09:18 -04:00
|
|
|
err := machineKey.UnmarshalText([]byte(util.MachinePublicKeyEnsurePrefix(machineKeyStr)))
|
2022-08-14 15:16:29 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot parse machine key")
|
2023-09-24 07:42:05 -04:00
|
|
|
nodeRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
|
2022-08-14 15:16:29 -04:00
|
|
|
http.Error(writer, "Cannot parse machine key", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
registerRequest := tailcfg.RegisterRequest{}
|
2023-05-26 06:26:34 -04:00
|
|
|
err = util.DecodeAndUnmarshalNaCl(body, ®isterRequest, &machineKey, h.privateKey2019)
|
2022-08-14 15:16:29 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot decode message")
|
2023-09-24 07:42:05 -04:00
|
|
|
nodeRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
|
2022-08-14 15:16:29 -04:00
|
|
|
http.Error(writer, "Cannot decode message", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 11:14:56 -04:00
|
|
|
h.handleRegister(writer, req, registerRequest, machineKey, false)
|
2022-08-14 15:16:29 -04:00
|
|
|
}
|