1use anyhow::Result;
2use http_body_util::{BodyExt, Empty};
3use hyper::body::Bytes;
4use hyper::http::uri::Scheme;
5use hyper::{Request, Uri};
6use hyper_util::rt::TokioIo;
7use tokio::io::{AsyncRead, AsyncWrite};
89use arti_client::{TorClient, TorClientConfig};
1011const TEST_URL: &str = "https://check.torproject.org/api/ip";
1213#[tokio::main]
14async fn main() -> Result<()> {
15// Arti uses the `tracing` crate for logging. Install a handler for this, to print Arti's logs.
16 // (You'll need to set RUST_LOG=info as an environment variable to actually see much; also try
17 // =debug for more detailed logging.)
18tracing_subscriber::fmt::init();
1920// You can run this example with any arbitrary HTTP/1.1 (raw or within TLS) URL, but we'll default to check.torproject.org
21 // because it's a good way of demonstrating that the connection is via Tor.
22let url: Uri = std::env::args()
23 .nth(1)
24 .unwrap_or_else(|| TEST_URL.into())
25 .parse()?;
26let host = url.host().unwrap();
27let https = url.scheme() == Some(&Scheme::HTTPS);
2829// The client config includes things like where to store persistent Tor network state.
30 // The defaults provided are the same as the Arti standalone application, and save data
31 // to a conventional place depending on operating system (for example, ~/.local/share/arti
32 // on Linux platforms)
33let config = TorClientConfig::default();
3435// We now let the Arti client start and bootstrap a connection to the network.
36 // (This takes a while to gather the necessary consensus state, etc.)
37let tor_client = TorClient::create_bootstrapped(config).await?;
3839let port = match url.port_u16() {
40Some(port) => port,
41_ if https => 443,
42_ => 80,
43 };
4445let stream = tor_client.connect((host, port)).await?;
4647// Following part is just standard usage of Hyper.
48eprintln!("[+] Making request to: {}", url);
4950if https {
51let cx = tokio_native_tls::native_tls::TlsConnector::builder().build()?;
52let cx = tokio_native_tls::TlsConnector::from(cx);
53let stream = cx.connect(host, stream).await?;
54 make_request(host, stream).await
55} else {
56 make_request(host, stream).await
57}
58}
5960async fn make_request(
61 host: &str,
62 stream: impl AsyncRead + AsyncWrite + Unpin + Send + 'static,
63) -> Result<()> {
64let (mut request_sender, connection) =
65 hyper::client::conn::http1::handshake(TokioIo::new(stream)).await?;
6667// Spawn a task to poll the connection and drive the HTTP state.
68tokio::spawn(async move {
69 connection.await.unwrap();
70 });
7172let mut resp = request_sender
73 .send_request(
74 Request::builder()
75 .header("Host", host)
76 .uri(TEST_URL)
77 .method("GET")
78 .body(Empty::<Bytes>::new())?,
79 )
80 .await?;
8182eprintln!("[+] Response status: {}", resp.status());
83while let Some(frame) = resp.body_mut().frame().await {
84let bytes = frame?.into_data().unwrap();
85eprintln!("[+] Response body:\n\n{}", std::str::from_utf8(&bytes)?);
86 }
8788Ok(())
89}