2016-01-05 04:59:45 +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
2016-01-05 04:59:45 +00:00
namespace Pterodactyl\Repositories ;
2016-01-10 05:38:16 +00:00
use DB ;
2016-01-05 04:59:45 +00:00
use Validator ;
2016-12-07 22:46:38 +00:00
use IPTools\Network ;
2016-01-05 04:59:45 +00:00
use Pterodactyl\Models ;
use Pterodactyl\Services\UuidService ;
use Pterodactyl\Exceptions\DisplayException ;
use Pterodactyl\Exceptions\DisplayValidationException ;
2016-12-07 22:46:38 +00:00
class NodeRepository
{
2016-01-05 04:59:45 +00:00
public function __construct ()
{
//
}
public function create ( array $data )
{
// Validate Fields
$validator = Validator :: make ( $data , [
'name' => 'required|regex:/^([\w .-]{1,100})$/' ,
2017-02-11 01:26:38 +00:00
'location_id' => 'required|numeric|min:1|exists:locations,id' ,
2016-01-05 04:59:45 +00:00
'public' => 'required|numeric|between:0,1' ,
'fqdn' => 'required|string|unique:nodes,fqdn' ,
'scheme' => 'required|regex:/^(http(s)?)$/' ,
'memory' => 'required|numeric|min:1' ,
'memory_overallocate' => 'required|numeric|min:-1' ,
'disk' => 'required|numeric|min:1' ,
'disk_overallocate' => 'required|numeric|min:-1' ,
'daemonBase' => 'required|regex:/^([\/][\d\w.\-\/]+)$/' ,
'daemonSFTP' => 'required|numeric|between:1,65535' ,
2016-01-05 23:31:25 +00:00
'daemonListen' => 'required|numeric|between:1,65535' ,
2016-01-05 04:59:45 +00:00
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ( $validator -> fails ()) {
throw new DisplayValidationException ( $validator -> errors ());
}
2016-08-16 22:45:22 +00:00
// Verify the FQDN if using SSL
if ( filter_var ( $data [ 'fqdn' ], FILTER_VALIDATE_IP ) && $data [ 'scheme' ] === 'https' ) {
2017-02-03 21:50:28 +00:00
throw new DisplayException ( 'A fully qualified domain name is required to use a secure comunication method on this node.' );
2016-01-05 23:31:25 +00:00
}
2016-08-16 22:45:22 +00:00
// Verify FQDN is resolvable, or if not using SSL that the IP is valid.
2016-12-07 22:46:38 +00:00
if ( ! filter_var ( gethostbyname ( $data [ 'fqdn' ]), FILTER_VALIDATE_IP )) {
2016-08-16 22:45:22 +00:00
throw new DisplayException ( 'The FQDN (or IP Address) provided does not resolve to a valid IP address.' );
2016-01-05 04:59:45 +00:00
}
// Should we be nulling the overallocations?
$data [ 'memory_overallocate' ] = ( $data [ 'memory_overallocate' ] < 0 ) ? null : $data [ 'memory_overallocate' ];
$data [ 'disk_overallocate' ] = ( $data [ 'disk_overallocate' ] < 0 ) ? null : $data [ 'disk_overallocate' ];
// Set the Secret
$uuid = new UuidService ;
$data [ 'daemonSecret' ] = ( string ) $uuid -> generate ( 'nodes' , 'daemonSecret' );
2017-02-09 22:43:54 +00:00
return Models\Node :: create ( $data );
2016-01-05 04:59:45 +00:00
}
2016-01-05 23:31:25 +00:00
public function update ( $id , array $data )
{
2016-08-16 22:45:22 +00:00
$node = Models\Node :: findOrFail ( $id );
2016-01-05 23:31:25 +00:00
// Validate Fields
$validator = $validator = Validator :: make ( $data , [
'name' => 'regex:/^([\w .-]{1,100})$/' ,
2017-02-11 01:26:38 +00:00
'location_id' => 'numeric|min:1|exists:locations,id' ,
2016-01-05 23:31:25 +00:00
'public' => 'numeric|between:0,1' ,
'fqdn' => 'string|unique:nodes,fqdn,' . $id ,
'scheme' => 'regex:/^(http(s)?)$/' ,
'memory' => 'numeric|min:1' ,
'memory_overallocate' => 'numeric|min:-1' ,
'disk' => 'numeric|min:1' ,
'disk_overallocate' => 'numeric|min:-1' ,
2016-12-01 23:33:32 +00:00
'upload_size' => 'numeric|min:0' ,
2017-02-16 20:57:02 +00:00
'daemonBase' => 'sometimes|regex:/^([\/][\d\w.\-\/]+)$/' ,
2016-01-05 23:31:25 +00:00
'daemonSFTP' => 'numeric|between:1,65535' ,
'daemonListen' => 'numeric|between:1,65535' ,
2017-02-16 20:57:02 +00:00
'reset_secret' => 'sometimes|nullable|accepted' ,
2016-01-05 23:31:25 +00:00
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ( $validator -> fails ()) {
throw new DisplayValidationException ( $validator -> errors ());
}
// Verify the FQDN
if ( isset ( $data [ 'fqdn' ])) {
2016-08-16 22:45:22 +00:00
// Verify the FQDN if using SSL
2016-12-07 22:46:38 +00:00
if (( isset ( $data [ 'scheme' ]) && $data [ 'scheme' ] === 'https' ) || ( ! isset ( $data [ 'scheme' ]) && $node -> scheme === 'https' )) {
2016-08-16 22:45:22 +00:00
if ( filter_var ( $data [ 'fqdn' ], FILTER_VALIDATE_IP )) {
throw new DisplayException ( 'A fully qualified domain name is required to use secure comunication on this node.' );
}
2016-01-05 23:31:25 +00:00
}
2016-08-16 22:45:22 +00:00
// Verify FQDN is resolvable, or if not using SSL that the IP is valid.
2016-12-07 22:46:38 +00:00
if ( ! filter_var ( gethostbyname ( $data [ 'fqdn' ]), FILTER_VALIDATE_IP )) {
2016-08-16 22:45:22 +00:00
throw new DisplayException ( 'The FQDN (or IP Address) provided does not resolve to a valid IP address.' );
2016-01-05 23:31:25 +00:00
}
}
// Should we be nulling the overallocations?
if ( isset ( $data [ 'memory_overallocate' ])) {
$data [ 'memory_overallocate' ] = ( $data [ 'memory_overallocate' ] < 0 ) ? null : $data [ 'memory_overallocate' ];
}
if ( isset ( $data [ 'disk_overallocate' ])) {
$data [ 'disk_overallocate' ] = ( $data [ 'disk_overallocate' ] < 0 ) ? null : $data [ 'disk_overallocate' ];
}
// Set the Secret
2017-02-16 20:57:02 +00:00
if ( isset ( $data [ 'reset_secret' ]) && ! is_null ( $data [ 'reset_secret' ])) {
2016-01-05 23:31:25 +00:00
$uuid = new UuidService ;
$data [ 'daemonSecret' ] = ( string ) $uuid -> generate ( 'nodes' , 'daemonSecret' );
unset ( $data [ 'reset_secret' ]);
}
2016-12-01 23:33:32 +00:00
$oldDaemonKey = $node -> daemonSecret ;
$node -> update ( $data );
try {
2017-02-03 21:50:28 +00:00
$node -> guzzleClient ([ 'X-Access-Token' => $oldDaemonKey ]) -> request ( 'PATCH' , '/config' , [
2016-12-01 23:33:32 +00:00
'json' => [
'web' => [
'listen' => $node -> daemonListen ,
'ssl' => [
'enabled' => ( $node -> scheme === 'https' ),
],
],
'sftp' => [
'path' => $node -> daemonBase ,
'port' => $node -> daemonSFTP ,
],
'remote' => [
'base' => config ( 'app.url' ),
'download' => route ( 'remote.download' ),
'installed' => route ( 'remote.install' ),
],
'uploads' => [
'size_limit' => $node -> upload_size ,
],
'keys' => [
$node -> daemonSecret ,
2016-12-07 22:46:38 +00:00
],
2016-12-01 23:33:32 +00:00
],
]);
} catch ( \Exception $ex ) {
throw new DisplayException ( 'Failed to update the node configuration, however your changes have been saved to the database. You will need to manually update the configuration file for the node to apply these changes.' );
}
2016-01-05 23:31:25 +00:00
}
2017-03-03 22:30:39 +00:00
/**
* Adds allocations to a provided node .
* @ param integer $id
* @ param array $data
*/
public function addAllocations ( $id , array $data )
2016-01-10 05:38:16 +00:00
{
$node = Models\Node :: findOrFail ( $id );
2017-03-03 22:30:39 +00:00
$validator = Validator :: make ( $data , [
'allocation_ip' => 'required|string' ,
'allocation_alias' => 'sometimes|required|string|max:255' ,
'allocation_ports' => 'required|array' ,
]);
2016-02-27 15:30:59 +00:00
2017-03-03 22:30:39 +00:00
if ( $validator -> fails ()) {
throw new DisplayValidationException ( $validator -> errors ());
}
$explode = explode ( '/' , $data [ 'allocation_ip' ]);
if ( count ( $explode ) !== 1 ) {
if ( ! ctype_digit ( $explode [ 1 ]) || ( $explode [ 1 ] > 32 || $explode [ 1 ] < 25 )) {
throw new DisplayException ( 'CIDR notation only allows masks between /32 and /25.' );
}
}
DB :: transaction ( function () use ( $parsed , $node , $data ) {
foreach ( Network :: parse ( gethostbyname ( $data [ 'allocation_ip' ])) as $ip ) {
foreach ( $data [ 'allocation_ports' ] as $port ) {
// Determine if this is a valid single port, or a valid port range.
if ( ! ctype_digit ( $port ) && ! preg_match ( '/^(\d{1,5})-(\d{1,5})$/' , $port )) {
throw new DisplayException ( 'The mapping for <code>' . $port . '</code> is invalid and cannot be processed.' );
2016-10-21 21:33:26 +00:00
}
2017-03-03 22:30:39 +00:00
if ( preg_match ( '/^(\d{1,5})-(\d{1,5})$/' , $port , $matches )) {
$block = range ( $matches [ 1 ], $matches [ 2 ]);
if ( count ( $block ) > 1000 ) {
throw new DisplayException ( 'Adding more than 1000 ports at once is not supported. Please use a smaller port range.' );
2016-02-27 15:30:59 +00:00
}
2017-03-03 22:30:39 +00:00
foreach ( $block as $unit ) {
// Insert into Database
Models\Allocation :: firstOrCreate ([
2017-02-11 01:26:38 +00:00
'node_id' => $node -> id ,
2016-01-10 05:38:16 +00:00
'ip' => $ip ,
2017-03-03 22:30:39 +00:00
'port' => $unit ,
'ip_alias' => isset ( $data [ 'allocation_alias' ]) ? $data [ 'allocation_alias' ] : null ,
'server_id' => null ,
2016-01-10 05:38:16 +00:00
]);
}
2017-03-03 22:30:39 +00:00
} else {
// Insert into Database
Models\Allocation :: firstOrCreate ([
'node_id' => $node -> id ,
'ip' => $ip ,
'port' => $port ,
'ip_alias' => isset ( $data [ 'allocation_alias' ]) ? $data [ 'allocation_alias' ] : null ,
'server_id' => null ,
]);
2016-01-10 05:38:16 +00:00
}
}
}
2017-03-03 22:30:39 +00:00
});
2016-01-10 05:38:16 +00:00
}
2016-01-16 05:25:21 +00:00
public function delete ( $id )
{
2017-02-09 22:43:54 +00:00
$node = Models\Node :: withCount ( 'servers' ) -> findOrFail ( $id );
if ( $node -> servers_count > 0 ) {
2016-11-26 21:29:13 +00:00
throw new DisplayException ( 'You cannot delete a node with servers currently attached to it.' );
}
DB :: beginTransaction ();
try {
// Unlink Database Servers
Models\DatabaseServer :: where ( 'linked_node' , $node -> id ) -> update ([
'linked_node' => null ,
]);
// Delete Allocations
2017-02-11 01:26:38 +00:00
Models\Allocation :: where ( 'node_id' , $node -> id ) -> delete ();
2016-11-26 21:29:13 +00:00
2017-01-07 17:10:11 +00:00
// Delete configure tokens
2017-01-07 17:26:45 +00:00
Models\NodeConfigurationToken :: where ( 'node' , $node -> id ) -> delete ();
2017-01-07 17:10:11 +00:00
2016-11-26 21:29:13 +00:00
// Delete Node
$node -> delete ();
DB :: commit ();
} catch ( \Exception $ex ) {
DB :: rollback ();
throw $ex ;
}
2016-01-16 05:25:21 +00:00
}
2016-01-05 04:59:45 +00:00
}