relay: sender: receiver: add name hashing and move name creation from relay server to sender

This commit is contained in:
Patryk Hegenberg 2024-04-22 21:17:55 +02:00
parent affedf3f3b
commit 5db7d6991c
6 changed files with 122 additions and 35 deletions

73
Cargo.lock generated
View file

@ -210,6 +210,15 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.16.0"
@ -237,6 +246,7 @@ dependencies = [
"clap 4.5.4",
"dotenv",
"env_logger",
"hex",
"http-body-util",
"hyper",
"hyper-util",
@ -247,6 +257,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"sha2",
"tokio",
"tower-http",
"tracing",
@ -342,6 +353,35 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "cpufeatures"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "dotenv"
version = "0.15.0"
@ -501,6 +541,16 @@ dependencies = [
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.14"
@ -564,6 +614,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 = "http"
version = "1.1.0"
@ -1288,6 +1344,17 @@ dependencies = [
"serde",
]
[[package]]
name = "sha2"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
@ -1635,6 +1702,12 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicase"
version = "2.7.0"

View file

@ -26,3 +26,5 @@ tower-http = {version = "0.5.2", features = ["fs"]}
axum-client-ip = "0.6.0"
lazy_static = "1.4.0"
local-ip-address = "0.6.1"
sha2 = "0.10.8"
hex = "0.4.3"

View file

@ -1,14 +1,19 @@
use crate::error::TransferNotFoundError;
use crate::transfer_info::transfer_info::TransferInfoRequest;
use hex;
use reqwest::Client;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::copy;
use tracing::{debug, error, info};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub async fn download_info(relay: &str, filename: &str) -> Result<TransferInfoRequest> {
match reqwest::get(format!("{}/download/{}", relay, filename)).await {
pub async fn download_info(relay: &str, name: &str) -> Result<TransferInfoRequest> {
let hashed_name = Sha256::digest(name.as_bytes());
let hashed_string = hex::encode(hashed_name);
match reqwest::get(format!("{}/download/{}", relay, hashed_string)).await {
Ok(resp) => {
let json = resp.json::<TransferInfoRequest>().await?;
debug!("Json Response: {:#?}", json);

View file

@ -7,7 +7,6 @@ use axum::{
Router,
};
use axum_client_ip::SecureClientIpSource;
use rand::{seq::SliceRandom, thread_rng};
use serde_json::json;
use std::{
env,
@ -76,7 +75,7 @@ async fn download_info(
) -> impl IntoResponse {
debug!("Get new download request from: {}", addr.ip().to_string());
let data = shared_state.data.lock().unwrap();
match data.iter().find(|request| request.name == name) {
match data.iter().find(|request| request.body.name == name) {
Some(request) => {
debug!("Found transfer name.");
(StatusCode::OK, Json(request.clone()))
@ -86,12 +85,12 @@ async fn download_info(
(
StatusCode::NOT_FOUND,
Json(TransferInfoRequest {
name: "".to_string(),
message: "error".to_string(),
body: TransferInfoBody {
keyword: "".to_string(),
files: "".to_string(),
ip: "".to_string(),
name: "".to_string(),
},
}),
)
@ -107,12 +106,12 @@ async fn upload_info(
debug!("Got upload request from {}", addr.ip().to_string());
let mut data = shared_state.data.lock().unwrap();
let t_request = TransferInfoRequest {
name: generate_random_name(),
message: "created".to_string(),
body: TransferInfoBody {
keyword: payload.keyword,
files: payload.files,
ip: payload.ip,
name: payload.name,
},
};
data.push(t_request.clone());
@ -135,30 +134,6 @@ async fn status() -> impl IntoResponse {
(StatusCode::OK, Json(response))
}
fn generate_random_name() -> String {
let mut rng = thread_rng();
let adjective = adjectives().choose(&mut rng).unwrap();
// let adjective = adjectives().sample(&mut rng).unwrap();
let noun1 = nouns1().choose(&mut rng).unwrap();
let noun2 = nouns2().choose(&mut rng).unwrap();
format!("{adjective}-{noun1}-{noun2}")
}
fn adjectives() -> &'static [&'static str] {
static ADJECTIVES: &[&str] = &["funny", "smart", "creative", "friendly", "great"];
ADJECTIVES
}
fn nouns1() -> &'static [&'static str] {
static NOUNS1: &[&str] = &["dog", "cat", "flower", "tree", "house"];
NOUNS1
}
fn nouns2() -> &'static [&'static str] {
static NOUNS2: &[&str] = &["cookie", "cake", "frosting"];
NOUNS2
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()

View file

@ -1,21 +1,28 @@
use crate::error::TransferNotCreatedError;
use crate::transfer_info::transfer_info::TransferInfoRequest;
use hex;
use local_ip_address;
use rand::{seq::SliceRandom, thread_rng};
use reqwest::{Client, StatusCode};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use tracing::{debug, error, info};
use tracing::debug;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub async fn send_info(relay: &str, file: &str) -> Result<String> {
let sender_ip = local_ip_address::local_ip().unwrap();
debug!("local ip is: {}", sender_ip);
let ip_str = sender_ip.to_owned().to_string();
let rand_name = generate_random_name();
let hashed_name = Sha256::digest(rand_name.as_bytes());
let hashed_string = hex::encode(hashed_name);
debug!("local ip is: {}", sender_ip);
debug!("Send Request to: {:?}", relay.to_string());
let mut map = HashMap::new();
map.insert("keyword", "test");
map.insert("files", file);
map.insert("ip", ip_str.as_str());
map.insert("name", hashed_string.as_str());
let client = Client::new();
let res = client
@ -26,10 +33,35 @@ pub async fn send_info(relay: &str, file: &str) -> Result<String> {
if res.status() == StatusCode::CREATED {
let transfer_info: TransferInfoRequest = res.json().await?;
debug!("Json Response: {:#?}", transfer_info);
Ok(transfer_info.name)
Ok(rand_name)
} else {
Err(Box::new(TransferNotCreatedError::new(
"Transfer could not be created.",
)))
}
}
fn generate_random_name() -> String {
let mut rng = thread_rng();
let adjective = adjectives().choose(&mut rng).unwrap();
// let adjective = adjectives().sample(&mut rng).unwrap();
let noun1 = nouns1().choose(&mut rng).unwrap();
let noun2 = nouns2().choose(&mut rng).unwrap();
format!("{adjective}-{noun1}-{noun2}")
}
fn adjectives() -> &'static [&'static str] {
static ADJECTIVES: &[&str] = &["funny", "smart", "creative", "friendly", "great"];
ADJECTIVES
}
fn nouns1() -> &'static [&'static str] {
static NOUNS1: &[&str] = &["dog", "cat", "flower", "tree", "house"];
NOUNS1
}
fn nouns2() -> &'static [&'static str] {
static NOUNS2: &[&str] = &["cookie", "cake", "frosting"];
NOUNS2
}

View file

@ -2,7 +2,6 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TransferInfoRequest {
pub name: String,
pub message: String,
pub body: TransferInfoBody,
}
@ -12,17 +11,18 @@ pub struct TransferInfoBody {
pub keyword: String,
pub files: String,
pub ip: String,
pub name: String,
}
impl TransferInfoRequest {
pub fn new() -> Self {
Self {
name: "".to_string(),
message: "".to_string(),
body: TransferInfoBody {
keyword: "".to_string(),
files: "".to_string(),
ip: "".to_string(),
name: "".to_string(),
},
}
}