2023-10-13 12:25:30 +00:00
< ? php
namespace App\Actions\Proxy ;
2024-10-03 19:29:55 +00:00
use App\Enums\ProxyTypes ;
2023-10-13 12:25:30 +00:00
use App\Models\Server ;
2024-11-06 14:16:12 +00:00
use Illuminate\Support\Facades\Log ;
2023-10-13 12:25:30 +00:00
use Lorisleiva\Actions\Concerns\AsAction ;
2024-10-03 19:29:55 +00:00
use Symfony\Component\Yaml\Yaml ;
2023-10-13 12:25:30 +00:00
class CheckProxy
{
use AsAction ;
2024-06-10 20:43:34 +00:00
2024-10-03 19:29:55 +00:00
// It should return if the proxy should be started (true) or not (false)
public function handle ( Server $server , $fromUI = false ) : bool
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-11-22 14:50:52 +00:00
[ 'uptime' => $uptime , 'error' => $error ] = $server -> validateConnection ();
2024-06-10 20:43:34 +00:00
if ( ! $uptime ) {
2025-01-07 14:31:43 +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 ) {
2025-01-07 14:31:43 +00:00
throw new \Exception ( 'Proxy should not run. You selected the Custom Proxy.' );
} else {
return false ;
2023-10-17 17:00:23 +00:00
}
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 ();
2025-01-07 14:31:43 +00:00
if ( $status === 'running' ) {
return false ;
}
2023-11-29 13:59:06 +00:00
2025-01-07 14:31:43 +00:00
return true ;
} else {
$status = getContainerStatus ( $server , 'coolify-proxy' );
if ( $status === 'running' ) {
$server -> proxy -> set ( 'status' , 'running' );
$server -> save ();
2024-10-03 19:29:55 +00:00
2025-01-07 14:31:43 +00:00
return false ;
}
if ( $server -> settings -> is_cloudflare_tunnel ) {
return false ;
}
$ip = $server -> ip ;
if ( $server -> id === 0 ) {
$ip = 'host.docker.internal' ;
}
$portsToCheck = [ '80' , '443' ];
try {
if ( $server -> proxyType () !== ProxyTypes :: NONE -> value ) {
$proxyCompose = CheckConfiguration :: run ( $server );
if ( isset ( $proxyCompose )) {
$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 ();
}
2024-10-03 19:29:55 +00:00
}
}
2025-01-07 14:31:43 +00:00
} else {
$portsToCheck = [];
2023-11-29 13:59:06 +00:00
}
2025-01-07 14:31:43 +00:00
} catch ( \Exception $e ) {
Log :: error ( 'Error checking proxy: ' . $e -> getMessage ());
2024-10-03 19:29:55 +00:00
}
2025-01-07 14:31:43 +00:00
if ( count ( $portsToCheck ) === 0 ) {
2025-01-07 13:52:08 +00:00
return false ;
}
2025-01-07 14:31:43 +00:00
foreach ( $portsToCheck as $port ) {
$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 ;
}
}
}
2025-01-07 13:52:08 +00:00
2025-01-07 14:31:43 +00:00
return true ;
}
2023-10-13 12:25:30 +00:00
}
}