mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Filter non global ips from discovery (#3023)
## Issue Addressed #3006 ## Proposed Changes This PR changes the default behaviour of lighthouse to ignore discovered IPs that are not globally routable. It adds a CLI flag, --enable-local-discovery to permit the non-global IPs in discovery. NOTE: We should take care in merging this as I will break current set-ups that rely on local IP discovery. I made this the non-default behaviour because we dont really want to be wasting resources attempting to connect to non-routable addresses and we dont want to propagate these to others (on the chance we can connect to one of these local nodes), improving discoveries efficiency.
This commit is contained in:
@@ -176,6 +176,7 @@ impl Default for Config {
|
||||
.filter_rate_limiter(filter_rate_limiter)
|
||||
.filter_max_bans_per_ip(Some(5))
|
||||
.filter_max_nodes_per_ip(Some(10))
|
||||
.table_filter(|enr| enr.ip().map_or(false, |ip| is_global(&ip))) // Filter non-global IPs
|
||||
.ban_duration(Some(Duration::from_secs(3600)))
|
||||
.ping_interval(Duration::from_secs(300))
|
||||
.build();
|
||||
@@ -347,3 +348,28 @@ pub fn gossipsub_config(network_load: u8, fork_context: Arc<ForkContext>) -> Gos
|
||||
.build()
|
||||
.expect("valid gossipsub configuration")
|
||||
}
|
||||
|
||||
/// Helper function to determine if the IpAddr is a global address or not. The `is_global()`
|
||||
/// function is not yet stable on IpAddr.
|
||||
#[allow(clippy::nonminimal_bool)]
|
||||
fn is_global(addr: &std::net::Ipv4Addr) -> bool {
|
||||
// check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
|
||||
// globally routable addresses in the 192.0.0.0/24 range.
|
||||
if u32::from_be_bytes(addr.octets()) == 0xc0000009
|
||||
|| u32::from_be_bytes(addr.octets()) == 0xc000000a
|
||||
{
|
||||
return true;
|
||||
}
|
||||
!addr.is_private()
|
||||
&& !addr.is_loopback()
|
||||
&& !addr.is_link_local()
|
||||
&& !addr.is_broadcast()
|
||||
&& !addr.is_documentation()
|
||||
// shared
|
||||
&& !(addr.octets()[0] == 100 && (addr.octets()[1] & 0b1100_0000 == 0b0100_0000)) &&!(addr.octets()[0] & 240 == 240 && !addr.is_broadcast())
|
||||
// addresses reserved for future protocols (`192.0.0.0/24`)
|
||||
// reserved
|
||||
&& !(addr.octets()[0] == 192 && addr.octets()[1] == 0 && addr.octets()[2] == 0)
|
||||
// Make sure the address is not in 0.0.0.0/8
|
||||
&& addr.octets()[0] != 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user