Skip to content

Commit 5f07b47

Browse files
committed
fix: parse proxy config and check the set ports usage
1 parent 5bcd813 commit 5f07b47

File tree

6 files changed

+59
-24
lines changed

6 files changed

+59
-24
lines changed

app/Actions/Proxy/CheckProxy.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace App\Actions\Proxy;
44

5+
use App\Enums\ProxyTypes;
56
use App\Models\Server;
67
use Lorisleiva\Actions\Concerns\AsAction;
8+
use Symfony\Component\Yaml\Yaml;
79

810
class CheckProxy
911
{
1012
use AsAction;
1113

12-
public function handle(Server $server, $fromUI = false)
14+
// It should return if the proxy should be started (true) or not (false)
15+
public function handle(Server $server, $fromUI = false): bool
1316
{
1417
if (! $server->isFunctional()) {
1518
return false;
@@ -62,22 +65,42 @@ public function handle(Server $server, $fromUI = false)
6265
$ip = 'host.docker.internal';
6366
}
6467

65-
$connection80 = @fsockopen($ip, '80');
66-
$connection443 = @fsockopen($ip, '443');
67-
$port80 = is_resource($connection80) && fclose($connection80);
68-
$port443 = is_resource($connection443) && fclose($connection443);
69-
if ($port80) {
70-
if ($fromUI) {
71-
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>");
68+
$portsToCheck = ['80', '443'];
69+
70+
try {
71+
if ($server->proxyType() !== ProxyTypes::NONE->value) {
72+
$proxyCompose = CheckConfiguration::run($server);
73+
if (isset($proxyCompose)) {
74+
$yaml = Yaml::parse($proxyCompose);
75+
$portsToCheck = [];
76+
if ($server->proxyType() === ProxyTypes::TRAEFIK->value) {
77+
$ports = data_get($yaml, 'services.traefik.ports');
78+
} elseif ($server->proxyType() === ProxyTypes::CADDY->value) {
79+
$ports = data_get($yaml, 'services.caddy.ports');
80+
}
81+
if (isset($ports)) {
82+
foreach ($ports as $port) {
83+
$portsToCheck[] = str($port)->before(':')->value();
84+
}
85+
}
86+
}
7287
} else {
73-
return false;
88+
$portsToCheck = [];
7489
}
90+
} catch (\Exception $e) {
91+
ray($e->getMessage());
7592
}
76-
if ($port443) {
77-
if ($fromUI) {
78-
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>");
79-
} else {
80-
return false;
93+
if (count($portsToCheck) === 0) {
94+
return false;
95+
}
96+
foreach ($portsToCheck as $port) {
97+
$connection = @fsockopen($ip, $port);
98+
if (is_resource($connection) && fclose($connection)) {
99+
if ($fromUI) {
100+
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>");
101+
} else {
102+
return false;
103+
}
81104
}
82105
}
83106

app/Livewire/Server/Proxy/Status.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use App\Actions\Docker\GetContainersStatus;
66
use App\Actions\Proxy\CheckProxy;
7-
use App\Jobs\ContainerStatusJob;
7+
use App\Actions\Proxy\StartProxy;
88
use App\Models\Server;
99
use Livewire\Component;
1010

@@ -44,7 +44,10 @@ public function checkProxy(bool $notification = false)
4444
}
4545
$this->numberOfPolls++;
4646
}
47-
CheckProxy::run($this->server, true);
47+
$shouldStart = CheckProxy::run($this->server, true);
48+
if ($shouldStart) {
49+
StartProxy::run($this->server, false);
50+
}
4851
$this->dispatch('proxyStatusUpdated');
4952
if ($this->server->proxy->status === 'running') {
5053
$this->polling = false;

resources/views/components/status/restarting.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
'status' => 'Restarting',
33
'lastDeploymentInfo' => null,
44
'lastDeploymentLink' => null,
5+
'noLoading' => false,
56
])
67
<div class="flex items-center">
7-
<x-loading wire:loading.delay.longer />
8+
@if (!$noLoading)
9+
<x-loading wire:loading.delay.longer />
10+
@endif
811
<span wire:loading.remove.delay.longer class="flex items-center">
912
<div class="badge badge-warning "></div>
1013
<div class="pl-2 pr-1 text-xs font-bold tracking-wider dark:text-warning" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif>

resources/views/components/status/running.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
'status' => 'Running',
33
'lastDeploymentInfo' => null,
44
'lastDeploymentLink' => null,
5+
'noLoading' => false,
56
])
67
<div class="flex items-center">
7-
<x-loading wire:loading.delay.longer />
8+
@if (!$noLoading)
9+
<x-loading wire:loading.delay.longer />
10+
@endif
811
<span wire:loading.remove.delay.longer class="flex items-center">
912
<div class="badge badge-success "></div>
1013
<div class="pl-2 pr-1 text-xs font-bold tracking-wider text-success" @if($lastDeploymentInfo) title="{{$lastDeploymentInfo}}" @endif>

resources/views/components/status/stopped.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
@props([
22
'status' => 'Stopped',
3+
'noLoading' => false,
34
])
45
<div class="flex items-center">
5-
<x-loading wire:loading.delay.longer />
6+
@if (!$noLoading)
7+
<x-loading wire:loading.delay.longer />
8+
@endif
69
<span wire:loading.remove.delay.longer class="flex items-center">
710
<div class="badge badge-error "></div>
811
<div class="pl-2 pr-1 text-xs font-bold tracking-wider text-error">{{ str($status)->before(':')->headline() }}</div>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<div x-init="$wire.checkProxy()" class="flex gap-2">
22
@if (data_get($server, 'proxy.status') === 'running')
3-
<x-status.running status="Proxy Running" />
3+
<x-status.running status="Proxy Running" noLoading />
44
@elseif (data_get($server, 'proxy.status') === 'restarting')
5-
<x-status.restarting status="Proxy Restarting" />
5+
<x-status.restarting status="Proxy Restarting" noLoading />
66
@elseif (data_get($server, 'proxy.force_stop'))
7-
<x-status.stopped status="Proxy Stopped" />
7+
<x-status.stopped status="Proxy Stopped" noLoading />
88
@elseif (data_get($server, 'proxy.status') === 'exited')
9-
<x-status.stopped status="Proxy Exited" />
9+
<x-status.stopped status="Proxy Exited" noLoading />
1010
@else
11-
<x-status.stopped status="Proxy Not Running" />
11+
<x-status.stopped status="Proxy Not Running" noLoading />
1212
@endif
1313
<x-forms.button wire:click='checkProxy(true)'>Refresh</x-forms.button>
1414
</div>

0 commit comments

Comments
 (0)