Esempio n. 1
0
        private void CloseNTMiner()
        {
            bool isClosed = false;

            try {
                using (HttpClient client = new HttpClient()) {
                    Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://localhost:{Consts.MinerClientPort}/api/MinerClient/CloseNTMiner", new SignatureRequest {
                    });
                    ResponseBase response = message.Result.Content.ReadAsAsync <ResponseBase>().Result;
                    isClosed = response.IsSuccess();
                }
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
            if (!isClosed)
            {
                try {
                    string location = NTMinerRegistry.GetLocation();
                    if (!string.IsNullOrEmpty(location) && File.Exists(location))
                    {
                        string processName = Path.GetFileNameWithoutExtension(location);
                        Windows.TaskKill.Kill(processName);
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
            }
        }
Esempio n. 2
0
        static SpecialPath()
        {
            string location = NTMinerRegistry.GetLocation();

            if (!string.IsNullOrEmpty(location) && File.Exists(location))
            {
                try {
                    if (File.Exists(Path.Combine(Path.GetDirectoryName(location), "home.lock")))
                    {
                        _homeDirFullName = Path.GetDirectoryName(location);
                    }
                    else
                    {
                        _homeDirFullName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NTMiner");
                    }
                }
                catch {
                    _homeDirFullName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NTMiner");
                }
            }
            else
            {
                _homeDirFullName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NTMiner");
            }
            NTMinerLocalJsonFileFullName  = Path.Combine(_homeDirFullName, NTKeyword.LocalJsonFileName);
            NTMinerServerJsonFileFullName = Path.Combine(_homeDirFullName, NTKeyword.ServerJsonFileName);
            GpuProfilesJsonFileFullName   = Path.Combine(_homeDirFullName, NTKeyword.GpuProfilesFileName);
        }
Esempio n. 3
0
 public ResponseBase RestartNTMiner([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     if (request.WorkId != Guid.Empty)
     {
         File.WriteAllText(SpecialPath.NTMinerLocalJsonFileFullName, request.LocalJson);
         File.WriteAllText(SpecialPath.NTMinerServerJsonFileFullName, request.ServerJson);
     }
     Task.Factory.StartNew(() => {
         try {
             if (IsNTMinerOpened())
             {
                 CloseNTMiner();
                 System.Threading.Thread.Sleep(1000);
             }
             string arguments = string.Empty;
             if (request.WorkId != Guid.Empty)
             {
                 arguments = "--work";
             }
             string location = NTMinerRegistry.GetLocation();
             if (!string.IsNullOrEmpty(location) && File.Exists(location))
             {
                 Windows.Cmd.RunClose(location, arguments);
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
     });
     return(ResponseBase.Ok());
 }
Esempio n. 4
0
 static void Main(string[] args)
 {
     try {
         bool mutexCreated;
         try {
             _sMutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated);
         }
         catch {
             mutexCreated = false;
         }
         if (mutexCreated)
         {
             NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true);
             bool isAutoBoot = NTMinerRegistry.GetIsAutoBoot();
             if (isAutoBoot)
             {
                 string location = NTMinerRegistry.GetLocation();
                 if (!string.IsNullOrEmpty(location))
                 {
                     string arguments = NTMinerRegistry.GetArguments();
                     try {
                         Process.Start(location, arguments);
                     }
                     catch (Exception e) {
                         Logger.ErrorDebugLine(e.Message, e);
                     }
                 }
             }
             Run();
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e.Message, e);
     }
 }
Esempio n. 5
0
 static void Main(string[] args) {
     SetOut(new ConsoleOut());
     NTMinerConsole.MainUiOk();
     if (args.Length != 0) {
         if (args.Contains("--sha1", StringComparer.OrdinalIgnoreCase)) {
             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) {
                 NTMinerConsole.Disable();
             }
             NTMinerRegistry.SetDaemonVersion(Sha1);
             NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true);
             if (!CommandLineArgs.Args.Contains("--bootByMinerClient")) {
                 #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.GetMinerClientArguments(NTMinerAppType.MinerClient);
                             try {
                                 Process.Start(location, arguments);
                                 NTMinerConsole.DevOk(() => $"启动挖矿端 {location} {arguments}");
                             }
                             catch (Exception e) {
                                 Logger.ErrorDebugLine($"启动挖矿端失败因为异常 {location} {arguments}", e);
                             }
                         }
                         else {
                             NTMinerConsole.DevDebug($"挖矿端已经在运行中无需启动");
                         }
                     }
                 }
                 #endregion
             }
             Run();
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
     }
 }
Esempio n. 6
0
        private static bool IsNTMinerOpened()
        {
            string location = NTMinerRegistry.GetLocation();

            if (!string.IsNullOrEmpty(location) && File.Exists(location))
            {
                string    processName = Path.GetFileNameWithoutExtension(location);
                Process[] processes   = Process.GetProcessesByName(processName);
                return(processes.Length != 0);
            }
            return(false);
        }
