From fe211c20372a5e107fb709b519ed58e5fdb8a9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=CC=81o=20Monnom?= Date: Thu, 11 Jan 2024 08:56:54 +0100 Subject: [PATCH 1/2] more explicit url errors --- Cargo.lock | 2 +- livekit-api/src/signal_client/mod.rs | 35 +++++++++++++++++-- .../src/signal_client/signal_stream.rs | 9 +---- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd06bceb..d2ab36d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1067,7 +1067,7 @@ dependencies = [ [[package]] name = "livekit-ffi" -version = "0.3.17" +version = "0.3.18" dependencies = [ "console-subscriber", "dashmap", diff --git a/livekit-api/src/signal_client/mod.rs b/livekit-api/src/signal_client/mod.rs index ab3a9ed6..7d62849d 100644 --- a/livekit-api/src/signal_client/mod.rs +++ b/livekit-api/src/signal_client/mod.rs @@ -49,7 +49,7 @@ pub enum SignalError { #[error("ws failure: {0}")] WsError(#[from] WsError), #[error("failed to parse the url {0}")] - UrlParse(#[from] url::ParseError), + UrlParse(String), #[error("client error: {0} - {1}")] Client(StatusCode, String), #[error("server error: {0} - {1}")] @@ -392,7 +392,20 @@ fn is_queuable(signal: &proto::signal_request::Message) -> bool { } fn get_livekit_url(url: &str, token: &str, options: &SignalOptions) -> SignalResult { - let mut lk_url = url::Url::parse(url)?; + let mut lk_url = url::Url::parse(url).map_err(|err| SignalError::UrlParse(err.to_string()))?; + + if !lk_url.has_host() { + return Err(SignalError::UrlParse("missing host or scheme".into())); + } + + // Automatically switch to websocket scheme when using user is providing http(s) scheme + if lk_url.scheme() == "https" { + lk_url.set_scheme("wss").unwrap(); + } else if lk_url.scheme() == "http" { + lk_url.set_scheme("ws").unwrap(); + } else if lk_url.scheme() != "wss" && lk_url.scheme() != "ws" { + return Err(SignalError::UrlParse(format!("unsupported scheme: {}", lk_url.scheme()))); + } if let Ok(mut segs) = lk_url.path_segments_mut() { segs.push("rtc"); @@ -442,3 +455,21 @@ get_async_message!( proto::signal_response::Message::Reconnect(msg) => msg, proto::ReconnectResponse ); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn livekit_url_test() { + let it = "null_token"; + let io = SignalOptions::default(); + + assert!(get_livekit_url("localhost:7880", it, &io).is_err()); + assert_eq!(get_livekit_url("https://localhost:7880", it, &io).unwrap().scheme(), "wss"); + assert_eq!(get_livekit_url("http://localhost:7880", it, &io).unwrap().scheme(), "ws"); + assert_eq!(get_livekit_url("wss://localhost:7880", it, &io).unwrap().scheme(), "wss"); + assert_eq!(get_livekit_url("ws://localhost:7880", it, &io).unwrap().scheme(), "ws"); + assert!(get_livekit_url("ftp://localhost:7880", it, &io).is_err()); + } +} diff --git a/livekit-api/src/signal_client/signal_stream.rs b/livekit-api/src/signal_client/signal_stream.rs index d17475f9..e5561001 100644 --- a/livekit-api/src/signal_client/signal_stream.rs +++ b/livekit-api/src/signal_client/signal_stream.rs @@ -58,7 +58,7 @@ impl SignalStream { /// SignalStream will never try to reconnect if the connection has been /// closed. pub async fn connect( - mut url: url::Url, + url: url::Url, ) -> SignalResult<(Self, mpsc::UnboundedReceiver>)> { { // Don't log sensitive info @@ -82,13 +82,6 @@ impl SignalStream { log::info!("connecting to {}", url); } - // Automatically switch to websocket scheme when using http - if url.scheme() == "https" { - url.set_scheme("wss").unwrap(); - } else if url.scheme() == "http" { - url.set_scheme("ws").unwrap(); - } - let (ws_stream, _) = connect_async(url).await?; let (ws_writer, ws_reader) = ws_stream.split(); From e994ba11d42a7eaa558e72edb69e2126934234b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=CC=81o=20Monnom?= Date: Thu, 11 Jan 2024 09:01:01 +0100 Subject: [PATCH 2/2] Update mod.rs --- livekit-api/src/signal_client/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/livekit-api/src/signal_client/mod.rs b/livekit-api/src/signal_client/mod.rs index 7d62849d..88680a2f 100644 --- a/livekit-api/src/signal_client/mod.rs +++ b/livekit-api/src/signal_client/mod.rs @@ -48,7 +48,7 @@ pub const PROTOCOL_VERSION: u32 = 9; pub enum SignalError { #[error("ws failure: {0}")] WsError(#[from] WsError), - #[error("failed to parse the url {0}")] + #[error("failed to parse the url: {0}")] UrlParse(String), #[error("client error: {0} - {1}")] Client(StatusCode, String),