public List <PeerNodeModel> GetPeerInfo() { List <PeerNodeModel> peerList = new List <PeerNodeModel>(); List <INetworkPeer> peers = this.ConnectionManager.ConnectedPeers.ToList(); foreach (INetworkPeer peer in peers) { if ((peer != null) && (peer.RemoteSocketAddress != null)) { PeerNodeModel peerNode = new PeerNodeModel { Id = peers.IndexOf(peer), Address = peer.RemoteSocketEndpoint.ToString() }; if (peer.MyVersion != null) { peerNode.LocalAddress = peer.MyVersion.AddressReceiver?.ToString(); peerNode.Services = ((ulong)peer.MyVersion.Services).ToString("X"); peerNode.Version = (uint)peer.MyVersion.Version; peerNode.SubVersion = peer.MyVersion.UserAgent; peerNode.StartingHeight = peer.MyVersion.StartHeight; } ConnectionManagerBehavior connectionManagerBehavior = peer.Behavior <ConnectionManagerBehavior>(); if (connectionManagerBehavior != null) { peerNode.Inbound = connectionManagerBehavior.Inbound; peerNode.IsWhiteListed = connectionManagerBehavior.Whitelisted; } if (peer.TimeOffset != null) { peerNode.TimeOffset = peer.TimeOffset.Value.Seconds; } peerList.Add(peerNode); } } return(peerList); }
public string GetNodeStats() { var builder = new StringBuilder(); foreach (INetworkPeer peer in this.ConnectedPeers) { ConnectionManagerBehavior connectionManagerBehavior = peer.Behavior <ConnectionManagerBehavior>(); ChainHeadersBehavior chainHeadersBehavior = peer.Behavior <ChainHeadersBehavior>(); string agent = peer.PeerVersion != null ? peer.PeerVersion.UserAgent : "[Unknown]"; builder.AppendLine( "Peer:" + (peer.RemoteSocketEndpoint + ", ").PadRight(LoggingConfiguration.ColumnLength + 15) + (" connected:" + (connectionManagerBehavior.Inbound ? "inbound" : "outbound") + ",").PadRight(LoggingConfiguration.ColumnLength + 7) + (" height:" + (chainHeadersBehavior.PendingTip != null ? chainHeadersBehavior.PendingTip.Height.ToString() : peer.PeerVersion?.StartHeight.ToString() ?? "unknown") + ",").PadRight(LoggingConfiguration.ColumnLength + 2) + " agent:" + agent); } return(builder.ToString()); }
/// <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"; INetworkPeer 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 { this.logger.LogTrace("(-)[WHITELISTED]"); return; } } PeerAddress peerAddress = this.peerAddressManager.FindPeer(endpoint); if (peerAddress == null) { this.logger.LogTrace("(-)[PEERNOTFOUND]"); return; } peerAddress.BanTimeStamp = this.dateTimeProvider.GetUtcNow(); peerAddress.BanUntil = this.dateTimeProvider.GetUtcNow().AddSeconds(banTimeSeconds); peerAddress.BanReason = reason; this.logger.LogDebug("Peer '{0}' banned for reason '{1}', until {2}.", endpoint, reason, peerAddress.BanUntil.ToString()); this.logger.LogTrace("(-)"); }
/// <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."); this.logger.LogTrace("(-)[PEER_BANNED]"); return; } } this.AttachedPeer.MessageReceived.Register(this.OnMessageReceivedAsync); this.chainHeadersBehavior = this.AttachedPeer.Behaviors.Find <ChainHeadersBehavior>(); this.connectionManagerBehavior = this.AttachedPeer.Behaviors.Find <ConnectionManagerBehavior>(); this.eventHandlerRegistered = true; this.logger.LogTrace("(-)"); }