Pterodactyl-Panel/app/Models/Node.php

278 lines
8.6 KiB
PHP
Raw Normal View History

<?php
2016-12-07 23:46:38 +01:00
namespace Pterodactyl\Models;
use Illuminate\Support\Str;
2019-12-08 20:29:46 +01:00
use Symfony\Component\Yaml\Yaml;
use Illuminate\Container\Container;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Encryption\Encrypter;
/**
* @property int $id
* @property string $uuid
* @property bool $public
* @property string $name
* @property string|null $description
* @property int $location_id
* @property int|null $database_host_id
* @property string $fqdn
* @property int $listen_port_http
* @property int $public_port_http
* @property int $listen_port_sftp
* @property int $public_port_sftp
* @property string $scheme
* @property bool $behind_proxy
* @property bool $maintenance_mode
* @property int $memory
* @property int $memory_overallocate
* @property int $disk
* @property int $disk_overallocate
* @property int $upload_size
* @property string $daemon_token_id
* @property string $daemon_token
* @property string $daemon_base
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
2021-01-30 21:55:13 +01:00
* @property \Pterodactyl\Models\DatabaseHost $databaseHost
*/
class Node extends Model
{
use Notifiable;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
2021-01-23 21:33:34 +01:00
public const RESOURCE_NAME = 'node';
2021-01-23 21:33:34 +01:00
public const DAEMON_TOKEN_ID_LENGTH = 16;
public const DAEMON_TOKEN_LENGTH = 64;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'nodes';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['daemon_token_id', 'daemon_token'];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
'location_id' => 'integer',
'database_host_id' => 'integer',
'listen_port_http' => 'integer',
'listen_port_sftp' => 'integer',
'public_port_http' => 'integer',
'public_port_sftp' => 'integer',
'memory' => 'integer',
'disk' => 'integer',
'behind_proxy' => 'boolean',
'public' => 'boolean',
'maintenance_mode' => 'boolean',
];
2016-01-27 04:17:51 +01:00
2016-01-05 05:59:45 +01:00
/**
* Fields that are mass assignable.
2016-01-05 05:59:45 +01:00
*
* @var array
*/
protected $fillable = [
'public', 'name', 'location_id', 'database_host_id',
'listen_port_http', 'listen_port_sftp', 'public_port_http', 'public_port_sftp',
'fqdn', 'scheme', 'behind_proxy',
'memory', 'memory_overallocate', 'disk',
'disk_overallocate', 'upload_size', 'daemon_base',
'description', 'maintenance_mode',
];
2016-01-05 05:59:45 +01:00
2021-07-17 23:18:05 +02:00
public static array $validationRules = [
'name' => 'required|regex:/^([\w .-]{1,100})$/',
'description' => 'string|nullable',
'location_id' => 'required|exists:locations,id',
2021-02-11 18:32:13 +01:00
'database_host_id' => 'sometimes|nullable|exists:database_hosts,id',
'public' => 'boolean',
'fqdn' => 'required|string',
'listen_port_http' => 'required|numeric|between:1,65535',
'listen_port_sftp' => 'required|numeric|between:1,65535',
'public_port_http' => 'required|numeric|between:1,65535',
'public_port_sftp' => 'required|numeric|between:1,65535',
'scheme' => 'required',
'behind_proxy' => 'boolean',
'memory' => 'required|numeric|min:1',
'memory_overallocate' => 'required|numeric|min:-1',
'disk' => 'required|numeric|min:1',
'disk_overallocate' => 'required|numeric|min:-1',
'daemon_base' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
'maintenance_mode' => 'boolean',
'upload_size' => 'int|between:1,1024',
];
/**
* Default values for specific columns that are generally not changed on base installs.
*
* @var array
*/
protected $attributes = [
'listen_port_http' => 8080,
'listen_port_sftp' => 2022,
'public_port_http' => 8080,
'public_port_sftp' => 2022,
'public' => true,
'behind_proxy' => false,
'memory_overallocate' => 0,
'disk_overallocate' => 0,
'daemon_base' => '/var/lib/pterodactyl/volumes',
'maintenance_mode' => false,
];
/**
* Get the connection address to use when making calls to this node.
*/
public function getConnectionAddress(): string
{
return sprintf('%s://%s:%s', $this->scheme, $this->fqdn, $this->public_port_http);
}
/**
2020-04-10 17:38:20 +02:00
* Returns the configuration as an array.
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function getConfiguration()
2017-01-07 18:39:41 +01:00
{
2020-04-10 17:38:20 +02:00
return [
2019-12-08 20:29:46 +01:00
'debug' => false,
'uuid' => $this->uuid,
'token_id' => $this->daemon_token_id,
'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token),
2019-12-08 20:29:46 +01:00
'api' => [
'host' => '0.0.0.0',
'port' => $this->listen_port_http,
2017-01-07 18:39:41 +01:00
'ssl' => [
2021-01-23 21:33:34 +01:00
'enabled' => (!$this->behind_proxy && $this->scheme === 'https'),
'cert' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/fullchain.pem',
'key' => '/etc/letsencrypt/live/' . Str::lower($this->fqdn) . '/privkey.pem',
2017-01-07 18:39:41 +01:00
],
2019-12-08 20:29:46 +01:00
'upload_limit' => $this->upload_size,
2017-01-07 18:39:41 +01:00
],
2019-12-08 20:29:46 +01:00
'system' => [
'data' => $this->daemon_base,
2019-12-08 20:29:46 +01:00
'sftp' => [
'bind_port' => $this->listen_port_sftp,
],
2019-12-08 20:29:46 +01:00
],
'allowed_mounts' => $this->mounts->pluck('source')->toArray(),
2019-12-08 20:29:46 +01:00
'remote' => route('index'),
2017-01-07 18:39:41 +01:00
];
2020-04-10 17:38:20 +02:00
}
/**
* Returns the configuration in Yaml format.
*
* @return string
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2020-04-10 17:38:20 +02:00
*/
public function getYamlConfiguration()
{
2020-09-23 01:09:28 +02:00
return Yaml::dump($this->getConfiguration(), 4, 2, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
2020-04-10 17:38:20 +02:00
}
/**
2020-04-10 17:38:20 +02:00
* Returns the configuration in JSON format.
*
* @return string
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2020-04-10 17:38:20 +02:00
*/
public function getJsonConfiguration(bool $pretty = false)
{
2020-04-10 17:38:20 +02:00
return json_encode($this->getConfiguration(), $pretty ? JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : JSON_UNESCAPED_SLASHES);
}
/**
* Helper function to return the decrypted key for a node.
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function getDecryptedKey(): string
{
return (string) Container::getInstance()->make(Encrypter::class)->decrypt(
$this->daemon_token
);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function mounts()
{
return $this->hasManyThrough(Mount::class, MountNode::class, 'node_id', 'id', 'id', 'mount_id');
}
/**
* Gets the location associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function location()
{
return $this->belongsTo(Location::class);
}
/**
* Gets the servers associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servers()
{
return $this->hasMany(Server::class);
}
/**
* Gets the allocations associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function allocations()
{
return $this->hasMany(Allocation::class);
}
/**
* Gets the database host associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function databaseHost()
{
return $this->belongsTo(DatabaseHost::class);
}
/**
* Returns a boolean if the node is viable for an additional server to be placed on it.
*/
2020-04-05 00:24:58 +02:00
public function isViable(int $memory, int $disk): bool
{
$memoryLimit = $this->memory * (1 + ($this->memory_overallocate / 100));
$diskLimit = $this->disk * (1 + ($this->disk_overallocate / 100));
return ($this->sum_memory + $memory) <= $memoryLimit && ($this->sum_disk + $disk) <= $diskLimit;
}
2018-04-08 22:36:40 +02:00
}