///--------------------------------------------------------------------------------------------------------
        ///
        static public List <QtumPeerInfo> GetPeerList()
        {
            List <QtumPeerInfo> list = new List <QtumPeerInfo>();

            string peerInfoStr = "{\n PeerList : " + commandline.Process("getpeerinfo") + "\n}";

            JObject json = null;

            if (TryParseJson(peerInfoStr, out json) == false)
            {
                return(list);
            }

            JToken peerListJson = json["PeerList"];

            for (int i = 0; i < peerListJson.Count(); ++i)
            {
                JToken peerInfo = peerListJson[i];

                QtumPeerInfo newInfo = new QtumPeerInfo();

                double ping    = 0;
                double minPing = 0;

                if (peerInfo["pingwait"] != null)
                {
                    double.TryParse(peerInfo["pingwait"].ToString(), out ping);
                    minPing = ping;
                }
                else
                {
                    if (peerInfo["minping"] != null)
                    {
                        double.TryParse(peerInfo["minping"].ToString(), out minPing);
                    }

                    if (peerInfo["pingtime"] != null)
                    {
                        double.TryParse(peerInfo["pingtime"].ToString(), out ping);
                    }
                }

                newInfo.pingTime   = (ping + minPing) * 0.5;
                newInfo.addr       = peerInfo["addr"].ToString();
                newInfo.version    = peerInfo["version"].ToString();
                newInfo.subVersion = peerInfo["subver"].ToString();

                list.Add(newInfo);
            }

            return(list);
        }
        ///--------------------------------------------------------------------------------------------------------
        ///
        static public void BanPeer(QtumPeerInfo peer)
        {
            if (peer == null || string.IsNullOrEmpty(peer.addr))
            {
                return;
            }

            string address = peer.addr;

            if (address.IndexOf(":") != -1)
            {
                address = address.Substring(0, address.IndexOf(":"));
            }

            commandline.Process(string.Format("setban \"{0}\" \"add\" \"86400\"", address));
        }
Пример #3
0
        static public void Update()
        {
            if (((DateTime.Now - lastUpdateTime).Ticks / TimeSpan.TicksPerSecond) < 30)
            {
                return;
            }

            lastUpdateTime = DateTime.Now;

            try
            {
                List <QtumPeerInfo> peerList = QtumHandler.GetPeerList();

                for (int i = 0; i < peerList.Count; ++i)
                {
                    QtumPeerInfo info = peerList[i];

                    bool isBad = false;

                    if (info.pingTime >= Config.MiniumPing)
                    {
                        isBad = true;
                    }
                    else if (string.IsNullOrEmpty(info.subVersion) == false)
                    {
                        string subVersion = info.subVersion.Replace("/Satoshi:", "").Replace("/", "");

                        string[] subVersionStrList = subVersion.Split('.');

                        if (subVersionStrList.Length < 3)
                        {
                            isBad = true;
                        }
                        else
                        {
                            for (int n = 0; n < subVersionStrList.Length; ++n)
                            {
                                if (n >= 3)
                                {
                                    break;
                                }

                                int verNum = 0;
                                int.TryParse(subVersionStrList[n], out verNum);

                                if (Config.MiniumVersion[n] > verNum)
                                {
                                    isBad = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (isBad)
                    {
                        QtumHandler.BanPeer(info);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Log(e.ToString());
            }
        }