diff --git a/libp2p-websocket/Cargo.toml b/libp2p-websocket/Cargo.toml index 1561b3a6..52144403 100644 --- a/libp2p-websocket/Cargo.toml +++ b/libp2p-websocket/Cargo.toml @@ -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 } diff --git a/libp2p-websocket/src/browser.rs b/libp2p-websocket/src/browser.rs index 0440207b..fe911ec8 100644 --- a/libp2p-websocket/src/browser.rs +++ b/libp2p-websocket/src/browser.rs @@ -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 { (&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(()), } } diff --git a/libp2p-websocket/src/desktop.rs b/libp2p-websocket/src/desktop.rs index cdb082f0..38b9f65d 100644 --- a/libp2p-websocket/src/desktop.rs +++ b/libp2p-websocket/src/desktop.rs @@ -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 { transport: T, @@ -143,8 +143,9 @@ where fn dial(self, original_addr: Multiaddr) -> Result { 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))