From b86927b4329fb76763c55d94c80e54dc5bd4e4aa Mon Sep 17 00:00:00 2001 From: jellymlg Date: Tue, 15 Aug 2023 20:15:36 +0200 Subject: [PATCH 1/2] Added local check to peer connection --- .../core/network/NetworkController.scala | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/scala/scorex/core/network/NetworkController.scala b/src/main/scala/scorex/core/network/NetworkController.scala index 6eda4d3150..781f90f942 100644 --- a/src/main/scala/scorex/core/network/NetworkController.scala +++ b/src/main/scala/scorex/core/network/NetworkController.scala @@ -310,6 +310,19 @@ class NetworkController(ergoSettings: ErgoSettings, } } + /** + * Check if a given IPv4 or IPv6 address is local. + * @param remote - address to check + * @return true if the address is local, false otherwise + */ + private def checkLocalOnly(remote: InetSocketAddress): Boolean = + if(!networkSettings.localOnly) { // not only accept local + val address = remote.getAddress + address.isSiteLocalAddress || address.isLinkLocalAddress + } else { + false + } + /** * Connect to peer * @@ -320,13 +333,17 @@ class NetworkController(ergoSettings: ErgoSettings, getPeerAddress(peer) match { case Some(remote) => if (connectionForPeerAddress(remote).isEmpty && !unconfirmedConnections.contains(remote)) { - unconfirmedConnections += remote - tcpManager ! Connect( - remoteAddress = remote, - options = Nil, - timeout = Some(networkSettings.connectionTimeout), - pullMode = true - ) + if(checkLocalOnly(remote)) { + log.error(s"Prevented attempt to connect to local peer $remote. (scorex.network.localOnly is false)") + } else { + unconfirmedConnections += remote + tcpManager ! Connect( + remoteAddress = remote, + options = Nil, + timeout = Some(networkSettings.connectionTimeout), + pullMode = true + ) + } } else { log.warn(s"Connection to peer $remote is already established") } From f6a882dd1b0339916f6677f6a5a1fbf850e0a38d Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Thu, 24 Aug 2023 15:42:01 +0300 Subject: [PATCH 2/2] warn instead of err --- src/main/scala/scorex/core/network/NetworkController.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/scala/scorex/core/network/NetworkController.scala b/src/main/scala/scorex/core/network/NetworkController.scala index 781f90f942..2ac5376e1c 100644 --- a/src/main/scala/scorex/core/network/NetworkController.scala +++ b/src/main/scala/scorex/core/network/NetworkController.scala @@ -333,8 +333,8 @@ class NetworkController(ergoSettings: ErgoSettings, getPeerAddress(peer) match { case Some(remote) => if (connectionForPeerAddress(remote).isEmpty && !unconfirmedConnections.contains(remote)) { - if(checkLocalOnly(remote)) { - log.error(s"Prevented attempt to connect to local peer $remote. (scorex.network.localOnly is false)") + if (checkLocalOnly(remote)) { + log.warn(s"Prevented attempt to connect to local peer $remote. (scorex.network.localOnly is false)") } else { unconfirmedConnections += remote tcpManager ! Connect(