Add debug logging and global connection map
This commit is contained in:
Generated
+1
@@ -381,6 +381,7 @@ name = "mumble-webtransport-proxy"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"lazy_static",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
tokio = { version = "1.37.0", features = ["full"] }
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
tracing = { version = "0.1.40", features = ["async-await"] }
|
tracing = { version = "0.1.40", features = ["async-await"] }
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||||
|
|||||||
+29
-5
@@ -9,20 +9,29 @@ use tracing::Instrument;
|
|||||||
use tracing_subscriber::filter::LevelFilter;
|
use tracing_subscriber::filter::LevelFilter;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
use wtransport::endpoint::IncomingSession;
|
use wtransport::endpoint::IncomingSession;
|
||||||
use wtransport::Endpoint;
|
use wtransport::{connection, Endpoint};
|
||||||
use wtransport::Identity;
|
use wtransport::Identity;
|
||||||
use wtransport::ServerConfig;
|
use wtransport::ServerConfig;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
|
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
type GlobalMap = Mutex<HashMap<usize, wtransport::Connection>>;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref DATA_MAP: GlobalMap = Mutex::new(HashMap::new());
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
init_logging();
|
init_logging();
|
||||||
|
|
||||||
let config = ServerConfig::builder()
|
let config = ServerConfig::builder()
|
||||||
.with_bind_default(4433)
|
.with_bind_default(4433)
|
||||||
//.with_identity(&Identity::self_signed(["localhost"]).unwrap())
|
|
||||||
.with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
|
.with_identity(&Identity::load_pemfiles("cert.pem", "key.pem").await?)
|
||||||
.keep_alive_interval(Some(Duration::from_secs(3)))
|
.keep_alive_interval(Some(Duration::from_secs(20)))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let server = Endpoint::server(config)?;
|
let server = Endpoint::server(config)?;
|
||||||
@@ -38,8 +47,10 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_connection(incoming_session: IncomingSession, id: usize) {
|
async fn handle_connection(incoming_session: IncomingSession, id: usize) {
|
||||||
let _result = handle_connection_impl(incoming_session, id).await;
|
// Wrapper to handle connection establishment failures
|
||||||
//error!("{:?}", result);
|
if let Err(e) = handle_connection_impl(incoming_session, id).await {
|
||||||
|
error!("{:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_connection_impl(incoming_session: IncomingSession, id: usize) -> Result<()> {
|
async fn handle_connection_impl(incoming_session: IncomingSession, id: usize) -> Result<()> {
|
||||||
@@ -58,11 +69,22 @@ async fn handle_connection_impl(incoming_session: IncomingSession, id: usize) ->
|
|||||||
let connection = session_request.accept().await?;
|
let connection = session_request.accept().await?;
|
||||||
let stream = connection.accept_bi().await?;
|
let stream = connection.accept_bi().await?;
|
||||||
|
|
||||||
|
info!("Connecting to corresponding Mumble server...");
|
||||||
|
|
||||||
let server_tcp = TcpStream::connect("127.0.0.1:64738").await?.into_split();
|
let server_tcp = TcpStream::connect("127.0.0.1:64738").await?.into_split();
|
||||||
|
|
||||||
|
info!("Connected to Mumble Server!");
|
||||||
|
|
||||||
|
// Store connection in global map to prevent it getting dropped
|
||||||
|
DATA_MAP.lock().unwrap().insert(id, connection);
|
||||||
|
|
||||||
|
info!("Spawing jobs...");
|
||||||
|
// Spawn tasks to handle transmitting data between the WebTransport client and Mumble TCP Server
|
||||||
tokio::spawn(handle_client_to_server(stream.1, server_tcp.1).instrument(info_span!("Handler", "Client to server")));
|
tokio::spawn(handle_client_to_server(stream.1, server_tcp.1).instrument(info_span!("Handler", "Client to server")));
|
||||||
tokio::spawn(handle_server_to_client(stream.0, server_tcp.0).instrument(info_span!("Handler", "Server to client")));
|
tokio::spawn(handle_server_to_client(stream.0, server_tcp.0).instrument(info_span!("Handler", "Server to client")));
|
||||||
|
|
||||||
|
info!("Spawned jobs.");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,11 +103,13 @@ async fn client_to_server_loop(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut buffer = vec![0; 65536].into_boxed_slice();
|
let mut buffer = vec![0; 65536].into_boxed_slice();
|
||||||
loop {
|
loop {
|
||||||
|
info!("Reading Data");
|
||||||
let _bytes_read = match client_stream.read(&mut buffer).await? {
|
let _bytes_read = match client_stream.read(&mut buffer).await? {
|
||||||
Some(bytes_read) => bytes_read,
|
Some(bytes_read) => bytes_read,
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
info!("Writing data");
|
||||||
server_stream.try_write(&buffer)?;
|
server_stream.try_write(&buffer)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user