KeyManager.cs 11 KB

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