Skip to content

Commit

Permalink
Add tor-launch-service feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Nov 29, 2024
1 parent 126f041 commit 6e4b805
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 20 additions & 5 deletions src/native/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TorClient<PreferredRuntime>> = 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<SpawnError>),
#[cfg(feature = "tor-launch-service")]
Spawn(SpawnError),
}

impl std::error::Error for Error {}
Expand All @@ -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}"),
}
}
Expand All @@ -63,21 +76,24 @@ impl From<ConfigBuildError> for Error {
}
}

#[cfg(feature = "tor-launch-service")]
impl From<ProxyConfigError> for Error {
fn from(e: ProxyConfigError) -> Self {
Self::ProxyConfig(e)
}
}

#[cfg(feature = "tor-launch-service")]
impl From<InvalidNickname> for Error {
fn from(e: InvalidNickname) -> Self {
Self::InvalidNickname(e)
}
}

#[cfg(feature = "tor-launch-service")]
impl From<SpawnError> for Error {
fn from(e: SpawnError) -> Self {
Self::Spawn(Arc::new(e))
Self::Spawn(e)
}
}

Expand Down Expand Up @@ -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<PreferredRuntime>, Error> {
Expand All @@ -119,7 +134,6 @@ async fn get_tor_client<'a>(
.await
}

#[inline]
pub(super) async fn connect(
domain: &str,
port: u16,
Expand All @@ -130,6 +144,7 @@ pub(super) async fn connect(
}

/// Launch onion service and forward requests from `hiddenservice.onion:<port>` to [`SocketAddr`].
#[cfg(feature = "tor-launch-service")]
pub async fn launch_onion_service<S>(
nickname: S,
addr: SocketAddr,
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//! Prelude
#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(ambiguous_glob_reexports)]
#![doc(hidden)]
Expand Down

0 comments on commit 6e4b805

Please # to comment.