2024-01-07 15:23:41 +00:00
< ? php
namespace App\Livewire\Settings ;
2024-08-07 09:42:55 +00:00
use App\Jobs\CheckForUpdatesJob ;
2024-01-07 15:23:41 +00:00
use App\Models\InstanceSettings ;
2024-08-06 12:36:56 +00:00
use App\Models\Server ;
2025-01-07 13:52:08 +00:00
use Exception ;
2024-10-24 14:21:22 +00:00
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Hash ;
2024-12-03 23:30:39 +00:00
use Livewire\Attributes\Computed ;
2024-11-05 08:36:40 +00:00
use Livewire\Attributes\Validate ;
2024-01-07 15:23:41 +00:00
use Livewire\Component ;
class Index extends Component
{
public InstanceSettings $settings ;
2024-06-10 20:43:34 +00:00
2024-10-28 09:10:27 +00:00
protected Server $server ;
2024-06-10 20:43:34 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-08-06 12:36:56 +00:00
public bool $is_auto_update_enabled ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string|max:255')]
2024-10-28 09:10:27 +00:00
public ? string $fqdn = null ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('required|integer|min:1025|max:65535')]
2024-10-28 09:10:27 +00:00
public int $public_port_min ;
2024-11-05 08:36:40 +00:00
#[Validate('required|integer|min:1025|max:65535')]
2024-10-28 09:10:27 +00:00
public int $public_port_max ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $custom_dns_servers = null ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string|max:255')]
2024-10-28 09:10:27 +00:00
public ? string $instance_name = null ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $allowed_ips = null ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $public_ipv4 = null ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $public_ipv6 = null ;
2024-11-05 08:36:40 +00:00
#[Validate('string')]
2024-08-06 12:36:56 +00:00
public string $auto_update_frequency ;
2024-11-25 16:56:19 +00:00
#[Validate('string|required')]
2024-08-06 12:36:56 +00:00
public string $update_check_frequency ;
2024-11-05 08:36:40 +00:00
#[Validate('required|string|timezone')]
2024-10-28 09:10:27 +00:00
public string $instance_timezone ;
2024-10-17 10:07:35 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-10-28 09:10:27 +00:00
public bool $do_not_track ;
2024-10-17 10:07:35 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-10-28 09:10:27 +00:00
public bool $is_registration_enabled ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-10-28 09:10:27 +00:00
public bool $is_dns_validation_enabled ;
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-10-28 09:10:27 +00:00
public bool $is_api_enabled ;
2024-11-05 08:36:40 +00:00
#[Validate('boolean')]
2024-10-28 09:10:27 +00:00
public bool $disable_two_step_confirmation ;
2024-08-06 12:36:56 +00:00
2024-10-28 09:10:27 +00:00
public function render ()
{
return view ( 'livewire.settings.index' );
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function mount ()
{
2024-10-28 09:10:27 +00:00
if ( ! isInstanceAdmin ()) {
return redirect () -> route ( 'dashboard' );
2024-01-07 15:23:41 +00:00
}
2025-01-07 13:52:08 +00:00
$this -> settings = instanceSettings ();
$this -> fqdn = $this -> settings -> fqdn ;
$this -> public_port_min = $this -> settings -> public_port_min ;
$this -> public_port_max = $this -> settings -> public_port_max ;
$this -> custom_dns_servers = $this -> settings -> custom_dns_servers ;
$this -> instance_name = $this -> settings -> instance_name ;
$this -> allowed_ips = $this -> settings -> allowed_ips ;
$this -> public_ipv4 = $this -> settings -> public_ipv4 ;
$this -> public_ipv6 = $this -> settings -> public_ipv6 ;
$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 ;
$this -> is_dns_validation_enabled = $this -> settings -> is_dns_validation_enabled ;
$this -> is_api_enabled = $this -> settings -> is_api_enabled ;
$this -> auto_update_frequency = $this -> settings -> auto_update_frequency ;
$this -> update_check_frequency = $this -> settings -> update_check_frequency ;
$this -> instance_timezone = $this -> settings -> instance_timezone ;
$this -> disable_two_step_confirmation = $this -> settings -> disable_two_step_confirmation ;
return null ;
2024-01-07 15:23:41 +00:00
}
2024-06-10 20:43:34 +00:00
2024-12-03 23:30:39 +00:00
#[Computed]
public function timezones () : array
{
return collect ( timezone_identifiers_list ())
-> sort ()
-> values ()
-> toArray ();
}
2024-10-28 09:10:27 +00:00
public function instantSave ( $isSave = true )
2024-08-06 12:36:56 +00:00
{
2024-11-25 16:56:19 +00:00
$this -> validate ();
if ( $this -> settings -> is_auto_update_enabled === true ) {
$this -> validate ([
'auto_update_frequency' => [ 'required' , 'string' ],
]);
}
2024-10-28 09:10:27 +00:00
$this -> settings -> fqdn = $this -> fqdn ;
$this -> settings -> public_port_min = $this -> public_port_min ;
$this -> settings -> public_port_max = $this -> public_port_max ;
$this -> settings -> custom_dns_servers = $this -> custom_dns_servers ;
$this -> settings -> instance_name = $this -> instance_name ;
$this -> settings -> allowed_ips = $this -> allowed_ips ;
$this -> settings -> public_ipv4 = $this -> public_ipv4 ;
$this -> settings -> public_ipv6 = $this -> public_ipv6 ;
2024-08-06 12:36:56 +00:00
$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 ;
$this -> settings -> is_dns_validation_enabled = $this -> is_dns_validation_enabled ;
$this -> settings -> is_api_enabled = $this -> is_api_enabled ;
$this -> settings -> auto_update_frequency = $this -> auto_update_frequency ;
$this -> settings -> update_check_frequency = $this -> update_check_frequency ;
2024-10-17 10:07:35 +00:00
$this -> settings -> disable_two_step_confirmation = $this -> disable_two_step_confirmation ;
2024-10-28 09:10:27 +00:00
$this -> settings -> instance_timezone = $this -> instance_timezone ;
if ( $isSave ) {
$this -> settings -> save ();
$this -> dispatch ( 'success' , 'Settings updated!' );
}
2024-08-06 12:36:56 +00:00
}
public function submit ()
{
try {
$error_show = false ;
2025-01-07 13:52:08 +00:00
$this -> server = Server :: query () -> findOrFail ( 0 );
2024-08-06 12:36:56 +00:00
$this -> resetErrorBag ();
2024-11-14 09:02:37 +00:00
if ( ! validate_timezone ( $this -> instance_timezone )) {
$this -> instance_timezone = config ( 'app.timezone' );
2025-01-07 13:52:08 +00:00
throw new Exception ( 'Invalid timezone.' );
2024-11-14 09:02:37 +00:00
}
2025-01-07 13:52:08 +00:00
$this -> settings -> instance_timezone = $this -> instance_timezone ;
2024-11-14 09:02:37 +00:00
2024-08-06 12:36:56 +00:00
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.' );
2025-01-07 13:52:08 +00:00
return null ;
2024-08-06 12:36:56 +00:00
}
$this -> validate ();
2024-08-07 09:55:16 +00:00
if ( $this -> is_auto_update_enabled && ! validate_cron_expression ( $this -> auto_update_frequency )) {
2024-08-06 12:36:56 +00:00
$this -> dispatch ( 'error' , 'Invalid Cron / Human expression for Auto Update Frequency.' );
2025-01-07 13:52:08 +00:00
if ( ! isset ( $this -> auto_update_frequency ) || ( $this -> auto_update_frequency === '' || $this -> auto_update_frequency === '0' )) {
2024-08-07 09:55:16 +00:00
$this -> auto_update_frequency = '0 0 * * *' ;
}
2024-08-06 12:36:56 +00:00
2025-01-07 13:52:08 +00:00
return null ;
2024-08-06 12:36:56 +00:00
}
2024-08-07 09:55:16 +00:00
if ( ! validate_cron_expression ( $this -> update_check_frequency )) {
2024-08-06 12:36:56 +00:00
$this -> dispatch ( 'error' , 'Invalid Cron / Human expression for Update Check Frequency.' );
2025-01-07 13:52:08 +00:00
if ( ! isset ( $this -> update_check_frequency ) || ( $this -> update_check_frequency === '' || $this -> update_check_frequency === '0' )) {
2024-08-07 09:55:16 +00:00
$this -> update_check_frequency = '0 * * * *' ;
}
2024-08-06 12:36:56 +00:00
2025-01-07 13:52:08 +00:00
return null ;
2024-08-06 12:36:56 +00:00
}
2025-01-07 13:52:08 +00:00
if ( $this -> settings -> is_dns_validation_enabled && $this -> settings -> fqdn && ! validate_dns_entry ( $this -> settings -> fqdn , $this -> server )) {
$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. " );
$error_show = true ;
2024-08-06 12:36:56 +00:00
}
if ( $this -> settings -> fqdn ) {
check_domain_usage ( domain : $this -> settings -> fqdn );
}
$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 ( ',' );
$this -> settings -> allowed_ips = str ( $this -> settings -> allowed_ips ) -> replaceEnd ( ',' , '' ) -> trim ();
$this -> settings -> allowed_ips = str ( $this -> settings -> allowed_ips ) -> trim () -> explode ( ',' ) -> map ( function ( $ip ) {
return str ( $ip ) -> trim ();
});
$this -> settings -> allowed_ips = $this -> settings -> allowed_ips -> unique ();
$this -> settings -> allowed_ips = $this -> settings -> allowed_ips -> implode ( ',' );
2024-10-28 09:10:27 +00:00
$this -> instantSave ( isSave : false );
2024-08-06 12:36:56 +00:00
$this -> settings -> save ();
$this -> server -> setupDynamicProxyConfiguration ();
if ( ! $error_show ) {
$this -> dispatch ( 'success' , 'Instance settings updated successfully!' );
}
2025-01-07 13:52:08 +00:00
} catch ( Exception $e ) {
2024-08-06 12:36:56 +00:00
return handleError ( $e , $this );
}
2025-01-07 13:52:08 +00:00
return null ;
2024-08-06 12:36:56 +00:00
}
2024-08-07 09:42:55 +00:00
public function checkManually ()
{
CheckForUpdatesJob :: dispatchSync ();
$this -> dispatch ( 'updateAvailable' );
2024-10-01 08:37:40 +00:00
$settings = instanceSettings ();
2024-08-07 09:42:55 +00:00
if ( $settings -> new_version_available ) {
$this -> dispatch ( 'success' , 'New version available!' );
} else {
$this -> dispatch ( 'success' , 'No new version available.' );
}
}
2025-01-07 13:36:09 +00:00
public function toggleTwoStepConfirmation ( $password ) : bool
2024-10-17 10:23:13 +00:00
{
2024-10-24 14:21:22 +00:00
if ( ! Hash :: check ( $password , Auth :: user () -> password )) {
$this -> addError ( 'password' , 'The provided password is incorrect.' );
2025-01-07 13:36:09 +00:00
return false ;
2024-10-24 14:21:22 +00:00
}
2024-10-28 09:10:27 +00:00
$this -> settings -> disable_two_step_confirmation = $this -> disable_two_step_confirmation = true ;
2024-10-17 10:23:13 +00:00
$this -> settings -> save ();
$this -> dispatch ( 'success' , 'Two step confirmation has been disabled.' );
2025-01-07 13:36:09 +00:00
return true ;
2024-10-17 10:23:13 +00:00
}
2024-08-26 13:26:08 +00:00
}