From 6e4b805ed4b79a155be23e208ec4959c52597073 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 29 Nov 2024 16:01:06 +0100 Subject: [PATCH] Add `tor-launch-service` feature --- Cargo.toml | 7 ++++--- README.md | 9 +++++---- src/native/tor.rs | 25 ++++++++++++++++++++----- src/prelude.rs | 1 + 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 822f793..2cbdc90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,20 +14,21 @@ keywords = ["async", "tokio", "wasm", "websocket"] [features] default = [] socks = ["dep:tokio-socks"] -tor = ["dep:arti-client", "dep:tor-hsservice", "dep:tor-hsrproxy", "dep:tor-rtcompat"] +tor = ["tokio/sync", "dep:arti-client", "dep:tor-rtcompat"] +tor-launch-service = ["tor", "arti-client/onion-service-service", "dep:tor-hsservice", "dep:tor-hsrproxy"] [dependencies] futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] } url = { version = "2.5", default-features = false } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -tokio = { version = "1", features = ["net", "sync", "time"] } +tokio = { version = "1", features = ["net", "time"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } # Required to enable the necessary features for tokio-tungstenite tokio-socks = { version = "0.5", optional = true } tokio-tungstenite = { version = "0.24", features = ["rustls-tls-webpki-roots"] } # TOR deps -arti-client = { version = "0.22", default-features = false, features = ["onion-service-client", "onion-service-service", "rustls", "static-sqlite", "tokio"], optional = true } +arti-client = { version = "0.22", default-features = false, features = ["onion-service-client", "rustls", "static-sqlite", "tokio"], optional = true } tor-hsservice = { version = "0.22", default-features = false, optional = true } tor-hsrproxy = { version = "0.22", default-features = false, optional = true } tor-rtcompat = { version = "0.22", default-features = false, features = ["rustls", "tokio"], optional = true } diff --git a/README.md b/README.md index e086501..c89fbfd 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,11 @@ async fn main() { The following crate feature flags are available: -| Feature | Default | Description | -|---------|:-------:|------------------------------------| -| `socks` | No | Enable `socks` proxy support | -| `tor` | No | Enable embedded tor client support | +| Feature | Default | Description | +|-----------------------|:-------:|-------------------------------------------------------------------------| +| `socks` | No | Enable `socks` proxy support | +| `tor` | No | Enable embedded tor client support | +| `tor-launch-service ` | No | Enable embedded tor client with support to launch hidden onion services | ## Minimum Supported Rust Version (MSRV) diff --git a/src/native/tor.rs b/src/native/tor.rs index ebec871..4ba43c1 100644 --- a/src/native/tor.rs +++ b/src/native/tor.rs @@ -4,37 +4,47 @@ //! Tor use std::fmt; +#[cfg(feature = "tor-launch-service")] use std::net::SocketAddr; use std::path::PathBuf; +#[cfg(feature = "tor-launch-service")] use std::sync::Arc; +#[cfg(feature = "tor-launch-service")] use arti_client::config::onion_service::OnionServiceConfigBuilder; use arti_client::config::{CfgPath, ConfigBuildError, TorClientConfigBuilder}; use arti_client::{DataStream, TorClient, TorClientConfig}; +#[cfg(feature = "tor-launch-service")] use futures_util::task::{SpawnError, SpawnExt}; use tokio::sync::OnceCell; +#[cfg(feature = "tor-launch-service")] use tor_hsrproxy::config::{ Encapsulation, ProxyAction, ProxyConfigBuilder, ProxyConfigError, ProxyPattern, ProxyRule, TargetAddr, }; +#[cfg(feature = "tor-launch-service")] use tor_hsrproxy::OnionServiceReverseProxy; +#[cfg(feature = "tor-launch-service")] use tor_hsservice::{HsNickname, InvalidNickname, OnionServiceConfig, RunningOnionService}; use tor_rtcompat::PreferredRuntime; static TOR_CLIENT: OnceCell> = OnceCell::const_new(); -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum Error { /// Arti Client error ArtiClient(arti_client::Error), /// Config builder error ConfigBuilder(ConfigBuildError), /// Proxy config error + #[cfg(feature = "tor-launch-service")] ProxyConfig(ProxyConfigError), /// Invalid nickname + #[cfg(feature = "tor-launch-service")] InvalidNickname(InvalidNickname), /// Spawn error - Spawn(Arc), + #[cfg(feature = "tor-launch-service")] + Spawn(SpawnError), } impl std::error::Error for Error {} @@ -44,8 +54,11 @@ impl fmt::Display for Error { match self { Self::ArtiClient(e) => write!(f, "{e}"), Self::ConfigBuilder(e) => write!(f, "{e}"), + #[cfg(feature = "tor-launch-service")] Self::ProxyConfig(e) => write!(f, "{e}"), + #[cfg(feature = "tor-launch-service")] Self::InvalidNickname(e) => write!(f, "{e}"), + #[cfg(feature = "tor-launch-service")] Self::Spawn(e) => write!(f, "{e}"), } } @@ -63,21 +76,24 @@ impl From for Error { } } +#[cfg(feature = "tor-launch-service")] impl From for Error { fn from(e: ProxyConfigError) -> Self { Self::ProxyConfig(e) } } +#[cfg(feature = "tor-launch-service")] impl From for Error { fn from(e: InvalidNickname) -> Self { Self::InvalidNickname(e) } } +#[cfg(feature = "tor-launch-service")] impl From for Error { fn from(e: SpawnError) -> Self { - Self::Spawn(Arc::new(e)) + Self::Spawn(e) } } @@ -110,7 +126,6 @@ async fn init_tor_client( } /// Get or init tor client -#[inline] async fn get_tor_client<'a>( custom_path: Option<&PathBuf>, ) -> Result<&'a TorClient, Error> { @@ -119,7 +134,6 @@ async fn get_tor_client<'a>( .await } -#[inline] pub(super) async fn connect( domain: &str, port: u16, @@ -130,6 +144,7 @@ pub(super) async fn connect( } /// Launch onion service and forward requests from `hiddenservice.onion:` to [`SocketAddr`]. +#[cfg(feature = "tor-launch-service")] pub async fn launch_onion_service( nickname: S, addr: SocketAddr, diff --git a/src/prelude.rs b/src/prelude.rs index 1eabafc..145fad3 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,6 +3,7 @@ //! Prelude +#![allow(unused_imports)] #![allow(unknown_lints)] #![allow(ambiguous_glob_reexports)] #![doc(hidden)]