コード例 #1
0
        static void Main()
        {
            NTMinerConsole.SetIsMainUiOk(true);
            NTMinerConsole.DisbleQuickEditMode();
            DevMode.SetDevMode();

            Windows.ConsoleHandler.Register(Exit);

            Console.Title = $"{ServerAppType.WsServer.GetName()}_{ServerRoot.HostConfig.ThisServerAddress}";
            // 通过WsServer的网络缓解对WebApiServer的外网流量的压力。WsServer调用WebApiServer的时候走内网调用节省外网带宽
            RpcRoot.SetOfficialServerAddress(ServerRoot.HostConfig.RpcServerLocalAddress);
            // 用本节点的地址作为队列名,消费消息时根据路由键区分消息类型
            string queue        = $"{ServerAppType.WsServer.GetName()}.{ServerRoot.HostConfig.ThisServerAddress}";
            string durableQueue = queue + MqKeyword.DurableQueueEndsWith;

            AbstractMqMessagePath[] mqMessagePaths = new AbstractMqMessagePath[] {
                new ReadOnlyUserMqMessagePath(durableQueue),
                new MinerSignMqMessagePath(durableQueue),
                new WsServerNodeMqMessagePath(queue),
                new OperationMqMessagePath(queue)
            };
            if (!ServerConnection.Create(ServerAppType.WsServer, mqMessagePaths, out IServerConnection serverConfig))
            {
                NTMinerConsole.UserError("启动失败,无法继续,因为服务器上下文创建失败");
                return;
            }
            MinerClientMqSender    = new MinerClientMqSender(serverConfig);
            SpeedDataRedis         = new SpeedDataRedis(serverConfig);
            WsServerNodeRedis      = new WsServerNodeRedis(serverConfig);
            OperationMqSender      = new OperationMqSender(serverConfig);
            UserMqSender           = new UserMqSender(serverConfig);
            _wsServerNodeMqSender  = new WsServerNodeMqSender(serverConfig);
            WsServerNodeAddressSet = new WsServerNodeAddressSet(WsServerNodeRedis, _wsServerNodeMqSender);
            var minerRedis = new ReadOnlyMinerRedis(serverConfig);
            var userRedis  = new ReadOnlyUserRedis(serverConfig);

            VirtualRoot.StartTimer();
            RpcRoot.SetRpcUser(new RpcUser(ServerRoot.HostConfig.RpcLoginName, HashUtil.Sha1(ServerRoot.HostConfig.RpcPassword)));
            RpcRoot.SetIsOuterNet(false);
            // 构造函数中异步访问redis初始化用户列表,因为是异步的所以提前构造
            ReadOnlyUserSet       = new ReadOnlyUserSet(userRedis);
            MinerSignSet          = new MinerSignSet(minerRedis);
            _wsServer             = new SharpWsServer(ServerRoot.HostConfig);
            MinerClientSessionSet = new MinerClientSessionSet(_wsServer.MinerClientWsSessionsAdapter);
            MinerStudioSessionSet = new MinerStudioSessionSet(_wsServer.MinerStudioWsSessionsAdapter);
            _wsServer.Start();
            VirtualRoot.RaiseEvent(new WebSocketServerStatedEvent());
            _started = true;

            Console.ReadKey(true);
            Exit();
        }
コード例 #2
0
 public static void ExportWorkJson(MineWorkData mineWorkData, out string localJson, out string serverJson)
 {
     localJson  = string.Empty;
     serverJson = string.Empty;
     try {
         LocalJsonDb  localJsonObj  = new LocalJsonDb(Instance, mineWorkData);
         ServerJsonDb serverJsonObj = new ServerJsonDb(Instance, localJsonObj);
         localJson  = VirtualRoot.JsonSerializer.Serialize(localJsonObj);
         serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj);
         mineWorkData.ServerJsonSha1 = HashUtil.Sha1(serverJson);
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
     }
 }
