2023-04-25 08:06:45 +00:00
< ? php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Settings ;
2023-04-25 08:06:45 +00:00
use App\Models\InstanceSettings as ModelsInstanceSettings ;
2023-05-25 10:00:09 +00:00
use App\Models\Server ;
2023-04-25 08:06:45 +00:00
use Livewire\Component ;
2023-06-07 20:07:26 +00:00
class Configuration extends Component
2023-04-25 08:06:45 +00:00
{
public ModelsInstanceSettings $settings ;
2023-06-23 11:13:02 +00:00
public bool $do_not_track ;
public bool $is_auto_update_enabled ;
public bool $is_registration_enabled ;
2024-01-15 09:03:15 +00:00
public bool $is_dns_validation_enabled ;
2024-05-17 07:52:19 +00:00
// public bool $next_channel;
2023-06-22 12:18:17 +00:00
protected string $dynamic_config_path = '/data/coolify/proxy/dynamic' ;
2023-05-31 07:22:08 +00:00
protected Server $server ;
2023-04-25 08:06:45 +00:00
protected $rules = [
'settings.fqdn' => 'nullable' ,
2023-07-14 09:27:08 +00:00
'settings.resale_license' => 'nullable' ,
2023-04-25 08:06:45 +00:00
'settings.public_port_min' => 'required' ,
'settings.public_port_max' => 'required' ,
2024-01-15 09:03:15 +00:00
'settings.custom_dns_servers' => 'nullable' ,
2023-04-25 08:06:45 +00:00
];
2023-06-16 10:35:40 +00:00
protected $validationAttributes = [
'settings.fqdn' => 'FQDN' ,
2023-07-14 09:27:08 +00:00
'settings.resale_license' => 'Resale License' ,
2023-06-16 10:35:40 +00:00
'settings.public_port_min' => 'Public port min' ,
'settings.public_port_max' => 'Public port max' ,
2024-01-15 09:03:15 +00:00
'settings.custom_dns_servers' => 'Custom DNS servers' ,
2023-06-16 10:35:40 +00:00
];
2023-08-08 09:51:36 +00:00
2023-04-25 08:06:45 +00:00
public function mount ()
{
$this -> do_not_track = $this -> settings -> do_not_track ;
$this -> is_auto_update_enabled = $this -> settings -> is_auto_update_enabled ;
$this -> is_registration_enabled = $this -> settings -> is_registration_enabled ;
2024-05-17 07:52:19 +00:00
// $this->next_channel = $this->settings->next_channel;
2024-01-15 09:03:15 +00:00
$this -> is_dns_validation_enabled = $this -> settings -> is_dns_validation_enabled ;
2023-04-25 08:06:45 +00:00
}
2023-08-08 09:51:36 +00:00
2023-04-25 08:06:45 +00:00
public function instantSave ()
{
$this -> settings -> do_not_track = $this -> do_not_track ;
$this -> settings -> is_auto_update_enabled = $this -> is_auto_update_enabled ;
$this -> settings -> is_registration_enabled = $this -> is_registration_enabled ;
2024-01-15 09:03:15 +00:00
$this -> settings -> is_dns_validation_enabled = $this -> is_dns_validation_enabled ;
2024-05-17 07:52:19 +00:00
// if ($this->next_channel) {
// $this->settings->next_channel = false;
// $this->next_channel = false;
// } else {
// $this->settings->next_channel = $this->next_channel;
// }
2023-04-25 08:06:45 +00:00
$this -> settings -> save ();
2023-12-07 18:06:32 +00:00
$this -> dispatch ( 'success' , 'Settings updated!' );
2023-04-25 08:06:45 +00:00
}
2023-08-08 09:51:36 +00:00
public function submit ()
{
2024-04-08 10:15:44 +00:00
try {
$error_show = false ;
$this -> server = Server :: findOrFail ( 0 );
$this -> resetErrorBag ();
if ( $this -> settings -> public_port_min > $this -> settings -> public_port_max ) {
$this -> addError ( 'settings.public_port_min' , 'The minimum port must be lower than the maximum port.' );
return ;
}
$this -> validate ();
2024-01-15 09:03:15 +00:00
2024-04-17 16:49:31 +00:00
if ( $this -> settings -> is_dns_validation_enabled && $this -> settings -> fqdn ) {
2024-04-08 10:15:44 +00:00
if ( ! validate_dns_entry ( $this -> settings -> fqdn , $this -> server )) {
2024-05-30 10:28:29 +00:00
$this -> dispatch ( 'error' , " Validating DNS failed.<br><br>Make sure you have added the DNS records correctly.<br><br> { $this -> settings -> fqdn } -> { $this -> server -> ip } <br><br>Check this <a target='_blank' class='underline dark:text-white' href='https://coolify.io/docs/knowledge-base/dns-configuration'>documentation</a> for further help. " );
2024-04-08 10:15:44 +00:00
$error_show = true ;
}
}
2024-04-17 16:49:31 +00:00
if ( $this -> settings -> fqdn ) check_domain_usage ( domain : $this -> settings -> fqdn );
2024-04-08 10:15:44 +00:00
$this -> settings -> custom_dns_servers = str ( $this -> settings -> custom_dns_servers ) -> replaceEnd ( ',' , '' ) -> trim ();
$this -> settings -> custom_dns_servers = str ( $this -> settings -> custom_dns_servers ) -> trim () -> explode ( ',' ) -> map ( function ( $dns ) {
return str ( $dns ) -> trim () -> lower ();
});
$this -> settings -> custom_dns_servers = $this -> settings -> custom_dns_servers -> unique ();
$this -> settings -> custom_dns_servers = $this -> settings -> custom_dns_servers -> implode ( ',' );
2024-01-15 09:03:15 +00:00
2024-04-08 10:15:44 +00:00
$this -> settings -> save ();
$this -> server -> setupDynamicProxyConfiguration ();
if ( ! $error_show ) {
$this -> dispatch ( 'success' , 'Instance settings updated successfully!' );
}
} catch ( \Exception $e ) {
return handleError ( $e , $this );
}
2023-05-31 07:22:08 +00:00
}
2023-04-25 08:06:45 +00:00
}