diff --git a/client-demo/Cargo.toml b/client-demo/Cargo.toml index bab8bfb1f..026c1dd98 100644 --- a/client-demo/Cargo.toml +++ b/client-demo/Cargo.toml @@ -10,6 +10,8 @@ publish = false tor-netdir = { path="../tor-netdir", version= "*" } tor-netdoc = { path="../tor-netdoc", version= "*" } tor-proto = { path="../tor-proto", version= "*" } +tor-linkspec = { path="../tor-linkspec", version= "*" } +rand = "*" log = "*" async-std = "*" futures = "*" @@ -18,3 +20,4 @@ native-tls = "*" async-native-tls = "*" thiserror = "*" +simple-logging = "*" diff --git a/client-demo/src/err.rs b/client-demo/src/err.rs new file mode 100644 index 000000000..e95b73794 --- /dev/null +++ b/client-demo/src/err.rs @@ -0,0 +1,37 @@ +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Netdir: {0}")] + NetDir(#[source] tor_netdir::Error), + #[error("Protocol: {0}")] + Proto(#[source] tor_proto::Error), + #[error("Io: {0}")] + Io(#[source] std::io::Error), + #[error("Tls: {0}")] + Tls(#[source] native_tls::Error), + #[error("Misc: {0}")] + Misc(&'static str), +} + +impl From for Error { + fn from(e: tor_netdir::Error) -> Self { + Error::NetDir(e) + } +} +impl From for Error { + fn from(e: tor_proto::Error) -> Self { + Error::Proto(e) + } +} + +impl From for Error { + fn from(e: std::io::Error) -> Self { + Error::Io(e) + } +} +impl From for Error { + fn from(e: native_tls::Error) -> Self { + Error::Tls(e) + } +} + +pub type Result = std::result::Result; diff --git a/client-demo/src/main.rs b/client-demo/src/main.rs index ae126f083..617280421 100644 --- a/client-demo/src/main.rs +++ b/client-demo/src/main.rs @@ -1,25 +1,38 @@ +mod err; + +use err::{Error, Result}; + +use log::{info, LevelFilter}; use std::path::PathBuf; +use tor_linkspec::ChanTarget; -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("Netdir: {0}")] - NetDir(#[source] tor_netdir::Error), - #[error("Protocol: {0}")] - Proto(#[source] tor_proto::Error), -} +//use async_std::prelude::*; +use async_native_tls::TlsConnector; +use async_std::net; -impl From for Error { - fn from(e: tor_netdir::Error) -> Self { - Error::NetDir(e) - } -} -impl From for Error { - fn from(e: tor_proto::Error) -> Self { - Error::Proto(e) - } -} +use rand::thread_rng; -type Result = std::result::Result; +pub struct Channel {} + +async fn connect(target: &C) -> Result { + let addr = target + .get_addrs() + .get(0) + .ok_or(Error::Misc("No addresses for chosen relay‽"))?; + + let connector = TlsConnector::new() + .danger_accept_invalid_certs(true) + .danger_accept_invalid_hostnames(true); + + info!("Connecting to {}", addr); + let stream = net::TcpStream::connect(addr).await?; + + info!("Negotiating TLS with {}", addr); + let _tlscon = connector.connect("ignored", stream).await?; + info!("TLS negotiated."); + + Ok(Channel {}) +} fn get_netdir() -> Result { let mut pb: PathBuf = std::env::var_os("HOME").unwrap().into(); @@ -36,7 +49,17 @@ fn get_netdir() -> Result { } fn main() -> Result<()> { - let _dir = get_netdir()?; + simple_logging::log_to_stderr(LevelFilter::Info); - Ok(()) + let dir = get_netdir()?; + + let g = dir + .pick_relay(&mut thread_rng(), |_, u| u) + .ok_or(Error::Misc("no usable relays"))?; + + async_std::task::block_on(async { + let _con = connect(&g).await?; + + Ok(()) + }) }