Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Noise/Noise.Core.Test/PeerConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void PeerShouldAddAndGetEndpointsDistinct()

var expectedCount = 2;

Assert.Equal(expectedCount, pc.GetEndpoints().Count());
Assert.Equal(expectedCount, pc.GetEndpoints(false).Count());
}

[Fact]
Expand Down Expand Up @@ -288,6 +288,30 @@ public void PeerShouldSkipDisconnectedEndpoints()
Assert.Equal(expectedCount, pc.GetEndpoints(true).Count());
}

[Fact]
public void PeerShoulUpdateConnectedStatusForEndpoint()
{
var pc = MockUpPeerConfiguration();

var firstEndpoint = "127.0.0.1";
pc.InsertEndpoint(firstEndpoint);

var secondEndpoint = "127.0.0.2";
pc.InsertEndpoint(secondEndpoint);

pc.SetEndpointAsDisconnected(firstEndpoint);

var expectedCountBefore = 1;

Assert.Equal(expectedCountBefore, pc.GetEndpoints(true).Count());

pc.SetEndpointAsConnected(firstEndpoint);

var expectedCountAfter = 2;

Assert.Equal(expectedCountAfter, pc.GetEndpoints(true).Count());
}

[Fact]
public void PeerShouldApplyPreference()
{
Expand Down
6 changes: 6 additions & 0 deletions Noise/Noise.Core/Client/NoiseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ private void Connect()
}
catch (Exception)
{
if (_peerConfiguration.Preferences.UseEndpointAttemptFilter)
{
LogVerbose($"Endpoint: {_peerIp} will be marked as a disconnected endpoint.");
_peerConfiguration.SetEndpointAsDisconnected(_peerIp);
}

throw;
}

Expand Down
18 changes: 15 additions & 3 deletions Noise/Noise.Core/Peer/PeerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ private int GenerateOrdinalNumberIdentifier()

public IEnumerable<PeerEndpoint> GetEndpoints(bool onlyConnected = true)
{
return onlyConnected
? _peerEndpoints.Where(e => e.IsConnected)
: _peerEndpoints;
if (!onlyConnected) return _peerEndpoints;

return _peerEndpoints
.Where(e =>
e.IsConnected ||
e.LastRequestAttempt is null ||
e.LastRequestAttempt.Value.AddSeconds(Preferences.EndpointAttemptIntervalSeconds) < DateTime.Now);
}

public IEnumerable<RemotePeer> GetPeers()
Expand All @@ -52,6 +56,14 @@ public void SetEndpointAsDisconnected(string endpoint)
_peerEndpoints.Single(e => e.Endpoint == endpoint).SetDisconnected();
}

public void SetEndpointAsConnected(string endpoint)
{
if (!IsEndpointKnown(endpoint))
throw new PeerDataException(PeerDataProblemType.ENDPOINT_NOT_FOUND);

_peerEndpoints.Single(e => e.Endpoint == endpoint).SetConnected();
}

public void InsertPeer(string publicKey, string receivingSignature = null, string alias = null)
{
if (publicKey == Secrets.PublicKey)
Expand Down
21 changes: 17 additions & 4 deletions Noise/Noise.Core/Peer/PeerEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ public class PeerEndpoint
public string Endpoint { get; private set; }
public bool IsConnected { get; private set; }

public void SetConnected() => IsConnected = true;
public void SetDisconnected() => IsConnected = false;
public DateTime? LastRequestAttempt { get; private set; }

public void SetConnected()
{
IsConnected = true;
LastRequestAttempt = null;
}

public void SetDisconnected()
{
IsConnected = false;
LastRequestAttempt = DateTime.Now;
}

public PeerEndpointPersistence Serialize()
{
Expand All @@ -34,7 +45,8 @@ public static PeerEndpoint FromParameters(string endpoint)
return new PeerEndpoint
{
Endpoint = ipv4Address,
IsConnected = true
IsConnected = true,
LastRequestAttempt = null
};
}

Expand All @@ -43,7 +55,8 @@ public static PeerEndpoint Deserialize(PeerEndpointPersistence peerEndpoint)
return new PeerEndpoint
{
Endpoint = peerEndpoint.Endpoint,
IsConnected = true
IsConnected = true,
LastRequestAttempt = null
};
}
}
Expand Down
24 changes: 19 additions & 5 deletions Noise/Noise.Core/Peer/PeerPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class PeerPreferences
[ConfigurablePreference]
public string IndependentMediumCertification { get; private set; }

[ConfigurablePreference]
public bool UseEndpointAttemptFilter { get; private set; }

