forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEggConfigurationService.php
More file actions
249 lines (215 loc) · 8.24 KB
/
EggConfigurationService.php
File metadata and controls
249 lines (215 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Pterodactyl\Services\Eggs;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
class EggConfigurationService
{
private const NOT_MATCHED = '__no_match';
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
*/
private $configurationStructureService;
/**
* EggConfigurationService constructor.
*
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
*/
public function __construct(
EggRepositoryInterface $repository,
ServerConfigurationStructureService $configurationStructureService
) {
$this->repository = $repository;
$this->configurationStructureService = $configurationStructureService;
}
/**
* Return an Egg file to be used by the Daemon.
*
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle(Server $server): array
{
$configs = $this->replacePlaceholders(
$server, json_decode($server->egg->inherit_config_files)
);
return [
'startup' => json_decode($server->egg->inherit_config_startup),
'stop' => $this->convertStopToNewFormat($server->egg->inherit_config_stop),
'configs' => $configs,
];
}
/**
* 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.
*
* @param string $stop
* @return array
*/
protected function convertStopToNewFormat(string $stop): array
{
if (! Str::startsWith($stop, '^')) {
return [
'type' => 'command',
'value' => $stop,
];
}
$signal = substr($stop, 1);
if (strtoupper($signal) === 'C') {
return [
'type' => 'stop',
'value' => null,
];
}
return [
'type' => 'signal',
'value' => strtoupper($signal),
];
}
/**
* @param \Pterodactyl\Models\Server $server
* @param object $configs
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
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.
$structure = $this->configurationStructureService->handle($server, true);
foreach ($configs as $file => &$data) {
$this->iterate($data->find, $structure);
}
$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) {
$append = ['file' => $file, 'replace' => []];
// I like to think I understand PHP pretty well, but if you don't pass $value
// by reference here, you'll end up with a resursive array loop if the config
// file has two replacements that reference the same item in the configuration
// array for the server.
foreach ($data as $key => &$value) {
if ($key !== 'find') {
$append[$key] = $value;
continue;
}
foreach ($value as $find => $replace) {
if (is_object($replace)) {
foreach ($replace as $match => $replaceWith) {
$append['replace'][] = [
'match' => $find,
'if_value' => $match,
'replace_with' => $replaceWith,
];
}
continue;
}
$append['replace'][] = [
'match' => $find,
'replace_with' => $replace,
];
}
}
$response[] = $append;
}
return $response;
}
/**
* @param string $value
* @param array $structure
* @return string|null
*/
protected function matchAndReplaceKeys(string $value, array $structure): ?string
{
preg_match('/{{(?<key>.*)}}/', $value, $matches);
if (! $key = $matches['key'] ?? null) {
return self::NOT_MATCHED;
}
// 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.
if (! Str::startsWith($key, ['server.', 'env.', 'config.'])) {
return self::NOT_MATCHED;
}
// 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.
switch ($key) {
case 'config.docker.interface':
$key = 'config.docker.network.interface';
break;
case 'server.build.env.SERVER_MEMORY':
case 'env.SERVER_MEMORY':
$key = 'server.build.memory';
break;
case 'server.build.env.SERVER_IP':
case 'env.SERVER_IP':
$key = 'server.build.default.ip';
break;
case 'server.build.env.SERVER_PORT':
case 'env.SERVER_PORT':
$key = 'server.build.default.port';
break;
}
// 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.')) {
return preg_replace('/{{(.*)}}/', "{{{$key}}}", $value);
}
// 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), ''
);
return preg_replace('/{{(.*)}}/', $plucked, $value);
}
// Finally, replace anything starting with env. with the expected environment
// variable from the server configuration.
$plucked = Arr::get(
$structure, preg_replace('/^env\./', 'build.env.', $key), ''
);
return preg_replace('/{{(.*)}}/', $plucked, $value);
}
/**
* 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
* @param array $structure
*/
private function iterate(&$data, array $structure)
{
if (! is_iterable($data) && ! is_object($data)) {
return;
}
foreach ($data as &$value) {
if (is_iterable($value) || is_object($value)) {
$this->iterate($value, $structure);
continue;
}
$response = $this->matchAndReplaceKeys($value, $structure);
if ($response === self::NOT_MATCHED) {
continue;
}
$value = $response;
}
}
}