Skip to content

Commit

Permalink
Swap openssl with rusttls
Browse files Browse the repository at this point in the history
  • Loading branch information
Draculente committed Oct 5, 2023
1 parent fbcdad0 commit 12c5223
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 39 deletions.
134 changes: 120 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ serde = "1.0.188"
serde_derive = "1.0.188"
urlencoding = "2.1.3"
thhp = "0.2.0"
reqwest = { version = "0.11.22", features = ["blocking"] }
reqwest = { version = "0.11.22", features = ["blocking", "rustls-tls"] }
http-bytes = "0.1.0"
anyhow = "1.0.75"
clap = { version = "4.4.6", features = ["cargo"] }
55 changes: 37 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use reqwest::{blocking::Client, Error};
use std::{env, fs};

use serde_derive::Deserialize;
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Replaceable {
impl Config {
pub fn read_from_config(location: &str) -> anyhow::Result<Config> {
let is_config_host = env::var("WEBCOMMAND_HOST_MODE") == Ok("true".to_owned());
let content = load_config(location, is_config_host);
let content = Config::load_config_str(location, is_config_host);

let raw: RawConfig = toml::from_str(content?.as_str())?;

Expand Down Expand Up @@ -111,27 +112,45 @@ impl Config {
);
redirect_url
}
}

fn load_config(location: &str, is_config_host: bool) -> anyhow::Result<String> {
println!(
"Executing as {}.",
fn load_config_str(location: &str, is_config_host: bool) -> anyhow::Result<String> {
println!(
"Executing as {}.",
if is_config_host {
"config host"
} else {
"config mirror"
}
);

if is_config_host {
"config host"
fs::read_to_string(location).map_err(|_| anyhow!("please provide the config file"))
} else {
"config mirror"
let config_host = env::var("WEBCOMMAND_CONFIG").map_err(|_| {
anyhow!("please provide the url to the config host in WEBCOMMAND_CONFIG.")
})?;
let config_host = get_config_url(&config_host);
let config = Config::get_request_client()?
.get(config_host)
.send()?
.text()?;
Ok(config)
}
);

if is_config_host {
fs::read_to_string(location).map_err(|_| anyhow!("please provide the config file"))
} else {
let config_host = env::var("WEBCOMMAND_CONFIG").map_err(|_| {
anyhow!("please provide the url to the config host in WEBCOMMAND_CONFIG.")
})?;
let config_host = get_config_url(&config_host);
let config = reqwest::blocking::get(config_host)?.text()?;
Ok(config)
}

fn get_request_client() -> Result<Client, Error> {
reqwest::blocking::ClientBuilder::new()
.use_rustls_tls()
.build()
}

pub fn trigger_host_reload(&self) -> anyhow::Result<()> {
if !self.is_config_host {
Config::get_request_client()?
.get(get_reload_url(&self.location))
.send()?;
}
Ok(())
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
pub mod config;
mod simple_server;
use clap::crate_version;
use config::{get_config_url, get_reload_url, Config};
use config::{get_config_url, Config};
use http_bytes::{
http::{Method, Response, StatusCode},
Request,
Expand Down Expand Up @@ -76,11 +76,7 @@ fn send_config_file(_: &Request, config: &Config) -> anyhow::Result<SResponse> {
}

fn reload_config_handler(_: &Request, config: &mut Config) -> anyhow::Result<SResponse> {
if !config.is_config_host {
if let Err(e) = reqwest::blocking::get(get_reload_url(&config.location)) {
eprintln!("Error while triggering reload on config host: {}", e);
}
}
config.trigger_host_reload()?;

config.reload_config()?;
let response_text = "Reloaded configuration".as_bytes();
Expand Down

0 comments on commit 12c5223

Please sign in to comment.