1 extern crate actix_web
;
5 use listenfd
::ListenFd
;
7 use actix_web
::{web
, middleware
, App
, HttpServer
, HttpResponse
, Responder
, Result
, web
::Query
};
10 use std
::io
::prelude
::*;
11 use ron
::de
::from_reader
;
12 use serde
::Deserialize
;
13 use std
::{fs
::File
, env
::args
};
15 use itertools
::Itertools
;
21 #[template(path = "main.html")]
22 struct MainTemplate
<'a
> {
26 #[derive(Deserialize)]
31 static DEFAULT_MESSAGE
: &str = "Marc, roule un pet'!";
32 static KEY
: &str = "secret";
34 fn main_page(query
: Query
<Request
>) -> Result
<HttpResponse
> {
38 match crypto
::decrypt(KEY
, b
) {
40 None
=> String
::from(DEFAULT_MESSAGE
)
42 None
=> String
::from(DEFAULT_MESSAGE
)
45 let hello
= MainTemplate
{ sentence
: &m
};
47 let s
= hello
.render().unwrap();
48 Ok(HttpResponse
::Ok().content_type("text/html").body(s
))
51 #[derive(Debug, Deserialize)]
56 fn get_exe_name() -> String
{
57 let first_arg
= std
::env
::args().nth(0).unwrap();
59 let sep
: &[_
] = &['
\\'
, '
/'
];
60 first_arg
[first_arg
.rfind(sep
).unwrap()+1..].to_string()
65 println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
68 fn read_key() -> String
{
69 let mut key
= String
::new();
70 File
::open(consts
::FILE_KEY
)
71 .expect(&format!("Failed to open key file: {}", consts
::FILE_KEY
))
72 .read_to_string(&mut key
)
73 .expect(&format!("Failed to read key file: {}", consts
::FILE_KEY
));
76 url
::percent_encoding
::percent_decode(key
.as_bytes())
78 .expect(&format!("Failed to decode key file: {}", consts
::FILE_KEY
))
82 fn main() -> std
::io
::Result
<()> {
83 let args
: Vec
<String
> = args().collect();
87 if args
.iter().any(|arg
| arg
== "--help") {
90 } else if let Some((position_arg_encrypt
, _
)) = args
.iter().find_position(|arg
| arg
== &"--encrypt") {
91 match args
.iter().nth(position_arg_encrypt
+ 1) {
92 Some(mess_to_encrypt
) => {
94 let encrypted_mess
= crypto
::encrypt(&key
, mess_to_encrypt
);
95 let encrypted_mess_encoded
= url
::percent_encoding
::utf8_percent_encode(&encrypted_mess
, url
::percent_encoding
::DEFAULT_ENCODE_SET
).to_string();
96 println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded
);
102 } else if let Some((position_arg_decrypt
, _
)) = args
.iter().find_position(|arg
| arg
== &"--decrypt") {
103 match args
.iter().nth(position_arg_decrypt
+ 1) {
104 Some(cipher_text
) => {
105 let cipher_text_decoded
= url
::percent_encoding
::percent_decode(cipher_text
.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
106 let plain_text
= crypto
::decrypt(&key
, &cipher_text_decoded
).unwrap();
107 println!("Decrypted message: {}", plain_text
);
109 None
=> print_usage()
115 println!("Starting RUP as web server...");
117 let config
: Config
= {
118 let f
= File
::open(consts
::FILE_CONF
).expect(&format!("Failed to open configuration file {}", consts
::FILE_CONF
));
119 match from_reader(f
) {
121 Err(e
) => panic!("Failed to load config: {}", e
)
125 println!("Configuration: {:?}", config
);
127 let mut listenfd
= ListenFd
::from_env();
132 .wrap(middleware
::Compress
::default())
133 .wrap(middleware
::Logger
::default())
134 .service(web
::resource("/").to(main_page
))
135 .service(fs
::Files
::new("/static", "static").show_files_listing())
140 if let Some(l
) = listenfd
.take_tcp_listener(0).unwrap() {
141 server
.listen(l
).unwrap()
143 server
.bind(&format!("0.0.0.0:{}", config
.port
)).unwrap()