extern crate actix_web;
extern crate listenfd;
extern crate askama;
+extern crate percent_encoding;
use listenfd::ListenFd;
use actix_files as fs;
m: Option<String>
}
-static DEFAULT_MESSAGE: &str = "Marc, roule un pet'!";
-static KEY: &str = "secret";
-
fn main_page(query: Query<Request>) -> Result<HttpResponse> {
let m =
match &query.m {
Some(b) =>
match crypto::decrypt(KEY, b) {
- Some(m) => m,
- None => String::from(DEFAULT_MESSAGE)
+ Ok(m) => m,
+ Err(_e) => String::from(consts::DEFAULT_MESSAGE) // TODO: log error.
},
- None => String::from(DEFAULT_MESSAGE)
+ None => String::from(consts::DEFAULT_MESSAGE)
};
let hello = MainTemplate { sentence: &m };
let sep: &[_] = &['\\', '/'];
first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
}
-
-fn print_usage() {
- println!("Usage:");
- println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
-}
-
fn read_key() -> String {
let mut key = String::new();
File::open(consts::FILE_KEY)
.expect(&format!("Failed to read key file: {}", consts::FILE_KEY));
String::from(
- url::percent_encoding::percent_decode(key.as_bytes())
+ 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<String> = 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(());
- }
+ if process_args(&key) { return Ok(()) }
println!("Starting RUP as web server...");
server.run()
}
+
+fn process_args(key: &String) -> bool {
+ fn print_usage() {
+ println!("Usage:");
+ println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
+ }
+
+ let args: Vec<String> = args().collect();
+
+ if args.iter().any(|arg| arg == "--help") {
+ print_usage();
+ return true
+ } 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) => {
+ match crypto::encrypt(&key, mess_to_encrypt) {
+ Ok(encrypted_mess) => {
+ let encrypted_mess_encoded = percent_encoding::utf8_percent_encode(&encrypted_mess, percent_encoding::NON_ALPHANUMERIC).to_string();
+ println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded); },
+ Err(error) =>
+ println!("Unable to encrypt: {:?}", error)
+ }
+ }
+ None => print_usage()
+ }
+
+ return true
+ } 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 = percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
+ match crypto::decrypt(&key, &cipher_text_decoded) {
+ Ok(plain_text) =>
+ println!("Decrypted message: {}", plain_text),
+ Err(error) =>
+ println!("Unable to decrypt: {:?}", error)
+ }
+ }
+ None => print_usage()
+ }
+
+ return true
+ }
+
+ return false
+}