Add static asset serving

This commit is contained in:
2024-11-09 12:50:56 -07:00
parent 1d8f3fd791
commit b19f629605
3 changed files with 435 additions and 18 deletions
+71 -16
View File
@@ -1,5 +1,7 @@
use anyhow::Result;
use std::net::ToSocketAddrs;
use anyhow::{Context, Result};
use std::env;
use std::future::IntoFuture;
use std::net::{SocketAddr, ToSocketAddrs};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;
@@ -81,13 +83,63 @@ impl ServerCertVerifier for NoCertificateVerification {
}
}
struct Config {
proxy_listen_address: String,
http_listen_address: String,
cert_path: String,
key_path: String,
mumble_server_address: SocketAddr,
gui_path: String,
}
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: env::var("MUMBLE_WEBTRANSPORT_GUI_PATH").unwrap_or("gui".to_string()),
})
}
}
async fn redirect() -> axum::response::Redirect {
axum::response::Redirect::to("/static/index.html")
}
#[tokio::main]
async fn main() -> Result<()> {
init_logging();
let proxy_config = Config::get()?;
// Setup HTTP Server
//let http = axum::Router::new().route("/", axum::routing::get(serve_gui));
let app = axum::Router::new()
.nest_service(
"/gui",
tower_http::services::ServeDir::new(proxy_config.gui_path),
)
.route("/", axum::routing::get(redirect));
let listener = tokio::net::TcpListener::bind(proxy_config.http_listen_address)
.await
.unwrap();
tokio::spawn(axum::serve(listener, app).into_future());
// Setup WebTransport proxy listener
let config = ServerConfig::builder()
.with_bind_default(4433)
.with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
.with_bind_address(proxy_config.proxy_listen_address.parse()?)
.with_identity(
&Identity::load_pemfiles(proxy_config.cert_path, proxy_config.key_path).await?,
)
.keep_alive_interval(Some(Duration::from_secs(20)))
.build();
@@ -98,21 +150,30 @@ async fn main() -> Result<()> {
for id in 0.. {
let incoming_session = server.accept().await;
tokio::spawn(
handle_connection(incoming_session, id).instrument(info_span!("Connection", id)),
handle_connection(incoming_session, id, proxy_config.mumble_server_address)
.instrument(info_span!("Connection", id)),
);
}
Ok(())
}
async fn handle_connection(incoming_session: IncomingSession, id: usize) {
async fn handle_connection(
incoming_session: IncomingSession,
id: usize,
mumble_server_address: SocketAddr,
) {
// Wrapper to handle connection establishment failures
if let Err(e) = handle_connection_impl(incoming_session, id).await {
if let Err(e) = handle_connection_impl(incoming_session, id, mumble_server_address).await {
error!("{:?}", e);
}
}
async fn handle_connection_impl(incoming_session: IncomingSession, id: usize) -> Result<()> {
async fn handle_connection_impl(
incoming_session: IncomingSession,
id: usize,
mumble_server_address: SocketAddr,
) -> Result<()> {
info!("Waiting for session request...");
let session_request = incoming_session.await?;
@@ -135,15 +196,9 @@ async fn handle_connection_impl(incoming_session: IncomingSession, id: usize) ->
let connector = TlsConnector::from(Arc::new(config));
let addr = env!("WEBTRANSPORT_PROXY_MUMBLE_SERVER_URL")
.to_string()
.to_socket_addrs()?
.next()
.unwrap();
let server_tcp = TcpStream::connect(addr).await?;
let server_tcp = TcpStream::connect(mumble_server_address).await?;
let server_stream = connector
.connect("ohea.xyz".try_into()?, server_tcp)
.connect("example.com".try_into()?, server_tcp)
.await?;
let (read_server, write_server) = tokio::io::split(server_stream);