|
|
@ -1,22 +1,19 @@ |
|
|
|
//! Connection helper.
|
|
|
|
//! Connection helper.
|
|
|
|
|
|
|
|
|
|
|
|
extern crate tokio_dns; |
|
|
|
|
|
|
|
extern crate tokio_tcp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::net::SocketAddr; |
|
|
|
|
|
|
|
use std::io::Result as IoResult; |
|
|
|
use std::io::Result as IoResult; |
|
|
|
|
|
|
|
use std::net::SocketAddr; |
|
|
|
|
|
|
|
|
|
|
|
use self::tokio_tcp::TcpStream; |
|
|
|
use tokio_tcp::TcpStream; |
|
|
|
|
|
|
|
|
|
|
|
use futures::{future, Future}; |
|
|
|
use futures::{future, Future}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
use tungstenite::client::url_mode; |
|
|
|
use tungstenite::client::url_mode; |
|
|
|
use tungstenite::handshake::client::Response; |
|
|
|
use tungstenite::handshake::client::Response; |
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
|
|
|
|
use stream::{NoDelay, PeerAddr}; |
|
|
|
use super::{client_async, Request, WebSocketStream}; |
|
|
|
use super::{WebSocketStream, Request, client_async}; |
|
|
|
use crate::stream::{NoDelay, PeerAddr}; |
|
|
|
|
|
|
|
|
|
|
|
impl NoDelay for TcpStream { |
|
|
|
impl NoDelay for TcpStream { |
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { |
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { |
|
|
@ -30,24 +27,21 @@ impl PeerAddr for TcpStream { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature="tls")] |
|
|
|
#[cfg(feature = "tls")] |
|
|
|
mod encryption { |
|
|
|
mod encryption { |
|
|
|
extern crate native_tls; |
|
|
|
use native_tls::TlsConnector; |
|
|
|
extern crate tokio_tls; |
|
|
|
use tokio_tls::{TlsConnector as TokioTlsConnector, TlsStream}; |
|
|
|
|
|
|
|
|
|
|
|
use self::native_tls::TlsConnector; |
|
|
|
|
|
|
|
use self::tokio_tls::{TlsConnector as TokioTlsConnector, TlsStream}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::io::{Read, Result as IoResult, Write}; |
|
|
|
use std::net::SocketAddr; |
|
|
|
use std::net::SocketAddr; |
|
|
|
use std::io::{Read, Write, Result as IoResult}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use futures::{future, Future}; |
|
|
|
use futures::{future, Future}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
use tungstenite::stream::Mode; |
|
|
|
use tungstenite::stream::Mode; |
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
|
|
|
|
use stream::{NoDelay, PeerAddr, Stream as StreamSwitcher}; |
|
|
|
use crate::stream::{NoDelay, PeerAddr, Stream as StreamSwitcher}; |
|
|
|
|
|
|
|
|
|
|
|
/// A stream that might be protected with TLS.
|
|
|
|
/// A stream that might be protected with TLS.
|
|
|
|
pub type MaybeTlsStream<S> = StreamSwitcher<S, TlsStream<S>>; |
|
|
|
pub type MaybeTlsStream<S> = StreamSwitcher<S, TlsStream<S>>; |
|
|
@ -66,50 +60,58 @@ mod encryption { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn wrap_stream<S>(socket: S, domain: String, mode: Mode) |
|
|
|
pub fn wrap_stream<S>( |
|
|
|
-> Box<dyn Future<Item=AutoStream<S>, Error=Error> + Send> |
|
|
|
socket: S, |
|
|
|
|
|
|
|
domain: String, |
|
|
|
|
|
|
|
mode: Mode, |
|
|
|
|
|
|
|
) -> Box<dyn Future<Item = AutoStream<S>, Error = Error> + Send> |
|
|
|
where |
|
|
|
where |
|
|
|
S: 'static + AsyncRead + AsyncWrite + Send, |
|
|
|
S: 'static + AsyncRead + AsyncWrite + Send, |
|
|
|
{ |
|
|
|
{ |
|
|
|
match mode { |
|
|
|
match mode { |
|
|
|
Mode::Plain => Box::new(future::ok(StreamSwitcher::Plain(socket))), |
|
|
|
Mode::Plain => Box::new(future::ok(StreamSwitcher::Plain(socket))), |
|
|
|
Mode::Tls => { |
|
|
|
Mode::Tls => Box::new( |
|
|
|
Box::new(future::result(TlsConnector::new()) |
|
|
|
future::result(TlsConnector::new()) |
|
|
|
.map(TokioTlsConnector::from) |
|
|
|
.map(TokioTlsConnector::from) |
|
|
|
.and_then(move |connector| connector.connect(&domain, socket)) |
|
|
|
.and_then(move |connector| connector.connect(&domain, socket)) |
|
|
|
.map(StreamSwitcher::Tls) |
|
|
|
.map(StreamSwitcher::Tls) |
|
|
|
.map_err(Error::Tls)) |
|
|
|
.map_err(Error::Tls), |
|
|
|
} |
|
|
|
), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature="tls")] |
|
|
|
#[cfg(feature = "tls")] |
|
|
|
pub use self::encryption::MaybeTlsStream; |
|
|
|
pub use self::encryption::MaybeTlsStream; |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature="tls"))] |
|
|
|
#[cfg(not(feature = "tls"))] |
|
|
|
mod encryption { |
|
|
|
mod encryption { |
|
|
|
use futures::{future, Future}; |
|
|
|
use futures::{future, Future}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
use tungstenite::stream::Mode; |
|
|
|
use tungstenite::stream::Mode; |
|
|
|
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
|
|
|
|
pub type AutoStream<S> = S; |
|
|
|
pub type AutoStream<S> = S; |
|
|
|
|
|
|
|
|
|
|
|
pub fn wrap_stream<S>(socket: S, _domain: String, mode: Mode) |
|
|
|
pub fn wrap_stream<S>( |
|
|
|
-> Box<Future<Item=AutoStream<S>, Error=Error> + Send> |
|
|
|
socket: S, |
|
|
|
|
|
|
|
_domain: String, |
|
|
|
|
|
|
|
mode: Mode, |
|
|
|
|
|
|
|
) -> Box<Future<Item = AutoStream<S>, Error = Error> + Send> |
|
|
|
where |
|
|
|
where |
|
|
|
S: 'static + AsyncRead + AsyncWrite + Send, |
|
|
|
S: 'static + AsyncRead + AsyncWrite + Send, |
|
|
|
{ |
|
|
|
{ |
|
|
|
match mode { |
|
|
|
match mode { |
|
|
|
Mode::Plain => Box::new(future::ok(socket)), |
|
|
|
Mode::Plain => Box::new(future::ok(socket)), |
|
|
|
Mode::Tls => Box::new(future::err(Error::Url("TLS support not compiled in.".into()))), |
|
|
|
Mode::Tls => Box::new(future::err(Error::Url( |
|
|
|
|
|
|
|
"TLS support not compiled in.".into(), |
|
|
|
|
|
|
|
))), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
use self::encryption::{AutoStream, wrap_stream}; |
|
|
|
use self::encryption::{wrap_stream, AutoStream}; |
|
|
|
|
|
|
|
|
|
|
|
/// Get a domain from an URL.
|
|
|
|
/// Get a domain from an URL.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
@ -122,8 +124,10 @@ fn domain(request: &Request) -> Result<String, Error> { |
|
|
|
|
|
|
|
|
|
|
|
/// Creates a WebSocket handshake from a request and a stream,
|
|
|
|
/// Creates a WebSocket handshake from a request and a stream,
|
|
|
|
/// upgrading the stream to TLS if required.
|
|
|
|
/// upgrading the stream to TLS if required.
|
|
|
|
pub fn client_async_tls<R, S>(request: R, stream: S) |
|
|
|
pub fn client_async_tls<R, S>( |
|
|
|
-> Box<dyn Future<Item=(WebSocketStream<AutoStream<S>>, Response), Error=Error> + Send> |
|
|
|
request: R, |
|
|
|
|
|
|
|
stream: S, |
|
|
|
|
|
|
|
) -> Box<dyn Future<Item = (WebSocketStream<AutoStream<S>>, Response), Error = Error> + Send> |
|
|
|
where |
|
|
|
where |
|
|
|
R: Into<Request<'static>>, |
|
|
|
R: Into<Request<'static>>, |
|
|
|
S: 'static + AsyncRead + AsyncWrite + NoDelay + Send, |
|
|
|
S: 'static + AsyncRead + AsyncWrite + NoDelay + Send, |
|
|
@ -141,20 +145,23 @@ where |
|
|
|
Err(e) => return Box::new(future::err(e)), |
|
|
|
Err(e) => return Box::new(future::err(e)), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Box::new(wrap_stream(stream, domain, mode) |
|
|
|
Box::new( |
|
|
|
|
|
|
|
wrap_stream(stream, domain, mode) |
|
|
|
.and_then(|mut stream| { |
|
|
|
.and_then(|mut stream| { |
|
|
|
NoDelay::set_nodelay(&mut stream, true) |
|
|
|
NoDelay::set_nodelay(&mut stream, true) |
|
|
|
.map(move |()| stream) |
|
|
|
.map(move |()| stream) |
|
|
|
.map_err(|e| e.into()) |
|
|
|
.map_err(|e| e.into()) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.and_then(move |stream| client_async(request, stream))) |
|
|
|
.and_then(move |stream| client_async(request, stream)), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Connect to a given URL.
|
|
|
|
/// Connect to a given URL.
|
|
|
|
pub fn connect_async<R>(request: R) |
|
|
|
pub fn connect_async<R>( |
|
|
|
-> Box<dyn Future<Item=(WebSocketStream<AutoStream<TcpStream>>, Response), Error=Error> + Send> |
|
|
|
request: R, |
|
|
|
|
|
|
|
) -> Box<dyn Future<Item = (WebSocketStream<AutoStream<TcpStream>>, Response), Error = Error> + Send> |
|
|
|
where |
|
|
|
where |
|
|
|
R: Into<Request<'static>> |
|
|
|
R: Into<Request<'static>>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
let request: Request = request.into(); |
|
|
|
let request: Request = request.into(); |
|
|
|
|
|
|
|
|
|
|
@ -162,8 +169,14 @@ where |
|
|
|
Ok(domain) => domain, |
|
|
|
Ok(domain) => domain, |
|
|
|
Err(err) => return Box::new(future::err(err)), |
|
|
|
Err(err) => return Box::new(future::err(err)), |
|
|
|
}; |
|
|
|
}; |
|
|
|
let port = request.url.port_or_known_default().expect("Bug: port unknown"); |
|
|
|
let port = request |
|
|
|
|
|
|
|
.url |
|
|
|
|
|
|
|
.port_or_known_default() |
|
|
|
|
|
|
|
.expect("Bug: port unknown"); |
|
|
|
|
|
|
|
|
|
|
|
Box::new(tokio_dns::TcpStream::connect((domain.as_str(), port)).map_err(|e| e.into()) |
|
|
|
Box::new( |
|
|
|
.and_then(move |socket| client_async_tls(request, socket))) |
|
|
|
tokio_dns::TcpStream::connect((domain.as_str(), port)) |
|
|
|
|
|
|
|
.map_err(|e| e.into()) |
|
|
|
|
|
|
|
.and_then(move |socket| client_async_tls(request, socket)), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|