break everything

- upgrade dependencies
- run gofmt and goimports to organize code
- fix typos
- other small tweaks
This commit is contained in:
Matthew Penner
2021-08-02 15:07:00 -06:00
parent 4a5e0bb86f
commit 3c54c1f840
57 changed files with 1056 additions and 450 deletions

View File

@@ -2,9 +2,10 @@ package websocket
import (
"context"
"time"
"github.com/pterodactyl/wings/events"
"github.com/pterodactyl/wings/server"
"time"
)
// Checks the time to expiration on the JWT every 30 seconds until the token has

View File

@@ -2,22 +2,24 @@ package websocket
import (
"context"
"emperror.dev/errors"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"
"emperror.dev/errors"
"github.com/apex/log"
"github.com/gbrlsnchs/jwt/v3"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/environment/docker"
"github.com/pterodactyl/wings/router/tokens"
"github.com/pterodactyl/wings/server"
"net/http"
"strings"
"sync"
"time"
)
const (
@@ -55,11 +57,10 @@ func IsJwtError(err error) bool {
errors.Is(err, jwt.ErrExpValidation)
}
// Parses a JWT into a websocket token payload.
// NewTokenPayload parses a JWT into a websocket token payload.
func NewTokenPayload(token []byte) (*tokens.WebsocketPayload, error) {
payload := tokens.WebsocketPayload{}
err := tokens.ParseToken(token, &payload)
if err != nil {
var payload tokens.WebsocketPayload
if err := tokens.ParseToken(token, &payload); err != nil {
return nil, err
}
@@ -180,7 +181,7 @@ func (h *Handler) unsafeSendJson(v interface{}) error {
return h.Connection.WriteJSON(v)
}
// Checks if the JWT is still valid.
// TokenValid checks if the JWT is still valid.
func (h *Handler) TokenValid() error {
j := h.GetJwt()
if j == nil {
@@ -199,14 +200,14 @@ func (h *Handler) TokenValid() error {
return ErrJwtNoConnectPerm
}
if h.server.Id() != j.GetServerUuid() {
if h.server.ID() != j.GetServerUuid() {
return ErrJwtUuidMismatch
}
return nil
}
// Sends an error back to the connected websocket instance by checking the permissions
// 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 (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error {
@@ -236,7 +237,7 @@ func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error
return h.unsafeSendJson(wsm)
}
// Converts an error message into a more readable representation and returns a 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 (h *Handler) GetErrorMessage(msg string) (string, uuid.UUID) {
u := uuid.Must(uuid.NewRandom())
@@ -246,13 +247,7 @@ func (h *Handler) GetErrorMessage(msg string) (string, uuid.UUID) {
return m, u
}
// Sets the JWT for the websocket in a race-safe manner.
func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
h.Lock()
h.jwt = token
h.Unlock()
}
// GetJwt returns the JWT for the websocket in a race-safe manner.
func (h *Handler) GetJwt() *tokens.WebsocketPayload {
h.RLock()
defer h.RUnlock()
@@ -260,7 +255,14 @@ func (h *Handler) GetJwt() *tokens.WebsocketPayload {
return h.jwt
}
// Handle the inbound socket request and route it to the proper server action.
// setJwt sets the JWT for the websocket in a race-safe manner.
func (h *Handler) setJwt(token *tokens.WebsocketPayload) {
h.Lock()
h.jwt = token
h.Unlock()
}
// HandleInbound handles an inbound socket request and route it to the proper action.
func (h *Handler) HandleInbound(m Message) error {
if m.Event != AuthenticationEvent {
if err := h.TokenValid(); err != nil {