DaemonUtil.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using NTMiner.Ws;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. /// <summary>
  8. /// 注意不要挪动这里的命名空间也不要挪动该代码文件所处的程序集
  9. /// 嵌入的资源的位置和命名空间有关契约关系
  10. /// </summary>
  11. namespace NTMiner.Daemon {
  12. public static class DaemonUtil {
  13. public static void RunNTMinerDaemon() {
  14. if (ClientAppType.IsMinerStudio) {
  15. return;
  16. }
  17. string processName = Path.GetFileNameWithoutExtension(NTKeyword.NTMinerDaemonFileName);
  18. Process[] processes = Process.GetProcessesByName(processName);
  19. if (processes.Length != 0) {
  20. string thatVersion = NTMinerRegistry.GetDaemonVersion();
  21. try {
  22. string thisVersion = ThisNTMinerDaemonFileVersion;
  23. if (thatVersion != thisVersion) {
  24. Logger.InfoDebugLine($"发现新版Daemon:{thatVersion}->{thisVersion}");
  25. RpcRoot.Client.NTMinerDaemonService.CloseDaemonAsync(() => {
  26. System.Threading.Thread.Sleep(1000);
  27. Windows.TaskKill.Kill(processName, waitForExit: true);
  28. System.Threading.Thread.Sleep(1000);
  29. VirtualRoot.Execute(new RefreshWsStateCommand(new WsClientState {
  30. Status = WsClientStatus.Closed,
  31. Description = "更新守护程序中…",
  32. LastTryOn = DateTime.Now,
  33. NextTrySecondsDelay = 10
  34. }));
  35. ExtractRunNTMinerDaemonAsync();
  36. });
  37. }
  38. }
  39. catch (Exception e) {
  40. Logger.ErrorDebugLine(e);
  41. }
  42. }
  43. else {
  44. ExtractRunNTMinerDaemonAsync();
  45. }
  46. }
  47. private static void ExtractRunNTMinerDaemonAsync() {
  48. Task.Factory.StartNew(() => {
  49. ExtractResource(NTKeyword.NTMinerDaemonFileName);
  50. Windows.Cmd.RunClose(MinerClientTempPath.DaemonFileFullName, "--bootByMinerClient", waitForExit: true, createNoWindow: !DevMode.IsDevMode);
  51. Logger.OkDebugLine("守护进程启动成功");
  52. });
  53. }
  54. public static void RunDevConsoleAsync(string poolIp, string consoleTitle) {
  55. if (ClientAppType.IsMinerStudio) {
  56. return;
  57. }
  58. Task.Factory.StartNew(() => {
  59. if (!File.Exists(MinerClientTempPath.DevConsoleFileFullName)) {
  60. ExtractResource(NTKeyword.DevConsoleFileName);
  61. Logger.OkDebugLine("DevConsole解压成功");
  62. }
  63. else if (HashUtil.Sha1(File.ReadAllBytes(MinerClientTempPath.DevConsoleFileFullName)) != ThisDevConsoleFileVersion) {
  64. Windows.TaskKill.Kill(NTKeyword.DevConsoleFileName, waitForExit: true);
  65. ExtractResource(NTKeyword.DevConsoleFileName);
  66. Logger.OkDebugLine("发现新版DevConsole,更新成功");
  67. }
  68. string argument = poolIp + " " + consoleTitle;
  69. Process.Start(MinerClientTempPath.DevConsoleFileFullName, argument);
  70. Logger.OkDebugLine("DevConsole启动成功");
  71. });
  72. }
  73. private static void ExtractResource(string name) {
  74. try {
  75. Type type = typeof(DaemonUtil);
  76. Assembly assembly = type.Assembly;
  77. string daemonDir = Path.GetDirectoryName(MinerClientTempPath.DaemonFileFullName);
  78. assembly.ExtractManifestResource(type, name, Path.Combine(daemonDir, name));
  79. }
  80. catch (Exception e) {
  81. Logger.ErrorDebugLine(e);
  82. }
  83. }
  84. private static string s_thisDevConsoleFileVersion;
  85. private static string ThisDevConsoleFileVersion {
  86. get {
  87. if (s_thisDevConsoleFileVersion == null) {
  88. try {
  89. string name = NTKeyword.DevConsoleFileName;
  90. Type type = typeof(DaemonUtil);
  91. Assembly assembly = type.Assembly;
  92. using (var stream = assembly.GetManifestResourceStream(type, name)) {
  93. byte[] data = new byte[stream.Length];
  94. stream.Read(data, 0, data.Length);
  95. s_thisDevConsoleFileVersion = HashUtil.Sha1(data);
  96. }
  97. }
  98. catch (Exception e) {
  99. Logger.ErrorDebugLine(e);
  100. s_thisDevConsoleFileVersion = string.Empty;
  101. }
  102. }
  103. return s_thisDevConsoleFileVersion;
  104. }
  105. }
  106. private static string s_thisNTMinerDaemonFileVersion;
  107. private static string ThisNTMinerDaemonFileVersion {
  108. get {
  109. if (s_thisNTMinerDaemonFileVersion == null) {
  110. try {
  111. string name = "sha1";
  112. Type type = typeof(DaemonUtil);
  113. Assembly assembly = type.Assembly;
  114. using (var stream = assembly.GetManifestResourceStream(type, name)) {
  115. byte[] data = new byte[stream.Length];
  116. stream.Read(data, 0, data.Length);
  117. s_thisNTMinerDaemonFileVersion = System.Text.Encoding.UTF8.GetString(data);
  118. }
  119. }
  120. catch (Exception e) {
  121. Logger.ErrorDebugLine(e);
  122. s_thisNTMinerDaemonFileVersion = string.Empty;
  123. }
  124. }
  125. return s_thisNTMinerDaemonFileVersion;
  126. }
  127. }
  128. }
  129. }