2017-10-06 04:09:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2020-09-13 16:59:54 +00:00
|
|
|
use Pterodactyl\Models\Mount;
|
2017-10-06 04:09:43 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
|
|
|
|
class ServerConfigurationStructureService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $environment;
|
2017-10-06 04:09:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ServerConfigurationStructureService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\EnvironmentService $environment
|
2017-10-06 04:09:43 +00:00
|
|
|
*/
|
2020-10-06 04:54:29 +00:00
|
|
|
public function __construct(EnvironmentService $environment)
|
|
|
|
{
|
2017-10-06 04:09:43 +00:00
|
|
|
$this->environment = $environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-27 04:49:54 +00:00
|
|
|
* Return a configuration array for a specific server when passed a server model.
|
|
|
|
*
|
2019-09-22 22:30:53 +00:00
|
|
|
* DO NOT MODIFY THIS FUNCTION. This powers legacy code handling for the new Wings
|
|
|
|
* daemon, if you modify the structure eggs will break unexpectedly.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-12-24 18:10:40 +00:00
|
|
|
* @param array $override
|
2020-10-31 18:14:28 +00:00
|
|
|
* @param bool $legacy deprecated
|
2017-10-06 04:09:43 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-12-24 18:10:40 +00:00
|
|
|
public function handle(Server $server, array $override = [], bool $legacy = false): array
|
2017-10-06 04:09:43 +00:00
|
|
|
{
|
2020-12-24 18:10:40 +00:00
|
|
|
$clone = $server;
|
|
|
|
// If any overrides have been set on this call make sure to update them on the
|
|
|
|
// cloned instance so that the configuration generated uses them.
|
|
|
|
if (!empty($override)) {
|
|
|
|
$clone = $server->fresh();
|
|
|
|
foreach ($override as $key => $value) {
|
|
|
|
$clone->setAttribute($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $legacy
|
|
|
|
? $this->returnLegacyFormat($clone)
|
|
|
|
: $this->returnCurrentFormat($clone);
|
2019-11-16 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the new data format used for the Wings daemon.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function returnCurrentFormat(Server $server)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'uuid' => $server->uuid,
|
2021-01-17 23:51:56 +00:00
|
|
|
'suspended' => $server->isSuspended(),
|
2019-11-16 21:33:01 +00:00
|
|
|
'environment' => $this->environment->handle($server),
|
2019-12-22 05:01:38 +00:00
|
|
|
'invocation' => $server->startup,
|
2020-08-30 16:40:19 +00:00
|
|
|
'skip_egg_scripts' => $server->skip_scripts,
|
2019-11-16 21:33:01 +00:00
|
|
|
'build' => [
|
2019-12-22 05:01:38 +00:00
|
|
|
'memory_limit' => $server->memory,
|
2019-11-16 21:33:01 +00:00
|
|
|
'swap' => $server->swap,
|
2019-12-22 05:01:38 +00:00
|
|
|
'io_weight' => $server->io,
|
|
|
|
'cpu_limit' => $server->cpu,
|
2020-03-29 20:55:06 +00:00
|
|
|
'threads' => $server->threads,
|
2019-12-22 05:01:38 +00:00
|
|
|
'disk_space' => $server->disk,
|
2019-11-16 21:33:01 +00:00
|
|
|
],
|
|
|
|
'container' => [
|
|
|
|
'image' => $server->image,
|
2019-11-24 23:08:54 +00:00
|
|
|
'oom_disabled' => $server->oom_disabled,
|
2019-11-16 21:33:01 +00:00
|
|
|
'requires_rebuild' => false,
|
|
|
|
],
|
|
|
|
'allocations' => [
|
|
|
|
'default' => [
|
|
|
|
'ip' => $server->allocation->ip,
|
|
|
|
'port' => $server->allocation->port,
|
|
|
|
],
|
2019-11-25 04:40:29 +00:00
|
|
|
'mappings' => $server->getAllocationMappings(),
|
2019-11-16 21:33:01 +00:00
|
|
|
],
|
2020-09-13 16:59:54 +00:00
|
|
|
'mounts' => $server->mounts->map(function (Mount $mount) {
|
|
|
|
return [
|
|
|
|
'source' => $mount->source,
|
|
|
|
'target' => $mount->target,
|
|
|
|
'read_only' => $mount->read_only,
|
|
|
|
];
|
|
|
|
}),
|
2021-01-11 01:02:14 +00:00
|
|
|
'egg' => [
|
|
|
|
'id' => $server->egg->uuid,
|
|
|
|
'file_denylist' => [
|
|
|
|
'config.yml',
|
|
|
|
'**/*.json'
|
|
|
|
]
|
|
|
|
// 'file_denylist' => explode(PHP_EOL, $server->egg->inherit_file_denylist),
|
|
|
|
]
|
2019-11-16 21:33:01 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the legacy server data format to continue support for old egg configurations
|
|
|
|
* that have not yet been updated.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
2020-10-31 18:14:28 +00:00
|
|
|
* @deprecated
|
2019-11-16 21:33:01 +00:00
|
|
|
*/
|
|
|
|
protected function returnLegacyFormat(Server $server)
|
|
|
|
{
|
2017-10-06 04:09:43 +00:00
|
|
|
return [
|
|
|
|
'uuid' => $server->uuid,
|
|
|
|
'build' => [
|
|
|
|
'default' => [
|
|
|
|
'ip' => $server->allocation->ip,
|
|
|
|
'port' => $server->allocation->port,
|
|
|
|
],
|
|
|
|
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
|
|
|
|
return $item->pluck('port');
|
|
|
|
})->toArray(),
|
2017-10-27 04:49:54 +00:00
|
|
|
'env' => $this->environment->handle($server),
|
2019-08-03 20:41:24 +00:00
|
|
|
'oom_disabled' => $server->oom_disabled,
|
2020-12-24 18:10:40 +00:00
|
|
|
'memory' => (int)$server->memory,
|
|
|
|
'swap' => (int)$server->swap,
|
|
|
|
'io' => (int)$server->io,
|
|
|
|
'cpu' => (int)$server->cpu,
|
2020-03-29 18:41:55 +00:00
|
|
|
'threads' => $server->threads,
|
2020-12-24 18:10:40 +00:00
|
|
|
'disk' => (int)$server->disk,
|
2017-10-06 04:09:43 +00:00
|
|
|
'image' => $server->image,
|
|
|
|
],
|
|
|
|
'service' => [
|
2017-10-09 02:36:22 +00:00
|
|
|
'egg' => $server->egg->uuid,
|
2017-10-06 04:09:43 +00:00
|
|
|
'skip_scripts' => $server->skip_scripts,
|
|
|
|
],
|
|
|
|
'rebuild' => false,
|
2021-01-17 23:51:56 +00:00
|
|
|
'suspended' => $server->isSuspended() ? 1 : 0,
|
2017-10-06 04:09:43 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|