Skip to content

Commit 6e4b805

Browse files
committed
Add tor-launch-service feature
1 parent 126f041 commit 6e4b805

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ keywords = ["async", "tokio", "wasm", "websocket"]
1414
[features]
1515
default = []
1616
socks = ["dep:tokio-socks"]
17-
tor = ["dep:arti-client", "dep:tor-hsservice", "dep:tor-hsrproxy", "dep:tor-rtcompat"]
17+
tor = ["tokio/sync", "dep:arti-client", "dep:tor-rtcompat"]
18+
tor-launch-service = ["tor", "arti-client/onion-service-service", "dep:tor-hsservice", "dep:tor-hsrproxy"]
1819

1920
[dependencies]
2021
futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] }
2122
url = { version = "2.5", default-features = false }
2223

2324
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
24-
tokio = { version = "1", features = ["net", "sync", "time"] }
25+
tokio = { version = "1", features = ["net", "time"] }
2526
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } # Required to enable the necessary features for tokio-tungstenite
2627
tokio-socks = { version = "0.5", optional = true }
2728
tokio-tungstenite = { version = "0.24", features = ["rustls-tls-webpki-roots"] }
2829

2930
# TOR deps
30-
arti-client = { version = "0.22", default-features = false, features = ["onion-service-client", "onion-service-service", "rustls", "static-sqlite", "tokio"], optional = true }
31+
arti-client = { version = "0.22", default-features = false, features = ["onion-service-client", "rustls", "static-sqlite", "tokio"], optional = true }
3132
tor-hsservice = { version = "0.22", default-features = false, optional = true }
3233
tor-hsrproxy = { version = "0.22", default-features = false, optional = true }
3334
tor-rtcompat = { version = "0.22", default-features = false, features = ["rustls", "tokio"], optional = true }

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ async fn main() {
3838

3939
The following crate feature flags are available:
4040

41-
| Feature | Default | Description |
42-
|---------|:-------:|------------------------------------|
43-
| `socks` | No | Enable `socks` proxy support |
44-
| `tor` | No | Enable embedded tor client support |
41+
| Feature | Default | Description |
42+
|-----------------------|:-------:|-------------------------------------------------------------------------|
43+
| `socks` | No | Enable `socks` proxy support |
44+
| `tor` | No | Enable embedded tor client support |
45+
| `tor-launch-service ` | No | Enable embedded tor client with support to launch hidden onion services |
4546

4647
## Minimum Supported Rust Version (MSRV)
4748

src/native/tor.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,47 @@
44
//! Tor
55
66
use std::fmt;
7+
#[cfg(feature = "tor-launch-service")]
78
use std::net::SocketAddr;
89
use std::path::PathBuf;
10+
#[cfg(feature = "tor-launch-service")]
911
use std::sync::Arc;
1012

13+
#[cfg(feature = "tor-launch-service")]
1114
use arti_client::config::onion_service::OnionServiceConfigBuilder;
1215
use arti_client::config::{CfgPath, ConfigBuildError, TorClientConfigBuilder};
1316
use arti_client::{DataStream, TorClient, TorClientConfig};
17+
#[cfg(feature = "tor-launch-service")]
1418
use futures_util::task::{SpawnError, SpawnExt};
1519
use tokio::sync::OnceCell;
20+
#[cfg(feature = "tor-launch-service")]
1621
use tor_hsrproxy::config::{
1722
Encapsulation, ProxyAction, ProxyConfigBuilder, ProxyConfigError, ProxyPattern, ProxyRule,
1823
TargetAddr,
1924
};
25+
#[cfg(feature = "tor-launch-service")]
2026
use tor_hsrproxy::OnionServiceReverseProxy;
27+
#[cfg(feature = "tor-launch-service")]
2128
use tor_hsservice::{HsNickname, InvalidNickname, OnionServiceConfig, RunningOnionService};
2229
use tor_rtcompat::PreferredRuntime;
2330

2431
static TOR_CLIENT: OnceCell<TorClient<PreferredRuntime>> = OnceCell::const_new();
2532

26-
#[derive(Debug, Clone)]
33+
#[derive(Debug)]
2734
pub enum Error {
2835
/// Arti Client error
2936
ArtiClient(arti_client::Error),
3037
/// Config builder error
3138
ConfigBuilder(ConfigBuildError),
3239
/// Proxy config error
40+
#[cfg(feature = "tor-launch-service")]
3341
ProxyConfig(ProxyConfigError),
3442
/// Invalid nickname
43+
#[cfg(feature = "tor-launch-service")]
3544
InvalidNickname(InvalidNickname),
3645
/// Spawn error
37-
Spawn(Arc<SpawnError>),
46+
#[cfg(feature = "tor-launch-service")]
47+
Spawn(SpawnError),
3848
}
3949

4050
impl std::error::Error for Error {}
@@ -44,8 +54,11 @@ impl fmt::Display for Error {
4454
match self {
4555
Self::ArtiClient(e) => write!(f, "{e}"),
4656
Self::ConfigBuilder(e) => write!(f, "{e}"),
57+
#[cfg(feature = "tor-launch-service")]
4758
Self::ProxyConfig(e) => write!(f, "{e}"),
59+
#[cfg(feature = "tor-launch-service")]
4860
Self::InvalidNickname(e) => write!(f, "{e}"),
61+
#[cfg(feature = "tor-launch-service")]
4962
Self::Spawn(e) => write!(f, "{e}"),
5063
}
5164
}
@@ -63,21 +76,24 @@ impl From<ConfigBuildError> for Error {
6376
}
6477
}
6578

79+
#[cfg(feature = "tor-launch-service")]
6680
impl From<ProxyConfigError> for Error {
6781
fn from(e: ProxyConfigError) -> Self {
6882
Self::ProxyConfig(e)
6983
}
7084
}
7185

86+
#[cfg(feature = "tor-launch-service")]
7287
impl From<InvalidNickname> for Error {
7388
fn from(e: InvalidNickname) -> Self {
7489
Self::InvalidNickname(e)
7590
}
7691
}
7792

93+
#[cfg(feature = "tor-launch-service")]
7894
impl From<SpawnError> for Error {
7995
fn from(e: SpawnError) -> Self {
80-
Self::Spawn(Arc::new(e))
96+
Self::Spawn(e)
8197
}
8298
}
8399

@@ -110,7 +126,6 @@ async fn init_tor_client(
110126
}
111127

112128
/// Get or init tor client
113-
#[inline]
114129
async fn get_tor_client<'a>(
115130
custom_path: Option<&PathBuf>,
116131
) -> Result<&'a TorClient<PreferredRuntime>, Error> {
@@ -119,7 +134,6 @@ async fn get_tor_client<'a>(
119134
.await
120135
}
121136

122-
#[inline]
123137
pub(super) async fn connect(
124138
domain: &str,
125139
port: u16,
@@ -130,6 +144,7 @@ pub(super) async fn connect(
130144
}
131145

132146
/// Launch onion service and forward requests from `hiddenservice.onion:<port>` to [`SocketAddr`].
147+
#[cfg(feature = "tor-launch-service")]
133148
pub async fn launch_onion_service<S>(
134149
nickname: S,
135150
addr: SocketAddr,

src/prelude.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Prelude
55
6+
#![allow(unused_imports)]
67
#![allow(unknown_lints)]
78
#![allow(ambiguous_glob_reexports)]
89
#![doc(hidden)]

0 commit comments

Comments
 (0)