From ca32d687690d5f401364983c8cb52479b55d7344 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 7 Aug 2018 22:34:16 -0700 Subject: [PATCH] rustls example --- .travis.yml | 1 + Cargo.toml | 1 + rustls/Cargo.toml | 15 ++++++++++++ rustls/README.md | 16 +++++++++++++ rustls/cert.pem | 31 ++++++++++++++++++++++++ rustls/key.pem | 51 +++++++++++++++++++++++++++++++++++++++ rustls/src/main.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+) create mode 100644 rustls/Cargo.toml create mode 100644 rustls/README.md create mode 100644 rustls/cert.pem create mode 100644 rustls/key.pem create mode 100644 rustls/src/main.rs diff --git a/.travis.yml b/.travis.yml index 9bd4f006..3e7ad3de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -57,6 +57,7 @@ script: cd template_askama && cargo check && cd .. cd template_tera && cargo check && cd .. cd tls && cargo check && cd .. + cd rustls && cargo check && cd .. cd unix-socket && cargo check && cd .. cd web-cors/backend && cargo check && cd ../.. cd websocket && cargo check && cd .. diff --git a/Cargo.toml b/Cargo.toml index 17ed4eb5..0d7ca7d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ members = [ "template_askama", "template_tera", "tls", + "rustls", "unix-socket", "web-cors/backend", "websocket", diff --git a/rustls/Cargo.toml b/rustls/Cargo.toml new file mode 100644 index 00000000..66faee26 --- /dev/null +++ b/rustls/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rustls-example" +version = "0.1.0" +authors = ["Nikolay Kim "] +workspace = "../" + +[[bin]] +name = "server" +path = "src/main.rs" + +[dependencies] +env_logger = "0.5" +rustls = "0.13" +actix = "0.7" +actix-web = { git = "https://github.com/actix/actix-web.git", features=["rust-tls"] } diff --git a/rustls/README.md b/rustls/README.md new file mode 100644 index 00000000..a32f0525 --- /dev/null +++ b/rustls/README.md @@ -0,0 +1,16 @@ +# tls example + +## Usage + +### server + +```bash +cd examples/rustls +cargo run (or ``cargo watch -x run``) +# Started http server: 127.0.0.1:8443 +``` + +### web client + +- curl: ``curl -v https://127.0.0.1:8443/index.html --compressed -k`` +- browser: [https://127.0.0.1:8443/index.html](https://127.0.0.1:8443/index.html) diff --git a/rustls/cert.pem b/rustls/cert.pem new file mode 100644 index 00000000..159aacea --- /dev/null +++ b/rustls/cert.pem @@ -0,0 +1,31 @@ +-----BEGIN CERTIFICATE----- +MIIFPjCCAyYCCQDvLYiYD+jqeTANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJV +UzELMAkGA1UECAwCQ0ExCzAJBgNVBAcMAlNGMRAwDgYDVQQKDAdDb21wYW55MQww +CgYDVQQLDANPcmcxGDAWBgNVBAMMD3d3dy5leGFtcGxlLmNvbTAeFw0xODAxMjUx +NzQ2MDFaFw0xOTAxMjUxNzQ2MDFaMGExCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJD +QTELMAkGA1UEBwwCU0YxEDAOBgNVBAoMB0NvbXBhbnkxDDAKBgNVBAsMA09yZzEY +MBYGA1UEAwwPd3d3LmV4YW1wbGUuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A +MIICCgKCAgEA2WzIA2IpVR9Tb9EFhITlxuhE5rY2a3S6qzYNzQVgSFggxXEPn8k1 +sQEcer5BfAP986Sck3H0FvB4Bt/I8PwOtUCmhwcc8KtB5TcGPR4fjXnrpC+MIK5U +NLkwuyBDKziYzTdBj8kUFX1WxmvEHEgqToPOZfBgsS71cJAR/zOWraDLSRM54jXy +voLZN4Ti9rQagQrvTQ44Vz5ycDQy7UxtbUGh1CVv69vNVr7/SOOh/Nw5FNOZWLWr +odGyoec5wh9iqRZgRqiTUc6Lt7V2RWc2X2gjwST2UfI+U46Ip3oaQ7ZD4eAkoqND +xdniBZAykVG3c/99ux4BAESTF8fsNch6UticBxYMuTu+ouvP0psfI9wwwNliJDmA +CRUTB9AgRynbL1AzhqQoDfsb98IZfjfNOpwnwuLwpMAPhbgd5KNdZaIJ4Hb6/stI +yFElOExxd3TAxF2Gshd/lq1JcNHAZ1DSXV5MvOWT/NWgXwbIzUgQ8eIi+HuDYX2U +UuaB6R8tbd52H7rbUv6HrfinuSlKWqjSYLkiKHkwUpoMw8y9UycRSzs1E9nPwPTO +vRXb0mNCQeBCV9FvStNVXdCUTT8LGPv87xSD2pmt7LijlE6mHLG8McfcWkzA69un +CEHIFAFDimTuN7EBljc119xWFTcHMyoZAfFF+oTqwSbBGImruCxnaJECAwEAATAN +BgkqhkiG9w0BAQsFAAOCAgEApavsgsn7SpPHfhDSN5iZs1ILZQRewJg0Bty0xPfk +3tynSW6bNH3nSaKbpsdmxxomthNSQgD2heOq1By9YzeOoNR+7Pk3s4FkASnf3ToI +JNTUasBFFfaCG96s4Yvs8KiWS/k84yaWuU8c3Wb1jXs5Rv1qE1Uvuwat1DSGXSoD +JNluuIkCsC4kWkyq5pWCGQrabWPRTWsHwC3PTcwSRBaFgYLJaR72SloHB1ot02zL +d2age9dmFRFLLCBzP+D7RojBvL37qS/HR+rQ4SoQwiVc/JzaeqSe7ZbvEH9sZYEu +ALowJzgbwro7oZflwTWunSeSGDSltkqKjvWvZI61pwfHKDahUTmZ5h2y67FuGEaC +CIOUI8dSVSPKITxaq3JL4ze2e9/0Lt7hj19YK2uUmtMAW5Tirz4Yx5lyGH9U8Wur +y/X8VPxTc4A9TMlJgkyz0hqvhbPOT/zSWB10zXh0glKAsSBryAOEDxV1UygmSir7 +YV8Qaq+oyKUTMc1MFq5vZ07M51EPaietn85t8V2Y+k/8XYltRp32NxsypxAJuyxh +g/ko6RVTrWa1sMvz/F9LFqAdKiK5eM96lh9IU4xiLg4ob8aS/GRAA8oIFkZFhLrt +tOwjIUPmEPyHWFi8dLpNuQKYalLYhuwZftG/9xV+wqhKGZO9iPrpHSYBRTap8w2y +1QU= +-----END CERTIFICATE----- diff --git a/rustls/key.pem b/rustls/key.pem new file mode 100644 index 00000000..aac387c6 --- /dev/null +++ b/rustls/key.pem @@ -0,0 +1,51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45cBCbRV3H4JILS5c= +-----END RSA PRIVATE KEY----- diff --git a/rustls/src/main.rs b/rustls/src/main.rs new file mode 100644 index 00000000..47dbb177 --- /dev/null +++ b/rustls/src/main.rs @@ -0,0 +1,60 @@ +extern crate actix; +extern crate actix_web; +extern crate env_logger; +extern crate rustls; + +use std::fs::File; +use std::io::BufReader; + +use actix_web::{http, middleware, server, App, Error, HttpRequest, HttpResponse}; +use rustls::internal::pemfile::{certs, rsa_private_keys}; +use rustls::{NoClientAuth, ServerConfig}; + +use actix_web::fs::StaticFiles; + +/// simple handle +fn index(req: &HttpRequest) -> Result { + println!("{:?}", req); + Ok(HttpResponse::Ok() + .content_type("text/plain") + .body("Welcome!")) +} + +fn main() { + if ::std::env::var("RUST_LOG").is_err() { + ::std::env::set_var("RUST_LOG", "actix_web=info"); + } + env_logger::init(); + let sys = actix::System::new("ws-example"); + + // load ssl keys + let mut config = ServerConfig::new(NoClientAuth::new()); + let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap()); + let key_file = &mut BufReader::new(File::open("key.pem").unwrap()); + let cert_chain = certs(cert_file).unwrap(); + let mut keys = rsa_private_keys(key_file).unwrap(); + config.set_single_cert(cert_chain, keys.remove(0)).unwrap(); + + // actix acceptor + let acceptor = server::RustlsAcceptor::new(config); + + server::new(|| { + App::new() + // enable logger + .middleware(middleware::Logger::default()) + // register simple handler, handle all methods + .resource("/index.html", |r| r.f(index)) + // with path parameters + .resource("/", |r| r.method(http::Method::GET).f(|_| { + HttpResponse::Found() + .header("LOCATION", "/index.html") + .finish() + })) + .handler("/static", StaticFiles::new("static").unwrap()) + }).bind_with("127.0.0.1:8443", acceptor) + .unwrap() + .start(); + + println!("Started http server: 127.0.0.1:8443"); + let _ = sys.run(); +} \ No newline at end of file