use config.toml & send gui config to client
This commit is contained in:
+39
-30
@@ -1,12 +1,13 @@
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use axum::extract::State;
|
||||
use axum::http::{Response, StatusCode};
|
||||
use axum::response::IntoResponse;
|
||||
use std::env;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::future::IntoFuture;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
use tokio::fs::read_to_string;
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::pin;
|
||||
@@ -87,48 +88,39 @@ impl ServerCertVerifier for NoCertificateVerification {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
struct GuiConfig {
|
||||
proxy_url: Option<String>,
|
||||
cert_hash: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct Config {
|
||||
proxy_listen_address: String,
|
||||
http_listen_address: String,
|
||||
cert_path: String,
|
||||
key_path: String,
|
||||
mumble_server_address: SocketAddr,
|
||||
mumble_server_url: String,
|
||||
gui_path: PathBuf,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn get() -> Result<Config> {
|
||||
Ok(Config {
|
||||
proxy_listen_address: env::var("MUMBLE_WEBTRANSPORT_PROXY_LISTEN_ADDRESS")
|
||||
.unwrap_or("127.0.0.1:4433".to_string()),
|
||||
http_listen_address: env::var("MUMBLE_WEBTRANSPORT_HTTP_LISTEN_ADDRESS")
|
||||
.unwrap_or("127.0.0.1:4434".to_string()),
|
||||
cert_path: env::var("MUMBLE_WEBTRANSPORT_CERT_PATH").unwrap_or("cert.pem".to_string()),
|
||||
key_path: env::var("MUMBLE_WEBTRANSPORT_KEY_PATH").unwrap_or("key.pem".to_string()),
|
||||
mumble_server_address: env::var("MUMBLE_WEBTRANSPORT_MUMBLE_SERVER_ADDRESS")
|
||||
.with_context(|| "Please set MUMBLE_WEBTRANSPORT_MUMBLE_SERVER_ADDRESS")?
|
||||
.to_socket_addrs()?
|
||||
.next()
|
||||
.ok_or(anyhow::anyhow!("Invalid mumble server address"))?,
|
||||
gui_path: PathBuf::from(
|
||||
env::var("MUMBLE_WEBTRANSPORT_GUI_PATH").unwrap_or("gui".to_string()),
|
||||
),
|
||||
})
|
||||
}
|
||||
gui: GuiConfig,
|
||||
}
|
||||
|
||||
//async fn serve_index_html_with_config(State(config): State<Config>) -> impl IntoResponse {
|
||||
async fn serve_index_html_with_config(State(config): State<Config>) -> impl IntoResponse {
|
||||
// Load the HTML file
|
||||
let html = match tokio::fs::read_to_string(config.gui_path.join("index.html")).await {
|
||||
let html = match read_to_string(config.gui_path.join("index.html")).await {
|
||||
Ok(content) => content,
|
||||
Err(_) => return (StatusCode::NOT_FOUND, "File not found").into_response(),
|
||||
};
|
||||
|
||||
// Insert the script tag with configuration
|
||||
let config_script = "<script>window.config = {\"foo\": \"bar\"}</script>";
|
||||
let modified_html = html.replace("</head>", &format!("{}\n</head>", config_script));
|
||||
let modified_html = html.replace(
|
||||
"</head>",
|
||||
&format!(
|
||||
"<script>window.config = {}</script>\n</head>",
|
||||
serde_json::to_string(&config.gui).unwrap(),
|
||||
),
|
||||
);
|
||||
|
||||
// Create a response with the modified HTML
|
||||
Response::builder()
|
||||
@@ -143,7 +135,24 @@ async fn serve_index_html_with_config(State(config): State<Config>) -> impl Into
|
||||
async fn main() -> Result<()> {
|
||||
init_logging();
|
||||
|
||||
let proxy_config = Config::get()?;
|
||||
let proxy_config: Config = toml::from_str(
|
||||
&read_to_string("./config.toml")
|
||||
.await
|
||||
.context("reading config.toml (try making a copy of config.toml.example)")?,
|
||||
)?;
|
||||
|
||||
let mumble_server_addr = proxy_config
|
||||
.mumble_server_url
|
||||
.to_socket_addrs()
|
||||
.context(format!(
|
||||
"parsing mumble_server_url={}",
|
||||
proxy_config.mumble_server_url
|
||||
))?
|
||||
.next()
|
||||
.ok_or(anyhow!(
|
||||
"no socket addrs in mumble_server_url={}",
|
||||
proxy_config.mumble_server_url
|
||||
))?;
|
||||
|
||||
// Setup HTTP Server
|
||||
//let http = axum::Router::new().route("/", axum::routing::get(serve_gui));
|
||||
@@ -172,7 +181,7 @@ async fn main() -> Result<()> {
|
||||
for id in 0.. {
|
||||
let incoming_session = server.accept().await;
|
||||
tokio::spawn(
|
||||
handle_connection(incoming_session, id, proxy_config.mumble_server_address)
|
||||
handle_connection(incoming_session, id, mumble_server_addr)
|
||||
.instrument(info_span!("Connection", id)),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user