| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use App\Services\VpnManager;
- use Illuminate\Support\Facades\Log;
- 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()
- {
- $this->refreshStatus();
- }
- public function render()
- {
- return view('livewire.vpn-management');
- }
- public function refreshStatus()
- {
- $this->vpnStatus = $this->vpnManager->getVpnStatus();
- $this->lastUpdate = $this->vpnManager->getLastUpdate();
- $this->emit('vpnStatusUpdated', $this->vpnStatus);
- }
- public function updateCredentials()
- {
- $this->validate();
- try {
- $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 (\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 connectVpn()
- {
- try {
- $result = $this->vpnManager->connectVpn();
- if ($result) {
- $this->emit('showToast', 'success', 'Comando di connessione VPN inviato!');
- Log::info('VPN connection command sent');
- // 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');
- }
- } catch (\Exception $e) {
- $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
- Log::error('Error connecting VPN: ' . $e->getMessage());
- }
- }
- public function disconnectVpn()
- {
- try {
- $result = $this->vpnManager->disconnectVpn();
- if ($result) {
- $this->emit('showToast', 'success', 'Comando di disconnessione VPN inviato!');
- Log::info('VPN disconnection command sent');
- // Refresh status after a delay
- $this->emit('refreshStatusDelayed');
- } else {
- $this->emit('showToast', 'error', 'Errore nella disconnessione VPN');
- Log::error('Failed to send VPN disconnection command');
- }
- } catch (\Exception $e) {
- $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
- Log::error('Error disconnecting VPN: ' . $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'
- };
- }
- }
|