2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
2016-01-20 00:10:39 +00:00
|
|
|
/**
|
2016-01-20 21:05:16 +00:00
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2016-01-20 00:10:39 +00:00
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
|
|
use Auth;
|
2017-02-17 23:19:53 +00:00
|
|
|
use Cache;
|
2017-03-05 00:24:46 +00:00
|
|
|
use Carbon;
|
2017-04-09 17:15:15 +00:00
|
|
|
use Schema;
|
2017-02-03 00:41:38 +00:00
|
|
|
use Javascript;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Sofa\Eloquence\Eloquence;
|
|
|
|
use Sofa\Eloquence\Validable;
|
2015-12-06 18:58:49 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-01-25 00:15:03 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
class Server extends Model implements ValidableContract
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2017-07-20 01:49:41 +00:00
|
|
|
use Eloquence, Notifiable, Validable;
|
2016-10-28 00:05:29 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'servers';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-10-28 00:05:29 +00:00
|
|
|
protected $hidden = ['daemonSecret', 'sftp_password'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = ['deleted_at'];
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2015-12-15 20:08:41 +00:00
|
|
|
/**
|
|
|
|
* Fields that are not mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-10-28 00:05:29 +00:00
|
|
|
protected $guarded = ['id', 'installed', 'created_at', 'updated_at', 'deleted_at'];
|
2015-12-15 20:08:41 +00:00
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
protected static $applicationRules = [
|
|
|
|
'owner_id' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'memory' => 'required',
|
|
|
|
'swap' => 'required',
|
|
|
|
'io' => 'required',
|
|
|
|
'cpu' => 'required',
|
|
|
|
'disk' => 'required',
|
|
|
|
'service_id' => 'required',
|
|
|
|
'option_id' => 'required',
|
|
|
|
'pack_id' => 'sometimes',
|
|
|
|
'auto_deploy' => 'sometimes',
|
|
|
|
'custom_id' => 'sometimes',
|
|
|
|
'skip_scripts' => 'sometimes',
|
|
|
|
];
|
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
protected static $dataIntegrityRules = [
|
|
|
|
'owner_id' => 'exists:users,id',
|
|
|
|
'name' => 'regex:/^([\w .-]{1,200})$/',
|
|
|
|
'node_id' => 'exists:nodes,id',
|
|
|
|
'description' => 'nullable|string',
|
|
|
|
'memory' => 'numeric|min:0',
|
|
|
|
'swap' => 'numeric|min:-1',
|
|
|
|
'io' => 'numeric|between:10,1000',
|
|
|
|
'cpu' => 'numeric|min:0',
|
|
|
|
'disk' => 'numeric|min:0',
|
|
|
|
'allocation_id' => 'exists:allocations,id',
|
|
|
|
'service_id' => 'exists:services,id',
|
|
|
|
'option_id' => 'exists:service_options,id',
|
|
|
|
'pack_id' => 'nullable|numeric|min:0',
|
|
|
|
'custom_container' => 'nullable|string',
|
|
|
|
'startup' => 'nullable|string',
|
|
|
|
'auto_deploy' => 'accepted',
|
|
|
|
'custom_id' => 'numeric|unique:servers,id',
|
|
|
|
'skip_scripts' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'node_id' => 'integer',
|
2017-04-21 21:52:44 +00:00
|
|
|
'skip_scripts' => 'boolean',
|
2017-03-15 01:18:36 +00:00
|
|
|
'suspended' => 'integer',
|
|
|
|
'owner_id' => 'integer',
|
|
|
|
'memory' => 'integer',
|
|
|
|
'swap' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'io' => 'integer',
|
|
|
|
'cpu' => 'integer',
|
|
|
|
'oom_disabled' => 'integer',
|
|
|
|
'allocation_id' => 'integer',
|
|
|
|
'service_id' => 'integer',
|
|
|
|
'option_id' => 'integer',
|
|
|
|
'pack_id' => 'integer',
|
|
|
|
'installed' => 'integer',
|
|
|
|
];
|
2016-01-27 03:17:51 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
/**
|
|
|
|
* Parameters for search querying.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-05 22:20:07 +00:00
|
|
|
protected $searchableColumns = [
|
|
|
|
'name' => 10,
|
|
|
|
'username' => 10,
|
|
|
|
'uuidShort' => 9,
|
|
|
|
'uuid' => 8,
|
|
|
|
'pack.name' => 7,
|
|
|
|
'user.email' => 6,
|
|
|
|
'user.username' => 6,
|
|
|
|
'node.name' => 2,
|
2017-03-15 01:18:36 +00:00
|
|
|
];
|
2017-02-19 00:31:44 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
2016-01-23 02:43:50 +00:00
|
|
|
* Returns a single server specified by UUID.
|
|
|
|
* DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
|
|
|
|
* YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
2017-08-05 22:20:07 +00:00
|
|
|
* @param string $uuid
|
|
|
|
* @param array $with
|
|
|
|
* @param array $withCount
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Pterodactyl\Models\Server
|
2017-08-05 22:20:07 +00:00
|
|
|
* @throws \Exception
|
2017-03-19 23:36:50 +00:00
|
|
|
* @todo Remove $with and $withCount due to cache issues, they aren't used anyways.
|
2015-12-06 18:58:49 +00:00
|
|
|
*/
|
2017-03-12 23:34:06 +00:00
|
|
|
public static function byUuid($uuid, array $with = [], array $withCount = [])
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2017-03-05 00:27:24 +00:00
|
|
|
if (! Auth::check()) {
|
|
|
|
throw new \Exception('You must call Server:byUuid as an authenticated user.');
|
|
|
|
}
|
|
|
|
|
2017-02-17 23:19:53 +00:00
|
|
|
// Results are cached because we call this functions a few times on page load.
|
2017-03-05 00:24:46 +00:00
|
|
|
$result = Cache::tags(['Model:Server', 'Model:Server:byUuid:' . $uuid])->remember('Model:Server:byUuid:' . $uuid . Auth::user()->uuid, Carbon::now()->addMinutes(15), function () use ($uuid) {
|
2017-02-17 23:19:53 +00:00
|
|
|
$query = self::with('service', 'node')->where(function ($q) use ($uuid) {
|
|
|
|
$q->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
|
|
|
|
});
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-02-17 23:19:53 +00:00
|
|
|
if (! Auth::user()->isRootAdmin()) {
|
|
|
|
$query->whereIn('id', Auth::user()->serverAccessArray());
|
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-02-17 23:19:53 +00:00
|
|
|
return $query->first();
|
|
|
|
});
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
if (! is_null($result)) {
|
2017-02-02 23:21:36 +00:00
|
|
|
$result->daemonSecret = Auth::user()->daemonToken($result);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 23:21:36 +00:00
|
|
|
return $result;
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-07 22:46:38 +00:00
|
|
|
* Returns non-administrative headers for accessing a server on the daemon.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
2017-04-15 16:48:47 +00:00
|
|
|
* @param Pterodactyl\Models\User|null $user
|
2015-12-06 18:58:49 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-15 16:48:47 +00:00
|
|
|
public function guzzleHeaders(User $user = null)
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2017-04-15 16:48:47 +00:00
|
|
|
// If no specific user is passed, see if we can find an active
|
|
|
|
// auth session to pull data from.
|
|
|
|
if (is_null($user) && Auth::check()) {
|
|
|
|
$user = Auth::user();
|
|
|
|
}
|
|
|
|
|
2017-02-02 23:21:36 +00:00
|
|
|
return [
|
|
|
|
'X-Access-Server' => $this->uuid,
|
2017-04-15 16:48:47 +00:00
|
|
|
'X-Access-Token' => ($user) ? $user->daemonToken($this) : $this->daemonSecret,
|
2017-02-02 23:21:36 +00:00
|
|
|
];
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
2017-02-02 21:24:08 +00:00
|
|
|
|
2017-02-03 00:41:38 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the Guzzle client for this specific server using defined access token.
|
|
|
|
*
|
2017-04-15 16:48:47 +00:00
|
|
|
* @param Pterodactyl\Models\User|null $user
|
2017-02-03 00:41:38 +00:00
|
|
|
* @return \GuzzleHttp\Client
|
|
|
|
*/
|
2017-04-15 16:48:47 +00:00
|
|
|
public function guzzleClient(User $user = null)
|
2017-02-03 00:41:38 +00:00
|
|
|
{
|
2017-04-15 16:48:47 +00:00
|
|
|
return $this->node->guzzleClient($this->guzzleHeaders($user));
|
2017-02-03 00:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns javascript object to be embedded on server view pages with relevant information.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param array|null $additional
|
|
|
|
* @param array|null $overwrite
|
2017-02-03 00:41:38 +00:00
|
|
|
* @return \Laracasts\Utilities\JavaScript\JavaScriptFacade
|
|
|
|
*/
|
|
|
|
public function js($additional = null, $overwrite = null)
|
|
|
|
{
|
|
|
|
$response = [
|
|
|
|
'server' => collect($this->makeVisible('daemonSecret'))->only([
|
|
|
|
'uuid',
|
|
|
|
'uuidShort',
|
|
|
|
'daemonSecret',
|
2017-02-03 00:43:55 +00:00
|
|
|
'username',
|
2017-02-03 00:41:38 +00:00
|
|
|
]),
|
|
|
|
'node' => collect($this->node)->only([
|
|
|
|
'fqdn',
|
|
|
|
'scheme',
|
2017-02-03 00:43:55 +00:00
|
|
|
'daemonListen',
|
2017-02-03 00:41:38 +00:00
|
|
|
]),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (is_array($additional)) {
|
|
|
|
$response = array_merge($response, $additional);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($overwrite)) {
|
|
|
|
$response = $overwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Javascript::put($response);
|
|
|
|
}
|
|
|
|
|
2017-04-09 17:15:15 +00:00
|
|
|
/**
|
|
|
|
* Return the columns available for this table.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-09 23:16:39 +00:00
|
|
|
public function getTableColumns()
|
|
|
|
{
|
2017-04-09 17:15:15 +00:00
|
|
|
return Schema::getColumnListing($this->getTable());
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:50:28 +00:00
|
|
|
/**
|
|
|
|
* Gets the user who owns the server.
|
|
|
|
*
|
2017-02-05 23:00:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-03 21:50:28 +00:00
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2017-02-05 23:00:39 +00:00
|
|
|
return $this->belongsTo(User::class, 'owner_id');
|
2017-02-03 21:50:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 00:38:54 +00:00
|
|
|
/**
|
|
|
|
* Gets the subusers associated with a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function subusers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Subuser::class);
|
|
|
|
}
|
|
|
|
|
2017-02-06 00:19:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the default allocation for a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function allocation()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Allocation::class, 'id', 'allocation_id');
|
|
|
|
}
|
|
|
|
|
2017-02-02 21:24:08 +00:00
|
|
|
/**
|
|
|
|
* Gets all allocations associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function allocations()
|
|
|
|
{
|
2017-02-03 21:50:28 +00:00
|
|
|
return $this->hasMany(Allocation::class, 'server_id');
|
2017-02-02 21:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the pack associated with this server.
|
|
|
|
*
|
2017-03-15 01:18:36 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 21:24:08 +00:00
|
|
|
*/
|
|
|
|
public function pack()
|
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
return $this->belongsTo(Pack::class);
|
2017-02-02 21:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service associated with this server.
|
|
|
|
*
|
2017-02-05 23:00:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 21:24:08 +00:00
|
|
|
*/
|
|
|
|
public function service()
|
|
|
|
{
|
2017-02-05 23:00:39 +00:00
|
|
|
return $this->belongsTo(Service::class);
|
2017-02-02 21:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service option associated with this server.
|
|
|
|
*
|
2017-02-05 23:00:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 21:24:08 +00:00
|
|
|
*/
|
|
|
|
public function option()
|
|
|
|
{
|
2017-02-12 21:02:23 +00:00
|
|
|
return $this->belongsTo(ServiceOption::class);
|
2017-02-02 21:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the service variables associated with this server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function variables()
|
|
|
|
{
|
2017-02-12 21:03:17 +00:00
|
|
|
return $this->hasMany(ServerVariable::class);
|
2017-02-02 21:24:08 +00:00
|
|
|
}
|
2017-02-02 23:21:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the node associated with this server.
|
|
|
|
*
|
2017-02-05 23:00:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2017-02-02 23:21:36 +00:00
|
|
|
*/
|
|
|
|
public function node()
|
|
|
|
{
|
2017-02-05 23:00:39 +00:00
|
|
|
return $this->belongsTo(Node::class);
|
2017-02-02 23:21:36 +00:00
|
|
|
}
|
2017-02-03 00:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets information for the tasks associated with this server.
|
|
|
|
*
|
|
|
|
* @TODO adjust server column in tasks to be server_id
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function tasks()
|
|
|
|
{
|
2017-04-15 17:52:43 +00:00
|
|
|
return $this->hasMany(Task::class);
|
2017-02-03 00:41:38 +00:00
|
|
|
}
|
2017-02-09 22:43:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all databases associated with a server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function databases()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Database::class);
|
|
|
|
}
|
2017-04-08 00:28:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the location of the server.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function location()
|
|
|
|
{
|
|
|
|
return $this->node->location();
|
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|