diff --git a/Cargo.lock b/Cargo.lock index ae84c6b..6430aa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,7 +406,7 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "caesar-transfer-iu" -version = "0.2.0" +version = "0.3.1" dependencies = [ "aes-gcm", "axum 0.7.5", diff --git a/Cargo.toml b/Cargo.toml index 951c5c1..8e8903b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "caesar-transfer-iu" -version = "0.2.0" +version = "0.3.1" edition = "2021" build = "src/build.rs" authors = ["Manuel Keidel", "Patryk Hegenberg", "Krzysztof Stankiewicz"] diff --git a/src/receiver/http_client.rs b/src/receiver/http_client.rs index df700f0..518b7b8 100644 --- a/src/receiver/http_client.rs +++ b/src/receiver/http_client.rs @@ -8,7 +8,7 @@ use crate::relay::transfer::TransferResponse; type Result = std::result::Result>; pub async fn download_info(relay: &str, name: &str) -> Result { - let url = String::from("http://") + relay; + let url = String::from(relay); let hashed_name = Sha256::digest(name.as_bytes()); let hashed_string = hex::encode(hashed_name); @@ -25,7 +25,7 @@ pub async fn download_info(relay: &str, name: &str) -> Result } pub async fn download_success(relay: &str, name: &str) -> Result<()> { - let url = String::from("http://") + relay; + let url = String::from(relay); let hashed_name = Sha256::digest(name.as_bytes()); let hashed_string = hex::encode(hashed_name); diff --git a/src/receiver/mod.rs b/src/receiver/mod.rs index 3e071c7..c31db42 100644 --- a/src/receiver/mod.rs +++ b/src/receiver/mod.rs @@ -38,7 +38,7 @@ pub mod client; pub mod http_client; -use crate::receiver::client as receiver; +use crate::{receiver::client as receiver, sender::util::replace_protocol}; use tokio_tungstenite::{ connect_async, @@ -47,9 +47,12 @@ use tokio_tungstenite::{ use tracing::{debug, error}; pub async fn start_receiver(relay: &str, name: &str) { - let res = http_client::download_info(relay, name).await.unwrap(); + let http_url = replace_protocol(relay); + let res = http_client::download_info(http_url.as_str(), name) + .await + .unwrap(); debug!("Got room_id from Server: {:?}", res); - let res_ip = res.ip + ":9000"; + let res_ip = String::from("ws://") + res.ip.as_str() + ":9000"; if let Err(local_err) = start_ws_com(res_ip.as_str(), res.local_room_id.as_str()).await { debug!("Failed to connect local: {local_err}"); @@ -57,7 +60,7 @@ pub async fn start_receiver(relay: &str, name: &str) { debug!("Failed to connect remote: {relay_err}"); } } - let success = http_client::download_success(relay, name).await; + let success = http_client::download_success(http_url.as_str(), name).await; match success { Ok(()) => debug!("Success"), Err(e) => error!("Error: {e:?}"), @@ -72,7 +75,7 @@ pub async fn start_receiver(relay: &str, name: &str) { } pub async fn start_ws_com(relay: &str, name: &str) -> Result<(), Box> { - let url = String::from("ws://") + relay + "/ws"; + let url = String::from(relay) + "/ws"; let Ok(mut request) = url.into_client_request() else { println!("Error: Failed to create request."); return Err("Failed to create request".into()); diff --git a/src/sender/client.rs b/src/sender/client.rs index 02f0ad6..321de17 100644 --- a/src/sender/client.rs +++ b/src/sender/client.rs @@ -1,5 +1,5 @@ use crate::sender::http_client::send_info; -use crate::sender::util::hash_random_name; +use crate::sender::util::{hash_random_name, replace_protocol}; use crate::shared::{ packets::{ list_packet, packet::Value, ChunkPacket, HandshakePacket, HandshakeResponsePacket, @@ -135,7 +135,7 @@ fn on_create_room( let send_url = url.to_string(); let h_name = hash_name.to_string(); - let server_url = String::from("http://") + relay.as_str(); + let server_url = replace_protocol(relay.as_str()); let res = std::thread::spawn(move || { tokio::runtime::Builder::new_current_thread() .enable_all() diff --git a/src/sender/mod.rs b/src/sender/mod.rs index a57d52a..a41c886 100644 --- a/src/sender/mod.rs +++ b/src/sender/mod.rs @@ -85,7 +85,7 @@ pub async fn start_sender(relay: Arc, files: Arc>) { }); let local_thread = task::spawn(async move { connect_to_server( - Arc::new(String::from("0.0.0.0:9000")), + Arc::new(String::from("ws://0.0.0.0:9000")), local_files.clone(), Some(local_room_id), local_relay.clone(), @@ -146,7 +146,7 @@ async fn connect_to_server( tx: mpsc::Sender<()>, is_local: bool, ) { - let url = format!("ws://{}/ws", relay); + let url = format!("{}/ws", relay); let message_relay = format!("{}", message_server); let transfer_name = format!("{}", transfer_name); match url.clone().into_client_request() { diff --git a/src/sender/util.rs b/src/sender/util.rs index d429a24..d8927b8 100644 --- a/src/sender/util.rs +++ b/src/sender/util.rs @@ -32,6 +32,15 @@ pub fn hash_random_name(name: String) -> String { hex::encode(hashed_name) } +pub fn replace_protocol(address: &str) -> String { + let mut result = address.to_string(); + result = result.replace("ws://", "http://"); + + result = result.replace("wss://", "https://"); + + result +} + #[cfg(test)] mod tests { use super::*;