diff --git a/Cargo.toml b/Cargo.toml index a2e32db..b3be25a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,9 @@ path = "src/shuttle.rs" futures-util = "0.3" tungstenite = "0.21.0" tokio = { version = "1.28.1", features = ["full"] } -tokio-tungstenite = { version = "0.21.0", features =["rustls-tls-webpki-roots"] } +tokio-tungstenite = { version = "0.21.0", features = [ + "rustls-tls-webpki-roots", +] } serde_json = { version = "1.0" } serde = { version = "1.0", features = ["derive"] } uuid = { version = "1.3.2", features = ["v4"] } @@ -47,8 +49,8 @@ tower = { version = "0.4", features = ["util"] } shuttle-axum = { version = "0.44.0" } shuttle-runtime = { version = "0.44.0" } dotenvy = "0.15.7" +reqwest = { version = "0.12.4", features = ["blocking", "json"] } hex = "0.4.3" -reqwest = { version = "0.12.4", features = ["json", "blocking"] } [build-dependencies] prost-build = "0.12.4" diff --git a/src/receiver/http_client.rs b/src/receiver/http_client.rs new file mode 100644 index 0000000..9c6e155 --- /dev/null +++ b/src/receiver/http_client.rs @@ -0,0 +1,25 @@ +use hex; +use reqwest; +use sha2::{Digest, Sha256}; +use tracing::error; + +use crate::relay::transfer::Transfer; + +type Result = std::result::Result>; + +pub async fn download_info(relay: &str, name: &str) -> Result { + let url = String::from("http://") + relay; + let hashed_name = Sha256::digest(name.as_bytes()); + let hashed_string = hex::encode(hashed_name); + + match reqwest::get(format!("{}/download/{}", url, hashed_string)).await { + Ok(resp) => match resp.json::().await { + Ok(res) => Ok(res.room_id), + Err(e) => Err(Box::new(e)), + }, + Err(err) => { + error!("Error: {err}"); + Err(Box::new(err)) + } + } +} diff --git a/src/receiver/mod.rs b/src/receiver/mod.rs index db83660..7221412 100644 --- a/src/receiver/mod.rs +++ b/src/receiver/mod.rs @@ -36,6 +36,7 @@ /// The `start` function takes ownership of the WebSocket stream and /// the file paths, so we pass them by value. pub mod client; +pub mod http_client; use crate::receiver::client as receiver; @@ -43,10 +44,18 @@ use tokio_tungstenite::{ connect_async, tungstenite::{client::IntoClientRequest, http::HeaderValue}, }; -use tracing::error; +use tracing::{debug, error}; pub async fn start_receiver(relay: &str, name: &str) { - let Ok(mut request) = relay.into_client_request() else { + let room_id = http_client::download_info(relay, name).await.unwrap(); + debug!("Got Room_ID from Server: {room_id}"); + + start_ws_com(relay, room_id.as_str()).await; +} + +pub async fn start_ws_com(relay: &str, name: &str) { + let url = String::from("ws://") + relay + "/ws"; + let Ok(mut request) = url.into_client_request() else { println!("Error: Failed to create request."); return; }; @@ -59,13 +68,17 @@ pub async fn start_receiver(relay: &str, name: &str) { println!("Attempting to connect..."); - let Ok((socket, _)) = connect_async(request).await else { - error!("Error: Failed to connect."); - return; - }; + // let Ok((socket, _)) = connect_async(request).await else { + // error!("Error: Failed to connect."); + // return; + // }; + match connect_async(request).await { + Ok((socket, _)) => receiver::start(socket, name).await, + Err(e) => error!("Error: Failed to connect: {e:?}"), + }; // The start function is defined in the // receiver::client module and is the function that interacts with // the server to receive files. - receiver::start(socket, name).await + // receiver::start(socket, name).await }