KeyManager.cs 11 KB

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