1 extern crate actix_web
;
4 extern crate percent_encoding
;
6 use listenfd
::ListenFd
;
8 use actix_web
::{web
, middleware
, App
, HttpServer
, HttpResponse
, Result
, web
::Query
};
11 use std
::io
::prelude
::*;
12 use ron
::de
::from_reader
;
13 use serde
::Deserialize
;
14 use std
::{fs
::File
, env
::args
};
16 use itertools
::Itertools
;
22 #[template(path = "main.html")]
23 struct MainTemplate
<'a
> {
27 #[derive(Deserialize)]
32 fn main_page(query
: Query
<Request
>, key
: &str) -> Result
<HttpResponse
> {
36 match crypto
::decrypt(key
, b
) {
38 Err(_e
) => String
::from(consts
::DEFAULT_MESSAGE
) // TODO: log error.
40 None
=> String
::from(consts
::DEFAULT_MESSAGE
)
43 let hello
= MainTemplate
{ sentence
: &m
};
45 let s
= hello
.render().unwrap();
46 Ok(HttpResponse
::Ok().content_type("text/html").body(s
))
49 #[derive(Debug, Deserialize)]
54 fn get_exe_name() -> String
{
55 let first_arg
= std
::env
::args().nth(0).unwrap();
56 let sep
: &[_
] = &['
\\'
, '
/'
];
57 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
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
));
68 percent_encoding
::percent_decode(key
.replace('
\n'
, "").as_bytes())
70 .unwrap_or_else(|_
| panic!("Failed to decode key file: {}", consts
::FILE_KEY
))
74 fn main() -> std
::io
::Result
<()> {
77 if process_args(&key
) { return Ok(()) }
79 println!("Starting RUP as web server...");
81 let config
: Config
= {
82 let f
= File
::open(consts
::FILE_CONF
).unwrap_or_else(|_
| panic!("Failed to open configuration file {}", consts
::FILE_CONF
));
83 match from_reader(f
) {
85 Err(e
) => panic!("Failed to load config: {}", e
)
89 println!("Configuration: {:?}", config
);
91 let mut listenfd
= ListenFd
::from_env();
95 let key
= key
.clone(); // Is this neccessary??
98 .wrap(middleware
::Compress
::default())
99 .wrap(middleware
::Logger
::default())
100 .service(web
::resource("/").to(move |query
| main_page(query
, &key
)))
101 .service(fs
::Files
::new("/static", "static").show_files_listing())
106 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
107 server
.listen(l
).unwrap()
109 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()
115 fn process_args(key
: &str) -> bool
{
118 println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
121 let args
: Vec
<String
> = args().collect();
123 if args
.iter().any(|arg
| arg
== "--help") {
126 } else if let Some((position_arg_encrypt
, _
)) = args
.iter().find_position(|arg
| arg
== &"--encrypt") {
127 match args
.get(position_arg_encrypt
+ 1) {
128 Some(mess_to_encrypt
) => {
129 match crypto
::encrypt(&key
, mess_to_encrypt
) {
130 Ok(encrypted_mess
) => {
131 let encrypted_mess_encoded
= percent_encoding
::utf8_percent_encode(&encrypted_mess
, percent_encoding
::NON_ALPHANUMERIC
).to_string();
132 println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded
); },
134 println!("Unable to encrypt: {:?}", error
)
137 None
=> print_usage()
141 } else if let Some((position_arg_decrypt
, _
)) = args
.iter().find_position(|arg
| arg
== &"--decrypt") {
142 match args
.get(position_arg_decrypt
+ 1) {
143 Some(cipher_text
) => {
144 let cipher_text_decoded
= percent_encoding
::percent_decode(cipher_text
.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
145 match crypto
::decrypt(&key
, &cipher_text_decoded
) {
147 println!("Decrypted message: {}", plain_text
),
149 println!("Unable to decrypt: {:?}", error
)
152 None
=> print_usage()