Skip to content

Commit

Permalink
WIP - adding embedded SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Oct 12, 2018
1 parent e057519 commit 4faf27c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ serde_json = "1.0"
serde_yaml = "0.8"

ratel = "0.7.0"
tempdir = "0.3.7"
28 changes: 26 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate openssl;
extern crate regex;
extern crate serde_yaml;
extern crate url;
extern crate tempdir;

use actix_web::{server, App};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
Expand All @@ -21,6 +22,10 @@ use bs::from_file::FromFile;
use bs::options::{ProgramOptions, ProxyScheme};
use bs::setup::{apply_presets, state_and_presets};
use openssl::ssl::SslAcceptorBuilder;
use std::ffi::CString;
use std::env;
use tempdir::TempDir;
use std::fs::File;

fn main() {
match ProgramOptions::from_vec(&mut std::env::args_os()).and_then(run_with_opts) {
Expand Down Expand Up @@ -108,10 +113,29 @@ fn run_with_opts(opts: ProgramOptions) -> Result<(), ProgramStartError> {
/// Todo: allow key/cert options
///
fn get_ssl_builder() -> SslAcceptorBuilder {

use std::fs::File;
use std::io::{self, Write};

let tmp_dir = TempDir::new("example").unwrap();
let file_key = tmp_dir.path().join("key.pem");
let file_cert = tmp_dir.path().join("cert.pem");

let mut tmp_file = File::create(&file_key).unwrap();
tmp_file.write_all(include_bytes!("key.pem")).unwrap();
tmp_file.sync_all().unwrap();

let mut tmp_file2 = File::create(&file_cert).unwrap();
tmp_file2.write_all(include_bytes!("cert.pem")).unwrap();
tmp_file2.sync_all().unwrap();

let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("src/key.pem", SslFiletype::PEM)
.set_private_key_file(file_key, SslFiletype::PEM)
.unwrap();
builder.set_certificate_chain_file("src/cert.pem").unwrap();
builder.set_certificate_chain_file(file_cert).unwrap();

tmp_dir.close().unwrap();

builder
}

0 comments on commit 4faf27c

Please sign in to comment.