diff --git a/app/Models/Node.php b/app/Models/Node.php index 78e0828f7..520df2a5e 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -74,35 +74,6 @@ class Node extends Model 'daemonSFTP', 'daemonListen', ]; - /** - * @var array - */ - protected static $guzzle = []; - - /** - * @var array - */ - protected static $nodes = []; - - /** - * Returns an instance of the database object for the requested node ID. - * - * @param int $id - * @return \Illuminate\Database\Eloquent\Model - */ - public static function getByID($id) - { - - // The Node is already cached. - if (array_key_exists($id, self::$nodes)) { - return self::$nodes[$id]; - } - - self::$nodes[$id] = self::where('id', $id)->first(); - - return self::$nodes[$id]; - } - /** * Return an instance of the Guzzle client for this specific node. * @@ -119,32 +90,6 @@ class Node extends Model ]); } - /** - * Returns a Guzzle Client for the node in question. - * - * @param int $node - * @return \GuzzleHttp\Client - * @deprecated - */ - public static function guzzleRequest($node) - { - - // The Guzzle Client is cached already. - if (array_key_exists($node, self::$guzzle)) { - return self::$guzzle[$node]; - } - - $nodeData = self::getByID($node); - - self::$guzzle[$node] = new Client([ - 'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen), - 'timeout' => 5.0, - 'connect_timeout' => 3.0, - ]); - - return self::$guzzle[$node]; - } - /** * Returns the configuration in JSON format. * @@ -194,12 +139,7 @@ class Node extends Model 'keys' => [$this->daemonSecret], ]; - $json_options = JSON_UNESCAPED_SLASHES; - if ($pretty) { - $json_options |= JSON_PRETTY_PRINT; - } - - return json_encode($config, $json_options); + return json_encode($config, ($pretty) ? JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : JSON_UNESCAPED_SLASHES); } /** diff --git a/app/Repositories/Daemon/CommandRepository.php b/app/Repositories/Daemon/CommandRepository.php index 5631518dc..1905478e7 100644 --- a/app/Repositories/Daemon/CommandRepository.php +++ b/app/Repositories/Daemon/CommandRepository.php @@ -32,14 +32,10 @@ use Pterodactyl\Exceptions\DisplayException; class CommandRepository { protected $server; - protected $node; - protected $client; public function __construct($server) { $this->server = ($server instanceof Models\Server) ? $server : Models\Server::findOrFail($server); - $this->node = Models\Node::getByID($this->server->node); - $this->client = Models\Node::guzzleRequest($this->server->node); } /** @@ -56,15 +52,10 @@ class CommandRepository // Additionally not all calls to this will be from a logged in user. // (e.g. task queue or API) try { - $response = $this->client->request('POST', '/server/command', [ - 'headers' => [ - 'X-Access-Token' => $this->server->daemonSecret, - 'X-Access-Server' => $this->server->uuid, - ], - 'json' => [ - 'command' => $command, - ], - ]); + $response = $this->server->node->guzzleClient([ + 'X-Access-Token' => $this->server->daemonSecret, + 'X-Access-Server' => $this->server->uuid, + ])->request('POST', '/server/command', ['json' => ['command' => $command]]); if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { throw new DisplayException('Command sending responded with a non-200 error code.'); diff --git a/app/Repositories/Daemon/PowerRepository.php b/app/Repositories/Daemon/PowerRepository.php index 705ea3e82..d43ec1a7d 100644 --- a/app/Repositories/Daemon/PowerRepository.php +++ b/app/Repositories/Daemon/PowerRepository.php @@ -31,14 +31,10 @@ use Pterodactyl\Exceptions\DisplayException; class PowerRepository { protected $server; - protected $node; - protected $client; public function __construct($server) { $this->server = ($server instanceof Models\Server) ? $server : Models\Server::findOrFail($server); - $this->node = Models\Node::getByID($this->server->node); - $this->client = Models\Node::guzzleRequest($this->server->node); } public function do($action) @@ -48,15 +44,10 @@ class PowerRepository // Additionally not all calls to this will be from a logged in user. // (e.g. task queue or API) try { - $response = $this->client->request('PUT', '/server/power', [ - 'headers' => [ - 'X-Access-Token' => $this->server->daemonSecret, - 'X-Access-Server' => $this->server->uuid, - ], - 'json' => [ - 'action' => $action, - ], - ]); + $response = $this->server->node->guzzleClient([ + 'X-Access-Token' => $this->server->daemonSecret, + 'X-Access-Server' => $this->server->uuid, + ])->request('PUT', '/server/power', ['json' => ['action' => $action]]); if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { throw new DisplayException('Power status responded with a non-200 error code.');