Просмотр исходного кода

net/hosts: use reference in block_all_ports()

skoupidi 2 лет назад
Родитель
Сommit
fa6a4be257
3 измененных файлов с 6 добавлено и 8 удалено
  1. 1 1
      src/net/acceptor.rs
  2. 1 3
      src/net/connector.rs
  3. 4 4
      src/net/hosts.rs

+ 1 - 1
src/net/acceptor.rs

@@ -145,7 +145,7 @@ impl Acceptor {
                 Ok((stream, url)) => {
                 Ok((stream, url)) => {
                     // Check if we reject this peer
                     // Check if we reject this peer
                     if hosts.container.contains(HostColor::Black as usize, &url) ||
                     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);
                         warn!(target: "net::acceptor::run_accept_loop()", "Peer {} is blacklisted", url);
                         continue
                         continue

+ 1 - 3
src/net/connector.rs

@@ -57,9 +57,7 @@ impl Connector {
     /// Establish an outbound connection
     /// Establish an outbound connection
     pub async fn connect(&self, url: &Url) -> Result<(Url, ChannelPtr)> {
     pub async fn connect(&self, url: &Url) -> Result<(Url, ChannelPtr)> {
         let hosts = self.session.upgrade().unwrap().p2p().hosts();
         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);
             warn!(target: "net::connector::connect", "Peer {} is blacklisted", url);
             return Err(Error::ConnectFailed)
             return Err(Error::ConnectFailed)
         }
         }

+ 4 - 4
src/net/hosts.rs

@@ -1136,7 +1136,7 @@ impl Hosts {
     /// hostname in the blacklist. This method will check if a host is
     /// 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
     /// stored in the blacklist without a port, and if so, it will return
     /// true.
     /// 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();
         let host = url.host().unwrap();
         self.container.hostlists[HostColor::Black as usize]
         self.container.hostlists[HostColor::Black as usize]
             .read()
             .read()
@@ -1184,7 +1184,7 @@ impl Hosts {
 
 
             // Blacklist peers should never enter the hostlist.
             // Blacklist peers should never enter the hostlist.
             if self.container.contains(HostColor::Black as usize, addr_) ||
             if self.container.contains(HostColor::Black as usize, addr_) ||
-                self.block_all_ports(addr_.clone())
+                self.block_all_ports(addr_)
             {
             {
                 warn!(
                 warn!(
                     target: "net::hosts::filter_addresses",
                     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, blacklist1.clone(), 0);
         hosts.container.store(HostColor::Black as usize, blacklist2.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]
     #[test]