From 0bae1ce3366dc0f1e1dc1a8956df8775bcee5fb1 Mon Sep 17 00:00:00 2001 From: Astro Date: Tue, 6 Jun 2017 02:03:38 +0200 Subject: [PATCH] switch from rustls to native-tls --- Cargo.toml | 4 ++-- examples/echo_bot.rs | 16 +++------------- src/lib.rs | 4 ++-- src/starttls.rs | 36 ++++++++++++++++++++---------------- src/xmpp_stream.rs | 6 ++---- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5da50ccad8d58aff1c6af27d9a15c0a591d791e7..82c0ea9b74c7de518789bbd72e4c590ed4e02169 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ tokio-core = "*" tokio-io = "*" bytes = "*" RustyXML = "*" -rustls = "*" -tokio-rustls = "*" +native-tls = "*" +tokio-tls = "*" sasl = "*" rustc-serialize = "*" diff --git a/examples/echo_bot.rs b/examples/echo_bot.rs index 416e98d599646043772105717b7a045a6da8e8c8..9dfe1556b86690402aeeca1d0201a659b36c575d 100644 --- a/examples/echo_bot.rs +++ b/examples/echo_bot.rs @@ -1,16 +1,11 @@ extern crate futures; extern crate tokio_core; extern crate tokio_xmpp; -extern crate rustls; -use std::sync::Arc; -use std::io::BufReader; -use std::fs::File; use tokio_core::reactor::Core; use futures::{Future, Stream}; use tokio_xmpp::TcpClient; use tokio_xmpp::xmpp_codec::Packet; -use rustls::ClientConfig; fn main() { use std::net::ToSocketAddrs; @@ -18,23 +13,18 @@ fn main() { .to_socket_addrs().unwrap() .next().unwrap(); - let mut config = ClientConfig::new(); - let mut certfile = BufReader::new(File::open("/usr/share/ca-certificates/CAcert/root.crt").unwrap()); - config.root_store.add_pem_file(&mut certfile).unwrap(); - let arc_config = Arc::new(config); - let mut core = Core::new().unwrap(); let client = TcpClient::connect( &addr, &core.handle() + ).map_err(|e| format!("{}", e) ).and_then(|stream| { if stream.can_starttls() { - stream.starttls(arc_config) + stream.starttls() } else { panic!("No STARTTLS") } - }).map_err(|e| format!("{}", e) - ).and_then(|stream| { + }).and_then(|stream| { stream.auth("astrobot", "").expect("auth") }).and_then(|stream| { stream.for_each(|event| { diff --git a/src/lib.rs b/src/lib.rs index ceb6842433ab5645986f1f34b65208e3d22aceea..6a402c366b22db62ceab465256310623dda657ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,8 +4,8 @@ extern crate tokio_core; extern crate tokio_io; extern crate bytes; extern crate xml; -extern crate rustls; -extern crate tokio_rustls; +extern crate native_tls; +extern crate tokio_tls; extern crate sasl; extern crate rustc_serialize as serialize; diff --git a/src/starttls.rs b/src/starttls.rs index b0fdc92f16c6807d8159e4e8baac7e92d8310719..c893f0597bdd45c4d667366021f7f29fd9e926a9 100644 --- a/src/starttls.rs +++ b/src/starttls.rs @@ -1,12 +1,10 @@ use std::mem::replace; -use std::io::Error; -use std::sync::Arc; use futures::{Future, Sink, Poll, Async}; use futures::stream::Stream; use futures::sink; use tokio_io::{AsyncRead, AsyncWrite}; -use rustls::*; -use tokio_rustls::*; +use tokio_tls::*; +use native_tls::TlsConnector; use xml; use xmpp_codec::*; @@ -18,7 +16,7 @@ pub const NS_XMPP_TLS: &str = "urn:ietf:params:xml:ns:xmpp-tls"; pub struct StartTlsClient { state: StartTlsClientState, - arc_config: Arc, + domain: String, } enum StartTlsClientState { @@ -26,12 +24,16 @@ enum StartTlsClientState { SendStartTls(sink::Send>), AwaitProceed(XMPPStream), StartingTls(ConnectAsync), - Start(StreamStart>), + Start(StreamStart>), } impl StartTlsClient { /// Waits for - pub fn from_stream(xmpp_stream: XMPPStream, arc_config: Arc) -> Self { + pub fn from_stream(xmpp_stream: XMPPStream) -> Self { + let domain = xmpp_stream.stream_attrs.get("from") + .map(|s| s.to_owned()) + .unwrap_or_else(|| String::new()); + let nonza = xml::Element::new( "starttls".to_owned(), Some(NS_XMPP_TLS.to_owned()), vec![] @@ -42,14 +44,14 @@ impl StartTlsClient { StartTlsClient { state: StartTlsClientState::SendStartTls(send), - arc_config: arc_config, + domain, } } } impl Future for StartTlsClient { - type Item = XMPPStream>; - type Error = Error; + type Item = XMPPStream>; + type Error = String; fn poll(&mut self) -> Poll { let old_state = replace(&mut self.state, StartTlsClientState::Invalid); @@ -67,7 +69,7 @@ impl Future for StartTlsClient { Ok(Async::NotReady) => (StartTlsClientState::SendStartTls(send), Ok(Async::NotReady)), Err(e) => - (StartTlsClientState::SendStartTls(send), Err(e)), + (StartTlsClientState::SendStartTls(send), Err(format!("{}", e))), }, StartTlsClientState::AwaitProceed(mut xmpp_stream) => match xmpp_stream.poll() { @@ -76,7 +78,9 @@ impl Future for StartTlsClient { { println!("* proceed *"); let stream = xmpp_stream.into_inner(); - let connect = self.arc_config.connect_async("spaceboyz.net", stream); + let connect = TlsConnector::builder().unwrap() + .build().unwrap() + .connect_async(&self.domain, stream); let new_state = StartTlsClientState::StartingTls(connect); retry = true; (new_state, Ok(Async::NotReady)) @@ -88,13 +92,13 @@ impl Future for StartTlsClient { Ok(_) => (StartTlsClientState::AwaitProceed(xmpp_stream), Ok(Async::NotReady)), Err(e) => - (StartTlsClientState::AwaitProceed(xmpp_stream), Err(e)), + (StartTlsClientState::AwaitProceed(xmpp_stream), Err(format!("{}", e))), }, StartTlsClientState::StartingTls(mut connect) => match connect.poll() { Ok(Async::Ready(tls_stream)) => { println!("Got a TLS stream!"); - let start = XMPPStream::from_stream(tls_stream, "spaceboyz.net".to_owned()); + let start = XMPPStream::from_stream(tls_stream, self.domain.clone()); let new_state = StartTlsClientState::Start(start); retry = true; (new_state, Ok(Async::NotReady)) @@ -102,7 +106,7 @@ impl Future for StartTlsClient { Ok(Async::NotReady) => (StartTlsClientState::StartingTls(connect), Ok(Async::NotReady)), Err(e) => - (StartTlsClientState::StartingTls(connect), Err(e)), + (StartTlsClientState::StartingTls(connect), Err(format!("{}", e))), }, StartTlsClientState::Start(mut start) => match start.poll() { @@ -111,7 +115,7 @@ impl Future for StartTlsClient { Ok(Async::NotReady) => (StartTlsClientState::Start(start), Ok(Async::NotReady)), Err(e) => - (StartTlsClientState::Invalid, Err(e)), + (StartTlsClientState::Invalid, Err(format!("{}", e))), }, StartTlsClientState::Invalid => unreachable!(), diff --git a/src/xmpp_stream.rs b/src/xmpp_stream.rs index 56c778c4777f2744add2f36cf972e3fa3b3ead72..a8793db1f59f045dfb201643519c1c28bf5c347d 100644 --- a/src/xmpp_stream.rs +++ b/src/xmpp_stream.rs @@ -1,10 +1,8 @@ use std::default::Default; -use std::sync::Arc; use std::collections::HashMap; use futures::*; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::codec::Framed; -use rustls::ClientConfig; use xml; use sasl::common::Credentials; @@ -44,8 +42,8 @@ impl XMPPStream { .is_some() } - pub fn starttls(self, arc_config: Arc) -> StartTlsClient { - StartTlsClient::from_stream(self, arc_config) + pub fn starttls(self) -> StartTlsClient { + StartTlsClient::from_stream(self) } pub fn auth(self, username: &str, password: &str) -> Result, String> {