From 5f07b473e9459bfc3267fd7c80c58fdeb9b67111 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 3 Oct 2024 21:29:55 +0200 Subject: [PATCH] fix: parse proxy config and check the set ports usage --- app/Actions/Proxy/CheckProxy.php | 51 ++++++++++++++----- app/Livewire/Server/Proxy/Status.php | 7 ++- .../components/status/restarting.blade.php | 5 +- .../views/components/status/running.blade.php | 5 +- .../views/components/status/stopped.blade.php | 5 +- .../livewire/server/proxy/status.blade.php | 10 ++-- 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/app/Actions/Proxy/CheckProxy.php b/app/Actions/Proxy/CheckProxy.php index cf0f6015c..03a0beddf 100644 --- a/app/Actions/Proxy/CheckProxy.php +++ b/app/Actions/Proxy/CheckProxy.php @@ -2,14 +2,17 @@ namespace App\Actions\Proxy; +use App\Enums\ProxyTypes; use App\Models\Server; use Lorisleiva\Actions\Concerns\AsAction; +use Symfony\Component\Yaml\Yaml; class CheckProxy { use AsAction; - public function handle(Server $server, $fromUI = false) + // It should return if the proxy should be started (true) or not (false) + public function handle(Server $server, $fromUI = false): bool { if (! $server->isFunctional()) { return false; @@ -62,22 +65,42 @@ class CheckProxy $ip = 'host.docker.internal'; } - $connection80 = @fsockopen($ip, '80'); - $connection443 = @fsockopen($ip, '443'); - $port80 = is_resource($connection80) && fclose($connection80); - $port443 = is_resource($connection443) && fclose($connection443); - if ($port80) { - if ($fromUI) { - throw new \Exception("Port 80 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); + $portsToCheck = ['80', '443']; + + try { + if ($server->proxyType() !== ProxyTypes::NONE->value) { + $proxyCompose = CheckConfiguration::run($server); + if (isset($proxyCompose)) { + $yaml = Yaml::parse($proxyCompose); + $portsToCheck = []; + if ($server->proxyType() === ProxyTypes::TRAEFIK->value) { + $ports = data_get($yaml, 'services.traefik.ports'); + } elseif ($server->proxyType() === ProxyTypes::CADDY->value) { + $ports = data_get($yaml, 'services.caddy.ports'); + } + if (isset($ports)) { + foreach ($ports as $port) { + $portsToCheck[] = str($port)->before(':')->value(); + } + } + } } else { - return false; + $portsToCheck = []; } + } catch (\Exception $e) { + ray($e->getMessage()); } - if ($port443) { - if ($fromUI) { - throw new \Exception("Port 443 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); - } else { - return false; + if (count($portsToCheck) === 0) { + return false; + } + foreach ($portsToCheck as $port) { + $connection = @fsockopen($ip, $port); + if (is_resource($connection) && fclose($connection)) { + if ($fromUI) { + throw new \Exception("Port $port is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); + } else { + return false; + } } } diff --git a/app/Livewire/Server/Proxy/Status.php b/app/Livewire/Server/Proxy/Status.php index 20db4dad4..f4f18381f 100644 --- a/app/Livewire/Server/Proxy/Status.php +++ b/app/Livewire/Server/Proxy/Status.php @@ -4,7 +4,7 @@ namespace App\Livewire\Server\Proxy; use App\Actions\Docker\GetContainersStatus; use App\Actions\Proxy\CheckProxy; -use App\Jobs\ContainerStatusJob; +use App\Actions\Proxy\StartProxy; use App\Models\Server; use Livewire\Component; @@ -44,7 +44,10 @@ class Status extends Component } $this->numberOfPolls++; } - CheckProxy::run($this->server, true); + $shouldStart = CheckProxy::run($this->server, true); + if ($shouldStart) { + StartProxy::run($this->server, false); + } $this->dispatch('proxyStatusUpdated'); if ($this->server->proxy->status === 'running') { $this->polling = false; diff --git a/resources/views/components/status/restarting.blade.php b/resources/views/components/status/restarting.blade.php index fc10a0a75..c9b6c2127 100644 --- a/resources/views/components/status/restarting.blade.php +++ b/resources/views/components/status/restarting.blade.php @@ -2,9 +2,12 @@ 'status' => 'Restarting', 'lastDeploymentInfo' => null, 'lastDeploymentLink' => null, + 'noLoading' => false, ])
- + @if (!$noLoading) + + @endif
diff --git a/resources/views/components/status/running.blade.php b/resources/views/components/status/running.blade.php index 2e0907a2a..8bb45b884 100644 --- a/resources/views/components/status/running.blade.php +++ b/resources/views/components/status/running.blade.php @@ -2,9 +2,12 @@ 'status' => 'Running', 'lastDeploymentInfo' => null, 'lastDeploymentLink' => null, + 'noLoading' => false, ])
- + @if (!$noLoading) + + @endif
diff --git a/resources/views/components/status/stopped.blade.php b/resources/views/components/status/stopped.blade.php index f168fb3c2..63bc2399d 100644 --- a/resources/views/components/status/stopped.blade.php +++ b/resources/views/components/status/stopped.blade.php @@ -1,8 +1,11 @@ @props([ 'status' => 'Stopped', + 'noLoading' => false, ])
- + @if (!$noLoading) + + @endif
{{ str($status)->before(':')->headline() }}
diff --git a/resources/views/livewire/server/proxy/status.blade.php b/resources/views/livewire/server/proxy/status.blade.php index 621e0bc8c..4dce706e8 100644 --- a/resources/views/livewire/server/proxy/status.blade.php +++ b/resources/views/livewire/server/proxy/status.blade.php @@ -1,14 +1,14 @@
@if (data_get($server, 'proxy.status') === 'running') - + @elseif (data_get($server, 'proxy.status') === 'restarting') - + @elseif (data_get($server, 'proxy.force_stop')) - + @elseif (data_get($server, 'proxy.status') === 'exited') - + @else - + @endif Refresh