public void ReportStateAsync(string host, Guid clientId, bool isMining) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = new HttpClient()) { client.Timeout = timeSpan; ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://{host}:{WebApiConst.ControlCenterPort}/api/{SControllerName}/{nameof(IReportController.ReportState)}", request); Write.DevDebug($"{nameof(ReportStateAsync)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { if (e is TaskCanceledException) { Write.DevError($"本次ReportStateAsync已取消,因为耗时超过{timeSpan.TotalSeconds}秒"); } else { e = e.GetInnerException(); Logger.ErrorDebugLine(e.Message, e); } } }); }
public void ReportSpeedAsync(string host, SpeedData data) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = new HttpClient()) { // 可能超过3秒钟,查查原因。因为我的网络不稳经常断线。 client.Timeout = timeSpan; Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://{host}:{WebApiConst.ControlCenterPort}/api/{SControllerName}/{nameof(IReportController.ReportSpeed)}", data); Write.DevDebug($"{nameof(ReportSpeedAsync)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { if (e is TaskCanceledException) { Write.DevError($"本次ReportSpeedAsync已取消,因为耗时超过{timeSpan.TotalSeconds}秒"); } else { e = e.GetInnerException(); Logger.ErrorDebugLine(e.Message, e); } } }); }
public void CloseDaemon() { try { using (HttpClient client = new HttpClient()) { Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{Consts.NTMinerDaemonPort}/api/{s_controllerName}/{nameof(INTMinerDaemonController.CloseDaemon)}", null); Write.DevDebug($"{nameof(CloseDaemon)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void CloseDaemon() { try { using (HttpClient client = RpcRoot.Create()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{s_controllerName}/{nameof(INTMinerDaemonController.CloseDaemon)}", null); Write.DevDebug($"{nameof(CloseDaemon)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Write.DevException(e); } }
public void SetAutoBootStart([FromUri] bool autoBoot, [FromUri] bool autoStart) { Logger.InfoDebugLine($"开机启动{(autoBoot ? "√" : "×")},自动挖矿{(autoStart ? "√" : "×")}"); MinerProfileUtil.SetAutoStart(autoBoot, autoStart); if (IsNTMinerOpened()) { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/RefreshAutoBootStart", null); Write.DevDebug($"{nameof(SetAutoBootStart)} {getHttpResponse.Result.ReasonPhrase}"); } } }
public void SetAutoBootStart([FromUri] bool autoBoot, [FromUri] bool autoStart) { NTMinerRegistry.SetIsAutoBoot(autoBoot); NTMinerRegistry.SetIsAutoStart(autoStart); if (IsNTMinerOpened()) { using (HttpClient client = new HttpClient()) { Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{Consts.MinerClientPort}/api/MinerClient/RefreshAutoBootStart", null); Write.DevDebug($"{nameof(SetAutoBootStart)} {message.Result.ReasonPhrase}"); } } }
public void CloseDaemon() { try { using (HttpClient client = new HttpClient()) { Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{VirtualRoot.NTMinerDaemonPort}/api/{s_controllerName}/{nameof(INTMinerDaemonController.CloseDaemon)}", null); Write.DevDebug($"{nameof(CloseDaemon)} {message.Result.ReasonPhrase}"); } } catch { // 吞掉异常,以免用户恐慌 } }
public void SetAutoBootStartAsync(string clientIp, bool autoBoot, bool autoStart) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{s_controllerName}/{nameof(INTMinerDaemonController.SetAutoBootStart)}?autoBoot={autoBoot}&autoStart={autoStart}", null); Write.DevDebug($"{nameof(SetAutoBootStartAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void RefreshAutoBootStartAsync() { Task.Factory.StartNew(() => { try { using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(3000); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort}/api/{s_controllerName}/{nameof(IMinerClientController.RefreshAutoBootStart)}", null); Write.DevDebug($"{nameof(RefreshAutoBootStartAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void SetAutoBootStartAsync(string clientHost, bool autoBoot, bool autoStart) { Task.Factory.StartNew(() => { try { using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(3000); Task <HttpResponseMessage> message = client.PostAsync($"http://{clientHost}:{Consts.NTMinerDaemonPort}/api/{s_controllerName}/{nameof(INTMinerDaemonController.SetAutoBootStart)}?autoBoot={autoBoot}&autoStart={autoStart}", null); Write.DevDebug($"{nameof(SetAutoBootStartAsync)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void SaveGpuProfilesJsonAsync(string clientHost, string json) { Task.Factory.StartNew(() => { try { using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(3000); HttpContent content = new StringContent(json); Task <HttpResponseMessage> message = client.PostAsync($"http://{clientHost}:{Consts.NTMinerDaemonPort}/api/{s_controllerName}/{nameof(INTMinerDaemonController.SaveGpuProfilesJson)}", content); Write.DevDebug($"{nameof(SaveGpuProfilesJsonAsync)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void OverClockAsync() { Task.Factory.StartNew(() => { try { using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(3000); Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{WebApiConst.MinerClientPort}/api/{s_controllerName}/{nameof(IMinerClientController.OverClock)}", null); Write.DevDebug($"{nameof(OverClockAsync)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { e = e.GetInnerException(); Logger.ErrorDebugLine(e.Message, e); } }); }
public void SaveGpuProfilesJsonAsync(string clientIp, string json) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = TimeSpan.FromSeconds(3); HttpContent content = new StringContent(json); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{s_controllerName}/{nameof(INTMinerDaemonController.SaveGpuProfilesJson)}", content); Write.DevDebug($"{nameof(SaveGpuProfilesJsonAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void ReportSpeedAsync(string host, SpeedData data) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = new HttpClient()) { // 可能超过3秒钟,查查原因。因为我的网络不稳经常断线。 client.Timeout = timeSpan; Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://{host}:{Consts.ControlCenterPort}/api/{SControllerName}/{nameof(IReportController.ReportSpeed)}", data); Write.DevDebug($"{nameof(ReportSpeedAsync)} {message.Result.ReasonPhrase}"); } } catch { // 吞掉异常,以免用户恐慌 } }); }
public void CloseServices() { try { Process[] processes = Process.GetProcessesByName("NTMinerServices"); if (processes.Length == 0) { return; } using (HttpClient client = new HttpClient()) { Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{Consts.ControlCenterPort}/api/{SControllerName}/{nameof(IControlCenterController.CloseServices)}", null); Write.DevDebug($"{nameof(CloseServices)} {message.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void SaveGpuProfilesJson() { try { string json = Request.Content.ReadAsStringAsync().Result; SpecialPath.SaveGpuProfilesJsonFile(json); if (IsNTMinerOpened()) { using (HttpClient client = new HttpClient()) { Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{Consts.MinerClientPort}/api/MinerClient/OverClock", null); Write.DevDebug($"{nameof(SaveGpuProfilesJson)} {message.Result.ReasonPhrase}"); } } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void SaveGpuProfilesJson() { try { Logger.InfoDebugLine("保存显卡参数"); string json = Request.Content.ReadAsStringAsync().Result; SpecialPath.SaveGpuProfilesJsonFile(json); if (IsNTMinerOpened()) { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/OverClock", null); Write.DevDebug($"{nameof(SaveGpuProfilesJson)} {getHttpResponse.Result.ReasonPhrase}"); } } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void ReportStateAsync(string host, Guid clientId, bool isMining) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = new HttpClient()) { client.Timeout = timeSpan; ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://{host}:{Consts.ControlCenterPort}/api/{SControllerName}/{nameof(IReportController.ReportState)}", request); Write.DevDebug($"{nameof(ReportStateAsync)} {message.Result.ReasonPhrase}"); } } catch { // 吞掉异常,以免用户恐慌 } }); }
public void ReportStateAsync(string host, Guid clientId, bool isMining) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = timeSpan; ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{host}:{NTKeyword.ControlCenterPort.ToString()}/api/{SControllerName}/{nameof(IReportController.ReportState)}", request); Write.DevDebug($"{nameof(ReportStateAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Write.DevException(e); } }); }
public static bool IsValid <TResponse>(this IGetSignData data, IUser user, string sign, ulong timestamp, string clientIp, out TResponse response) where TResponse : ResponseBase, new() { if (clientIp == "localhost" || clientIp == "127.0.0.1") { response = null; return(true); } if (_isInnerIpEnabled && Net.Util.IsInnerIp(clientIp)) { response = null; return(true); } if (user == null) { string message = "用户不存在"; response = ResponseBase.NotExist <TResponse>(message); return(false); } else if (user.LoginName == "admin" && string.IsNullOrEmpty(user.Password)) { string message = "第一次使用,请先设置密码"; response = ResponseBase.NotExist <TResponse>(message); return(false); } if (!timestamp.IsInTime()) { response = ResponseBase.Expired <TResponse>(); return(false); } string mySign = GetSign(data, user.LoginName, user.Password, timestamp); if (sign != mySign) { string message = "用户名或密码错误"; response = ResponseBase.Forbidden <TResponse>(message); Write.DevDebug($"{message} sign:{sign} mySign:{mySign}"); return(false); } response = null; return(true); }
private void RefreshServerJsonFile() { RpcRoot.OfficialServer.GetJsonFileVersionAsync(EntryAssemblyInfo.ServerJsonFileName, serverState => { AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion); string localServerJsonFileVersion = GetServerJsonVersion(); if (!string.IsNullOrEmpty(serverState.JsonFileVersion) && localServerJsonFileVersion != serverState.JsonFileVersion) { GetAliyunServerJson((data) => { string rawJson = Encoding.UTF8.GetString(data); SpecialPath.WriteServerJsonFile(rawJson); SetServerJsonVersion(serverState.JsonFileVersion); // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false ContextReInit(isWork: VirtualRoot.IsMinerStudio); VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"刷新server.json配置", toConsole: true); }); } else { Write.DevDebug("server.json没有新版本"); } }); }
protected override void OnMessage(MessageEventArgs e) { if (string.IsNullOrEmpty(e.Data) || e.Data[0] != '{' || e.Data[e.Data.Length - 1] != '}') { return; } WsMessage message = VirtualRoot.JsonSerializer.Deserialize <WsMessage>(e.Data); if (message == null) { return; } switch (message.GetType()) { case WsMessageType.Speed: Write.DevDebug(e.Data); break; default: base.Send(new WsMessage().SetType(message.GetType()).SetCode(400).SetDes("invalid action").SetPhrase("invalid action").ToJson()); break; } Write.DevWarn("Sessions Count: " + base.Sessions.Count); }
private void RefreshServerJsonFile() { OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); string localServerJsonFileVersion = GetServerJsonVersion(); if (!string.IsNullOrEmpty(serverJsonFileVersion) && localServerJsonFileVersion != serverJsonFileVersion) { GetAliyunServerJson((data) => { Write.UserInfo($"更新配置{localServerJsonFileVersion}->{serverJsonFileVersion}"); string rawJson = Encoding.UTF8.GetString(data); SpecialPath.WriteServerJsonFile(rawJson); SetServerJsonVersion(serverJsonFileVersion); ReInitServerJson(); // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false ContextReInit(isWork: VirtualRoot.IsMinerStudio); Write.UserInfo("更新成功"); }); } else { Write.DevDebug("server.json没有新版本"); } }); }
static void Main(string[] args) { if (args.Length != 0) { if (args.Contains("--sha1")) { File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1); return; } } try { SystemEvents.SessionEnding += (sender, e) => { OsSessionEndingEvent.ReasonSessionEnding reason; switch (e.Reason) { case SessionEndReasons.Logoff: reason = OsSessionEndingEvent.ReasonSessionEnding.Logoff; break; case SessionEndReasons.SystemShutdown: reason = OsSessionEndingEvent.ReasonSessionEnding.Shutdown; break; default: reason = OsSessionEndingEvent.ReasonSessionEnding.Unknown; break; } VirtualRoot.RaiseEvent(new OsSessionEndingEvent(reason)); }; VirtualRoot.StartTimer(); _waitHandle = new AutoResetEvent(false); bool mutexCreated; try { _sMutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated); } catch { mutexCreated = false; } if (mutexCreated) { NTMinerRegistry.SetDaemonVersion(Sha1); NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true); bool isAutoBoot = MinerProfileUtil.GetIsAutoBoot(); if (isAutoBoot) { string location = NTMinerRegistry.GetLocation(); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string processName = Path.GetFileName(location); Process[] processes = Process.GetProcessesByName(processName); if (processes.Length == 0) { string arguments = NTMinerRegistry.GetArguments(); if (NTMinerRegistry.GetIsLastIsWork()) { arguments = "--work " + arguments; } try { Process.Start(location, arguments); Write.DevOk($"启动挖矿端 {location} {arguments}"); } catch (Exception e) { Logger.ErrorDebugLine($"启动挖矿端失败因为异常 {location} {arguments}", e); } } else { Write.DevDebug($"挖矿端已经在运行中无需启动"); } } } else { Write.DevDebug($"挖矿端未设置为自动启动"); } Run(); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
static void Main(string[] args) { if (args.Length != 0) { if (args.Contains("--sha1")) { File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1); return; } } try { if (DevMode.IsDevMode && !Debugger.IsAttached) { Write.Init(); } _waitHandle = new AutoResetEvent(false); bool mutexCreated; try { _sMutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated); } catch { mutexCreated = false; } if (mutexCreated) { StartTimer(); Registry.SetDaemonVersion(Sha1); Registry.SetAutoBoot("NTMinerDaemon", true); bool isAutoBoot = Registry.GetIsAutoBoot(); if (isAutoBoot) { string location = Registry.GetLocation(); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string processName = Path.GetFileName(location); Process[] processes = Process.GetProcessesByName(processName); if (processes.Length == 0) { string arguments = Registry.GetArguments(); try { Process.Start(location, arguments); Write.DevOk($"启动挖矿端 {location} {arguments}"); } catch (Exception e) { Logger.ErrorDebugLine($"启动挖矿端失败因为异常 {location} {arguments}", e); } } else { Write.DevDebug($"挖矿端已经在运行中无需启动"); } } } else { Write.DevDebug($"挖矿端未设置为自动启动"); } Run(); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void Init(Action callback) { Task.Factory.StartNew(() => { bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); if (isWork) { DoInit(isWork, callback); if (IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(true); } return; } else { if (IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(false); } } // 如果是Debug模式且不是群控客户端且不是作业则使用本地数据库初始化 if (DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio) { DoInit(isWork: false, callback: callback); return; } Logger.InfoDebugLine("开始下载server.json"); SpecialPath.GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine("GetAliyunServerJson下载成功"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { SpecialPath.WriteServerJsonFile(serverJson); } OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { SetServerJsonVersion(serverJsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); }); } else { Logger.InfoDebugLine("GetAliyunServerJson下载失败"); } DoInit(isWork, callback); }); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.On <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); string localServerJsonFileVersion = GetServerJsonVersion(); if (!string.IsNullOrEmpty(serverJsonFileVersion) && localServerJsonFileVersion != serverJsonFileVersion) { SpecialPath.GetAliyunServerJson((data) => { Write.UserInfo($"server.json配置文件有新版本{localServerJsonFileVersion}->{serverJsonFileVersion}"); string rawJson = Encoding.UTF8.GetString(data); SpecialPath.WriteServerJsonFile(rawJson); SetServerJsonVersion(serverJsonFileVersion); ReInitServerJson(); bool isUseJson = !DevMode.IsDebugMode || VirtualRoot.IsMinerStudio; if (isUseJson) { // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false ContextReInit(isWork: VirtualRoot.IsMinerStudio); Write.UserInfo("刷新完成"); } else { Write.UserInfo("不是使用的server.json,无需刷新"); } }); } else { Write.DevDebug("server.json没有新版本"); } }); }); #endregion }); // 因为这个操作大概需要200毫秒 Task.Factory.StartNew(() => { NVIDIAGpuSet.NvmlInit(); }); }
static void Main(string[] args) { HomePath.SetHomeDirFullName(AppDomain.CurrentDomain.BaseDirectory); SetOut(new ConsoleOut()); if (args.Length != 0) { if (args.Contains("--sha1")) { File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1); return; } } try { SystemEvents.SessionEnding += SessionEndingEventHandler; StartTimer(); _waitHandle = new AutoResetEvent(false); bool mutexCreated; try { _sMutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated); } catch { mutexCreated = false; } if (mutexCreated) { if (!DevMode.IsDevMode) { Write.Disable(); } NTMinerRegistry.SetDaemonVersion(Sha1); NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true); #region 是否自动启动挖矿端 bool isAutoBoot = MinerProfileUtil.GetIsAutoBoot(); if (isAutoBoot) { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string processName = Path.GetFileName(location); Process[] processes = Process.GetProcessesByName(processName); if (processes.Length == 0) { string arguments = NTMinerRegistry.GetArguments(NTMinerAppType.MinerClient); if (NTMinerRegistry.GetIsLastIsWork()) { arguments = "--work " + arguments; } try { Process.Start(location, arguments); Write.DevOk(() => $"启动挖矿端 {location} {arguments}"); } catch (Exception e) { Logger.ErrorDebugLine($"启动挖矿端失败因为异常 {location} {arguments}", e); } } else { Write.DevDebug($"挖矿端已经在运行中无需启动"); } } } #endregion Run(); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }