PR feedback integrated

This commit is contained in:
Philipp Hoenisch 2020-10-21 09:55:47 +11:00
parent e67e940768
commit e1d8a1c39b
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
2 changed files with 24 additions and 35 deletions

View File

@ -36,63 +36,54 @@ impl UnauthenticatedConnection {
} }
/// checks if tor is running /// checks if tor is running
async fn tor_running(&self) -> Result<()> { async fn assert_tor_running(&self) -> Result<()> {
// Make sure you are running tor and this is your socks port // Make sure you are running tor and this is your socks port
let proxy = reqwest::Proxy::all(format!("socks5h://{}", self.tor_proxy_address).as_str()) let proxy = reqwest::Proxy::all(format!("socks5h://{}", self.tor_proxy_address).as_str())
.expect("tor proxy should be there"); .map_err(|_| anyhow!("tor proxy should be there"))?;
let client = reqwest::Client::builder() let client = reqwest::Client::builder().proxy(proxy).build()?;
.proxy(proxy)
.build()
.expect("should be able to build reqwest client");
let res = client.get("https://check.torproject.org").send().await?; let res = client.get("https://check.torproject.org").send().await?;
let text = res.text().await?; let text = res.text().await?;
let is_tor = text.contains("Congratulations. This browser is configured to use Tor.");
if is_tor { if !text.contains("Congratulations. This browser is configured to use Tor.") {
Ok(())
} else {
bail!("Tor is currently not running") bail!("Tor is currently not running")
} }
Ok(())
} }
async fn init_unauthenticated_connection(&self) -> Result<UnauthenticatedConn<TcpStream>> { async fn init_unauthenticated_connection(&self) -> Result<UnauthenticatedConn<TcpStream>> {
// Connect to local tor service via control port // Connect to local tor service via control port
let sock = TcpStream::connect(self.tor_control_port_address).await?; let sock = TcpStream::connect(self.tor_control_port_address).await?;
let unauthenticated_connection = UnauthenticatedConn::new(sock); let uc = UnauthenticatedConn::new(sock);
Ok(unauthenticated_connection) Ok(uc)
} }
/// Create a new authenticated connection to your local Tor service /// Create a new authenticated connection to your local Tor service
pub async fn init_authenticated_connection(self) -> Result<AuthenticatedConnection> { pub async fn init_authenticated_connection(self) -> Result<AuthenticatedConnection> {
self.tor_running().await?; self.assert_tor_running().await?;
let mut unauthenticated_connection = match self.init_unauthenticated_connection().await { let mut uc = self
Err(_) => bail!("Tor instance not running"), .init_unauthenticated_connection()
Ok(unauthenticated_connection) => unauthenticated_connection, .await
}; .map_err(|_| anyhow!("Tor instance not running."))?;
let tor_info = uc
.load_protocol_info()
.await
.map_err(|_| anyhow!("Failed to load protocol info from Tor."))?;
let tor_info = match unauthenticated_connection.load_protocol_info().await {
Ok(info) => info,
Err(_) => bail!("Failed to load protocol info from Tor."),
};
let tor_auth_data = tor_info let tor_auth_data = tor_info
.make_auth_data()? .make_auth_data()?
.expect("Failed to make auth data."); .ok_or_else(|| anyhow!("Failed to make auth data."))?;
// Get an authenticated connection to the Tor via the Tor Controller protocol. // Get an authenticated connection to the Tor via the Tor Controller protocol.
if unauthenticated_connection uc.authenticate(&tor_auth_data)
.authenticate(&tor_auth_data)
.await .await
.is_err() .map_err(|_| anyhow!("Failed to authenticate with Tor"))?;
{
bail!("Failed to authenticate with Tor")
}
let authenticated_connection = unauthenticated_connection.into_authenticated().await;
Ok(AuthenticatedConnection { Ok(AuthenticatedConnection {
authenticated_connection, authenticated_connection: uc.into_authenticated().await,
}) })
} }
} }
@ -121,7 +112,6 @@ impl AuthenticatedConnection {
.iter(), .iter(),
) )
.await .await
.map_err(|_| anyhow!("Could not add onion service."))?; .map_err(|_| anyhow!("Could not add onion service."))
Ok(())
} }
} }

View File

@ -28,7 +28,6 @@ mod tor_test {
rx.await.ok(); rx.await.ok();
}); });
tokio::spawn(async { tokio::spawn(async {
// server.await.unwrap();
if let Err(e) = graceful.await { if let Err(e) = graceful.await {
eprintln!("server error: {}", e); eprintln!("server error: {}", e);
} }
@ -93,7 +92,7 @@ mod tor_test {
let proxy = reqwest::Proxy::all(format!("socks5h://127.0.0.1:{}", proxy_port).as_str()) let proxy = reqwest::Proxy::all(format!("socks5h://127.0.0.1:{}", proxy_port).as_str())
.expect("tor proxy should be there"); .expect("tor proxy should be there");
let client = reqwest::Client::builder().proxy(proxy).build().unwrap(); let client = reqwest::Client::builder().proxy(proxy).build()?;
let onion_address = tor_secret_key_v3.public().get_onion_address().to_string(); let onion_address = tor_secret_key_v3.public().get_onion_address().to_string();
let onion_url = format!("http://{}:8080", onion_address); let onion_url = format!("http://{}:8080", onion_address);