|
2 | 2 |
|
3 | 3 | namespace App\Actions\Proxy;
|
4 | 4 |
|
| 5 | +use App\Enums\ProxyTypes; |
5 | 6 | use App\Models\Server;
|
6 | 7 | use Lorisleiva\Actions\Concerns\AsAction;
|
| 8 | +use Symfony\Component\Yaml\Yaml; |
7 | 9 |
|
8 | 10 | class CheckProxy
|
9 | 11 | {
|
10 | 12 | use AsAction;
|
11 | 13 |
|
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 |
13 | 16 | {
|
14 | 17 | if (! $server->isFunctional()) {
|
15 | 18 | return false;
|
@@ -62,22 +65,42 @@ public function handle(Server $server, $fromUI = false)
|
62 | 65 | $ip = 'host.docker.internal';
|
63 | 66 | }
|
64 | 67 |
|
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 | + } |
72 | 87 | } else {
|
73 |
| - return false; |
| 88 | + $portsToCheck = []; |
74 | 89 | }
|
| 90 | + } catch (\Exception $e) { |
| 91 | + ray($e->getMessage()); |
75 | 92 | }
|
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 | + } |
81 | 104 | }
|
82 | 105 | }
|
83 | 106 |
|
|
0 commit comments