From f7122ec520e7741e347ad926034f1b49471d6c55 Mon Sep 17 00:00:00 2001
From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com>
Date: Tue, 25 Mar 2025 18:28:01 +0100
Subject: [PATCH] refactor(proxy): improve port availability checks with
multiple methods
---
app/Actions/Proxy/CheckProxy.php | 46 ++++++++++++++++++++++----------
1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/app/Actions/Proxy/CheckProxy.php b/app/Actions/Proxy/CheckProxy.php
index 6c8dd5234..27608547a 100644
--- a/app/Actions/Proxy/CheckProxy.php
+++ b/app/Actions/Proxy/CheckProxy.php
@@ -30,10 +30,6 @@ public function handle(Server $server, $fromUI = false): bool
if (is_null($proxyType) || $proxyType === 'NONE' || $server->proxy->force_stop) {
return false;
}
- ['uptime' => $uptime, 'error' => $error] = $server->validateConnection();
- if (! $uptime) {
- throw new \Exception($error);
- }
if (! $server->isProxyShouldRun()) {
if ($fromUI) {
throw new \Exception('Proxy should not run. You selected the Custom Proxy.');
@@ -68,6 +64,38 @@ public function handle(Server $server, $fromUI = false): bool
$portsToCheck = ['80', '443'];
+ foreach ($portsToCheck as $port) {
+ // Try multiple methods to check port availability
+ $commands = [
+ // Method 1: Check /proc/net/tcp directly (convert port to hex)
+ "cat /proc/net/tcp | grep -q '00000000:".str_pad(dechex($port), 4, '0', STR_PAD_LEFT)."'",
+ // Method 2: Use ss command (modern alternative to netstat)
+ "ss -tuln | grep -q ':$port '",
+ // Method 3: Use lsof if available
+ "lsof -i :$port >/dev/null 2>&1",
+ // Method 4: Use fuser if available
+ "fuser $port/tcp >/dev/null 2>&1",
+ ];
+
+ $portInUse = false;
+ foreach ($commands as $command) {
+ try {
+ instant_remote_process([$command], $server);
+ $portInUse = true;
+ break;
+ } catch (\Throwable $e) {
+
+ continue;
+ }
+ }
+ if ($portInUse) {
+ 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://coolify.io/discord");
+ } else {
+ return false;
+ }
+ }
+ }
try {
if ($server->proxyType() !== ProxyTypes::NONE->value) {
$proxyCompose = CheckConfiguration::run($server);
@@ -94,16 +122,6 @@ public function handle(Server $server, $fromUI = false): bool
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;
- }
- }
- }
return true;
}