[ConfigurablePreference]
public int EndpointAttemptIntervalSeconds { get; private set; }

public bool ApplyPreference(string name, string value)
{
try
Expand Down Expand Up @@ -62,7 +68,9 @@ public PeerPreferencesPersistence Serialize()
{
VerboseMode = VerboseMode,
UseTracker = UseTracker,
IndependentMediumCertification = IndependentMediumCertification
IndependentMediumCertification = IndependentMediumCertification,
UseEndpointAttemptFilter = UseEndpointAttemptFilter,
EndpointAttemptIntervalSeconds = EndpointAttemptIntervalSeconds
};
}

Expand All @@ -75,17 +83,23 @@ public static PeerPreferences Initialize()
{
VerboseMode = false,
UseTracker = false,
IndependentMediumCertification = string.Empty
IndependentMediumCertification = string.Empty,
UseEndpointAttemptFilter = true,
EndpointAttemptIntervalSeconds = 60 * 5
};
}

public static PeerPreferences Deserialize(PeerPreferencesPersistence peerPreferences)
{
var defaultPreferences = Initialize();

return new PeerPreferences
{
VerboseMode = peerPreferences.VerboseMode,
UseTracker = peerPreferences.UseTracker,
IndependentMediumCertification = peerPreferences.IndependentMediumCertification
VerboseMode = peerPreferences.VerboseMode ?? defaultPreferences.VerboseMode,
UseTracker = peerPreferences.UseTracker ?? defaultPreferences.UseTracker,
IndependentMediumCertification = peerPreferences.IndependentMediumCertification ?? defaultPreferences.IndependentMediumCertification,
UseEndpointAttemptFilter = peerPreferences.UseEndpointAttemptFilter ?? defaultPreferences.UseEndpointAttemptFilter,
EndpointAttemptIntervalSeconds = peerPreferences.EndpointAttemptIntervalSeconds ?? defaultPreferences.EndpointAttemptIntervalSeconds
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
{
public class PeerPreferencesPersistence
{
public bool UseTracker { get; set; }
public bool VerboseMode { get; set; }
public bool? UseTracker { get; set; }
public bool? VerboseMode { get; set; }
public string IndependentMediumCertification { get; set; }
public bool? UseEndpointAttemptFilter { get; set; }
public int? EndpointAttemptIntervalSeconds { get; set; }
}
}
7 changes: 7 additions & 0 deletions Noise/Noise.Core/Server/NoiseServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ private void PeerConnectedEventHandler(object sender, PeerConnectedEventArgs e)
{
var senderEndpoint = e.PeerEndpoint;
LogVerbose($"Peer with endpoint: {senderEndpoint} disconnected.");

if (_peerConfiguration.Preferences.UseEndpointAttemptFilter)
{
// TODO: A new preference will be introduced to describe how to handle unknown endpoints that has connected to us.
if (_peerConfiguration.IsEndpointKnown(senderEndpoint))
_peerConfiguration.SetEndpointAsConnected(senderEndpoint);
}
}

private void SignatureReceivedEventHandler(object sender, SignatureReceivedEventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions Noise/Noise.Host/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private async Task ExecuteSign(string[] args, CancellationTokenSource cts)
if (selectedPeer.SendingSignature is not null && !overrite)
throw new CommandHandlerException($"This peer has a signature assigned.{Environment.NewLine}{usage}");

foreach (var endpoint in _peerConfiguration.GetEndpoints())
foreach (var endpoint in _peerConfiguration.GetEndpoints(true))
{
using var client = CreateClient(endpoint.Endpoint);
await client.SendSignature(selectedPeer.PublicKey, cts.Token);
Expand All @@ -198,7 +198,7 @@ private async Task ExecuteSend(string[] args, CancellationTokenSource cts)
var messageStringBuilder = new StringBuilder(string.Empty);
foreach (var a in args) messageStringBuilder.Append(a);

foreach (var endpoint in _peerConfiguration.GetEndpoints())
foreach (var endpoint in _peerConfiguration.GetEndpoints(true))
{
using var client = CreateClient(endpoint.Endpoint);
await client.SendMessage(selectedPeer.PublicKey, messageStringBuilder.ToString(), cts.Token);
Expand Down Expand Up @@ -237,7 +237,7 @@ private void ExecuteList(string[] args)

if (type == "endpoint")
{
foreach (var endpoint in _peerConfiguration.GetEndpoints())
foreach (var endpoint in _peerConfiguration.GetEndpoints(true))
{
_outputMonitor.WriteRaw(endpoint.Endpoint, true);
}
Expand Down