Add support for allocation management on nodes.

Allows deleting entire IP blocks, as well as allocating new IPs and
Ports via CIDR ranges, single IP, and single ports or a port range.
This commit is contained in:
Dane Everitt 2016-01-10 00:38:16 -05:00
parent aaf9768a92
commit 179481c547
8 changed files with 282 additions and 8 deletions

View file

@ -68,7 +68,7 @@ class NodesController extends Controller
{
$node = Models\Node::findOrFail($id);
$allocations = [];
$alloc = Models\Allocation::select('ip', 'port', 'assigned_to')->where('node', $node->id)->get();
$alloc = Models\Allocation::select('ip', 'port', 'assigned_to')->where('node', $node->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get();
if ($alloc) {
foreach($alloc as &$alloc) {
if (!array_key_exists($alloc->ip, $allocations)) {
@ -122,9 +122,15 @@ class NodesController extends Controller
])->withInput();
}
public function deletePortAllocation(Request $request, $id, $ip, $port)
public function deleteAllocation(Request $request, $id, $ip, $port = null)
{
$allocation = Models\Allocation::where('node', $id)->whereNull('assigned_to')->where('ip', $ip)->where('port', $port)->first();
$query = Models\Allocation::where('node', $id)->whereNull('assigned_to')->where('ip', $ip);
if (is_null($port) || $port === 'undefined') {
$allocation = $query;
} else {
$allocation = $query->where('port', $port)->first();
}
if (!$allocation) {
return response()->json([
'error' => 'Unable to find an allocation matching those details to delete.'
@ -134,4 +140,50 @@ class NodesController extends Controller
return response('', 204);
}
public function getAllocationsJson(Request $request, $id)
{
$allocations = Models\Allocation::select('ip')->where('node', $id)->groupBy('ip')->get();
return response()->json($allocations);
}
public function postAllocations(Request $request, $id)
{
$processedData = [];
foreach($request->input('allocate_ip') as $ip) {
if (!array_key_exists($ip, $processedData)) {
$processedData[$ip] = [];
}
}
foreach($request->input('allocate_port') as $portid => $ports) {
if (array_key_exists($portid, $request->input('allocate_ip'))) {
$json = json_decode($ports);
if (json_last_error() === 0 && !empty($json)) {
foreach($json as &$parsed) {
array_push($processedData[$request->input('allocate_ip')[$portid]], $parsed->value);
}
}
}
}
try {
if(empty($processedData)) {
throw new \Pterodactyl\Exceptions\DisplayException('It seems that no data was passed to this function.');
}
$node = new NodeRepository;
$node->addAllocations($id, $processedData);
Alert::success('Successfully added new allocations to this node.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);
Alert::danger('An unhandled exception occured while attempting to add allocations this node. Please try again.')->flash();
} finally {
return redirect()->route('admin.nodes.view', [
'id' => $id,
'tab' => 'tab_allocation'
]);
}
}
}

View file

@ -172,8 +172,18 @@ class AdminRoutes {
'uses' => 'Admin\NodesController@postView'
]);
$router->delete('/view/{id}/allocation/{ip}/{port}', [
'uses' => 'Admin\NodesController@deletePortAllocation'
$router->delete('/view/{id}/allocation/{ip}/{port?}', [
'uses' => 'Admin\NodesController@deleteAllocation'
]);
$router->get('/view/{id}/allocations.json', [
'as' => 'admin.nodes.view.allocations',
'uses' => 'Admin\NodesController@getAllocationsJson'
]);
$router->post('/view/{id}/allocations', [
'as' => 'admin.nodes.post.allocations',
'uses' => 'Admin\NodesController@postAllocations'
]);
});

View file

@ -14,4 +14,11 @@ class Allocation extends Model
*/
protected $table = 'allocations';
/**
* Fields that are not mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
}

View file

@ -2,11 +2,13 @@
namespace Pterodactyl\Repositories;
use DB;
use Validator;
use Pterodactyl\Models;
use Pterodactyl\Services\UuidService;
use IPTools\Network;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
@ -123,4 +125,62 @@ class NodeRepository {
}
public function addAllocations($id, array $allocations)
{
$node = Models\Node::findOrFail($id);
DB::beginTransaction();
foreach($allocations as $rawIP => $ports) {
$parsedIP = Network::parse($rawIP);
foreach($parsedIP as $ip) {
foreach($ports as $port) {
if (!is_int($port) && !preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
foreach(range($matches[1], $matches[2]) as $assignPort) {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort
]);
if (!$alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort,
'assigned_to' => null
]);
$alloc->save();
}
}
} else {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
'port' => $port
]);
if (!$alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $port,
'assigned_to' => null
]);
$alloc->save();
}
}
}
}
}
try {
DB::commit();
return true;
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}
}