Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Nodes/NodeUpdateService.php
10261 views
1
<?php
2
3
namespace Pterodactyl\Services\Nodes;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\Node;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Database\ConnectionInterface;
9
use Illuminate\Contracts\Encryption\Encrypter;
10
use Pterodactyl\Repositories\Eloquent\NodeRepository;
11
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
12
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
13
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
14
15
class NodeUpdateService
16
{
17
/**
18
* NodeUpdateService constructor.
19
*/
20
public function __construct(
21
private ConnectionInterface $connection,
22
private DaemonConfigurationRepository $configurationRepository,
23
private Encrypter $encrypter,
24
private NodeRepository $repository,
25
) {
26
}
27
28
/**
29
* Update the configuration values for a given node on the machine.
30
*
31
* @throws \Throwable
32
*/
33
public function handle(Node $node, array $data, bool $resetToken = false): Node
34
{
35
if ($resetToken) {
36
$data['daemon_token'] = $this->encrypter->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
37
$data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);
38
}
39
40
[$updated, $exception] = $this->connection->transaction(function () use ($data, $node) {
41
/** @var Node $updated */
42
$updated = $this->repository->withFreshModel()->update($node->id, $data, true, true);
43
44
try {
45
// If we're changing the FQDN for the node, use the newly provided FQDN for the connection
46
// address. This should alleviate issues where the node gets pointed to a "valid" FQDN that
47
// isn't actually running the daemon software, and therefore you can't actually change it
48
// back.
49
//
50
// This makes more sense anyways, because only the Panel uses the FQDN for connecting, the
51
// node doesn't actually care about this.
52
//
53
// @see https://github.com/pterodactyl/panel/issues/1931
54
$node->fqdn = $updated->fqdn;
55
56
$this->configurationRepository->setNode($node)->update($updated);
57
} catch (DaemonConnectionException $exception) {
58
Log::warning($exception, ['node_id' => $node->id]);
59
60
// Never actually throw these exceptions up the stack. If we were able to change the settings
61
// but something went wrong with Wings we just want to store the update and let the user manually
62
// make changes as needed.
63
//
64
// This avoids issues with proxies such as Cloudflare which will see Wings as offline and then
65
// inject their own response pages, causing this logic to get fucked up.
66
//
67
// @see https://github.com/pterodactyl/panel/issues/2712
68
return [$updated, true];
69
}
70
71
return [$updated, false];
72
});
73
74
if ($exception) {
75
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
76
}
77
78
return $updated;
79
}
80
}
81
82