X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=15b9c025f67bb86acb9d93be0d2b1e89c0272c07;hb=HEAD;hp=b86ce5762185ed4e6c7683037f2b692f9620cf7d;hpb=b1f2c5b803a7e85a3b0c8d999cf9a13d28c6c6c2;p=rup.git diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index b86ce57..0000000 --- a/src/main.rs +++ /dev/null @@ -1,147 +0,0 @@ -extern crate actix_web; -extern crate listenfd; -extern crate askama; - -use listenfd::ListenFd; -use actix_files as fs; -use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query}; -use askama::Template; - -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")] -struct MainTemplate<'a> { - sentence: &'a str, -} - -#[derive(Deserialize)] -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) => - match crypto::decrypt(KEY, b) { - Some(m) => m, - None => String::from(DEFAULT_MESSAGE) - }, - None => String::from(DEFAULT_MESSAGE) - }; - - 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( - || { - App::new() - .wrap(middleware::Compress::default()) - .wrap(middleware::Logger::default()) - .service(web::resource("/").to(main_page)) - .service(fs::Files::new("/static", "static").show_files_listing()) - } - ); - - server = - if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { - server.listen(l).unwrap() - } else { - server.bind(&format!("0.0.0.0:{}", config.port)).unwrap() - }; - - server.run() -}