Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

9.8 KB
Raw Blame
package agent

import (
	"context"
	"errors"
	"fmt"

	"github.com/fxamacker/cbor/v2"
	"github.com/henrygd/beszel/internal/common"
	"github.com/henrygd/beszel/internal/entities/monitor"
	"github.com/henrygd/beszel/internal/entities/smart"
	"github.com/henrygd/beszel/internal/entities/system"
	"github.com/lxzan/gws"

	"log/slog"
)

// HandlerContext provides context for request handlers
type HandlerContext struct {
	Client         *WebSocketClient
	Conn           *gws.Conn // WebSocket that carried this request, if any
	Agent          *Agent
	Request        *common.HubRequest[cbor.RawMessage]
	RequestID      *uint32
	HubVerified    bool
	ConnectionType system.ConnectionType // Transport that carried this request
	// SendResponse abstracts how a handler sends responses (WS or SSH)
	SendResponse func(data any, requestID *uint32) error
}

// RequestHandler defines the interface for handling specific websocket request types
type RequestHandler interface {
	// Handle processes the request and returns an error if unsuccessful
	Handle(hctx *HandlerContext) error
}

// Responder sends handler responses back to the hub (over WS or SSH)
type Responder interface {
	SendResponse(data any, requestID *uint32) error
}

// HandlerRegistry manages the mapping between actions and their handlers
type HandlerRegistry struct {
	handlers map[common.WebSocketAction]RequestHandler
}

// NewHandlerRegistry creates a new handler registry with default handlers
func NewHandlerRegistry() *HandlerRegistry {
	registry := &HandlerRegistry{
		handlers: make(map[common.WebSocketAction]RequestHandler),
	}

	registry.Register(common.GetData, &GetDataHandler{})
	registry.Register(common.CheckFingerprint, &CheckFingerprintHandler{})
	registry.Register(common.GetContainerLogs, &GetContainerLogsHandler{})
	registry.Register(common.GetContainerInfo, &GetContainerInfoHandler{})
	registry.Register(common.GetSmartData, &GetSmartDataHandler{})
	registry.Register(common.GetSystemdInfo, &GetSystemdInfoHandler{})
	registry.Register(common.GetSystemdLogs, &GetSystemdLogsHandler{})
	registry.Register(common.SyncNetworkMonitors, &SyncNetworkMonitorsHandler{})
	registry.Register(common.GetZfsData, &GetZfsDataHandler{})
	registry.Register(common.GetPackageUpdates, &GetPackageUpdatesHandler{})

	return registry
}

// Register registers a handler for a specific action type
func (hr *HandlerRegistry) Register(action common.WebSocketAction, handler RequestHandler) {
	hr.handlers[action] = handler
}

// Handle routes the request to the appropriate handler
func (hr *HandlerRegistry) Handle(hctx *HandlerContext) error {
	handler, exists := hr.handlers[hctx.Request.Action]
	if !exists {
		return fmt.Errorf("unknown action: %d", hctx.Request.Action)
	}

	// Check verification requirement - default to requiring verification
	if hctx.Request.Action != common.CheckFingerprint && !hctx.HubVerified {
		return errors.New("hub not verified")
	}

	// Log handler execution for debugging
	// slog.Debug("Executing handler", "action", hctx.Request.Action)

	return handler.Handle(hctx)
}

