2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-02-06 20:16:54 +00:00
|
|
|
use App\Classes\PterodactylClient;
|
|
|
|
use App\Settings\PterodactylSettings;
|
2021-06-05 09:26:32 +00:00
|
|
|
use Exception;
|
|
|
|
use GuzzleHttp\Promise\PromiseInterface;
|
|
|
|
use Hidehalo\Nanoid\Client;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
|
|
use Illuminate\Http\Client\Response;
|
2023-01-05 18:33:46 +00:00
|
|
|
use Spatie\Activitylog\LogOptions;
|
2021-06-05 09:26:32 +00:00
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Server
|
|
|
|
*/
|
|
|
|
class Server extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
use LogsActivity;
|
2023-02-06 20:16:54 +00:00
|
|
|
|
|
|
|
private PterodactylClient $pterodactyl;
|
|
|
|
|
2023-01-05 18:33:46 +00:00
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
{
|
|
|
|
return LogOptions::defaults()
|
2023-02-01 20:03:20 +00:00
|
|
|
-> logOnlyDirty()
|
|
|
|
-> logOnly(['*'])
|
|
|
|
-> dontSubmitEmptyLogs();
|
2023-01-05 18:33:46 +00:00
|
|
|
}
|
2023-02-06 20:16:54 +00:00
|
|
|
|
2021-06-05 09:26:32 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
protected static $ignoreChangedAttributes = ['pterodactyl_id', 'identifier', 'updated_at'];
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected static $logAttributes = ['name', 'description'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2023-02-01 20:03:20 +00:00
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'suspended',
|
|
|
|
'identifier',
|
|
|
|
'product_id',
|
|
|
|
'pterodactyl_id',
|
2021-06-05 09:26:32 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2023-01-05 17:02:42 +00:00
|
|
|
protected $casts = [
|
|
|
|
'suspended' => 'datetime',
|
2021-06-05 09:26:32 +00:00
|
|
|
];
|
|
|
|
|
2023-02-06 20:16:54 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$ptero_settings = new PterodactylSettings();
|
|
|
|
$this->pterodactyl = new PterodactylClient($ptero_settings);
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:04:46 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
2021-06-05 09:26:32 +00:00
|
|
|
parent::boot();
|
|
|
|
|
2021-06-06 20:04:46 +00:00
|
|
|
static::creating(function (Server $server) {
|
2021-06-05 09:26:32 +00:00
|
|
|
$client = new Client();
|
|
|
|
|
|
|
|
$server->{$server->getKeyName()} = $client->generateId($size = 21);
|
|
|
|
});
|
|
|
|
|
2021-06-06 20:04:46 +00:00
|
|
|
static::deleting(function (Server $server) {
|
2023-02-06 20:16:54 +00:00
|
|
|
$response = $server->pterodactyl->client_admin->delete("/application/servers/{$server->pterodactyl_id}");
|
2023-02-01 20:03:20 +00:00
|
|
|
if ($response->failed() && ! is_null($server->pterodactyl_id)) {
|
2021-09-15 06:30:15 +00:00
|
|
|
//only return error when it's not a 404 error
|
|
|
|
if ($response['errors'][0]['status'] != '404') {
|
|
|
|
throw new Exception($response['errors'][0]['code']);
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
public function isSuspended()
|
|
|
|
{
|
2023-02-01 20:03:20 +00:00
|
|
|
return ! is_null($this->suspended);
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PromiseInterface|Response
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
public function getPterodactylServer()
|
|
|
|
{
|
2023-02-06 20:16:54 +00:00
|
|
|
return $this->pterodactyl->client_admin->get("/application/servers/{$this->pterodactyl_id}");
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
public function suspend()
|
|
|
|
{
|
2023-02-06 20:16:54 +00:00
|
|
|
$response = $this->pterodactyl->suspendServer($this);
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
$this->update([
|
2023-01-05 17:01:42 +00:00
|
|
|
'suspended' => now(),
|
2021-06-05 09:26:32 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function unSuspend()
|
|
|
|
{
|
2023-02-06 20:16:54 +00:00
|
|
|
$response = $this->pterodactyl->unSuspendServer($this);
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
$this->update([
|
2023-01-05 17:01:42 +00:00
|
|
|
'suspended' => null,
|
2021-06-05 09:26:32 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasOne
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
public function product()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Product::class, 'id', 'product_id');
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
2021-06-06 20:04:46 +00:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class, 'user_id', 'id');
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
}
|