| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Schema;
- class VpnManager
- {
- private $configPath = 'scripts/vpn-config.conf';
- private $scriptPath = 'scripts/vpn-connect.sh';
- public function updateCredentials($username, $password, $server)
- {
- try {
- $configContent = "# Configurazione VPN - storage/scripts/vpn-config.conf\n";
- $configContent .= "# ATTENZIONE: Questo file deve avere permessi 600\n\n";
- $configContent .= "VPN_USERNAME=\"{$username}\"\n";
- $configContent .= "VPN_PASSWORD=\"{$password}\"\n";
- $configContent .= "VPN_SERVER=\"{$server}\"\n";
- Storage::put($this->configPath, $configContent);
- $fullPath = storage_path( $this->configPath);
- chmod($fullPath, 0600);
- Log::info('Credenziali VPN aggiornate con successo');
- return true;
- } catch (\Exception $e) {
- Log::error('Errore nell\'aggiornamento delle credenziali VPN: ' . $e->getMessage());
- return false;
- }
- }
- public function getVpnStatus($forceRefresh = false)
- {
- try {
- if ($forceRefresh) {
- return $this->syncStatusWithSystem();
- }
- if (!Schema::hasTable('vpn_status')) {
- Log::warning('Tabella vpn_status non esiste');
- return 'disconnected';
- }
- $status = \DB::table('vpn_status')->first();
- if (!$status) {
- return $this->syncStatusWithSystem();
- }
- return $status->status;
- } catch (\Exception $e) {
- Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
- return 'disconnected';
- }
- }
- public function getLastUpdate()
- {
- try {
- if (!Schema::hasTable('vpn_status')) {
- return now();
- }
- $status = \DB::table('vpn_status')->first();
- return $status ? $status->last_update : now();
- } catch (\Exception $e) {
- Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
- return now();
- }
- }
- public function checkRealVpnStatus()
- {
- try {
- // Esegui il comando per verificare lo stato reale
- $output = [];
- $returnVar = 0;
- exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output, $returnVar);
- $statusText = implode(' ', $output);
- Log::info('VPN real status check', [
- 'output' => $output,
- 'return_var' => $returnVar,
- 'status_text' => $statusText
- ]);
- // Determina lo stato basandosi sull'output
- if (
- strpos($statusText, 'state: Connected') !== false ||
- strpos($statusText, 'Connected') !== false
- ) {
- return 'connected';
- } elseif (
- strpos($statusText, 'Disconnected') !== false ||
- strpos($statusText, 'state: Disconnected') !== false
- ) {
- return 'disconnected';
- } else {
- return 'unknown';
- }
- } catch (\Exception $e) {
- Log::error('Error checking real VPN status: ' . $e->getMessage());
- return 'error';
- }
- }
- public function syncStatusWithSystem()
- {
- try {
- $realStatus = $this->checkRealVpnStatus();
- \DB::table('vpn_status')->updateOrInsert(
- ['id' => 1],
- [
- 'status' => $realStatus,
- 'last_update' => now(),
- 'updated_at' => now(),
- ]
- );
- Log::info('VPN status synced with system', [
- 'real_status' => $realStatus
- ]);
- return $realStatus;
- } catch (\Exception $e) {
- Log::error('Error syncing VPN status: ' . $e->getMessage());
- return 'error';
- }
- }
- public function connectVpn()
- {
- try {
- $currentStatus = $this->checkRealVpnStatus();
- if ($currentStatus === 'connected') {
- Log::info('VPN already connected, no action needed');
- return true;
- }
- $scriptPath = storage_path($this->scriptPath);
- $configPath = storage_path($this->configPath);
- // Check if both script and config exist
- if (!file_exists($scriptPath)) {
- Log::error('Script VPN non trovato: ' . $scriptPath);
- return false;
- }
- if (!file_exists($configPath)) {
- Log::error('Config VPN non trovato: ' . $configPath);
- return false;
- }
- // Make sure script is executable
- $this->makeScriptExecutable();
- // Execute the script and capture output
- $command = "timeout 60 bash {$scriptPath} 2>&1";
- exec($command, $output, $returnVar);
- Log::info('Comando VPN connect eseguito', [
- 'command' => $command,
- 'output' => $output,
- 'return_var' => $returnVar
- ]);
- // Wait a moment and check status
- sleep(5);
- $attempts = 0;
- $maxAttempts = 12; // 1 minute total (5 seconds * 12)
- while ($attempts < $maxAttempts) {
- $newStatus = $this->checkRealVpnStatus();
- if ($newStatus === 'connected') {
- $this->syncStatusWithSystem();
- Log::info('VPN connection successful after ' . ($attempts + 1) . ' attempts');
- return true;
- }
- sleep(5);
- $attempts++;
- }
- Log::warning('VPN connection timeout or failed', [
- 'final_status' => $this->checkRealVpnStatus(),
- 'attempts' => $attempts
- ]);
- return false;
- } catch (\Exception $e) {
- Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
- return false;
- }
- }
- public function disconnectVpn()
- {
- try {
- $command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
- exec($command, $output, $returnVar);
- \DB::table('vpn_status')->updateOrInsert(
- ['id' => 1],
- [
- 'status' => 'disconnected',
- 'last_update' => now(),
- 'updated_at' => now(),
- ]
- );
- Log::info('Comando VPN disconnect eseguito', [
- 'command' => $command,
- 'return_var' => $returnVar
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('Errore nell\'esecuzione del comando VPN disconnect: ' . $e->getMessage());
- return false;
- }
- }
- public function makeScriptExecutable()
- {
- try {
- $fullPath = storage_path($this->scriptPath);
- chmod($fullPath, 0755);
- return true;
- } catch (\Exception $e) {
- Log::error('Errore nell\'impostazione dei permessi script: ' . $e->getMessage());
- return false;
- }
- }
- }
|