Add a new format version (2) for encrypted text.
[rup.git] / src / main.rs
1 extern crate actix_web;
2 extern crate listenfd;
3 extern crate askama;
4 extern crate percent_encoding;
5
6 use listenfd::ListenFd;
7 use actix_files as fs;
8 use actix_web::{web, middleware, App, HttpServer, HttpResponse, web::Query};
9 use askama::Template;
10
11 use std::io::prelude::*;
12 use ron::de::from_reader;
13 use serde::Deserialize;
14 use std::{fs::File, env::args};
15
16 use itertools::Itertools;
17
18 mod consts;
19 mod crypto;
20
21 #[derive(Template)]
22 #[template(path = "main.html")]
23 struct MainTemplate<'a> {
24 sentence: &'a str,
25 }
26
27 #[derive(Deserialize)]
28 pub struct Request {
29 m: Option<String>
30 }
31
32 fn main_page(query: Query<Request>, key: &str) -> HttpResponse {
33 let m =
34 match &query.m {
35 Some(b) =>
36 match crypto::decrypt(key, b) {
37 Ok(m) => m,
38 Err(_e) => String::from(consts::DEFAULT_MESSAGE) // TODO: log error.
39 },
40 None => String::from(consts::DEFAULT_MESSAGE)
41 };
42
43 let hello = MainTemplate { sentence: &m };
44
45 let s = hello.render().unwrap();
46 HttpResponse::Ok().content_type("text/html").body(s)
47 }
48
49 #[derive(Debug, Deserialize)]
50 struct Config {
51 port: u16
52 }
53
54 fn get_exe_name() -> String {
55 let first_arg = std::env::args().next().unwrap();
56 let sep: &[_] = &['\\', '/'];
57 first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
58 }
59
60 fn read_key() -> String {
61 let mut key = String::new();
62 File::open(consts::FILE_KEY)
63 .unwrap_or_else(|_| panic!("Failed to open key file: {}", consts::FILE_KEY))
64 .read_to_string(&mut key)
65 .unwrap_or_else(|_| panic!("Failed to read key file: {}", consts::FILE_KEY));
66
67 String::from(
68 percent_encoding::percent_decode(key.replace('\n', "").as_bytes())
69 .decode_utf8()
70 .unwrap_or_else(|_| panic!("Failed to decode key file: {}", consts::FILE_KEY))
71 )
72 }
73
74 #[actix_rt::main]
75 async fn main() -> std::io::Result<()> {
76 let key = read_key();
77
78 if process_args(&key) { return Ok(()) }
79
80 println!("Starting RUP as web server...");
81
82 let config: Config = {
83 let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
84 match from_reader(f) {
85 Ok(c) => c,
86 Err(e) => panic!("Failed to load config: {}", e)
87 }
88 };
89
90 println!("Configuration: {:?}", config);
91
92 let mut listenfd = ListenFd::from_env();
93 let mut server =
94 HttpServer::new(
95 move || {
96 let key = key.clone(); // Is this neccessary??
97
98 App::new()
99 .wrap(middleware::Compress::default())
100 .wrap(middleware::Logger::default())
101 .service(web::resource("/").to(move |query| main_page(query, &key)))
102 .service(fs::Files::new("/static", "static").show_files_listing())
103 }
104 );
105
106 server =
107 if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
108 server.listen(l).unwrap()
109 } else {
110 server.bind(&format!("0.0.0.0:{}", config.port)).unwrap()
111 };
112
113 server.run().await
114 }
115
116 fn process_args(key: &str) -> bool {
117 fn print_usage() {
118 println!("Usage:");
119 println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
120 }
121
122 let args: Vec<String> = args().collect();
123
124 if args.iter().any(|arg| arg == "--help") {
125 print_usage();
126 return true
127 } else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") {
128 match args.get(position_arg_encrypt + 1) {
129 Some(mess_to_encrypt) => {
130 // Encrypt to version 2 (version 1 is obsolete).
131 match crypto::encrypt(&key, mess_to_encrypt, 2) {
132 Ok(encrypted_mess) => {
133 let encrypted_mess_encoded = percent_encoding::utf8_percent_encode(&encrypted_mess, percent_encoding::NON_ALPHANUMERIC).to_string();
134 println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded); },
135 Err(error) =>
136 println!("Unable to encrypt: {:?}", error)
137 }
138 }
139 None => print_usage()
140 }
141
142 return true
143 } else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") {
144 match args.get(position_arg_decrypt + 1) {
145 Some(cipher_text) => {
146 let cipher_text_decoded = percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
147 match crypto::decrypt(&key, &cipher_text_decoded) {
148 Ok(plain_text) =>
149 println!("Decrypted message: {}", plain_text),
150 Err(error) =>
151 println!("Unable to decrypt: {:?}", error)
152 }
153 }
154 None => print_usage()
155 }
156
157 return true
158 }
159
160 false
161 }