'required|string', 'password' => 'required|string', 'server' => 'required|string', ]; protected $vpnManager; public function boot(VpnManager $vpnManager) { $this->vpnManager = $vpnManager; } public function mount() { // Al caricamento, forza una sincronizzazione con il sistema $this->refreshStatus(true); } public function render() { return view('livewire.vpn-management'); } public function refreshStatus($forceSystemCheck = false) { $this->vpnStatus = $this->vpnManager->getVpnStatus($forceSystemCheck); $this->lastUpdate = $this->vpnManager->getLastUpdate(); $this->emit('vpnStatusUpdated', $this->vpnStatus); } public function syncWithSystem() { try { $realStatus = $this->vpnManager->syncStatusWithSystem(); $this->vpnStatus = $realStatus; $this->lastUpdate = $this->vpnManager->getLastUpdate(); $this->emit('vpnStatusUpdated', $this->vpnStatus); $this->emit('showToast', 'success', 'Stato sincronizzato: ' . $this->getStatusText()); Log::info('VPN status manually synced from frontend', [ 'new_status' => $realStatus ]); } catch (\Exception $e) { $this->emit('showToast', 'error', 'Errore nella sincronizzazione: ' . $e->getMessage()); Log::error('Error syncing VPN status from frontend: ' . $e->getMessage()); } } public function connectVpn() { try { // Se già connesso, non fare nulla if ($this->vpnStatus === 'connected') { $this->emit('showToast', 'info', 'VPN già connessa'); return; } $result = $this->vpnManager->connectVpn(); if ($result) { $this->emit('showToast', 'success', 'Comando di connessione VPN inviato!'); Log::info('VPN connection command sent from frontend'); // Refresh status after a delay $this->emit('refreshStatusDelayed'); } else { $this->emit('showToast', 'error', 'Errore nel lancio della connessione VPN'); Log::error('Failed to send VPN connection command from frontend'); } } catch (\Exception $e) { $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage()); Log::error('Error in connectVpn from frontend: ' . $e->getMessage()); } } public function disconnectVpn() { try { // Se già disconnesso, non fare nulla if ($this->vpnStatus === 'disconnected') { $this->emit('showToast', 'info', 'VPN già disconnessa'); return; } $result = $this->vpnManager->disconnectVpn(); if ($result) { $this->emit('showToast', 'success', 'Comando di disconnessione VPN inviato!'); Log::info('VPN disconnection command sent from frontend'); // Update status immediately for disconnect $this->vpnStatus = 'disconnected'; $this->lastUpdate = now(); $this->emit('vpnStatusUpdated', $this->vpnStatus); } else { $this->emit('showToast', 'error', 'Errore nella disconnessione VPN'); Log::error('Failed to send VPN disconnection command from frontend'); } } catch (\Exception $e) { $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage()); Log::error('Error in disconnectVpn from frontend: ' . $e->getMessage()); } } public function updateCredentials() { try { $this->validate(); $success = $this->vpnManager->updateCredentials( $this->username, $this->password, $this->server ); if ($success) { $this->successMessage = 'Credenziali VPN aggiornate con successo!'; $this->errorMessage = ''; $this->password = ''; // Clear password field for security // Emit event for JavaScript notification $this->emit('showToast', 'success', 'Credenziali VPN aggiornate con successo!'); Log::info('VPN credentials updated successfully'); } else { $this->errorMessage = 'Errore nell\'aggiornamento delle credenziali VPN.'; $this->successMessage = ''; $this->emit('showToast', 'error', 'Errore nell\'aggiornamento delle credenziali VPN.'); } } catch (ValidationException $e) { // Validation errors are handled automatically by Livewire $this->emit('showToast', 'error', 'Errori di validazione nei campi'); } catch (\Exception $e) { $this->errorMessage = 'Errore: ' . $e->getMessage(); $this->successMessage = ''; $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage()); Log::error('Error updating VPN credentials: ' . $e->getMessage()); } } public function getStatusBadgeClass() { return match($this->vpnStatus) { 'connected' => 'badge badge-success', 'disconnected' => 'badge badge-danger', 'error' => 'badge badge-danger', default => 'badge badge-warning' }; } public function getStatusBadgeText() { return match($this->vpnStatus) { 'connected' => 'ON', 'disconnected' => 'OFF', 'error' => 'ERR', default => '?' }; } public function getStatusIconClass() { return match($this->vpnStatus) { 'connected' => 'info-box-icon bg-success', 'disconnected' => 'info-box-icon bg-danger', 'error' => 'info-box-icon bg-danger', default => 'info-box-icon bg-warning' }; } public function getStatusIcon() { return match($this->vpnStatus) { 'connected' => 'fas fa-check', 'disconnected' => 'fas fa-times', 'error' => 'fas fa-exclamation-triangle', default => 'fas fa-question' }; } public function getStatusText() { return match($this->vpnStatus) { 'connected' => 'Connesso', 'disconnected' => 'Disconnesso', 'error' => 'Errore', default => 'Sconosciuto' }; } }