You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 软件授权注册码信息,取服务器的硬件信息
/// </summary>
public static LicenseInfo licenseInfo { get; private set; } = new LicenseInfo();
/// <summary>
/// 获取服务器硬件信息
/// </summary>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// 工厂拿到授权码进行激活
/// </summary>
/// <param name="licenseE">授权码</param>
/// <returns></returns>
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<LicenseInfo>(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;
}
}
}
}
}
}
/// <summary>
/// 授权信息
/// </summary>
[Serializable]
public class LicenseInfo
{
/// <summary>
/// cpu信息
/// </summary>
[DataMember]
public string cpuInfo { get; set; }
/// <summary>
/// bios信息
/// </summary>
[DataMember]
public string biosInfo { get; set; }
/// <summary>
/// 主板信息
/// </summary>
[DataMember]
public string motherBoardInfo { get; set; }
/// <summary>
/// mac地址信息
/// </summary>
[DataMember]
public string macInfo { get; set; }
/// <summary>
/// 授权开始时间
/// </summary>
[DataMember]
public string licenseStartDateTime { get; set; }
/// <summary>
/// 授权到期时间
/// </summary>
[DataMember]
public string licenseEndDateTime { get; set; }
public bool isActivated { get; set; }
}
}