websocket

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 15 Imported by: 1

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"
	ErrorEvent                 = "daemon error"
)
View Source
const (
	PermissionConnect          = "websocket.*"
	PermissionSendCommand      = "control.console"
	PermissionSendPowerStart   = "control.start"
	PermissionSendPowerStop    = "control.stop"
	PermissionSendPowerRestart = "control.restart"
	PermissionReceiveErrors    = "admin.errors"
	PermissionReceiveInstall   = "admin.install"
	PermissionReceiveBackups   = "backup.read"
)

Variables

This section is empty.

Functions

func NewTokenPayload

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

Parses a JWT into a websocket token payload.

Types

type Handler

type Handler struct {
	sync.RWMutex
	Connection *websocket.Conn
	// contains filtered or unexported fields
}

func GetHandler

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

Returns a new websocket handler using the context provided.

func (*Handler) GetErrorMessage

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

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

func (*Handler) HandleInbound

func (h *Handler) HandleInbound(m Message) error

Handle the inbound socket request and route it to the proper server action.

func (*Handler) ListenForExpiration

func (h *Handler) ListenForExpiration(ctx context.Context)

Checks the time to expiration on the JWT every 30 seconds until the token has expired. If we are within 3 minutes of the token expiring, send a notice over the socket that it is expiring soon. If it has expired, send that notice as well.

func (*Handler) ListenForServerEvents

func (h *Handler) ListenForServerEvents(ctx context.Context)

Listens for different events happening on a server and sends them along to the connected websocket.

func (*Handler) SendErrorJson

func (h *Handler) SendErrorJson(err error) error

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

Checks if the JWT is still valid.

type Message

type Message struct {
	// The event to perform. Should be one of the following that are supported:
	//
	// - status : Returns the server's power state.
	// - logs : Returns the server log data at the time of the request.
	// - power : Performs a power action aganist the server based the data.
	// - command : Performs a command on a server using the data field.
	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