2020-10-19 23:33:32 -04:00
|
|
|
#[cfg(feature = "tor")]
|
|
|
|
mod tor_test {
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
use anyhow::Result;
|
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
|
|
|
use reqwest::StatusCode;
|
|
|
|
use spectral::prelude::*;
|
|
|
|
use std::convert::Infallible;
|
|
|
|
use tokio::sync::oneshot::Receiver;
|
|
|
|
use torut::onion::TorSecretKeyV3;
|
|
|
|
use xmr_btc::tor::{AuthenticatedConnection, TOR_PROXY_ADDR};
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
async fn hello_world(
|
|
|
|
_req: hyper::Request<hyper::Body>,
|
|
|
|
) -> Result<hyper::Response<hyper::Body>, Infallible> {
|
|
|
|
Ok(hyper::Response::new("Hello World".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_test_service(port: u16, rx: Receiver<()>) {
|
|
|
|
let make_svc =
|
|
|
|
make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello_world)) });
|
|
|
|
let addr = ([127, 0, 0, 1], port).into();
|
|
|
|
let server = hyper::Server::bind(&addr).serve(make_svc);
|
|
|
|
let graceful = server.with_graceful_shutdown(async {
|
|
|
|
rx.await.ok();
|
|
|
|
});
|
|
|
|
tokio::spawn(async {
|
|
|
|
// server.await.unwrap();
|
|
|
|
if let Err(e) = graceful.await {
|
|
|
|
eprintln!("server error: {}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_tor_control_port() -> Result<()> {
|
|
|
|
// Setup test HTTP Server
|
|
|
|
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
|
|
|
|
let port = 8080;
|
|
|
|
start_test_service(port, rx);
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
// Connect to local Tor service
|
|
|
|
let mut authenticated_connection = AuthenticatedConnection::new().await?;
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
// Expose an onion service that re-directs to the echo server.
|
|
|
|
let tor_secret_key_v3 = TorSecretKeyV3::generate();
|
|
|
|
authenticated_connection
|
|
|
|
.add_service(port, &tor_secret_key_v3)
|
|
|
|
.await?;
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
// Test if Tor service forwards to HTTP Server
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
let proxy = reqwest::Proxy::all(format!("socks5h://{}", *TOR_PROXY_ADDR).as_str())
|
|
|
|
.expect("tor proxy should be there");
|
|
|
|
let client = reqwest::Client::builder().proxy(proxy).build().unwrap();
|
|
|
|
let onion_address = tor_secret_key_v3.public().get_onion_address().to_string();
|
|
|
|
let onion_url = format!("http://{}:8080", onion_address);
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
let res = client.get(&onion_url).send().await?;
|
|
|
|
assert_that(&res.status()).is_equal_to(StatusCode::OK);
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
let text = res.text().await?;
|
|
|
|
assert_that!(text).contains("Hello World");
|
2020-10-19 20:43:13 -04:00
|
|
|
|
2020-10-19 23:33:32 -04:00
|
|
|
// gracefully shut down server
|
|
|
|
let _ = tx.send(());
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-19 20:43:13 -04:00
|
|
|
}
|