|
9 | 9 |
|
10 | 10 | namespace Pterodactyl\Services\Eggs; |
11 | 11 |
|
| 12 | +use Illuminate\Support\Arr; |
| 13 | +use Illuminate\Support\Str; |
12 | 14 | use Pterodactyl\Models\Egg; |
| 15 | +use Pterodactyl\Models\Server; |
13 | 16 | use Pterodactyl\Contracts\Repository\EggRepositoryInterface; |
| 17 | +use Pterodactyl\Services\Servers\ServerConfigurationStructureService; |
14 | 18 |
|
15 | 19 | class EggConfigurationService |
16 | 20 | { |
17 | 21 | /** |
18 | 22 | * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface |
19 | 23 | */ |
20 | | - protected $repository; |
| 24 | + private $repository; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService |
| 28 | + */ |
| 29 | + private $configurationStructureService; |
21 | 30 |
|
22 | 31 | /** |
23 | 32 | * EggConfigurationService constructor. |
24 | 33 | * |
25 | 34 | * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository |
| 35 | + * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService |
26 | 36 | */ |
27 | | - public function __construct(EggRepositoryInterface $repository) |
28 | | - { |
| 37 | + public function __construct( |
| 38 | + EggRepositoryInterface $repository, |
| 39 | + ServerConfigurationStructureService $configurationStructureService |
| 40 | + ) { |
29 | 41 | $this->repository = $repository; |
| 42 | + $this->configurationStructureService = $configurationStructureService; |
30 | 43 | } |
31 | 44 |
|
32 | 45 | /** |
33 | 46 | * Return an Egg file to be used by the Daemon. |
34 | 47 | * |
35 | | - * @param int|\Pterodactyl\Models\Egg $egg |
| 48 | + * @param \Pterodactyl\Models\Server $server |
36 | 49 | * @return array |
37 | 50 | * |
38 | 51 | * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
39 | 52 | */ |
40 | | - public function handle($egg): array |
| 53 | + public function handle(Server $server): array |
| 54 | + { |
| 55 | + $configs = $this->replacePlaceholders( |
| 56 | + $server, json_decode($server->egg->inherit_config_files) |
| 57 | + ); |
| 58 | + |
| 59 | + return [ |
| 60 | + 'startup' => json_decode($server->egg->inherit_config_startup), |
| 61 | + 'stop' => $this->convertStopToNewFormat($server->egg->inherit_config_stop), |
| 62 | + 'configs' => $configs, |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Converts a legacy stop string into a new generation stop option for a server. |
| 68 | + * |
| 69 | + * For most eggs, this ends up just being a command sent to the server console, but |
| 70 | + * if the stop command is something starting with a caret (^), it will be converted |
| 71 | + * into the associated kill signal for the instance. |
| 72 | + * |
| 73 | + * @param string $stop |
| 74 | + * @return array |
| 75 | + */ |
| 76 | + protected function convertStopToNewFormat(string $stop): array |
41 | 77 | { |
42 | | - if (! $egg instanceof Egg) { |
43 | | - $egg = $this->repository->getWithCopyAttributes($egg); |
| 78 | + if (! Str::startsWith($stop, '^')) { |
| 79 | + return [ |
| 80 | + 'type' => 'command', |
| 81 | + 'value' => $stop, |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + $signal = substr($stop, 1); |
| 86 | + if (strtoupper($signal) === 'C') { |
| 87 | + return [ |
| 88 | + 'type' => 'stop', |
| 89 | + 'value' => null, |
| 90 | + ]; |
44 | 91 | } |
45 | 92 |
|
46 | 93 | return [ |
47 | | - 'startup' => json_decode($egg->inherit_config_startup), |
48 | | - 'stop' => $egg->inherit_config_stop, |
49 | | - 'configs' => json_decode($egg->inherit_config_files), |
50 | | - 'log' => json_decode($egg->inherit_config_logs), |
51 | | - 'query' => 'none', |
| 94 | + 'type' => 'signal', |
| 95 | + 'value' => strtoupper($signal), |
52 | 96 | ]; |
53 | 97 | } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param \Pterodactyl\Models\Server $server |
| 101 | + * @param object $configs |
| 102 | + * @return array |
| 103 | + * |
| 104 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 105 | + */ |
| 106 | + protected function replacePlaceholders(Server $server, object $configs) |
| 107 | + { |
| 108 | + // Get the legacy configuration structure for the server so that we |
| 109 | + // can property map the egg placeholders to values. |
| 110 | + $structure = $this->configurationStructureService->handle($server); |
| 111 | + |
| 112 | + foreach ($configs as $file => $data) { |
| 113 | + foreach ($data->find ?? [] as &$value) { |
| 114 | + preg_match('/^{{(?<key>.*)}}$/', $value, $matches); |
| 115 | + |
| 116 | + if (! $key = $matches['key'] ?? null) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + // Matched something in {{server.X}} format, now replace that with the actual |
| 121 | + // value from the server properties. |
| 122 | + // |
| 123 | + // The Daemon supports server.X, env.X, and config.X placeholders. |
| 124 | + if (! Str::startsWith($key, ['server.', 'env.', 'config.'])) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + // We don't want to do anything with config keys since the Daemon will need to handle |
| 129 | + // that. For example, the Spigot egg uses "config.docker.interface" to identify the Docker |
| 130 | + // interface to proxy through, but the Panel would be unaware of that. |
| 131 | + if (Str::startsWith($key, 'config.')) { |
| 132 | + $value = "{{{$key}}}"; |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + // The legacy Daemon would set SERVER_MEMORY, SERVER_IP, and SERVER_PORT with their |
| 137 | + // respective values on the Daemon side. Ensure that anything referencing those properly |
| 138 | + // replaces them with the matching config value. |
| 139 | + switch ($key) { |
| 140 | + case 'server.build.env.SERVER_MEMORY': |
| 141 | + case 'env.SERVER_MEMORY': |
| 142 | + $key = 'server.build.memory'; |
| 143 | + break; |
| 144 | + case 'server.build.env.SERVER_IP': |
| 145 | + case 'env.SERVER_IP': |
| 146 | + $key = 'server.build.default.ip'; |
| 147 | + break; |
| 148 | + case 'server.build.env.SERVER_PORT': |
| 149 | + case 'env.SERVER_PORT': |
| 150 | + $key = 'server.build.default.port'; |
| 151 | + break; |
| 152 | + } |
| 153 | + |
| 154 | + // Replace anything starting with "server." with the value out of the server configuration |
| 155 | + // array that used to be created for the old daemon. |
| 156 | + if (Str::startsWith($key, 'server.')) { |
| 157 | + $value = Arr::get( |
| 158 | + $structure, preg_replace('/^server\./', '', $key), '' |
| 159 | + ); |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + // Finally, replace anything starting with env. with the expected environment |
| 164 | + // variable from the server configuration. |
| 165 | + $value = Arr::get( |
| 166 | + $structure, preg_replace('/^env\./', 'build.env.', $key), '' |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + $response = []; |
| 172 | + // Normalize the output of the configuration for the new Wings Daemon to more |
| 173 | + // easily ingest, as well as make things more flexible down the road. |
| 174 | + foreach ($configs as $file => $data) { |
| 175 | + $append = ['file' => $file, 'replace' => []]; |
| 176 | + |
| 177 | + // I like to think I understand PHP pretty well, but if you don't pass $value |
| 178 | + // by reference here, you'll end up with a resursive array loop if the config |
| 179 | + // file has two replacements that reference the same item in the configuration |
| 180 | + // array for the server. |
| 181 | + foreach ($data as $key => &$value) { |
| 182 | + if ($key !== 'find') { |
| 183 | + $append[$key] = $value; |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + foreach ($value as $find => $replace) { |
| 188 | + $append['replace'][] = ['match' => $find, 'value' => $replace]; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + $response[] = $append; |
| 193 | + } |
| 194 | + |
| 195 | + return $response; |
| 196 | + } |
54 | 197 | } |
0 commit comments