-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Limited number of connections per IP #2046
base: master
Are you sure you want to change the base?
Conversation
@@ -90,6 +90,10 @@ pub struct P2PArgs { | |||
#[clap(long = "max-peers-connected", default_value = "50", env)] | |||
pub max_peers_connected: u32, | |||
|
|||
/// Max number of unique peers connected with the same remote IP address. | |||
#[clap(long = "max-peers-per-remote-ip", default_value = "5", env)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[clap(long = "max-peers-per-remote-ip", default_value = "5", env)] | |
#[clap(long = "max-peers-per-remote-ip", default_value = "50", env)] |
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, 5=)
@@ -203,6 +206,7 @@ impl Config<NotInitialized> { | |||
bootstrap_nodes: vec![], | |||
enable_mdns: false, | |||
max_peers_connected: 50, | |||
max_peers_per_remote_ip: 50, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
max_peers_per_remote_ip: 50, | |
max_peers_per_remote_ip: 5, |
The config default here doesn't match the default on clap
) -> Result<THandler<Self>, ConnectionDenied> { | ||
if let Some(ip) = get_ip_addr(remote_addr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that get_ip_addr
could return two different IP for an IPv4 and his IPv6 mapped or libp2p prevents IPv4-mapped in IPv6 ?
If it's the first case then they will fall in two separate entries and in this case we might want get_ip_addr
to canonicalise the IP (https://doc.rust-lang.org/std/net/enum.IpAddr.html#method.to_canonical)
@@ -71,15 +84,17 @@ pub struct Behaviour { | |||
connected_reserved_nodes: HashSet<PeerId>, | |||
pending_connections: HashSet<ConnectionId>, | |||
pending_events: VecDeque<ToSwarm<PeerReportEvent, Void>>, | |||
ips_to_peers: HashMap<IpAddr, HashSet<PeerId>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seem that this is currently used only to track a number of connection per ip. If we don't want this to be usable for others use cases in the future we could use
HashMap<IpAddr, usize>
It will simplify the current usage that we have by avoiding having to do .len()
to measure it.
let entry = self.ips_to_peers.entry(ip); | ||
|
||
match entry { | ||
Entry::Occupied(mut occupied) => { | ||
let set = occupied.get_mut(); | ||
|
||
if set.len() >= self.max_connections_per_ip { | ||
return Err(ConnectionDenied::new( | ||
"Max connections per IP reached", | ||
)) | ||
} | ||
|
||
set.insert(peer); | ||
} | ||
Entry::Vacant(_) => { | ||
// Do nothing | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let entry = self.ips_to_peers.entry(ip); | |
match entry { | |
Entry::Occupied(mut occupied) => { | |
let set = occupied.get_mut(); | |
if set.len() >= self.max_connections_per_ip { | |
return Err(ConnectionDenied::new( | |
"Max connections per IP reached", | |
)) | |
} | |
set.insert(peer); | |
} | |
Entry::Vacant(_) => { | |
// Do nothing | |
} | |
} | |
if let Some(peers) = self.ips_to_peers.get_mut(&ip) { | |
if peers.len() >= self.max_connections_per_ip { | |
return Err(ConnectionDenied::new( | |
"Max connections per IP reached", | |
)) | |
} | |
peers.insert(peer); | |
} |
let ip = get_ip_addr(send_back_addr); | ||
|
||
if let Some(ip) = ip { | ||
let entry = self.ips_to_peers.entry(ip); | ||
|
||
match entry { | ||
Entry::Occupied(mut occupied) => { | ||
let set = occupied.get_mut(); | ||
set.remove(&peer_id); | ||
|
||
if set.is_empty() { | ||
occupied.remove(); | ||
} | ||
} | ||
Entry::Vacant(_) => { | ||
tracing::warn!( | ||
"IP address not found in the \ | ||
map during disconnection of the peer {:?}", | ||
peer_id | ||
) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let ip = get_ip_addr(send_back_addr); | |
if let Some(ip) = ip { | |
let entry = self.ips_to_peers.entry(ip); | |
match entry { | |
Entry::Occupied(mut occupied) => { | |
let set = occupied.get_mut(); | |
set.remove(&peer_id); | |
if set.is_empty() { | |
occupied.remove(); | |
} | |
} | |
Entry::Vacant(_) => { | |
tracing::warn!( | |
"IP address not found in the \ | |
map during disconnection of the peer {:?}", | |
peer_id | |
) | |
} | |
} | |
} | |
if let Some(ip) = get_ip_addr(send_back_addr) { | |
if let Some(peers) = self.ips_to_peers.get_mut(&ip) { | |
peers.remove(&peer_id); | |
if peers.is_empty() { | |
self.ips_to_peers.remove(&ip); | |
} | |
} else { | |
tracing::warn!( | |
"IP address not found in the \ | |
map during disconnection of the peer {:?}", | |
peer_id | |
) | |
} | |
} |
It is possible to create many connections from the same subnetwork and occupy connection slots. This change should limit the number of connections allowed from the same remote IP.
Checklist
Before requesting review