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

net: allow control of schemes in the blacklist imported from Settings

draoi 2 лет назад
Родитель
Сommit
8ae9e42e71
2 измененных файлов с 28 добавлено и 15 удалено
  1. 18 11
      src/net/hosts.rs
  2. 10 4
      src/net/settings.rs

+ 18 - 11
src/net/hosts.rs

@@ -1086,17 +1086,24 @@ impl Hosts {
 
     /// Import blacklisted peers specified in the config file.
     pub(in crate::net) async fn import_blacklist(&self) -> Result<()> {
-        for (mut host, ports) in self.settings.read().await.blacklist.clone() {
-            // If the ports are empty, simply store the host_str. We will use this to
-            // blacklist all ports of a given peer in `block_all_ports()`.
-            if ports.is_empty() {
-                self.container.store(HostColor::Black as usize, host.clone(), 0);
-            }
-            // Otherwise, store all the specified ports.
-            else {
-                for port in ports {
-                    host.set_port(Some(port))?;
-                    self.container.store(HostColor::Black as usize, host.clone(), 0);
+        for (hostname, schemes, ports) in self.settings.read().await.blacklist.clone() {
+            // If schemes are not set use default tcp+tls.
+            let schemes = if schemes.is_empty() { vec!["tcp+tls".to_string()] } else { schemes };
+
+            // If ports are not set block all ports.
+            let ports = if ports.is_empty() { vec![0] } else { ports };
+
+            for scheme in schemes {
+                for &port in &ports {
+                    let url_string = if port == 0 {
+                        format!("{}://{}", scheme, hostname)
+                    } else {
+                        format!("{}://{}:{}", scheme, hostname, port)
+                    };
+
+                    if let Ok(url) = Url::parse(&url_string) {
+                        self.container.store(HostColor::Black as usize, url, 0);
+                    }
                 }
             }
         }

+ 10 - 4
src/net/settings.rs

@@ -19,6 +19,8 @@
 use structopt::StructOpt;
 use url::Url;
 
+type BlacklistEntry = (String, Vec<String>, Vec<u16>);
+
 /// P2P network settings. The scope of this is a P2P network instance
 /// configured by the library user.
 #[derive(Debug, Clone)]
@@ -77,8 +79,10 @@ pub struct Settings {
     /// process is paused.
     pub time_with_no_connections: u64,
     /// Nodes to avoid interacting with for the duration of the program,
-    /// in the format ["scheme://host", [port, port]]
-    pub blacklist: Vec<(Url, Vec<u16>)>,
+    /// in the format ["host", ["scheme", "scheme"], [port, port]]
+    /// If scheme is left empty it will default to "tcp+tls".
+    /// If ports are left empty all ports from this peer will be blocked.
+    pub blacklist: Vec<BlacklistEntry>,
 }
 
 impl Default for Settings {
@@ -227,10 +231,12 @@ pub struct SettingsOpt {
     pub time_with_no_connections: Option<u64>,
 
     /// Nodes to avoid interacting with for the duration of the program,
-    /// in the format ["scheme://host", [port, port]]
+    /// in the format ["host", ["scheme", "scheme"], [port, port]]
+    /// If scheme is left empty it will default to "tcp+tls".
+    /// If ports are left empty all ports from this peer will be blocked.
     #[serde(default)]
     #[structopt(skip)]
-    pub blacklist: Vec<(Url, Vec<u16>)>,
+    pub blacklist: Vec<BlacklistEntry>,
 }
 
 impl From<SettingsOpt> for Settings {