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 ;
2024-10-24 14:21:22 +00:00
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Hash ;
2024-10-28 09:10:27 +00:00
use Livewire\Attributes\Locked ;
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-10-28 09:10:27 +00:00
#[Locked]
public $timezones ;
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('nullable|string|max:255')]
2024-10-28 09:10:27 +00:00
public ? string $resale_license = 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-05 08:36:40 +00:00
#[Validate('string')]
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' );
} else {
2024-10-01 08:33:56 +00:00
$this -> settings = instanceSettings ();
2024-10-28 09:10:27 +00:00
$this -> fqdn = $this -> settings -> fqdn ;
$this -> resale_license = $this -> settings -> resale_license ;
$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 ;
2024-08-06 12:36:56 +00:00
$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 ;
2024-08-16 14:01:57 +00:00
$this -> timezones = collect ( timezone_identifiers_list ()) -> sort () -> values () -> toArray ();
2024-10-28 09:10:27 +00:00
$this -> instance_timezone = $this -> settings -> instance_timezone ;
2024-10-17 10:07:35 +00:00
$this -> disable_two_step_confirmation = $this -> settings -> disable_two_step_confirmation ;
2024-01-07 15:23:41 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-10-28 09:10:27 +00:00
public function instantSave ( $isSave = true )
2024-08-06 12:36:56 +00:00
{
2024-10-28 09:10:27 +00:00
$this -> settings -> fqdn = $this -> fqdn ;
$this -> settings -> resale_license = $this -> resale_license ;
$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 ;
$this -> server = Server :: findOrFail ( 0 );
$this -> resetErrorBag ();
2024-11-14 09:02:37 +00:00
if ( ! validate_timezone ( $this -> instance_timezone )) {
$this -> instance_timezone = config ( 'app.timezone' );
throw new \Exception ( 'Invalid timezone.' );
} else {
$this -> settings -> instance_timezone = $this -> instance_timezone ;
}
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.' );
return ;
}
$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.' );
2024-08-07 09:55:16 +00:00
if ( empty ( $this -> auto_update_frequency )) {
$this -> auto_update_frequency = '0 0 * * *' ;
}
2024-08-06 12:36:56 +00:00
return ;
}
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.' );
2024-08-07 09:55:16 +00:00
if ( empty ( $this -> update_check_frequency )) {
$this -> update_check_frequency = '0 * * * *' ;
}
2024-08-06 12:36:56 +00:00
return ;
}
if ( $this -> settings -> is_dns_validation_enabled && $this -> settings -> fqdn ) {
if ( ! 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 ;
}
}
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!' );
}
} catch ( \Exception $e ) {
return handleError ( $e , $this );
}
}
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.' );
}
}
2024-10-24 14:21:22 +00:00
public function toggleTwoStepConfirmation ( $password )
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.' );
return ;
}
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.' );
}
2024-08-26 13:26:08 +00:00
}