KeyManager.cs 10 KB

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