KeyManager.cs 10 KB

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