Skip to content

Commit 8f8bd0b

Browse files
committed
Added Automatic Allocations
Known issues: - Port range to auto create is hard coded - React interface is still a WIP.
1 parent bcbd2c4 commit 8f8bd0b

File tree

8 files changed

+186
-4
lines changed

8 files changed

+186
-4
lines changed

.env.bkup

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
APP_ENV=local
2+
APP_DEBUG=true
3+
APP_KEY=base64:TdNqOn+57eXjhYF9xaM4WZZ0JAAyeGcHF/5dcclZhhk=
4+
APP_THEME=pterodactyl
5+
APP_TIMEZONE=America/Los_Angeles
6+
APP_CLEAR_TASKLOG=720
7+
APP_DELETE_MINUTES=10
8+
APP_ENVIRONMENT_ONLY=false
9+
LOG_CHANNEL=daily
10+
APP_LOCALE=en
11+
12+
DB_HOST=127.0.0.1
13+
DB_PORT=33060
14+
DB_DATABASE=panel
15+
DB_USERNAME=pterodactyl
16+
DB_PASSWORD=pterodactyl
17+
18+
HASHIDS_SALT=g9nStpee2fwL2bUsfQqy
19+
HASHIDS_LENGTH=8
20+
21+
MAIL_DRIVER=smtp
22+
MAIL_HOST=host.pterodactyl.test
23+
MAIL_PORT=1025
24+
MAIL_USERNAME=
25+
MAIL_PASSWORD=
26+
MAIL_ENCRYPTION=tls
27+
MAIL_FROM="outgoing@example.com"
28+
29+
QUEUE_HIGH=high
30+
QUEUE_STANDARD=standard
31+
QUEUE_LOW=low
32+
33+
APP_SERVICE_AUTHOR="you@example.com"
34+
APP_URL="http://pterodactyl.test"
35+
CACHE_DRIVER=redis
36+
SESSION_DRIVER=redis
37+
QUEUE_CONNECTION=redis
38+
REDIS_HOST=host.pterodactyl.test
39+
REDIS_PASSWORD=null
40+
REDIS_PORT=6379
41+
MAIL_FROM_NAME="Pterodactyl Panel"

app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
1414
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
1515
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
16+
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
1617
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
18+
use Pterodactyl\Services\Allocations\AssignmentService;
19+
use Illuminate\Support\Facades\Log;
1720

1821
class NetworkAllocationController extends ClientApiController
1922
{
@@ -27,20 +30,28 @@ class NetworkAllocationController extends ClientApiController
2730
*/
2831
private $serverRepository;
2932

33+
/**
34+
* @var \Pterodactyl\Services\Allocations\AssignmentService
35+
*/
36+
protected $assignmentService;
37+
3038
/**
3139
* NetworkController constructor.
3240
*
3341
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
3442
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
43+
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
3544
*/
3645
public function __construct(
3746
AllocationRepository $repository,
38-
ServerRepository $serverRepository
47+
ServerRepository $serverRepository,
48+
AssignmentService $assignmentService
3949
) {
4050
parent::__construct();
4151

4252
$this->repository = $repository;
4353
$this->serverRepository = $serverRepository;
54+
$this->assignmentService = $assignmentService;
4455
}
4556

4657
/**
@@ -100,6 +111,66 @@ public function setPrimary(SetPrimaryAllocationRequest $request, Server $server,
100111
->toArray();
101112
}
102113

114+
/**
115+
* Set the notes for the allocation for a server.
116+
*s
117+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest $request
118+
* @param \Pterodactyl\Models\Server $server
119+
* @param \Pterodactyl\Models\Allocation $allocation
120+
* @return array
121+
*
122+
* @throws \Pterodactyl\Exceptions\DisplayException
123+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
124+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
125+
*/
126+
public function addNew(NewAllocationRequest $request, Server $server): array
127+
{
128+
Log::info('addNew()');
129+
$topRange = 25700;
130+
$bottomRange = 25565;
131+
132+
if($server->allocation_limit <= $server->allocations->count()) {
133+
Log::error('You have created the maximum number of allocations!');
134+
throw new DisplayException(
135+
'You have created the maximum number of allocations!'
136+
);
137+
}
138+
139+
$allocation = $server->node->allocations()->where('ip',$server->allocation->ip)->whereNull('server_id')->first();
140+
141+
if(!$allocation) {
142+
if($server->node->allocations()->where('ip',$server->allocation->ip)->count() >= $topRange-$bottomRange) {
143+
Log::error('No allocations available!');
144+
throw new DisplayException(
145+
'No more allocations available!'
146+
);
147+
}
148+
Log::info('Creating new allocation...');
149+
$allPorts = $server->node->allocations()->select(['port'])->where('ip',$server->allocation->ip)->get()->pluck('port')->toArray();
150+
151+
do {
152+
$port = rand($bottomRange, $topRange);
153+
Log::info('Picking port....');
154+
// TODO ADD ITERATOR THAT TIMES OUT AFTER SEARCHING FOR SO MUCH TIME?
155+
} while(array_search($port, $allPorts));
156+
157+
$this->assignmentService->handle($server->node,[
158+
'allocation_ip'=>$server->allocation->ip,
159+
'allocation_ports'=>[$port],
160+
'server_id'=>$server->id
161+
]);
162+
163+
$allocation = $server->node->allocations()->where('ip',$server->allocation->ip)->where('port', $port)->first();
164+
165+
}
166+
167+
$allocation->update(['server_id' => $server->id]);
168+
169+
return $this->fractal->item($allocation)
170+
->transformWith($this->getTransformer(AllocationTransformer::class))
171+
->toArray();
172+
}
173+
103174
/**
104175
* Delete an allocation from a server.
105176
*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
4+
5+
use Illuminate\Support\Collection;
6+
use Pterodactyl\Models\Allocation;
7+
use Pterodactyl\Models\Permission;
8+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
9+
10+
class NewAllocationRequest extends ClientApiRequest
11+
{
12+
/**
13+
* @return string
14+
*/
15+
public function permission(): string
16+
{
17+
return Permission::ACTION_ALLOCATION_CREATE;
18+
}
19+
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Allocation } from '@/api/server/getServer';
2+
import http from '@/api/http';
3+
import { rawDataToServerAllocation } from '@/api/transformers';
4+
5+
export default (uuid: string): Promise<Allocation> => {
6+
return new Promise((resolve, reject) => {
7+
http.get(`/api/client/servers/${uuid}/network/allocations/new`)
8+
.then(({ data }) => resolve(rawDataToServerAllocation(data)))
9+
.catch(reject);
10+
});
11+
};

