|
@@ -15,23 +15,19 @@ class VpnManager
|
|
|
public function updateCredentials($username, $password, $server)
|
|
public function updateCredentials($username, $password, $server)
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- // Crea il contenuto del file di configurazione
|
|
|
|
|
$configContent = "# Configurazione VPN - storage/scripts/vpn-config.conf\n";
|
|
$configContent = "# Configurazione VPN - storage/scripts/vpn-config.conf\n";
|
|
|
$configContent .= "# ATTENZIONE: Questo file deve avere permessi 600\n\n";
|
|
$configContent .= "# ATTENZIONE: Questo file deve avere permessi 600\n\n";
|
|
|
$configContent .= "VPN_USERNAME=\"{$username}\"\n";
|
|
$configContent .= "VPN_USERNAME=\"{$username}\"\n";
|
|
|
$configContent .= "VPN_PASSWORD=\"{$password}\"\n";
|
|
$configContent .= "VPN_PASSWORD=\"{$password}\"\n";
|
|
|
$configContent .= "VPN_SERVER=\"{$server}\"\n";
|
|
$configContent .= "VPN_SERVER=\"{$server}\"\n";
|
|
|
|
|
|
|
|
- // Salva il file di configurazione
|
|
|
|
|
Storage::put($this->configPath, $configContent);
|
|
Storage::put($this->configPath, $configContent);
|
|
|
|
|
|
|
|
- // Imposta i permessi corretti (solo per sistemi Unix/Linux)
|
|
|
|
|
- $fullPath = storage_path('app/' . $this->configPath);
|
|
|
|
|
|
|
+ $fullPath = storage_path( $this->configPath);
|
|
|
chmod($fullPath, 0600);
|
|
chmod($fullPath, 0600);
|
|
|
|
|
|
|
|
Log::info('Credenziali VPN aggiornate con successo');
|
|
Log::info('Credenziali VPN aggiornate con successo');
|
|
|
return true;
|
|
return true;
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Errore nell\'aggiornamento delle credenziali VPN: ' . $e->getMessage());
|
|
Log::error('Errore nell\'aggiornamento delle credenziali VPN: ' . $e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
@@ -41,12 +37,10 @@ class VpnManager
|
|
|
public function getVpnStatus($forceRefresh = false)
|
|
public function getVpnStatus($forceRefresh = false)
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- // Se richiesto refresh, sincronizza con il sistema
|
|
|
|
|
if ($forceRefresh) {
|
|
if ($forceRefresh) {
|
|
|
return $this->syncStatusWithSystem();
|
|
return $this->syncStatusWithSystem();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Verifica se la tabella esiste
|
|
|
|
|
if (!Schema::hasTable('vpn_status')) {
|
|
if (!Schema::hasTable('vpn_status')) {
|
|
|
Log::warning('Tabella vpn_status non esiste');
|
|
Log::warning('Tabella vpn_status non esiste');
|
|
|
return 'disconnected';
|
|
return 'disconnected';
|
|
@@ -55,12 +49,10 @@ class VpnManager
|
|
|
$status = \DB::table('vpn_status')->first();
|
|
$status = \DB::table('vpn_status')->first();
|
|
|
|
|
|
|
|
if (!$status) {
|
|
if (!$status) {
|
|
|
- // Se non ci sono record, sincronizza con il sistema
|
|
|
|
|
return $this->syncStatusWithSystem();
|
|
return $this->syncStatusWithSystem();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $status->status;
|
|
return $status->status;
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
|
|
Log::error('Errore nel recupero dello stato VPN: ' . $e->getMessage());
|
|
|
return 'disconnected';
|
|
return 'disconnected';
|
|
@@ -76,7 +68,6 @@ class VpnManager
|
|
|
|
|
|
|
|
$status = \DB::table('vpn_status')->first();
|
|
$status = \DB::table('vpn_status')->first();
|
|
|
return $status ? $status->last_update : now();
|
|
return $status ? $status->last_update : now();
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
|
|
Log::error('Errore nel recupero dell\'ultimo aggiornamento VPN: ' . $e->getMessage());
|
|
|
return now();
|
|
return now();
|
|
@@ -100,16 +91,19 @@ class VpnManager
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
// Determina lo stato basandosi sull'output
|
|
// Determina lo stato basandosi sull'output
|
|
|
- if (strpos($statusText, 'state: Connected') !== false ||
|
|
|
|
|
- strpos($statusText, 'Connected') !== false) {
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ strpos($statusText, 'state: Connected') !== false ||
|
|
|
|
|
+ strpos($statusText, 'Connected') !== false
|
|
|
|
|
+ ) {
|
|
|
return 'connected';
|
|
return 'connected';
|
|
|
- } elseif (strpos($statusText, 'Disconnected') !== false ||
|
|
|
|
|
- strpos($statusText, 'state: Disconnected') !== false) {
|
|
|
|
|
|
|
+ } elseif (
|
|
|
|
|
+ strpos($statusText, 'Disconnected') !== false ||
|
|
|
|
|
+ strpos($statusText, 'state: Disconnected') !== false
|
|
|
|
|
+ ) {
|
|
|
return 'disconnected';
|
|
return 'disconnected';
|
|
|
} else {
|
|
} else {
|
|
|
return 'unknown';
|
|
return 'unknown';
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error checking real VPN status: ' . $e->getMessage());
|
|
Log::error('Error checking real VPN status: ' . $e->getMessage());
|
|
|
return 'error';
|
|
return 'error';
|
|
@@ -121,7 +115,6 @@ class VpnManager
|
|
|
try {
|
|
try {
|
|
|
$realStatus = $this->checkRealVpnStatus();
|
|
$realStatus = $this->checkRealVpnStatus();
|
|
|
|
|
|
|
|
- // Aggiorna il database con lo stato reale
|
|
|
|
|
\DB::table('vpn_status')->updateOrInsert(
|
|
\DB::table('vpn_status')->updateOrInsert(
|
|
|
['id' => 1],
|
|
['id' => 1],
|
|
|
[
|
|
[
|
|
@@ -136,7 +129,6 @@ class VpnManager
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
return $realStatus;
|
|
return $realStatus;
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error syncing VPN status: ' . $e->getMessage());
|
|
Log::error('Error syncing VPN status: ' . $e->getMessage());
|
|
|
return 'error';
|
|
return 'error';
|
|
@@ -146,31 +138,63 @@ class VpnManager
|
|
|
public function connectVpn()
|
|
public function connectVpn()
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- // Prima controlla se è già connesso
|
|
|
|
|
$currentStatus = $this->checkRealVpnStatus();
|
|
$currentStatus = $this->checkRealVpnStatus();
|
|
|
if ($currentStatus === 'connected') {
|
|
if ($currentStatus === 'connected') {
|
|
|
Log::info('VPN already connected, no action needed');
|
|
Log::info('VPN already connected, no action needed');
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $scriptPath = storage_path('app/' . $this->scriptPath);
|
|
|
|
|
|
|
+ $scriptPath = storage_path($this->scriptPath);
|
|
|
|
|
+ $configPath = storage_path($this->configPath);
|
|
|
|
|
|
|
|
|
|
+ // Check if both script and config exist
|
|
|
if (!file_exists($scriptPath)) {
|
|
if (!file_exists($scriptPath)) {
|
|
|
Log::error('Script VPN non trovato: ' . $scriptPath);
|
|
Log::error('Script VPN non trovato: ' . $scriptPath);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Esegue lo script in background
|
|
|
|
|
- $command = "bash {$scriptPath} > /dev/null 2>&1 &";
|
|
|
|
|
|
|
+ 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);
|
|
exec($command, $output, $returnVar);
|
|
|
|
|
|
|
|
Log::info('Comando VPN connect eseguito', [
|
|
Log::info('Comando VPN connect eseguito', [
|
|
|
'command' => $command,
|
|
'command' => $command,
|
|
|
|
|
+ 'output' => $output,
|
|
|
'return_var' => $returnVar
|
|
'return_var' => $returnVar
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ // 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) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
|
|
Log::error('Errore nell\'esecuzione del comando VPN connect: ' . $e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
@@ -180,11 +204,9 @@ class VpnManager
|
|
|
public function disconnectVpn()
|
|
public function disconnectVpn()
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- // Comando per disconnettere VPN Cisco AnyConnect
|
|
|
|
|
$command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
|
|
$command = "/opt/cisco/anyconnect/bin/vpn disconnect > /dev/null 2>&1 &";
|
|
|
exec($command, $output, $returnVar);
|
|
exec($command, $output, $returnVar);
|
|
|
|
|
|
|
|
- // Aggiorna lo stato nel database
|
|
|
|
|
\DB::table('vpn_status')->updateOrInsert(
|
|
\DB::table('vpn_status')->updateOrInsert(
|
|
|
['id' => 1],
|
|
['id' => 1],
|
|
|
[
|
|
[
|
|
@@ -200,7 +222,6 @@ class VpnManager
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Errore nell\'esecuzione del comando VPN disconnect: ' . $e->getMessage());
|
|
Log::error('Errore nell\'esecuzione del comando VPN disconnect: ' . $e->getMessage());
|
|
|
return false;
|
|
return false;
|
|
@@ -210,7 +231,7 @@ class VpnManager
|
|
|
public function makeScriptExecutable()
|
|
public function makeScriptExecutable()
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
- $fullPath = storage_path('app/' . $this->scriptPath);
|
|
|
|
|
|
|
+ $fullPath = storage_path($this->scriptPath);
|
|
|
chmod($fullPath, 0755);
|
|
chmod($fullPath, 0755);
|
|
|
return true;
|
|
return true;
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|