Licensing.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.Extensions.Logging;
  2. using Microsoft.Extensions.Logging.Abstractions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.NetworkInformation;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Edge.Core
  12. {
  13. public class Licensing
  14. {
  15. private static Licensing _licensing;
  16. private string licPath;
  17. private Licensing()
  18. {
  19. licPath = @"data.bin";
  20. }
  21. public static Licensing Instance()
  22. {
  23. if (_licensing == null)
  24. {
  25. _licensing = new Licensing();
  26. }
  27. return _licensing;
  28. }
  29. public async Task<bool> CheckLicense(string license = "")
  30. {
  31. var result = false;
  32. if (File.Exists(licPath))
  33. {
  34. //存在文件
  35. using (var sr = new StreamReader(licPath))
  36. {
  37. string encryptedLicense = null;
  38. var licenseLimit = 10;
  39. while ((encryptedLicense = sr.ReadLine()) != null && licenseLimit-- > 0)
  40. {
  41. result = IsValidLicense(encryptedLicense.Trim(), GetMacAddress());
  42. if (result == true)
  43. {
  44. break;
  45. }
  46. }
  47. sr.Close();
  48. }
  49. }
  50. return result;
  51. }
  52. public async Task<bool> VerifyLicense(string license)
  53. {
  54. var result = IsValidLicense(license, GetMacAddress());
  55. if (result)
  56. {
  57. if (Environment.OSVersion.Platform == PlatformID.Unix)
  58. {
  59. using (TextWriter sr = new StreamWriter(licPath, true))
  60. {
  61. license = license.Replace(" ", "").Replace("\n", "").Replace("\r", ""); ;
  62. sr.NewLine = "\n";
  63. sr.WriteLine(license);
  64. sr.Close();
  65. }
  66. }
  67. else
  68. {
  69. using (var sr = new StreamWriter(licPath, true))
  70. {
  71. sr.WriteLine(license);
  72. sr.Close();
  73. }
  74. }
  75. }
  76. return result;
  77. }
  78. public string GetMacAddress(ILogger logger)
  79. {
  80. NetworkInterface.GetAllNetworkInterfaces().ToList().ForEach(i => logger.LogInformation($"Name: {i.Description}, " +
  81. $"PhysicalAddress: {i.GetPhysicalAddress()}, NetworkType: {i.NetworkInterfaceType}, Platform {Environment.OSVersion.Platform}"));
  82. return GetMacAddress();
  83. }
  84. public static string MD5Encrypt(string strText)
  85. {
  86. var md5 = new MD5CryptoServiceProvider();
  87. byte[] result = md5.ComputeHash(Encoding.UTF8.GetBytes(strText));
  88. return BitConverter.ToString(result).Replace("-", "");
  89. }
  90. private bool IsValidLicense(string encryptedLicense, string macString)
  91. {
  92. var s = macString + "&key=Wayne123";
  93. var encrypted = MD5Encrypt(s);
  94. return encryptedLicense == encrypted.Substring(0, 8);
  95. }
  96. private string GetMacAddress()
  97. {
  98. string firstMacAddress = string.Empty;
  99. if (Environment.OSVersion.Platform == PlatformID.Unix)
  100. {
  101. firstMacAddress = NetworkInterface
  102. .GetAllNetworkInterfaces()
  103. //nic.OperationalStatus == OperationalStatus.Up
  104. .Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.Description.Contains("eth"))
  105. .Select(nic => nic.GetPhysicalAddress().ToString())
  106. .FirstOrDefault();
  107. }
  108. else
  109. {
  110. firstMacAddress = NetworkInterface
  111. .GetAllNetworkInterfaces()
  112. //nic.OperationalStatus == OperationalStatus.Up
  113. .Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && !nic.Description.Contains("Virtual"))
  114. .Select(nic => nic.GetPhysicalAddress().ToString())
  115. .FirstOrDefault();
  116. }
  117. return firstMacAddress;
  118. }
  119. }
  120. }