From 5a62f83ec848aea541671af7cabb0c411676d351 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 11 Sep 2020 23:11:57 -0700 Subject: [PATCH] Don't run pre-boot actions if the server is already running --- router/websocket/websocket.go | 5 +++-- server/errors.go | 14 +++----------- server/power.go | 7 ++++++- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/router/websocket/websocket.go b/router/websocket/websocket.go index 5714657..430c566 100644 --- a/router/websocket/websocket.go +++ b/router/websocket/websocket.go @@ -159,9 +159,10 @@ func (h *Handler) TokenValid() error { // error message, otherwise we just send back a standard error message. func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error { j := h.GetJwt() + expected := errors.Is(err, server.ErrSuspended) || errors.Is(err, server.ErrIsRunning) message := "an unexpected error was encountered while handling this request" - if server.IsSuspendedError(err) || (j != nil && j.HasPermission(PermissionReceiveErrors)) { + if expected || (j != nil && j.HasPermission(PermissionReceiveErrors)) { message = err.Error() } @@ -171,7 +172,7 @@ func (h *Handler) SendErrorJson(msg Message, err error, shouldLog ...bool) error wsm.Args = []string{m} if len(shouldLog) == 0 || (len(shouldLog) == 1 && shouldLog[0] == true) { - if !server.IsSuspendedError(err) { + if !expected { h.server.Log().WithFields(log.Fields{"event": msg.Event, "error_identifier": u.String(), "error": err}). Error("failed to handle websocket process; an error was encountered processing an event") } diff --git a/server/errors.go b/server/errors.go index 2f4682c..80fdaa9 100644 --- a/server/errors.go +++ b/server/errors.go @@ -1,17 +1,9 @@ package server -type suspendedError struct { -} +import "github.com/pkg/errors" -func (e *suspendedError) Error() string { - return "server is currently in a suspended state" -} - -func IsSuspendedError(err error) bool { - _, ok := err.(*suspendedError) - - return ok -} +var ErrIsRunning = errors.New("server is running") +var ErrSuspended = errors.New("server is currently in a suspended state") type crashTooFrequent struct { } diff --git a/server/power.go b/server/power.go index 9d1e0fa..a46aa84 100644 --- a/server/power.go +++ b/server/power.go @@ -4,6 +4,7 @@ import ( "context" "github.com/pkg/errors" "github.com/pterodactyl/wings/config" + "github.com/pterodactyl/wings/environment" "golang.org/x/sync/semaphore" "os" "time" @@ -87,6 +88,10 @@ func (s *Server) HandlePowerAction(action PowerAction, waitSeconds ...int) error switch action { case PowerActionStart: + if s.GetState() != environment.ProcessOfflineState { + return ErrIsRunning + } + // Run the pre-boot logic for the server before processing the environment start. if err := s.onBeforeStart(); err != nil { return err @@ -134,7 +139,7 @@ func (s *Server) onBeforeStart() error { // Disallow start & restart if the server is suspended. Do this check after performing a sync // action with the Panel to ensure that we have the most up-to-date information for that server. if s.IsSuspended() { - return new(suspendedError) + return ErrSuspended } // Ensure we sync the server information with the environment so that any new environment variables