2019-03-17 00:10:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2019-05-02 04:45:39 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-01-17 19:46:08 +00:00
|
|
|
use Pterodactyl\Models\AuditLog;
|
2020-04-07 03:28:14 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2019-10-26 21:36:37 +00:00
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
|
2019-07-28 03:23:51 +00:00
|
|
|
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
|
2019-03-17 00:10:04 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2019-05-05 00:26:24 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
|
2020-12-24 17:15:03 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest;
|
2019-05-02 03:54:40 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ListFilesRequest;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\ChmodFilesRequest;
|
2019-05-05 00:26:24 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DeleteFileRequest;
|
2019-05-04 23:04:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\RenameFileRequest;
|
2019-05-02 04:45:39 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CreateFolderRequest;
|
2020-07-11 20:38:49 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CompressFilesRequest;
|
2020-07-15 04:16:38 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\DecompressFilesRequest;
|
2019-05-25 23:24:13 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\GetFileContentsRequest;
|
2019-05-27 22:30:49 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\WriteFileContentRequest;
|
2019-03-17 00:10:04 +00:00
|
|
|
|
|
|
|
class FileController extends ClientApiController
|
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private DaemonFileRepository $fileRepository;
|
|
|
|
private ResponseFactory $responseFactory;
|
|
|
|
private NodeJWTService $jwtService;
|
2020-04-07 03:28:14 +00:00
|
|
|
|
2019-03-17 00:10:04 +00:00
|
|
|
/**
|
|
|
|
* FileController constructor.
|
|
|
|
*/
|
2019-10-26 21:36:37 +00:00
|
|
|
public function __construct(
|
2021-03-05 17:03:12 +00:00
|
|
|
DaemonFileRepository $fileRepository,
|
2019-10-26 21:36:37 +00:00
|
|
|
ResponseFactory $responseFactory,
|
2021-03-05 17:03:12 +00:00
|
|
|
NodeJWTService $jwtService
|
2019-10-26 21:36:37 +00:00
|
|
|
) {
|
2019-03-17 00:10:04 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2019-05-02 03:54:40 +00:00
|
|
|
$this->fileRepository = $fileRepository;
|
2019-10-26 21:36:37 +00:00
|
|
|
$this->responseFactory = $responseFactory;
|
2020-04-07 03:28:14 +00:00
|
|
|
$this->jwtService = $jwtService;
|
2019-05-02 03:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a listing of files in a given directory.
|
|
|
|
*
|
2019-07-28 03:23:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2019-05-02 03:54:40 +00:00
|
|
|
*/
|
2020-07-11 23:00:30 +00:00
|
|
|
public function directory(ListFilesRequest $request, Server $server): array
|
2019-05-02 03:54:40 +00:00
|
|
|
{
|
2020-07-18 17:23:28 +00:00
|
|
|
$contents = $this->fileRepository
|
|
|
|
->setServer($server)
|
2020-12-17 05:38:46 +00:00
|
|
|
->getDirectory($request->get('directory') ?? '/');
|
2019-07-28 03:23:51 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($contents)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(FileObjectTransformer::class)
|
2019-07-28 03:23:51 +00:00
|
|
|
->toArray();
|
2019-03-17 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:24:13 +00:00
|
|
|
/**
|
|
|
|
* Return the contents of a specified file for the user.
|
|
|
|
*
|
2021-01-17 19:46:08 +00:00
|
|
|
* @throws \Throwable
|
2019-05-25 23:24:13 +00:00
|
|
|
*/
|
2020-07-11 23:00:30 +00:00
|
|
|
public function contents(GetFileContentsRequest $request, Server $server): Response
|
2019-05-25 23:24:13 +00:00
|
|
|
{
|
2021-01-17 19:46:08 +00:00
|
|
|
$response = $this->fileRepository->setServer($server)->getContent(
|
2021-01-26 03:20:51 +00:00
|
|
|
$request->get('file'),
|
|
|
|
config('pterodactyl.files.max_edit_size')
|
2019-05-25 23:24:13 +00:00
|
|
|
);
|
2021-01-17 19:46:08 +00:00
|
|
|
|
|
|
|
return new Response($response, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
|
2019-05-25 23:24:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 21:36:37 +00:00
|
|
|
/**
|
2020-04-07 03:28:14 +00:00
|
|
|
* Generates a one-time token with a link that the user can use to
|
|
|
|
* download a given file.
|
|
|
|
*
|
2021-01-17 19:46:08 +00:00
|
|
|
* @throws \Throwable
|
2019-10-26 21:36:37 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function download(GetFileContentsRequest $request, Server $server): array
|
2019-10-26 21:36:37 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$token = $server->audit(AuditLog::SERVER__FILESYSTEM_DOWNLOAD, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
$audit->metadata = ['file' => $request->get('file')];
|
|
|
|
|
|
|
|
return $this->jwtService
|
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
|
|
|
->setClaims([
|
|
|
|
'file_path' => rawurldecode($request->get('file')),
|
|
|
|
'server_uuid' => $server->uuid,
|
|
|
|
])
|
|
|
|
->handle($server->node, $request->user()->id . $server->uuid);
|
|
|
|
});
|
2020-04-07 03:28:14 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
'object' => 'signed_url',
|
|
|
|
'attributes' => [
|
|
|
|
'url' => sprintf(
|
|
|
|
'%s/download/file?token=%s',
|
|
|
|
$server->node->getConnectionAddress(),
|
2021-01-23 20:09:16 +00:00
|
|
|
$token->toString()
|
2020-04-07 03:28:14 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
];
|
2019-10-26 21:36:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 22:30:49 +00:00
|
|
|
/**
|
|
|
|
* Writes the contents of the specified file to the server.
|
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Throwable
|
2019-05-27 22:30:49 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function write(WriteFileContentRequest $request, Server $server): Response
|
2019-05-27 22:30:49 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:52:44 +00:00
|
|
|
$audit->subaction = 'write_content';
|
|
|
|
$audit->metadata = ['file' => $request->get('file')];
|
2021-01-17 19:46:08 +00:00
|
|
|
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($server)
|
|
|
|
->putContent($request->get('file'), $request->getContent());
|
|
|
|
});
|
2019-05-27 22:30:49 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2019-05-27 22:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 04:45:39 +00:00
|
|
|
/**
|
|
|
|
* Creates a new folder on the server.
|
|
|
|
*
|
2021-01-17 19:46:08 +00:00
|
|
|
* @throws \Throwable
|
2019-05-02 04:45:39 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function create(CreateFolderRequest $request, Server $server): Response
|
2019-05-02 04:45:39 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:52:44 +00:00
|
|
|
$audit->subaction = 'create_folder';
|
|
|
|
$audit->metadata = ['file' => $request->input('root', '/') . $request->input('name')];
|
2021-01-17 19:46:08 +00:00
|
|
|
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($server)
|
|
|
|
->createDirectory($request->input('name'), $request->input('root', '/'));
|
|
|
|
});
|
2019-05-02 04:45:39 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2019-05-04 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames a file on the remote machine.
|
|
|
|
*
|
2021-01-17 19:46:08 +00:00
|
|
|
* @throws \Throwable
|
2019-05-04 23:04:59 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function rename(RenameFileRequest $request, Server $server): Response
|
2019-05-04 23:04:59 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_RENAME, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
|
|
|
|
|
|
|
|
$this->fileRepository
|
|
|
|
->setServer($server)
|
|
|
|
->renameFiles($request->input('root'), $request->input('files'));
|
|
|
|
});
|
2019-05-05 00:26:24 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2019-05-05 00:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a file on the server.
|
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Throwable
|
2019-05-05 00:26:24 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function copy(CopyFileRequest $request, Server $server): Response
|
2019-05-05 00:26:24 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_WRITE, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:52:44 +00:00
|
|
|
$audit->subaction = 'copy_file';
|
|
|
|
$audit->metadata = ['file' => $request->input('location')];
|
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
$this->fileRepository
|
|
|
|
->setServer($server)
|
|
|
|
->copyFile($request->input('location'));
|
|
|
|
});
|
2019-05-05 00:26:24 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-07-11 20:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Throwable
|
2020-07-11 20:38:49 +00:00
|
|
|
*/
|
2020-07-11 23:00:30 +00:00
|
|
|
public function compress(CompressFilesRequest $request, Server $server): array
|
2020-07-11 20:38:49 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$file = $server->audit(AuditLog::SERVER__FILESYSTEM_COMPRESS, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
// Allow up to five minutes for this request to process before timing out.
|
|
|
|
set_time_limit(300);
|
2020-07-15 04:16:38 +00:00
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
|
|
|
|
|
|
|
|
return $this->fileRepository->setServer($server)
|
|
|
|
->compressFiles(
|
2021-01-26 03:20:51 +00:00
|
|
|
$request->input('root'),
|
|
|
|
$request->input('files')
|
2021-01-17 19:46:08 +00:00
|
|
|
);
|
|
|
|
});
|
2020-07-11 20:38:49 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($file)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(FileObjectTransformer::class)
|
2020-07-11 20:38:49 +00:00
|
|
|
->toArray();
|
2019-05-05 00:26:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 04:16:49 +00:00
|
|
|
/**
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Throwable
|
2020-07-15 04:16:49 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function decompress(DecompressFilesRequest $request, Server $server): Response
|
2020-07-15 04:16:49 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$file = $server->audit(AuditLog::SERVER__FILESYSTEM_DECOMPRESS, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
// Allow up to five minutes for this request to process before timing out.
|
|
|
|
set_time_limit(300);
|
2020-07-15 04:16:49 +00:00
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('file')];
|
|
|
|
|
|
|
|
$this->fileRepository->setServer($server)
|
|
|
|
->decompressFile($request->input('root'), $request->input('file'));
|
|
|
|
});
|
2020-07-15 04:16:49 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-07-15 04:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 00:26:24 +00:00
|
|
|
/**
|
2020-07-11 22:37:59 +00:00
|
|
|
* Deletes files or folders for the server in the given root directory.
|
2019-05-05 00:26:24 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Throwable
|
2019-05-05 00:26:24 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteFileRequest $request, Server $server): Response
|
2019-05-05 00:26:24 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_DELETE, function (AuditLog $audit, Server $server) use ($request) {
|
2021-01-17 19:46:08 +00:00
|
|
|
$audit->metadata = ['root' => $request->input('root'), 'files' => $request->input('files')];
|
|
|
|
|
|
|
|
$this->fileRepository->setServer($server)
|
|
|
|
->deleteFiles(
|
2021-01-26 03:20:51 +00:00
|
|
|
$request->input('root'),
|
|
|
|
$request->input('files')
|
2021-01-17 19:46:08 +00:00
|
|
|
);
|
|
|
|
});
|
2019-05-04 23:04:59 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2019-05-02 04:45:39 +00:00
|
|
|
}
|
2020-11-07 04:47:03 +00:00
|
|
|
|
2020-11-29 20:49:29 +00:00
|
|
|
/**
|
|
|
|
* Updates file permissions for file(s) in the given root directory.
|
|
|
|
*
|
2021-07-25 19:24:52 +00:00
|
|
|
* @throws \Throwable
|
2020-11-29 20:49:29 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function chmod(ChmodFilesRequest $request, Server $server): Response
|
2020-11-29 20:49:29 +00:00
|
|
|
{
|
2021-07-25 19:24:52 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_CHMOD, function (AuditLog $audit, Server $server) use ($request) {
|
|
|
|
$audit->metadata = ['directory' => $request->input('root'), 'files' => $request->input('files')];
|
|
|
|
|
|
|
|
$this->fileRepository->setServer($server)
|
|
|
|
->chmodFiles(
|
|
|
|
$request->input('root'),
|
|
|
|
$request->input('files'),
|
|
|
|
);
|
|
|
|
});
|
2020-11-29 20:49:29 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-11-29 20:49:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 04:47:03 +00:00
|
|
|
/**
|
2020-12-24 17:15:03 +00:00
|
|
|
* Requests that a file be downloaded from a remote location by Wings.
|
|
|
|
*
|
2021-01-17 19:46:08 +00:00
|
|
|
* @throws \Throwable
|
2020-11-07 04:47:03 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function pull(PullFileRequest $request, Server $server): Response
|
2020-11-07 04:47:03 +00:00
|
|
|
{
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__FILESYSTEM_PULL, function (AuditLog $audit, Server $server) use ($request) {
|
2021-07-25 19:24:52 +00:00
|
|
|
$audit->metadata = ['directory' => $request->input('root'), 'url' => $request->input('url')];
|
2021-01-17 19:46:08 +00:00
|
|
|
|
2021-07-25 19:24:52 +00:00
|
|
|
$this->fileRepository->setServer($server)
|
|
|
|
->pull(
|
|
|
|
$request->input('root'),
|
|
|
|
$request->input('url'),
|
|
|
|
);
|
2021-01-17 19:46:08 +00:00
|
|
|
});
|
2020-12-24 17:15:03 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-11-07 04:47:03 +00:00
|
|
|
}
|
2019-03-17 00:10:04 +00:00
|
|
|
}
|