VpnManager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Storage;
  4. use Illuminate\Support\Facades\Crypt;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Schema;
  7. class VpnManager
  8. {
  9. private $configPath = 'scripts/vpn-config.conf';
  10. private $scriptPath = 'scripts/vpn-connect.sh';
  11. public function updateCredentials($username, $password, $server)
  12. {
  13. try {
  14. // Crea il contenuto del file di configurazione
  15. $configContent = "# Configurazione VPN - storage/scripts/vpn-config.conf\n";
  16. $configContent .= "# ATTENZIONE: Questo file deve avere permessi 600\n\n";
  17. $configContent .= "VPN_USERNAME=\"{$username}\"\n";
  18. $configContent .= "VPN_PASSWORD=\"{$password}\"\n";
  19. $configContent .= "VPN_SERVER=\"{$server}\"\n";
  20. // Salva il file di configurazione
  21. Storage::put($this->configPath, $configContent);
  22. // Imposta i permessi corretti (solo per sistemi Unix/Linux)
  23. $fullPath = storage_path('app/' . $this->configPath);
  24. chmod($fullPath, 0600);
  25. Log::info('Credenziali VPN aggiornate con successo');
  26. return true;
  27. } catch (\Exception $e) {
  28. Log::error('Errore nell\'aggiornamento delle credenziali VPN: ' . $e->getMessage());
  29. return false;
  30. }
  31. }
  32. public function getVpnStatus($forceRefresh = false)
  33. {
  34. try {
  35. // Se richiesto refresh, sincronizza con il sistema
  36. if ($forceRefresh) {
  37. return $this->syncStatusWithSystem();
  38. }
  39. // Verifica se la tabella esiste
  40. if (!Schema::hasTable('vpn_status')) {
  41. Log::warning('Tabella vpn_status non esiste');
  42. return 'disconnected';
  43. }
  44. $status = \DB::table('vpn_status')->first();
  45. if (!$status) {
  46. // Se non ci sono record, sincronizza con il sistema
  47. return $this->syncStatusWithSystem();
  48. }
  49. return $status->status;
  50. } catch (\Exception $e) {
  51. Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
  52. return 'disconnected';
  53. }
  54. }
  55. public function getLastUpdate()
  56. {
  57. try {
  58. if (!Schema::hasTable('vpn_status')) {
  59. return now();
  60. }
  61. $status = \DB::table('vpn_status')->first();
  62. return $status ? $status->last_update : now();
  63. } catch (\Exception $e) {
  64. Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
  65. return now();
  66. }
  67. }
  68. public function checkRealVpnStatus()
  69. {
  70. try {
  71. // Esegui il comando per verificare lo stato reale
  72. $output = [];
  73. $returnVar = 0;
  74. exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output, $returnVar);
  75. $statusText = implode(' ', $output);
  76. Log::info('VPN real status check', [
  77. 'output' => $output,
  78. 'return_var' => $returnVar,
  79. 'status_text' => $statusText
  80. ]);
  81. // Determina lo stato basandosi sull'output
  82. if (strpos($statusText, 'state: Connected') !== false ||
  83. strpos($statusText, 'Connected') !== false) {
  84. return 'connected';
  85. } elseif (strpos($statusText, 'Disconnected') !== false ||
  86. strpos($statusText, 'state: Disconnected') !== false) {
  87. return 'disconnected';
  88. } else {
  89. return 'unknown';
  90. }
  91. } catch (\Exception $e) {
  92. Log::error('Error checking real VPN status: ' . $e->getMessage());
  93. return 'error';
  94. }
  95. }
  96. public function syncStatusWithSystem()
  97. {
  98. try {
  99. $realStatus = $this->checkRealVpnStatus();
  100. // Aggiorna il database con lo stato reale
  101. \DB::table('vpn_status')->updateOrInsert(
  102. ['id' => 1],
  103. [
  104. 'status' => $realStatus,
  105. 'last_update' => now(),
  106. 'updated_at' => now(),
  107. ]
  108. );
  109. Log::info('VPN status synced with system', [
  110. 'real_status' => $realStatus
  111. ]);
  112. return $realStatus;
  113. } catch (\Exception $e) {
  114. Log::error('Error syncing VPN status: ' . $e->getMessage());
  115. return 'error';
  116. }
  117. }
  118. public function connectVpn()
  119. {
  120. try {
  121. // Prima controlla se è già connesso
  122. $currentStatus = $this->checkRealVpnStatus();
  123. if ($currentStatus === 'connected') {
  124. Log::info('VPN already connected, no action needed');
  125. return true;
  126. }
  127. $scriptPath = storage_path('app/' . $this->scriptPath);
  128. if (!file_exists($scriptPath)) {
  129. Log::error('Script VPN non trovato: ' . $scriptPath);
  130. return false;
  131. }
  132. // Esegue lo script in background
  133. $command = "bash {$scriptPath} > /dev/null 2>&1 &";
  134. exec($command, $output, $returnVar);
  135. Log::info('Comando VPN connect eseguito', [
  136. 'command' => $command,
  137. 'return_var' => $returnVar
  138. ]);
  139. return true;
  140. } catch (\Exception $e) {
  141. Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
  142. return false;
  143. }
  144. }
  145. public function disconnectVpn()
  146. {
  147. try {
  148. // Comando per disconnettere VPN Cisco AnyConnect
  149. $command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
  150. exec($command, $output, $returnVar);
  151. // Aggiorna lo stato nel database
  152. \DB::table('vpn_status')->updateOrInsert(
  153. ['id' => 1],
  154. [
  155. 'status' => 'disconnected',
  156. 'last_update' => now(),
  157. 'updated_at' => now(),
  158. ]
  159. );
  160. Log::info('Comando VPN disconnect eseguito', [
  161. 'command' => $command,
  162. 'return_var' => $returnVar
  163. ]);
  164. return true;
  165. } catch (\Exception $e) {
  166. Log::error('Errore nell\'esecuzione del comando VPN disconnect: ' . $e->getMessage());
  167. return false;
  168. }
  169. }
  170. public function makeScriptExecutable()
  171. {
  172. try {
  173. $fullPath = storage_path('app/' . $this->scriptPath);
  174. chmod($fullPath, 0755);
  175. return true;
  176. } catch (\Exception $e) {
  177. Log::error('Errore nell\'impostazione dei permessi script: ' . $e->getMessage());
  178. return false;
  179. }
  180. }
  181. }