2023-05-22 14:06:04 +01:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::{self, Read as _},
|
|
|
|
};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-03-06 23:16:56 -08:00
|
|
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
2023-05-22 14:06:04 +01:00
|
|
|
use openssl::{
|
|
|
|
pkey::{PKey, Private},
|
|
|
|
ssl::{SslAcceptor, SslMethod},
|
|
|
|
};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
/// simple handle
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
2022-06-07 22:53:38 -04:00
|
|
|
println!("{req:?}");
|
2018-04-13 09:18:42 +08:00
|
|
|
Ok(HttpResponse::Ok()
|
2018-05-08 11:08:43 -07:00
|
|
|
.content_type("text/plain")
|
2023-05-22 14:06:04 +01:00
|
|
|
.body("Hello HTTPS World!"))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn main() -> io::Result<()> {
|
2023-03-14 03:11:49 +00:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2023-05-22 14:06:04 +01:00
|
|
|
// build TLS config from files
|
2018-04-13 09:18:42 +08:00
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2023-05-22 14:06:04 +01:00
|
|
|
|
|
|
|
// set the encrypted private key
|
2018-05-08 11:08:43 -07:00
|
|
|
builder
|
2023-05-22 14:06:04 +01:00
|
|
|
.set_private_key(&load_encrypted_private_key())
|
2018-05-08 11:08:43 -07:00
|
|
|
.unwrap();
|
2023-05-22 14:06:04 +01:00
|
|
|
|
|
|
|
// set the unencrypted private key
|
|
|
|
// (uncomment if you generate your own key+cert with `mkcert`, and also remove the statement above)
|
2023-05-22 14:18:07 +01:00
|
|
|
// builder
|
|
|
|
// .set_private_key_file("key.pem", openssl::ssl::SslFiletype::PEM)
|
|
|
|
// .unwrap();
|
2023-05-22 14:06:04 +01:00
|
|
|
|
|
|
|
// set the certificate chain file location
|
2018-05-20 21:03:29 -07:00
|
|
|
builder.set_certificate_chain_file("cert.pem").unwrap();
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2023-03-14 03:11:49 +00:00
|
|
|
log::info!("starting HTTPS server at http://localhost:8443");
|
|
|
|
|
2019-03-06 23:16:56 -08:00
|
|
|
HttpServer::new(|| {
|
2018-05-08 11:08:43 -07:00
|
|
|
App::new()
|
2018-04-13 09:18:42 +08:00
|
|
|
// enable logger
|
2019-03-26 04:29:00 +01:00
|
|
|
.wrap(middleware::Logger::default())
|
2023-05-22 14:06:04 +01:00
|
|
|
// simple root handler
|
|
|
|
.service(web::resource("/").route(web::get().to(index)))
|
2019-03-06 23:16:56 -08:00
|
|
|
})
|
2019-12-07 23:59:24 +06:00
|
|
|
.bind_openssl("127.0.0.1:8443", builder)?
|
2023-05-22 14:06:04 +01:00
|
|
|
.workers(2)
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
2023-05-22 14:06:04 +01:00
|
|
|
|
|
|
|
fn load_encrypted_private_key() -> PKey<Private> {
|
|
|
|
let mut file = File::open("key.pem").unwrap();
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
file.read_to_end(&mut buffer).expect("Failed to read file");
|
|
|
|
|
|
|
|
PKey::private_key_from_pem_passphrase(&buffer, b"password").unwrap()
|
|
|
|
}
|