websocket

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 22 Imported by: 0

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: 缺少 jwt")
	ErrJwtNoConnectPerm = errors.New("jwt: 缺少連線權限")
	ErrJwtUuidMismatch  = errors.New("jwt: 伺服器 uuid 不相符")
	ErrJwtOnDenylist    = errors.New("jwt: 簽發時間過久(已列入拒絕清單)")
)

Functions

func IsJwtError

func IsJwtError(err error) bool

func NewTokenPayload

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

NewTokenPayload 將一個 JWT 解析為 websocket token payload。

Types

type Event

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 使用提供的 context 回傳一個新的 websocket 處理器。

func (*Handler) GetErrorMessage

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

GetErrorMessage 將錯誤訊息轉換成更易讀的表示方式,並回傳 一個可以用來交叉比對找出觸發特定錯誤的 UUID。

func (*Handler) GetJwt

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

GetJwt 以競態安全(race-safe)的方式回傳此 websocket 的 JWT。

func (*Handler) HandleInbound

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

HandleInbound 處理傳入的 socket 請求,並將其路由到正確的動作。

func (*Handler) IsThrottled

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

func (*Handler) Logger

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

func (*Handler) SendErrorJson

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

SendErrorJson 透過檢查 token 的權限,將錯誤回傳給已連線的 websocket 實例。如果使用者具有「接收錯誤(receive-errors)」 的授權,我們會回傳實際的錯誤訊息,否則就只回傳一則 標準的錯誤訊息。

func (*Handler) SendJson

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

func (*Handler) TokenValid

func (h *Handler) TokenValid() error

TokenValid 檢查該 JWT 是否仍然有效。

func (*Handler) Uuid

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

type LimiterBucket

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

func NewLimiter

func NewLimiter() *LimiterBucket

func (*LimiterBucket) For

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

For 回傳指定事件類型的內部速率限制器。在大多數情況下, 這是所有事件共用的速率限制器,但某些「較耗資源」或 低頻率的事件會實作自己專屬的限制器。

type Message

type Message struct {
	// 要執行的事件。
	Event Event `json:"event"`

	// 要一併傳遞的資料,目前只用於電源操作/指令。其他請求
	// 應該省略這個欄位,或傳入空值,因為這個欄位會被忽略。
	Args []string `json:"args,omitempty"`
}

Jump to

Keyboard shortcuts

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