2024-02-22 12:29:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire\Server\Proxy;
|
|
|
|
|
|
|
|
|
|
use App\Models\Server;
|
2026-02-27 10:41:01 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2024-02-22 12:29:28 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class DynamicConfigurations extends Component
|
|
|
|
|
{
|
2026-02-27 10:41:01 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2026-07-16 13:21:00 +00:00
|
|
|
public const MAX_CONFIGURATION_FILE_SIZE_BYTES = 1024 * 1024;
|
|
|
|
|
|
|
|
|
|
public const MAX_TOTAL_CONFIGURATION_SIZE_BYTES = 5 * 1024 * 1024;
|
|
|
|
|
|
|
|
|
|
public const MAX_CONFIGURATION_FILES = 100;
|
|
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public ?Server $server = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public $parameters = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public Collection $contents;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
public function getListeners()
|
|
|
|
|
{
|
|
|
|
|
$teamId = auth()->user()->currentTeam()->id;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-12 11:30:40 +00:00
|
|
|
return [
|
2025-06-06 12:47:54 +00:00
|
|
|
"echo-private:team.{$teamId},ProxyStatusChangedUI" => 'loadDynamicConfigurations',
|
2024-03-12 11:30:40 +00:00
|
|
|
'loadDynamicConfigurations',
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
protected $rules = [
|
|
|
|
|
'contents.*' => 'nullable|string',
|
|
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-06-06 16:50:32 +00:00
|
|
|
public function initLoadDynamicConfigurations()
|
|
|
|
|
{
|
|
|
|
|
$this->loadDynamicConfigurations();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public function loadDynamicConfigurations()
|
|
|
|
|
{
|
2026-02-27 10:41:01 +00:00
|
|
|
try {
|
|
|
|
|
$this->authorize('view', $this->server);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
2024-03-11 14:08:05 +00:00
|
|
|
$proxy_path = $this->server->proxyPath();
|
2026-07-16 13:21:00 +00:00
|
|
|
$fileLimit = self::MAX_CONFIGURATION_FILES + 1;
|
|
|
|
|
$files = instant_remote_process(["mkdir -p $proxy_path/dynamic && ls -1 {$proxy_path}/dynamic | head -n {$fileLimit}"], $this->server);
|
2025-01-07 14:31:43 +00:00
|
|
|
$files = collect(explode("\n", $files))->filter(fn ($file) => ! empty($file));
|
2024-02-22 12:29:28 +00:00
|
|
|
$files = $files->map(fn ($file) => trim($file));
|
|
|
|
|
$files = $files->sort();
|
|
|
|
|
$contents = collect([]);
|
2026-07-16 13:21:00 +00:00
|
|
|
$skippedFiles = collect([]);
|
|
|
|
|
$totalBytes = 0;
|
|
|
|
|
if ($files->count() > self::MAX_CONFIGURATION_FILES) {
|
|
|
|
|
$skippedFiles->push('additional files');
|
|
|
|
|
}
|
|
|
|
|
foreach ($files->take(self::MAX_CONFIGURATION_FILES) as $file) {
|
2024-02-22 12:29:28 +00:00
|
|
|
$without_extension = str_replace('.', '|', $file);
|
2026-07-16 13:21:00 +00:00
|
|
|
$filePath = escapeshellarg("{$proxy_path}/dynamic/{$file}");
|
|
|
|
|
$readLimit = self::MAX_CONFIGURATION_FILE_SIZE_BYTES + 1;
|
|
|
|
|
$content = instant_remote_process(["head -c {$readLimit} {$filePath}"], $this->server);
|
|
|
|
|
$content = $content ?? '';
|
|
|
|
|
$contentBytes = strlen($content);
|
|
|
|
|
|
|
|
|
|
if ($contentBytes > self::MAX_CONFIGURATION_FILE_SIZE_BYTES || $totalBytes + $contentBytes > self::MAX_TOTAL_CONFIGURATION_SIZE_BYTES) {
|
|
|
|
|
$skippedFiles->push($file);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contents[$without_extension] = $content;
|
|
|
|
|
$totalBytes += $contentBytes;
|
|
|
|
|
}
|
|
|
|
|
if ($skippedFiles->isNotEmpty()) {
|
|
|
|
|
$this->dispatch('warning', 'Some dynamic configurations were not loaded because they exceed the safe display limits: '.$skippedFiles->implode(', '));
|
2024-02-22 12:29:28 +00:00
|
|
|
}
|
|
|
|
|
$this->contents = $contents;
|
2024-08-05 18:08:37 +00:00
|
|
|
$this->dispatch('$refresh');
|
2025-06-06 16:50:32 +00:00
|
|
|
$this->dispatch('success', 'Dynamic configurations loaded.');
|
2024-02-22 12:29:28 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
|
|
|
|
$this->parameters = get_route_parameters();
|
|
|
|
|
try {
|
|
|
|
|
$this->server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->first();
|
|
|
|
|
if (is_null($this->server)) {
|
|
|
|
|
return redirect()->route('server.index');
|
|
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-02-22 12:29:28 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-22 12:29:28 +00:00
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.server.proxy.dynamic-configurations');
|
|
|
|
|
}
|
|
|
|
|
}
|