From 5646c69a3e55b20119bcd719b39facc83ddc4253 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Tue, 21 Nov 2023 11:52:09 +0100 Subject: [PATCH] use secure websocket when https is used Signed-off-by: Berend Sliedrecht --- crates/cloudagent-python/src/cloudagent/webhook.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/cloudagent-python/src/cloudagent/webhook.rs b/crates/cloudagent-python/src/cloudagent/webhook.rs index f15a18bd..b3609902 100644 --- a/crates/cloudagent-python/src/cloudagent/webhook.rs +++ b/crates/cloudagent-python/src/cloudagent/webhook.rs @@ -8,13 +8,15 @@ use tungstenite::connect; impl WebhookModule for CloudAgentPython { /// Listen to all incoming webhook async fn listen(&self, on_event: fn(serde_json::Value)) -> Result<()> { - let stripped_agent_url = match &self.endpoint { - s if s.starts_with("http://") => &s[7..], - s if s.starts_with("https://") => &s[8..], + let (uses_tls, stripped_agent_url) = match &self.endpoint { + s if s.starts_with("http://") => (false, &s[7..]), + s if s.starts_with("https://") => (true, &s[8..]), s => return Err(Error::InvalidAgentUrl(s.clone()).into()), }; - let listen_url = format!("wss://{stripped_agent_url}/ws"); + let scheme = if uses_tls { "wss" } else { "ws" }; + + let listen_url = format!("{scheme}://{stripped_agent_url}/ws"); info!({ "message": format!("Listening on {listen_url}") }); let (mut socket, _response) = connect(listen_url)?;