switch from rustls to native-tls

This commit is contained in:
Astro 2017-06-06 02:03:38 +02:00
commit 0bae1ce336
5 changed files with 29 additions and 37 deletions

View file

@ -9,7 +9,7 @@ tokio-core = "*"
tokio-io = "*"
bytes = "*"
RustyXML = "*"
rustls = "*"
tokio-rustls = "*"
native-tls = "*"
tokio-tls = "*"
sasl = "*"
rustc-serialize = "*"

View file

@ -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| {

View file

@ -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;

View file

@ -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<S: AsyncRead + AsyncWrite> {
state: StartTlsClientState<S>,
arc_config: Arc<ClientConfig>,
domain: String,
}
enum StartTlsClientState<S: AsyncRead + AsyncWrite> {
@ -26,12 +24,16 @@ enum StartTlsClientState<S: AsyncRead + AsyncWrite> {
SendStartTls(sink::Send<XMPPStream<S>>),
AwaitProceed(XMPPStream<S>),
StartingTls(ConnectAsync<S>),
Start(StreamStart<TlsStream<S, ClientSession>>),
Start(StreamStart<TlsStream<S>>),
}
impl<S: AsyncRead + AsyncWrite> StartTlsClient<S> {
/// Waits for <stream:features>
pub fn from_stream(xmpp_stream: XMPPStream<S>, arc_config: Arc<ClientConfig>) -> Self {
pub fn from_stream(xmpp_stream: XMPPStream<S>) -> 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<S: AsyncRead + AsyncWrite> StartTlsClient<S> {
StartTlsClient {
state: StartTlsClientState::SendStartTls(send),
arc_config: arc_config,
domain,
}
}
}
impl<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
type Item = XMPPStream<TlsStream<S, ClientSession>>;
type Error = Error;
type Item = XMPPStream<TlsStream<S>>;
type Error = String;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let old_state = replace(&mut self.state, StartTlsClientState::Invalid);
@ -67,7 +69,7 @@ impl<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
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<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
{
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<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
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<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
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<S: AsyncRead + AsyncWrite> Future for StartTlsClient<S> {
Ok(Async::NotReady) =>
(StartTlsClientState::Start(start), Ok(Async::NotReady)),
Err(e) =>
(StartTlsClientState::Invalid, Err(e)),
(StartTlsClientState::Invalid, Err(format!("{}", e))),
},
StartTlsClientState::Invalid =>
unreachable!(),

View file

@ -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<S: AsyncRead + AsyncWrite> XMPPStream<S> {
.is_some()
}
pub fn starttls(self, arc_config: Arc<ClientConfig>) -> StartTlsClient<S> {
StartTlsClient::from_stream(self, arc_config)
pub fn starttls(self) -> StartTlsClient<S> {
StartTlsClient::from_stream(self)
}
pub fn auth(self, username: &str, password: &str) -> Result<ClientAuth<S>, String> {