using Estsh.Core.Const;
using Hardware.Info;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Estsh.Core.Util
{
public class LicenseUtil
{
///
/// 软件授权注册码信息,取服务器的硬件信息
///
public static LicenseInfo licenseInfo { get; private set; } = new LicenseInfo();
///
/// 获取服务器硬件信息
///
///
public static void GetLicenseRegistInfo()
{
//此类库兼容Windows linux macOS
IHardwareInfo hardwareInfo = new HardwareInfo();
//hardwareInfo.RefreshMemoryStatus();
//hardwareInfo.RefreshBatteryList();
hardwareInfo.RefreshBIOSList();
hardwareInfo.RefreshCPUList(false);
//hardwareInfo.RefreshDriveList();
//hardwareInfo.RefreshKeyboardList();
//hardwareInfo.RefreshMemoryList();
//hardwareInfo.RefreshMonitorList();
hardwareInfo.RefreshMotherboardList();
//hardwareInfo.RefreshMouseList();
hardwareInfo.RefreshNetworkAdapterList(false);
//hardwareInfo.RefreshPrinterList();
//hardwareInfo.RefreshSoundDeviceList();
//hardwareInfo.RefreshVideoControllerList();
// 在 Windows 中首次使用延迟 21 秒,后续刷新 15 秒,详情参考GitHub上源代码说明
//hardwareInfo.RefreshAll();
foreach (var cpu in hardwareInfo.CpuList)
{
if (!string.IsNullOrEmpty(cpu.ProcessorId))
{
licenseInfo.cpuInfo = cpu.ProcessorId;
break;
}
}
foreach (var bios in hardwareInfo.BiosList)
{
if (!string.IsNullOrEmpty(bios.SerialNumber))
{
licenseInfo.biosInfo = bios.SerialNumber;
break;
}
}
foreach (var motherBoard in hardwareInfo.MotherboardList)
{
if (!string.IsNullOrEmpty(motherBoard.SerialNumber))
{
licenseInfo.motherBoardInfo = motherBoard.SerialNumber;
break;
}
}
foreach (var network in hardwareInfo.NetworkAdapterList)
{
if (network.IPAddressList.Count > 0)
{
licenseInfo.macInfo = network.MACAddress;
break;
}
}
}
///
/// 工厂拿到授权码进行激活
///
/// 授权码
///
public static void LicenseActivate(string licenseE)
{
RsaHelper rsaHelper = new RsaHelper(RSAType.RSA2, Encoding.UTF8, CommonConstWords.RSA_PRIVATE_KEY, CommonConstWords.RSA_PUBLIC_KEY);
LicenseInfo license = JsonConvert.DeserializeObject(rsaHelper.SubDecrypt(licenseE));
if (license != null)
{
//此类库兼容Windows linux macOS
IHardwareInfo hardwareInfo = new HardwareInfo();
hardwareInfo.RefreshBIOSList();
hardwareInfo.RefreshCPUList(false);
hardwareInfo.RefreshMotherboardList();
hardwareInfo.RefreshNetworkAdapterList(false);
if (hardwareInfo.CpuList.Exists(a => a.ProcessorId.Equals(license.cpuInfo))
&& hardwareInfo.BiosList.Exists(a => a.SerialNumber.Equals(license.biosInfo))
&& hardwareInfo.MotherboardList.Exists(a => a.SerialNumber.Equals(license.motherBoardInfo))
&& hardwareInfo.NetworkAdapterList.Exists(a => a.MACAddress.Equals(license.macInfo)))
{
if (!string.IsNullOrEmpty(license.licenseEndDateTime))
{
if (Convert.ToDateTime(license.licenseEndDateTime).CompareTo(DateTime.Now) > 0)
{
licenseInfo.isActivated = true;
licenseInfo.licenseEndDateTime = license.licenseEndDateTime;
licenseInfo.licenseStartDateTime = license.licenseStartDateTime;
}
}
}
}
}
}
///
/// 授权信息
///
[Serializable]
public class LicenseInfo
{
///
/// cpu信息
///
[DataMember]
public string cpuInfo { get; set; }
///
/// bios信息
///
[DataMember]
public string biosInfo { get; set; }
///
/// 主板信息
///
[DataMember]
public string motherBoardInfo { get; set; }
///
/// mac地址信息
///
[DataMember]
public string macInfo { get; set; }
///
/// 授权开始时间
///
[DataMember]
public string licenseStartDateTime { get; set; }
///
/// 授权到期时间
///
[DataMember]
public string licenseEndDateTime { get; set; }
public bool isActivated { get; set; }
}
}