Skip to content

Commit e8c175f

Browse files
authored
Add IP Aliasing (pterodactyl#72)
* complete support for IP Alias's throughout panel Includes a database change and probably better allocation handling anyways closes pterodactyl#37
1 parent f1a3008 commit e8c175f

File tree

14 files changed

+206
-76
lines changed

14 files changed

+206
-76
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ public function __construct()
5252
public function getIndex(Request $request)
5353
{
5454
return view('admin.servers.index', [
55-
'servers' => Models\Server::select('servers.*', 'nodes.name as a_nodeName', 'users.email as a_ownerEmail')
56-
->join('nodes', 'servers.node', '=', 'nodes.id')
57-
->join('users', 'servers.owner', '=', 'users.id')
58-
->paginate(20),
55+
'servers' => Models\Server::select(
56+
'servers.*',
57+
'nodes.name as a_nodeName',
58+
'users.email as a_ownerEmail',
59+
'allocations.ip',
60+
'allocations.port',
61+
'allocations.ip_alias'
62+
)->join('nodes', 'servers.node', '=', 'nodes.id')
63+
->join('users', 'servers.owner', '=', 'users.id')
64+
->join('allocations', 'servers.allocation', '=', 'allocations.id')
65+
->paginate(20),
5966
]);
6067
}
6168

@@ -76,12 +83,16 @@ public function getView(Request $request, $id)
7683
'locations.long as a_locationName',
7784
'services.name as a_serviceName',
7885
'services.executable as a_serviceExecutable',
79-
'service_options.name as a_servceOptionName'
86+
'service_options.name as a_servceOptionName',
87+
'allocations.ip',
88+
'allocations.port',
89+
'allocations.ip_alias'
8090
)->join('nodes', 'servers.node', '=', 'nodes.id')
8191
->join('users', 'servers.owner', '=', 'users.id')
8292
->join('locations', 'nodes.location', '=', 'locations.id')
8393
->join('services', 'servers.service', '=', 'services.id')
8494
->join('service_options', 'servers.option', '=', 'service_options.id')
95+
->join('allocations', 'servers.allocation', '=', 'allocations.id')
8596
->where('servers.id', $id)
8697
->first();
8798

@@ -91,8 +102,8 @@ public function getView(Request $request, $id)
91102

