2017-04-02 20:51:56 +00:00
< ? php
/**
* Pterodactyl - Panel
* Copyright ( c ) 2015 - 2017 Dane Everitt < dane @ daneeveritt . com >.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license .
* https :// opensource . org / licenses / MIT
2017-04-02 20:51:56 +00:00
*/
namespace Pterodactyl\Http\Controllers\API\Admin ;
2017-05-24 03:56:54 +00:00
use Log ;
2017-04-02 20:51:56 +00:00
use Fractal ;
use Illuminate\Http\Request ;
use Pterodactyl\Models\Server ;
2017-04-09 17:15:15 +00:00
use GuzzleHttp\Exception\TransferException ;
use Pterodactyl\Exceptions\DisplayException ;
2017-04-02 20:51:56 +00:00
use Pterodactyl\Http\Controllers\Controller ;
2017-04-09 17:15:15 +00:00
use Pterodactyl\Repositories\ServerRepository ;
2017-04-02 20:51:56 +00:00
use Pterodactyl\Transformers\Admin\ServerTransformer ;
2017-04-09 17:15:15 +00:00
use Pterodactyl\Exceptions\DisplayValidationException ;
2017-04-02 20:51:56 +00:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter ;
class ServerController extends Controller
{
/**
* Controller to handle returning all servers on the system .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
2017-04-02 20:51:56 +00:00
* @ return array
*/
public function index ( Request $request )
{
2017-04-09 17:15:15 +00:00
$this -> authorize ( 'server-list' , $request -> apiKey ());
2017-04-09 20:04:08 +00:00
$servers = Server :: paginate ( config ( 'pterodactyl.paginate.api.servers' ));
$fractal = Fractal :: create () -> collection ( $servers )
2017-04-08 16:05:29 +00:00
-> transformWith ( new ServerTransformer ( $request ))
2017-04-09 20:04:08 +00:00
-> withResourceName ( 'user' )
-> paginateWith ( new IlluminatePaginatorAdapter ( $servers ));
2017-04-09 20:05:18 +00:00
if ( config ( 'pterodactyl.api.include_on_list' ) && $request -> input ( 'include' )) {
2017-04-09 20:04:08 +00:00
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
}
return $fractal -> toArray ();
2017-04-02 20:51:56 +00:00
}
/**
* Controller to handle returning information on a single server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-02 20:51:56 +00:00
* @ return array
*/
public function view ( Request $request , $id )
{
2017-04-09 17:15:15 +00:00
$this -> authorize ( 'server-view' , $request -> apiKey ());
2017-04-02 20:51:56 +00:00
$server = Server :: findOrFail ( $id );
$fractal = Fractal :: create () -> item ( $server );
if ( $request -> input ( 'include' )) {
2017-04-08 00:28:58 +00:00
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
2017-04-02 20:51:56 +00:00
}
2017-04-08 16:05:29 +00:00
return $fractal -> transformWith ( new ServerTransformer ( $request ))
2017-04-02 20:51:56 +00:00
-> withResourceName ( 'server' )
-> toArray ();
}
2017-04-09 17:15:15 +00:00
/**
* Create a new server on the system .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\JsonResponse | array
*/
public function store ( Request $request )
{
$this -> authorize ( 'server-create' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$server = $repo -> create ( $request -> all ());
$fractal = Fractal :: create () -> item ( $server ) -> transformWith ( new ServerTransformer ( $request ));
if ( $request -> input ( 'include' )) {
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
}
return $fractal -> withResourceName ( 'server' ) -> toArray ();
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
'error' => json_decode ( $ex -> getMessage ()),
], 400 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to add this server. Please try again.' ,
], 500 );
}
}
/**
* Delete a server from the system .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\Response | \Illuminate\Http\JsonResponse
*/
public function delete ( Request $request , $id )
{
$this -> authorize ( 'server-delete' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$repo -> delete ( $id , $request -> has ( 'force_delete' ));
return response ( '' , 204 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to add this server. Please try again.' ,
], 500 );
}
}
2017-04-09 23:16:39 +00:00
/**
2017-04-09 17:15:15 +00:00
* Update the details for a server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\JsonResponse | array
*/
public function details ( Request $request , $id )
{
$this -> authorize ( 'server-edit-details' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$server = $repo -> updateDetails ( $id , $request -> intersect ([
'owner_id' , 'name' , 'description' , 'reset_token' ,
]));
$fractal = Fractal :: create () -> item ( $server ) -> transformWith ( new ServerTransformer ( $request ));
if ( $request -> input ( 'include' )) {
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
}
return $fractal -> withResourceName ( 'server' ) -> toArray ();
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
'error' => json_decode ( $ex -> getMessage ()),
], 400 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to modify this server. Please try again.' ,
], 500 );
}
}
/**
* Set the new docker container for a server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\RedirectResponse | array
*/
public function container ( Request $request , $id )
{
$this -> authorize ( 'server-edit-container' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$server = $repo -> updateContainer ( $id , $request -> intersect ( 'docker_image' ));
$fractal = Fractal :: create () -> item ( $server ) -> transformWith ( new ServerTransformer ( $request ));
if ( $request -> input ( 'include' )) {
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
}
return $fractal -> withResourceName ( 'server' ) -> toArray ();
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
'error' => json_decode ( $ex -> getMessage ()),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to modify this server container. Please try again.' ,
], 500 );
}
}
/**
* Toggles the install status for a server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\Response | \Illuminate\Http\JsonResponse
*/
public function install ( Request $request , $id )
{
$this -> authorize ( 'server-install' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$repo -> toggleInstall ( $id );
return response ( '' , 204 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to toggle the install status for this server. Please try again.' ,
], 500 );
}
}
/**
* Setup a server to have a container rebuild .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\Response | \Illuminate\Http\JsonResponse
*/
public function rebuild ( Request $request , $id )
{
$this -> authorize ( 'server-rebuild' , $request -> apiKey ());
$server = Server :: with ( 'node' ) -> findOrFail ( $id );
try {
$server -> node -> guzzleClient ([
'X-Access-Server' => $server -> uuid ,
'X-Access-Token' => $server -> node -> daemonSecret ,
]) -> request ( 'POST' , '/server/rebuild' );
return response ( '' , 204 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
}
}
/**
* Manage the suspension status for a server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\Response | \Illuminate\Http\JsonResponse
*/
public function suspend ( Request $request , $id )
{
$this -> authorize ( 'server-suspend' , $request -> apiKey ());
$repo = new ServerRepository ;
$action = $request -> input ( 'action' );
if ( ! in_array ( $action , [ 'suspend' , 'unsuspend' ])) {
return response () -> json ([
'error' => 'The action provided was invalid. Action should be one of: suspend, unsuspend.' ,
], 400 );
}
try {
2017-04-18 00:16:05 +00:00
$repo -> toggleAccess ( $id , ( $action === 'unsuspend' ));
2017-04-09 17:15:15 +00:00
return response ( '' , 204 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to ' . $action . ' this server. Please try again.' ,
], 500 );
}
}
/**
* Update the build configuration for a server .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\JsonResponse | array
*/
public function build ( Request $request , $id )
{
$this -> authorize ( 'server-edit-build' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$server = $repo -> changeBuild ( $id , $request -> intersect ([
'allocation_id' , 'add_allocations' , 'remove_allocations' ,
'memory' , 'swap' , 'io' , 'cpu' ,
]));
$fractal = Fractal :: create () -> item ( $server ) -> transformWith ( new ServerTransformer ( $request ));
if ( $request -> input ( 'include' )) {
$fractal -> parseIncludes ( explode ( ',' , $request -> input ( 'include' )));
}
return $fractal -> withResourceName ( 'server' ) -> toArray ();
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
'error' => json_decode ( $ex -> getMessage ()),
], 400 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to modify the build settings for this server. Please try again.' ,
], 500 );
}
}
/**
* Update the startup command as well as variables .
*
2017-08-22 03:10:48 +00:00
* @ param \Illuminate\Http\Request $request
* @ param int $id
2017-04-09 17:15:15 +00:00
* @ return \Illuminate\Http\Response | \Illuminate\Http\JsonResponse
*/
public function startup ( Request $request , $id )
{
$this -> authorize ( 'server-edit-startup' , $request -> apiKey ());
$repo = new ServerRepository ;
try {
$repo -> updateStartup ( $id , $request -> all (), true );
return response ( '' , 204 );
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
'error' => json_decode ( $ex -> getMessage ()),
], 400 );
} catch ( DisplayException $ex ) {
return response () -> json ([
'error' => $ex -> getMessage (),
], 400 );
} catch ( TransferException $ex ) {
Log :: warning ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.' ,
], 504 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2017-04-09 23:16:39 +00:00
2017-04-09 17:15:15 +00:00
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to modify the startup settings for this server. Please try again.' ,
], 500 );
}
}
2017-04-02 20:51:56 +00:00
}