2022-11-04 10:26:33 +00:00
|
|
|
//go:build ts2019
|
|
|
|
|
2023-05-10 07:24:05 +00:00
|
|
|
package hscontrol
|
2022-08-14 20:57:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-05-11 07:09:18 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-08-14 20:57:03 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PollNetMapHandler takes care of /machine/:id/map
|
|
|
|
//
|
|
|
|
// This is the busiest endpoint, as it keeps the HTTP long poll that updates
|
|
|
|
// the clients when something in the network changes.
|
|
|
|
//
|
|
|
|
// The clients POST stuff like HostInfo and their Endpoints here, but
|
|
|
|
// only after their first request (marked with the ReadOnly field).
|
|
|
|
//
|
|
|
|
// At this moment the updates are sent in a quite horrendous way, but they kinda work.
|
|
|
|
func (h *Headscale) PollNetMapHandler(
|
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
machineKeyStr, ok := vars["mkey"]
|
|
|
|
if !ok || machineKeyStr == "" {
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "PollNetMap").
|
|
|
|
Msg("No machine key in request")
|
|
|
|
http.Error(writer, "No machine key in request", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace().
|
|
|
|
Str("handler", "PollNetMap").
|
|
|
|
Str("id", machineKeyStr).
|
|
|
|
Msg("PollNetMapHandler called")
|
|
|
|
body, _ := io.ReadAll(req.Body)
|
|
|
|
|
|
|
|
var machineKey key.MachinePublic
|
2023-05-11 07:09:18 +00:00
|
|
|
err := machineKey.UnmarshalText([]byte(util.MachinePublicKeyEnsurePrefix(machineKeyStr)))
|
2022-08-14 20:57:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "PollNetMap").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot parse client key")
|
|
|
|
|
|
|
|
http.Error(writer, "Cannot parse client key", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mapRequest := tailcfg.MapRequest{}
|
2023-05-26 10:26:34 +00:00
|
|
|
err = util.DecodeAndUnmarshalNaCl(body, &mapRequest, &machineKey, h.privateKey2019)
|
2022-08-14 20:57:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "PollNetMap").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot decode message")
|
|
|
|
http.Error(writer, "Cannot decode message", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node, err := h.db.GetNodeByMachineKey(machineKey)
|
2022-08-14 20:57:03 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Warn().
|
|
|
|
Str("handler", "PollNetMap").
|
2023-09-24 11:42:05 +00:00
|
|
|
Msgf("Ignoring request, cannot find node with key %s", machineKey.String())
|
2022-08-14 20:57:03 +00:00
|
|
|
|
|
|
|
http.Error(writer, "", http.StatusUnauthorized)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "PollNetMap").
|
2023-09-24 11:42:05 +00:00
|
|
|
Msgf("Failed to fetch node from the database with Machine key: %s", machineKey.String())
|
2022-08-14 20:57:03 +00:00
|
|
|
http.Error(writer, "", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("handler", "PollNetMap").
|
|
|
|
Str("id", machineKeyStr).
|
2023-09-24 11:42:05 +00:00
|
|
|
Str("node", node.Hostname).
|
|
|
|
Msg("A node is sending a MapRequest via legacy protocol")
|
2022-08-14 20:57:03 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
h.handlePoll(writer, req.Context(), node, mapRequest, false)
|
2022-08-14 20:57:03 +00:00
|
|
|
}
|