| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use App\Services\VpnManager;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Validation\ValidationException;
- class VpnManagement extends Component
- {
- public $username = 'com.cmrmp013.0001';
- public $password = '';
- public $server = 'anyvpn.ilportaledellautomobilista.it/utentiMCTC';
- public $vpnStatus = 'unknown';
- public $lastUpdate = null;
- public $successMessage = '';
- public $errorMessage = '';
- protected $rules = [
- 'username' => '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'
- };
- }
- }
|