diff --git a/MasstransferCommon/Model/Enum/LicenseTypeEnum.cs b/MasstransferCommon/Model/Enum/LicenseTypeEnum.cs new file mode 100644 index 0000000..5ec0d7c --- /dev/null +++ b/MasstransferCommon/Model/Enum/LicenseTypeEnum.cs @@ -0,0 +1,13 @@ +namespace MasstransferCommon.Model.Enum; + +/// +/// 防止注册表被恶意篡改,这里的注册许可类型需要做一些混淆 +/// +public enum LicenseTypeEnum +{ + // 临时许可 + Temporary = unchecked(int.MaxValue - 123456), + + // 正式许可 + Formal = unchecked(int.MaxValue - 123457) +} \ No newline at end of file diff --git a/MasstransferCommon/Model/Enum/LockStateEnum.cs b/MasstransferCommon/Model/Enum/LockStateEnum.cs new file mode 100644 index 0000000..b9bc109 --- /dev/null +++ b/MasstransferCommon/Model/Enum/LockStateEnum.cs @@ -0,0 +1,19 @@ +namespace MasstransferCommon.Model.Enum; + +/// +/// 锁定状态 +/// +public enum LockStateEnum +{ + // 锁定状态 + LockedState = int.MaxValue / 3, + + // 等待锁定状态 + WaitToLockedState = int.MaxValue / 4, + + // 解锁状态 + UnLockedState = int.MaxValue / 5, + + // 等待解锁状态 + WaitToUnLockedState = int.MaxValue / 6 +} \ No newline at end of file diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index 4503ab9..c58cb27 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -1,7 +1,22 @@ -class Program +using MasstransferExporter.RemoteControl; +using MasstransferExporter.RemoteControl.Model; + +class Program { static void Main() { + Thread.Sleep(30000); + + var cmd = new LockCmd + { + Action = 1, + ExpiryTime = 0, + LockType = 0 + }; + + RemoteLockService.HandleLockCmd(cmd); + + Console.WriteLine("按任意键退出"); Console.ReadKey(); } diff --git a/MasstransferExporter/RemoteControl/Model/LockCmd.cs b/MasstransferExporter/RemoteControl/Model/LockCmd.cs new file mode 100644 index 0000000..cc19b23 --- /dev/null +++ b/MasstransferExporter/RemoteControl/Model/LockCmd.cs @@ -0,0 +1,23 @@ +namespace MasstransferExporter.RemoteControl.Model; + +/// +/// 锁机指令 +/// +public class LockCmd +{ + /// + /// 0 锁机 1 解锁 + /// + public int Action { get; set; } + + /// + /// 0 立即执行 + /// 1 到期执行 + /// + public int LockType { get; set; } + + /// + /// 到期时间 + /// + public long ExpiryTime { get; set; } +} \ No newline at end of file diff --git a/MasstransferExporter/RemoteControl/RemoteLockService.cs b/MasstransferExporter/RemoteControl/RemoteLockService.cs new file mode 100644 index 0000000..6973ca6 --- /dev/null +++ b/MasstransferExporter/RemoteControl/RemoteLockService.cs @@ -0,0 +1,56 @@ +using MasstransferCommon.Model.Enum; +using MasstransferCommon.Utils; +using MasstransferExporter.RemoteControl.Model; +using Microsoft.Win32; + +namespace MasstransferExporter.RemoteControl; + +/// +/// 远程锁定服务 +/// +public class RemoteLockService +{ + private const string KeyPath = @"Software\Masstransfer\Security"; + + /// + /// 处理接收到锁机业务指令 + /// + public static void HandleLockCmd(LockCmd cmd) + { + var action = cmd.Action; + + if (action == 0) + { + // 更新注册表锁机状态 + var lockType = cmd.LockType; + if (lockType == 0) + { + RegistryHelper.WriteValue(KeyPath, "LockState", LockStateEnum.LockedState, RegistryValueKind.DWord); + } + else + { + RegistryHelper.WriteValue(KeyPath, "LockState", LockStateEnum.WaitToLockedState, + RegistryValueKind.DWord); + // 写入超时的时间 + RegistryHelper.WriteValue(KeyPath, "ExpireTime", cmd.ExpiryTime, RegistryValueKind.DWord); + } + } + else + { + // 解锁 + var lockType = cmd.LockType; + if (lockType == 0) + { + RegistryHelper.WriteValue(KeyPath, "LockState", LockStateEnum.UnLockedState, + RegistryValueKind.DWord); + } + else + { + RegistryHelper.WriteValue(KeyPath, "LockState", LockStateEnum.WaitToUnLockedState, + RegistryValueKind.DWord); + // 写入等待解锁超时的时间 + RegistryHelper.WriteValue(KeyPath, "ExpireTime", cmd.ExpiryTime, RegistryValueKind.DWord); + } + } + } +} \ No newline at end of file