コード例 #3
0
        public static string CalcSign(string loginName, string passwordSha1, long timestamp, ISignableData data = null)
        {
            StringBuilder sb;

            if (data == null)
            {
                sb = new StringBuilder();
            }
            else
            {
                sb = data.GetSignData();
            }
            sb.Append("LoginName").Append(loginName).Append("Password").Append(passwordSha1).Append("Timestamp").Append(timestamp);
            return(HashUtil.Sha1(sb.ToString()));
        }
コード例 #4
0
        public void Test1()
        {
            SpeedDto  speedDto1 = VirtualRoot.JsonSerializer.Deserialize <SpeedDto>(File.ReadAllText(Path.Combine(TestUtil.DataDirFullName, "speedData.json")));
            WsMessage message   = new WsMessage(Guid.NewGuid(), WsMessage.Speed)
            {
                Data = speedDto1
            };

            byte[] data     = Encoding.UTF8.GetBytes(VirtualRoot.JsonSerializer.Serialize(message));
            double dataSize = data.Length / NTKeyword.DoubleK;

            Console.WriteLine($"原始大小 {dataSize.ToString()} kb");
            data = message.SignToBytes(HashUtil.Sha1("password1"));
            double dataNewSize = data.Length / NTKeyword.DoubleK;

            Assert.IsTrue(VirtualRoot.BinarySerializer.IsGZipped(data));
            Console.WriteLine($"序列化后大小 {dataNewSize.ToString()} kb,是原来大小的 {(dataNewSize * 100 / dataSize).ToString()} %");
            message = VirtualRoot.BinarySerializer.Deserialize <WsMessage>(data);
            Assert.IsTrue(message.TryGetData(out SpeedDto speedDto2));
            Assert.AreEqual(speedDto1.ClientId, speedDto2.ClientId);
        }
