2023-05-10 09:24:05 +02:00
|
|
|
package hscontrol
|
2022-08-13 21:12:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
2022-08-14 12:00:43 +02:00
|
|
|
// // NoiseRegistrationHandler handles the actual registration process of a machine.
|
2023-05-02 08:15:33 +02:00
|
|
|
func (ns *noiseServer) NoiseRegistrationHandler(
|
2022-08-13 21:12:19 +02:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
|
|
|
log.Trace().Caller().Msgf("Noise registration handler for client %s", req.RemoteAddr)
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(writer, "Wrong method", http.StatusMethodNotAllowed)
|
2022-08-14 12:40:22 +02:00
|
|
|
|
2022-08-13 21:12:19 +02:00
|
|
|
return
|
|
|
|
}
|
2023-05-02 08:15:33 +02:00
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Any("headers", req.Header).
|
|
|
|
Msg("Headers")
|
|
|
|
|
2022-08-13 21:12:19 +02:00
|
|
|
body, _ := io.ReadAll(req.Body)
|
|
|
|
registerRequest := tailcfg.RegisterRequest{}
|
|
|
|
if err := json.Unmarshal(body, ®isterRequest); err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot parse RegisterRequest")
|
|
|
|
machineRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
|
|
|
|
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:15:33 +02:00
|
|
|
ns.nodeKey = registerRequest.NodeKey
|
|
|
|
|
2023-06-06 17:14:56 +02:00
|
|
|
ns.headscale.handleRegister(writer, req, registerRequest, ns.conn.Peer(), true)
|
2022-08-13 21:12:19 +02:00
|
|
|
}
|