This repository was archived by the owner on Apr 13, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +4
-0
lines changed
src/main/scala/scorex/core/network/peer Expand file tree Collapse file tree 1 file changed +4
-0
lines changed Original file line number Diff line number Diff line change @@ -149,6 +149,7 @@ object PeerManager {
149
149
sc : ScorexContext ): Map [InetSocketAddress , PeerInfo ] = knownPeers
150
150
}
151
151
152
+ // Extract /16 IPv4 prefix (IP group)
152
153
private def getIpGroup (addr : InetSocketAddress ) : Int = {
153
154
val ip = addr.getAddress.getAddress
154
155
val group = ((ip(0 ) & 0xFF ) << 8 ) | (ip(1 ) & 0xFF )
@@ -160,12 +161,15 @@ object PeerManager {
160
161
override def choose (knownPeers : Map [InetSocketAddress , PeerInfo ],
161
162
blacklistedPeers : Seq [InetAddress ],
162
163
sc : ScorexContext ): Option [PeerInfo ] = {
164
+ // First of all try to establish connections to the hosts from different IP group
165
+ // It will complicate eclipse attacks
163
166
val excludedGroups = excludedPeers.flatMap(_.peerSpec.address).map(getIpGroup(_)).toSet
164
167
val allCandidates = knownPeers.values.filterNot { p =>
165
168
excludedPeers.exists(_.peerSpec.address == p.peerSpec.address) ||
166
169
blacklistedPeers.exists(addr => p.peerSpec.address.map(_.getAddress).contains(addr))
167
170
}.toSeq
168
171
val preferredCandidates = allCandidates.filterNot(_.peerSpec.address.fold(true )(addr => excludedGroups.contains(getIpGroup(addr))))
172
+ // If it is not possible to connect to the node from different IP group, then try to connect somewhere
169
173
val candidates = if (preferredCandidates.nonEmpty) preferredCandidates else allCandidates
170
174
if (candidates.nonEmpty) Some (candidates(Random .nextInt(candidates.size)))
171
175
else None
You can’t perform that action at this time.
0 commit comments