Esempio n. 1
0
        /// <inheritdoc />
        public void BanPeer(IPEndPoint endpoint, int banTimeSeconds, string reason = null)
        {
            Guard.NotNull(endpoint, nameof(endpoint));
            this.logger.LogTrace("({0}:'{1}',{2}:'{3}')", nameof(endpoint), endpoint, nameof(reason), reason);

            reason = reason ?? "unknown";

            bool        banPeer = true;
            NetworkPeer peer    = this.connectionManager.ConnectedPeers.FindByEndpoint(endpoint);

            if (peer != null)
            {
                ConnectionManagerBehavior peerBehavior = peer.Behavior <ConnectionManagerBehavior>();
                if (!peerBehavior.Whitelisted)
                {
                    peer.Disconnect($"The peer was banned, reason: {reason}");
                }
                else
                {
                    banPeer = false;
                    this.logger.LogTrace("Peer '{0}' is whitelisted, for reason '{1}' it was not banned!", endpoint, reason);
                }
            }

            if (banPeer)
            {
                this.logger.LogDebug("Peer '{0}' banned for reason '{1}'.", endpoint, reason);
                this.banStore.BanPeer(endpoint, this.dateTimeProvider.GetUtcNow().AddSeconds(banTimeSeconds));
            }

            this.logger.LogTrace("(-)");
        }
Esempio n. 2
0
        public string GetNodeStats()
        {
            var builder = new StringBuilder();

            foreach (NetworkPeer node in this.ConnectedPeers)
            {
                ConnectionManagerBehavior connectionManagerBehavior = node.Behavior <ConnectionManagerBehavior>();
                ChainHeadersBehavior      chainHeadersBehavior      = node.Behavior <ChainHeadersBehavior>();

                string agent = node.PeerVersion != null ? node.PeerVersion.UserAgent : "[Unknown]";
                builder.AppendLine(
                    "Peer:" + (node.RemoteInfo() + ", ").PadRight(LoggingConfiguration.ColumnLength + 15) +
                    (" connected" + " (" + (connectionManagerBehavior.Inbound ? "inbound" : "outbound") + "),").PadRight(LoggingConfiguration.ColumnLength + 7) +
                    (" agent " + agent + ", ").PadRight(LoggingConfiguration.ColumnLength + 2) +
                    " height=" + (chainHeadersBehavior.PendingTip != null ? chainHeadersBehavior.PendingTip.Height.ToString() : "unknown"));
            }

            return(builder.ToString());
        }
Esempio n. 3
0
        public List <PeerNodeModel> GetPeerInfo()
        {
            List <PeerNodeModel> peerList = new List <PeerNodeModel>();

            List <NetworkPeer> nodes = this.ConnectionManager.ConnectedPeers.ToList();

            foreach (NetworkPeer node in nodes)
            {
                if ((node != null) && (node.RemoteSocketAddress != null))
                {
                    PeerNodeModel peerNode = new PeerNodeModel
                    {
                        Id      = nodes.IndexOf(node),
                        Address = node.RemoteSocketEndpoint.ToString()
                    };

                    if (node.MyVersion != null)
                    {
                        peerNode.LocalAddress   = node.MyVersion.AddressReceiver?.ToString();
                        peerNode.Services       = ((ulong)node.MyVersion.Services).ToString("X");
                        peerNode.Version        = (uint)node.MyVersion.Version;
                        peerNode.SubVersion     = node.MyVersion.UserAgent;
                        peerNode.StartingHeight = node.MyVersion.StartHeight;
                    }

                    ConnectionManagerBehavior connectionManagerBehavior = node.Behavior <ConnectionManagerBehavior>();
                    if (connectionManagerBehavior != null)
                    {
                        peerNode.Inbound       = connectionManagerBehavior.Inbound;
                        peerNode.IsWhiteListed = connectionManagerBehavior.Whitelisted;
                    }

                    if (node.TimeOffset != null)
                    {
                        peerNode.TimeOffset = node.TimeOffset.Value.Seconds;
                    }

                    peerList.Add(peerNode);
                }
            }

            return(peerList);
        }
Esempio n. 4
0
        /// <inheritdoc />
        protected override void AttachCore()
        {
            this.logger.LogTrace("()");

            var peer = this.AttachedPeer;

            if (peer.State == NetworkPeerState.Connected)
            {
                if (this.peerBanning.IsBanned(peer.RemoteSocketEndpoint))
                {
                    this.logger.LogDebug("Peer '{0}' was previously banned.", peer.RemoteSocketEndpoint);
                    peer.Disconnect("A banned node tried to connect.");
                    return;
                }
            }

            this.AttachedPeer.MessageReceived += this.AttachedNode_MessageReceived;
            this.chainHeadersBehavior          = this.AttachedPeer.Behaviors.Find <ChainHeadersBehavior>();
            this.connectionManagerBehavior     = this.AttachedPeer.Behaviors.Find <ConnectionManagerBehavior>();

            this.logger.LogTrace("(-)");
        }