From 95c7450b000a473553372efc98446b1fc3ed42c9 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 13 Jan 2025 11:45:59 +0100 Subject: [PATCH] move auth_noise into noise Signed-off-by: Kristoffer Dalby --- hscontrol/auth_noise.go | 56 ----------------------------------------- hscontrol/noise.go | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 56 deletions(-) delete mode 100644 hscontrol/auth_noise.go diff --git a/hscontrol/auth_noise.go b/hscontrol/auth_noise.go deleted file mode 100644 index 6659dfa5..00000000 --- a/hscontrol/auth_noise.go +++ /dev/null @@ -1,56 +0,0 @@ -package hscontrol - -import ( - "encoding/json" - "io" - "net/http" - - "github.com/rs/zerolog/log" - "tailscale.com/tailcfg" -) - -// // NoiseRegistrationHandler handles the actual registration process of a node. -func (ns *noiseServer) NoiseRegistrationHandler( - 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) - - return - } - - log.Trace(). - Any("headers", req.Header). - Caller(). - Msg("Headers") - - 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") - http.Error(writer, "Internal error", http.StatusInternalServerError) - - return - } - - // 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 - } - - ns.nodeKey = registerRequest.NodeKey - - ns.headscale.handleRegister(writer, req, registerRequest, ns.conn.Peer()) -} diff --git a/hscontrol/noise.go b/hscontrol/noise.go index 393b9608..8c49e426 100644 --- a/hscontrol/noise.go +++ b/hscontrol/noise.go @@ -232,3 +232,49 @@ func (ns *noiseServer) NoisePollNetMapHandler( sess.serveLongPoll() } } + +// NoiseRegistrationHandler handles the actual registration process of a node. +func (ns *noiseServer) NoiseRegistrationHandler( + 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) + + return + } + + log.Trace(). + Any("headers", req.Header). + Caller(). + Msg("Headers") + + 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") + http.Error(writer, "Internal error", http.StatusInternalServerError) + + return + } + + // 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, "unsupported client version", http.StatusBadRequest) + + return + } + + ns.nodeKey = registerRequest.NodeKey + + ns.headscale.handleRegister(writer, req, registerRequest, ns.conn.Peer()) +}