From 8b621597625524458fcf6c8ae26d0f2864045b63 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Sun, 28 Apr 2024 23:13:35 +0200 Subject: [PATCH] feature(sender): added output of randomly generated name in order to output a randomly generated name a new http_client was included to perform the request to the relay server. --- Cargo.lock | 324 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 + src/relay/server.rs | 26 +-- src/sender/client.rs | 8 + src/sender/http_client.rs | 31 ++++ src/sender/mod.rs | 1 + src/sender/util.rs | 10 +- 7 files changed, 376 insertions(+), 26 deletions(-) create mode 100644 src/sender/http_client.rs diff --git a/Cargo.lock b/Cargo.lock index 8912e8d..44b4599 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -419,6 +419,7 @@ dependencies = [ "flume", "futures-util", "headers", + "hex", "hmac", "local-ip-address", "p256", @@ -427,6 +428,7 @@ dependencies = [ "prost-types", "qr2term", "rand", + "reqwest", "sanitize-filename", "serde", "serde_json", @@ -574,6 +576,16 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -778,6 +790,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -833,6 +854,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -859,6 +895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -878,6 +915,12 @@ dependencies = [ "futures-util", ] +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-macro" version = "0.3.30" @@ -908,9 +951,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", + "futures-io", "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -992,6 +1037,25 @@ dependencies = [ "tracing", ] +[[package]] +name = "h2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "816ec7294445779408f36fe57bc5b7fc1cf59664059096c65f905c1c61f58069" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 1.1.0", + "indexmap 2.2.6", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -1055,6 +1119,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hkdf" version = "0.12.4" @@ -1157,7 +1227,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", + "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", "httparse", @@ -1180,6 +1250,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", + "h2 0.4.4", "http 1.1.0", "http-body 1.0.0", "httparse", @@ -1188,6 +1259,7 @@ dependencies = [ "pin-project-lite", "smallvec", "tokio", + "want", ] [[package]] @@ -1202,6 +1274,22 @@ dependencies = [ "tokio-io-timeout", ] +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.3.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.3" @@ -1209,6 +1297,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" dependencies = [ "bytes", + "futures-channel", "futures-util", "http 1.1.0", "http-body 1.0.0", @@ -1216,6 +1305,9 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", + "tower", + "tower-service", + "tracing", ] [[package]] @@ -1280,6 +1372,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "ipnet" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" + [[package]] name = "itertools" version = "0.10.5" @@ -1423,6 +1521,24 @@ dependencies = [ "getrandom", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "neli" version = "0.6.4" @@ -1504,6 +1620,50 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "openssl" +version = "0.10.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "opentelemetry" version = "0.21.0" @@ -1669,6 +1829,12 @@ dependencies = [ "spki", ] +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + [[package]] name = "polyval" version = "0.6.2" @@ -1903,6 +2069,49 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +[[package]] +name = "reqwest" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" +dependencies = [ + "base64 0.22.0", + "bytes", + "encoding_rs", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.4.4", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.3.1", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rfc6979" version = "0.4.0" @@ -1961,6 +2170,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-pemfile" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" +dependencies = [ + "base64 0.22.0", + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.5.0" @@ -2000,6 +2219,15 @@ dependencies = [ "regex", ] +[[package]] +name = "schannel" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -2020,6 +2248,29 @@ dependencies = [ "zeroize", ] +[[package]] +name = "security-framework" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.22" @@ -2394,6 +2645,27 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tempfile" version = "3.10.1" @@ -2500,6 +2772,16 @@ dependencies = [ "syn 2.0.60", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.25.0" @@ -2563,7 +2845,7 @@ dependencies = [ "axum 0.6.20", "base64 0.21.7", "bytes", - "h2", + "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.28", @@ -2860,6 +3142,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vec_map" version = "0.8.2" @@ -2912,6 +3200,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.92" @@ -2941,6 +3241,16 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "web-time" version = "0.2.4" @@ -3130,6 +3440,16 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winreg" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "zeroize" version = "1.7.0" diff --git a/Cargo.toml b/Cargo.toml index f4b9510..a2e32db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,8 @@ tower = { version = "0.4", features = ["util"] } shuttle-axum = { version = "0.44.0" } shuttle-runtime = { version = "0.44.0" } dotenvy = "0.15.7" +hex = "0.4.3" +reqwest = { version = "0.12.4", features = ["json", "blocking"] } [build-dependencies] prost-build = "0.12.4" diff --git a/src/relay/server.rs b/src/relay/server.rs index 57ce034..dd11b88 100644 --- a/src/relay/server.rs +++ b/src/relay/server.rs @@ -314,9 +314,9 @@ pub async fn download_info( ( StatusCode::NOT_FOUND, Json(Transfer { - name: "".to_string(), - ip: "".to_string(), - room_id: "".to_string(), + name: String::from(""), + ip: String::from(""), + room_id: String::from(""), }), ) } @@ -351,24 +351,4 @@ pub async fn download_success( })), ) } - // match data.iter().find(|request| request.body.name == name) { - // Some(request) => { - // debug!("Found transfer name."); - // return ( - // StatusCode::OK, - // Json(json!({ - // "message" : "transfer deleted" - // })), - // ); - // } - // None => { - // warn!("couldn't find transfer-name: {}", name); - // return ( - // StatusCode::NOT_FOUND, - // Json(json!({ - // "message" : "transfer not found" - // })), - // ); - // } - // } } diff --git a/src/sender/client.rs b/src/sender/client.rs index eef4645..c044b16 100644 --- a/src/sender/client.rs +++ b/src/sender/client.rs @@ -1,3 +1,5 @@ +use crate::sender::http_client::send_info; +use crate::sender::util::{generate_random_name, hash_random_name}; use crate::shared::{ packets::{ list_packet, packet::Value, ChunkPacket, HandshakePacket, HandshakeResponsePacket, @@ -121,6 +123,11 @@ fn on_create_room(context: &Context, id: String) -> Status { let base64 = general_purpose::STANDARD.encode(&context.hmac); let url = format!("{}-{}", id, base64); + let rand_name = generate_random_name(); + let hash_name = hash_random_name(rand_name.clone()); + + let res = send_info("http://localhost:8000", &hash_name, url.as_str()); + debug!("Got Result: {:#?}", res); // Print a newline to the console to separate the output from the command // line. println!(); @@ -137,6 +144,7 @@ fn on_create_room(context: &Context, id: String) -> Status { // Print a message to the console with the URL. println!("Created room: {}", url); + println!("Transfername is: {}", rand_name); // Continue the event loop. Status::Continue() diff --git a/src/sender/http_client.rs b/src/sender/http_client.rs new file mode 100644 index 0000000..41109c3 --- /dev/null +++ b/src/sender/http_client.rs @@ -0,0 +1,31 @@ +use std::collections::HashMap; +use tracing::error; + +use local_ip_address::{local_ip, local_ipv6}; +use reqwest::blocking::Client; + +type Result = std::result::Result>; + +pub fn send_info(relay: &str, name: &str, room_id: &str) -> Result { + let sender_ip = match local_ipv6() { + Ok(ip) => ip, + Err(_) => match local_ip() { + Ok(ip) => ip, + Err(e) => { + error!("Error getting local ip: {e:?}"); + return Err(Box::new(e)); + } + }, + }; + let ip_str = sender_ip.to_owned().to_string(); + let mut map = HashMap::new(); + map.insert("name", String::from(name)); + map.insert("ip", ip_str); + map.insert("room_id", String::from(room_id)); + let client = Client::new(); + let _ = match client.post(format!("{}/upload", relay)).json(&map).send() { + Ok(_) => Ok(room_id.to_string()), + Err(e) => Err(Box::new(e)), + }; + Ok("".to_string()) +} diff --git a/src/sender/mod.rs b/src/sender/mod.rs index f216012..701ee3f 100644 --- a/src/sender/mod.rs +++ b/src/sender/mod.rs @@ -39,6 +39,7 @@ /// The `start` function takes ownership of the `WebSocketStream` and the file /// paths, so we pass it the `paths` vector by value. pub mod client; +pub mod http_client; pub mod util; use crate::sender::client as sender; diff --git a/src/sender/util.rs b/src/sender/util.rs index 8c8d4a2..14f3fa0 100644 --- a/src/sender/util.rs +++ b/src/sender/util.rs @@ -1,6 +1,8 @@ +use hex; use rand::{seq::SliceRandom, thread_rng}; +use sha2::{Digest, Sha256}; -fn generate_random_name() -> String { +pub fn generate_random_name() -> String { let mut rng = thread_rng(); let adjective = adjectives().choose(&mut rng).unwrap(); // let adjective = adjectives().sample(&mut rng).unwrap(); @@ -24,6 +26,12 @@ fn nouns2() -> &'static [&'static str] { static NOUNS2: &[&str] = &["cookie", "cake", "frosting"]; NOUNS2 } + +pub fn hash_random_name(name: String) -> String { + let hashed_name = Sha256::digest(name.as_bytes()); + return hex::encode(hashed_name); +} + #[cfg(test)] mod tests { use super::*;