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
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
let proxy = reqwest::Proxy::all(format!("socks5h://{}", self.tor_proxy_address).as_str())
.expect("tor proxy should be there");
let client = reqwest::Client::builder()
.proxy(proxy)
.build()
.expect("should be able to build reqwest client");
.map_err(|_| anyhow!("tor proxy should be there"))?;
let client = reqwest::Client::builder().proxy(proxy).build()?;
let res = client.get("https://check.torproject.org").send().await?;
let text = res.text().await?;
let is_tor = text.contains("Congratulations. This browser is configured to use Tor.");
if is_tor {
Ok(())
} else {
if !text.contains("Congratulations. This browser is configured to use Tor.") {
bail!("Tor is currently not running")
}
Ok(())
}
async fn init_unauthenticated_connection(&self) -> Result<UnauthenticatedConn<TcpStream>> {
// Connect to local tor service via control port
let sock = TcpStream::connect(self.tor_control_port_address).await?;
let unauthenticated_connection = UnauthenticatedConn::new(sock);
Ok(unauthenticated_connection)
let uc = UnauthenticatedConn::new(sock);
Ok(uc)
}
/// Create a new authenticated connection to your local Tor service
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 {
Err(_) => bail!("Tor instance not running"),
Ok(unauthenticated_connection) => unauthenticated_connection,
};
let mut uc = self
.init_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
.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.
if unauthenticated_connection
.authenticate(&tor_auth_data)
uc.authenticate(&tor_auth_data)
.await
.is_err()
{
bail!("Failed to authenticate with Tor")
}
let authenticated_connection = unauthenticated_connection.into_authenticated().await;
.map_err(|_| anyhow!("Failed to authenticate with Tor"))?;
Ok(AuthenticatedConnection {
authenticated_connection,
authenticated_connection: uc.into_authenticated().await,
})
}
}
@ -121,7 +112,6 @@ impl AuthenticatedConnection {
.iter(),
)
.await
.map_err(|_| anyhow!("Could not add onion service."))?;
Ok(())
.map_err(|_| anyhow!("Could not add onion service."))
}
}

View File

@ -28,7 +28,6 @@ mod tor_test {
rx.await.ok();
});
tokio::spawn(async {
// server.await.unwrap();
if let Err(e) = graceful.await {
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())
.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_url = format!("http://{}:8080", onion_address);