Skip to content

Commit

Permalink
Merge pull request #2279 from rsksmart/jurajpiar/no_dns_for_find_node
Browse files Browse the repository at this point in the history
fix(discovery): makes pong send only IP without hostname
  • Loading branch information
Vovchyk authored Apr 18, 2024
2 parents 9405ac5 + 2bd1e0d commit 6fab6d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void handlePong(InetSocketAddress pongAddress, PongPeerMessage message)
this.pendingPingRequests.remove(message.getMessageId());
NodeChallenge challenge = this.challengeManager.removeChallenge(message.getMessageId());
if (challenge == null) {
this.addConnection(message, request.getAddress().getHostString(), request.getAddress().getPort());
this.addConnection(message, request.getAddress().getAddress().getHostAddress(), request.getAddress().getPort());
}
} else {
logger.debug("handlePong - Peer discovery request with id [{}] is either null or invalid", message.getMessageId());
Expand Down
28 changes: 24 additions & 4 deletions rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void handlePingMessageFromDifferentNetwork() throws Exception {

ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
String check = UUID.randomUUID().toString();
PingPeerMessage pingPeerMessageWithDifferentNetwork = PingPeerMessage.create(HOST_1, PORT_1, check, key1, this.NETWORK_ID2);
PingPeerMessage pingPeerMessageWithDifferentNetwork = PingPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID2);
DiscoveryEvent incomingPingEvent = new DiscoveryEvent(pingPeerMessageWithDifferentNetwork, new InetSocketAddress(HOST_1, PORT_1));

//A message is received
Expand Down Expand Up @@ -176,7 +176,7 @@ void handlePingMessage() throws Exception {

ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
String check = UUID.randomUUID().toString();
PingPeerMessage nodeMessage = PingPeerMessage.create(HOST_1, PORT_1, check, key1, this.NETWORK_ID1);
PingPeerMessage nodeMessage = PingPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID1);
DiscoveryEvent incomingPingEvent = new DiscoveryEvent(nodeMessage, new InetSocketAddress(HOST_2, PORT_3));

//A message is received
Expand Down Expand Up @@ -234,7 +234,7 @@ void handlePongMessage() throws Exception {

peerExplorer.start(false);

//A incoming pong for a Ping we did not sent.
//An incoming pong for a Ping we did not sent.
String check = UUID.randomUUID().toString();
PongPeerMessage incomingPongMessage = PongPeerMessage.create(HOST_1, PORT_1, check, key1, NETWORK_ID1);
DiscoveryEvent incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1));
Expand All @@ -257,13 +257,17 @@ void handlePongMessage() throws Exception {
addedNodes = peerExplorer.getNodes();
assertEquals(1, addedNodes.size());

//A incoming pong for a ping we sent but from a different address
//An incoming pong for a ping we sent but from a different address
incomingPongMessage = PongPeerMessage.create(HOST_4, PORT_4, ((PingPeerMessage) initialPingMessages.get(1).getMessage()).getMessageId(), key4, NETWORK_ID1);
incomingPongEvent = new DiscoveryEvent(incomingPongMessage, new InetSocketAddress(HOST_1, PORT_1));
channel.clearEvents();
channel.channelRead0(ctx, incomingPongEvent);
assertEquals(1, peerExplorer.getNodes().size());
assertEquals(1,peerExplorer.getKnownHosts().size());

// Verify that only IP is being sent, not hostname
assertNoHostnameFromDiscovery(peerExplorer);

peerExplorer.dispose();
}

Expand Down Expand Up @@ -426,6 +430,9 @@ void handleFindNodeMessage() throws Exception {
NeighborsPeerMessage neighborsPeerMessage = (NeighborsPeerMessage) sentEvents.get(0).getMessage();
assertEquals(1, neighborsPeerMessage.getNodes().size());

// Verify that only IP is being sent, not hostname
assertNoHostnameFromDiscovery(peerExplorer);

peerExplorer.dispose();
}

Expand Down Expand Up @@ -763,4 +770,17 @@ private static InetAddress parseAddress(String host) {
throw new IllegalArgumentException(e);
}
}

private static void assertNoHostnameFromDiscovery(PeerExplorer peerExplorer) {
ECKey key = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
Node localhostNode = new Node(key.getNodeId(), HOST_1, PORT_1); // more stable if loopback address overrides
String localhostIP = localhostNode.getAddress().getAddress().getHostAddress();
Optional<Node> discoveredNode = peerExplorer.getNodes()
.stream().filter((Node node) -> Objects.equals(
node.getAddress().getAddress().getHostAddress(),
localhostIP))
.findFirst();
assertTrue(discoveredNode.isPresent());
assertNotEquals(HOST_1, discoveredNode.get().getHost());
}
}

0 comments on commit 6fab6d4

Please sign in to comment.