public void PeerBanning_SavingAndLoading_BannedPeerToAddressManager() { DataFolder dataFolder = CreateDataFolder(this); var loggerFactory = ExtendedLoggerFactory.Create(); IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object); peerAddressManager.AddPeer(endpoint, IPAddress.Loopback); var nodeSettings = new NodeSettings(new StratisRegTest()); var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings); var peerCollection = new Mock <IReadOnlyNetworkPeerCollection>(); peerCollection.Setup(p => p.FindByIp(It.IsAny <IPAddress>())).Returns(new List <INetworkPeer>()); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.ConnectionSettings).Returns(connectionManagerSettings); connectionManager.Setup(c => c.ConnectedPeers).Returns(peerCollection.Object); var peerBanning = new PeerBanning(connectionManager.Object, loggerFactory, DateTimeProvider.Default, peerAddressManager); peerBanning.BanAndDisconnectPeer(endpoint, connectionManagerSettings.BanTimeSeconds, nameof(PeerBanningTest)); peerAddressManager.SavePeers(); peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object); peerAddressManager.LoadPeers(); PeerAddress peer = peerAddressManager.FindPeer(endpoint); Assert.NotNull(peer.BanTimeStamp); Assert.NotNull(peer.BanUntil); Assert.NotEmpty(peer.BanReason); }
public void PeerState_AllConnectionDataGetsReset() { IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); PeerAddressManager peerAddressManager = this.CreatePeerAddressManager(); peerAddressManager.AddPeer(endpoint, IPAddress.Loopback); peerAddressManager.PeerConnected(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerAttempted(endpoint, DateTime.UtcNow); peerAddressManager.PeerConnected(endpoint, DateTime.UtcNow); PeerAddress peerOne = peerAddressManager.FindPeer(endpoint); Assert.Equal(0, peerOne.ConnectionAttempts); Assert.Null(peerOne.LastAttempt); Assert.NotNull(peerOne.LastConnectionSuccess); }
public void PeerBanning_Add_Peer_To_Address_Manager_And_Ban() { var dataFolder = CreateDataFolder(this); var loggerFactory = new ExtendedLoggerFactory(); loggerFactory.AddConsoleWithFilters(); var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, dataFolder, loggerFactory, new Mock <ISelfEndpointTracker>().Object); peerAddressManager.AddPeer(endpoint, endpoint.Address.MapToIPv6()); var nodeSettings = new NodeSettings(new StratisRegTest()); var connectionManagerSettings = new ConnectionManagerSettings(nodeSettings); var peerCollection = new Mock <IReadOnlyNetworkPeerCollection>(); peerCollection.Setup(p => p.FindByIp(It.IsAny <IPAddress>())).Returns(new List <INetworkPeer>()); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.ConnectionSettings).Returns(connectionManagerSettings); connectionManager.Setup(c => c.ConnectedPeers).Returns(peerCollection.Object); var peerBanning = new PeerBanning(connectionManager.Object, loggerFactory, DateTimeProvider.Default, peerAddressManager); peerBanning.BanAndDisconnectPeer(endpoint, connectionManagerSettings.BanTimeSeconds, nameof(PeerBanningTest)); PeerAddress peer = peerAddressManager.FindPeer(endpoint); Assert.True(peer.BanUntil.HasValue); Assert.NotNull(peer.BanUntil); Assert.NotEmpty(peer.BanReason); }
public void PeerAddressManager_AttemptThresholdReached_ResetAttempts() { IPAddress ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); DataFolder peerFolder = CreateDataFolder(this); var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object, this.connectionManagerSettings)); addressManager.AddPeer(endpoint, IPAddress.Loopback); DateTime applicableDate = DateTimeProvider.Default.GetUtcNow(); //Ensure that there was 10 failed attempts for (int i = 0; i < 10; i++) { addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-i)); } //Ensure that the last attempt was more than 12 hours ago addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-13)); //This call should now reset the counts DateTime resetTimestamp = DateTimeProvider.Default.GetUtcNow(); addressManager.PeerAttempted(endpoint, resetTimestamp); PeerAddress savedPeer = addressManager.FindPeer(endpoint); Assert.Equal(1, savedPeer.ConnectionAttempts); Assert.Equal(resetTimestamp, savedPeer.LastAttempt); Assert.Null(savedPeer.LastConnectionSuccess); Assert.Null(savedPeer.LastConnectionHandshake); Assert.Null(savedPeer.LastSeen); Assert.Equal("::ffff:127.0.0.1", savedPeer.Loopback.ToString()); }
public void PeerState_AllConnectionDataGetsReset() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var networkAddress = new NetworkAddress(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(peerFolder, this.extendedLoggerFactory); addressManager.AddPeer(networkAddress, IPAddress.Loopback); addressManager.PeerConnected(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerAttempted(networkAddress.Endpoint, DateTime.UtcNow); addressManager.PeerConnected(networkAddress.Endpoint, DateTime.UtcNow); var peerOne = addressManager.FindPeer(networkAddress.Endpoint); Assert.Equal(0, peerOne.ConnectionAttempts); Assert.Null(peerOne.LastConnectionAttempt); Assert.NotNull(peerOne.LastConnectionSuccess); }
public void PeerAddressManager_AttemptThresholdReached_ResetAttempts() { var ipAddress = IPAddress.Parse("::ffff:192.168.0.1"); var endpoint = new IPEndPoint(ipAddress, 80); var peerFolder = AssureEmptyDirAsDataFolder(Path.Combine(AppContext.BaseDirectory, "PeerAddressManager")); var addressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.loggerFactory); addressManager.AddPeer(endpoint, IPAddress.Loopback); var applicableDate = DateTimeProvider.Default.GetUtcNow(); //Ensure that there was 10 failed attempts for (int i = 0; i < 10; i++) { addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-i)); } //Ensure that the last attempt was more than 12 hours ago addressManager.PeerAttempted(endpoint, applicableDate.AddHours(-13)); //This call should now reset the counts var resetTimestamp = DateTimeProvider.Default.GetUtcNow(); addressManager.PeerAttempted(endpoint, resetTimestamp); var savedPeer = addressManager.FindPeer(endpoint); Assert.Equal(1, savedPeer.ConnectionAttempts); Assert.Equal(resetTimestamp, savedPeer.LastAttempt); Assert.Null(savedPeer.LastConnectionSuccess); Assert.Null(savedPeer.LastConnectionHandshake); Assert.Null(savedPeer.LastSeen); Assert.Equal("127.0.0.1", savedPeer.Loopback.ToString()); }
public void PeerConnectorDiscovery_ConnectsTo_NodeInSameNetworkGroup_WithIpRangeFilteringDisabled() { // IpRangeFiltering enabled by default, disabled explicitly. var nodeSettings = new NodeSettings(this.Network, args: new[] { "-IpRangeFiltering=false" }); var connectionManagerSettingsExisting = new ConnectionManagerSettings(nodeSettings); DataFolder peerFolder = CreateDataFolder(this); var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, peerFolder, this.extendedLoggerFactory, new SelfEndpointTracker(this.extendedLoggerFactory, connectionManagerSettingsExisting)); Mock <INetworkPeerFactory> networkPeerFactoryExisting = new Mock <INetworkPeerFactory>(); Mock <IConnectionManager> connectionManagerExisting = new Mock <IConnectionManager>(); var networkPeerParameters = new NetworkPeerConnectionParameters(); networkPeerParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(connectionManagerExisting.Object, this.extendedLoggerFactory)); connectionManagerExisting.SetupGet(np => np.Parameters).Returns(networkPeerParameters); connectionManagerExisting.SetupGet(np => np.ConnectedPeers).Returns(new NetworkPeerCollection()); var peerConnector = new PeerConnectorDiscovery(this.asyncProvider, DateTimeProvider.Default, this.extendedLoggerFactory, this.Network, networkPeerFactoryExisting.Object, this.nodeLifetime, nodeSettings, connectionManagerSettingsExisting, peerAddressManager, new SelfEndpointTracker(this.extendedLoggerFactory, connectionManagerSettingsExisting)); peerConnector.Initialize(connectionManagerExisting.Object); //Peer 1. IPAddress originalAddressPeer1 = IPAddress.Parse("::ffff:57.48.183.81"); // ipv4 var endpointPeer1 = new IPEndPoint(originalAddressPeer1, 80); peerAddressManager.AddPeer(endpointPeer1, IPAddress.Loopback); bool connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer1, connectionManagerExisting); Assert.True(connectedToThisPeer); // Peer 2 has different network group to Peer 1. IPAddress addressInDifferentNetworkGroupPeer2 = IPAddress.Parse("99be:f5c5:adc2:525c:f6d7:7b30:5336:5a0f"); // ipv6 var endpointPeer2 = new IPEndPoint(addressInDifferentNetworkGroupPeer2, 80); peerAddressManager.AddPeer(endpointPeer2, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer2, connectionManagerExisting); Assert.True(connectedToThisPeer); // Different network group: connects. // Peer 3 in same network group as Peer 2. IPAddress addressInSameNetworkGroupPeer3 = IPAddress.Parse("99be:f5c5:adc2:525c:db45:d36e:ce01:a394"); // ipv6 var endpointPeer3 = new IPEndPoint(addressInSameNetworkGroupPeer3, 80); peerAddressManager.AddPeer(endpointPeer3, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer3, connectionManagerExisting); Assert.True(connectedToThisPeer); // Same network group: connects. // Peer 4 has different network group to Peer 1. IPAddress addressInDifferentNetworkGroupPeer4 = IPAddress.Parse("::ffff:58.48.183.81"); // ipv4 var endpointPeer4 = new IPEndPoint(addressInDifferentNetworkGroupPeer4, 80); peerAddressManager.AddPeer(endpointPeer4, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer4, connectionManagerExisting); Assert.True(connectedToThisPeer); // Different network group: connects. // Peer 5 has same network group as Peer 1. IPAddress addressInSameNetworkGroupPeer5 = IPAddress.Parse("::ffff:57.48.183.82"); // ipv4 var endpointPeer5 = new IPEndPoint(addressInSameNetworkGroupPeer5, 80); peerAddressManager.AddPeer(endpointPeer5, IPAddress.Loopback); connectedToThisPeer = ConnectToPeer(peerAddressManager, networkPeerFactoryExisting, connectionManagerSettingsExisting, peerConnector, endpointPeer5, connectionManagerExisting); Assert.True(connectedToThisPeer); // Same network group: connects. }