actually read the config maybe

This commit is contained in:
2025-10-25 19:56:58 -06:00
parent d9695be153
commit 55a91b1459
3 changed files with 21 additions and 28 deletions
+21 -25
View File
@@ -31,7 +31,7 @@ fn default_cert_alt_names() -> Vec<String> {
vec!["localhost".into()]
}
#[derive(Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
struct Config {
https_listen_address: SocketAddr,
http_listen_address: Option<SocketAddr>,
@@ -41,7 +41,7 @@ struct Config {
cert_alt_names: Vec<String>,
mumble_server_url: String,
mumble_server_address: Option<SocketAddr>,
gui_path: PathBuf,
gui_path: Option<PathBuf>,
gui: Mutex<GuiConfig>,
}
@@ -52,8 +52,15 @@ static CONFIG: OnceCell<Config> = OnceCell::new();
async fn serve_gui_index_html(req: &Request, res: &mut Response) {
let config = CONFIG.get().unwrap();
let path = match &config.gui_path {
Some(p) => p.join("index.html"),
None => {
res.status_code(StatusCode::NOT_FOUND);
return;
}
};
// Load the HTML file
let path = config.gui_path.join("index.html");
let html = match fs::read_to_string(&path).await {
Ok(content) => content,
Err(err) => {
@@ -74,10 +81,9 @@ async fn serve_gui_index_html(req: &Request, res: &mut Response) {
res.render(Text::Html(modified_html));
}
async fn init_config() -> Result<()> {
fn init_config() -> Result<()> {
let mut config: Config = toml::from_str(
&fs::read_to_string("./config.toml")
.await
&std::fs::read_to_string("./config.toml")
.context("reading config.toml (try making a copy of config.toml.example)")?,
)?;
let mumble_server_addr = config
@@ -102,10 +108,9 @@ async fn init_config() -> Result<()> {
#[tokio::main]
async fn main() -> Result<()> {
init_logging();
init_config().await?;
init_config()?;
let config = CONFIG.get().unwrap();
info!("config\n{}", toml::to_string_pretty(&config.gui)?);
info!("gui config\n{}", serde_json::to_string_pretty(&config.gui)?);
info!("config\n{}", toml::to_string_pretty(&config)?);
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
@@ -150,20 +155,18 @@ async fn main() -> Result<()> {
let rustls_config = RustlsConfig::new(Keycert::new().cert(cert.as_slice()).key(key.as_slice()));
let config_craft = ConfigCraft {
client_config: MumbleClientConfig {
force_proxy: true,
proxy_url: "https://localhost:4433".to_string(),
cert_hash: config.gui.lock().unwrap().cert_hash.clone().unwrap(),
},
client_config: config.gui.lock().unwrap().clone(),
};
// Server routing
let router = Router::new()
let mut router = Router::new()
.get(serve_gui_index_html)
.push(Router::with_path("/proxy").goal(connect_proxy))
.push(Router::with_path("/config").get(config_craft.get_config()))
.push(Router::with_path("/<*+rest>").get(StaticDir::new(config.gui_path.clone())))
.hoop(Logger::new());
if let Some(gui_path) = config.gui_path.clone() {
router = router.push(Router::with_path("/<*+rest>").get(StaticDir::new(gui_path)));
}
let cors = Cors::new().allow_origin(AllowOrigin::any()).into_handler();
@@ -190,22 +193,15 @@ async fn main() -> Result<()> {
Ok(())
}
#[derive(Serialize, Clone)]
struct MumbleClientConfig {
force_proxy: bool,
proxy_url: String,
cert_hash: Vec<u8>,
}
#[derive(Clone)]
pub struct ConfigCraft {
client_config: MumbleClientConfig,
client_config: GuiConfig,
}
#[craft]
impl ConfigCraft {
#[craft(handler)]
async fn get_config(&self) -> Json<MumbleClientConfig> {
async fn get_config(&self) -> Json<GuiConfig> {
Json(self.client_config.clone())
}
}