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

Added localOnly check to peer connections #2024

Merged
merged 3 commits into from
Aug 24, 2023
Merged
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
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