2024-11-22 16:10:05 +00:00
|
|
|
package diagnostic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2024-11-25 19:24:51 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-11-22 16:10:05 +00:00
|
|
|
"time"
|
|
|
|
|
2024-11-25 18:43:32 +00:00
|
|
|
"github.com/google/uuid"
|
2024-11-22 16:10:05 +00:00
|
|
|
"github.com/rs/zerolog"
|
2024-11-25 18:43:32 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2024-11-22 16:10:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2024-12-11 09:29:20 +00:00
|
|
|
log *zerolog.Logger
|
|
|
|
timeout time.Duration
|
|
|
|
systemCollector SystemCollector
|
|
|
|
tunnelID uuid.UUID
|
|
|
|
connectorID uuid.UUID
|
|
|
|
tracker *tunnelstate.ConnTracker
|
|
|
|
cliFlags map[string]string
|
|
|
|
icmpSources []string
|
2024-11-22 16:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDiagnosticHandler(
|
|
|
|
log *zerolog.Logger,
|
|
|
|
timeout time.Duration,
|
|
|
|
systemCollector SystemCollector,
|
2024-11-25 18:43:32 +00:00
|
|
|
tunnelID uuid.UUID,
|
|
|
|
connectorID uuid.UUID,
|
|
|
|
tracker *tunnelstate.ConnTracker,
|
2024-12-11 09:29:20 +00:00
|
|
|
cliFlags map[string]string,
|
2024-12-10 18:42:33 +00:00
|
|
|
icmpSources []string,
|
2024-11-22 16:10:05 +00:00
|
|
|
) *Handler {
|
2024-11-25 18:43:32 +00:00
|
|
|
logger := log.With().Logger()
|
2024-11-22 16:10:05 +00:00
|
|
|
if timeout == 0 {
|
|
|
|
timeout = defaultCollectorTimeout
|
|
|
|
}
|
|
|
|
|
2024-12-11 09:29:20 +00:00
|
|
|
cliFlags[configurationKeyUID] = strconv.Itoa(os.Getuid())
|
2024-11-22 16:10:05 +00:00
|
|
|
return &Handler{
|
2024-12-11 09:29:20 +00:00
|
|
|
log: &logger,
|
|
|
|
timeout: timeout,
|
|
|
|
systemCollector: systemCollector,
|
|
|
|
tunnelID: tunnelID,
|
|
|
|
connectorID: connectorID,
|
|
|
|
tracker: tracker,
|
|
|
|
cliFlags: cliFlags,
|
|
|
|
icmpSources: icmpSources,
|
2024-11-22 16:10:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 17:08:42 +00:00
|
|
|
func (handler *Handler) InstallEndpoints(router *http.ServeMux) {
|
2024-12-03 09:14:59 +00:00
|
|
|
router.HandleFunc(cliConfigurationEndpoint, handler.ConfigurationHandler)
|
2024-11-29 17:08:42 +00:00
|
|
|
router.HandleFunc(tunnelStateEndpoint, handler.TunnelStateHandler)
|
|
|
|
router.HandleFunc(systemInformationEndpoint, handler.SystemHandler)
|
|
|
|
}
|
|
|
|
|
2024-12-11 10:48:41 +00:00
|
|
|
type SystemInformationResponse struct {
|
|
|
|
Info *SystemInformation `json:"info"`
|
|
|
|
Err error `json:"errors"`
|
|
|
|
}
|
|
|
|
|
2024-11-22 16:10:05 +00:00
|
|
|
func (handler *Handler) SystemHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
logger := handler.log.With().Str(collectorField, systemCollectorName).Logger()
|
|
|
|
logger.Info().Msg("Collection started")
|
|
|
|
|
2024-11-25 18:43:32 +00:00
|
|
|
defer logger.Info().Msg("Collection finished")
|
2024-11-22 16:10:05 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(request.Context(), handler.timeout)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
2024-12-11 10:48:41 +00:00
|
|
|
info, err := handler.systemCollector.Collect(ctx)
|
2024-11-22 16:10:05 +00:00
|
|
|
|
2024-12-11 10:48:41 +00:00
|
|
|
response := SystemInformationResponse{
|
|
|
|
Info: info,
|
|
|
|
Err: err,
|
2024-11-22 16:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(writer)
|
2024-12-11 10:48:41 +00:00
|
|
|
err = encoder.Encode(response)
|
2024-11-22 16:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error().Err(err).Msgf("error occurred whilst serializing information")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 17:08:42 +00:00
|
|
|
type TunnelState struct {
|
2024-11-25 18:43:32 +00:00
|
|
|
TunnelID uuid.UUID `json:"tunnelID,omitempty"`
|
|
|
|
ConnectorID uuid.UUID `json:"connectorID,omitempty"`
|
|
|
|
Connections []tunnelstate.IndexedConnectionInfo `json:"connections,omitempty"`
|
2024-12-10 18:42:33 +00:00
|
|
|
ICMPSources []string `json:"icmp_sources,omitempty"`
|
2024-11-25 18:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) TunnelStateHandler(writer http.ResponseWriter, _ *http.Request) {
|
|
|
|
log := handler.log.With().Str(collectorField, tunnelStateCollectorName).Logger()
|
|
|
|
log.Info().Msg("Collection started")
|
|
|
|
|
|
|
|
defer log.Info().Msg("Collection finished")
|
|
|
|
|
2024-11-29 17:08:42 +00:00
|
|
|
body := TunnelState{
|
2024-11-25 18:43:32 +00:00
|
|
|
handler.tunnelID,
|
|
|
|
handler.connectorID,
|
|
|
|
handler.tracker.GetActiveConnections(),
|
2024-12-10 18:42:33 +00:00
|
|
|
handler.icmpSources,
|
2024-11-25 18:43:32 +00:00
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(writer)
|
|
|
|
|
|
|
|
err := encoder.Encode(body)
|
|
|
|
if err != nil {
|
|
|
|
handler.log.Error().Err(err).Msgf("error occurred whilst serializing information")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 19:24:51 +00:00
|
|
|
func (handler *Handler) ConfigurationHandler(writer http.ResponseWriter, _ *http.Request) {
|
|
|
|
log := handler.log.With().Str(collectorField, configurationCollectorName).Logger()
|
|
|
|
log.Info().Msg("Collection started")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
log.Info().Msg("Collection finished")
|
|
|
|
}()
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(writer)
|
|
|
|
|
2024-12-11 09:29:20 +00:00
|
|
|
err := encoder.Encode(handler.cliFlags)
|
2024-11-25 19:24:51 +00:00
|
|
|
if err != nil {
|
|
|
|
handler.log.Error().Err(err).Msgf("error occurred whilst serializing response")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, bytes []byte, logger *zerolog.Logger) {
|
|
|
|
bytesWritten, err := w.Write(bytes)
|
2024-11-22 16:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error().Err(err).Msg("error occurred writing response")
|
|
|
|
} else if bytesWritten != len(bytes) {
|
|
|
|
logger.Error().Msgf("error incomplete write response %d/%d", bytesWritten, len(bytes))
|
|
|
|
}
|
|
|
|
}
|