2025-06-08 22:24:14 -05:00
|
|
|
|
// client/src/app.rs
|
2025-06-17 20:54:31 -05:00
|
|
|
|
|
2025-06-15 20:19:27 -05:00
|
|
|
|
#![forbid(unsafe_code)]
|
2025-06-17 20:54:31 -05:00
|
|
|
|
|
2025-06-11 00:37:01 -05:00
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
use std::time::Duration;
|
2025-06-16 17:54:47 -05:00
|
|
|
|
use tokio::{sync::broadcast, task::JoinHandle};
|
|
|
|
|
|
use tokio_stream::{wrappers::BroadcastStream, StreamExt};
|
2025-06-08 04:11:58 -05:00
|
|
|
|
use tonic::Request;
|
2025-06-17 20:54:31 -05:00
|
|
|
|
use tracing::{debug, error, info, warn};
|
2025-06-15 20:19:27 -05:00
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
use navka_common::navka::{relay_client::RelayClient, KeyboardReport, MouseReport};
|
2025-06-17 08:17:23 -05:00
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
use crate::input::inputs::InputAggregator;
|
2025-06-08 04:11:58 -05:00
|
|
|
|
|
|
|
|
|
|
pub struct NavkaClientApp {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
aggregator: Option<InputAggregator>,
|
2025-06-08 04:11:58 -05:00
|
|
|
|
server_addr: String,
|
2025-06-08 13:11:31 -05:00
|
|
|
|
dev_mode: bool,
|
2025-06-17 08:17:23 -05:00
|
|
|
|
kbd_tx: broadcast::Sender<KeyboardReport>,
|
|
|
|
|
|
mou_tx: broadcast::Sender<MouseReport>,
|
2025-06-08 04:11:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NavkaClientApp {
|
|
|
|
|
|
pub fn new() -> Result<Self> {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
let dev_mode = std::env::var("NAVKA_DEV_MODE").is_ok();
|
|
|
|
|
|
let server_addr = std::env::args()
|
2025-06-08 04:11:58 -05:00
|
|
|
|
.nth(1)
|
|
|
|
|
|
.or_else(|| std::env::var("NAVKA_SERVER_ADDR").ok())
|
2025-06-17 20:54:31 -05:00
|
|
|
|
.unwrap_or_else(|| "http://127.0.0.1:50051".into());
|
2025-06-15 20:19:27 -05:00
|
|
|
|
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let (kbd_tx, _) = broadcast::channel::<KeyboardReport>(1024);
|
|
|
|
|
|
let (mou_tx, _) = broadcast::channel::<MouseReport>(4096);
|
2025-06-17 20:54:31 -05:00
|
|
|
|
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let mut agg = InputAggregator::new(dev_mode, kbd_tx.clone(), mou_tx.clone());
|
2025-06-17 20:54:31 -05:00
|
|
|
|
agg.init()?; // grab devices
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Self { aggregator: Some(agg),
|
|
|
|
|
|
server_addr, dev_mode,
|
|
|
|
|
|
kbd_tx, mou_tx })
|
2025-06-08 04:11:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn run(&mut self) -> Result<()> {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
/* detach the aggregator before spawn so `self` is not moved */
|
|
|
|
|
|
let aggregator = self.aggregator.take().expect("InputAggregator present");
|
2025-06-17 22:02:33 -05:00
|
|
|
|
let agg_task = tokio::spawn(async move {
|
|
|
|
|
|
let mut agg = aggregator;
|
|
|
|
|
|
agg.run().await
|
|
|
|
|
|
});
|
2025-06-08 04:11:58 -05:00
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
/* two networking tasks */
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let kbd_loop = self.stream_loop_keyboard();
|
|
|
|
|
|
let mou_loop = self.stream_loop_mouse();
|
|
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
/* optional suicide timer */
|
|
|
|
|
|
let suicide = async {
|
2025-06-08 18:11:44 -05:00
|
|
|
|
if self.dev_mode {
|
2025-06-15 20:19:27 -05:00
|
|
|
|
tokio::time::sleep(Duration::from_secs(30)).await;
|
2025-06-17 20:54:31 -05:00
|
|
|
|
warn!("dev‑mode timeout");
|
2025-06-18 01:41:11 -05:00
|
|
|
|
self.aggregator.keyboards.dev.ungrab();
|
|
|
|
|
|
self.aggregator.mice.dev.ungrab();
|
2025-06-17 20:54:31 -05:00
|
|
|
|
std::process::exit(0);
|
|
|
|
|
|
} else { futures::future::pending::<()>().await }
|
2025-06-08 13:35:23 -05:00
|
|
|
|
};
|
2025-06-08 13:11:31 -05:00
|
|
|
|
|
2025-06-08 13:35:23 -05:00
|
|
|
|
tokio::select! {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
_ = kbd_loop => unreachable!(),
|
|
|
|
|
|
_ = mou_loop => unreachable!(),
|
|
|
|
|
|
_ = suicide => unreachable!(),
|
|
|
|
|
|
// _ = suicide => { warn!("dev‑mode timeout"); std::process::exit(0) },
|
|
|
|
|
|
r = agg_task => {
|
|
|
|
|
|
error!("aggregator task ended: {r:?}");
|
|
|
|
|
|
std::process::exit(1)
|
2025-06-08 18:11:44 -05:00
|
|
|
|
}
|
2025-06-08 04:11:58 -05:00
|
|
|
|
}
|
2025-06-08 18:11:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
/*──────────────── keyboard stream ───────────────*/
|
2025-06-17 08:17:23 -05:00
|
|
|
|
async fn stream_loop_keyboard(&self) {
|
2025-06-08 18:11:44 -05:00
|
|
|
|
loop {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
info!("⌨️ connect {}", self.server_addr);
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let mut cli = match RelayClient::connect(self.server_addr.clone()).await {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(c) => c,
|
|
|
|
|
|
Err(e) => { error!("connect: {e}"); Self::delay().await; continue }
|
2025-06-08 18:11:44 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
let outbound = BroadcastStream::new(self.kbd_tx.subscribe()).filter_map(|r| r.ok());
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let resp = match cli.stream_keyboard(Request::new(outbound)).await {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(r) => r, Err(e) => { error!("stream_keyboard: {e}"); Self::delay().await; continue }
|
2025-06-17 08:17:23 -05:00
|
|
|
|
};
|
2025-06-15 22:15:50 -05:00
|
|
|
|
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let mut inbound = resp.into_inner();
|
|
|
|
|
|
while let Some(m) = inbound.message().await.transpose() {
|
|
|
|
|
|
match m {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(r) => debug!("kbd echo {} B", r.data.len()),
|
2025-06-17 08:17:23 -05:00
|
|
|
|
Err(e) => { error!("kbd inbound: {e}"); break }
|
2025-06-08 18:11:44 -05:00
|
|
|
|
}
|
2025-06-17 08:17:23 -05:00
|
|
|
|
}
|
2025-06-17 20:54:31 -05:00
|
|
|
|
warn!("⌨️ disconnected");
|
|
|
|
|
|
Self::delay().await;
|
2025-06-17 08:17:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
/*──────────────── mouse stream ──────────────────*/
|
2025-06-17 08:17:23 -05:00
|
|
|
|
async fn stream_loop_mouse(&self) {
|
|
|
|
|
|
loop {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
info!("🖱️ connect {}", self.server_addr);
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let mut cli = match RelayClient::connect(self.server_addr.clone()).await {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(c) => c,
|
|
|
|
|
|
Err(e) => { error!("connect: {e}"); Self::delay().await; continue }
|
2025-06-08 18:11:44 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-17 20:54:31 -05:00
|
|
|
|
let outbound = BroadcastStream::new(self.mou_tx.subscribe()).filter_map(|r| r.ok());
|
2025-06-17 08:17:23 -05:00
|
|
|
|
let resp = match cli.stream_mouse(Request::new(outbound)).await {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(r) => r, Err(e) => { error!("stream_mouse: {e}"); Self::delay().await; continue }
|
2025-06-17 08:17:23 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut inbound = resp.into_inner();
|
|
|
|
|
|
while let Some(m) = inbound.message().await.transpose() {
|
|
|
|
|
|
match m {
|
2025-06-17 20:54:31 -05:00
|
|
|
|
Ok(r) => debug!("mouse echo {} B", r.data.len()),
|
2025-06-17 08:17:23 -05:00
|
|
|
|
Err(e) => { error!("mouse inbound: {e}"); break }
|
2025-06-08 18:11:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-17 20:54:31 -05:00
|
|
|
|
warn!("🖱️ disconnected");
|
|
|
|
|
|
Self::delay().await;
|
2025-06-08 18:11:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-17 20:54:31 -05:00
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
|
async fn delay() { tokio::time::sleep(Duration::from_secs(1)).await; }
|
2025-06-08 04:11:58 -05:00
|
|
|
|
}
|