From fa6a4be25762173485cf1c75c8cc6e27d818ee8f Mon Sep 17 00:00:00 2001 From: skoupidi Date: Mon, 22 Jul 2024 19:34:51 +0300 Subject: [PATCH] net/hosts: use reference in block_all_ports() --- src/net/acceptor.rs | 2 +- src/net/connector.rs | 4 +--- src/net/hosts.rs | 8 ++++---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/net/acceptor.rs b/src/net/acceptor.rs index c8317b545..fa184e92b 100644 --- a/src/net/acceptor.rs +++ b/src/net/acceptor.rs @@ -145,7 +145,7 @@ impl Acceptor { Ok((stream, url)) => { // Check if we reject this peer if hosts.container.contains(HostColor::Black as usize, &url) || - hosts.block_all_ports(url.clone()) + hosts.block_all_ports(&url) { warn!(target: "net::acceptor::run_accept_loop()", "Peer {} is blacklisted", url); continue diff --git a/src/net/connector.rs b/src/net/connector.rs index 2e6887805..511a38ebd 100644 --- a/src/net/connector.rs +++ b/src/net/connector.rs @@ -57,9 +57,7 @@ impl Connector { /// Establish an outbound connection pub async fn connect(&self, url: &Url) -> Result<(Url, ChannelPtr)> { let hosts = self.session.upgrade().unwrap().p2p().hosts(); - if hosts.container.contains(HostColor::Black as usize, url) || - hosts.block_all_ports(url.host_str().unwrap().to_string()) - { + if hosts.container.contains(HostColor::Black as usize, url) || hosts.block_all_ports(url) { warn!(target: "net::connector::connect", "Peer {} is blacklisted", url); return Err(Error::ConnectFailed) } diff --git a/src/net/hosts.rs b/src/net/hosts.rs index 6a4d7978c..5b44d47f6 100644 --- a/src/net/hosts.rs +++ b/src/net/hosts.rs @@ -1136,7 +1136,7 @@ impl Hosts { /// hostname in the blacklist. This method will check if a host is /// stored in the blacklist without a port, and if so, it will return /// true. - pub(in crate::net) fn block_all_ports(&self, url: Url) -> bool { + pub(in crate::net) fn block_all_ports(&self, url: &Url) -> bool { let host = url.host().unwrap(); self.container.hostlists[HostColor::Black as usize] .read() @@ -1184,7 +1184,7 @@ impl Hosts { // Blacklist peers should never enter the hostlist. if self.container.contains(HostColor::Black as usize, addr_) || - self.block_all_ports(addr_.clone()) + self.block_all_ports(addr_) { warn!( target: "net::hosts::filter_addresses", @@ -1482,8 +1482,8 @@ mod tests { hosts.container.store(HostColor::Black as usize, blacklist1.clone(), 0); hosts.container.store(HostColor::Black as usize, blacklist2.clone(), 0); - assert!(hosts.block_all_ports(blacklist2)); - assert!(!hosts.block_all_ports(blacklist1)); + assert!(hosts.block_all_ports(&blacklist2)); + assert!(!hosts.block_all_ports(&blacklist1)); } #[test]