2023-10-13 12:25:30 +00:00
< ? php
namespace App\Actions\Proxy ;
use App\Models\Server ;
use Lorisleiva\Actions\Concerns\AsAction ;
class CheckProxy
{
use AsAction ;
2024-06-10 20:43:34 +00:00
2023-10-17 17:00:23 +00:00
public function handle ( Server $server , $fromUI = false )
2023-10-13 12:25:30 +00:00
{
2024-06-10 20:43:34 +00:00
if ( ! $server -> isFunctional ()) {
2024-05-22 10:20:36 +00:00
return false ;
}
2024-05-24 09:17:23 +00:00
if ( $server -> isBuildServer ()) {
if ( $server -> proxy ) {
$server -> proxy = null ;
$server -> save ();
}
2024-06-10 20:43:34 +00:00
2024-05-24 09:17:23 +00:00
return false ;
}
2024-05-22 18:42:08 +00:00
$proxyType = $server -> proxyType ();
if ( is_null ( $proxyType ) || $proxyType === 'NONE' || $server -> proxy -> force_stop ) {
2024-03-11 14:08:05 +00:00
return false ;
}
2024-04-16 13:42:38 +00:00
[ 'uptime' => $uptime , 'error' => $error ] = $server -> validateConnection ();
2024-06-10 20:43:34 +00:00
if ( ! $uptime ) {
2024-04-16 13:42:38 +00:00
throw new \Exception ( $error );
2024-04-09 06:45:37 +00:00
}
2024-06-10 20:43:34 +00:00
if ( ! $server -> isProxyShouldRun ()) {
2023-10-17 17:00:23 +00:00
if ( $fromUI ) {
2024-06-10 20:43:34 +00:00
throw new \Exception ( 'Proxy should not run. You selected the Custom Proxy.' );
2023-10-17 17:00:23 +00:00
} else {
return false ;
}
2023-10-13 12:25:30 +00:00
}
2023-11-29 13:59:06 +00:00
if ( $server -> isSwarm ()) {
$status = getContainerStatus ( $server , 'coolify-proxy_traefik' );
$server -> proxy -> set ( 'status' , $status );
2023-10-13 12:25:30 +00:00
$server -> save ();
2023-12-19 12:47:12 +00:00
if ( $status === 'running' ) {
return false ;
}
2024-06-10 20:43:34 +00:00
2023-12-19 12:47:12 +00:00
return true ;
2023-11-29 13:59:06 +00:00
} else {
$status = getContainerStatus ( $server , 'coolify-proxy' );
if ( $status === 'running' ) {
$server -> proxy -> set ( 'status' , 'running' );
$server -> save ();
2024-06-10 20:43:34 +00:00
2023-10-17 17:00:23 +00:00
return false ;
}
2024-03-27 17:24:24 +00:00
if ( $server -> settings -> is_cloudflare_tunnel ) {
return false ;
}
2023-11-29 13:59:06 +00:00
$ip = $server -> ip ;
if ( $server -> id === 0 ) {
$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 ) {
2024-03-18 07:53:44 +00:00
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> " );
2023-11-29 13:59:06 +00:00
} else {
return false ;
}
}
if ( $port443 ) {
if ( $fromUI ) {
2024-03-18 07:53:44 +00:00
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> " );
2023-11-29 13:59:06 +00:00
} else {
return false ;
}
2023-10-17 17:00:23 +00:00
}
2024-06-10 20:43:34 +00:00
2023-11-29 13:59:06 +00:00
return true ;
2023-10-13 12:25:30 +00:00
}
}
}