fix incorrect error handling logic when a JWT is created wrongly; closes pterodactyl/panel#3295

Prior to this logic not only was the error response incorrect for events, but we registered event listeners before the authentication event; so if auth failed we flooded the socket with tons of output that was never going to be sent anyways.

This change now waits to register listeners until the socket is fully authenticated and we're guaranteed to have a token present.
This commit is contained in:
Dane Everitt
2021-10-25 21:23:45 -07:00
parent 023d7ec1ec
commit 32d6594476
3 changed files with 36 additions and 28 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"time"
"emperror.dev/errors"
"github.com/gin-gonic/gin"
ws "github.com/gorilla/websocket"
@@ -64,17 +63,6 @@ func getServerWebsocket(c *gin.Context) {
}
}()
go func() {
if err := handler.ListenForServerEvents(ctx); err != nil {
handler.Logger().Warn("error while processing server event; closing websocket connection")
if err := handler.Connection.Close(); err != nil {
handler.Logger().WithField("error", errors.WithStack(err)).Error("error closing websocket connection")
}
}
}()
go handler.ListenForExpiration(ctx)
for {
j := websocket.Message{}
@@ -94,7 +82,7 @@ func getServerWebsocket(c *gin.Context) {
}
go func(msg websocket.Message) {
if err := handler.HandleInbound(msg); err != nil {
if err := handler.HandleInbound(ctx, msg); err != nil {
handler.SendErrorJson(msg, err)
}
}(j)