Răsfoiți Sursa

net/transport/tor: Introduce a static reusable TorClient for dialers

parazyd 2 ani în urmă
părinte
comite
f57d4a98e3
3 a modificat fișierele cu 19 adăugiri și 4 ștergeri
  1. 1 0
      Cargo.lock
  2. 3 2
      Cargo.toml
  3. 15 2
      src/net/transport/tor.rs

+ 1 - 0
Cargo.lock

@@ -1687,6 +1687,7 @@ dependencies = [
  "toml 0.8.8",
  "tor-error",
  "tor-hscrypto",
+ "tor-rtcompat",
  "url",
  "wasmer",
  "wasmer-compiler-singlepass",

+ 3 - 2
Cargo.toml

@@ -70,7 +70,8 @@ async-rustls = {version = "0.4.1", features = ["dangerous_configuration"], optio
 # Pluggable Transports
 socket2 = {version = "0.5.5", features = ["all"], optional = true}
 arti-client = {version = "0.10.2", default-features = false, features = ["async-std", "error_detail", "rustls", "onion-service-client"], optional = true}
-tor-error = {version = "0.5.4"}
+tor-error = {version = "0.5.4", optional = true}
+tor-rtcompat = {version = "0.9.5", features = ["async-std", "rustls"], optional = true}
 tor-hscrypto = {version = "0.3.4", optional = true}
 
 # TLS cert utilities
@@ -215,7 +216,7 @@ event-graph = [
 
 p2p-unix = []
 p2p-tcp = ["socket2"]
-p2p-tor = ["arti-client", "tor-hscrypto", "libsqlite3-sys"]
+p2p-tor = ["arti-client", "tor-hscrypto", "tor-error", "tor-rtcompat", "libsqlite3-sys"]
 p2p-nym = []
 
 net = [

+ 15 - 2
src/net/transport/tor.rs

@@ -20,9 +20,14 @@ use std::time::Duration;
 
 use arti_client::{config::BoolOrAuto, DataStream, StreamPrefs, TorClient};
 use log::debug;
+use smol::lock::OnceCell;
+use tor_rtcompat::PreferredRuntime;
 
 use crate::{system::timeout::timeout, Result};
 
+/// A static for `TorClient` reusability
+static TOR_CLIENT: OnceCell<TorClient<PreferredRuntime>> = OnceCell::new();
+
 /// Tor Dialer implementation
 #[derive(Debug, Clone)]
 pub struct TorDialer;
@@ -41,8 +46,16 @@ impl TorDialer {
         conn_timeout: Option<Duration>,
     ) -> Result<DataStream> {
         debug!(target: "net::tor::do_dial", "Dialing {}:{} with Tor...", host, port);
-        debug!(target: "net::tor::do_dial", "Bootstrapping...");
-        let client = TorClient::builder().create_bootstrapped().await?;
+
+        // Initialize or fetch the static TOR_CLIENT that should be reused in
+        // the Tor dialer
+        let client = TOR_CLIENT
+            .get_or_try_init(|| async {
+                debug!(target: "net::tor::do_dial", "Bootstrapping...");
+                TorClient::builder().create_bootstrapped().await
+            })
+            .await?;
+
         let mut stream_prefs = StreamPrefs::new();
         stream_prefs.connect_to_onion_services(BoolOrAuto::Explicit(true));