2019-03-25 00:27:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-06-23 04:38:16 +00:00
|
|
|
"context"
|
2021-08-29 20:37:18 +00:00
|
|
|
"encoding/json"
|
2019-04-21 19:02:28 +00:00
|
|
|
"fmt"
|
2021-05-02 22:41:02 +00:00
|
|
|
"net/http"
|
2021-01-23 18:45:29 +00:00
|
|
|
"os"
|
2021-01-10 01:22:39 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2020-05-29 05:07:53 +00:00
|
|
|
"github.com/apex/log"
|
2020-12-25 19:21:09 +00:00
|
|
|
"github.com/creasty/defaults"
|
2021-08-02 21:07:00 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
|
|
|
|
2020-10-17 18:35:20 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2020-08-11 04:38:42 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
"github.com/pterodactyl/wings/events"
|
2021-02-02 04:50:23 +00:00
|
|
|
"github.com/pterodactyl/wings/remote"
|
2020-09-27 19:24:08 +00:00
|
|
|
"github.com/pterodactyl/wings/server/filesystem"
|
2020-12-25 21:32:41 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
2019-03-25 00:27:14 +00:00
|
|
|
)
|
|
|
|
|
2021-01-26 04:28:24 +00:00
|
|
|
// Server is the high level definition for a server instance being controlled
|
|
|
|
// by Wings.
|
2019-03-25 00:27:14 +00:00
|
|
|
type Server struct {
|
2020-07-19 23:27:55 +00:00
|
|
|
// Internal mutex used to block actions that need to occur sequentially, such as
|
|
|
|
// writing the configuration to the disk.
|
|
|
|
sync.RWMutex
|
2020-12-25 19:21:09 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel *context.CancelFunc
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
emitterLock sync.Mutex
|
|
|
|
powerLock *semaphore.Weighted
|
2020-12-26 01:04:18 +00:00
|
|
|
throttleOnce sync.Once
|
2020-07-19 23:27:55 +00:00
|
|
|
|
|
|
|
// Maintains the configuration for the server. This is the data that gets returned by the Panel
|
|
|
|
// such as build settings and container images.
|
2021-02-02 04:50:23 +00:00
|
|
|
cfg Configuration
|
|
|
|
client remote.Client
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// The crash handler for this server instance.
|
|
|
|
crasher CrashHandler
|
2020-05-21 20:53:00 +00:00
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
resources ResourceUsage
|
2020-08-11 04:38:42 +00:00
|
|
|
Environment environment.ProcessEnvironment `json:"-"`
|
2020-09-27 19:24:08 +00:00
|
|
|
|
|
|
|
fs *filesystem.Filesystem
|
2019-03-25 00:27:14 +00:00
|
|
|
|
2020-01-18 22:04:26 +00:00
|
|
|
// Events emitted by the server instance.
|
2022-01-18 03:23:29 +00:00
|
|
|
emitter *events.Bus
|
2019-09-23 03:47:38 +00:00
|
|
|
|
|
|
|
// Defines the process configuration for the server instance. This is dynamically
|
|
|
|
// fetched from the Pterodactyl Server instance each time the server process is
|
|
|
|
// started, and then cached here.
|
2021-02-02 05:28:46 +00:00
|
|
|
procConfig *remote.ProcessConfiguration
|
2019-11-24 23:08:38 +00:00
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
// Tracks the installation process for this server and prevents a server from running
|
|
|
|
// two installer processes at the same time. This also allows us to cancel a running
|
|
|
|
// installation process, for example when a server is deleted from the panel while the
|
|
|
|
// installer process is still running.
|
2020-12-26 01:04:18 +00:00
|
|
|
installing *system.AtomicBool
|
|
|
|
transferring *system.AtomicBool
|
2021-03-12 23:19:35 +00:00
|
|
|
restoring *system.AtomicBool
|
2020-12-25 21:32:41 +00:00
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// The console throttler instance used to control outputs.
|
|
|
|
throttler *ConsoleThrottler
|
2020-10-04 03:46:29 +00:00
|
|
|
|
|
|
|
// Tracks open websocket connections for the server.
|
|
|
|
wsBag *WebsocketBag
|
|
|
|
wsBagLocker sync.Mutex
|
2022-01-18 03:23:29 +00:00
|
|
|
|
|
|
|
logSink *sinkPool
|
|
|
|
installSink *sinkPool
|
2019-03-25 00:27:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 04:56:18 +00:00
|
|
|
// New returns a new server instance with a context and all of the default
|
|
|
|
// values set on the struct.
|
2021-02-02 04:50:23 +00:00
|
|
|
func New(client remote.Client) (*Server, error) {
|
2020-12-25 19:21:09 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
s := Server{
|
2020-12-26 01:04:18 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: &cancel,
|
2021-02-02 04:50:23 +00:00
|
|
|
client: client,
|
2020-12-26 01:04:18 +00:00
|
|
|
installing: system.NewAtomicBool(false),
|
|
|
|
transferring: system.NewAtomicBool(false),
|
2021-03-12 23:19:35 +00:00
|
|
|
restoring: system.NewAtomicBool(false),
|
2022-01-18 03:23:29 +00:00
|
|
|
|
|
|
|
logSink: newSinkPool(),
|
|
|
|
installSink: newSinkPool(),
|
2020-12-25 19:21:09 +00:00
|
|
|
}
|
|
|
|
if err := defaults.Set(&s); err != nil {
|
2021-03-04 04:56:18 +00:00
|
|
|
return nil, errors.Wrap(err, "server: could not set default values for struct")
|
2020-12-25 19:21:09 +00:00
|
|
|
}
|
|
|
|
if err := defaults.Set(&s.cfg); err != nil {
|
2021-03-04 04:56:18 +00:00
|
|
|
return nil, errors.Wrap(err, "server: could not set defaults for server configuration")
|
2020-12-25 19:21:09 +00:00
|
|
|
}
|
2020-12-26 01:04:18 +00:00
|
|
|
s.resources.State = system.NewAtomicString(environment.ProcessOfflineState)
|
2020-12-25 19:21:09 +00:00
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// ID returns the UUID for the server instance.
|
|
|
|
func (s *Server) ID() string {
|
2020-07-20 00:46:39 +00:00
|
|
|
return s.Config().GetUuid()
|
2019-12-22 21:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:02:27 +00:00
|
|
|
// Id returns the UUID for the server instance. This function is deprecated
|
|
|
|
// in favor of Server.ID().
|
|
|
|
//
|
|
|
|
// Deprecated
|
|
|
|
func (s *Server) Id() string {
|
|
|
|
return s.ID()
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:21:09 +00:00
|
|
|
// Cancels the context assigned to this server instance. Assuming background tasks
|
|
|
|
// are using this server's context for things, all of the background tasks will be
|
|
|
|
// stopped as a result.
|
|
|
|
func (s *Server) CtxCancel() {
|
|
|
|
if s.ctxCancel != nil {
|
|
|
|
(*s.ctxCancel)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a context instance for the server. This should be used to allow background
|
|
|
|
// tasks to be canceled if the server is removed. It will only be canceled when the
|
|
|
|
// application is stopped or if the server gets deleted.
|
|
|
|
func (s *Server) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
// Returns all of the environment variables that should be assigned to a running
|
|
|
|
// server instance.
|
|
|
|
func (s *Server) GetEnvironmentVariables() []string {
|
2020-12-27 18:49:08 +00:00
|
|
|
out := []string{
|
2020-10-17 18:35:20 +00:00
|
|
|
fmt.Sprintf("TZ=%s", config.Get().System.Timezone),
|
2020-07-19 23:27:55 +00:00
|
|
|
fmt.Sprintf("STARTUP=%s", s.Config().Invocation),
|
2020-08-28 04:08:33 +00:00
|
|
|
fmt.Sprintf("SERVER_MEMORY=%d", s.MemoryLimit()),
|
2020-09-07 22:37:35 +00:00
|
|
|
fmt.Sprintf("SERVER_IP=%s", s.Config().Allocations.DefaultMapping.Ip),
|
2020-07-19 23:27:55 +00:00
|
|
|
fmt.Sprintf("SERVER_PORT=%d", s.Config().Allocations.DefaultMapping.Port),
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eloop:
|
2020-07-19 23:27:55 +00:00
|
|
|
for k := range s.Config().EnvVars {
|
2020-10-17 18:35:20 +00:00
|
|
|
// Don't allow any environment variables that we have already set above.
|
2019-12-28 22:57:19 +00:00
|
|
|
for _, e := range out {
|
2021-08-02 21:07:00 +00:00
|
|
|
if strings.HasPrefix(e, strings.ToUpper(k)+"=") {
|
2019-12-28 22:57:19 +00:00
|
|
|
continue eloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s=%s", strings.ToUpper(k), s.Config().EnvVars.Get(k)))
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:07:53 +00:00
|
|
|
func (s *Server) Log() *log.Entry {
|
2021-08-02 21:07:00 +00:00
|
|
|
return log.WithField("server", s.ID())
|
2020-05-29 05:07:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 21:02:37 +00:00
|
|
|
// Sync syncs the state of the server on the Panel with Wings. This ensures that
|
|
|
|
// we're always using the state of the server from the Panel and allows us to
|
|
|
|
// not require successful API calls to Wings to do things.
|
2019-12-22 21:21:21 +00:00
|
|
|
//
|
2021-04-03 21:02:37 +00:00
|
|
|
// This also means mass actions can be performed against servers on the Panel
|
|
|
|
// and they will automatically sync with Wings when the server is started.
|
2019-12-22 21:21:21 +00:00
|
|
|
func (s *Server) Sync() error {
|
2021-08-02 21:07:00 +00:00
|
|
|
cfg, err := s.client.GetServerConfiguration(s.Context(), s.ID())
|
2020-10-31 17:04:20 +00:00
|
|
|
if err != nil {
|
2021-05-02 22:41:02 +00:00
|
|
|
if err := remote.AsRequestError(err); err != nil && err.StatusCode() == http.StatusNotFound {
|
2019-12-22 21:21:21 +00:00
|
|
|
return &serverDoesNotExist{}
|
2019-12-17 04:47:35 +00:00
|
|
|
}
|
2021-05-02 22:41:02 +00:00
|
|
|
return errors.WithStackIf(err)
|
2019-09-23 04:22:16 +00:00
|
|
|
}
|
2021-08-29 20:37:18 +00:00
|
|
|
|
|
|
|
if err := s.SyncWithConfiguration(cfg); err != nil {
|
|
|
|
return errors.WithStackIf(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the disk space limits for the server whenever the configuration for
|
|
|
|
// it changes.
|
|
|
|
s.fs.SetDiskLimit(s.DiskSpace())
|
|
|
|
|
|
|
|
s.SyncWithEnvironment()
|
|
|
|
|
|
|
|
return nil
|
2020-04-10 21:39:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 20:37:18 +00:00
|
|
|
// SyncWithConfiguration accepts a configuration object for a server and will
|
|
|
|
// sync all of the values with the existing server state. This only replaces the
|
|
|
|
// existing configuration and process configuration for the server. The
|
|
|
|
// underlying environment will not be affected. This is because this function
|
|
|
|
// can be called from scoped where the server may not be fully initialized,
|
|
|
|
// therefore other things like the filesystem and environment may not exist yet.
|
2021-02-02 04:50:23 +00:00
|
|
|
func (s *Server) SyncWithConfiguration(cfg remote.ServerConfigurationResponse) error {
|
2021-09-13 21:00:26 +00:00
|
|
|
c := Configuration{
|
|
|
|
CrashDetectionEnabled: config.Get().System.CrashDetection.CrashDetectionEnabled,
|
|
|
|
}
|
2021-08-29 20:37:18 +00:00
|
|
|
if err := json.Unmarshal(cfg.Settings, &c); err != nil {
|
|
|
|
return errors.WithStackIf(err)
|
2019-12-22 21:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 20:37:18 +00:00
|
|
|
s.cfg.mu.Lock()
|
|
|
|
defer s.cfg.mu.Unlock()
|
|
|
|
|
2021-09-13 21:00:26 +00:00
|
|
|
// Lock the new configuration. Since we have the deferred Unlock above we need
|
2021-08-29 20:37:18 +00:00
|
|
|
// to make sure that the NEW configuration object is already locked since that
|
2021-09-13 21:00:26 +00:00
|
|
|
// defer is running on the memory address for "s.cfg.mu" which we're explicitly
|
2021-08-29 20:37:18 +00:00
|
|
|
// changing on the next line.
|
|
|
|
c.mu.Lock()
|
|
|
|
|
|
|
|
//goland:noinspection GoVetCopyLock
|
|
|
|
s.cfg = c
|
|
|
|
|
2020-07-18 23:03:25 +00:00
|
|
|
s.Lock()
|
|
|
|
s.procConfig = cfg.ProcessConfiguration
|
|
|
|
s.Unlock()
|
|
|
|
|
2019-12-22 21:21:21 +00:00
|
|
|
return nil
|
2019-03-25 00:27:14 +00:00
|
|
|
}
|
2019-04-04 05:01:11 +00:00
|
|
|
|
2019-04-06 19:27:44 +00:00
|
|
|
// Reads the log file for a server up to a specified number of bytes.
|
2020-09-07 20:04:56 +00:00
|
|
|
func (s *Server) ReadLogfile(len int) ([]string, error) {
|
2019-04-20 23:26:55 +00:00
|
|
|
return s.Environment.Readlog(len)
|
2019-04-06 23:53:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 05:01:11 +00:00
|
|
|
// Determine if the server is bootable in it's current state or not. This will not
|
|
|
|
// indicate why a server is not bootable, only if it is.
|
|
|
|
func (s *Server) IsBootable() bool {
|
2019-04-20 23:26:55 +00:00
|
|
|
exists, _ := s.Environment.Exists()
|
2019-04-20 23:20:08 +00:00
|
|
|
|
|
|
|
return exists
|
2019-04-04 05:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 19:08:40 +00:00
|
|
|
// Initializes a server instance. This will run through and ensure that the environment
|
2019-04-04 05:01:11 +00:00
|
|
|
// for the server is setup, and that all of the necessary files are created.
|
|
|
|
func (s *Server) CreateEnvironment() error {
|
2020-08-11 04:38:42 +00:00
|
|
|
// Ensure the data directory exists before getting too far through this process.
|
2020-09-27 19:24:08 +00:00
|
|
|
if err := s.EnsureDataDirectoryExists(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 23:26:55 +00:00
|
|
|
return s.Environment.Create()
|
2019-04-21 19:02:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Checks if the server is marked as being suspended or not on the system.
|
|
|
|
func (s *Server) IsSuspended() bool {
|
|
|
|
return s.Config().Suspended
|
|
|
|
}
|
2020-08-11 04:38:42 +00:00
|
|
|
|
2021-02-02 05:28:46 +00:00
|
|
|
func (s *Server) ProcessConfiguration() *remote.ProcessConfiguration {
|
2020-08-20 02:08:15 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.procConfig
|
2020-09-07 20:04:56 +00:00
|
|
|
}
|
2021-01-23 18:45:29 +00:00
|
|
|
|
|
|
|
// Filesystem returns an instance of the filesystem for this server.
|
|
|
|
func (s *Server) Filesystem() *filesystem.Filesystem {
|
|
|
|
return s.fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureDataDirectoryExists ensures that the data directory for the server
|
|
|
|
// instance exists.
|
|
|
|
func (s *Server) EnsureDataDirectoryExists() error {
|
|
|
|
if _, err := os.Lstat(s.fs.Path()); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
s.Log().Debug("server: creating root directory and setting permissions")
|
2021-09-13 21:00:26 +00:00
|
|
|
if err := os.MkdirAll(s.fs.Path(), 0o700); err != nil {
|
2021-01-23 18:45:29 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
if err := s.fs.Chown("/"); err != nil {
|
|
|
|
s.Log().WithField("error", err).Warn("server: failed to chown server data directory")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.WrapIf(err, "server: failed to stat server root directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-26 04:28:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// OnStateChange sets the state of the server internally. This function handles crash detection as
|
2021-01-26 04:28:24 +00:00
|
|
|
// well as reporting to event listeners for the server.
|
|
|
|
func (s *Server) OnStateChange() {
|
|
|
|
prevState := s.resources.State.Load()
|
|
|
|
|
|
|
|
st := s.Environment.State()
|
|
|
|
// Update the currently tracked state for the server.
|
|
|
|
s.resources.State.Store(st)
|
|
|
|
|
|
|
|
// Emit the event to any listeners that are currently registered.
|
|
|
|
if prevState != s.Environment.State() {
|
|
|
|
s.Log().WithField("status", st).Debug("saw server status change event")
|
|
|
|
s.Events().Publish(StatusEvent, st)
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
// Reset the resource usage to 0 when the process fully stops so that all the UI
|
2021-01-26 04:28:24 +00:00
|
|
|
// views in the Panel correctly display 0.
|
|
|
|
if st == environment.ProcessOfflineState {
|
|
|
|
s.resources.Reset()
|
|
|
|
s.emitProcUsage()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If server was in an online state, and is now in an offline state we should handle
|
|
|
|
// that as a crash event. In that scenario, check the last crash time, and the crash
|
|
|
|
// counter.
|
|
|
|
//
|
|
|
|
// In the event that we have passed the thresholds, don't do anything, otherwise
|
|
|
|
// automatically attempt to start the process back up for the user. This is done in a
|
|
|
|
// separate thread as to not block any actions currently taking place in the flow
|
|
|
|
// that called this function.
|
|
|
|
if (prevState == environment.ProcessStartingState || prevState == environment.ProcessRunningState) && s.Environment.State() == environment.ProcessOfflineState {
|
|
|
|
s.Log().Info("detected server as entering a crashed state; running crash handler")
|
|
|
|
|
|
|
|
go func(server *Server) {
|
|
|
|
if err := server.handleServerCrash(); err != nil {
|
|
|
|
if IsTooFrequentCrashError(err) {
|
|
|
|
server.Log().Info("did not restart server after crash; occurred too soon after the last")
|
|
|
|
} else {
|
|
|
|
s.PublishConsoleOutputFromDaemon("Server crash was detected but an error occurred while handling it.")
|
|
|
|
server.Log().WithField("error", err).Error("failed to handle server crash")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:50:23 +00:00
|
|
|
// IsRunning determines if the server state is running or not. This is different
|
2021-08-02 21:07:00 +00:00
|
|
|
// from the environment state, it is simply the tracked state from this daemon
|
2021-02-02 04:50:23 +00:00
|
|
|
// instance, and not the response from Docker.
|
2021-01-26 04:28:24 +00:00
|
|
|
func (s *Server) IsRunning() bool {
|
|
|
|
st := s.Environment.State()
|
|
|
|
|
|
|
|
return st == environment.ProcessRunningState || st == environment.ProcessStartingState
|
|
|
|
}
|
2021-04-04 17:20:27 +00:00
|
|
|
|
|
|
|
// APIResponse is a type returned when requesting details about a single server
|
|
|
|
// instance on Wings. This includes the information needed by the Panel in order
|
|
|
|
// to show resource utilization and the current state on this system.
|
|
|
|
type APIResponse struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
IsSuspended bool `json:"is_suspended"`
|
|
|
|
Utilization ResourceUsage `json:"utilization"`
|
|
|
|
Configuration Configuration `json:"configuration"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAPIResponse returns the server struct as an API object that can be consumed
|
|
|
|
// by callers.
|
|
|
|
func (s *Server) ToAPIResponse() APIResponse {
|
|
|
|
return APIResponse{
|
|
|
|
State: s.Environment.State(),
|
|
|
|
IsSuspended: s.IsSuspended(),
|
|
|
|
Utilization: s.Proc(),
|
|
|
|
Configuration: *s.Config(),
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 03:23:29 +00:00
|
|
|
|
|
|
|
func (s *Server) LogSink() *sinkPool {
|
|
|
|
return s.logSink
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) InstallSink() *sinkPool {
|
|
|
|
return s.installSink
|
|
|
|
}
|