2015-12-06 19:58:49 +01:00
|
|
|
<?php
|
2016-12-07 23:46:38 +01:00
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2019-12-08 20:29:46 +01:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2020-04-11 00:15:38 +02:00
|
|
|
use Illuminate\Container\Container;
|
2017-01-25 01:15:03 +01:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2020-04-11 00:15:38 +02:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2019-09-06 06:53:33 +02:00
|
|
|
/**
|
2021-01-28 05:52:11 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property string $uuid
|
|
|
|
* @property bool $public
|
|
|
|
* @property string $name
|
|
|
|
* @property string|null $description
|
|
|
|
* @property int $location_id
|
|
|
|
* @property string $fqdn
|
|
|
|
* @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 int $daemonListen
|
|
|
|
* @property int $daemonSFTP
|
|
|
|
* @property string $daemonBase
|
|
|
|
* @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
|
2019-12-28 21:03:19 +01:00
|
|
|
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
|
2019-09-06 06:53:33 +02:00
|
|
|
*/
|
2020-04-04 08:22:35 +02:00
|
|
|
class Node extends Model
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2020-04-04 08:22:35 +02:00
|
|
|
use Notifiable;
|
2017-01-25 01:15:03 +01:00
|
|
|
|
2018-01-26 04:26:06 +01:00
|
|
|
/**
|
|
|
|
* 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';
|
2018-01-26 04:26:06 +01:00
|
|
|
|
2021-01-23 21:33:34 +01:00
|
|
|
public const DAEMON_TOKEN_ID_LENGTH = 16;
|
|
|
|
public const DAEMON_TOKEN_LENGTH = 64;
|
2017-10-27 06:49:54 +02:00
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'nodes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-11 00:15:38 +02:00
|
|
|
protected $hidden = ['daemon_token_id', 'daemon_token'];
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2017-08-06 00:20:07 +02:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'location_id' => 'integer',
|
|
|
|
'memory' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'daemonListen' => 'integer',
|
|
|
|
'daemonSFTP' => 'integer',
|
|
|
|
'behind_proxy' => 'boolean',
|
2018-03-26 20:56:58 +02:00
|
|
|
'public' => 'boolean',
|
2018-05-31 16:34:35 +02:00
|
|
|
'maintenance_mode' => 'boolean',
|
2017-08-06 00:20:07 +02:00
|
|
|
];
|
2016-01-27 04:17:51 +01:00
|
|
|
|
2016-01-05 05:59:45 +01:00
|
|
|
/**
|
2017-02-03 21:19:14 +01:00
|
|
|
* Fields that are mass assignable.
|
2016-01-05 05:59:45 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-02-03 21:19:14 +01:00
|
|
|
protected $fillable = [
|
2017-02-11 02:26:38 +01:00
|
|
|
'public', 'name', 'location_id',
|
2017-04-28 04:52:37 +02:00
|
|
|
'fqdn', 'scheme', 'behind_proxy',
|
|
|
|
'memory', 'memory_overallocate', 'disk',
|
2020-04-11 00:15:38 +02:00
|
|
|
'disk_overallocate', 'upload_size', 'daemonBase',
|
2017-02-11 02:26:38 +01:00
|
|
|
'daemonSFTP', 'daemonListen',
|
2018-05-31 16:34:35 +02:00
|
|
|
'description', 'maintenance_mode',
|
2017-02-03 21:19:14 +01:00
|
|
|
];
|
2016-01-05 05:59:45 +01:00
|
|
|
|
2017-08-06 00:20:07 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 07:19:57 +02:00
|
|
|
public static $validationRules = [
|
|
|
|
'name' => 'required|regex:/^([\w .-]{1,100})$/',
|
2020-04-18 02:52:40 +02:00
|
|
|
'description' => 'string|nullable',
|
2019-09-05 07:19:57 +02:00
|
|
|
'location_id' => 'required|exists:locations,id',
|
2017-08-06 00:20:07 +02:00
|
|
|
'public' => 'boolean',
|
2019-09-05 07:19:57 +02:00
|
|
|
'fqdn' => 'required|string',
|
|
|
|
'scheme' => 'required',
|
2017-08-06 00:20:07 +02:00
|
|
|
'behind_proxy' => 'boolean',
|
2019-09-05 07:19:57 +02:00
|
|
|
'memory' => 'required|numeric|min:1',
|
|
|
|
'memory_overallocate' => 'required|numeric|min:-1',
|
|
|
|
'disk' => 'required|numeric|min:1',
|
|
|
|
'disk_overallocate' => 'required|numeric|min:-1',
|
|
|
|
'daemonBase' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
|
|
|
|
'daemonSFTP' => 'required|numeric|between:1,65535',
|
|
|
|
'daemonListen' => 'required|numeric|between:1,65535',
|
2018-05-31 16:34:35 +02:00
|
|
|
'maintenance_mode' => 'boolean',
|
2018-09-03 23:53:58 +02:00
|
|
|
'upload_size' => 'int|between:1,1024',
|
2017-08-06 00:20:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default values for specific columns that are generally not changed on base installs.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'public' => true,
|
|
|
|
'behind_proxy' => false,
|
|
|
|
'memory_overallocate' => 0,
|
|
|
|
'disk_overallocate' => 0,
|
2020-05-10 00:44:13 +02:00
|
|
|
'daemonBase' => '/var/lib/pterodactyl/volumes',
|
2017-08-06 00:20:07 +02:00
|
|
|
'daemonSFTP' => 2022,
|
|
|
|
'daemonListen' => 8080,
|
2018-05-31 16:34:35 +02:00
|
|
|
'maintenance_mode' => false,
|
2017-08-06 00:20:07 +02:00
|
|
|
];
|
2017-03-03 23:30:39 +01:00
|
|
|
|
2018-07-21 08:45:07 +02:00
|
|
|
/**
|
|
|
|
* 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->daemonListen);
|
|
|
|
}
|
|
|
|
|
2017-01-07 18:06:09 +01:00
|
|
|
/**
|
2020-04-10 17:38:20 +02:00
|
|
|
* Returns the configuration as an array.
|
2017-01-07 18:06:09 +01:00
|
|
|
*
|
2020-04-11 00:15:38 +02:00
|
|
|
* @return array
|
2017-01-07 18:06:09 +01:00
|
|
|
*/
|
2020-04-11 00:15:38 +02:00
|
|
|
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,
|
2020-04-11 00:15:38 +02:00
|
|
|
'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' => [
|
2017-01-07 18:06:09 +01:00
|
|
|
'host' => '0.0.0.0',
|
2019-12-08 20:29:46 +01:00
|
|
|
'port' => $this->daemonListen,
|
2017-01-07 18:39:41 +01:00
|
|
|
'ssl' => [
|
2021-01-23 21:33:34 +01:00
|
|
|
'enabled' => (!$this->behind_proxy && $this->scheme === 'https'),
|
2019-12-25 01:48:50 +01:00
|
|
|
'cert' => '/etc/letsencrypt/live/' . $this->fqdn . '/fullchain.pem',
|
2017-01-23 01:14:55 +01:00
|
|
|
'key' => '/etc/letsencrypt/live/' . $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->daemonBase,
|
|
|
|
'sftp' => [
|
|
|
|
'bind_port' => $this->daemonSFTP,
|
2017-10-25 06:35:25 +02:00
|
|
|
],
|
2019-12-08 20:29:46 +01:00
|
|
|
],
|
2020-09-13 18:59:54 +02: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
|
|
|
|
*/
|
2020-04-11 00:15:38 +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
|
|
|
}
|
2017-01-07 18:06:09 +01:00
|
|
|
|
2020-04-11 00:15:38 +02:00
|
|
|
/**
|
2020-04-10 17:38:20 +02:00
|
|
|
* Returns the configuration in JSON format.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-04-11 00:15:38 +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);
|
2017-01-07 18:06:09 +01:00
|
|
|
}
|
2017-02-03 21:19:14 +01:00
|
|
|
|
2020-04-11 00:15:38 +02:00
|
|
|
/**
|
|
|
|
* Helper function to return the decrypted key for a node.
|
|
|
|
*/
|
|
|
|
public function getDecryptedKey(): string
|
|
|
|
{
|
2021-01-23 21:09:16 +01:00
|
|
|
return (string) Container::getInstance()->make(Encrypter::class)->decrypt(
|
2020-04-11 00:15:38 +02:00
|
|
|
$this->daemon_token
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-13 18:59:54 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
|
|
|
*/
|
|
|
|
public function mounts()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Mount::class, MountNode::class, 'node_id', 'id', 'id', 'mount_id');
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:19:14 +01:00
|
|
|
/**
|
|
|
|
* Gets the location associated with a node.
|
|
|
|
*
|
2017-02-06 00:00:39 +01:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 21:19:14 +01:00
|
|
|
*/
|
|
|
|
public function location()
|
|
|
|
{
|
2017-02-06 00:00:39 +01:00
|
|
|
return $this->belongsTo(Location::class);
|
2017-02-03 21:19:14 +01:00
|
|
|
}
|
2017-02-03 22:50:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2020-04-04 05:45:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2020-04-04 05:45:37 +02:00
|
|
|
$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
|
|
|
}
|