From 0ad773fc943ea66038998873676b0a2debf5e236 Mon Sep 17 00:00:00 2001 From: darkfi Date: Sun, 9 Feb 2025 13:55:12 +0100 Subject: [PATCH] net/hosts: looksmax move_hosts() All code must be beautiful --- src/net/hosts.rs | 105 ++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/src/net/hosts.rs b/src/net/hosts.rs index 105e297d2..6f2c2e3d0 100644 --- a/src/net/hosts.rs +++ b/src/net/hosts.rs @@ -1426,70 +1426,61 @@ impl Hosts { debug!(target: "net::hosts::move_host()", "Trying to move addr={} destination={:?}", addr, destination); - match self.try_register(addr.clone(), HostState::Move) { - Ok(new_state) => { - debug!(target: "net::hosts::move_host()", "Moving addr={} destination={:?}, state={:?}", - addr, destination, new_state); + // If we cannot register this address as move, this will simply return here. + self.try_register(addr.clone(), HostState::Move)?; - match destination { - // Downgrade to grey. Remove from white and gold. - HostColor::Grey => { - self.container.remove_if_exists(HostColor::Gold, addr); - self.container.remove_if_exists(HostColor::White, addr); + debug!(target: "net::hosts::move_host()", "Moving addr={} destination={:?}", + addr.clone(), destination); - self.container.store_or_update(HostColor::Grey, addr.clone(), last_seen); - self.container.sort_by_last_seen(HostColor::Grey as usize); - self.container.resize(HostColor::Grey); + match destination { + // Downgrade to grey. Remove from white and gold. + HostColor::Grey => { + self.container.remove_if_exists(HostColor::Gold, addr); + self.container.remove_if_exists(HostColor::White, addr); + + self.container.store_or_update(HostColor::Grey, addr.clone(), last_seen); + self.container.sort_by_last_seen(HostColor::Grey as usize); + self.container.resize(HostColor::Grey); + } + + // Remove from Greylist, add to Whitelist. Called by the Refinery. + HostColor::White => { + self.container.remove_if_exists(HostColor::Grey, addr); + + self.container.store_or_update(HostColor::White, addr.clone(), last_seen); + self.container.sort_by_last_seen(HostColor::White as usize); + self.container.resize(HostColor::White); + } + + // Upgrade to gold. Remove from white or grey. + HostColor::Gold => { + self.container.remove_if_exists(HostColor::Grey, addr); + self.container.remove_if_exists(HostColor::White, addr); + + self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen); + self.container.sort_by_last_seen(HostColor::Gold as usize); + } + + // Move to black. Remove from all other lists. + HostColor::Black => { + // We ignore UNIX sockets here so we will just work + // with stuff that has host_str(). + if addr.host_str().is_some() { + // Localhost connections should never enter the blacklist + // This however allows any Tor and Nym connections. + if self.is_local_host(addr) { + return Ok(()); } - // Remove from Greylist, add to Whitelist. Called by the Refinery. - HostColor::White => { - self.container.remove_if_exists(HostColor::Grey, addr); + self.container.remove_if_exists(HostColor::Grey, addr); + self.container.remove_if_exists(HostColor::White, addr); + self.container.remove_if_exists(HostColor::Gold, addr); - self.container.store_or_update(HostColor::White, addr.clone(), last_seen); - self.container.sort_by_last_seen(HostColor::White as usize); - self.container.resize(HostColor::White); - } - - // Upgrade to gold. Remove from white or grey. - HostColor::Gold => { - self.container.remove_if_exists(HostColor::Grey, addr); - self.container.remove_if_exists(HostColor::White, addr); - - self.container.store_or_update(HostColor::Gold, addr.clone(), last_seen); - self.container.sort_by_last_seen(HostColor::Gold as usize); - } - - // Move to black. Remove from all other lists. - HostColor::Black => { - // We ignore UNIX sockets here so we will just work - // with stuff that has host_str(). - if addr.host_str().is_some() { - // Localhost connections should never enter the blacklist - // This however allows any Tor and Nym connections. - if self.is_local_host(addr) { - return Ok(()); - } - - self.container.remove_if_exists(HostColor::Grey, addr); - self.container.remove_if_exists(HostColor::White, addr); - self.container.remove_if_exists(HostColor::Gold, addr); - - self.container.store_or_update( - HostColor::Black, - addr.clone(), - last_seen, - ); - } - } - - HostColor::Dark => return Err(Error::InvalidHostColor), + self.container.store_or_update(HostColor::Black, addr.clone(), last_seen); } } - Err(e) => { - warn!(target: "net::hosts::move_host", "Cannot move host={:?}, err={:?}", - addr.clone(), e); - } + + HostColor::Dark => return Err(Error::InvalidHostColor), } Ok(())