websocket

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const (
	AuthenticationSuccessEvent = Event("auth success")
	TokenExpiringEvent         = Event("token expiring")
	TokenExpiredEvent          = Event("token expired")
	AuthenticationEvent        = Event("auth")
	SetStateEvent              = Event("set state")
	SendServerLogsEvent        = Event("send logs")
	SendCommandEvent           = Event("send command")
	SendStatsEvent             = Event("send stats")
	ErrorEvent                 = Event("daemon error")
	JwtErrorEvent              = Event("jwt error")
	ThrottledEvent             = Event("throttled")
)
View Source
const (
	PermissionConnect          = "websocket.connect"
	PermissionSendCommand      = "control.console"
	PermissionSendPowerStart   = "control.start"
	PermissionSendPowerStop    = "control.stop"
	PermissionSendPowerRestart = "control.restart"
	PermissionReceiveErrors    = "admin.websocket.errors"
	PermissionReceiveInstall   = "admin.websocket.install"
	PermissionReceiveTransfer  = "admin.websocket.transfer"
	PermissionReceiveBackups   = "backup.read"
)

Variables

View Source
var (
	ErrJwtNotPresent    = errors.New("jwt: no jwt present")
	ErrJwtNoConnectPerm = errors.New("jwt: missing connect permission")
	ErrJwtUuidMismatch  = errors.New("jwt: server uuid mismatch")
	ErrJwtOnDenylist    = errors.New("jwt: created too far in past (denylist)")
)

Functions

func IsJwtError

func IsJwtError(err error) bool

func NewTokenPayload

func NewTokenPayload(token []byte) (*tokens.WebsocketPayload, error)

NewTokenPayload parses a JWT into a websocket token payload.

Types

type Event added in v1.12.0

type Event string

type Handler

type Handler struct {
	sync.RWMutex `json:"-"`
	Connection   *websocket.Conn `json:"-"`
	// contains filtered or unexported fields
}

func GetHandler

func GetHandler(s *server.Server, w http.ResponseWriter, r *http.Request, c *gin.Context) (*Handler, error)

GetHandler returns a new websocket handler using the context provided.

func (*Handler) GetErrorMessage

func (h *Handler) GetErrorMessage(msg string) (string, uuid.UUID)

GetErrorMessage converts an error message into a more readable representation and returns a UUID that can be cross-referenced to find the specific error that triggered.

func (*Handler) GetJwt

func (h *Handler) GetJwt() *tokens.WebsocketPayload

GetJwt returns the JWT for the websocket in a race-safe manner.

func (*Handler) HandleInbound

func (h *Handler) HandleInbound(ctx context.Context, m Message) error

HandleInbound handles an inbound socket request and route it to the proper action.

func (*Handler) IsThrottled added in v1.12.0

func (h *Handler) IsThrottled(e Event) bool

func (*Handler) Logger added in v1.5.3

func (h *Handler) Logger() *log.Entry

func (*Handler) SendErrorJson

func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error

SendErrorJson sends an error back to the connected websocket instance by checking the permissions of the token. If the user has the "receive-errors" grant we will send back the actual error message, otherwise we just send back a standard error message.

func (*Handler) SendJson

func (h *Handler) SendJson(v Message) error

func (*Handler) TokenValid

func (h *Handler) TokenValid() error

TokenValid checks if the JWT is still valid.

func (*Handler) Uuid

func (h *Handler) Uuid() uuid.UUID

type LimiterBucket added in v1.12.0

type LimiterBucket struct {
	// contains filtered or unexported fields
}

func NewLimiter added in v1.12.0

func NewLimiter() *LimiterBucket

func (*LimiterBucket) For added in v1.12.0

func (l *LimiterBucket) For(e Event) *rate.Limiter

For returns the internal rate limiter for the given event type. In most cases this is a shared rate limiter for events, but certain "heavy" or low-frequency events implement their own limiters.

type Message

type Message struct {
	// The event to perform.
	Event Event `json:"event"`

	// The data to pass along, only used by power/command currently. Other requests
	// should either omit the field or pass an empty value as it is ignored.
	Args []string `json:"args,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL