Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate connection #1685

Merged
merged 10 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
23 changes: 23 additions & 0 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ internal static IPEndPoint GetIpEndPoint(string hostAndPort)
return null;
}

/// <summary>
/// Check the new connection <br/>
/// If it is equal to the Nonce of local or any remote node, it'll return false, else we'll return true and update the Listener address of the connected remote node.
/// </summary>
/// <param name="actor">Remote node actor</param>
/// <param name="node">Remote node</param>
public bool AllowNewConnection(IActorRef actor, RemoteNode node)
{
if (node.Version.Nonce == Nonce) return false;

foreach (var other in RemoteNodes.Values)
{
// filter duplicate connections
if (other != node && other.Remote.Address.Equals(node.Remote.Address) && other.Version?.Nonce == node.Version.Nonce)
return false;
}
if (node.Remote.Port != node.ListenerTcpPort && node.ListenerTcpPort != 0)
{
ConnectedPeers.TryUpdate(actor, node.Listener, node.Remote);
}
return true;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
{
return RemoteNodes.Values;
Expand Down
7 changes: 1 addition & 6 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,7 @@ private void OnVersionMessageReceived(VersionPayload payload)
break;
}
}
if (payload.Nonce == LocalNode.Nonce || payload.Magic != ProtocolSettings.Default.Magic)
{
Disconnect(true);
return;
}
if (LocalNode.Singleton.RemoteNodes.Values.Where(p => p != this).Any(p => p.Remote.Address.Equals(Remote.Address) && p.Version?.Nonce == payload.Nonce))
if (payload.Magic != ProtocolSettings.Default.Magic || !LocalNode.Singleton.AllowNewConnection(Self, this))
{
Disconnect(true);
return;
Expand Down
3 changes: 2 additions & 1 deletion tests/neo.UnitTests/Network/P2P/UT_RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.Network.P2P;
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;
using System.Net;

namespace Neo.UnitTests.Network.P2P
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public void RemoteNode_Test_Abort_DifferentMagic()
public void RemoteNode_Test_Accept_IfSameMagic()
{
var connectionTestProbe = CreateTestProbe();
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null));
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, new IPEndPoint(IPAddress.Parse("192.168.1.2"), 8080), new IPEndPoint(IPAddress.Parse("192.168.1.1"), 8080)));

var msg = Message.Create(MessageCommand.Version, new VersionPayload()
{
Expand Down