Преглед изворни кода

net: Use forked Url crate that supports our transport schemes

Now IP addresses are properly parsed
parazyd пре 2 година
родитељ
комит
2f97f384e7
4 измењених фајлова са 51 додато и 52 уклоњено
  1. 33 21
      Cargo.lock
  2. 4 0
      Cargo.toml
  3. 10 11
      src/net/channel.rs
  4. 4 20
      src/net/hosts.rs

+ 33 - 21
Cargo.lock

@@ -1430,7 +1430,7 @@ dependencies = [
  "base64 0.13.1",
  "hkdf 0.10.0",
  "hmac 0.10.1",
- "percent-encoding",
+ "percent-encoding 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.8.5",
  "sha2 0.9.9",
  "time 0.2.27",
@@ -1983,6 +1983,7 @@ dependencies = [
  "rand 0.8.5",
  "randomx",
  "rcgen",
+ "regex",
  "rusqlite",
  "rustls-pemfile",
  "semver 1.0.23",
@@ -2265,12 +2266,12 @@ dependencies = [
 
 [[package]]
 name = "darling"
-version = "0.20.9"
+version = "0.20.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1"
+checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
 dependencies = [
- "darling_core 0.20.9",
- "darling_macro 0.20.9",
+ "darling_core 0.20.10",
+ "darling_macro 0.20.10",
 ]
 
 [[package]]
@@ -2303,9 +2304,9 @@ dependencies = [
 
 [[package]]
 name = "darling_core"
-version = "0.20.9"
+version = "0.20.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "622687fe0bac72a04e5599029151f5796111b90f1baaa9b544d807a5e31cd120"
+checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
 dependencies = [
  "fnv",
  "ident_case",
@@ -2339,11 +2340,11 @@ dependencies = [
 
 [[package]]
 name = "darling_macro"
-version = "0.20.9"
+version = "0.20.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178"
+checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
 dependencies = [
- "darling_core 0.20.9",
+ "darling_core 0.20.10",
  "quote",
  "syn 2.0.70",
 ]
@@ -2937,7 +2938,7 @@ version = "0.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e08b6c6ab82d70f08844964ba10c7babb716de2ecaeab9be5717918a5177d3af"
 dependencies = [
- "darling 0.20.9",
+ "darling 0.20.10",
  "proc-macro2",
  "quote",
  "syn 2.0.70",
@@ -3240,7 +3241,15 @@ version = "1.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
 dependencies = [
- "percent-encoding",
+ "percent-encoding 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "form_urlencoded"
+version = "1.2.1"
+source = "git+https://github.com/darkrenaissance/rust-url?branch=main#61b5bb1b8c92d362d8fdd8b717555e1f363eca1e"
+dependencies = [
+ "percent-encoding 2.3.1 (git+https://github.com/darkrenaissance/rust-url?branch=main)",
 ]
 
 [[package]]
@@ -3923,8 +3932,7 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
 [[package]]
 name = "idna"
 version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
+source = "git+https://github.com/darkrenaissance/rust-url?branch=main#61b5bb1b8c92d362d8fdd8b717555e1f363eca1e"
 dependencies = [
  "unicode-bidi",
  "unicode-normalization",
@@ -4967,6 +4975,11 @@ version = "2.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
 
+[[package]]
+name = "percent-encoding"
+version = "2.3.1"
+source = "git+https://github.com/darkrenaissance/rust-url?branch=main#61b5bb1b8c92d362d8fdd8b717555e1f363eca1e"
+
 [[package]]
 name = "phf"
 version = "0.11.2"
@@ -6235,7 +6248,7 @@ version = "0.8.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "c7715380eec75f029a4ef7de39a9200e0a63823176b759d055b613f5a87df6a6"
 dependencies = [
- "percent-encoding",
+ "percent-encoding 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde",
  "thiserror",
 ]
@@ -6255,7 +6268,7 @@ version = "0.7.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
 dependencies = [
- "form_urlencoded",
+ "form_urlencoded 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "itoa",
  "ryu",
  "serde",
@@ -6285,7 +6298,7 @@ version = "3.8.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b80d3d6b56b64335c0180e5ffde23b3c5e08c14c585b51a15bd0e95393f46703"
 dependencies = [
- "darling 0.20.9",
+ "darling 0.20.10",
  "proc-macro2",
  "quote",
  "syn 2.0.70",
@@ -8455,12 +8468,11 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
 [[package]]
 name = "url"
 version = "2.5.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c"
+source = "git+https://github.com/darkrenaissance/rust-url?branch=main#61b5bb1b8c92d362d8fdd8b717555e1f363eca1e"
 dependencies = [
- "form_urlencoded",
+ "form_urlencoded 1.2.1 (git+https://github.com/darkrenaissance/rust-url?branch=main)",
  "idna",
- "percent-encoding",
+ "percent-encoding 2.3.1 (git+https://github.com/darkrenaissance/rust-url?branch=main)",
  "serde",
 ]
 

+ 4 - 0
Cargo.toml

@@ -102,6 +102,7 @@ url = {version = "2.5.2", features = ["serde"], optional = true}
 
 # Misc
 simplelog = {version = "0.12.2", optional = true}
+regex = {version = "1.10.5", optional = true}
 
 # Crypto
 rand = {version = "0.8.5", optional = true}
@@ -242,6 +243,7 @@ net = [
     "futures-rustls",
     "rand",
     "rcgen",
+    "regex",
     "rustls-pemfile",
     "semver",
     "smol",
@@ -332,6 +334,8 @@ halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"}
 halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v4"}
 # Temp smol-2.0 fix
 async-lock = {git="https://github.com/smol-rs/async-lock", rev="542831132f2c707aae1c380edd43452053433814"}
+# Forked "url" crate with added P2P schemas
+url = {git="https://github.com/darkrenaissance/rust-url", branch="main"}
 
 [[bench]]
 name = "zk_arith"

+ 10 - 11
src/net/channel.rs

@@ -406,21 +406,20 @@ impl Channel {
 
                 // An inbound Tor connection can't really be banned :)
                 #[cfg(feature = "p2p-tor")]
-                if peer.scheme() == "tor" && self.p2p().hosts().is_local_host(peer) {
+                if (peer.scheme() == "tor" || peer.scheme() == "tor+tls") &&
+                    self.p2p().hosts().is_local_host(peer)
+                {
                     return
                 }
 
-                // We do this hack in order to parse IPs properly.
-                // https://github.com/whatwg/url/issues/749
-                let addr = Url::parse(&format!("http://{}", peer.host_str().unwrap())).unwrap();
-
-                match Url::parse(&addr.as_str().replace("http", peer.scheme())) {
-                    Ok(v) => v,
-                    Err(e) => {
-                        error!("[P2P] ban() failed to parse {:?}: {}", peer, e);
-                        return
-                    }
+                #[cfg(feature = "p2p-unix")]
+                if peer.scheme() == "unix" {
+                    return
                 }
+
+                let mut addr = peer.clone();
+                addr.set_port(None).unwrap();
+                addr
             } else {
                 peer.clone()
             }

+ 4 - 20
src/net/hosts.rs

@@ -1042,12 +1042,8 @@ impl Hosts {
             return false
         }
 
-        // We do this hack in order to parse IPs properly.
-        // https://github.com/whatwg/url/issues/749
-        let addr = Url::parse(&url.as_str().replace(url.scheme(), "http")).unwrap();
-
         // Filter private IP ranges
-        match addr.host().unwrap() {
+        match url.host().unwrap() {
             url::Host::Ipv4(ip) => {
                 if !ip.is_global() {
                     return true
@@ -1074,11 +1070,7 @@ impl Hosts {
             return false
         }
 
-        // We do this hack in order to parse IPs properly.
-        // https://github.com/whatwg/url/issues/749
-        let addr = Url::parse(&url.as_str().replace(url.scheme(), "http")).unwrap();
-
-        if let url::Host::Ipv6(_) = addr.host().unwrap() {
+        if let url::Host::Ipv6(_) = url.host().unwrap() {
             return true
         }
         false
@@ -1133,11 +1125,7 @@ impl Hosts {
 
         'addr_loop: for (addr_, last_seen) in addrs {
             // Validate that the format is `scheme://host_str:port`
-            if addr_.host_str().is_none() ||
-                addr_.port().is_none() ||
-                addr_.cannot_be_a_base() ||
-                addr_.path_segments().is_some()
-            {
+            if addr_.host_str().is_none() || addr_.port().is_none() || addr_.cannot_be_a_base() {
                 debug!(
                     target: "net::hosts::filter_addresses",
                     "[{}] has invalid addr format. Skipping", addr_,
@@ -1191,14 +1179,10 @@ impl Hosts {
                 }
             }
 
-            // We do this hack in order to parse IPs properly.
-            // https://github.com/whatwg/url/issues/749
-            let addr = Url::parse(&addr_.as_str().replace(addr_.scheme(), "http")).unwrap();
-
             // Filter non-global ranges if we're not allowing localnet.
             // Should never be allowed in production, so we don't really care
             // about some of them (e.g. 0.0.0.0, or broadcast, etc.).
-            if !settings.localnet && self.is_local_host(&addr) {
+            if !settings.localnet && self.is_local_host(addr_) {
                 debug!(
                     target: "net::hosts::filter_addresses",
                     "[{}] Filtering non-global ranges", addr_,