VpnManager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\DB;
  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()
  33. {
  34. try {
  35. $status = DB::table('vpn_status')->first();
  36. return $status ? $status->status : 'unknown';
  37. } catch (\Exception $e) {
  38. Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
  39. return 'error';
  40. }
  41. }
  42. public function getLastUpdate()
  43. {
  44. try {
  45. $status = DB::table('vpn_status')->first();
  46. return $status ? $status->last_update : null;
  47. } catch (\Exception $e) {
  48. Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
  49. return null;
  50. }
  51. }
  52. public function connectVpn()
  53. {
  54. try {
  55. $scriptPath = storage_path('app/' . $this->scriptPath);
  56. if (!file_exists($scriptPath)) {
  57. Log::error('Script VPN non trovato: ' . $scriptPath);
  58. return false;
  59. }
  60. // Esegue lo script in background
  61. $command = "bash {$scriptPath} > /dev/null 2>&1 &";
  62. exec($command, $output, $returnVar);
  63. Log::info('Comando VPN connect eseguito', [
  64. 'command' => $command,
  65. 'return_var' => $returnVar
  66. ]);
  67. return true;
  68. } catch (\Exception $e) {
  69. Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
  70. return false;
  71. }
  72. }
  73. public function disconnectVpn()
  74. {
  75. try {
  76. // Comando per disconnettere VPN Cisco AnyConnect
  77. $command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
  78. exec($command, $output, $returnVar);
  79. // Aggiorna lo stato nel database
  80. DB::table('vpn_status')->updateOrInsert(
  81. ['id' => 1],
  82. [
  83. 'status' => 'disconnected',
  84. 'last_update' => now(),
  85. 'updated_at' => now(),
  86. ]
  87. );
  88. Log::info('Comando VPN disconnect eseguito', [
  89. 'command' => $command,
  90. 'return_var' => $returnVar
  91. ]);
  92. return true;
  93. } catch (\Exception $e) {
  94. Log::error('Errore nell\'esecuzione del comando VPN disconnect: ' . $e->getMessage());
  95. return false;
  96. }
  97. }
  98. public function makeScriptExecutable()
  99. {
  100. try {
  101. $fullPath = storage_path('app/' . $this->scriptPath);
  102. chmod($fullPath, 0755);
  103. return true;
  104. } catch (\Exception $e) {
  105. Log::error('Errore nell\'impostazione dei permessi script: ' . $e->getMessage());
  106. return false;
  107. }
  108. }
  109. }