info('=== VPN STATUS REPORT ===');
$this->newLine();
// 1. Stato dal database
$this->checkDatabaseStatus();
// 2. Stato dal sistema (se richiesto)
if ($this->option('refresh') || $this->option('sync')) {
$this->checkSystemStatus();
}
// 3. Sincronizzazione (se richiesta)
if ($this->option('sync')) {
$this->syncStatus();
}
// 4. Verifica file e configurazioni
$this->checkFiles();
// 5. Mostra log recenti
$this->showRecentLogs();
$this->newLine();
$this->info('=== END REPORT ===');
}
private function checkDatabaseStatus()
{
$this->line('1. Database Status:');
try {
$status = DB::table('vpn_status')->first();
if ($status) {
$statusColor = $status->status === 'connected' ? 'green' : 'red';
$this->line(" Status: {$status->status}>");
$this->line(" Last Update: {$status->last_update}");
$this->line(" Record ID: {$status->id}");
} else {
$this->error(' No status record found in database');
}
} catch (\Exception $e) {
$this->error(' Database error: ' . $e->getMessage());
}
$this->newLine();
}
private function syncStatus()
{
$this->line('3. Synchronizing Database with System:');
try {
// Ottieni stato dal sistema
$output = [];
exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output);
$statusText = implode(' ', $output);
$systemStatus = 'unknown';
if (strpos($statusText, 'state: Connected') !== false) {
$systemStatus = 'connected';
} elseif (strpos($statusText, 'Disconnected') !== false) {
$systemStatus = 'disconnected';
}
// Ottieni stato dal database
$dbRecord = DB::table('vpn_status')->first();
$dbStatus = $dbRecord ? $dbRecord->status : 'no_record';
$this->line(" System Status: {$systemStatus}>");
$this->line(" Database Status: {$dbStatus}>");
if ($systemStatus !== 'unknown' && $systemStatus !== $dbStatus) {
// Aggiorna il database
DB::table('vpn_status')->updateOrInsert(
['id' => 1],
[
'status' => $systemStatus,
'last_update' => now(),
'updated_at' => now(),
]
);
$this->line(" ✅ Database updated to: {$systemStatus}>");
Log::info('VPN status synced via command', [
'old_status' => $dbStatus,
'new_status' => $systemStatus
]);
} else {
$this->line(' ℹ️ No sync needed - statuses match');
}
} catch (\Exception $e) {
$this->error(" ❌ Sync failed: " . $e->getMessage());
}
}
private function checkSystemStatus()
{
$this->line('2. System Status (Live Check):');
if (file_exists('/opt/cisco/anyconnect/bin/vpn')) {
$this->line(' ✅ Cisco AnyConnect: Installed');
$output = [];
$returnVar = 0;
exec('/opt/cisco/anyconnect/bin/vpn state 2>&1', $output, $returnVar);
$this->line(' System Status Output:');
foreach ($output as $line) {
$this->line(' ' . $line);
}
// Analizza l'output
$statusText = implode(' ', $output);
if (strpos($statusText, 'Connected') !== false) {
$this->line(' ✅ System Status: CONNECTED>', 'green');
} elseif (strpos($statusText, 'Disconnected') !== false) {
$this->line(' ❌ System Status: DISCONNECTED>');
} else {
$this->line(' ⚠️ System Status: UNKNOWN>');
}
} else {
$this->error(' ❌ Cisco AnyConnect: Not Installed');
}
$this->newLine();
}
private function checkFiles()
{
$this->line('3. Configuration Files:');
$files = [
'scripts/vpn-connect.sh' => 'Connection Script',
'scripts/vpn-disconnect.sh' => 'Disconnection Script',
'scripts/vpn-config.conf' => 'Configuration File',
];
foreach ($files as $file => $description) {
if (Storage::exists($file)) {
$this->line(" ✅ {$description}: Present");
// Mostra i permessi per gli script
if (str_ends_with($file, '.sh')) {
$fullPath = storage_path($file);
$perms = substr(sprintf('%o', fileperms($fullPath)), -4);
$this->line(" Permissions: {$perms}");
}
} else {
$this->error(" ❌ {$description}: Missing");
}
}
// Controlla directory logs
if (Storage::exists('logs')) {
$this->line(' ✅ Logs Directory: Present');
} else {
$this->error(' ❌ Logs Directory: Missing');
}
$this->newLine();
}
private function showRecentLogs()
{
$this->line('4. Recent Log Entries:');
$logFile = storage_path('app/logs/vpn-connection.log');
if (file_exists($logFile)) {
$this->line(" Log file: {$logFile}");
// Leggi le ultime 10 righe
$lines = file($logFile);
$recentLines = array_slice($lines, -10);
$this->line(' Last 10 entries:');
foreach ($recentLines as $line) {
$this->line(' ' . trim($line));
}
} else {
$this->error(' Log file not found');
}
$this->newLine();
}
}