private static void KernelProcessDaemon(IMineContext mineContext, Action clear) { if (_kernelProcessDaemon != null) { VirtualRoot.DeletePath(_kernelProcessDaemon); _kernelProcessDaemon = null; clear?.Invoke(); } string processName = mineContext.Kernel.GetProcessName(); _kernelProcessDaemon = VirtualRoot.BuildEventPath <Per1MinuteEvent>("周期性检查挖矿内核是否消失,如果消失尝试重启", LogEnum.DevConsole, action: message => { if (mineContext == Instance.LockedMineContext) { if (!string.IsNullOrEmpty(processName)) { Process[] processes = Process.GetProcessesByName(processName); if (processes.Length == 0) { mineContext.AutoRestartKernelCount += 1; VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), processName + $"挖矿内核进程消失", toConsole: true); if (Instance.MinerProfile.IsAutoRestartKernel && mineContext.AutoRestartKernelCount <= Instance.MinerProfile.AutoRestartKernelTimes) { VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"尝试第{mineContext.AutoRestartKernelCount.ToString()}次重启,共{Instance.MinerProfile.AutoRestartKernelTimes.ToString()}次", toConsole: true); Instance.RestartMine(); Instance.LockedMineContext.AutoRestartKernelCount = mineContext.AutoRestartKernelCount; } else { Instance.StopMineAsync(StopMineReason.KernelProcessLost); } if (_kernelProcessDaemon != null) { VirtualRoot.DeletePath(_kernelProcessDaemon); clear?.Invoke(); } } } } else { if (_kernelProcessDaemon != null) { VirtualRoot.DeletePath(_kernelProcessDaemon); _kernelProcessDaemon = null; clear?.Invoke(); } } }); }
// 从磁盘读取local.json反序列化为LocalJson对象 private static void LocalJsonInit() { if (!_localJsonInited) { lock (_locker) { if (!_localJsonInited) { string localJson = HomePath.ReadLocalJsonFile(_workType); LocalJsonDb localJsonDb = null; if (!string.IsNullOrEmpty(localJson)) { localJsonDb = VirtualRoot.JsonSerializer.Deserialize <LocalJsonDb>(localJson); } if (localJsonDb == null) { if (ClientAppType.IsMinerClient) { localJsonDb = JsonDb.LocalJsonDb.ConvertFromNTMinerContext(); VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), "当前作业由本机数据自动生成,因为本机没有作业记录,请先在群控端创建或编辑作业。", OutEnum.Warn, toConsole: true); } else { localJsonDb = new LocalJsonDb(); } } _localJsonDb = localJsonDb; if (ClientAppType.IsMinerClient) { #region 因为是群控作业,将开机启动和自动挖矿设置为true var repository = new LiteDbReadWriteRepository <MinerProfileData>(HomePath.LocalDbFileFullName); MinerProfileData localProfile = repository.GetByKey(MinerProfileData.DefaultId); if (localProfile != null) { MinerProfileData.CopyWorkIgnoreValues(localProfile, _localJsonDb.MinerProfile); // 如果是作业模式则必须设置为开机自动重启 if (!localProfile.IsAutoBoot) { localProfile.IsAutoBoot = true; repository.Update(localProfile); } } _localJsonDb.MinerProfile.IsAutoBoot = true; NTMinerRegistry.SetIsAutoStart(true); #endregion #region 矿机名 if (!string.IsNullOrEmpty(_workerName)) { _localJsonDb.MinerProfile.MinerName = _workerName; } else { // 当用户使用群控作业但没有指定群控矿机名时使用从local.litedb中读取的矿工名 if (string.IsNullOrEmpty(_localJsonDb.MinerProfile.MinerName)) { if (localProfile != null) { _localJsonDb.MinerProfile.MinerName = localProfile.MinerName; } // 如果local.litedb中也没有矿机名则使用去除了特殊符号的本机机器名作为矿机名 if (string.IsNullOrEmpty(_localJsonDb.MinerProfile.MinerName)) { _localJsonDb.MinerProfile.MinerName = NTKeyword.GetSafeMinerName(ThisPcName); } } } #endregion } _localJsonInited = true; } } } }
public void Init(Action callback) { Task.Factory.StartNew(() => { bool isSelfWork = Environment.GetCommandLineArgs().Contains("--selfWork", StringComparer.OrdinalIgnoreCase); bool isWork = isSelfWork || Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); _workType = isSelfWork ? WorkType.SelfWork : (isWork ? WorkType.MineWork : WorkType.None); if (ClientAppType.IsMinerClient) { NTMinerRegistry.SetWorkType(_workType); } if (isWork) { DoInit(callback); } else { // 如果是Debug模式且不是群控客户端则使用本地数据库初始化 bool useLocalDb = DevMode.IsDevMode && !ClientAppType.IsMinerStudio; if (useLocalDb) { DoInit(callback); } else { Logger.InfoDebugLine(nameof(RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson)); RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine($"{nameof(RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson)} ok"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { HomePath.WriteServerJsonFile(serverJson); } RpcRoot.OfficialServer.AppSettingService.GetJsonFileVersionAsync(ClientAppType.AppType, HomePath.ExportServerJsonFileName, serverState => { SetServerJsonVersion(serverState.JsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion); if (serverState.Time == 0) { NTMinerConsole.UserWarn("网络不通或服务器暂时不可用,请检查矿机网络"); } else if (Math.Abs((long)Timestamp.GetTimestamp() - (long)serverState.Time) >= Timestamp.DesyncSeconds) { NTMinerConsole.UserWarn($"本机和服务器时间不同步,请调整,本地:{DateTime.Now.ToString()},服务器:{Timestamp.FromTimestamp(serverState.Time).ToString()}。此问题不影响挖矿。"); } }); } else { if (!File.Exists(HomePath.ServerJsonFileFullName)) { VirtualRoot.ThisLocalError(nameof(NTMinerContext), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn); } else { VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), "配置文件下载失败,使用最近一次成功下载的配置文件"); } } DoInit(callback); }); } } VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), $"启动{VirtualRoot.AppName}"); }); }
private void Link() { VirtualRoot.BuildCmdPath <RegCmdHereCommand>(path: message => { try { Cmd.RegCmdHere(); VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), "添加windows右键命令行成功"); } catch (Exception e) { Logger.ErrorDebugLine(e); VirtualRoot.ThisLocalError(nameof(NTMinerContext), "添加windows右键命令行失败", OutEnum.Warn); } }, location: this.GetType()); VirtualRoot.BuildCmdPath <UnRegCmdHereCommand>(path: message => { try { Cmd.UnRegCmdHere(); VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), "移除windows右键命令行成功"); } catch (Exception e) { Logger.ErrorDebugLine(e); VirtualRoot.ThisLocalError(nameof(NTMinerContext), "移除windows右键命令行失败", OutEnum.Warn); } }, location: this.GetType()); VirtualRoot.BuildEventPath <Per1MinuteEvent>("每1分钟阻止系统休眠", LogEnum.None, path: message => { Power.PreventSleep(MinerProfile.IsPreventDisplaySleep); }, location: this.GetType()); #region 挖矿开始时将无份额内核重启份额计数置0 int shareCount = 0; DateTime shareOn = DateTime.Now; DateTime highSpeedOn = DateTime.Now; DateTime overClockHighSpeedOn = DateTime.Now; VirtualRoot.BuildEventPath <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole, path: message => { // 将无份额内核重启份额计数置0 shareCount = 0; highSpeedOn = DateTime.Now; overClockHighSpeedOn = DateTime.Now; if (!message.MineContext.IsRestart) { // 当不是内核重启时更新shareOn,如果是内核重启不用更新shareOn从而给不干扰无内核矿机重启的逻辑 shareOn = DateTime.Now; } }, location: this.GetType()); #endregion #region 每20秒钟检查是否需要重启 VirtualRoot.BuildEventPath <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None, path: message => { #region 低算力重启电脑 if (IsMining && LockedMineContext.ProcessCreatedOn != DateTime.MinValue) { var coinProfile = MinerProfile.GetCoinProfile(MinerProfile.CoinId); if (coinProfile.IsLowSpeedRestartComputer && coinProfile.LowSpeed != 0 && coinProfile.LowSpeedRestartComputerMinutes > 0) { IGpuSpeed totalSpeed = GpusSpeed.CurrentSpeed(GpuAllId); if (totalSpeed.MainCoinSpeed.SpeedOn.AddMinutes(coinProfile.LowSpeedRestartComputerMinutes) >= message.BornOn) { if (totalSpeed.MainCoinSpeed.Value.ToNearSpeed(coinProfile.LowSpeed) >= coinProfile.LowSpeed) { highSpeedOn = message.BornOn; } } if (highSpeedOn.AddMinutes(coinProfile.LowSpeedRestartComputerMinutes) < message.BornOn) { string coinCode = string.Empty; if (ServerContext.CoinSet.TryGetCoin(MinerProfile.CoinId, out ICoin coin)) { coinCode = coin.Code; } VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{coinCode}总算力持续{coinProfile.LowSpeedRestartComputerMinutes}分钟低于{coinProfile.LowSpeed}重启电脑", toConsole: true); VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10)); if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart) { VirtualRoot.Execute(new SetAutoStartCommand(true, true)); } return; } } else { highSpeedOn = message.BornOn; } } #endregion #region 低算力重新应用超频 if (IsMining && LockedMineContext.ProcessCreatedOn != DateTime.MinValue) { var coinProfile = MinerProfile.GetCoinProfile(MinerProfile.CoinId); if (coinProfile.IsLowSpeedReOverClock && coinProfile.OverClockLowSpeed != 0 && coinProfile.LowSpeedReOverClockMinutes > 0) { IGpuSpeed totalSpeed = GpusSpeed.CurrentSpeed(GpuAllId); if (totalSpeed.MainCoinSpeed.SpeedOn.AddMinutes(coinProfile.LowSpeedReOverClockMinutes) >= message.BornOn) { if (totalSpeed.MainCoinSpeed.Value.ToNearSpeed(coinProfile.OverClockLowSpeed) >= coinProfile.OverClockLowSpeed) { overClockHighSpeedOn = message.BornOn; } } if (overClockHighSpeedOn.AddMinutes(coinProfile.LowSpeedReOverClockMinutes) < message.BornOn) { string coinCode = string.Empty; if (ServerContext.CoinSet.TryGetCoin(MinerProfile.CoinId, out ICoin coin)) { coinCode = coin.Code; } VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{coinCode}总算力持续{coinProfile.LowSpeedReOverClockMinutes}分钟低于{coinProfile.OverClockLowSpeed}重新应用超频", toConsole: true); VirtualRoot.Execute(new CoinOverClockCommand(MinerProfile.CoinId)); } } else { overClockHighSpeedOn = message.BornOn; } } #endregion #region 周期重启电脑 try { if (MinerProfile.IsPeriodicRestartComputer) { if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes) { string content = $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartComputerMinutes.ToString()}分钟重启电脑"; VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), content, toConsole: true); VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10)); if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart) { VirtualRoot.Execute(new SetAutoStartCommand(true, true)); } return; // 退出 } } } catch (Exception e) { Logger.ErrorDebugLine(e); } #endregion #region 周期重启内核 try { if (IsMining && MinerProfile.IsPeriodicRestartKernel && LockedMineContext.MineStartedOn != DateTime.MinValue) { DateTime dt = GetKernelRestartBaseOnTime(); if ((DateTime.Now - dt).TotalMinutes > 60 * MinerProfile.PeriodicRestartKernelHours + MinerProfile.PeriodicRestartKernelMinutes) { VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartKernelMinutes.ToString()}分钟重启内核", toConsole: true); RestartMine(); return; // 退出 } } } catch (Exception e) { Logger.ErrorDebugLine(e); } #endregion #region 无份额重启内核 try { if (IsMining && this.LockedMineContext.MainCoin != null) { int totalShare = 0; bool restartComputer = MinerProfile.NoShareRestartComputerMinutes > 0 && MinerProfile.IsNoShareRestartComputer && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes; bool restartKernel = MinerProfile.NoShareRestartKernelMinutes > 0 && MinerProfile.IsNoShareRestartKernel && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes; if (restartComputer || restartKernel) { ICoinShare mainCoinShare = this.CoinShareSet.GetOrCreate(this.LockedMineContext.MainCoin.GetId()); totalShare = mainCoinShare.TotalShareCount; if ((this.LockedMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null) { ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId()); totalShare += dualCoinShare.TotalShareCount; } // 如果份额没有增加 if (shareCount == totalShare) { // 重启电脑,基于MineStartedOn bool isRestartComputerMinutes = (DateTime.Now - this.LockedMineContext.MineStartedOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes; if (restartComputer && isRestartComputerMinutes) { if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart) { VirtualRoot.Execute(new SetAutoStartCommand(true, true)); } string content = $"{MinerProfile.NoShareRestartComputerMinutes.ToString()}分钟无份额重启电脑"; VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), content, toConsole: true); VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10)); return; // 退出 } // 重启内核,如果MineRestartedOn不是DateTime.MineValue则基于MineRestartedOn DateTime dt = GetKernelRestartBaseOnTime(); bool isRestartKernelMinutes = (DateTime.Now - dt).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes; if (restartKernel && isRestartKernelMinutes) { VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{MinerProfile.NoShareRestartKernelMinutes.ToString()}分钟无份额重启内核", toConsole: true); RestartMine(); return; // 退出 } } if (totalShare > shareCount) { shareCount = totalShare; shareOn = DateTime.Now; } }
protected override void OnStartup(StartupEventArgs e) { RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; // 通过群控升级挖矿端的时候升级器可能不存在所以需要下载,下载的时候需要用到下载器所以下载器需要提前注册 VirtualRoot.BuildCmdPath <ShowFileDownloaderCommand>(action: message => { UIThread.Execute(() => { FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete); }); }); VirtualRoot.BuildCmdPath <UpgradeCommand>(action: message => { AppStatic.Upgrade(message.FileName, message.Callback); }); if (!string.IsNullOrEmpty(CommandLineArgs.Upgrade)) { VirtualRoot.Execute(new UpgradeCommand(CommandLineArgs.Upgrade, () => { UIThread.Execute(() => { Environment.Exit(0); }); })); } else { try { appMutex = new Mutex(true, s_appPipName, out createdNew); } catch (Exception) { createdNew = false; } if (createdNew) { Logger.InfoDebugLine($"==================NTMiner.exe {MainAssemblyInfo.CurrentVersion.ToString()}=================="); NotiCenterWindowViewModel.IsHotKeyEnabled = true; SplashWindow splashWindow = null; // 在另一个UI线程运行欢迎界面以确保欢迎界面的响应不被耗时的主界面初始化过程阻塞 // 注意:必须确保SplashWindow没有用到任何其它界面用到的依赖对象 SplashWindow.ShowWindowAsync(window => { splashWindow = window; }); //ConsoleWindow.Instance.Show(); NotiCenterWindow.ShowWindow(); if (!NTMiner.Windows.WMI.IsWmiEnabled) { DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: "开源矿工无法运行所需的组件,因为本机未开启WMI服务,开源矿工需要使用WMI服务检测windows的内存、显卡等信息,请先手动开启WMI。", title: "提醒", icon: "Icon_Error")); Shutdown(); Environment.Exit(0); } if (!NTMiner.Windows.Role.IsAdministrator) { NotiCenterWindowViewModel.Instance.Manager .CreateMessage() .Warning("请以管理员身份运行。") .WithButton("点击以管理员身份运行", button => { WpfUtil.RunAsAdministrator(); }) .Dismiss().WithButton("忽略", button => { }).Queue(); } NTMinerRoot.Instance.Init(() => { _appViewFactory.Link(); if (VirtualRoot.IsLTWin10) { VirtualRoot.ThisLocalWarn(nameof(App), AppStatic.LowWinMessage, toConsole: true); } if (NTMinerRoot.Instance.GpuSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "没有矿卡或矿卡未驱动。", toConsole: true); } if (NTMinerRoot.Instance.ServerContext.CoinSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "访问阿里云失败,请尝试更换本机dns解决此问题。", toConsole: true); } UIThread.Execute(() => { if (NTMinerRoot.Instance.MinerProfile.IsNoUi && NTMinerRoot.Instance.MinerProfile.IsAutoStart) { ConsoleWindow.Instance.Hide(); VirtualRoot.Out.ShowSuccess("已切换为无界面模式运行,可在选项页调整设置", "开源矿工"); } else { // 预热视图模型 AppContext.Instance.VmsCtor(); _appViewFactory.ShowMainWindow(isToggle: false); } StartStopMineButtonViewModel.Instance.AutoStart(); AppContext.NotifyIcon = ExtendedNotifyIcon.Create("开源矿工", isMinerStudio: false); splashWindow?.Dispatcher.Invoke((Action) delegate() { splashWindow?.Close(); }); }); Task.Factory.StartNew(() => { if (NTMinerRoot.Instance.MinerProfile.IsAutoDisableWindowsFirewall) { Firewall.DisableFirewall(); } if (!Firewall.IsMinerClientRuleExists()) { Firewall.AddMinerClientRule(); } try { HttpServer.Start($"http://localhost:{NTKeyword.MinerClientPort.ToString()}"); Daemon.DaemonUtil.RunNTMinerDaemon(); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } NTMinerRoot.Instance.CpuPackage.Start(); }); }); Link(); } else { try { _appViewFactory.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerClient); } catch (Exception) { DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。", title: "提醒", icon: "Icon_Error")); Process currentProcess = Process.GetCurrentProcess(); NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess); } } } base.OnStartup(e); }
private void Link() { VirtualRoot.BuildEventPath <StartingMineFailedEvent>("开始挖矿失败", LogEnum.DevConsole, action: message => { AppContext.Instance.MinerProfileVm.IsMining = false; VirtualRoot.Out.ShowError(message.Message); }); #region 处理显示主界面命令 VirtualRoot.BuildCmdPath <ShowMainWindowCommand>(action: message => { ShowMainWindow(message.IsToggle); }); #endregion VirtualRoot.BuildCmdPath <CloseNTMinerCommand>(action: message => { // 不能推迟这个日志记录的时机,因为推迟会有windows异常日志 VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"退出{VirtualRoot.AppName}。原因:{message.Reason}"); UIThread.Execute(() => { try { Shutdown(); } catch (Exception e) { Logger.ErrorDebugLine(e); Environment.Exit(0); } }); }); #region 周期确保守护进程在运行 VirtualRoot.BuildEventPath <Per1MinuteEvent>("周期确保守护进程在运行", LogEnum.DevConsole, action: message => { Daemon.DaemonUtil.RunNTMinerDaemon(); }); #endregion #region 开始和停止挖矿后 VirtualRoot.BuildEventPath <MineStartedEvent>("启动1080ti小药丸、启动DevConsole? 更新挖矿按钮状态", LogEnum.DevConsole, action: message => { AppContext.Instance.MinerProfileVm.IsMining = true; StartStopMineButtonViewModel.Instance.BtnStopText = "正在挖矿"; // 启动DevConsole if (NTMinerRoot.IsUseDevConsole) { var mineContext = message.MineContext; string poolIp = mineContext.MainCoinPool.GetIp(); string consoleTitle = mineContext.MainCoinPool.Server; Daemon.DaemonUtil.RunDevConsoleAsync(poolIp, consoleTitle); } OhGodAnETHlargementPill.OhGodAnETHlargementPillUtil.Start(); }); VirtualRoot.BuildEventPath <MineStopedEvent>("停止挖矿后停止1080ti小药丸 挖矿停止后更新界面挖矿状态", LogEnum.DevConsole, action: message => { AppContext.Instance.MinerProfileVm.IsMining = false; StartStopMineButtonViewModel.Instance.BtnStopText = "尚未开始"; OhGodAnETHlargementPill.OhGodAnETHlargementPillUtil.Stop(); }); #endregion #region 处理禁用win10系统更新 VirtualRoot.BuildCmdPath <BlockWAUCommand>(action: message => { NTMiner.Windows.WindowsUtil.BlockWAU(); }); #endregion #region 优化windows VirtualRoot.BuildCmdPath <Win10OptimizeCommand>(action: message => { NTMiner.Windows.WindowsUtil.Win10Optimize(); }); #endregion #region 处理开启A卡计算模式 VirtualRoot.BuildCmdPath <SwitchRadeonGpuCommand>(action: message => { if (NTMinerRoot.Instance.GpuSet.GpuType == GpuType.AMD) { SwitchRadeonGpuMode(message.On); } }); #endregion #region 处理A卡驱动签名 VirtualRoot.BuildCmdPath <AtikmdagPatcherCommand>(action: message => { if (NTMinerRoot.Instance.GpuSet.GpuType == GpuType.AMD) { AtikmdagPatcher.AtikmdagPatcherUtil.Run(); } }); #endregion #region 启用或禁用windows远程桌面 VirtualRoot.BuildCmdPath <EnableWindowsRemoteDesktopCommand>(action: message => { if (NTMinerRegistry.GetIsRemoteDesktopEnabled()) { return; } string msg = "确定启用Windows远程桌面吗?"; DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: msg, title: "确认", onYes: () => { Rdp.SetRdpEnabled(true); Firewall.AddRdpRule(); })); }); #endregion #region 启用或禁用windows开机自动登录 VirtualRoot.BuildCmdPath <EnableOrDisableWindowsAutoLoginCommand>(action: message => { if (NTMiner.Windows.OS.Instance.IsAutoAdminLogon) { return; } NTMiner.Windows.Cmd.RunClose("control", "userpasswords2"); }); #endregion }
public void Init(Action callback) { Task.Factory.StartNew(() => { bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); if (isWork) // 是作业 { DoInit(isWork, callback); if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(true); } } else // 不是作业 { if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(false); } // 如果是Debug模式且不是群控客户端则使用本地数据库初始化 bool useLocalDb = DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio; if (useLocalDb) { DoInit(isWork: false, callback: callback); } else { Logger.InfoDebugLine(nameof(GetAliyunServerJson)); GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine($"{nameof(GetAliyunServerJson)}成功"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { SpecialPath.WriteServerJsonFile(serverJson); } OfficialServer.GetJsonFileVersionAsync(MainAssemblyInfo.ServerJsonFileName, serverState => { SetServerJsonVersion(serverState.JsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion); if (Math.Abs((long)Timestamp.GetTimestamp() - (long)serverState.Time) < Timestamp.DesyncSeconds) { Logger.OkDebugLine($"本机和服务器时间一致或相差不超过 {Timestamp.DesyncSeconds.ToString()} 秒"); } else { Write.UserWarn($"本机和服务器时间不同步,请调整,本地:{DateTime.Now.ToString()},服务器:{Timestamp.FromTimestamp(serverState.Time).ToString()}。此问题不影响挖矿。"); } }); } else { if (!File.Exists(SpecialPath.ServerJsonFileFullName)) { VirtualRoot.ThisLocalError(nameof(NTMinerRoot), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn); } else { VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), "配置文件下载失败,使用最近一次成功下载的配置文件", OutEnum.Warn); } } DoInit(isWork, callback); }); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.BuildEventPath <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { RefreshServerJsonFile(); }); #endregion } } VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"启动{VirtualRoot.AppName}"); }); }
private void Link() { VirtualRoot.BuildCmdPath <RegCmdHereCommand>(action: message => { try { Windows.Cmd.RegCmdHere(); VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), "windows右键命令行添加成功", OutEnum.Success); } catch (Exception e) { Logger.ErrorDebugLine(e); VirtualRoot.ThisLocalError(nameof(NTMinerRoot), "windows右键命令行添加失败", OutEnum.Warn); } }); VirtualRoot.BuildEventPath <Per1MinuteEvent>("每1分钟阻止系统休眠", LogEnum.None, action: message => { Windows.Power.PreventSleep(); }); #region 挖矿开始时将无份额内核重启份额计数置0 int shareCount = 0; DateTime shareOn = DateTime.Now; VirtualRoot.BuildEventPath <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole, action: message => { // 将无份额内核重启份额计数置0 shareCount = 0; if (!message.MineContext.IsRestart) { shareOn = DateTime.Now; } }); #endregion #region 每20秒钟检查是否需要重启 VirtualRoot.BuildEventPath <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None, action: message => { #region 重启电脑 try { if (MinerProfile.IsPeriodicRestartComputer) { if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes) { string content = $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartComputerMinutes.ToString()}分钟重启电脑"; VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), content, toConsole: true); Windows.Power.Restart(60); VirtualRoot.Execute(new CloseNTMinerCommand(content)); return; // 退出 } } } catch (Exception e) { Logger.ErrorDebugLine(e); } #endregion #region 周期重启内核 try { if (IsMining && MinerProfile.IsPeriodicRestartKernel) { if ((DateTime.Now - LockedMineContext.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartKernelHours + MinerProfile.PeriodicRestartKernelMinutes) { VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartKernelMinutes.ToString()}分钟重启内核", toConsole: true); RestartMine(); return; // 退出 } } } catch (Exception e) { Logger.ErrorDebugLine(e); } #endregion #region 无份额重启内核 try { if (IsMining && this.LockedMineContext.MainCoin != null) { int totalShare = 0; bool restartComputer = MinerProfile.IsNoShareRestartComputer && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes; bool restartKernel = MinerProfile.IsNoShareRestartKernel && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes; if (restartComputer || restartKernel) { ICoinShare mainCoinShare = this.CoinShareSet.GetOrCreate(this.LockedMineContext.MainCoin.GetId()); totalShare = mainCoinShare.TotalShareCount; if ((this.LockedMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null) { ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId()); totalShare += dualCoinShare.TotalShareCount; } // 如果份额没有增加 if (shareCount == totalShare) { if (restartComputer) { if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart) { VirtualRoot.Execute(new SetAutoStartCommand(true, true)); } string content = $"{MinerProfile.NoShareRestartComputerMinutes.ToString()}分钟无份额重启电脑"; VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), content, toConsole: true); Windows.Power.Restart(60); VirtualRoot.Execute(new CloseNTMinerCommand(content)); return; // 退出 } // 产生过份额或者已经两倍重启内核时间了 if (restartKernel && (totalShare > 0 || (DateTime.Now - shareOn).TotalMinutes > 2 * MinerProfile.NoShareRestartKernelMinutes)) { VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"{MinerProfile.NoShareRestartKernelMinutes.ToString()}分钟无份额重启内核", toConsole: true); RestartMine(); return; // 退出 } } if (totalShare > shareCount) { shareCount = totalShare; shareOn = DateTime.Now; } }
private void DoRun() { if (AppUtil.GetMutex(NTKeyword.MinerClientAppMutex)) { NotiCenterWindow.ShowWindow(); Logger.InfoDebugLine($"==================NTMiner.exe {EntryAssemblyInfo.CurrentVersionStr}=================="); // 在另一个UI线程运行欢迎界面以确保欢迎界面的响应不被耗时的主界面初始化过程阻塞 // 注意:必须确保SplashWindow没有用到任何其它界面用到的依赖对象 SplashWindow splashWindow = null; SplashWindow.ShowWindowAsync(window => { splashWindow = window; }); if (!NTMiner.Windows.WMI.IsWmiEnabled) { DialogWindow.ShowHardDialog(new DialogWindowViewModel( message: "开源矿工无法运行所需的组件,因为本机未开启WMI服务,开源矿工需要使用WMI服务检测windows的内存、显卡等信息,请先手动开启WMI。", title: "提醒", icon: "Icon_Error")); Shutdown(); Environment.Exit(0); } if (!NTMiner.Windows.Role.IsAdministrator) { NotiCenterWindowViewModel.Instance.Manager .CreateMessage() .Warning("提示", "请以管理员身份运行。") .WithButton("点击以管理员身份运行", button => { WpfUtil.RunAsAdministrator(); }) .Dismiss().WithButton("忽略", button => { }).Queue(); } BuildPaths(); NTMinerContext.Instance.Init(() => { _appViewFactory.BuildPaths(); if (VirtualRoot.IsLTWin10) { VirtualRoot.ThisLocalWarn(nameof(App), AppRoot.LowWinMessage, toConsole: true); } if (NTMinerContext.Instance.GpuSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "没有矿卡或矿卡未驱动。", toConsole: true); } if (NTMinerContext.WorkType != WorkType.None && NTMinerContext.Instance.ServerContext.CoinSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "访问阿里云失败,请尝试更换本机dns解决此问题。", toConsole: true); } UIThread.Execute(() => { Window mainWindow = null; AppRoot.NotifyIcon = ExtendedNotifyIcon.Create("开源矿工", isMinerStudio: false); if (NTMinerRegistry.GetIsNoUi() && NTMinerRegistry.GetIsAutoStart()) { ConsoleWindow.Instance.Hide(); VirtualRoot.Out.ShowSuccess("以无界面模式启动,可在选项页调整设置", header: "开源矿工"); } else { _appViewFactory.ShowMainWindow(isToggle: false, out mainWindow); } // 主窗口显式后退出SplashWindow splashWindow?.Dispatcher.Invoke((Action) delegate() { splashWindow?.OkClose(); }); // 启动时Windows状态栏显式的是SplashWindow的任务栏图标,SplashWindow关闭后激活主窗口的Windows任务栏图标 mainWindow?.Activate(); StartStopMineButtonViewModel.Instance.AutoStart(); // 注意:因为推迟到这里才启动的计时器,所以别忘了在Upgrade、和Action情况时启动计时器 VirtualRoot.StartTimer(new WpfTimingEventProducer()); if (CommandLineArgs.Action.TryParse(out MinerClientActionType resourceType)) { VirtualRoot.Execute(new MinerClientActionCommand(resourceType)); } }); Task.Factory.StartNew(() => { var minerProfile = NTMinerContext.Instance.MinerProfile; if (minerProfile.IsDisableUAC) { NTMiner.Windows.UAC.DisableUAC(); } if (minerProfile.IsAutoDisableWindowsFirewall) { Firewall.DisableFirewall(); } if (minerProfile.IsDisableWAU) { NTMiner.Windows.WAU.DisableWAUAsync(); } if (minerProfile.IsDisableAntiSpyware) { NTMiner.Windows.Defender.DisableAntiSpyware(); } if (!Firewall.IsMinerClientRuleExists()) { Firewall.AddMinerClientRule(); } try { HttpServer.Start($"http://{NTKeyword.Localhost}:{NTKeyword.MinerClientPort.ToString()}"); Daemon.DaemonUtil.RunNTMinerDaemon(); NoDevFee.NoDevFeeUtil.RunNTMinerNoDevFee(); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } }); }); } else { try { _appViewFactory.ShowMainWindow(this, NTMinerAppType.MinerClient); } catch (Exception) { DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: "另一个开源矿工正在运行但唤醒失败,请重试。", title: "错误", icon: "Icon_Error")); Process currentProcess = Process.GetCurrentProcess(); NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess); } } }
protected override void OnStartup(StartupEventArgs e) { RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; // 之所以提前到这里是因为升级之前可能需要下载升级器,下载升级器时需要下载器 VirtualRoot.AddCmdPath <ShowFileDownloaderCommand>(action: message => { FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete); }, location: this.GetType()); VirtualRoot.AddCmdPath <UpgradeCommand>(action: message => { AppStatic.Upgrade(message.FileName, message.Callback); }, location: this.GetType()); if (!string.IsNullOrEmpty(CommandLineArgs.Upgrade)) { VirtualRoot.Execute(new UpgradeCommand(CommandLineArgs.Upgrade, () => { UIThread.Execute(() => () => { Environment.Exit(0); }); })); } else { if (AppUtil.GetMutex(NTKeyword.MinerClientAppMutex)) { Logger.InfoDebugLine($"==================NTMiner.exe {EntryAssemblyInfo.CurrentVersion.ToString()}=================="); NotiCenterWindowViewModel.IsHotKeyEnabled = true; // 在另一个UI线程运行欢迎界面以确保欢迎界面的响应不被耗时的主界面初始化过程阻塞 // 注意:必须确保SplashWindow没有用到任何其它界面用到的依赖对象 SplashWindow splashWindow = null; SplashWindow.ShowWindowAsync(window => { splashWindow = window; }); NotiCenterWindow.Instance.ShowWindow(); if (!NTMiner.Windows.WMI.IsWmiEnabled) { DialogWindow.ShowHardDialog(new DialogWindowViewModel( message: "开源矿工无法运行所需的组件,因为本机未开启WMI服务,开源矿工需要使用WMI服务检测windows的内存、显卡等信息,请先手动开启WMI。", title: "提醒", icon: "Icon_Error")); Shutdown(); Environment.Exit(0); } if (!NTMiner.Windows.Role.IsAdministrator) { NotiCenterWindowViewModel.Instance.Manager .CreateMessage() .Warning("提示", "请以管理员身份运行。") .WithButton("点击以管理员身份运行", button => { WpfUtil.RunAsAdministrator(); }) .Dismiss().WithButton("忽略", button => { }).Queue(); } NTMinerRoot.Instance.Init(() => { _appViewFactory.Link(); if (VirtualRoot.IsLTWin10) { VirtualRoot.ThisLocalWarn(nameof(App), AppStatic.LowWinMessage, toConsole: true); } if (NTMinerRoot.Instance.GpuSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "没有矿卡或矿卡未驱动。", toConsole: true); } if (NTMinerRoot.Instance.ServerContext.CoinSet.Count == 0) { VirtualRoot.ThisLocalError(nameof(App), "访问阿里云失败,请尝试更换本机dns解决此问题。", toConsole: true); } UIThread.Execute(() => () => { Window mainWindow = null; AppContext.NotifyIcon = ExtendedNotifyIcon.Create("开源矿工", isMinerStudio: false); if (NTMinerRoot.Instance.MinerProfile.IsNoUi && NTMinerRoot.Instance.MinerProfile.IsAutoStart) { ConsoleWindow.Instance.Hide(); VirtualRoot.Out.ShowSuccess("以无界面模式启动,可在选项页调整设置", header: "开源矿工"); } else { _appViewFactory.ShowMainWindow(isToggle: false, out mainWindow); } // 主窗口显式后退出SplashWindow splashWindow?.Dispatcher.Invoke((Action) delegate() { splashWindow?.OkClose(); }); // 启动时Windows状态栏显式的是SplashWindow的任务栏图标,SplashWindow关闭后激活主窗口的Windows任务栏图标 mainWindow?.Activate(); StartStopMineButtonViewModel.Instance.AutoStart(); VirtualRoot.StartTimer(new WpfTimer()); }); Task.Factory.StartNew(() => { if (NTMinerRoot.Instance.MinerProfile.IsAutoDisableWindowsFirewall) { Firewall.DisableFirewall(); } if (!Firewall.IsMinerClientRuleExists()) { Firewall.AddMinerClientRule(); } try { HttpServer.Start($"http://localhost:{NTKeyword.MinerClientPort.ToString()}"); Daemon.DaemonUtil.RunNTMinerDaemon(); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } }); }); Link(); } else { try { _appViewFactory.ShowMainWindow(this, NTMinerAppType.MinerClient); } catch (Exception) { DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: "另一个开源矿工正在运行但唤醒失败,请重试。", title: "错误", icon: "Icon_Error")); Process currentProcess = Process.GetCurrentProcess(); NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess); } } } base.OnStartup(e); }
protected override void OnStartup(StartupEventArgs e) { RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; VirtualRoot.BuildCmdPath <ShowFileDownloaderCommand>(action: message => { UIThread.Execute(() => { FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete); }); }); VirtualRoot.BuildCmdPath <UpgradeCommand>(action: message => { AppStatic.Upgrade(message.FileName, message.Callback); }); try { appMutex = new Mutex(true, s_appPipName, out createdNew); } catch (Exception) { createdNew = false; } if (createdNew) { this.ShutdownMode = ShutdownMode.OnExplicitShutdown; NotiCenterWindow.ShowWindow(); LoginWindow.Login(() => { bool isInnerIp = Net.Util.IsInnerIp(NTMinerRegistry.GetControlCenterHost()); if (isInnerIp) { NTMinerServices.NTMinerServicesUtil.RunNTMinerServices(() => { Init(); }); } else { Init(); } }); VirtualRoot.BuildCmdPath <CloseNTMinerCommand>(action: message => { // 不能推迟这个日志记录的时机,因为推迟会有windows异常日志 VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"退出{VirtualRoot.AppName}。原因:{message.Reason}"); UIThread.Execute(() => { try { Shutdown(); } catch (Exception ex) { Logger.ErrorDebugLine(ex); Environment.Exit(0); } }); }); } else { try { _appViewFactory.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerStudio); } catch (Exception) { DialogWindow.ShowSoftDialog(new DialogWindowViewModel( message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。", title: "alert", icon: "Icon_Error")); Process currentProcess = Process.GetCurrentProcess(); NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess); } } base.OnStartup(e); }