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
|
2016-01-20 00:10:39 +00:00
|
|
|
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
|
|
|
|
*
|
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
|
|
|
*/
|
2015-12-06 18:58:49 +00:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
|
|
use Auth;
|
2015-12-14 03:22:16 +00:00
|
|
|
use Pterodactyl\Models\Subuser;
|
2015-12-06 18:58:49 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-10-28 00:05:29 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2016-10-20 21:04:58 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-10-20 20:42:54 +00:00
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
class Server extends Model
|
|
|
|
{
|
|
|
|
|
2016-10-28 00:05:29 +00:00
|
|
|
use SoftDeletes;
|
|
|
|
|
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
|
|
|
|
2016-01-27 03:17:51 +00:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'node' => 'integer',
|
2016-09-02 01:21:01 +00:00
|
|
|
'suspended' => 'integer',
|
2016-01-27 03:17:51 +00:00
|
|
|
'owner' => 'integer',
|
|
|
|
'memory' => 'integer',
|
|
|
|
'swap' => 'integer',
|
|
|
|
'disk' => 'integer',
|
|
|
|
'io' => 'integer',
|
|
|
|
'cpu' => 'integer',
|
|
|
|
'oom_disabled' => 'integer',
|
|
|
|
'port' => 'integer',
|
|
|
|
'service' => 'integer',
|
|
|
|
'option' => 'integer',
|
|
|
|
'installed' => 'integer',
|
|
|
|
];
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $serverUUIDInstance = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected static $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-10-28 00:05:29 +00:00
|
|
|
parent::__construct();
|
2016-10-20 20:48:37 +00:00
|
|
|
self::$user = Auth::user();
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if we need to change the server's daemonSecret value to
|
|
|
|
* match that of the user if they are a subuser.
|
|
|
|
*
|
|
|
|
* @param Illuminate\Database\Eloquent\Model\Server $server
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-20 20:42:54 +00:00
|
|
|
public static function getUserDaemonSecret(Server $server)
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (self::$user->id === $server->owner || self::$user->root_admin === 1) {
|
|
|
|
return $server->daemonSecret;
|
|
|
|
}
|
|
|
|
|
2015-12-14 03:22:16 +00:00
|
|
|
$subuser = Subuser::where('server_id', $server->id)->where('user_id', self::$user->id)->first();
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
if (is_null($subuser)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subuser->daemonSecret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns array of all servers owned by the logged in user.
|
2016-09-02 01:21:01 +00:00
|
|
|
* Returns all users servers if user is a root admin.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
2016-01-05 06:15:23 +00:00
|
|
|
public static function getUserServers($paginate = null)
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
|
|
|
|
2016-08-31 20:03:37 +00:00
|
|
|
$query = self::select(
|
|
|
|
'servers.*',
|
|
|
|
'nodes.name as nodeName',
|
|
|
|
'locations.short as a_locationShort',
|
2016-09-17 18:51:27 +00:00
|
|
|
'allocations.ip',
|
2016-08-31 20:03:37 +00:00
|
|
|
'allocations.ip_alias',
|
2016-10-15 00:22:23 +00:00
|
|
|
'allocations.port',
|
|
|
|
'services.name as a_serviceName',
|
|
|
|
'service_options.name as a_serviceOptionName'
|
2016-08-31 20:03:37 +00:00
|
|
|
)->join('nodes', 'servers.node', '=', 'nodes.id')
|
|
|
|
->join('locations', 'nodes.location', '=', 'locations.id')
|
2016-10-15 00:22:23 +00:00
|
|
|
->join('services', 'servers.service', '=', 'services.id')
|
|
|
|
->join('service_options', 'servers.option', '=', 'service_options.id')
|
2016-09-02 01:21:01 +00:00
|
|
|
->join('allocations', 'servers.allocation', '=', 'allocations.id');
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
if (self::$user->root_admin !== 1) {
|
2015-12-14 03:22:16 +00:00
|
|
|
$query->whereIn('servers.id', Subuser::accessServers());
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 06:15:23 +00:00
|
|
|
if (is_numeric($paginate)) {
|
|
|
|
return $query->paginate($paginate);
|
|
|
|
}
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
return $query->get();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @param string $uuid The Short-UUID of the server to return an object about.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
public static function getByUUID($uuid)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (array_key_exists($uuid, self::$serverUUIDInstance)) {
|
|
|
|
return self::$serverUUIDInstance[$uuid];
|
|
|
|
}
|
|
|
|
|
2016-01-09 00:54:06 +00:00
|
|
|
$query = self::select('servers.*', 'services.file as a_serviceFile')
|
2016-01-09 01:39:16 +00:00
|
|
|
->join('services', 'services.id', '=', 'servers.service')
|
2016-10-20 20:42:54 +00:00
|
|
|
->where('uuidShort', $uuid)
|
|
|
|
->orWhere('uuid', $uuid);
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
if (self::$user->root_admin !== 1) {
|
2015-12-14 03:22:16 +00:00
|
|
|
$query->whereIn('servers.id', Subuser::accessServers());
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->first();
|
|
|
|
|
|
|
|
if(!is_null($result)) {
|
|
|
|
$result->daemonSecret = self::getUserDaemonSecret($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$serverUUIDInstance[$uuid] = $result;
|
|
|
|
return self::$serverUUIDInstance[$uuid];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-02 23:45:08 +00:00
|
|
|
* Returns non-administrative headers for accessing a server on the daemon
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
|
|
|
* @param string $uuid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getGuzzleHeaders($uuid)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (array_key_exists($uuid, self::$serverUUIDInstance)) {
|
|
|
|
return [
|
|
|
|
'X-Access-Server' => self::$serverUUIDInstance[$uuid]->uuid,
|
|
|
|
'X-Access-Token' => self::$serverUUIDInstance[$uuid]->daemonSecret
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|