fix: parse proxy config and check the set ports usage

This commit is contained in:
Andras Bacsai
2024-10-03 21:29:55 +02:00
parent 5bcd813792
commit 5f07b473e9
6 changed files with 59 additions and 24 deletions

View File

@@ -2,14 +2,17 @@
namespace App\Actions\Proxy; namespace App\Actions\Proxy;
use App\Enums\ProxyTypes;
use App\Models\Server; use App\Models\Server;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
use Symfony\Component\Yaml\Yaml;
class CheckProxy class CheckProxy
{ {
use AsAction; 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()) { if (! $server->isFunctional()) {
return false; return false;
@@ -62,22 +65,42 @@ class CheckProxy
$ip = 'host.docker.internal'; $ip = 'host.docker.internal';
} }
$connection80 = @fsockopen($ip, '80'); $portsToCheck = ['80', '443'];
$connection443 = @fsockopen($ip, '443');
$port80 = is_resource($connection80) && fclose($connection80); try {
$port443 = is_resource($connection443) && fclose($connection443); if ($server->proxyType() !== ProxyTypes::NONE->value) {
if ($port80) { $proxyCompose = CheckConfiguration::run($server);
if ($fromUI) { if (isset($proxyCompose)) {
throw new \Exception("Port 80 is in use.<br>You must stop the process using this port.<br>Docs: <a target='_blank' href='https://coolify.io/docs'>https://coolify.io/docs</a><br>Discord: <a target='_blank' href='https://coollabs.io/discord'>https://coollabs.io/discord</a>"); $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 { } else {
return false; $portsToCheck = [];
} }
} catch (\Exception $e) {
ray($e->getMessage());
} }
if ($port443) { if (count($portsToCheck) === 0) {
if ($fromUI) { return false;
throw new \Exception("Port 443 is in use.<br>You must stop the process using this port.<br>Docs: <a target='_blank' href='https://coolify.io/docs'>https://coolify.io/docs</a><br>Discord: <a target='_blank' href='https://coollabs.io/discord'>https://coollabs.io/discord</a>"); }
} else { foreach ($portsToCheck as $port) {
return false; $connection = @fsockopen($ip, $port);
if (is_resource($connection) && fclose($connection)) {
if ($fromUI) {
throw new \Exception("Port $port is in use.<br>You must stop the process using this port.<br>Docs: <a target='_blank' href='https://coolify.io/docs'>https://coolify.io/docs</a><br>Discord: <a target='_blank' href='https://coollabs.io/discord'>https://coollabs.io/discord</a>");
} else {
return false;
}
} }
} }

View File

@@ -4,7 +4,7 @@ namespace App\Livewire\Server\Proxy;
use App\Actions\Docker\GetContainersStatus; use App\Actions\Docker\GetContainersStatus;
use App\Actions\Proxy\CheckProxy; use App\Actions\Proxy\CheckProxy;
use App\Jobs\ContainerStatusJob; use App\Actions\Proxy\StartProxy;
use App\Models\Server; use App\Models\Server;
use Livewire\Component; use Livewire\Component;
@@ -44,7 +44,10 @@ class Status extends Component
} }
$this->numberOfPolls++; $this->numberOfPolls++;
} }
CheckProxy::run($this->server, true); $shouldStart = CheckProxy::run($this->server, true);
if ($shouldStart) {
StartProxy::run($this->server, false);
}
$this->dispatch('proxyStatusUpdated'); $this->dispatch('proxyStatusUpdated');
if ($this->server->proxy->status === 'running') { if ($this->server->proxy->status === 'running') {
$this->polling = false; $this->polling = false;

View File

@@ -2,9 +2,12 @@
'status' => 'Restarting', 'status' => 'Restarting',
'lastDeploymentInfo' => null, 'lastDeploymentInfo' => null,
'lastDeploymentLink' => null, 'lastDeploymentLink' => null,
'noLoading' => false,
]) ])
<div class="flex items-center"> <div class="flex items-center">
<x-loading wire:loading.delay.longer /> @if (!$noLoading)
<x-loading wire:loading.delay.longer />
@endif
<span wire:loading.remove.delay.longer class="flex items-center"> <span wire:loading.remove.delay.longer class="flex items-center">
<div class="badge badge-warning "></div> <div class="badge badge-warning "></div>
<div class="pl-2 pr-1 text-xs font-bold tracking-wider dark:text-warning" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif> <div class="pl-2 pr-1 text-xs font-bold tracking-wider dark:text-warning" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif>

View File

@@ -2,9 +2,12 @@
'status' => 'Running', 'status' => 'Running',
'lastDeploymentInfo' => null, 'lastDeploymentInfo' => null,
'lastDeploymentLink' => null, 'lastDeploymentLink' => null,
'noLoading' => false,
]) ])
<div class="flex items-center"> <div class="flex items-center">
<x-loading wire:loading.delay.longer /> @if (!$noLoading)
<x-loading wire:loading.delay.longer />
@endif
<span wire:loading.remove.delay.longer class="flex items-center"> <span wire:loading.remove.delay.longer class="flex items-center">
<div class="badge badge-success "></div> <div class="badge badge-success "></div>
<div class="pl-2 pr-1 text-xs font-bold tracking-wider text-success" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif> <div class="pl-2 pr-1 text-xs font-bold tracking-wider text-success" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif>

View File

@@ -1,8 +1,11 @@
@props([ @props([
'status' => 'Stopped', 'status' => 'Stopped',
'noLoading' => false,
]) ])
<div class="flex items-center"> <div class="flex items-center">
<x-loading wire:loading.delay.longer /> @if (!$noLoading)
<x-loading wire:loading.delay.longer />
@endif
<span wire:loading.remove.delay.longer class="flex items-center"> <span wire:loading.remove.delay.longer class="flex items-center">
<div class="badge badge-error "></div> <div class="badge badge-error "></div>
<div class="pl-2 pr-1 text-xs font-bold tracking-wider text-error">{{ str($status)->before(':')->headline() }}</div> <div class="pl-2 pr-1 text-xs font-bold tracking-wider text-error">{{ str($status)->before(':')->headline() }}</div>

View File

@@ -1,14 +1,14 @@
<div x-init="$wire.checkProxy()" class="flex gap-2"> <div x-init="$wire.checkProxy()" class="flex gap-2">
@if (data_get($server, 'proxy.status') === 'running') @if (data_get($server, 'proxy.status') === 'running')
<x-status.running status="Proxy Running" /> <x-status.running status="Proxy Running" noLoading />
@elseif (data_get($server, 'proxy.status') === 'restarting') @elseif (data_get($server, 'proxy.status') === 'restarting')
<x-status.restarting status="Proxy Restarting" /> <x-status.restarting status="Proxy Restarting" noLoading />
@elseif (data_get($server, 'proxy.force_stop')) @elseif (data_get($server, 'proxy.force_stop'))
<x-status.stopped status="Proxy Stopped" /> <x-status.stopped status="Proxy Stopped" noLoading />
@elseif (data_get($server, 'proxy.status') === 'exited') @elseif (data_get($server, 'proxy.status') === 'exited')
<x-status.stopped status="Proxy Exited" /> <x-status.stopped status="Proxy Exited" noLoading />
@else @else
<x-status.stopped status="Proxy Not Running" /> <x-status.stopped status="Proxy Not Running" noLoading />
@endif @endif
<x-forms.button wire:click='checkProxy(true)'>Refresh</x-forms.button> <x-forms.button wire:click='checkProxy(true)'>Refresh</x-forms.button>
</div> </div>