2023-05-10 15:24:05 +08:00
|
|
|
package hscontrol
|
2022-08-14 03:12:19 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
2023-09-24 19:42:05 +08:00
|
|
|
// // NoiseRegistrationHandler handles the actual registration process of a node.
|
2023-05-02 14:15:33 +08:00
|
|
|
func (ns *noiseServer) NoiseRegistrationHandler(
|
2022-08-14 03:12:19 +08: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 18:40:22 +08:00
|
|
|
|
2022-08-14 03:12:19 +08:00
|
|
|
return
|
|
|
|
}
|
2023-05-02 14:15:33 +08:00
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Any("headers", req.Header).
|
2023-09-11 19:08:44 +08:00
|
|
|
Caller().
|
2023-05-02 14:15:33 +08:00
|
|
|
Msg("Headers")
|
|
|
|
|
2022-08-14 03:12:19 +08: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")
|
2023-09-24 19:42:05 +08:00
|
|
|
nodeRegistrations.WithLabelValues("unknown", "web", "error", "unknown").Inc()
|
2022-08-14 03:12:19 +08:00
|
|
|
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:31:33 +08:00
|
|
|
// Reject unsupported versions
|
|
|
|
if registerRequest.Version < MinimumCapVersion {
|
|
|
|
log.Info().
|
|
|
|
Caller().
|
|
|
|
Int("min_version", int(MinimumCapVersion)).
|
|
|
|
Int("client_version", int(registerRequest.Version)).
|
|
|
|
Msg("unsupported client connected")
|
|
|
|
http.Error(writer, "Internal error", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:15:33 +08:00
|
|
|
ns.nodeKey = registerRequest.NodeKey
|
|
|
|
|
2023-11-23 15:31:33 +08:00
|
|
|
ns.headscale.handleRegister(writer, req, registerRequest, ns.conn.Peer())
|
2022-08-14 03:12:19 +08:00
|
|
|
}
|