Path: blob/1.0-develop/app/Services/Servers/ServerConfigurationStructureService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Pterodactyl\Models\Mount;5use Pterodactyl\Models\Server;67class ServerConfigurationStructureService8{9/**10* ServerConfigurationStructureService constructor.11*/12public function __construct(private EnvironmentService $environment)13{14}1516/**17* Return a configuration array for a specific server when passed a server model.18*19* DO NOT MODIFY THIS FUNCTION. This powers legacy code handling for the new Wings20* daemon, if you modify the structure eggs will break unexpectedly.21*/22public function handle(Server $server, array $override = [], bool $legacy = false): array23{24$clone = $server;25// If any overrides have been set on this call make sure to update them on the26// cloned instance so that the configuration generated uses them.27if (!empty($override)) {28$clone = $server->fresh();29foreach ($override as $key => $value) {30$clone->setAttribute($key, $value);31}32}3334return $legacy35? $this->returnLegacyFormat($clone)36: $this->returnCurrentFormat($clone);37}3839/**40* Returns the new data format used for the Wings daemon.41*/42protected function returnCurrentFormat(Server $server): array43{44return [45'uuid' => $server->uuid,46'meta' => [47'name' => $server->name,48'description' => $server->description,49],50'suspended' => $server->isSuspended(),51'environment' => $this->environment->handle($server),52'invocation' => $server->startup,53'skip_egg_scripts' => $server->skip_scripts,54'build' => [55'memory_limit' => $server->memory,56'swap' => $server->swap,57'io_weight' => $server->io,58'cpu_limit' => $server->cpu,59'threads' => $server->threads,60'disk_space' => $server->disk,61'oom_disabled' => $server->oom_disabled,62],63'container' => [64'image' => $server->image,65// This field is deprecated — use the value in the "build" block.66//67// TODO: remove this key in V2.68'oom_disabled' => $server->oom_disabled,69'requires_rebuild' => false,70],71'allocations' => [72'force_outgoing_ip' => $server->egg->force_outgoing_ip,73'default' => [74'ip' => $server->allocation->ip,75'port' => $server->allocation->port,76],77'mappings' => $server->getAllocationMappings(),78],79'mounts' => $server->mounts->map(function (Mount $mount) {80return [81'source' => $mount->source,82'target' => $mount->target,83'read_only' => $mount->read_only,84];85}),86'egg' => [87'id' => $server->egg->uuid,88'file_denylist' => $server->egg->inherit_file_denylist,89],90];91}9293/**94* Returns the legacy server data format to continue support for old egg configurations95* that have not yet been updated.96*97* @deprecated98*/99protected function returnLegacyFormat(Server $server): array100{101return [102'uuid' => $server->uuid,103'build' => [104'default' => [105'ip' => $server->allocation->ip,106'port' => $server->allocation->port,107],108'ports' => $server->allocations->groupBy('ip')->map(function ($item) {109return $item->pluck('port');110})->toArray(),111'env' => $this->environment->handle($server),112'oom_disabled' => $server->oom_disabled,113'memory' => (int) $server->memory,114'swap' => (int) $server->swap,115'io' => (int) $server->io,116'cpu' => (int) $server->cpu,117'threads' => $server->threads,118'disk' => (int) $server->disk,119'image' => $server->image,120],121'service' => [122'egg' => $server->egg->uuid,123'skip_scripts' => $server->skip_scripts,124],125'rebuild' => false,126'suspended' => $server->isSuspended() ? 1 : 0,127];128}129}130131132