server updates

This commit is contained in:
Brad Stein 2025-06-16 18:31:11 -05:00
parent 24fb41a0d5
commit de2c99731f
2 changed files with 40 additions and 35 deletions

View File

@ -92,7 +92,7 @@ impl KeyboardAggregator {
self.dev_log(|| debug!(?report, "Keyboard HID report"));
self.send_report(report);
if self.is_magic_chord() {
self.dev_log(|| warn!("Magic chord pressed => 🧙‍♂️🪄💥 AVADA KEDAVRA!!! 💀"));
self.dev_log(|| warn!("Magic chord pressed => 🧙‍♂️🪄 AVADA KEDAVRA!!! 💥💀"));
std::process::exit(0);
}
}

View File

@ -36,50 +36,55 @@ impl Relay for Handler {
let (tx, rx) = tokio::sync::mpsc::channel(32);
tokio::spawn(async move {
while let Some(msg) = in_stream.next().await.transpose()? {
match msg.kind {
/* ───── KEYBOARD ───── */
Some(hid_report::Kind::KeyboardReport(ref v)) if v.len() == 8 => {
match kb.lock().await.write_all(v).await {
Ok(_) => info!("⌨️ → /dev/hidg0 (8 B)"),
loop {
match in_stream.next().await {
/* ──────────────── message received ──────────────── */
Some(Ok(msg)) => {
// 1. write to the right gadget ---------------------------------
let io_res = match &msg.kind {
Some(hid_report::Kind::KeyboardReport(v)) if v.len() == 8 => {
kb.lock().await.write_all(v).await.map(|_| "⌨️ → /dev/hidg0 (8B)")
}
Some(hid_report::Kind::MouseReport(v)) if v.len() == 4 => {
ms.lock().await.write_all(v).await.map(|_| "🖱️ → /dev/hidg1 (4B)")
}
_ => {
error!(?msg.kind, "⚠️ malformed packet");
continue; // skip echo
}
};
// 2. I/O result -------------------------------------------------
match io_res {
Ok(msg_txt) => info!("{msg_txt}"),
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
trace!("⌨️ /dev/hidg0 busy, dropped packet");
continue;
trace!("🐛 gadget busy, dropped packet");
continue; // skip echo
}
Err(e) => {
error!("⌨️ write error: {e}");
continue;
error!("write error: {e}");
continue; // skip echo
}
}
// 3. echo back (besteffort) -----------------------------------
let _ = tx.try_send(Ok(msg));
}
/* ───── MOUSE ───── */
Some(hid_report::Kind::MouseReport(ref v)) if v.len() == 4 => {
match ms.lock().await.write_all(v).await {
Ok(_) => info!("🖱️ → /dev/hidg1 (4 B)"),
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
trace!("🖱️ /dev/hidg1 busy, dropped packet");
continue;
}
Err(e) => {
error!("🖱️ write error: {e}");
continue;
}
}
}
/* ───── bad / unknown ───── */
_ => {
error!(?msg.kind, "⚠️ malformed packet");
continue;
/* ──────────────── benign backpressure error ──────────────── */
Some(Err(status)) => {
trace!("grpc recv error (ignored): {status}");
continue; // keep the stream alive
}
/* ──────────────── client closed the stream ──────────────── */
None => break,
}
// echo back so the client knows were alive
let _ = tx.send(Ok(msg)).await;
}
info!("🔚 client stream closed");
Ok::<_, Status>(())
// dropping `tx` here terminates the server→client stream gracefully
Ok::<(), Status>(())
});
Ok(Response::new(Box::pin(ReceiverStream::new(rx))))