diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-18 13:29:22 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-18 13:29:22 +0200 |
| commit | 45ac8eaf0e03d87c2f9728b2b7c84922c6f6a37d (patch) | |
| tree | f2cd9f1a268c0ef224279bae1d5e817dcaef1d75 /Software/Visual_Studio/PPC | |
| parent | 79f12332efed3507c2316eef9698e5766be31ff8 (diff) | |
| download | Tango-45ac8eaf0e03d87c2f9728b2b7c84922c6f6a37d.tar.gz Tango-45ac8eaf0e03d87c2f9728b2b7c84922c6f6a37d.zip | |
Implemented OS activation + UWF.
Added OSKey to MACHINES table and machine service.
Diffstat (limited to 'Software/Visual_Studio/PPC')
7 files changed, 269 insertions, 4 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs index 971dde32e..145512562 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -19,7 +19,9 @@ using Tango.Integration.Operation; using Tango.PMR.Synchronization; using Tango.PPC.Common.Application; using Tango.PPC.Common.Connection; +using Tango.PPC.Common.OS; using Tango.PPC.Common.RemoteAssistance; +using Tango.PPC.Common.UWF; using Tango.Settings; using Tango.SharedUI.Helpers; using Tango.SQLExaminer; @@ -35,6 +37,8 @@ namespace Tango.PPC.Common.MachineSetup public class MachineSetupManager : ExtendedObject, IMachineSetupManager { private IRemoteAssistanceProvider _remoteAssistance; + private IUnifiedWriteFilterManager _uwf; + private IWindowsActivationManager _windows_activation_manager; #region Events @@ -67,9 +71,11 @@ namespace Tango.PPC.Common.MachineSetup /// Initializes a new instance of the <see cref="MachineSetupManager"/> class. /// </summary> /// <param name="remoteAssistance">The remote assistance.</param> - public MachineSetupManager(IRemoteAssistanceProvider remoteAssistance) + public MachineSetupManager(IRemoteAssistanceProvider remoteAssistance, IUnifiedWriteFilterManager unifiedWriterFilterManager, IWindowsActivationManager windowsActivationManager) { _remoteAssistance = remoteAssistance; + _uwf = unifiedWriterFilterManager; + _windows_activation_manager = windowsActivationManager; } #endregion @@ -134,9 +140,20 @@ namespace Tango.PPC.Common.MachineSetup LogManager.Log($"Machine setup response received: {Environment.NewLine}{setup_response.ToJsonString()}"); - LogManager.Log("Installing remote assistance..."); - UpdateProgress("Installing remote assistance", "Installing..."); - await _remoteAssistance.InstallRemoteAssistance(serialNumber); + if (!demoMode) + { + LogManager.Log("Activating windows license..."); + UpdateProgress("Activating operation system license", "Activating..."); + await _windows_activation_manager.Activate(setup_response.OSKey); + + LogManager.Log("Installing remote assistance..."); + UpdateProgress("Installing remote assistance", "Installing..."); + await _remoteAssistance.InstallRemoteAssistance(serialNumber); + + LogManager.Log("Activating unified write filter..."); + UpdateProgress("Activating disk protection", "Activating..."); + await _uwf.Setup(); + } //Create temporary folders for packages. var _newPackageTempFolder = TemporaryManager.CreateFolder(); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/DefaultWindowsActivationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/DefaultWindowsActivationManager.cs new file mode 100644 index 000000000..2305e24ff --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/DefaultWindowsActivationManager.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Management; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Tango.PPC.Common.Scripting; + +namespace Tango.PPC.Common.OS +{ + using SLID = Guid; //SLID id declaration as typedef GUID SLID; in slpublic.h + + /// <summary> + /// Represents the default windows activation manager. + /// </summary> + /// <seealso cref="Tango.PPC.Common.OS.IWindowsActivationManager" /> + public class DefaultWindowsActivationManager : IWindowsActivationManager + { + private enum SL_GENUINE_STATE + { + SL_GEN_STATE_IS_GENUINE = 0, + SL_GEN_STATE_INVALID_LICENSE = 1, + SL_GEN_STATE_TAMPERED = 2, + SL_GEN_STATE_OFFLINE = 3, + SL_GEN_STATE_LAST = 4 + } + + [DllImportAttribute("Slwga.dll", EntryPoint = "SLIsGenuineLocal", CharSet = CharSet.None, ExactSpelling = false, SetLastError = false, PreserveSig = true, CallingConvention = CallingConvention.Winapi, BestFitMapping = false, ThrowOnUnmappableChar = false)] + [PreserveSigAttribute()] + private static extern uint SLIsGenuineLocal(ref SLID slid, [In, Out] ref SL_GENUINE_STATE genuineState, IntPtr val3); + + /// <summary> + /// Determines whether the OS is activated. + /// </summary> + /// <returns></returns> + public Task<bool> IsActivated() + { + return Task.Factory.StartNew<bool>(() => + { + Guid ApplicationID = new Guid("55c92734-d682-4d71-983e-d6ec3f16059f"); //Application ID GUID http://technet.microsoft.com/en-us/library/dd772270.aspx + SLID windowsSlid = (Guid)ApplicationID; + try + { + SL_GENUINE_STATE genuineState = SL_GENUINE_STATE.SL_GEN_STATE_LAST; + uint ResultInt = SLIsGenuineLocal(ref windowsSlid, ref genuineState, IntPtr.Zero); + if (ResultInt == 0) + { + return (genuineState == SL_GENUINE_STATE.SL_GEN_STATE_IS_GENUINE); + } + else + { + throw new InvalidOperationException("An error occurred while trying to get the OS activation status."); + } + + } + catch (Exception ex) + { + throw ex; + } + }); + } + + /// <summary> + /// Activates the OS using the specified activation key. + /// </summary> + /// <param name="activationKey">The activation key.</param> + /// <returns></returns> + public async Task Activate(string activationKey) + { + CmdCommand command = new CmdCommand("slmgr", $"-ipk {activationKey}"); + await command.Run(); + + if (!await IsActivated()) + { + throw new ApplicationException("The activation was completed but activation status returned a false response."); + } + } + + /// <summary> + /// Deactivates the OS license. + /// </summary> + /// <returns></returns> + /// <exception cref="System.NotImplementedException"></exception> + public Task Deactivate() + { + throw new NotImplementedException("Deactivating windows license is not supported."); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/IWindowsActivationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/IWindowsActivationManager.cs new file mode 100644 index 000000000..2fc79e5bb --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/OS/IWindowsActivationManager.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.OS +{ + /// <summary> + /// Represents a windows license activation manager. + /// </summary> + public interface IWindowsActivationManager + { + /// <summary> + /// Determines whether the OS is activated. + /// </summary> + /// <returns></returns> + Task<bool> IsActivated(); + + /// <summary> + /// Activates the OS using the specified activation key. + /// </summary> + /// <param name="activationKey">The activation key.</param> + /// <returns></returns> + Task Activate(String activationKey); + + /// <summary> + /// Deactivates the OS license. + /// </summary> + /// <returns></returns> + Task Deactivate(); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index faea5ed41..79c970e5a 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -87,6 +87,7 @@ </Reference> <Reference Include="System.Drawing" /> <Reference Include="System.IO.Compression.FileSystem" /> + <Reference Include="System.Management" /> <Reference Include="System.Numerics" /> <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.ServiceModel" /> @@ -158,6 +159,8 @@ <DependentUpon>MessageNotificationItemView.xaml</DependentUpon> </Compile> <Compile Include="Notifications\TaskBarItem.cs" /> + <Compile Include="OS\IWindowsActivationManager.cs" /> + <Compile Include="OS\DefaultWindowsActivationManager.cs" /> <Compile Include="PPCModuleAttribute.cs" /> <Compile Include="PPCModuleBase.cs" /> <Compile Include="Printing\IPrintingManager.cs" /> @@ -175,6 +178,8 @@ <Compile Include="Update\UploadVersionRequest.cs" /> <Compile Include="Update\UploadVersionResponse.cs" /> <Compile Include="Update\PPCUpdateService.cs" /> + <Compile Include="UWF\DefaultUnifiedWriteFilterManager.cs" /> + <Compile Include="UWF\IUnifiedWriteFilterManager.cs" /> <Page Include="Connectivity\AvailableWiFiConnectionsControl.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/DefaultUnifiedWriteFilterManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/DefaultUnifiedWriteFilterManager.cs new file mode 100644 index 000000000..f673171a5 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/DefaultUnifiedWriteFilterManager.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.PPC.Common.Scripting; + +namespace Tango.PPC.Common.UWF +{ + /// <summary> + /// Represents the default unified writer filter manager. + /// </summary> + /// <seealso cref="Tango.PPC.Common.UWF.IUnifiedWriteFilterManager" /> + public class DefaultUnifiedWriteFilterManager : IUnifiedWriteFilterManager + { + private const int UWF_CAPACITY_MB = 5000; + + private String[] _exclusions = + { + @"C:\Users\Tango\AppData\Roaming\Twine", + @"C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA", + @"C:\Program Files (x86)\Twine", + }; + + /// <summary> + /// Gets a value indicating whether UWF if currently enabled on the system. + /// </summary> + public bool IsEnabled { get; } + + /// <summary> + /// Installs and configures the service (requires restart). + /// </summary> + public async Task Setup() + { + CmdCommand command = null; + + command = new CmdCommand("uwfmgr.exe", "overlay twine-type disk"); + await command.Run(); + + command = new CmdCommand("uwfmgr.exe", $"overlay twine-size {UWF_CAPACITY_MB}"); + await command.Run(); + + command = new CmdCommand("uwfmgr.exe", $"overlay twine-warningthreshold {UWF_CAPACITY_MB}"); + await command.Run(); + + command = new CmdCommand("uwfmgr.exe", $"overlay twine-criticalthreshold {UWF_CAPACITY_MB}"); + await command.Run(); + + command = new CmdCommand("uwfmgr.exe", $"volume protect c:"); + await command.Run(); + + foreach (var exclusion in _exclusions) + { + command = new CmdCommand("uwfmgr.exe", $"file add-exclusion \"{exclusion}\""); + await command.Run(); + } + + command = new CmdCommand("uwfmgr.exe", $"filter enable"); + await command.Run(); + } + + /// <summary> + /// Enables the UWF service (requires restart). + /// </summary> + public async Task Enable() + { + CmdCommand command = new CmdCommand("uwfmgr.exe", $"filter enable"); + await command.Run(); + } + + /// <summary> + /// Disables the UWF service (requires restart). + /// </summary> + public async Task Disable() + { + CmdCommand command = new CmdCommand("uwfmgr.exe", $"filter disable"); + await command.Run(); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/IUnifiedWriteFilterManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/IUnifiedWriteFilterManager.cs new file mode 100644 index 000000000..05fa1876a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UWF/IUnifiedWriteFilterManager.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.UWF +{ + /// <summary> + /// Represents a windows unified write filter manager for disk write protection. + /// </summary> + public interface IUnifiedWriteFilterManager + { + /// <summary> + /// Gets a value indicating whether UWF if currently enabled on the system. + /// </summary> + bool IsEnabled { get; } + + /// <summary> + /// Installs and configures the service (requires restart). + /// </summary> + Task Setup(); + + /// <summary> + /// Enables the UWF service (requires restart). + /// </summary> + Task Enable(); + + /// <summary> + /// Disables the UWF service (requires restart). + /// </summary> + Task Disable(); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs index f8b7ce003..80154bc3f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs @@ -16,10 +16,12 @@ using Tango.PPC.Common.MachineUpdate; using Tango.PPC.Common.Modules; using Tango.PPC.Common.Navigation; using Tango.PPC.Common.Notifications; +using Tango.PPC.Common.OS; using Tango.PPC.Common.Printing; using Tango.PPC.Common.RemoteAssistance; using Tango.PPC.Common.Storage; using Tango.PPC.Common.Threading; +using Tango.PPC.Common.UWF; using Tango.PPC.UI.Authentication; using Tango.PPC.UI.Connectivity; using Tango.PPC.UI.Modules; @@ -65,6 +67,8 @@ namespace Tango.PPC.UI TangoIOC.Default.Unregister<IHotSpotProvider>(); TangoIOC.Default.Unregister<IRemoteAssistanceProvider>(); TangoIOC.Default.Unregister<IStorageProvider>(); + TangoIOC.Default.Unregister<IUnifiedWriteFilterManager>(); + TangoIOC.Default.Unregister<IWindowsActivationManager>(); TangoIOC.Default.Register<IDispatcherProvider, DefaultDispatcherProvider>(new DefaultDispatcherProvider(Application.Current.Dispatcher)); TangoIOC.Default.Register<INotificationProvider, DefaultNotificationProvider>(); @@ -84,6 +88,8 @@ namespace Tango.PPC.UI TangoIOC.Default.Register<IConnectivityProvider, DefaultConnectivityProvider>(); TangoIOC.Default.Register<IHotSpotProvider, DefaultHotSpotProvider>(); TangoIOC.Default.Register<IStorageProvider, DefaultStorageProvider>(); + TangoIOC.Default.Register<IUnifiedWriteFilterManager, DefaultUnifiedWriteFilterManager>(); + TangoIOC.Default.Register<IWindowsActivationManager, DefaultWindowsActivationManager>(); //TangoIOC.Default.Register<TeamFoundationServiceExtendedClient>(new TeamFoundationServiceExtendedClient("https://twinetfs.visualstudio.com", String.Empty, "szzfokrceo4rhd4eqi5qpmxn3pa5iwl3q7tlqd36l2m7smz2ynoa")); |
