VpnManagement.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Services\VpnManager;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Validation\ValidationException;
  7. class VpnManagement extends Component
  8. {
  9. public $username = 'com.cmrmp013.0001';
  10. public $password = '';
  11. public $server = 'anyvpn.ilportaledellautomobilista.it/utentiMCTC';
  12. public $vpnStatus = 'unknown';
  13. public $lastUpdate = null;
  14. public $successMessage = '';
  15. public $errorMessage = '';
  16. protected $rules = [
  17. 'username' => 'required|string',
  18. 'password' => 'required|string',
  19. 'server' => 'required|string',
  20. ];
  21. protected $vpnManager;
  22. public function boot(VpnManager $vpnManager)
  23. {
  24. $this->vpnManager = $vpnManager;
  25. }
  26. public function mount()
  27. {
  28. // Al caricamento, forza una sincronizzazione con il sistema
  29. $this->refreshStatus(true);
  30. }
  31. public function render()
  32. {
  33. return view('livewire.vpn-management');
  34. }
  35. public function refreshStatus($forceSystemCheck = false)
  36. {
  37. $this->vpnStatus = $this->vpnManager->getVpnStatus($forceSystemCheck);
  38. $this->lastUpdate = $this->vpnManager->getLastUpdate();
  39. $this->emit('vpnStatusUpdated', $this->vpnStatus);
  40. }
  41. public function syncWithSystem()
  42. {
  43. try {
  44. $realStatus = $this->vpnManager->syncStatusWithSystem();
  45. $this->vpnStatus = $realStatus;
  46. $this->lastUpdate = $this->vpnManager->getLastUpdate();
  47. $this->emit('vpnStatusUpdated', $this->vpnStatus);
  48. $this->emit('showToast', 'success', 'Stato sincronizzato: ' . $this->getStatusText());
  49. Log::info('VPN status manually synced from frontend', [
  50. 'new_status' => $realStatus
  51. ]);
  52. } catch (\Exception $e) {
  53. $this->emit('showToast', 'error', 'Errore nella sincronizzazione: ' . $e->getMessage());
  54. Log::error('Error syncing VPN status from frontend: ' . $e->getMessage());
  55. }
  56. }
  57. public function connectVpn()
  58. {
  59. try {
  60. // Se già connesso, non fare nulla
  61. if ($this->vpnStatus === 'connected') {
  62. $this->emit('showToast', 'info', 'VPN già connessa');
  63. return;
  64. }
  65. $result = $this->vpnManager->connectVpn();
  66. if ($result) {
  67. $this->emit('showToast', 'success', 'Comando di connessione VPN inviato!');
  68. Log::info('VPN connection command sent from frontend');
  69. // Refresh status after a delay
  70. $this->emit('refreshStatusDelayed');
  71. } else {
  72. $this->emit('showToast', 'error', 'Errore nel lancio della connessione VPN');
  73. Log::error('Failed to send VPN connection command from frontend');
  74. }
  75. } catch (\Exception $e) {
  76. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  77. Log::error('Error in connectVpn from frontend: ' . $e->getMessage());
  78. }
  79. }
  80. public function disconnectVpn()
  81. {
  82. try {
  83. // Se già disconnesso, non fare nulla
  84. if ($this->vpnStatus === 'disconnected') {
  85. $this->emit('showToast', 'info', 'VPN già disconnessa');
  86. return;
  87. }
  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 from frontend');
  92. // Update status immediately for disconnect
  93. $this->vpnStatus = 'disconnected';
  94. $this->lastUpdate = now();
  95. $this->emit('vpnStatusUpdated', $this->vpnStatus);
  96. } else {
  97. $this->emit('showToast', 'error', 'Errore nella disconnessione VPN');
  98. Log::error('Failed to send VPN disconnection command from frontend');
  99. }
  100. } catch (\Exception $e) {
  101. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  102. Log::error('Error in disconnectVpn from frontend: ' . $e->getMessage());
  103. }
  104. }
  105. public function updateCredentials()
  106. {
  107. try {
  108. $this->validate();
  109. $success = $this->vpnManager->updateCredentials(
  110. $this->username,
  111. $this->password,
  112. $this->server
  113. );
  114. if ($success) {
  115. $this->successMessage = 'Credenziali VPN aggiornate con successo!';
  116. $this->errorMessage = '';
  117. $this->password = ''; // Clear password field for security
  118. // Emit event for JavaScript notification
  119. $this->emit('showToast', 'success', 'Credenziali VPN aggiornate con successo!');
  120. Log::info('VPN credentials updated successfully');
  121. } else {
  122. $this->errorMessage = 'Errore nell\'aggiornamento delle credenziali VPN.';
  123. $this->successMessage = '';
  124. $this->emit('showToast', 'error', 'Errore nell\'aggiornamento delle credenziali VPN.');
  125. }
  126. } catch (ValidationException $e) {
  127. // Validation errors are handled automatically by Livewire
  128. $this->emit('showToast', 'error', 'Errori di validazione nei campi');
  129. } catch (\Exception $e) {
  130. $this->errorMessage = 'Errore: ' . $e->getMessage();
  131. $this->successMessage = '';
  132. $this->emit('showToast', 'error', 'Errore: ' . $e->getMessage());
  133. Log::error('Error updating VPN credentials: ' . $e->getMessage());
  134. }
  135. }
  136. public function getStatusBadgeClass()
  137. {
  138. return match($this->vpnStatus) {
  139. 'connected' => 'badge badge-success',
  140. 'disconnected' => 'badge badge-danger',
  141. 'error' => 'badge badge-danger',
  142. default => 'badge badge-warning'
  143. };
  144. }
  145. public function getStatusBadgeText()
  146. {
  147. return match($this->vpnStatus) {
  148. 'connected' => 'ON',
  149. 'disconnected' => 'OFF',
  150. 'error' => 'ERR',
  151. default => '?'
  152. };
  153. }
  154. public function getStatusIconClass()
  155. {
  156. return match($this->vpnStatus) {
  157. 'connected' => 'info-box-icon bg-success',
  158. 'disconnected' => 'info-box-icon bg-danger',
  159. 'error' => 'info-box-icon bg-danger',
  160. default => 'info-box-icon bg-warning'
  161. };
  162. }
  163. public function getStatusIcon()
  164. {
  165. return match($this->vpnStatus) {
  166. 'connected' => 'fas fa-check',
  167. 'disconnected' => 'fas fa-times',
  168. 'error' => 'fas fa-exclamation-triangle',
  169. default => 'fas fa-question'
  170. };
  171. }
  172. public function getStatusText()
  173. {
  174. return match($this->vpnStatus) {
  175. 'connected' => 'Connesso',
  176. 'disconnected' => 'Disconnesso',
  177. 'error' => 'Errore',
  178. default => 'Sconosciuto'
  179. };
  180. }
  181. }