This commit is contained in:
parent
3d757e00f2
commit
6a65e43ca9
10 changed files with 353 additions and 104 deletions
|
|
@ -8,6 +8,7 @@ use mtp::type_map::TypeMap;
|
|||
use std::future::Future;
|
||||
use std::path::Path;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn dev_cert_paths() -> (String, String) {
|
||||
let cert = std::env::var("MTP_DEV_CERT").unwrap_or_else(|_| {
|
||||
|
|
@ -41,13 +42,6 @@ async fn handle_pipe_loopback(
|
|||
let mut reader = req.accept().await?;
|
||||
println!(" [loopback] Pipe {pipe_id} accepted, reading data ...");
|
||||
|
||||
let mut buf = Vec::new();
|
||||
tokio::io::AsyncReadExt::read_to_end(&mut reader, &mut buf).await?;
|
||||
println!(
|
||||
" [loopback] Pipe {pipe_id} read {} bytes, creating return pipe ...",
|
||||
buf.len()
|
||||
);
|
||||
|
||||
let handle = conn.create_pipe("loopback").await?;
|
||||
println!(
|
||||
" [loopback] Return pipe created (id={}), waiting for client ...",
|
||||
|
|
@ -56,16 +50,19 @@ async fn handle_pipe_loopback(
|
|||
|
||||
match handle.wait().await? {
|
||||
Some(mut writer) => {
|
||||
println!(
|
||||
" [loopback] Client accepted return pipe, writing {} bytes ...",
|
||||
buf.len()
|
||||
);
|
||||
tokio::io::AsyncWriteExt::write_all(&mut writer, &buf).await?;
|
||||
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)",
|
||||
buf.len()
|
||||
);
|
||||
println!(" [loopback] Pipe {pipe_id} loopback complete ({} bytes)", total);
|
||||
}
|
||||
None => {
|
||||
eprintln!(" [loopback] Return pipe denied by client for pipe {pipe_id}");
|
||||
|
|
@ -86,8 +83,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let (_host_id, host_keyring) = keys::load_or_generate_host_keys("host.mk")?;
|
||||
keys::export_host_public_keys(&host_keyring)?;
|
||||
|
||||
let decrypt_keyring = mtp::crypto::Keyring::from_bytes(&host_keyring.to_bytes())
|
||||
.expect("re-load host keyring for decryption");
|
||||
let decrypt_keyring = Arc::new(
|
||||
mtp::crypto::Keyring::from_bytes(&host_keyring.to_bytes())
|
||||
.expect("re-load host keyring for decryption"),
|
||||
);
|
||||
|
||||
let (clients, next_id) = clients::load_client_db("clients.json")?;
|
||||
|
||||
|
|
@ -152,61 +151,64 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
println!("Server listening on {}", host.local_addr());
|
||||
|
||||
while let Some(conn) = host.accept().await? {
|
||||
let desc = conn.description.as_deref().unwrap_or("(no description)");
|
||||
println!(
|
||||
"\n--- New connection (version {}, description: {desc}) ---",
|
||||
conn.version
|
||||
);
|
||||
println!("Client ID: {}", conn.client_id);
|
||||
let decrypt_keyring = Arc::clone(&decrypt_keyring);
|
||||
tokio::spawn(async move {
|
||||
let desc = conn.description.as_deref().unwrap_or("(no description)");
|
||||
println!(
|
||||
"\n--- New connection (version {}, description: {desc}) ---",
|
||||
conn.version
|
||||
);
|
||||
println!("Client ID: {}", conn.client_id);
|
||||
|
||||
let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap();
|
||||
let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap();
|
||||
|
||||
println!("Waiting for messages / pipe requests ...");
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
println!("Waiting for messages / pipe requests ...");
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
|
||||
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}");
|
||||
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 = handlers::process_and_respond(
|
||||
&msg,
|
||||
tm,
|
||||
conn.client_public_key.as_ref(),
|
||||
&decrypt_keyring,
|
||||
);
|
||||
println!("Sending: {response}");
|
||||
if let Err(e) = conn.sender.send(&response).await {
|
||||
eprintln!("Send error: {e}");
|
||||
Err(e) => {
|
||||
println!("Pipe channel closed: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Connection ended: {e}");
|
||||
break;
|
||||
}
|
||||
msg = conn.receive() => {
|
||||
match msg {
|
||||
Ok(msg) => {
|
||||
println!("Received: {msg}");
|
||||
let response = handlers::process_and_respond(
|
||||
&msg,
|
||||
tm,
|
||||
conn.client_public_key.as_ref(),
|
||||
&decrypt_keyring,
|
||||
);
|
||||
println!("Sending: {response}");
|
||||
if let Err(e) = conn.sender.send(&response).await {
|
||||
eprintln!("Send error: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Connection ended: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn.sender.close();
|
||||
println!("Connection closed\n");
|
||||
conn.sender.close();
|
||||
println!("Connection closed\n");
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Reference in a new issue