| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- class VpnManager
- {
- private $configPath = 'scripts/vpn-config.conf';
- private $scriptPath = 'scripts/vpn-connect.sh';
- public function updateCredentials($username, $password, $server)
- {
- try {
- // Crea il contenuto del file di configurazione
- $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";
- // Salva il file di configurazione
- Storage::put($this->configPath, $configContent);
- // Imposta i permessi corretti (solo per sistemi Unix/Linux)
- $fullPath = storage_path('app/' . $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()
- {
- try {
- $status = DB::table('vpn_status')->first();
- return $status ? $status->status : 'unknown';
- } catch (\Exception $e) {
- Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
- return 'error';
- }
- }
- public function getLastUpdate()
- {
- try {
- $status = DB::table('vpn_status')->first();
- return $status ? $status->last_update : null;
- } catch (\Exception $e) {
- Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
- return null;
- }
- }
- public function connectVpn()
- {
- try {
- $scriptPath = storage_path('app/' . $this->scriptPath);
- if (!file_exists($scriptPath)) {
- Log::error('Script VPN non trovato: ' . $scriptPath);
- return false;
- }
- // Esegue lo script in background
- $command = "bash {$scriptPath} > /dev/null 2>&1 &";
- exec($command, $output, $returnVar);
- Log::info('Comando VPN connect eseguito', [
- 'command' => $command,
- 'return_var' => $returnVar
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
- return false;
- }
- }
- public function disconnectVpn()
- {
- try {
- // Comando per disconnettere VPN Cisco AnyConnect
- $command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
- exec($command, $output, $returnVar);
- // Aggiorna lo stato nel database
- 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('app/' . $this->scriptPath);
- chmod($fullPath, 0755);
- return true;
- } catch (\Exception $e) {
- Log::error('Errore nell\'impostazione dei permessi script: ' . $e->getMessage());
- return false;
- }
- }
- }
|