websocket

package
v1.11.13 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthenticationSuccessEvent = "auth success"
	TokenExpiringEvent         = "token expiring"
	TokenExpiredEvent          = "token expired"
	AuthenticationEvent        = "auth"
	SetStateEvent              = "set state"
	SendServerLogsEvent        = "send logs"
	SendCommandEvent           = "send command"
	SendStatsEvent             = "send stats"
	ErrorEvent                 = "daemon error"
	JwtErrorEvent              = "jwt error"
)
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 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) Logger

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 Message

type Message struct {
	// The event to perform.
	Event string `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