resources/scripts/components/server/network/AllocationRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
4242
return (
4343
<GreyRowBox
4444
$hoverable={false}
45-
// css={index > 0 ? tw`mt-2 overflow-x-auto` : tw`overflow-x-auto`}
45+
css={tw`mt-2 overflow-x-auto`}
4646
>
4747
<div css={tw`hidden md:block pl-4 pr-6 text-neutral-400`}>
4848
<FontAwesomeIcon icon={faNetworkWired}/>

resources/scripts/components/server/network/NetworkContainer.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect } from 'react';
1+
import React, { useCallback, useEffect, useState } from 'react';
22
import useSWR from 'swr';
33
import getServerAllocations from '@/api/server/network/getServerAllocations';
44
import { Allocation } from '@/api/server/getServer';
@@ -9,10 +9,15 @@ import { ServerContext } from '@/state/server';
99
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
1010
import AllocationRow from '@/components/server/network/AllocationRow';
1111
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
12+
import Button from '@/components/elements/Button';
13+
import newServerAllocation from '@/api/server/network/newServerAllocation';
14+
import tw from 'twin.macro';
15+
import GreyRowBox from '@/components/elements/GreyRowBox';
1216

1317
const NetworkContainer = () => {
1418
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
1519
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
20+
const [ addingAllocation, setAddingAllocation ] = useState(false);
1621

1722
const { clearFlashes, clearAndAddHttpError } = useFlash();
1823
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
@@ -39,6 +44,22 @@ const NetworkContainer = () => {
3944
});
4045
}, []);
4146

47+
const getNewAllocation = () => {
48+
clearFlashes('server:network');
49+
setAddingAllocation(true);
50+
51+
newServerAllocation(uuid)
52+
.then(allocation => {
53+
mutate(data => ({ ...data, items: data.concat(allocation) }), false);
54+
setAddingAllocation(false);
55+
})
56+
.catch(error => {
57+
clearAndAddHttpError({ key: 'server:network', error });
58+
mutate(data, false);
59+
setAddingAllocation(false);
60+
});
61+
};
62+
4263
const onNotesAdded = useCallback((id: number, notes: string) => {
4364
mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false);
4465
}, []);
@@ -57,6 +78,23 @@ const NetworkContainer = () => {
5778
/>
5879
))
5980
}
81+
<GreyRowBox
82+
$hoverable={false}
83+
css={tw`mt-2 overflow-x-auto flex items-center justify-center`}
84+
>
85+
{addingAllocation ?
86+
<Spinner size={'base'} centered/>
87+
:
88+
<Button
89+
color={'primary'}
90+
isSecondary
91+
onClick={() => getNewAllocation() }
92+
css={tw`my-2`}
93+
>
94+
Add New Allocation
95+
</Button>
96+
}
97+
</GreyRowBox>
6098
</ServerContentBlock>
6199
);
62100
};

routes/api-client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
Route::get('/allocations', 'Servers\NetworkAllocationController@index');
8484
Route::post('/allocations/{allocation}', 'Servers\NetworkAllocationController@update');
8585
Route::post('/allocations/{allocation}/primary', 'Servers\NetworkAllocationController@setPrimary');
86+
Route::get('/allocations/new', 'Servers\NetworkAllocationController@addNew');
8687
Route::delete('/allocations/{allocation}', 'Servers\NetworkAllocationController@delete');
8788
});
8889

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module.exports = {
112112
contentBase: path.join(__dirname, '/public'),
113113
publicPath: (process.env.PUBLIC_PATH || '') + '/assets/',
114114
allowedHosts: [
115-
'.pterodactyl.test',
115+
'pterodactyl.test',
116116
],
117117
headers: {
118118
'Access-Control-Allow-Origin': '*',

0 commit comments

Comments
 (0)