92103
return view('admin.servers.view', [
93104
'server' => $server,
94-
'assigned' => Models\Allocation::select('id', 'ip', 'port')->where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
95-
'unassigned' => Models\Allocation::select('id', 'ip', 'port')->where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
105+
'assigned' => Models\Allocation::where('assigned_to', $id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
106+
'unassigned' => Models\Allocation::where('node', $server->node)->whereNull('assigned_to')->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
96107
'startup' => Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')
97108
->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
98109
->where('service_variables.option_id', $server->option)

app/Http/Controllers/Server/AjaxController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ public function postSetConnection(Request $request, $uuid)
194194
{
195195

196196
$server = Server::getByUUID($uuid);
197+
$allocation = Models\Allocation::findOrFail($server->allocation);
198+
197199
$this->authorize('set-connection', $server);
198200

199-
if ($request->input('connection') === $server->ip . ':' . $server->port) {
201+
if ($request->input('connection') === $allocation->ip . ':' . $allocation->port) {
200202
return response()->json([
201203
'error' => 'You are already using this as your default connection.'
202204
], 409);

app/Http/Controllers/Server/ServerController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,8 @@ public function getDownloadFile(Request $request, $uuid, $file)
195195
public function getSettings(Request $request, $uuid)
196196
{
197197
$server = Models\Server::getByUUID($uuid);
198-
// $variables = Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')
199-
// ->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
200-
// ->where('service_variables.option_id', $server->option)
201-
// ->where('server_variables.server_id', $server->id)
202-
// ->get();
198+
$allocation = Models\Allocation::findOrFail($server->allocation);
199+
203200
$variables = Models\ServiceVariables::select(
204201
'service_variables.*',
205202
DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue')
@@ -217,8 +214,8 @@ public function getSettings(Request $request, $uuid)
217214

218215
$serverVariables = [
219216
'{{SERVER_MEMORY}}' => $server->memory,
220-
'{{SERVER_IP}}' => $server->ip,
221-
'{{SERVER_PORT}}' => $server->port,
217+
'{{SERVER_IP}}' => $allocation->ip,
218+
'{{SERVER_PORT}}' => $allocation->port,
222219
];
223220

224221
$processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup);

app/Models/Server.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,16 @@ protected static function getUserDaemonSecret(Server $server)
124124
public static function getUserServers($paginate = null)
125125
{
126126

127-
$query = self::select('servers.*', 'nodes.name as nodeName', 'locations.short as a_locationShort')
128-
->join('nodes', 'servers.node', '=', 'nodes.id')
129-
->join('locations', 'nodes.location', '=', 'locations.id')
130-
->where('active', 1);
127+
$query = self::select(
128+
'servers.*',
129+
'nodes.name as nodeName',
130+
'locations.short as a_locationShort',
131+
'allocations.ip_alias',
132+
'allocations.port'
133+
)->join('nodes', 'servers.node', '=', 'nodes.id')
134+
->join('locations', 'nodes.location', '=', 'locations.id')
135+
->join('allocations', 'servers.allocation', '=', 'allocations.id')
136+
->where('active', 1);
131137

132138
if (self::$user->root_admin !== 1) {
133139
$query->whereIn('servers.id', Subuser::accessServers());

app/Repositories/ServerRepository.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ public function create(array $data)
213213
'io' => $data['io'],
214214
'cpu' => $data['cpu'],
215215
'oom_disabled' => (isset($data['oom_disabled'])) ? true : false,
216-
'ip' => $data['ip'],
217-
'port' => $data['port'],
216+
'allocation' => $allocation->id,
218217
'service' => $data['service'],
219218
'option' => $data['option'],
220219
'startup' => $data['startup'],
@@ -253,11 +252,11 @@ public function create(array $data)
253252
'user' => $server->username,
254253
'build' => [
255254
'default' => [
256-
'ip' => $server->ip,
257-
'port' => (int) $server->port
255+
'ip' => $allocation->ip,
256+
'port' => (int) $allocation->port
258257
],
259258
'ports' => [
260-
(string) $server->ip => [ (int) $server->port ]
259+
(string) $allocation->ip => [ (int) $allocation->port ]
261260
],
262261
'env' => $environmentVariables,
263262
'memory' => (int) $server->memory,
@@ -413,17 +412,20 @@ public function changeBuild($id, array $data)
413412

414413
try {
415414
$server = Models\Server::findOrFail($id);
415+
$allocation = Models\Allocation::findOrFail($server->allocation);
416416

417417
if (isset($data['default'])) {
418418
list($ip, $port) = explode(':', $data['default']);
419-
if ($ip !== $server->ip || $port !== $server->port) {
420-
$allocation = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first();
421-
if (!$allocation) {
419+
if ($ip !== $allocation->ip || $port !== $allocation->port) {
420+
$selection = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first();
421+
if (!$selection) {
422422
throw new DisplayException('The requested default connection (' . $ip . ':' . $port . ') is not allocated to this server.');
423423
}
424424

425-
$server->ip = $ip;
426-
$server->port = $port;
425+
$server->allocation = $selection->id;
426+
427+
// Re-Run to keep updated for rest of function
428+
$allocation = Models\Allocation::findOrFail($server->allocation);
427429
}
428430
}
429431

@@ -437,7 +439,7 @@ public function changeBuild($id, array $data)
437439
}
438440

439441
// Can't remove the assigned IP/Port combo
440-
if ($ip === $server->ip && $port === $server->port) {
442+
if ($ip === $allocation->ip && $port === $allocation->port) {
441443
continue;
442444
}
443445

@@ -513,8 +515,8 @@ public function changeBuild($id, array $data)
513515
'json' => [
514516
'build' => [
515517
'default' => [
516-
'ip' => $server->ip,
517-
'port' => (int) $server->port
518+
'ip' => $allocation->ip,
519+
'port' => (int) $allocation->port
518520
],
519521
'ports|overwrite' => $additionalAssignments,
520522
'memory' => (int) $server->memory,

database/migrations/2016_08_15_234600_fix_column_name_for_databases.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddIpAlias extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('allocations', function (Blueprint $table) {
16+
$table->text('ip_alias')->nullable()->after('ip');
17+
});
18+
19+
$allocations = DB::select('SELECT id, ip FROM allocations');
20+
foreach($allocations as $allocation) {
21+
DB::update(
22+
'UPDATE allocations SET ip_alias = :ip WHERE id = :id',
23+
[
24+
'ip' => $allocation->ip,
25+
'id' => $allocation->id
26+
]
27+
);
28+
}
29+
}
30+
31+
/**
32+
* Reverse the migrations.
33+
*
34+
* @return void
35+
*/
36+
public function down()
37+
{
38+
Schema::table('allocations', function (Blueprint $table) {
39+
$table->dropColumn('ip_alias');
40+
});
41+
}
42+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class ModifyIpStorageMethod extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('servers', function (Blueprint $table) {
16+
$table->mediumInteger('allocation')->unsigned()->after('oom_disabled');
17+
});
18+
19+
// Parse All Servers
20+
$servers = DB::select('SELECT id, ip, port, node FROM servers');
21+
foreach($servers as $server) {
22+
$allocation = DB::select(
23+
'SELECT id FROM allocations WHERE ip = :ip AND port = :port AND node = :node',
24+
[
25+
'ip' => $server->ip,
26+
'port' => $server->port,
27+
'node' => $server->node
28+
]
29+
);
30+
31+
if (isset($allocation[0])) {
32+
DB::update(
33+
'UPDATE servers SET allocation = :alocid WHERE id = :id',
34+
[
35+
'alocid' => $allocation[0]->id,
36+
'id' => $server->id
37+
]
38+
);
39+
}
40+
}
41+
42+
// Updated the server allocations, remove old fields
43+
Schema::table('servers', function (Blueprint $table) {
44+
$table->dropColumn('ip');
45+
$table->dropColumn('port');
46+
});
47+
48+
}
49+
50+
/**
51+
* Reverse the migrations.
52+
*
53+
* @return void
54+
*/
55+
public function down()
56+
{
57+
58+
Schema::table('servers', function (Blueprint $table) {
59+
$table->text('ip')->after('allocation');
60+
$table->integer('port')->unsigned()->after('ip');
61+
});
62+
63+
// Find the allocations and reset the servers...
64+
$servers = DB::select('SELECT id, allocation FROM servers');
65+
foreach($servers as $server) {
66+
$allocation = DB::select('SELECT * FROM allocations WHERE id = :alocid', [ 'alocid' => $server->allocation ]);
67+
68+
if (isset($allocation[0])) {
69+
DB::update(
70+
'UPDATE servers SET ip = :ip, port = :port WHERE id = :id',
71+
[
72+
'ip' => $allocation[0]->ip,
73+
'port' => $allocation[0]->port,
74+
'id' => $server->id
75+
]
76+
);
77+
}
78+
}
79+
80+
Schema::table('servers', function (Blueprint $table) {
81+
$table->dropColumn('allocation');
82+
});
83+
}
84+
}

resources/views/admin/servers/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<td><a href="/admin/servers/view/{{ $server->id }}">{{ $server->name }}</td>
4747
<td><a href="/admin/users/view/{{ $server->owner }}">{{ $server->a_ownerEmail }}</a></td>
4848
<td class="hidden-xs"><a href="/admin/nodes/view/{{ $server->node }}">{{ $server->a_nodeName }}</a></td>
49-
<td><code>{{ $server->ip }}:{{ $server->port }}</code></td>
49+
<td><code>{{ $server->ip_alias }}:{{ $server->port }}</code> @if($server->ip !== $server->ip_alias)<span class="label label-default">alias</span>@endif</td>
5050
<td class="hidden-xs"><code>{{ $server->username }}</code></td>
5151
</tr>
5252
@endforeach

0 commit comments

Comments
 (0)