X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=b86ce5762185ed4e6c7683037f2b692f9620cf7d;hb=b1f2c5b803a7e85a3b0c8d999cf9a13d28c6c6c2;hp=24cfdbb51886fee26528c4463728abd69ab1fabc;hpb=8834567b2f53bad60b9d77ff7970077f9af0888d;p=rup.git diff --git a/src/main.rs b/src/main.rs index 24cfdbb..b86ce57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,16 @@ use listenfd::ListenFd; use actix_files as fs; use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query}; use askama::Template; -use serde::{Deserialize}; + +use std::io::prelude::*; +use ron::de::from_reader; +use serde::Deserialize; +use std::{fs::File, env::args}; + +use itertools::Itertools; + +mod consts; +mod crypto; #[derive(Template)] #[template(path = "main.html")] @@ -19,20 +28,102 @@ pub struct Request { m: Option } +static DEFAULT_MESSAGE: &str = "Marc, roule un pet'!"; +static KEY: &str = "secret"; + fn main_page(query: Query) -> Result { let m = match &query.m { - Some(b) => &b, - None => "Marc, roule un pet'!" + Some(b) => + match crypto::decrypt(KEY, b) { + Some(m) => m, + None => String::from(DEFAULT_MESSAGE) + }, + None => String::from(DEFAULT_MESSAGE) }; - let hello = MainTemplate { sentence: m }; + let hello = MainTemplate { sentence: &m }; let s = hello.render().unwrap(); Ok(HttpResponse::Ok().content_type("text/html").body(s)) } +#[derive(Debug, Deserialize)] +struct Config { + port: u16 +} + +fn get_exe_name() -> String { + let first_arg = std::env::args().nth(0).unwrap(); + //dbg!(&first_arg); + let sep: &[_] = &['\\', '/']; + first_arg[first_arg.rfind(sep).unwrap()+1..].to_string() +} + +fn print_usage() { + println!("Usage:"); + println!(" {} [--help] [--encrypt |--decrypt ]", get_exe_name()); +} + +fn read_key() -> String { + let mut key = String::new(); + File::open(consts::FILE_KEY) + .expect(&format!("Failed to open key file: {}", consts::FILE_KEY)) + .read_to_string(&mut key) + .expect(&format!("Failed to read key file: {}", consts::FILE_KEY)); + + String::from( + url::percent_encoding::percent_decode(key.as_bytes()) + .decode_utf8() + .expect(&format!("Failed to decode key file: {}", consts::FILE_KEY)) + ) +} + fn main() -> std::io::Result<()> { + let args: Vec = args().collect(); + + let key = read_key(); + + if args.iter().any(|arg| arg == "--help") { + print_usage(); + return Ok(()); + } else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") { + match args.iter().nth(position_arg_encrypt + 1) { + Some(mess_to_encrypt) => { + + let encrypted_mess = crypto::encrypt(&key, mess_to_encrypt); + let encrypted_mess_encoded = url::percent_encoding::utf8_percent_encode(&encrypted_mess, url::percent_encoding::DEFAULT_ENCODE_SET).to_string(); + println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded); + } + None => print_usage() + } + + return Ok(()); + } else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") { + match args.iter().nth(position_arg_decrypt + 1) { + Some(cipher_text) => { + let cipher_text_decoded = url::percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text"); + let plain_text = crypto::decrypt(&key, &cipher_text_decoded).unwrap(); + println!("Decrypted message: {}", plain_text); + } + None => print_usage() + } + + return Ok(()); + } + + println!("Starting RUP as web server..."); + + let config: Config = { + let f = File::open(consts::FILE_CONF).expect(&format!("Failed to open configuration file {}", consts::FILE_CONF)); + match from_reader(f) { + Ok(c) => c, + Err(e) => panic!("Failed to load config: {}", e) + } + }; + + println!("Configuration: {:?}", config); + let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new( @@ -49,7 +140,7 @@ fn main() -> std::io::Result<()> { if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { server.listen(l).unwrap() } else { - server.bind("0.0.0.0:8082").unwrap() + server.bind(&format!("0.0.0.0:{}", config.port)).unwrap() }; server.run()