コード例 #5
0
 public static string CalcSign(this WsMessage message, string password)
 {
     return(HashUtil.Sha1(GetSignData(message).Append(password).ToString()));
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: wuzongwen/NtMiner
        private static void UpdateAsync()
        {
            Task.Factory.StartNew(() => {
                try {
                    Task <byte[]> htmlDataTask = GetHtmlAsync("https://www.f2pool.com/");
                    byte[] htmlData            = null;
                    try {
                        Task.WaitAll(new Task[] { htmlDataTask }, 60 * 1000);
                        htmlData = htmlDataTask.Result;
                    }
                    catch {
                    }
                    if (htmlData != null && htmlData.Length != 0)
                    {
                        NTMinerConsole.UserOk($"{DateTime.Now.ToString()} - 鱼池首页html获取成功");
                        string html   = Encoding.UTF8.GetString(htmlData);
                        double usdCny = PickUsdCny(html);
                        NTMinerConsole.UserInfo($"usdCny={usdCny.ToString()}");
                        List <IncomeItem> incomeItems = PickIncomeItems(html);
                        NTMinerConsole.UserInfo($"鱼池首页有{incomeItems.Count.ToString()}个币种");
                        FillCny(incomeItems, usdCny);
                        NeatenSpeedUnit(incomeItems);
                        if (incomeItems != null && incomeItems.Count != 0)
                        {
                            RpcRoot.Login(new RpcUser(Config.RpcLoginName, HashUtil.Sha1(Config.RpcPassword)));
                            RpcRoot.SetIsOuterNet(false);
                            RpcRoot.OfficialServer.CalcConfigBinaryService.GetCalcConfigsAsync(data => {
                                NTMinerConsole.UserInfo($"NTMiner有{data.Count.ToString()}个币种");
                                HashSet <string> coinCodes = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                                foreach (CalcConfigData calcConfigData in data)
                                {
                                    IncomeItem incomeItem = incomeItems.FirstOrDefault(a => string.Equals(a.CoinCode, calcConfigData.CoinCode, StringComparison.OrdinalIgnoreCase));
                                    if (incomeItem != null)
                                    {
                                        coinCodes.Add(calcConfigData.CoinCode);
                                        calcConfigData.Speed           = incomeItem.Speed;
                                        calcConfigData.SpeedUnit       = incomeItem.SpeedUnit;
                                        calcConfigData.NetSpeed        = incomeItem.NetSpeed;
                                        calcConfigData.NetSpeedUnit    = incomeItem.NetSpeedUnit;
                                        calcConfigData.IncomePerDay    = incomeItem.IncomeCoin;
                                        calcConfigData.IncomeUsdPerDay = incomeItem.IncomeUsd;
                                        calcConfigData.IncomeCnyPerDay = incomeItem.IncomeCny;
                                        calcConfigData.ModifiedOn      = DateTime.Now;
                                        if (calcConfigData.ModifiedOn.AddMinutes(15) > calcConfigData.ModifiedOn.Date.AddDays(1))
                                        {
                                            calcConfigData.BaseNetSpeed     = calcConfigData.NetSpeed;
                                            calcConfigData.BaseNetSpeedUnit = calcConfigData.NetSpeedUnit;
                                        }
                                        else if (calcConfigData.BaseNetSpeed != 0)
                                        {
                                            if (calcConfigData.NetSpeedUnit == calcConfigData.BaseNetSpeedUnit)
                                            {
                                                calcConfigData.DayWave = (calcConfigData.NetSpeed - calcConfigData.BaseNetSpeed) / calcConfigData.BaseNetSpeed;
                                            }
                                            else
                                            {
                                                if (string.IsNullOrEmpty(calcConfigData.BaseNetSpeedUnit))
                                                {
                                                    calcConfigData.BaseNetSpeedUnit = calcConfigData.NetSpeedUnit;
                                                }
                                                var netSpeed           = calcConfigData.NetSpeed.FromUnitSpeed(calcConfigData.NetSpeedUnit);
                                                var baseNetSpeed       = calcConfigData.BaseNetSpeed.FromUnitSpeed(calcConfigData.BaseNetSpeedUnit);
                                                calcConfigData.DayWave = (netSpeed - baseNetSpeed) / baseNetSpeed;
                                            }
                                        }
                                    }
                                }
                                RpcRoot.OfficialServer.CalcConfigService.SaveCalcConfigsAsync(data, callback: (res, e) => {
                                    if (!res.IsSuccess())
                                    {
                                        VirtualRoot.Out.ShowError(res.ReadMessage(e), autoHideSeconds: 4);
                                    }
                                });
                                foreach (IncomeItem incomeItem in incomeItems)
                                {
                                    if (coinCodes.Contains(incomeItem.CoinCode))
                                    {
                                        continue;
                                    }
                                    NTMinerConsole.UserInfo(incomeItem.ToString());
                                }

                                foreach (var incomeItem in incomeItems)
                                {
                                    if (!coinCodes.Contains(incomeItem.CoinCode))
                                    {
                                        continue;
                                    }
                                    NTMinerConsole.UserOk(incomeItem.ToString());
                                }

                                NTMinerConsole.UserOk($"更新了{coinCodes.Count.ToString()}个币种:{string.Join(",", coinCodes)}");
                                int unUpdatedCount = data.Count - coinCodes.Count;
                                NTMinerConsole.UserWarn($"{unUpdatedCount.ToString()}个币种未更新{(unUpdatedCount == 0 ? string.Empty : ":" + string.Join(",", data.Select(a => a.CoinCode).Except(coinCodes)))}");
                            });
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
            });
        }
コード例 #7
0
        public string GetSign(string password)
        {
            StringBuilder sb = GetSignData().Append(nameof(UserData.Password)).Append(password);

            return(HashUtil.Sha1(sb.ToString()));
        }
コード例 #8
0
ファイル: SingleUser.cs プロジェクト: zheng9980419/ntminer
 public static string GetRemotePassword(Guid clientId)
 {
     return(HashUtil.Sha1($"{HashUtil.Sha1(PasswordSha1)}{clientId}"));
 }
コード例 #9
0
ファイル: UnitTest1.cs プロジェクト: feitianyiren/NtMiner
 public void Sha1Test()
 {
     Assert.AreEqual(string.Empty, HashUtil.Sha1(string.Empty));
     Assert.AreEqual(string.Empty, HashUtil.Sha1((string)null));
 }