From 56732537f8a6ccc36f66362c81b5ebbe46118650 Mon Sep 17 00:00:00 2001 From: Notselwyn <68616630+Notselwyn@users.noreply.github.com> Date: Sat, 22 Jul 2023 14:19:42 +0200 Subject: [PATCH] chore: extended ISystemInfo with CPU and removed unnecessary (debug) logging --- .../ISystemInfo.cs | 6 ++++++ .../Registry/Registry.cs | 1 - SafeExamBrowser.SystemComponents/SystemInfo.cs | 18 ++++++++++++++++++ .../VirtualMachineDetector.cs | 10 +++------- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/SafeExamBrowser.SystemComponents.Contracts/ISystemInfo.cs b/SafeExamBrowser.SystemComponents.Contracts/ISystemInfo.cs index 194bb969..6d5c2a04 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/ISystemInfo.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/ISystemInfo.cs @@ -13,11 +13,17 @@ namespace SafeExamBrowser.SystemComponents.Contracts /// public interface ISystemInfo { + /// /// The manufacturer and name of the BIOS. /// string BiosInfo { get; } + /// + /// The name of the CPU. + /// + string Cpu { get; } + /// /// Reveals whether the computer system contains a battery. /// diff --git a/SafeExamBrowser.SystemComponents/Registry/Registry.cs b/SafeExamBrowser.SystemComponents/Registry/Registry.cs index 43b67ddd..f70185a6 100644 --- a/SafeExamBrowser.SystemComponents/Registry/Registry.cs +++ b/SafeExamBrowser.SystemComponents/Registry/Registry.cs @@ -229,7 +229,6 @@ namespace SafeExamBrowser.SystemComponents.Registry try { - logger.Info($"default(RegistryKey) == null: {key == null}"); if (TryGetBaseKeyFromKeyName(keyName, out var baseKey, out var subKey)) { key = baseKey.OpenSubKey(subKey); diff --git a/SafeExamBrowser.SystemComponents/SystemInfo.cs b/SafeExamBrowser.SystemComponents/SystemInfo.cs index 207a2fab..770556a6 100644 --- a/SafeExamBrowser.SystemComponents/SystemInfo.cs +++ b/SafeExamBrowser.SystemComponents/SystemInfo.cs @@ -20,6 +20,7 @@ namespace SafeExamBrowser.SystemComponents public class SystemInfo : ISystemInfo { public string BiosInfo { get; private set; } + public string Cpu { get; private set; } public bool HasBattery { get; private set; } public string MacAddress { get; private set; } public string Manufacturer { get; private set; } @@ -33,6 +34,7 @@ namespace SafeExamBrowser.SystemComponents { InitializeBattery(); InitializeBiosInfo(); + InitializeCpu(); InitializeMacAddress(); InitializeMachineInfo(); InitializeOperatingSystem(); @@ -79,6 +81,22 @@ namespace SafeExamBrowser.SystemComponents } } + private void InitializeCpu() + { + try + { + var cpuObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); + foreach (var cpuObj in cpuObjSearcher.Get()) + { + Cpu = ((string) cpuObj["Name"]); + } + } + catch (Exception) + { + Cpu = ""; + } + } + private void InitializeMachineInfo() { var model = default(string); diff --git a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs index 258a9786..22d7042a 100644 --- a/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs +++ b/SafeExamBrowser.SystemComponents/VirtualMachineDetector.cs @@ -57,7 +57,7 @@ namespace SafeExamBrowser.SystemComponents // redundancy: registry check does this aswell (systemInfo may be using different methods) isVirtualMachine |= IsVirtualSystemInfo(biosInfo, manufacturer, model); - isVirtualMachine |= IsVirtualWmi(); + isVirtualMachine |= IsVirtualCpu(); isVirtualMachine |= IsVirtualRegistry(); if (macAddress != null && macAddress.Count() > 2) @@ -213,15 +213,11 @@ namespace SafeExamBrowser.SystemComponents return isVirtualMachine; } - private bool IsVirtualWmi() + private bool IsVirtualCpu() { var isVirtualMachine = false; - var cpuObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); - foreach (var cpuObj in cpuObjSearcher.Get()) - { - isVirtualMachine |= ((string) cpuObj["Name"]).ToLower().Contains(" kvm "); // qemu (KVM specifically) - } + isVirtualMachine |= systemInfo.Cpu.ToLower().Contains(" kvm "); // qemu (KVM specifically) return isVirtualMachine; }