Esempio n. 7
0
 public ResponseBase StartMine([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         Logger.InfoDebugLine("开始挖矿");
         ResponseBase response;
         if (request.WorkId != Guid.Empty)
         {
             File.WriteAllText(SpecialPath.NTMinerLocalJsonFileFullName, request.LocalJson);
             File.WriteAllText(SpecialPath.NTMinerServerJsonFileFullName, request.ServerJson);
         }
         string location = NTMinerRegistry.GetLocation();
         if (IsNTMinerOpened())
         {
             using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 WorkRequest innerRequest = new WorkRequest {
                     WorkId = request.WorkId
                 };
                 Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/StartMine", innerRequest);
                 response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result;
                 return(response);
             }
         }
         else
         {
             if (!string.IsNullOrEmpty(location) && File.Exists(location))
             {
                 string arguments = NTKeyword.AutoStartCmdParameterName;
                 if (request.WorkId != Guid.Empty)
                 {
                     arguments += " --work";
                 }
                 Windows.Cmd.RunClose(location, arguments);
                 return(ResponseBase.Ok());
             }
             return(ResponseBase.ServerError("挖矿端程序不存在"));
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Esempio n. 8
0
        static SpecialPath()
        {
            string location   = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient);
            string globalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NTMiner");
            string homeDirFullName;

            if (!string.IsNullOrEmpty(location) && File.Exists(location))
            {
                try {
                    if (File.Exists(Path.Combine(Path.GetDirectoryName(location), "home.lock")))
                    {
                        homeDirFullName = Path.GetDirectoryName(location);
                    }
                    else
                    {
                        homeDirFullName = globalPath;
                    }
                }
                catch {
                    homeDirFullName = globalPath;
                }
            }
            else
            {
                homeDirFullName = globalPath;
            }
            string selfWorkDirFullName = Path.Combine(homeDirFullName, "SelfWork");

            if (!Directory.Exists(selfWorkDirFullName))
            {
                Directory.CreateDirectory(selfWorkDirFullName);
            }
            string mineWorkDirFullName = Path.Combine(homeDirFullName, "MineWork");

            if (!Directory.Exists(mineWorkDirFullName))
            {
                Directory.CreateDirectory(mineWorkDirFullName);
            }
            _mineWorkServerJsonFileFullName = Path.Combine(mineWorkDirFullName, NTKeyword.ServerJsonFileName);
            _mineWorkLocalJsonFileFullName  = Path.Combine(mineWorkDirFullName, NTKeyword.LocalJsonFileName);
            _selfWorkServerJsonFileFullName = Path.Combine(selfWorkDirFullName, NTKeyword.ServerJsonFileName);
            _selfWorkLocalJsonFileFullName  = Path.Combine(selfWorkDirFullName, NTKeyword.LocalJsonFileName);
            _gpuProfilesJsonFileFullName    = Path.Combine(homeDirFullName, NTKeyword.GpuProfilesFileName);
        }
Esempio n. 9
0
 public ResponseBase UpgradeNTMiner([FromBody] UpgradeNTMinerRequest request)
 {
     if (request == null || string.IsNullOrEmpty(request.NTMinerFileName))
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     Task.Factory.StartNew(() => {
         try {
             string location = NTMinerRegistry.GetLocation();
             if (!string.IsNullOrEmpty(location) && File.Exists(location))
             {
                 string arguments = "upgrade=" + request.NTMinerFileName;
                 Windows.Cmd.RunClose(location, arguments);
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
     });
     return(ResponseBase.Ok());
 }
            // ReSharper disable once InconsistentNaming
            public void CloseNTMiner()
            {
                string location = NTMinerRegistry.GetLocation();

                if (string.IsNullOrEmpty(location) || !File.Exists(location))
                {
                    return;
                }
                string processName = Path.GetFileNameWithoutExtension(location);

                if (Process.GetProcessesByName(processName).Length == 0)
                {
                    return;
                }
                bool isClosed = false;

                try {
                    using (HttpClient client = new HttpClient()) {
                        client.Timeout = TimeSpan.FromMilliseconds(2000);
                        Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://localhost:{WebApiConst.MinerClientPort}/api/MinerClient/CloseNTMiner", new SignatureRequest {
                        });
                        ResponseBase response = message.Result.Content.ReadAsAsync <ResponseBase>().Result;
                        isClosed = response.IsSuccess();
                    }
                }
                catch (Exception e) {
                    e = e.GetInnerException();
                    Logger.ErrorDebugLine(e.Message, e);
                }
                if (!isClosed)
                {
                    try {
                        Windows.TaskKill.Kill(processName);
                    }
                    catch (Exception e) {
                        Logger.ErrorDebugLine(e.Message, e);
                    }
                }
            }
Esempio n. 11
0
 static void Main(string[] args)
 {
     try {
         Console.Title = "NTMinerDaemon";
         bool mutexCreated;
         try {
             s_mutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated);
         }
         catch {
             mutexCreated = false;
         }
         if (mutexCreated)
         {
             NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true);
             Type thisType = typeof(HostRoot);
             NotifyIcon = ExtendedNotifyIcon.Create(new System.Drawing.Icon(thisType.Assembly.GetManifestResourceStream(thisType, "logo.ico")), "挖矿端守护进程", isShowNotifyIcon: DevMode.IsDebugMode);
             bool isAutoBoot = NTMinerRegistry.GetIsAutoBoot();
             if (isAutoBoot)
             {
                 string location = NTMinerRegistry.GetLocation();
                 if (!string.IsNullOrEmpty(location))
                 {
                     string arguments = NTMinerRegistry.GetArguments();
                     try {
                         Process.Start(location, arguments);
                     }
                     catch (Exception e) {
                         Logger.ErrorDebugLine(e.Message, e);
                     }
                 }
             }
             Run();
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e.Message, e);
     }
 }
            // ReSharper disable once InconsistentNaming
            public void CloseNTMiner()
            {
                string location = NTMinerRegistry.GetLocation();

                if (string.IsNullOrEmpty(location) || !File.Exists(location))
                {
                    return;
                }
                string processName = Path.GetFileNameWithoutExtension(location);

                if (Process.GetProcessesByName(processName).Length == 0)
                {
                    return;
                }
                bool isClosed = false;

                try {
                    using (HttpClient client = RpcRoot.Create()) {
                        client.Timeout = TimeSpan.FromSeconds(2);
                        Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/{s_controllerName}/{nameof(IMinerClientController.CloseNTMiner)}", new SignRequest {
                        });
                        ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result;
                        isClosed = response.IsSuccess();
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                if (!isClosed)
                {
                    try {
                        Windows.TaskKill.Kill(processName);
                    }
                    catch (Exception e) {
                        Logger.ErrorDebugLine(e);
                    }
                }
            }
            public void CloseMinerStudio()
            {
                string location = NTMinerRegistry.GetLocation();

                if (string.IsNullOrEmpty(location) || !File.Exists(location))
                {
                    return;
                }
                string processName = Path.GetFileNameWithoutExtension(location);

                if (Process.GetProcessesByName(processName).Length == 0)
                {
                    return;
                }
                bool isClosed = false;

                try {
                    using (HttpClient client = new HttpClient()) {
                        Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://localhost:{Consts.MinerStudioPort}/api/{s_controllerName}/{nameof(IMinerStudioController.CloseMinerStudio)}", new SignRequest {
                        });
                        ResponseBase response = message.Result.Content.ReadAsAsync <ResponseBase>().Result;
                        isClosed = response.IsSuccess();
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                if (!isClosed)
                {
                    try {
                        Windows.TaskKill.Kill(processName);
                    }
                    catch (Exception e) {
                        Logger.ErrorDebugLine(e);
                    }
                }
            }
