Skip to content

Commit 36837df

Browse files
committed
Use beginning of UUID for server uuidShort
1 parent 85e75a2 commit 36837df

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1515
* Databases are now properly paginated when viewing a database host.
1616
* No more loading daemon keys for every server model being loaded, some of us value our databases.
1717
* Changed behavior of the subuser middleware to add a daemon access key if one is missing from the database for some reason.
18+
* Server short-codes are now based on the UUID as they were in previous versions of Pterodactyl.
1819

1920
## v0.7.4-h1 (Derelict Dermodactylus)
2021
### Fixed

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,13 @@ public function getServersForPowerAction(array $servers = [], array $nodes = [],
136136
* @return int
137137
*/
138138
public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int;
139+
140+
/**
141+
* Check if a given UUID and UUID-Short string are unique to a server.
142+
*
143+
* @param string $uuid
144+
* @param string $short
145+
* @return bool
146+
*/
147+
public function isUniqueUuidCombo(string $uuid, string $short): bool;
139148
}

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ public function getServersForPowerActionCount(array $servers = [], array $nodes
303303
return $this->getServersForPowerAction($servers, $nodes, true);
304304
}
305305

306+
/**
307+
* Check if a given UUID and UUID-Short string are unique to a server.
308+
*
309+
* @param string $uuid
310+
* @param string $short
311+
* @return bool
312+
*/
313+
public function isUniqueUuidCombo(string $uuid, string $short): bool
314+
{
315+
return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();
316+
}
317+
306318
/**
307319
* Return an array of server IDs that a given user can access based
308320
* on owner and subuser permissions.

app/Services/Servers/ServerCreationService.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ private function configureDeployment(array $data, DeploymentObject $deployment):
210210
*/
211211
private function createModel(array $data): Server
212212
{
213+
$uuid = $this->generateUniqueUuidCombo();
214+
213215
return $this->repository->create([
214216
'external_id' => array_get($data, 'external_id'),
215-
'uuid' => Uuid::uuid4()->toString(),
216-
'uuidShort' => str_random(8),
217+
'uuid' => $uuid,
218+
'uuidShort' => substr($uuid, 0, 8),
217219
'node_id' => array_get($data, 'node_id'),
218220
'name' => array_get($data, 'name'),
219221
'description' => array_get($data, 'description') ?? '',
@@ -289,4 +291,20 @@ private function getNodeFromAllocation(int $allocation): int
289291

290292
return $allocation->node_id;
291293
}
294+
295+
/**
296+
* Create a unique UUID and UUID-Short combo for a server.
297+
*
298+
* @return string
299+
*/
300+
private function generateUniqueUuidCombo(): string
301+
{
302+
$uuid = Uuid::uuid4()->toString();
303+
304+
if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {
305+
return $this->generateUniqueUuidCombo();
306+
}
307+
308+
return $uuid;
309+
}
292310
}

tests/Unit/Services/Servers/ServerCreationServiceTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
116116
]);
117117

118118
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
119+
$this->repository->shouldReceive('isUniqueUuidCombo')
120+
->once()
121+
->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8))
122+
->andReturn(true);
123+
119124
$this->repository->shouldReceive('create')->with(m::subset([
120125
'uuid' => $this->getKnownUuid(),
126+
'uuidShort' => substr($this->getKnownUuid(), 0, 8),
121127
'node_id' => $model->node_id,
122128
'allocation_id' => $model->allocation_id,
123129
'owner_id' => $model->owner_id,
@@ -164,8 +170,14 @@ public function testDataIsAutoFilled()
164170
$this->eggRepository->shouldReceive('setColumns->find')->once()->with($model->egg_id)->andReturn($eggModel);
165171

166172
$this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([]));
167-
$this->repository->shouldReceive('create')->once()->with(m::subset([
173+
$this->repository->shouldReceive('isUniqueUuidCombo')
174+
->once()
175+
->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8))
176+
->andReturn(true);
177+
178+
$this->repository->shouldReceive('create')->with(m::subset([
168179
'uuid' => $this->getKnownUuid(),
180+
'uuidShort' => substr($this->getKnownUuid(), 0, 8),
169181
'node_id' => $model->node_id,
170182
'allocation_id' => $model->allocation_id,
171183
'nest_id' => $model->nest_id,
@@ -211,8 +223,14 @@ public function testAutoDeploymentObject()
211223
$this->allocationSelectionService->shouldReceive('handle')->once()->withNoArgs()->andReturn($allocationModel);
212224

213225
$this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([]));
214-
$this->repository->shouldReceive('create')->once()->with(m::subset([
226+
$this->repository->shouldReceive('isUniqueUuidCombo')
227+
->once()
228+
->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8))
229+
->andReturn(true);
230+
231+
$this->repository->shouldReceive('create')->with(m::subset([
215232
'uuid' => $this->getKnownUuid(),
233+
'uuidShort' => substr($this->getKnownUuid(), 0, 8),
216234
'node_id' => $model->node_id,
217235
'allocation_id' => $model->allocation_id,
218236
'nest_id' => $model->nest_id,
@@ -244,6 +262,7 @@ public function testExceptionShouldBeThrownIfTheRequestFails()
244262
]);
245263

246264
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
265+
$this->repository->shouldReceive('isUniqueUuidCombo')->once()->andReturn(true);
247266
$this->repository->shouldReceive('create')->once()->andReturn($model);
248267
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1);
249268
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnSelf();

0 commit comments

Comments
 (0)