diff --git a/Cargo.toml b/Cargo.toml index d161c9d..bbfa5f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,15 @@ -[workspace] -members = [ - "caesar_cli", "caesar_core", "caesar_server", -] +[package] +name = "caesar" +version = "0.0.1" +edition = "2021" +authors = ["Manuel Keidel", "Patryk Hegenberg", "Krzysztof Stankiewicz"] -[workspace.dependencies] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.5.4", features = ["derive"] } +# reqwest = { version = "0.12.3", features = ["blocking", "json"] } +hyper = { version = "1", features = ["full"] } +tokio = { version = "1", features = ["full"] } +# http-body-util = "0.1" +# hyper-util = { version = "0.1", features = ["full"] } diff --git a/caesar_cli/Cargo.toml b/caesar_cli/Cargo.toml deleted file mode 100644 index f9996d4..0000000 --- a/caesar_cli/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "caesar_cli" -version = "0.0.1" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[[bin]] -name = "caesar" -path = "src/main.rs" - -[dependencies] -clap = { version = "4.5.4", features = ["derive"] } -reqwest = { version = "0.12.3", features = ["blocking", "json"] } diff --git a/caesar_cli/src/cli.rs b/caesar_cli/src/cli.rs deleted file mode 100644 index e243ac5..0000000 --- a/caesar_cli/src/cli.rs +++ /dev/null @@ -1,67 +0,0 @@ -use clap::{Parser, Subcommand}; -use reqwest::blocking::Client; - -use crate::command; - -#[derive(Parser)] -#[command(version, about, long_about = None)] -pub struct Cli { - /// Sets a custom config file - #[arg(short, long, value_name = "FILE")] - pub config: Option, - - /// Turn debugging information on - #[arg(short, long, action = clap::ArgAction::Count)] - pub debug: u8, - - /// Name of Transfer to download files - #[arg(short, long, value_name = "Transfer_Name")] - pub name: Option, - - #[command(subcommand)] - pub command: Option, -} - -#[derive(Subcommand)] -pub enum Commands { - /// Files to Send - Send { - /// Path to file(s) - #[arg(short, long, value_name = "FILE")] - file: Option, - }, -} - -impl Cli { - pub fn new() -> Self { - Self::parse() - } - pub fn handle_cli_args(&self, client: Client) -> Result<(), Box> { - if let Some(config_path) = self.config.as_deref() { - println!("Value for config: {}", config_path); - } - match self.debug { - 0 => println!("Debug mode is off"), - 1 => println!("Debug mode is kind of on"), - 2 => println!("Debug mode is on"), - _ => println!("Don't be crazy"), - } - - match &self.command { - Some(Commands::Send { file }) => { - command::send_info(client, file.as_deref().unwrap_or("test.txt"))?; - } - None => { - let filename = self.name.as_deref().unwrap_or("None"); - command::download_info(client, filename)? - } - } - Ok(()) - } -} - -impl Default for Cli { - fn default() -> Self { - Self::new() - } -} diff --git a/caesar_cli/src/command.rs b/caesar_cli/src/command.rs deleted file mode 100644 index 35df2f3..0000000 --- a/caesar_cli/src/command.rs +++ /dev/null @@ -1,33 +0,0 @@ -use reqwest::{blocking::Client, StatusCode}; -use std::collections::HashMap; - -pub fn send_info(client: Client, file: &str) -> Result<(), Box> { - let mut map = HashMap::new(); - map.insert("keyword", "test"); - map.insert("files", file); - - let res = client - .post("http://192.168.178.43:1323/upload") - .json(&map) - .send()?; - - if res.status() == StatusCode::OK { - let json: HashMap = res.json()?; - println!("JSON Response: {:?}", json); - } else { - println!("Error: Failed to send request"); - } - Ok(()) -} - -pub fn download_info(client: Client, filename: &str) -> Result<(), Box> { - let res = client - .get(format!("http://192.168.178.43:1323/download/{}", filename)) - .send()?; - - if res.status() == StatusCode::OK { - let json: HashMap = res.json()?; - println!("JSON Response: {:?}", json); - } - Ok(()) -} diff --git a/caesar_cli/src/main.rs b/caesar_cli/src/main.rs deleted file mode 100644 index 5948f94..0000000 --- a/caesar_cli/src/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -mod cli; -mod command; -use reqwest::blocking::Client; - -pub use crate::cli::*; - -fn main() { - let client = Client::new(); - let cli = cli::Cli::new(); - - if let Err(e) = cli.handle_cli_args(client) { - eprintln!("Error: {e}"); - } -} diff --git a/caesar_server/Cargo.toml b/caesar_server/Cargo.toml deleted file mode 100644 index 07f3d11..0000000 --- a/caesar_server/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "caesar_server" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/caesar_server/src/main.rs b/caesar_server/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/caesar_server/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..89e43ac --- /dev/null +++ b/src/args.rs @@ -0,0 +1,89 @@ +use crate::receiver; +use crate::sender; +use clap::{Parser, Subcommand}; +use reqwest::blocking::Client; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +pub struct Args { + #[command(subcommand)] + pub command: Option, +} + +#[derive(Subcommand)] +pub enum Commands { + Send { + /// Address of the relay server. Accepted formats are: 127.0.0.1:8080, [::1]:8080, example.com + #[arg(short, long)] + relay: bool, + /// Path to file(s) + #[arg(short, long, value_name = "FILE")] + file: Option, + }, + /// Receives Files from the sender with the matching password + Receive { + /// Address of the relay server. Accepted formats are: 127.0.0.1:8080, [::1]:8080, example.com + #[arg(short, long)] + relay: bool, + + /// Overwrite existing Files + #[arg(short, long)] + overwrite: bool, + + /// Name of Transfer to download files + #[arg(short, long, value_name = "Transfer_Name")] + name: Option, + }, + Serve { + /// Port to run the relay server on + #[arg(short, long)] + port: bool, + }, + Config { + /// Show path to config file + #[arg(short, long)] + path: bool, + + /// View configured Options + #[arg(short, long)] + show: bool, + + /// Edit the config file + #[arg(short, long)] + edit: bool, + + /// Reset changed config + #[arg(short, long)] + reset: bool, + }, +} + +impl Args { + pub fn new() -> Self { + Self::parse() + } + pub fn run(&self, client: Client) -> Result<(), Box> { + match &self.command { + Some(Commands::Send { relay, file }) => { + sender::send_info(client, file.as_deref().unwrap_or("test.txt"))?; + } + Some(Commands::Receive { + relay, + overwrite, + name, + }) => { + let transfer_name = name.as_deref().unwrap_or("None"); + receiver::download_info(client, transfer_name)? + } + Some(Commands::Serve { port }) => {} + Some(Commands::Config { + path, + show, + edit, + reset, + }) => {} + None => {} + } + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c6ddb4a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,13 @@ +use reqwest::blocking::Client; + +mod args; +pub mod receiver; +pub mod sender; + +fn main() { + let client = Client::new(); + let args = args::Args::new(); + if let Err(e) = args.run(client) { + eprintln!("Error {e}"); + } +} diff --git a/src/password.rs b/src/password.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/receiver.rs b/src/receiver.rs new file mode 100644 index 0000000..76fee0a --- /dev/null +++ b/src/receiver.rs @@ -0,0 +1,16 @@ +use reqwest::{blocking::Client, StatusCode}; +use std::collections::HashMap; + +type Result = std::result::Result>; + +pub fn download_info(client: Client, filename: &str) -> Result<()> { + let res = client + .get(format!("http://192.168.178.43:1323/download/{}", filename)) + .send()?; + + if res.status() == StatusCode::OK { + let json: HashMap = res.json()?; + println!("JSON Response: {:?}", json); + } + Ok(()) +} diff --git a/src/sender.rs b/src/sender.rs new file mode 100644 index 0000000..ed3644c --- /dev/null +++ b/src/sender.rs @@ -0,0 +1,23 @@ +use reqwest::{blocking::Client, StatusCode}; +use std::collections::HashMap; + +type Result = std::result::Result>; + +pub fn send_info(client: Client, file: &str) -> Result<()> { + let mut map = HashMap::new(); + map.insert("keyword", "test"); + map.insert("files", file); + + let res = client + .post("http://192.168.178.43:1323/upload") + .json(&map) + .send()?; + + if res.status() == StatusCode::OK { + let json: HashMap = res.json()?; + println!("JSON Response: {:?}", json); + } else { + println!("Error: Faile to send request"); + } + Ok(()) +} diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..3abb8a6 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,10 @@ +use std::convert::Infallible; +use std::net::SocketAddr; + +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::server::conn::http1; +use hyper::service::service_fn; +use hyper::{Request, Response}; +use hyper_util::rt::TokioIo; +use tokio::net::TcpListener;