Skip to content

Commit

Permalink
Merge pull request #2024 from ergoplatform/i1964-localOnly
Browse files Browse the repository at this point in the history
Added localOnly check to peer connections
  • Loading branch information
kushti committed Aug 24, 2023
2 parents 91c24c3 + f6a882d commit c9018f0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/main/scala/scorex/core/network/NetworkController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.warn(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")
}
Expand Down

0 comments on commit c9018f0

Please sign in to comment.