Add http macro

This commit is contained in:
Lance Pioch 2022-10-21 16:34:26 -04:00
parent 5331fd2cdb
commit 264df1876b
2 changed files with 34 additions and 0 deletions

View file

@ -2,15 +2,20 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Support\Facades\Http;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Znck\Eloquent\Traits\BelongsToThrough; use Znck\Eloquent\Traits\BelongsToThrough;
use GuzzleHttp\Exception\TransferException;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException; use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
/** /**
* \Pterodactyl\Models\Server. * \Pterodactyl\Models\Server.
@ -378,4 +383,20 @@ class Server extends Model
throw new ServerStateConflictException($this); throw new ServerStateConflictException($this);
} }
} }
/**
* Sends a command or multiple commands to a running server instance.
*
* @throws DaemonConnectionException|GuzzleException
*/
public function send(array|string $command): ResponseInterface
{
try {
return Http::daemon($this->node)->post("/api/servers/{$this->uuid}/commands", [
'json' => ['commands' => is_array($command) ? $command : [$command]],
])->toPsrResponse();
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
}
}
} }

View file

@ -6,8 +6,10 @@ use View;
use Cache; use Cache;
use Pterodactyl\Models; use Pterodactyl\Models;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Pterodactyl\Models\Node;
use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\URL;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Pterodactyl\Extensions\Themes\Theme; use Pterodactyl\Extensions\Themes\Theme;
@ -49,6 +51,17 @@ class AppServiceProvider extends ServiceProvider
'task' => Models\Task::class, 'task' => Models\Task::class,
'user' => Models\User::class, 'user' => Models\User::class,
]); ]);
Http::macro(
'daemon',
fn (Node $node, array $headers = []) => Http::acceptJson()->withHeaders([
'Authorization' => 'Bearer ' . $node->getDecryptedKey(),
] + $headers)
->withOptions(['verify' => (bool) app()->environment('production')])
->timeout(config('pterodactyl.guzzle.timeout'))
->connectTimeout(config('pterodactyl.guzzle.connect_timeout'))
->baseUrl($node->getConnectionAddress())
);
} }
/** /**