2017-10-06 04:09:43 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
namespace Pterodactyl\Services\Eggs;
|
2017-10-06 04:09:43 +00:00
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
2017-10-06 04:09:43 +00:00
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
class EggConfigurationService
|
2017-10-06 04:09:43 +00:00
|
|
|
{
|
2019-09-22 22:30:53 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
|
|
|
private $configurationStructureService;
|
2017-10-06 04:09:43 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* EggConfigurationService constructor.
|
2017-10-06 04:09:43 +00:00
|
|
|
*/
|
2020-11-01 20:25:02 +00:00
|
|
|
public function __construct(ServerConfigurationStructureService $configurationStructureService)
|
|
|
|
{
|
2019-09-22 22:30:53 +00:00
|
|
|
$this->configurationStructureService = $configurationStructureService;
|
2017-10-06 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 21:16:51 +00:00
|
|
|
* Return an Egg file to be used by the Daemon.
|
2017-10-06 04:09:43 +00:00
|
|
|
*/
|
2019-09-22 22:30:53 +00:00
|
|
|
public function handle(Server $server): array
|
|
|
|
{
|
|
|
|
$configs = $this->replacePlaceholders(
|
2021-01-23 20:33:34 +00:00
|
|
|
$server,
|
|
|
|
json_decode($server->egg->inherit_config_files)
|
2019-09-22 22:30:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
2020-08-05 03:34:44 +00:00
|
|
|
'startup' => $this->convertStartupToNewFormat(json_decode($server->egg->inherit_config_startup, true)),
|
2019-09-22 22:30:53 +00:00
|
|
|
'stop' => $this->convertStopToNewFormat($server->egg->inherit_config_stop),
|
|
|
|
'configs' => $configs,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:34:44 +00:00
|
|
|
/**
|
|
|
|
* Convert the "done" variable into an array if it is not currently one.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function convertStartupToNewFormat(array $startup)
|
|
|
|
{
|
|
|
|
$done = Arr::get($startup, 'done');
|
|
|
|
|
2020-08-05 03:38:24 +00:00
|
|
|
return [
|
2020-08-05 03:34:44 +00:00
|
|
|
'done' => is_string($done) ? [$done] : $done,
|
2020-08-05 03:39:18 +00:00
|
|
|
'user_interaction' => Arr::get($startup, 'userInteraction') ?? Arr::get($startup, 'user_interaction') ?? [],
|
2020-08-05 03:38:24 +00:00
|
|
|
'strip_ansi' => Arr::get($startup, 'strip_ansi') ?? false,
|
|
|
|
];
|
2020-08-05 03:34:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
/**
|
|
|
|
* Converts a legacy stop string into a new generation stop option for a server.
|
|
|
|
*
|
|
|
|
* For most eggs, this ends up just being a command sent to the server console, but
|
|
|
|
* if the stop command is something starting with a caret (^), it will be converted
|
|
|
|
* into the associated kill signal for the instance.
|
|
|
|
*/
|
|
|
|
protected function convertStopToNewFormat(string $stop): array
|
2017-10-06 04:09:43 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!Str::startsWith($stop, '^')) {
|
2019-09-22 22:30:53 +00:00
|
|
|
return [
|
|
|
|
'type' => 'command',
|
|
|
|
'value' => $stop,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$signal = substr($stop, 1);
|
|
|
|
if (strtoupper($signal) === 'C') {
|
|
|
|
return [
|
|
|
|
'type' => 'stop',
|
|
|
|
'value' => null,
|
|
|
|
];
|
2017-10-06 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2019-09-22 22:30:53 +00:00
|
|
|
'type' => 'signal',
|
|
|
|
'value' => strtoupper($signal),
|
2017-10-06 04:09:43 +00:00
|
|
|
];
|
|
|
|
}
|
2019-09-22 22:30:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function replacePlaceholders(Server $server, object $configs)
|
|
|
|
{
|
|
|
|
// Get the legacy configuration structure for the server so that we
|
|
|
|
// can property map the egg placeholders to values.
|
2020-12-24 18:10:40 +00:00
|
|
|
$structure = $this->configurationStructureService->handle($server, [], true);
|
2019-09-22 22:30:53 +00:00
|
|
|
|
|
|
|
$response = [];
|
|
|
|
// Normalize the output of the configuration for the new Wings Daemon to more
|
|
|
|
// easily ingest, as well as make things more flexible down the road.
|
|
|
|
foreach ($configs as $file => $data) {
|
2021-01-23 20:09:16 +00:00
|
|
|
$append = array_merge((array) $data, ['file' => $file, 'replace' => []]);
|
2020-11-01 21:07:00 +00:00
|
|
|
|
|
|
|
foreach ($this->iterate($data->find, $structure) as $find => $replace) {
|
|
|
|
if (is_object($replace)) {
|
|
|
|
foreach ($replace as $match => $replaceWith) {
|
|
|
|
$append['replace'][] = [
|
|
|
|
'match' => $find,
|
|
|
|
'if_value' => $match,
|
|
|
|
'replace_with' => $replaceWith,
|
|
|
|
];
|
2020-04-12 19:22:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 21:07:00 +00:00
|
|
|
continue;
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
2020-11-01 21:07:00 +00:00
|
|
|
|
|
|
|
$append['replace'][] = [
|
|
|
|
'match' => $find,
|
|
|
|
'replace_with' => $replace,
|
|
|
|
];
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 21:07:00 +00:00
|
|
|
unset($append['find']);
|
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
$response[] = $append;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2020-04-12 01:27:47 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-01 20:25:02 +00:00
|
|
|
* Replaces the legacy modifies from eggs with their new counterpart. The legacy Daemon would
|
|
|
|
* set SERVER_MEMORY, SERVER_IP, and SERVER_PORT with their respective values on the Daemon
|
|
|
|
* side. Ensure that anything referencing those properly replaces them with the matching config
|
|
|
|
* value.
|
2020-04-12 01:27:47 +00:00
|
|
|
*/
|
2020-11-01 20:25:02 +00:00
|
|
|
protected function replaceLegacyModifiers(string $key, string $value): string
|
2020-04-12 01:27:47 +00:00
|
|
|
{
|
|
|
|
switch ($key) {
|
2020-04-12 19:22:50 +00:00
|
|
|
case 'config.docker.interface':
|
2020-11-01 20:25:02 +00:00
|
|
|
$replace = 'config.docker.network.interface';
|
2020-04-12 19:22:50 +00:00
|
|
|
break;
|
2020-04-12 01:27:47 +00:00
|
|
|
case 'server.build.env.SERVER_MEMORY':
|
|
|
|
case 'env.SERVER_MEMORY':
|
2020-11-01 20:25:02 +00:00
|
|
|
$replace = 'server.build.memory';
|
2020-04-12 01:27:47 +00:00
|
|
|
break;
|
|
|
|
case 'server.build.env.SERVER_IP':
|
|
|
|
case 'env.SERVER_IP':
|
2020-11-01 20:25:02 +00:00
|
|
|
$replace = 'server.build.default.ip';
|
2020-04-12 01:27:47 +00:00
|
|
|
break;
|
|
|
|
case 'server.build.env.SERVER_PORT':
|
|
|
|
case 'env.SERVER_PORT':
|
2020-11-01 20:25:02 +00:00
|
|
|
$replace = 'server.build.default.port';
|
2020-04-12 01:27:47 +00:00
|
|
|
break;
|
2020-11-01 20:25:02 +00:00
|
|
|
// By default we don't need to change anything, only if we ended up matching a specific legacy item.
|
|
|
|
default:
|
|
|
|
$replace = $key;
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:25:02 +00:00
|
|
|
return str_replace("{{{$key}}}", "{{{$replace}}}", $value);
|
|
|
|
}
|
2020-04-12 19:22:50 +00:00
|
|
|
|
2020-11-01 20:25:02 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2020-11-01 20:25:02 +00:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
protected function matchAndReplaceKeys($value, array $structure)
|
|
|
|
{
|
|
|
|
preg_match_all('/{{(?<key>[\w.-]*)}}/', $value, $matches);
|
|
|
|
|
|
|
|
foreach ($matches['key'] as $key) {
|
|
|
|
// Matched something in {{server.X}} format, now replace that with the actual
|
|
|
|
// value from the server properties.
|
|
|
|
//
|
|
|
|
// The Daemon supports server.X, env.X, and config.X placeholders.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!Str::startsWith($key, ['server.', 'env.', 'config.'])) {
|
2020-11-01 20:25:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do a replacement on anything that is not a string, we don't want to unintentionally
|
|
|
|
// modify the resulting output.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_string($value)) {
|
2020-11-01 20:25:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $this->replaceLegacyModifiers($key, $value);
|
|
|
|
|
|
|
|
// We don't want to do anything with config keys since the Daemon will need to handle
|
|
|
|
// that. For example, the Spigot egg uses "config.docker.interface" to identify the Docker
|
|
|
|
// interface to proxy through, but the Panel would be unaware of that.
|
|
|
|
if (Str::startsWith($key, 'config.')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace anything starting with "server." with the value out of the server configuration
|
|
|
|
// array that used to be created for the old daemon.
|
|
|
|
if (Str::startsWith($key, 'server.')) {
|
|
|
|
$plucked = Arr::get($structure, preg_replace('/^server\./', '', $key), '');
|
|
|
|
|
|
|
|
$value = str_replace("{{{$key}}}", $plucked, $value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, replace anything starting with env. with the expected environment
|
|
|
|
// variable from the server configuration.
|
2020-04-12 01:27:47 +00:00
|
|
|
$plucked = Arr::get(
|
2021-01-23 20:33:34 +00:00
|
|
|
$structure,
|
|
|
|
preg_replace('/^env\./', 'build.env.', $key),
|
|
|
|
''
|
2020-04-12 01:27:47 +00:00
|
|
|
);
|
|
|
|
|
2020-11-01 20:25:02 +00:00
|
|
|
$value = str_replace("{{{$key}}}", $plucked, $value);
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:25:02 +00:00
|
|
|
return $value;
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over a set of "find" values for a given file in the parser configuration. If
|
|
|
|
* the value of the line match is something iterable, continue iterating, otherwise perform
|
|
|
|
* a match & replace.
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2020-11-01 21:07:00 +00:00
|
|
|
* @return mixed
|
2020-04-12 01:27:47 +00:00
|
|
|
*/
|
2020-11-01 21:07:00 +00:00
|
|
|
private function iterate($data, array $structure)
|
2020-04-12 01:27:47 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_iterable($data) && !is_object($data)) {
|
2020-11-01 21:07:00 +00:00
|
|
|
return $data;
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 21:07:00 +00:00
|
|
|
// Remember, in PHP objects are always passed by reference, so if we do not clone this object
|
|
|
|
// instance we'll end up making modifications to the object outside the scope of this function
|
|
|
|
// which leads to some fun behavior in the parser.
|
|
|
|
$clone = clone $data;
|
|
|
|
foreach ($clone as $key => &$value) {
|
2020-04-12 01:27:47 +00:00
|
|
|
if (is_iterable($value) || is_object($value)) {
|
2020-11-01 21:07:00 +00:00
|
|
|
$value = $this->iterate($value, $structure);
|
2020-04-12 01:27:47 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-01 20:25:02 +00:00
|
|
|
$value = $this->matchAndReplaceKeys($value, $structure);
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
2020-11-01 21:07:00 +00:00
|
|
|
|
|
|
|
return $clone;
|
2020-04-12 01:27:47 +00:00
|
|
|
}
|
2017-10-06 04:09:43 +00:00
|
|
|
}
|