phlog

Source code for my blog/gemlog. It used to be on gopher, hence the name
git clone http://shtanton.xyz/git/repo/phlog
Log | Files | Refs

commit 023134c31cb39d57d32fa2cf7f9e238b5a4c6403
parent 63ba233b6c52be58db1ade9b5529b0b77de722b5
Author: Charlie Stanton <charlie@shtanton.com>
Date:   Wed,  5 Aug 2020 15:41:19 +0100

Adds ssl to the web server

Diffstat:
Mweb-server/Cargo.lock | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb-server/Cargo.toml | 3++-
Mweb-server/src/main.rs | 13++++++++++---
3 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/web-server/Cargo.lock b/web-server/Cargo.lock @@ -30,6 +30,8 @@ dependencies = [ "futures", "http", "log", + "openssl", + "tokio-openssl", "trust-dns-proto", "trust-dns-resolver", ] @@ -66,6 +68,7 @@ dependencies = [ "actix-rt", "actix-service", "actix-threadpool", + "actix-tls", "actix-utils", "base64", "bitflags", @@ -213,6 +216,8 @@ dependencies = [ "either", "futures", "log", + "openssl", + "tokio-openssl", ] [[package]] @@ -260,6 +265,7 @@ dependencies = [ "log", "mime", "net2", + "openssl", "pin-project", "regex", "serde", @@ -343,6 +349,7 @@ dependencies = [ "futures-core", "log", "mime", + "openssl", "percent-encoding", "rand", "serde", @@ -549,6 +556,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] name = "fuchsia-zircon" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1022,6 +1044,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" [[package]] +name = "openssl" +version = "0.10.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "lazy_static", + "libc", + "openssl-sys", +] + +[[package]] +name = "openssl-sys" +version = "0.9.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] name = "parking_lot" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1086,6 +1135,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] +name = "pkg-config" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" + +[[package]] name = "ppv-lite86" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1381,6 +1436,16 @@ dependencies = [ ] [[package]] +name = "tokio-openssl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c4b08c5f4208e699ede3df2520aca2e82401b2de33f45e96696a074480be594" +dependencies = [ + "openssl", + "tokio", +] + +[[package]] name = "tokio-util" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1549,6 +1614,12 @@ dependencies = [ ] [[package]] +name = "vcpkg" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" + +[[package]] name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1573,6 +1644,7 @@ dependencies = [ "actix-files", "actix-rt", "actix-web", + "openssl", ] [[package]] diff --git a/web-server/Cargo.toml b/web-server/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -actix-web = "2.0" +actix-web = {version="2.0", features=["openssl"]} actix-rt = "1.0" actix-files = "0.2.0" +openssl = "0.10" diff --git a/web-server/src/main.rs b/web-server/src/main.rs @@ -1,6 +1,7 @@ use actix_files::NamedFile; use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Result, http}; use std::path::PathBuf; +use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; struct AppState { htmldir: String, @@ -20,15 +21,21 @@ async fn main() -> std::io::Result<()> { return Ok(()); } let htmldir = args[1].to_string(); - HttpServer::new(move || { + let mut server = HttpServer::new(move || { App::new() .data(AppState { htmldir: htmldir.clone(), }) .route("/", web::get().to(|| HttpResponse::PermanentRedirect().header(http::header::LOCATION, "/index.html").finish())) .route("/{filename:.*}", web::get().to(file)) - }) - .bind(format!("127.0.0.1:{}", args[2]))? + }); + if let Some(port) = args.get(3) { + let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); + builder.set_private_key_file("/etc/letsencrypt/live/shtanton.com/privkey.pem", SslFiletype::PEM).unwrap(); + builder.set_certificate_chain_file("/etc/letsencrypt/live/shtanton.com/fullchain.pem").unwrap(); + server = server.bind_openssl(format!("0.0.0.0:{}", port), builder)? + } + server.bind(format!("0.0.0.0:{}", args[2]))? .run() .await }