KeyManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Aitex.Core.RT.Device.Unit;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.Util;
  8. using Microsoft.Win32;
  9. namespace Aitex.Core.RT.Key
  10. {
  11. public class KeyManager : Singleton<KeyManager>
  12. {
  13. public const string KeyTag = "Jet";
  14. public const string RegistryKey = "SOFTWARE\\Jet\\Keys";
  15. public string LocalMachineCode
  16. {
  17. get; private set;
  18. }
  19. public bool IsExpired
  20. {
  21. get
  22. {
  23. //return ExpireDateTime < CurrentDateTime;
  24. return false;
  25. }
  26. }
  27. public int LeftDays
  28. {
  29. get
  30. {
  31. if (IsExpired)
  32. return 0;
  33. return (ExpireDateTime - CurrentDateTime).Days;
  34. }
  35. }
  36. public DateTime CurrentDateTime
  37. {
  38. get
  39. {
  40. //if (_plcDateTime != null)
  41. // return _plcDateTime.CurrentDateTime;
  42. return DateTime.Now;
  43. }
  44. }
  45. public DateTime ExpireDateTime
  46. {
  47. get
  48. {
  49. return LastRegisterDateTime + new TimeSpan(LastRegisterDays, 0, 0, 0);
  50. }
  51. }
  52. public DateTime LastRegisterDateTime
  53. {
  54. get; set;
  55. }
  56. public int LastRegisterDays
  57. {
  58. get; set;
  59. }
  60. private PeriodicJob _thread;
  61. Dictionary<string, string> _historyKeys = new Dictionary<string, string>();
  62. private IoPlcDateTime _plcDateTime;
  63. object _locker = new object();
  64. DeviceTimer _timer0Days = new DeviceTimer();
  65. R_TRIG _trig0Days = new R_TRIG();
  66. DeviceTimer _timer3Days = new DeviceTimer();
  67. R_TRIG _trig3Days = new R_TRIG();
  68. DeviceTimer _timer7Days = new DeviceTimer();
  69. R_TRIG _trig7Days = new R_TRIG();
  70. DeviceTimer _timer15Days = new DeviceTimer();
  71. R_TRIG _trig15Days = new R_TRIG();
  72. public void Initialize(IoPlcDateTime plcDateTime)
  73. {
  74. _plcDateTime = plcDateTime;
  75. LocalMachineCode = new MachineCoder().CreateCode();
  76. UpdateLicenseInformation();
  77. _thread = new PeriodicJob(1000, OnTimer, "Register Key Thread", true);
  78. }
  79. void UpdateLicenseInformation()
  80. {
  81. try
  82. {
  83. RegistryKey regRoot = Registry.CurrentUser;
  84. RegistryKey regKeys = regRoot.OpenSubKey(RegistryKey, true);
  85. if (regKeys == null)
  86. {
  87. regKeys = regRoot.CreateSubKey(RegistryKey);
  88. }
  89. if (regKeys == null)
  90. {
  91. throw new ApplicationException("注册表操作失败,无法进行软件授权操作。\r\n请确保用管理员权限运行程序。");
  92. }
  93. foreach (var date in regKeys.GetSubKeyNames())
  94. {
  95. RegistryKey sub = regKeys.OpenSubKey(date);
  96. _historyKeys.Add(date, (string)sub.GetValue("Key"));
  97. }
  98. if (_historyKeys.Count == 0)
  99. {
  100. string reason;
  101. if (!Register(7, "---", out reason))
  102. throw new ApplicationException(reason);
  103. }
  104. else
  105. {
  106. List<string> date = _historyKeys.Keys.ToList();
  107. date.Sort();
  108. string last = date.Last();
  109. RegistryKey sub = regKeys.OpenSubKey(last);
  110. LastRegisterDateTime = new DateTime(int.Parse(last.Substring(0,4)),int.Parse(last.Substring(4,2)),int.Parse(last.Substring(6,2)),
  111. int.Parse(last.Substring(8,2)),int.Parse(last.Substring(10,2)),int.Parse(last.Substring(12,2)));
  112. LastRegisterDays = int.Parse(sub.GetValue("Days").ToString());
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. throw new ApplicationException(ex.Message);
  118. }
  119. }
  120. bool OnTimer()
  121. {
  122. try
  123. {
  124. _trig0Days.CLK = IsExpired;
  125. _trig3Days.CLK = LeftDays <= 3 && !IsExpired;
  126. _trig7Days.CLK = LeftDays <= 7 && LeftDays > 3;
  127. _trig15Days.CLK = LeftDays <= 15 && LeftDays > 7;
  128. if (_trig0Days.M) //到期之后,每1个小时提醒一次,预警形式
  129. {
  130. if (_trig0Days.Q)
  131. {
  132. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software expired at {0} ", ExpireDateTime.ToString()));
  133. _timer0Days.Start(1000 * 60 * 60 * 500);
  134. }
  135. if (_timer0Days.IsTimeout())
  136. {
  137. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software expired at {0} ", ExpireDateTime.ToString()));
  138. _timer0Days.Start(1000 * 60 * 60 * 500);
  139. }
  140. }
  141. if (_trig3Days.M) //3天内到期,每3个小时提醒一次,预警形式
  142. {
  143. if (_trig3Days.Q)
  144. {
  145. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software will be expired in {0} days, at {1} ", LeftDays,ExpireDateTime.ToString()));
  146. _timer3Days.Start(1000 * 60 * 60 * 500);
  147. }
  148. if (_timer3Days.IsTimeout())
  149. {
  150. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software will be expired in {0} days, at {1}", LeftDays,ExpireDateTime.ToString()));
  151. _timer3Days.Start(1000 * 60 * 60 * 500);
  152. }
  153. }
  154. if (_trig7Days.M)//7天内到期,每8个小时提醒一次,预警形式
  155. {
  156. if (_trig7Days.Q)
  157. {
  158. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software will be expired in {0} days, at {1} ", LeftDays,ExpireDateTime.ToString()));
  159. _timer7Days.Start(1000 * 60 * 60 * 500);
  160. }
  161. if (_timer7Days.IsTimeout())
  162. {
  163. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Software will be expired in {0} days, at {1}", LeftDays,ExpireDateTime.ToString()));
  164. _timer7Days.Start(1000 * 60 * 60 * 500);
  165. }
  166. }
  167. if (_trig15Days.M)//15天内到期,每8个小时提醒一次,信息形式
  168. {
  169. if (_trig15Days.Q)
  170. {
  171. EV.PostMessage("System", EventEnum.GeneralInfo, string.Format("Software will be expired in {0} days, at {1} ", LeftDays,ExpireDateTime.ToString()));
  172. _timer15Days.Start(1000 * 60 * 60 * 500);
  173. }
  174. if (_timer15Days.IsTimeout())
  175. {
  176. EV.PostMessage("System", EventEnum.GeneralInfo, string.Format("Software will be expired in {0} days, at {1}", LeftDays,ExpireDateTime.ToString()));
  177. _timer15Days.Start(1000 * 60 * 60 * 500);
  178. }
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. LOG.Write(ex);
  184. }
  185. return true;
  186. }
  187. bool Register(int days, string key, out string reason)
  188. {
  189. try
  190. {
  191. if (_historyKeys.ContainsValue(key))
  192. {
  193. reason = "注册码已经使用," + key;
  194. LOG.Write(reason);
  195. return false;
  196. }
  197. if (days < 0)
  198. {
  199. reason = "授权天数无效," + days;
  200. LOG.Write(reason);
  201. return false;
  202. }
  203. RegistryKey regRoot = Registry.CurrentUser;
  204. RegistryKey regKeys = regRoot.OpenSubKey(RegistryKey, true);
  205. if (regKeys == null)
  206. {
  207. regKeys = regRoot.CreateSubKey(RegistryKey);
  208. }
  209. if (regKeys == null)
  210. {
  211. reason = "注册表操作失败,无法进行软件授权操作。\r\n请确保用管理员权限运行程序。";
  212. LOG.Write(reason);
  213. return false;
  214. }
  215. DateTime now = CurrentDateTime;
  216. string itemName = now.ToString("yyyyMMddHHmmss");
  217. RegistryKey rkItem = regKeys.CreateSubKey(itemName);
  218. rkItem.SetValue("Date", itemName);
  219. rkItem.SetValue("Key", key);
  220. rkItem.SetValue("Days", days.ToString());
  221. regKeys.Close();
  222. _historyKeys.Add(itemName, key);
  223. LastRegisterDateTime = now;
  224. LastRegisterDays = days;
  225. }
  226. catch (Exception ex)
  227. {
  228. reason = "注册码无效," + ex;
  229. LOG.Write(reason);
  230. return false;
  231. }
  232. reason = "注册成功";
  233. return true;
  234. }
  235. public bool Register(string key, out string reason)
  236. {
  237. reason = string.Empty;
  238. RsaCryption rsa = new RsaCryption();
  239. string decry = string.Empty;
  240. try
  241. {
  242. decry = rsa.RSADecrypt(JetKey.PrivateKey, key);
  243. }
  244. catch (Exception ex)
  245. {
  246. reason = "注册码无效";
  247. LOG.Write(ex);
  248. return false;
  249. }
  250. string[] text = decry.Split(',');
  251. int days = 0;
  252. if (text.Length ==3 && text[0]==KeyTag && text[1]==LocalMachineCode && int.TryParse(text[2], out days))
  253. {
  254. return Register(days, key, out reason);
  255. }
  256. LOG.Error("注册码无效," + decry);
  257. reason = "注册码无效";
  258. return false;
  259. }
  260. }
  261. }