Skip to content

Commit f3360c0

Browse files
committed
feat(tor): add new AsyncTorClient
- feat(tor): add new async client, `AsyncTorClient`, which uses `arti-client` to establish Tor connections, and `hyper` as HTTP client over custom Tor anonymized data stream. - feat(tor): implements the common methods: `get_response`, `get_response_json`, `get_response_hex` and their `opt` versions too. feat(tor): wip wip 2
1 parent ac64e04 commit f3360c0

File tree

5 files changed

+587
-2
lines changed

5 files changed

+587
-2
lines changed

Cargo.toml

+36-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ hex = { version = "0.2", package = "hex-conservative" }
2323
log = "^0.4"
2424
minreq = { version = "2.11.0", features = ["json-using-serde"], optional = true }
2525
reqwest = { version = "0.11", features = ["json"], default-features = false, optional = true }
26+
arti-client = { version = "0.21.0", default-features = false, optional = true }
27+
tor-rtcompat = { version = "0.21.0", default-features = false, optional = true }
28+
hyper = { version = "1.4.1", features = ["client", "http1"], default-features = false, optional = true }
29+
hyper-util = { version = "0.1.7", features = ["tokio"], default-features = false, optional = true }
30+
tokio = { version = "1.38.1", optional = true }
31+
http-body-util = { version = "0.1.2", optional = true}
32+
http = { version = "1.1.0", optional = true }
33+
serde_json = { version = "1.0.127"}
34+
35+
tokio-rustls = { version = "0.26.0", default-features = false, features = [
36+
"logging",
37+
"tls12",
38+
"ring",
39+
] }
40+
webpki-roots = { version = "0.26.3" }
41+
rustls-pki-types = { version = "1.8.0" }
2642

2743
[dev-dependencies]
2844
serde_json = "1.0"
@@ -31,14 +47,33 @@ electrsd = { version = "0.28.0", features = ["legacy", "esplora_a33e97e1", "bitc
3147
lazy_static = "1.4.0"
3248

3349
[features]
34-
default = ["blocking", "async", "async-https"]
50+
default = ["blocking", "async", "async-https", "async-tor"]
3551
blocking = ["minreq", "minreq/proxy"]
3652
blocking-https = ["blocking", "minreq/https"]
3753
blocking-https-rustls = ["blocking", "minreq/https-rustls"]
3854
blocking-https-native = ["blocking", "minreq/https-native"]
3955
blocking-https-bundled = ["blocking", "minreq/https-bundled"]
56+
4057
async = ["reqwest", "reqwest/socks"]
4158
async-https = ["async", "reqwest/default-tls"]
4259
async-https-native = ["async", "reqwest/native-tls"]
4360
async-https-rustls = ["async", "reqwest/rustls-tls"]
4461
async-https-rustls-manual-roots = ["async", "reqwest/rustls-tls-manual-roots"]
62+
63+
async-tor = [
64+
"dep:arti-client",
65+
"arti-client/tokio",
66+
"arti-client/onion-service-client",
67+
"arti-client/native-tls",
68+
69+
"dep:tor-rtcompat",
70+
"tor-rtcompat/tokio",
71+
72+
"dep:hyper",
73+
"dep:hyper-util",
74+
"dep:tokio",
75+
"dep:http-body-util",
76+
"dep:http"
77+
]
78+
async-tor-https-native = ["async-tor", "arti-client/native-tls"]
79+
async-tor-https-rustls = ["async-tor", "arti-client/rustls"]

examples/tor.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::str::FromStr;
2+
3+
use bitcoin::Txid;
4+
use esplora_client::{r#async_tor::AsyncTorClient, Builder};
5+
6+
extern crate esplora_client;
7+
8+
#[tokio::main]
9+
async fn main() {
10+
// let client = Client::new("tcp://electrum.blockstream.info:50001").unwrap();
11+
// let res = client.server_features();
12+
// println!("{:#?}", res);
13+
14+
let builder = Builder::new("https://mempool.space/api");
15+
let client = AsyncTorClient::from_builder(builder).await.unwrap();
16+
17+
// let client = AsyncClient::from_builder(builder).unwrap();
18+
19+
let tx_id =
20+
Txid::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b").unwrap();
21+
let tx = client.get_tx(&tx_id).await.unwrap().unwrap();
22+
23+
println!("{:?}", tx);
24+
}

src/async.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
//! Esplora by way of `reqwest` HTTP client.
1313
14+
use core::str;
1415
use std::collections::HashMap;
1516
use std::str::FromStr;
1617

@@ -84,7 +85,9 @@ impl AsyncClient {
8485
/// [`bitcoin::consensus::Decodable`] deserialization.
8586
async fn get_response<T: Decodable>(&self, path: &str) -> Result<T, Error> {
8687
let url = format!("{}{}", self.url, path);
87-
let response = self.client.get(url).send().await?;
88+
let request = self.client.get(url);
89+
println!("{:?}", request);
90+
let response = request.send().await?;
8891

8992
match response.status().is_success() {
9093
true => Ok(deserialize::<T>(&response.bytes().await?)?),

0 commit comments

Comments
 (0)