2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Actions\Nodes;
|
|
|
|
|
|
2026-10-11 23:29:38 -04:00
|
|
|
use Illuminate\Support\Arr;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-10-11 23:29:38 -04:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Pterodactyl\Contracts\Nodes\UpdatesNodes;
|
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
|
|
|
|
|
use Pterodactyl\Facades\Daemon;
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Pterodactyl\Services\Extensions\ExtensionFields;
|
|
|
|
|
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
final readonly class UpdateNode implements UpdatesNodes
|
|
|
|
|
{
|
2026-10-09 06:26:45 +01:00
|
|
|
public function __construct(private ExtensionFields $extensions) {}
|
|
|
|
|
|
2026-10-11 06:48:21 -04:00
|
|
|
public function update(Node $node, array $input, bool $resetToken = false): Node
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
2026-10-11 23:29:38 -04:00
|
|
|
$validated = Validator::validate($input, CreateNode::rules());
|
|
|
|
|
// Blank strings are null, as they are in a request.
|
|
|
|
|
$validated = array_map(fn (mixed $value): mixed => is_string($value) && blank($value) ? null : $value, $validated);
|
|
|
|
|
|
2026-10-11 06:48:21 -04:00
|
|
|
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-10-11 23:29:38 -04:00
|
|
|
// The description, flags and upload size it is not given keep their stored values.
|
|
|
|
|
$attributes = [
|
|
|
|
|
...Arr::except($validated, ['daemon_listen', 'daemon_sftp', 'daemon_base']),
|
|
|
|
|
'daemonListen' => $validated['daemon_listen'],
|
|
|
|
|
'daemonSFTP' => $validated['daemon_sftp'],
|
|
|
|
|
// A node given no daemon base keeps its files in /var/lib/pterodactyl/volumes.
|
|
|
|
|
'daemonBase' => $validated['daemon_base'] ?? '/var/lib/pterodactyl/volumes',
|
|
|
|
|
];
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if ($resetToken) {
|
2026-10-11 06:48:21 -04:00
|
|
|
$attributes['daemon_token'] = Crypt::encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
|
|
|
|
|
$attributes['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2026-10-11 06:48:21 -04:00
|
|
|
[$updated, $exception] = DB::transaction(function () use ($attributes, $node, $extensions): array {
|
2026-10-05 18:10:18 -04:00
|
|
|
// Wings is reached with the pre-update scheme and port, but at the newly provided
|
|
|
|
|
// FQDN: if the node was pointed at a "valid" FQDN that is not actually running
|
|
|
|
|
// Wings, the operator can still change it back. Only the Panel uses the FQDN for
|
|
|
|
|
// connecting; the node itself does not care about it.
|
|
|
|
|
//
|
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/1931
|
|
|
|
|
$daemon = clone $node;
|
|
|
|
|
|
2026-10-11 06:48:21 -04:00
|
|
|
$node->forceFill($attributes)->save();
|
2026-10-09 06:26:45 +01:00
|
|
|
$this->extensions->save($node, $extensions);
|
2026-10-08 17:40:00 -04:00
|
|
|
// A location loaded before the save may no longer match location_id.
|
|
|
|
|
$node->unsetRelation('location');
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
try {
|
2026-10-08 17:40:00 -04:00
|
|
|
$daemon->fqdn = $node->fqdn;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-08 17:40:00 -04:00
|
|
|
Daemon::node($daemon)->update($node);
|
2026-10-05 18:10:18 -04:00
|
|
|
} catch (DaemonConnectionException $daemonConnectionException) {
|
|
|
|
|
Log::warning($daemonConnectionException->getMessage(), ['exception' => $daemonConnectionException, 'node_id' => $node->id]);
|
|
|
|
|
|
|
|
|
|
// Never actually throw these exceptions up the stack. If we were able to change the settings
|
|
|
|
|
// but something went wrong with Wings we just want to store the update and let the user manually
|
|
|
|
|
// make changes as needed.
|
|
|
|
|
//
|
|
|
|
|
// This avoids issues with proxies such as Cloudflare which will see Wings as offline and then
|
|
|
|
|
// inject their own response pages, causing this logic to get fucked up.
|
|
|
|
|
//
|
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2712
|
2026-10-08 17:40:00 -04:00
|
|
|
return [$node, true];
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 17:40:00 -04:00
|
|
|
return [$node, false];
|
2026-10-05 18:10:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($exception) {
|
|
|
|
|
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $updated;
|
|
|
|
|
}
|
|
|
|
|
}
|