Add support for WSS for dialing

This commit is contained in:
Pierre Krieger
2018-01-11 16:06:11 +01:00
parent 6837a3928d
commit 6c737c61bf
3 changed files with 15 additions and 10 deletions

View File

@ -11,7 +11,7 @@ rw-stream-sink = { path = "../rw-stream-sink" }
tokio-io = "0.1"
[target.'cfg(not(target_os = "emscripten"))'.dependencies]
websocket = { version = "0.20.2", default-features = false, features = ["async"] }
websocket = { version = "0.20.2", default-features = false, features = ["async", "async-ssl"] }
[target.'cfg(target_os = "emscripten")'.dependencies]
stdweb = { version = "0.1.3", default-features = false }

View File

@ -35,8 +35,6 @@ use tokio_io::{AsyncRead, AsyncWrite};
///
/// This implementation of `Transport` accepts any address that looks like
/// `/ip4/.../tcp/.../ws` or `/ip6/.../tcp/.../ws`, and connect to the corresponding IP and port.
///
/// > **Note**: The `/wss` protocol isn't supported.
#[derive(Debug, Clone)]
pub struct BrowserWsConfig;
@ -291,6 +289,12 @@ fn multiaddr_to_target(addr: &Multiaddr) -> Result<String, ()> {
(&AddrComponent::IP6(ref ip), &AddrComponent::TCP(port), &AddrComponent::WS) => {
Ok(format!("ws://[{}]:{}/", ip, port))
}
(&AddrComponent::IP4(ref ip), &AddrComponent::TCP(port), &AddrComponent::WSS) => {
Ok(format!("wss://{}:{}/", ip, port))
}
(&AddrComponent::IP6(ref ip), &AddrComponent::TCP(port), &AddrComponent::WSS) => {
Ok(format!("wss://[{}]:{}/", ip, port))
}
_ => Err(()),
}
}

View File

@ -31,10 +31,10 @@ use websocket::stream::async::Stream as AsyncStream;
/// Represents the configuration for a websocket transport capability for libp2p. Must be put on
/// top of another `Transport`.
///
/// This implementation of `Transport` accepts any address that ends with `/ws`, and will try to
/// pass the underlying multiaddress to the underlying `Transport`.
/// This implementation of `Transport` accepts any address that ends with `/ws` or `/wss`, and will
/// try to pass the underlying multiaddress to the underlying `Transport`.
///
/// > **Note**: The `/wss` protocol isn't supported.
/// > **Note**: `/wss` is only supported for dialing and not listening.
#[derive(Debug, Clone)]
pub struct WsConfig<T> {
transport: T,
@ -143,8 +143,9 @@ where
fn dial(self, original_addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
let mut inner_addr = original_addr.clone();
match inner_addr.pop() {
Some(AddrComponent::WS) => {}
let is_wss = match inner_addr.pop() {
Some(AddrComponent::WS) => false,
Some(AddrComponent::WSS) => true,
_ => return Err((self, original_addr)),
};
@ -160,10 +161,10 @@ where
}
};
let dial = inner_dial.into_future().and_then(|connec| {
let dial = inner_dial.into_future().and_then(move |connec| {
// We pass a dummy address to `ClientBuilder` because it is never used anywhere
// in the negotiation anyway, and we use `async_connect_on` to pass a stream.
ClientBuilder::new("ws://127.0.0.1:80")
ClientBuilder::new(if is_wss { "wss://127.0.0.1" } else { "ws://127.0.0.1" })
.expect("hard-coded ws address is always valid")
.async_connect_on(connec)
.map_err(|err| IoError::new(IoErrorKind::Other, err))