2023-05-10 07:24:05 +00:00
|
|
|
package hscontrol
|
2022-08-14 21:12:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
2023-11-23 07:31:33 +00:00
|
|
|
const (
|
2024-01-05 09:41:56 +00:00
|
|
|
MinimumCapVersion tailcfg.CapabilityVersion = 56
|
2023-11-23 07:31:33 +00:00
|
|
|
)
|
|
|
|
|
2022-08-14 21:12:18 +00:00
|
|
|
// NoisePollNetMapHandler takes care of /machine/:id/map using the Noise protocol
|
|
|
|
//
|
|
|
|
// 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.
|
2023-05-02 06:15:33 +00:00
|
|
|
func (ns *noiseServer) NoisePollNetMapHandler(
|
2022-08-14 21:12:18 +00:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
|
|
|
log.Trace().
|
|
|
|
Str("handler", "NoisePollNetMap").
|
|
|
|
Msg("PollNetMapHandler called")
|
2023-05-02 06:15:33 +00:00
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Any("headers", req.Header).
|
2023-09-11 11:08:44 +00:00
|
|
|
Caller().
|
2023-05-02 06:15:33 +00:00
|
|
|
Msg("Headers")
|
|
|
|
|
2022-08-14 21:12:18 +00:00
|
|
|
body, _ := io.ReadAll(req.Body)
|
|
|
|
|
|
|
|
mapRequest := tailcfg.MapRequest{}
|
|
|
|
if err := json.Unmarshal(body, &mapRequest); err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot parse MapRequest")
|
|
|
|
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-23 07:31:33 +00:00
|
|
|
// Reject unsupported versions
|
|
|
|
if mapRequest.Version < MinimumCapVersion {
|
|
|
|
log.Info().
|
|
|
|
Caller().
|
|
|
|
Int("min_version", int(MinimumCapVersion)).
|
|
|
|
Int("client_version", int(mapRequest.Version)).
|
|
|
|
Msg("unsupported client connected")
|
|
|
|
http.Error(writer, "Internal error", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-02 06:15:33 +00:00
|
|
|
ns.nodeKey = mapRequest.NodeKey
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node, err := ns.headscale.db.GetNodeByAnyKey(
|
2023-05-11 07:09:18 +00:00
|
|
|
ns.conn.Peer(),
|
|
|
|
mapRequest.NodeKey,
|
|
|
|
key.NodePublic{},
|
|
|
|
)
|
2022-08-14 21:12:18 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Warn().
|
|
|
|
Str("handler", "NoisePollNetMap").
|
2023-09-24 11:42:05 +00:00
|
|
|
Msgf("Ignoring request, cannot find node with key %s", mapRequest.NodeKey.String())
|
2022-08-14 21:12:18 +00:00
|
|
|
http.Error(writer, "Internal error", http.StatusNotFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error().
|
|
|
|
Str("handler", "NoisePollNetMap").
|
2023-09-24 11:42:05 +00:00
|
|
|
Msgf("Failed to fetch node from the database with node key: %s", mapRequest.NodeKey.String())
|
2022-08-14 21:12:18 +00:00
|
|
|
http.Error(writer, "Internal error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().
|
|
|
|
Str("handler", "NoisePollNetMap").
|
2023-09-24 11:42:05 +00:00
|
|
|
Str("node", node.Hostname).
|
2023-11-23 07:31:33 +00:00
|
|
|
Int("cap_ver", int(mapRequest.Version)).
|
2023-09-24 11:42:05 +00:00
|
|
|
Msg("A node sending a MapRequest with Noise protocol")
|
2022-08-14 21:12:18 +00:00
|
|
|
|
2023-11-23 07:31:33 +00:00
|
|
|
ns.headscale.handlePoll(writer, req.Context(), node, mapRequest)
|
2022-08-14 21:12:18 +00:00
|
|
|
}
|