Esempio n. 14
0
        private static LiteDatabase GetDb()
        {
            string location = NTMinerRegistry.GetLocation();

            if (!string.IsNullOrEmpty(location))
            {
                string dbFile        = Path.Combine(Path.GetDirectoryName(location), NTKeyword.LocalDbFileName);
                bool   isDbFileExist = File.Exists(dbFile);
                if (!isDbFileExist)
                {
                    dbFile        = Path.Combine(EntryAssemblyInfo.TempDirFullName, NTKeyword.LocalDbFileName);
                    isDbFileExist = File.Exists(dbFile);
                }
                if (!isDbFileExist)
                {
                    return(null);
                }
                return(new LiteDatabase($"filename={dbFile}"));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 15
0
        private static LiteDatabase GetDb()
        {
            string location = NTMinerRegistry.GetLocation();

            if (!string.IsNullOrEmpty(location))
            {
                string dbFile        = Path.Combine(Path.GetDirectoryName(location), "local.litedb");
                bool   isDbFileExist = File.Exists(dbFile);
                if (!isDbFileExist)
                {
                    dbFile        = Path.Combine(MainAssemblyInfo.TempDirFullName, "local.litedb");
                    isDbFileExist = File.Exists(dbFile);
                }
                if (!isDbFileExist)
                {
                    return(null);
                }
                return(new LiteDatabase($"filename={dbFile};journal=false"));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 16
0
 static void Main(string[] args)
 {
     if (args.Length != 0)
     {
         if (args.Contains("--sha1"))
         {
             File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1);
             return;
         }
     }
     try {
         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);
     }
 }