net/hosts: use reference in block_all_ports()

This commit is contained in:
skoupidi
2024-07-22 19:34:51 +03:00
parent 151aa186c4
commit fa6a4be257
3 changed files with 6 additions and 8 deletions

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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]