Brought Example up to spec
Some checks failed
CI / checks (push) Has been cancelled

This commit is contained in:
Alex Emmet 2026-07-19 00:29:24 +02:00
commit b262235ac7
41 changed files with 1688 additions and 653 deletions

91
example/Cargo.lock generated
View file

@ -232,6 +232,8 @@ dependencies = [
"mtp",
"rand 0.10.2",
"tokio",
"tracing",
"tracing-subscriber",
]
[[package]]
@ -1185,10 +1187,12 @@ dependencies = [
"rand 0.10.2",
"rand_core 0.10.1",
"rcgen",
"rustls",
"serde",
"sha2 0.11.0",
"thiserror 1.0.69",
"time",
"tokio",
"zeroize",
]
@ -1213,6 +1217,7 @@ dependencies = [
"rand 0.8.7",
"tokio",
"tracing",
"wtransport",
]
[[package]]
@ -1222,6 +1227,7 @@ dependencies = [
"async-trait",
"mtp-codec",
"mtp-common",
"mtp-crypto",
"rcgen",
"rustls",
"rustls-native-certs",
@ -1251,24 +1257,17 @@ dependencies = [
"http",
"mtp-codec",
"mtp-common",
"mtp-crypto",
"mtp-host",
"mtp-transport",
"quinn",
"rand 0.10.2",
"rustls",
"thiserror 2.0.18",
"tokio",
"tracing",
]
[[package]]
name = "mtp-webserver-example"
version = "0.2.0"
dependencies = [
"http",
"mtp",
"tokio",
]
[[package]]
name = "nom"
version = "7.1.3"
@ -1279,6 +1278,15 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "num-bigint"
version = "0.4.8"
@ -1874,8 +1882,13 @@ dependencies = [
"base64",
"hex",
"mtp",
"rustls",
"serde_json",
"tokio",
"tokio-rustls",
"tracing",
"tracing-subscriber",
"wtransport",
]
[[package]]
@ -1921,6 +1934,15 @@ dependencies = [
"sponge-cursor",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "2.0.1"
@ -2100,6 +2122,15 @@ dependencies = [
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070"
dependencies = [
"cfg-if",
]
[[package]]
name = "time"
version = "0.3.53"
@ -2183,6 +2214,16 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-rustls"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61"
dependencies = [
"rustls",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.7.18"
@ -2226,6 +2267,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]]
@ -2286,6 +2353,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "version_check"
version = "0.9.5"

View file

@ -1,6 +1,6 @@
[workspace]
members = [
"server",
"client", "keygen", "webserver",
"client", "keygen",
]
resolver = "3"

View file

@ -11,3 +11,5 @@ path = "src/main.rs"
mtp = { version = "0.2.0", path = "../../", features = ["client", "crypto", "files", "pipes"] }
tokio = { version = "1", features = ["full"] }
rand = "0.10.1"
tracing = "0.1.44"
tracing-subscriber = "0.3.23"

View file

@ -1,4 +1,6 @@
use std::fs;
use std::time::Instant;
use tokio::fs;
use mtp::client::{ClientConfig, MTPClient, MTPConnection};
use mtp::crypto::{
@ -14,16 +16,25 @@ pub async fn connect_or_register(
let keyring_path = format!("{key_prefix}.mk");
let id_path = format!("{key_prefix}.id");
let file_load_started = Instant::now();
if let (Ok(keyring), Ok(id)) = (
load_keyring_raw(&keyring_path),
fs::read_to_string(&id_path),
fs::read_to_string(&id_path).await,
) {
let client_id: u64 = id.trim().parse()?;
println!("Loaded client keys (ID: {client_id})");
println!(
"Loaded client keys (ID: {client_id}) in {:?}",
file_load_started.elapsed()
);
config.client_id = client_id;
let auth_started = Instant::now();
let conn = MTPClient::auth_connect(config, &keyring, &host_public_key).await?;
println!("Authenticated (version {})", conn.version);
println!(
"Authenticated (version {}) in {:?}",
conn.version,
auth_started.elapsed()
);
return Ok((conn, keyring));
}
@ -45,7 +56,7 @@ pub async fn connect_or_register(
println!("Registered with ID: {}", conn.client_id);
save_keyring_raw(&keyring, &keyring_path)?;
fs::write(&id_path, conn.client_id.to_string())?;
fs::write(&id_path, conn.client_id.to_string()).await?;
println!("Saved client keys -> {keyring_path}");
Ok((conn, keyring))

View file

@ -20,6 +20,7 @@ fn dev_cert_path() -> String {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let cert_path = dev_cert_path();
let cert_pem = fs::read(&cert_path).unwrap_or_else(|e| {
panic!(
@ -29,10 +30,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let host_public_key = match load_public_key_bundle("host.mpkb") {
Ok(bundle) => bundle,
Err(e) => {
return Err(format!(
"Missing host.mpkb: run the server first to export it ({e})"
)
.into());
return Err(
format!("Missing host.mpkb: run the server first to export it ({e})").into(),
);
}
};
@ -49,7 +49,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Pipe demo ---");
pipes::run_pipe_demo(&conn, 1).await?;
conn.sender.close();
conn.sender.close().await;
println!("\nDone");
Ok(())
}

View file

@ -50,7 +50,6 @@ pub async fn run_pipe_demo(
.finish()
.await
.map_err(|e| mtp::common::PipeError::IoError(e.to_string()))?;
println!(" [pipe {i}.{run}] writer: data sent and finished");
Ok::<(), mtp::common::PipeError>(())
}
Ok(None) => {

View file

@ -8,8 +8,13 @@ name = "server"
path = "src/main.rs"
[dependencies]
mtp = { version = "0.2.0", path = "../../", features = ["crypto", "tls", "host", "files", "pipes"] }
mtp = { version = "0.2.0", path = "../../", features = ["crypto", "tls", "web-server", "files", "pipes"] }
tokio = { version = "1", features = ["full"] }
tokio-rustls = "0.26"
rustls = "0.23"
serde_json = { version = "1" }
hex = "0.4"
base64 = "0.22"
tracing = "0.1.44"
tracing-subscriber = "0.3.23"
wtransport = "0.7.1"

View file

@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};
use tokio::fs;
use mtp::crypto::PublicKeyBundle;
pub fn load_client_db(
pub async fn load_client_db(
path: &str,
) -> Result<(Arc<Mutex<HashMap<u64, PublicKeyBundle>>>, Arc<Mutex<u64>>), Box<dyn std::error::Error>>
{
let clients_map = match fs::read_to_string(path) {
let clients_map = match fs::read_to_string(path).await {
Ok(data) => match serde_json::from_str(&data) {
Ok(clients) => clients,
Err(e) => {

View file

@ -1,4 +1,4 @@
use std::fs;
use tokio::fs;
use mtp::crypto::Keyring;
use mtp::files::{load_keyring_raw, save_keyring_raw, save_public_key_bundle};
@ -20,14 +20,16 @@ pub fn load_or_generate_host_keys(
Ok((HOST_ID, keyring))
}
pub fn export_host_public_keys(host_keyring: &Keyring) -> Result<(), Box<dyn std::error::Error>> {
pub async fn export_host_public_keys(
host_keyring: &Keyring,
) -> Result<(), Box<dyn std::error::Error>> {
let bundle = host_keyring.public_key_bundle();
save_public_key_bundle(&bundle, "host.mpkb")?;
/* The web client fetches the bundle as hex over HTTP. */
let bundle_hex = hex::encode(bundle.as_bytes());
fs::write("host_public_key_bundle.hex", &bundle_hex)?;
fs::create_dir_all("web-client/public")?;
fs::write("web-client/public/host_public_key_bundle.hex", &bundle_hex)?;
fs::write("host_public_key_bundle.hex", &bundle_hex).await?;
fs::create_dir_all("web-client/public").await?;
fs::write("web-client/public/host_public_key_bundle.hex", &bundle_hex).await?;
Ok(())
}

View file

@ -2,14 +2,19 @@ mod clients;
mod handlers;
mod keys;
mod tls;
#[path = "web-server.rs"]
mod web_server;
use mtp::host::{AuthenticationPolicy, HostConfig, MTPHost};
use mtp::host::HostConfig;
use mtp::type_map::TypeMap;
use std::future::Future;
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
const CONNECTION_IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);
const MAX_MESSAGES_PER_CONNECTION: u64 = 10_000;
fn dev_cert_paths() -> (String, String) {
let cert = std::env::var("MTP_DEV_CERT").unwrap_or_else(|_| {
if Path::new("example/dev-cert/cert.pem").exists() {
@ -29,62 +34,81 @@ fn dev_cert_paths() -> (String, String) {
}
async fn handle_pipe_loopback(
conn: &mtp::host::MTPConnection,
req: mtp::host::PipeRequest,
conn: &mtp::webserver::WebMTPConnection,
request: mtp::host::PipeRequest<
mtp::webserver::WebMtpSender,
mtp::webserver::H3TransportReceiver,
>,
) -> Result<(), Box<dyn std::error::Error>> {
let pipe_id = req.id();
let pipe_id = request.id();
println!(" [loopback] Accepting pipe {pipe_id} ...");
let mut reader = request.accept().await?;
let return_pipe = conn.create_pipe("loopback").await?;
println!(
" [loopback] Pipe request: id={pipe_id} description={:?}",
req.description()
" [loopback] Requested return pipe {}; waiting for client acceptance ...",
return_pipe.pipe_id()
);
let Some(mut writer) = return_pipe.wait().await? else {
return Err("client denied the return pipe".into());
};
println!(" [loopback] Calling accept() for pipe {pipe_id} ...");
let mut reader = req.accept().await?;
println!(" [loopback] Pipe {pipe_id} accepted, reading data ...");
let handle = conn.create_pipe("loopback").await?;
println!(
" [loopback] Return pipe created (id={}), waiting for client ...",
handle.pipe_id()
);
match handle.wait().await? {
Some(mut writer) => {
println!(" [loopback] Client accepted return pipe, echoing incoming bytes ...");
let mut total = 0usize;
let mut buf = [0u8; 16 * 1024];
loop {
let n = tokio::io::AsyncReadExt::read(&mut reader, &mut buf).await?;
if n == 0 {
break;
}
total += n;
tokio::io::AsyncWriteExt::write_all(&mut writer, &buf[..n]).await?;
}
writer.finish().await?;
println!(
" [loopback] Pipe {pipe_id} loopback complete ({} bytes)",
total
);
}
None => {
eprintln!(" [loopback] Return pipe denied by client for pipe {pipe_id}");
}
}
let copied = tokio::io::copy(&mut reader, &mut writer).await?;
writer.finish_async().await?;
println!(" [loopback] Pipe {pipe_id} complete ({copied} bytes)");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let (cert_path, key_path) = dev_cert_paths();
let (cert_pem, key_pem) = tls::load_or_generate_tls(&cert_path, &key_path)?;
let cert_hash = tls::certificate_sha256_hex(&cert_pem)?;
tls::export_webtransport_cert_hash(&cert_hash)?;
let (cert_pem, key_pem) = tls::load_or_generate_tls(&cert_path, &key_path).await?;
let cert_hash = tls::certificate_sha256_hex(&cert_pem).await?;
tls::export_webtransport_cert_hash(&cert_hash).await?;
println!("WebTransport certificate sha256: {cert_hash}");
let (_host_id, host_keyring) = keys::load_or_generate_host_keys("host.mk")?;
keys::export_host_public_keys(&host_keyring)?;
keys::export_host_public_keys(&host_keyring).await?;
let (clients, next_id) = clients::load_client_db("clients.json").await?;
let clients_for_get = clients.clone();
let get_existing_client = move |id: u64, _description: Option<String>| {
let clients = clients_for_get.clone();
Box::pin(async move { clients.lock().ok()?.get(&id).cloned() })
as Pin<Box<dyn Future<Output = Option<mtp::crypto::PublicKeyBundle>> + Send>>
};
let clients_for_register = clients.clone();
let next_id_for_register = next_id.clone();
let complete_register = move |bundle: mtp::crypto::PublicKeyBundle,
_description: Option<String>| {
let clients = clients_for_register.clone();
let next_id = next_id_for_register.clone();
Box::pin(async move {
let id = {
let mut next = next_id.lock().expect("client id mutex poisoned");
let id = *next;
*next += 1;
id
};
let json = {
let mut db = clients.lock().expect("client database mutex poisoned");
db.insert(id, bundle);
serde_json::to_string_pretty(&*db).ok()
};
if let Some(json) = json {
if let Err(error) = tokio::fs::write("clients.json", json).await {
eprintln!("Failed to persist clients.json: {error}");
}
}
println!("Registered new client with ID: {id}");
id
}) as Pin<Box<dyn Future<Output = u64> + Send>>
};
let decrypt_keyring = Arc::new(
match mtp::crypto::Keyring::from_bytes(&host_keyring.to_bytes()) {
@ -95,51 +119,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
},
);
let (clients, next_id) = clients::load_client_db("clients.json")?;
let clients_for_get = clients.clone();
let get_existing_user = move |id: u64, _description: Option<String>| {
let clients = clients_for_get.clone();
Box::pin(async move {
let result = clients.lock().unwrap().get(&id).cloned();
if result.is_some() {
println!("Auth lookup: client ID {id} found");
} else {
eprintln!("Auth lookup: unknown client ID {id}");
}
result
}) as Pin<Box<dyn Future<Output = Option<mtp::crypto::PublicKeyBundle>> + Send>>
};
let clients_for_register = clients.clone();
let next_id_for_register = next_id.clone();
let clients_path = "clients.json".to_string();
let complete_register = move |bundle: mtp::crypto::PublicKeyBundle,
_description: Option<String>| {
let db_arc = clients_for_register.clone();
let nid_arc = next_id_for_register.clone();
let path = clients_path.clone();
Box::pin(async move {
let mut db = db_arc.lock().unwrap();
let mut nid = nid_arc.lock().unwrap();
let id = *nid;
*nid += 1;
db.insert(id, bundle);
match serde_json::to_string_pretty(&*db) {
Ok(json) => match std::fs::write(&path, json) {
Ok(()) => {}
Err(e) => eprintln!("Failed to persist client database to {path}: {e}"),
},
Err(e) => {
eprintln!("Failed to serialize client database after registering {id}: {e}")
}
}
println!("Registered new client with ID: {}", id);
id
}) as Pin<Box<dyn Future<Output = u64> + Send>>
};
println!("Starting MTP server on port 8080 ...");
println!("Starting integrated MTP web server on port 8080 ...");
let config = HostConfig::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED),
@ -149,13 +129,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.with_authentication(
host_keyring,
Box::new(get_existing_user),
Box::new(get_existing_client),
Box::new(complete_register),
)
.with_authentication_policy(AuthenticationPolicy::ForceAuthentication);
);
let mut host = MTPHost::new(config).await?;
println!("Server listening on {}", host.local_addr());
let _https = web_server::spawn_https(
std::net::SocketAddr::new(config.ip, config.port),
&config.tls_fullchain,
&config.tls_key,
)
.await?;
let mut host = mtp::webserver::MTPWebServer::new(config, web_server::config()?).await?;
println!(
"Server listening on https://{} (TCP HTTPS + UDP WebTransport)",
host.local_addr()
);
while let Some(conn) = host.accept().await? {
let decrypt_keyring = Arc::clone(&decrypt_keyring);
@ -170,56 +158,81 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap();
println!("Waiting for messages / pipe requests ...");
loop {
tokio::select! {
biased;
let mut pipe_open = true;
let mut message_open = true;
let mut messages_received = 0_u64;
pipe_req = conn.receive_pipe() => {
match pipe_req {
Ok(req) => {
println!(" Pipe request: id={} desc={:?}", req.id(), req.description());
if let Err(e) = handle_pipe_loopback(&conn, req).await {
eprintln!(" Pipe loopback error: {e}");
}
}
Err(e) => {
println!("Pipe channel closed: {e}");
break;
}
}
}
msg = conn.receive() => {
match msg {
Ok(msg) => {
println!("Received: {msg}");
let response = match handlers::process_and_respond(
&msg,
tm,
conn.client_public_key.as_ref(),
&decrypt_keyring,
) {
Ok(response) => response,
Err(e) => {
eprintln!("Failed to build response: {e}");
continue;
while pipe_open || message_open {
let activity = tokio::time::timeout(CONNECTION_IDLE_TIMEOUT, async {
tokio::select! {
biased;
pipe_request = conn.receive_pipe(), if pipe_open => {
match pipe_request {
Ok(request) => {
if let Err(error) = handle_pipe_loopback(&conn, request).await {
eprintln!(" [loopback] Pipe error: {error}");
}
};
println!("Sending: {response}");
if let Err(e) = conn.sender.send(&response).await {
eprintln!("Send error: {e}");
break;
}
Err(mtp::common::CommunicationError::StreamClosed)
| Err(mtp::common::CommunicationError::ClosedByPeer) => {
println!("Pipe channel closed normally");
pipe_open = false;
}
Err(error) => {
println!("Pipe channel closed: {error}");
pipe_open = false;
}
}
Err(e) => {
println!("Connection ended: {e}");
break;
}
message = conn.receive(), if message_open => {
match message {
Ok(message) => {
messages_received += 1;
println!("Received: {message}");
match handlers::process_and_respond(
&message,
tm,
conn.client_public_key.as_ref(),
&decrypt_keyring,
) {
Ok(response) => {
println!("Sending: {response}");
if let Err(error) = conn.sender.send(&response).await {
eprintln!("Send error: {error}");
pipe_open = false;
message_open = false;
}
}
Err(error) => {
eprintln!("Failed to build response: {error}");
}
}
}
Err(mtp::common::CommunicationError::StreamClosed)
| Err(mtp::common::CommunicationError::ClosedByPeer) => {
println!("Message channel closed normally");
message_open = false;
}
Err(error) => {
println!("Message channel closed: {error}");
message_open = false;
}
}
}
}
})
.await;
if activity.is_err() {
println!("Connection idle timeout reached");
break;
}
if messages_received >= MAX_MESSAGES_PER_CONNECTION {
println!("Connection message limit reached");
break;
}
}
conn.sender.close();
println!("Connection closed\n");
});
}

View file

@ -1,34 +1,34 @@
use base64::Engine;
use std::fs;
use std::path::Path;
use tokio::fs;
pub fn load_or_generate_tls(
pub async fn load_or_generate_tls(
cert_path: &str,
key_path: &str,
) -> Result<(Vec<u8>, Vec<u8>), Box<dyn std::error::Error>> {
if let (Ok(c), Ok(k)) = (fs::read(cert_path), fs::read(key_path)) {
if let (Ok(c), Ok(k)) = (fs::read(cert_path).await, fs::read(key_path).await) {
println!("Using existing TLS cert from {cert_path}");
return Ok((c, k));
}
println!("Generating self-signed TLS certificate ...");
if let Some(parent) = Path::new(cert_path).parent() {
fs::create_dir_all(parent)?;
fs::create_dir_all(parent).await?;
}
if let Some(parent) = Path::new(key_path).parent() {
fs::create_dir_all(parent)?;
fs::create_dir_all(parent).await?;
}
let (cert_pem, key_pem) = mtp::crypto::tls::generate_self_signed_cert("localhost")?;
fs::write(cert_path, &cert_pem)?;
fs::write(key_path, &key_pem)?;
fs::write(cert_path, &cert_pem).await?;
fs::write(key_path, &key_pem).await?;
println!("Wrote {cert_path} and {key_path}");
Ok((cert_pem, key_pem))
}
pub fn certificate_sha256_hex(cert: &[u8]) -> Result<String, Box<dyn std::error::Error>> {
pub async fn certificate_sha256_hex(cert: &[u8]) -> Result<String, Box<dyn std::error::Error>> {
let der = if cert.starts_with(b"-----BEGIN CERTIFICATE-----") {
let pem = std::str::from_utf8(cert)?;
let base64 = pem
@ -43,14 +43,14 @@ pub fn certificate_sha256_hex(cert: &[u8]) -> Result<String, Box<dyn std::error:
Ok(hex::encode(mtp::crypto::sha256(&der)))
}
pub fn export_webtransport_cert_hash(hash: &str) -> Result<(), Box<dyn std::error::Error>> {
pub async fn export_webtransport_cert_hash(hash: &str) -> Result<(), Box<dyn std::error::Error>> {
let public_dir = if Path::new("web-client").exists() {
Path::new("web-client/public")
} else {
Path::new("example/web-client/public")
};
fs::create_dir_all(public_dir)?;
fs::write(public_dir.join("mtp_dev_cert_hash.txt"), hash)?;
fs::create_dir_all(public_dir).await?;
fs::write(public_dir.join("mtp_dev_cert_hash.txt"), hash).await?;
let dev_cert_dir = if Path::new("dev-cert").exists() {
Path::new("dev-cert")
@ -58,7 +58,7 @@ pub fn export_webtransport_cert_hash(hash: &str) -> Result<(), Box<dyn std::erro
Path::new("example/dev-cert")
};
if dev_cert_dir.exists() {
fs::write(dev_cert_dir.join("sha256.txt"), hash)?;
fs::write(dev_cert_dir.join("sha256.txt"), hash).await?;
}
Ok(())

View file

@ -0,0 +1,138 @@
use mtp::webserver::{Http3Request, Http3Response, WebServerConfig};
use rustls::pki_types::{PrivateKeyDer, pem::PemObject};
use std::{
io,
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::TcpListener,
task::JoinHandle,
};
use tokio_rustls::TlsAcceptor;
async fn ok(_request: Http3Request, response: Http3Response) -> Http3Response {
response
.header("content-type", "text/plain; charset=utf-8")
.body("OK")
}
pub fn config() -> Result<WebServerConfig, mtp::webserver::RouterError> {
WebServerConfig::new().route("/", ok)
}
/// Starts the conventional HTTPS side of the example host. WebTransport uses
/// UDP/QUIC on the same port; browsers still need TCP/TLS to navigate to a URL.
pub async fn spawn_https(
address: SocketAddr,
certificate_pem: &[u8],
key_pem: &[u8],
) -> io::Result<JoinHandle<()>> {
// The TCP listener is created before the QUIC endpoint, so it must select
// rustls' process-wide provider itself.
mtp::crypto::ensure_crypto_provider();
let certificates = rustls::pki_types::CertificateDer::pem_slice_iter(certificate_pem)
.collect::<Result<Vec<_>, _>>()
.map_err(io::Error::other)?;
let key = PrivateKeyDer::from_pem_slice(key_pem).map_err(io::Error::other)?;
let tls = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(certificates, key)
.map_err(io::Error::other)?;
let listener = TcpListener::bind(address).await?;
let acceptor = TlsAcceptor::from(Arc::new(tls));
let asset_root = web_client_dist();
match &asset_root {
Some(_) => println!(
"HTTPS web client available at https://localhost:{}",
address.port()
),
None => eprintln!(
"Web client build not found; HTTPS will show setup instructions. Run `pnpm --dir example/web-client build`."
),
}
Ok(tokio::spawn(async move {
loop {
let Ok((stream, _)) = listener.accept().await else {
break;
};
let acceptor = acceptor.clone();
let asset_root = asset_root.clone();
tokio::spawn(async move {
let Ok(mut stream) = acceptor.accept(stream).await else {
return;
};
let _ = serve_https_request(&mut stream, &asset_root).await;
});
}
}))
}
fn web_client_dist() -> Option<PathBuf> {
[
PathBuf::from("web-client/dist"),
PathBuf::from("example/web-client/dist"),
]
.into_iter()
.find(|path| path.join("index.html").is_file())
}
async fn serve_https_request<S>(stream: &mut S, asset_root: &Option<PathBuf>) -> io::Result<()>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let mut request = [0; 16 * 1024];
let size = stream.read(&mut request).await?;
let request = std::str::from_utf8(&request[..size]).unwrap_or_default();
let path = request
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.unwrap_or("/")
.split('?')
.next()
.unwrap_or("/");
let (status, content_type, body) = match asset_root {
Some(asset_root) => {
let relative = path.trim_start_matches('/');
let candidate = asset_root.join(relative);
let file = if relative.is_empty() || !candidate.is_file() || relative.contains("..") {
asset_root.join("index.html")
} else {
candidate
};
let content_type = content_type(&file);
match tokio::fs::read(&file).await {
Ok(body) => ("200 OK", content_type, body),
Err(_) => ("404 Not Found", "text/plain; charset=utf-8", b"Not found".to_vec()),
}
}
None => (
"503 Service Unavailable",
"text/html; charset=utf-8",
b"<!doctype html><title>MTP web client not built</title><p>Run <code>pnpm --dir example/web-client build</code>.</p>".to_vec(),
),
};
let response = format!(
"HTTP/1.1 {status}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
stream.write_all(response.as_bytes()).await?;
stream.write_all(&body).await?;
stream.shutdown().await
}
fn content_type(file: &Path) -> &'static str {
match file.extension().and_then(|extension| extension.to_str()) {
Some("html") => "text/html; charset=utf-8",
Some("js") => "text/javascript; charset=utf-8",
Some("css") => "text/css; charset=utf-8",
Some("wasm") => "application/wasm",
Some("svg") => "image/svg+xml",
Some("json") => "application/json",
_ => "application/octet-stream",
}
}

View file

@ -44,10 +44,10 @@ let activeClient: ReturnType<typeof createClient> extends Promise<infer T>
let micStream: MediaStream | null = null;
let mediaRecorder: MediaRecorder | null = null;
let activePipeWriter: MTPPipeWriter | null = null;
let loopbackAudioContext: AudioContext | null = null;
let micStreamGeneration = 0;
let pipeSendCount = 0;
let pendingPipeReaders: MTPPipeReader[] = [];
let audioContext: AudioContext | null = null;
let micMonitorAudio: HTMLAudioElement | null = null;
let currentPipePingMs: number | null = null;
let lastPipeSendStartedAt = 0;
let currentPipeId: number | null = null;
@ -56,6 +56,13 @@ let currentPipeState = "idle";
let loopbackPlaybackCount = 0;
let hasPipeRequestHandler = false;
// ===== AUDIO LOOPBACK STATE =====
// We accumulate all chunks into a single Blob, then decode and play it
// when the pipe closes. decodeAudioData needs a complete file, not fragments.
let loopbackBlobParts: BlobPart[] = [];
let loopbackMimeType = "";
let loopbackAudioElement: HTMLAudioElement | null = null;
const credentialStorage: MTPCredentialStorage = {
getItem: (key) => localStorage.getItem(key),
setItem: (key, value) => localStorage.setItem(key, value),
@ -126,11 +133,18 @@ function setPipeState(
function getPipeId(handle: unknown): number | null {
if (handle && typeof handle === "object") {
const candidate = handle as Record<string, unknown>;
const value =
let value =
candidate.pipeId ??
candidate.pipe_id ??
candidate["pipe-id"] ??
candidate.id;
if (typeof value === "function") {
try {
value = value.call(handle);
} catch {
return null;
}
}
if (typeof value === "number" && Number.isFinite(value)) {
return value;
}
@ -138,63 +152,64 @@ function getPipeId(handle: unknown): number | null {
return null;
}
function ensureAudioContext() {
if (!audioContext) {
audioContext = new AudioContext();
}
return audioContext;
// ===== FIXED AUDIO LOOPBACK: accumulate chunks, play as single file =====
function startLoopbackAccumulation(mimeType: string) {
loopbackBlobParts = [];
loopbackMimeType = mimeType;
pipeLog("Loopback: accumulating audio chunks...");
}
async function playLoopbackAudio(chunks: BlobPart[], mimeType: string) {
if (chunks.length === 0) return;
const blob = new Blob(chunks, { type: mimeType });
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.autoplay = true;
audio.onended = () => URL.revokeObjectURL(url);
audio.onerror = () => URL.revokeObjectURL(url);
function queueLoopbackChunk(data: Uint8Array) {
loopbackBlobParts.push(data.slice());
}
async function finishLoopbackPlayback() {
if (loopbackBlobParts.length === 0) {
pipeLog("Loopback: no chunks received.", "error");
return;
}
// Stop any previous playback
if (loopbackAudioElement) {
loopbackAudioElement.pause();
const src = loopbackAudioElement.src;
loopbackAudioElement.src = "";
if (src.startsWith("blob:")) {
URL.revokeObjectURL(src);
}
loopbackAudioElement = null;
}
// Concatenate all chunks into one Blob
const blob = new Blob(loopbackBlobParts, { type: loopbackMimeType });
loopbackBlobParts = [];
pipeLog(`Loopback: assembled ${blob.size} bytes, decoding...`);
try {
await ensureAudioContext().resume();
await audio.play();
const arrayBuffer = await blob.arrayBuffer();
const audioContext = new AudioContext();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start();
loopbackPlaybackCount += 1;
updateMetrics();
pipeLog(`Loopback playback started (${chunks.length} chunks).`);
pipeLog(`Loopback playback started (${audioBuffer.duration.toFixed(2)}s).`);
// Clean up audio context when done
source.onended = () => {
audioContext.close().catch(() => {});
};
} catch (e) {
URL.revokeObjectURL(url);
pipeLog(`Loopback playback failed: ${e}`, "error");
pipeLog(`Loopback decode/playback failed: ${e}`, "error");
}
}
async function startMicPlayback(stream: MediaStream) {
if (micMonitorAudio) {
micMonitorAudio.pause();
micMonitorAudio.srcObject = null;
micMonitorAudio = null;
}
const audio = new Audio();
audio.autoplay = true;
audio.controls = false;
audio.muted = false;
audio.srcObject = stream;
micMonitorAudio = audio;
try {
await ensureAudioContext().resume();
await audio.play();
pipeLog("Microphone monitoring playback started.");
} catch (e) {
pipeLog(`Microphone monitoring playback failed: ${e}`, "error");
}
}
function stopMicPlayback() {
if (!micMonitorAudio) return;
micMonitorAudio.pause();
micMonitorAudio.srcObject = null;
micMonitorAudio = null;
}
function renderStructured(value: unknown): string {
return JSON.stringify(value, (_key, item) => {
if (typeof item === "bigint") {
@ -375,7 +390,6 @@ async function connect() {
log("WebTransport is not supported in this browser.", "error");
return;
}
const hostPk = hexToBytes(HOST_PUBLIC_KEY.value);
saveHostPublicKey();
await loadDevCertHash();
@ -401,7 +415,7 @@ async function connect() {
const activeClientId = await client.auth();
clientId = activeClientId;
loadKeys();
log(`Connected as client ${activeClientId}`);
log(`Connected as authenticated client ${activeClientId}`);
log("\nSending typed Ping...");
await client.send(
@ -451,7 +465,9 @@ async function startMicStreaming() {
STOP_MIC.disabled = false;
setPipeState("creating", { pipeId: null, description: "mic-audio" });
pipeLog("Microphone acquired. Creating pipe ...");
await startMicPlayback(micStream);
pipeLog(
"Microphone monitoring is off; playback will use the server loopback.",
);
if (!hasPipeRequestHandler) {
activeClient.setOnPipeRequest(async (request) => {
@ -487,6 +503,7 @@ async function startMicStreaming() {
return;
}
activePipeWriter = writer;
const streamGeneration = ++micStreamGeneration;
setPipeState("streaming", {
pipeId: getPipeId(writer) ?? pipeId,
@ -500,12 +517,22 @@ async function startMicStreaming() {
const mimeType = MediaRecorder.isTypeSupported("audio/webm;codecs=opus")
? "audio/webm;codecs=opus"
: "audio/webm";
mediaRecorder = new MediaRecorder(micStream, { mimeType });
const recorder = new MediaRecorder(micStream, { mimeType });
mediaRecorder = recorder;
mediaRecorder.ondataavailable = async (event) => {
if (event.data.size === 0 || !activeClient) return;
recorder.ondataavailable = async (event) => {
// A final chunk can be queued before recorder.stop(). Do not use the
// captured writer unless this is still the current active stream.
if (
event.data.size === 0 ||
!activeClient ||
micStreamGeneration !== streamGeneration ||
mediaRecorder !== recorder ||
activePipeWriter !== writer
) {
return;
}
const startTime = performance.now();
pipeSendCount++;
const chunkNum = pipeSendCount;
@ -513,6 +540,14 @@ async function startMicStreaming() {
lastPipeSendStartedAt = performance.now();
const buffer = await event.data.arrayBuffer();
const data = new Uint8Array(buffer);
// arrayBuffer() yields, so shutdown may have happened meanwhile.
if (
micStreamGeneration !== streamGeneration ||
mediaRecorder !== recorder ||
activePipeWriter !== writer
) {
return;
}
await writer.write(data);
currentPipePingMs = performance.now() - lastPipeSendStartedAt;
updateMetrics();
@ -521,27 +556,29 @@ async function startMicStreaming() {
}
};
mediaRecorder.start(200); // emit data every 200ms
recorder.start(200); // emit data every 200ms
updateMetrics();
pipeLog("Streaming started (200ms chunks).");
}
async function readLoopbackPipe(reader: MTPPipeReader) {
const startTime = performance.now();
let totalBytes = 0;
let chunkCount = 0;
const chunks: BlobPart[] = [];
const mimeType = MediaRecorder.isTypeSupported("audio/webm;codecs=opus")
? "audio/webm;codecs=opus"
: "audio/webm";
try {
loopbackBlobParts = [];
loopbackMimeType = mimeType;
pipeLog("Loopback: accumulating chunks...");
while (true) {
const data = await reader.read();
if (data == null) break; // EOF
totalBytes += data.length;
chunkCount++;
chunks.push(data.slice().buffer);
loopbackBlobParts.push(data.slice());
}
} catch (e) {
pipeLog(` Return pipe read error: ${e}`, "error");
@ -554,26 +591,90 @@ async function readLoopbackPipe(reader: MTPPipeReader) {
`delay=${elapsed.toFixed(1)}ms`,
);
setPipeState("loopback-ready", { pingMs: elapsed });
await playLoopbackAudio(chunks, mimeType);
// Decode and play the complete recording
if (loopbackBlobParts.length > 0) {
try {
const blob = new Blob(loopbackBlobParts, { type: loopbackMimeType });
const arrayBuffer = await blob.arrayBuffer();
if (!loopbackAudioContext) {
loopbackAudioContext = new AudioContext();
}
const audioBuffer =
await loopbackAudioContext.decodeAudioData(arrayBuffer);
const source = loopbackAudioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(loopbackAudioContext.destination);
source.start();
loopbackPlaybackCount += 1;
updateMetrics();
pipeLog(
`Loopback playback started (${audioBuffer.duration.toFixed(2)}s).`,
);
} catch (e) {
pipeLog(`Loopback decode failed: ${e}`, "error");
}
}
// Clean up the reader from the pending list
const idx = pendingPipeReaders.indexOf(reader);
if (idx >= 0) pendingPipeReaders.splice(idx, 1);
}
// ===== CRITICAL FIX: stopMicStreaming must capture the final chunk =====
async function stopMicStreaming() {
if (mediaRecorder && mediaRecorder.state !== "inactive") {
mediaRecorder.stop();
mediaRecorder = null;
micStreamGeneration++;
const recorder = mediaRecorder;
mediaRecorder = null;
const writer = activePipeWriter;
activePipeWriter = null;
// STOPPING STRATEGY:
// 1. Request a final dataavailable event by calling requestData() if needed,
// then stop(). The final event contains the WebM trailer.
// 2. Wait for that final event to be processed (it writes through the pipe).
// 3. Only THEN close the pipe writer.
if (recorder) {
// Create a promise that resolves when the final dataavailable fires
const finalChunkPromise = new Promise<void>((resolve) => {
const originalHandler = recorder.ondataavailable;
recorder.ondataavailable = async (event) => {
// Call the original handler first so the chunk gets written to the pipe
if (originalHandler) {
await originalHandler.call(recorder, event);
}
// The final chunk from stop() has a 'type' but no special marker.
// MediaRecorder state will be 'inactive' after the final event.
if (recorder.state === "inactive") {
resolve();
}
};
});
if (recorder.state !== "inactive") {
recorder.stop();
}
// Wait up to 1 second for the final chunk to be captured and written
await Promise.race([
finalChunkPromise,
new Promise((_, reject) =>
setTimeout(() => reject(new Error("final chunk timeout")), 1000),
),
]).catch(() => {
pipeLog("Warning: final chunk may not have been captured", "error");
});
}
if (activePipeWriter) {
if (writer) {
try {
await activePipeWriter.close();
await writer.close();
} catch (e) {
pipeLog(`Pipe close error: ${e}`, "error");
} finally {
activePipeWriter = null;
}
}
@ -581,9 +682,6 @@ async function stopMicStreaming() {
micStream.getTracks().forEach((track) => track.stop());
micStream = null;
}
stopMicPlayback();
// Close pending pipe readers
pendingPipeReaders = [];
setPipeState("stopped", {
pipeId: currentPipeId,

View file

@ -1,10 +0,0 @@
[package]
name = "mtp-webserver-example"
version = "0.2.0"
edition = "2024"
publish = false
[dependencies]
http = "1"
mtp = { path = "../..", features = ["web-server"] }
tokio = { version = "1", features = ["full"] }

View file

@ -1,38 +0,0 @@
use http::StatusCode;
use mtp::{
host::HostConfig,
webserver::{Http3Request, Http3Response, MTPWebServer, WebServerConfig},
};
async fn health(_request: Http3Request, response: Http3Response) -> Http3Response {
response
.status(StatusCode::OK)
.header("content-type", "application/json")
.body(r#"{"status":"ok"}"#)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = HostConfig::new(
"0.0.0.0".parse()?,
4433,
std::fs::read("cert.pem")?,
std::fs::read("key.pem")?,
);
let web = WebServerConfig::new().route("/health", health)?.mtp_path("/mtp");
let mut server = MTPWebServer::new(config, web).await?;
println!("listening on {}", server.local_addr());
while let Some(connection) = server.accept().await? {
println!(
"MTP client connected: path={}, version={}, description={:?}",
connection.path, connection.version, connection.description
);
tokio::spawn(async move {
while let Ok(message) = connection.receiver.receive().await {
println!("received MTP message {}", message.get_id());
}
});
}
Ok(())
}