// GetHandler returns the handler for a specific action
func (hr *HandlerRegistry) GetHandler(action common.WebSocketAction) (RequestHandler, bool) {
	handler, exists := hr.handlers[action]
	return handler, exists
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetDataHandler handles system data requests
type GetDataHandler struct{}

func (h *GetDataHandler) Handle(hctx *HandlerContext) error {
	var options common.DataRequestOptions
	_ = cbor.Unmarshal(hctx.Request.Data, &options)

	hctx.Agent.markHubRequest()
	sysStats := hctx.Agent.gatherStats(options)
	// Cached stats may be shared by concurrent SSH and WebSocket requests.
	// Set the transport on the response copy, not on the cached data.
	response := *sysStats
	response.Info.ConnectionType = hctx.ConnectionType
	return hctx.SendResponse(&response, hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// CheckFingerprintHandler handles authentication challenges
type CheckFingerprintHandler struct{}

func (h *CheckFingerprintHandler) Handle(hctx *HandlerContext) error {
	return hctx.Client.handleAuthChallenge(hctx.Request, hctx.RequestID, hctx.Conn)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetContainerLogsHandler handles container log requests
type GetContainerLogsHandler struct{}

func (h *GetContainerLogsHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.dockerManager == nil {
		return hctx.SendResponse("", hctx.RequestID)
	}

	var req common.ContainerLogsRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}

	ctx := context.Background()
	logContent, err := hctx.Agent.dockerManager.getLogs(ctx, req.ContainerID)
	if err != nil {
		return err
	}

	return hctx.SendResponse(logContent, hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetContainerInfoHandler handles container info requests
type GetContainerInfoHandler struct{}

func (h *GetContainerInfoHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.dockerManager == nil {
		return hctx.SendResponse("", hctx.RequestID)
	}

	var req common.ContainerInfoRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}

	ctx := context.Background()
	info, err := hctx.Agent.dockerManager.getContainerInfo(ctx, req.ContainerID)
	if err != nil {
		return err
	}

	return hctx.SendResponse(string(info), hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetSmartDataHandler handles SMART data requests
type GetSmartDataHandler struct{}

func (h *GetSmartDataHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.smartManager == nil {
		return hctx.SendResponse(smart.SmartDataResponse{Data: map[string]smart.SmartData{}}, hctx.RequestID)
	}
	complete, err := hctx.Agent.smartManager.Refresh(false)
	if err != nil {
		slog.Debug("smart refresh failed", "err", err)
	}
	return hctx.SendResponse(smart.SmartDataResponse{
		Data:     hctx.Agent.smartManager.GetCurrentData(),
		Complete: complete,
	}, hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetZfsDataHandler handles ZFS detail data requests
type GetZfsDataHandler struct{}

func (h *GetZfsDataHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.storagePoolManager == nil {
		return hctx.SendResponse(nil, hctx.RequestID)
	}
	var req common.ZfsDataRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}
	return hctx.SendResponse(hctx.Agent.storagePoolManager.GetDetail(req.Force), hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetPackageUpdatesHandler returns the pending package updates found by the
// last background check. It never runs a check itself.
type GetPackageUpdatesHandler struct{}

func (h *GetPackageUpdatesHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.packageUpdates == nil {
		return hctx.SendResponse(system.PackageUpdates{}, hctx.RequestID)
	}
	return hctx.SendResponse(hctx.Agent.packageUpdates.list(), hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetSystemdInfoHandler handles detailed systemd service info requests
type GetSystemdInfoHandler struct{}

func (h *GetSystemdInfoHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.systemdManager == nil {
		return errors.ErrUnsupported
	}

	var req common.SystemdInfoRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}
	if req.ServiceName == "" {
		return errors.New("service name is required")
	}

	details, err := hctx.Agent.systemdManager.getServiceDetails(req.ServiceName)
	if err != nil {
		return err
	}

	return hctx.SendResponse(details, hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// GetSystemdLogsHandler handles recent systemd service log requests.
type GetSystemdLogsHandler struct{}

func (h *GetSystemdLogsHandler) Handle(hctx *HandlerContext) error {
	if hctx.Agent.systemdManager == nil {
		return errors.ErrUnsupported
	}

	var req common.SystemdLogsRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}
	if req.ServiceName == "" {
		return errors.New("service name is required")
	}

	logs, err := hctx.Agent.systemdManager.getServiceLogs(req.ServiceName)
	if err != nil {
		return err
	}

	return hctx.SendResponse(logs, hctx.RequestID)
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

// SyncNetworkMonitorsHandler handles monitor configuration sync from hub
type SyncNetworkMonitorsHandler struct{}

func (h *SyncNetworkMonitorsHandler) Handle(hctx *HandlerContext) error {
	var req monitor.SyncRequest
	if err := cbor.Unmarshal(hctx.Request.Data, &req); err != nil {
		return err
	}
	resp, err := hctx.Agent.monitorManager.HandleSyncRequest(req)
	if err != nil {
		return err
	}
	return hctx.SendResponse(resp, hctx.RequestID)
}