Skip to content

Commit

Permalink
feat(websocket): Allow wss connections on IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Aug 3, 2024
1 parent 5b4c43c commit 39a4587
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions transports/websocket/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::{error::Error, quicksink, tls};
use either::Either;
use futures::{future::BoxFuture, prelude::*, ready, stream::BoxStream};
use futures_rustls::rustls::pki_types::ServerName;
use futures_rustls::{client, rustls, server};

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Check rustdoc intra-doc links

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / clippy (1.78.0)

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / examples

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Compile on x86_64-apple-darwin

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Compile with MSRV

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Test libp2p

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Test libp2p-pnet

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Test libp2p-websocket

unused import: `rustls`

Check failure on line 25 in transports/websocket/src/framed.rs

View workflow job for this annotation

GitHub Actions / Test libp2p-server

unused import: `rustls`
use libp2p_core::{
connection::Endpoint,
Expand All @@ -33,6 +34,7 @@ use soketto::{
connection::{self, CloseReason},
handshake,
};
use std::net::IpAddr;
use std::{collections::HashMap, ops::DerefMut, sync::Arc};
use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll};
use url::Url;
Expand Down Expand Up @@ -325,15 +327,12 @@ where

let stream = if addr.use_tls {
// begin TLS session
let dns_name = addr
.dns_name
.expect("for use_tls we have checked that dns_name is some");
tracing::trace!(?dns_name, "Starting TLS handshake");
tracing::trace!(?addr.server_name, "Starting TLS handshake");
let stream = tls_config
.client
.connect(dns_name.clone(), stream)
.connect(addr.server_name.clone(), stream)
.map_err(|e| {
tracing::debug!(?dns_name, "TLS handshake failed: {}", e);
tracing::debug!(?addr.server_name, "TLS handshake failed: {}", e);
Error::Tls(tls::Error::from(e))
})
.await?;
Expand Down Expand Up @@ -461,7 +460,7 @@ where
struct WsAddress {
host_port: String,
path: String,
dns_name: Option<rustls::pki_types::ServerName<'static>>,
server_name: ServerName<'static>,
use_tls: bool,
tcp_addr: Multiaddr,
}
Expand All @@ -478,19 +477,21 @@ fn parse_ws_dial_addr<T>(addr: Multiaddr) -> Result<WsAddress, Error<T>> {
let mut protocols = addr.iter();
let mut ip = protocols.next();
let mut tcp = protocols.next();
let (host_port, dns_name) = loop {
let (host_port, server_name) = loop {
match (ip, tcp) {
(Some(Protocol::Ip4(ip)), Some(Protocol::Tcp(port))) => {
break (format!("{ip}:{port}"), None)
let server_name = ServerName::IpAddress(IpAddr::V4(ip).into());
break (format!("{ip}:{port}"), server_name);
}
(Some(Protocol::Ip6(ip)), Some(Protocol::Tcp(port))) => {
break (format!("{ip}:{port}"), None)
let server_name = ServerName::IpAddress(IpAddr::V6(ip).into());
break (format!("[{ip}]:{port}"), server_name);
}
(Some(Protocol::Dns(h)), Some(Protocol::Tcp(port)))
| (Some(Protocol::Dns4(h)), Some(Protocol::Tcp(port)))
| (Some(Protocol::Dns6(h)), Some(Protocol::Tcp(port)))
| (Some(Protocol::Dnsaddr(h)), Some(Protocol::Tcp(port))) => {
break (format!("{}:{}", &h, port), Some(tls::dns_name_ref(&h)?))
break (format!("{}:{}", &h, port), tls::dns_name_ref(&h)?)
}
(Some(_), Some(p)) => {
ip = Some(p);
Expand All @@ -509,13 +510,7 @@ fn parse_ws_dial_addr<T>(addr: Multiaddr) -> Result<WsAddress, Error<T>> {
match protocols.pop() {
p @ Some(Protocol::P2p(_)) => p2p = p,
Some(Protocol::Ws(path)) => break (false, path.into_owned()),
Some(Protocol::Wss(path)) => {
if dns_name.is_none() {
tracing::debug!(address=%addr, "Missing DNS name in WSS address");
return Err(Error::InvalidMultiaddr(addr));
}
break (true, path.into_owned());
}
Some(Protocol::Wss(path)) => break (true, path.into_owned()),
_ => return Err(Error::InvalidMultiaddr(addr)),
}
};
Expand All @@ -529,7 +524,7 @@ fn parse_ws_dial_addr<T>(addr: Multiaddr) -> Result<WsAddress, Error<T>> {

Ok(WsAddress {
host_port,
dns_name,
server_name,
path,
use_tls,
tcp_addr,
Expand Down

0 comments on commit 39a4587

Please sign in to comment.