VpnManagement.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Services\VpnManager;
  5. use Illuminate\Support\Facades\Log;
  6. class VpnManagement extends Component
  7. {
  8. public $username = 'com.cmrmp013.0001';
  9. public $password = '';
  10. public $server = 'anyvpn.ilportaledellautomobilista.it/utentiMCTC';
  11. public $vpnStatus = 'unknown';
  12. public $lastUpdate = null;
  13. public $successMessage = '';
  14. public $errorMessage = '';
  15. protected $rules = [
  16. 'username' => 'required|string',
  17. 'password' => 'required|string',
  18. 'server' => 'required|string',
  19. ];
  20. protected $vpnManager;
  21. public function boot(VpnManager $vpnManager)
  22. {
  23. $this->vpnManager = $vpnManager;
  24. }
  25. public function mount()
  26. {
  27. $this->refreshStatus();
  28. }
  29. public function render()
  30. {
  31. return view('livewire.vpn-management');
  32. }
  33. public function refreshStatus()
  34. {
  35. $this->vpnStatus = $this->vpnManager->getVpnStatus();
  36. $this->lastUpdate = $this->vpnManager->getLastUpdate();
  37. $this->emit('vpnStatusUpdated', $this->vpnStatus);
  38. }
  39. public function updateCredentials()
  40. {
  41. $this->validate();
  42. try {
  43. $success = $this->vpnManager->updateCredentials(
  44. $this->username,
  45. $this->password,
  46. $this->server
  47. );
  48. if ($success) {
  49. $this->successMessage = 'Credenziali VPN aggiornate con successo!';
  50. $this->errorMessage = '';
  51. $this->password = ''; // Clear password field for security
  52. // Emit event for JavaScript notification
  53. $this->emit('showToast', 'success', 'Credenziali VPN aggiornate con successo!');
  54. Log::info('VPN credentials updated successfully');
  55. } else {
  56. $this->errorMessage = 'Errore nell\'aggiornamento delle credenziali VPN.';
  57. $this->successMessage = '';
  58. $this->emit('showToast', 'error', 'Errore nell\'aggiornamento delle credenziali VPN.');
  59. }
  60. } catch (\Exception $e) {
  61. $this->errorMessage = 'Errore: ' . $e->getMessage();
  62. $this->successMessage = '';
  63. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  64. Log::error('Error updating VPN credentials: ' . $e->getMessage());
  65. }
  66. }
  67. public function connectVpn()
  68. {
  69. try {
  70. $result = $this->vpnManager->connectVpn();
  71. if ($result) {
  72. $this->emit('showToast', 'success', 'Comando di connessione VPN inviato!');
  73. Log::info('VPN connection command sent');
  74. // Refresh status after a delay
  75. $this->emit('refreshStatusDelayed');
  76. } else {
  77. $this->emit('showToast', 'error', 'Errore nel lancio della connessione VPN');
  78. Log::error('Failed to send VPN connection command');
  79. }
  80. } catch (\Exception $e) {
  81. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  82. Log::error('Error connecting VPN: ' . $e->getMessage());
  83. }
  84. }
  85. public function disconnectVpn()
  86. {
  87. try {
  88. $result = $this->vpnManager->disconnectVpn();
  89. if ($result) {
  90. $this->emit('showToast', 'success', 'Comando di disconnessione VPN inviato!');
  91. Log::info('VPN disconnection command sent');
  92. // Refresh status after a delay
  93. $this->emit('refreshStatusDelayed');
  94. } else {
  95. $this->emit('showToast', 'error', 'Errore nella disconnessione VPN');
  96. Log::error('Failed to send VPN disconnection command');
  97. }
  98. } catch (\Exception $e) {
  99. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  100. Log::error('Error disconnecting VPN: ' . $e->getMessage());
  101. }
  102. }
  103. public function getStatusBadgeClass()
  104. {
  105. return match($this->vpnStatus) {
  106. 'connected' => 'badge badge-success',
  107. 'disconnected' => 'badge badge-danger',
  108. 'error' => 'badge badge-danger',
  109. default => 'badge badge-warning'
  110. };
  111. }
  112. public function getStatusBadgeText()
  113. {
  114. return match($this->vpnStatus) {
  115. 'connected' => 'ON',
  116. 'disconnected' => 'OFF',
  117. 'error' => 'ERR',
  118. default => '?'
  119. };
  120. }
  121. public function getStatusIconClass()
  122. {
  123. return match($this->vpnStatus) {
  124. 'connected' => 'info-box-icon bg-success',
  125. 'disconnected' => 'info-box-icon bg-danger',
  126. 'error' => 'info-box-icon bg-danger',
  127. default => 'info-box-icon bg-warning'
  128. };
  129. }
  130. public function getStatusIcon()
  131. {
  132. return match($this->vpnStatus) {
  133. 'connected' => 'fas fa-check',
  134. 'disconnected' => 'fas fa-times',
  135. 'error' => 'fas fa-exclamation-triangle',
  136. default => 'fas fa-question'
  137. };
  138. }
  139. public function getStatusText()
  140. {
  141. return match($this->vpnStatus) {
  142. 'connected' => 'Connesso',
  143. 'disconnected' => 'Disconnesso',
  144. 'error' => 'Errore',
  145. default => 'Sconosciuto'
  146. };
  147. }
  148. }