diff --git a/lan-mouse-cli/src/lib.rs b/lan-mouse-cli/src/lib.rs index 3a22bd0..f74a406 100644 --- a/lan-mouse-cli/src/lib.rs +++ b/lan-mouse-cli/src/lib.rs @@ -283,8 +283,8 @@ impl Cli { FrontendEvent::PublicKeyFingerprint(fp) => { eprintln!("the public key fingerprint of this device is {fp}"); } - FrontendEvent::IncomingConnected(fp, addr, pos) => {} - FrontendEvent::IncomingDisconnected(fp) => {} + FrontendEvent::IncomingConnected(..) => {} + FrontendEvent::IncomingDisconnected(..) => {} } } diff --git a/lan-mouse-gtk/src/key_row.rs b/lan-mouse-gtk/src/key_row.rs index cfc0d9a..bb8b886 100644 --- a/lan-mouse-gtk/src/key_row.rs +++ b/lan-mouse-gtk/src/key_row.rs @@ -12,6 +12,12 @@ glib::wrapper! { @implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget; } +impl Default for KeyRow { + fn default() -> Self { + Self::new() + } +} + impl KeyRow { pub fn new() -> Self { Object::builder().build() diff --git a/lan-mouse-gtk/src/lib.rs b/lan-mouse-gtk/src/lib.rs index efbc99c..005b55d 100644 --- a/lan-mouse-gtk/src/lib.rs +++ b/lan-mouse-gtk/src/lib.rs @@ -142,8 +142,8 @@ fn build_ui(app: &Application) { FrontendEvent::PublicKeyFingerprint(fp) => { window.set_pk_fp(&fp); } - FrontendEvent::IncomingConnected(fp, addr, pos) => {} - FrontendEvent::IncomingDisconnected(fp) => {} + FrontendEvent::IncomingConnected(..) => {} + FrontendEvent::IncomingDisconnected(..) => {} } } } diff --git a/src/config.rs b/src/config.rs index b94b3a5..74dba56 100644 --- a/src/config.rs +++ b/src/config.rs @@ -332,8 +332,7 @@ impl Config { let authorized_fingerprints = config_toml .as_mut() - .map(|c| std::mem::take(&mut c.authorized_fingerprints)) - .flatten() + .and_then(|c| std::mem::take(&mut c.authorized_fingerprints)) .unwrap_or_default(); let mut clients: Vec<(TomlClient, Position)> = vec![]; diff --git a/src/connect.rs b/src/connect.rs index ac30270..c40fa7e 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -223,7 +223,7 @@ async fn receive_loop( tx: Sender<(ClientHandle, ProtoEvent)>, ) { let mut buf = [0u8; MAX_EVENT_SIZE]; - while let Ok(_) = conn.recv(&mut buf).await { + while conn.recv(&mut buf).await.is_ok() { if let Ok(event) = buf.try_into() { match event { ProtoEvent::Pong(b) => { diff --git a/src/crypto.rs b/src/crypto.rs index ddbb285..faacb94 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -25,12 +25,11 @@ pub fn generate_fingerprint(cert: &[u8]) -> String { .iter() .map(|x| format!("{x:02x}")) .collect::>(); - let fingerprint = bytes.join(":").to_lowercase(); - fingerprint + bytes.join(":").to_lowercase() } pub fn certificate_fingerprint(cert: &Certificate) -> String { - let certificate = cert.certificate.get(0).expect("certificate missing"); + let certificate = cert.certificate.first().expect("certificate missing"); generate_fingerprint(certificate) } @@ -46,9 +45,9 @@ pub fn load_certificate(path: &Path) -> Result { pub(crate) fn load_or_generate_key_and_cert(path: &Path) -> Result { if path.exists() && path.is_file() { - return Ok(load_certificate(path)?); + Ok(load_certificate(path)?) } else { - return Ok(generate_key_and_cert(path)?); + generate_key_and_cert(path) } } diff --git a/src/listen.rs b/src/listen.rs index dac0b10..9a288d3 100644 --- a/src/listen.rs +++ b/src/listen.rs @@ -32,11 +32,13 @@ pub enum ListenerCreationError { WebrtcDtls(#[from] webrtc_dtls::Error), } +type ArcConn = Arc; + pub(crate) struct LanMouseListener { listen_rx: Receiver<(ProtoEvent, SocketAddr)>, listen_tx: Sender<(ProtoEvent, SocketAddr)>, listen_task: JoinHandle<()>, - conns: Rc)>>>, + conns: Rc>>, } type VerifyPeerCertificateFn = Arc< @@ -82,8 +84,7 @@ impl LanMouseListener { let listener = listen(listen_addr, cfg).await?; - let conns: Rc)>>> = - Rc::new(Mutex::new(Vec::new())); + let conns: Rc>> = Rc::new(Mutex::new(Vec::new())); let conns_clone = conns.clone(); @@ -156,7 +157,7 @@ impl LanMouseListener { { let conn: &DTLSConn = conn.as_any().downcast_ref().expect("dtls conn"); let certs = conn.connection_state().await.peer_certificates; - let cert = certs.get(0)?; + let cert = certs.first()?; let fingerprint = crypto::generate_fingerprint(cert); Some(fingerprint) } else { @@ -177,9 +178,9 @@ impl Stream for LanMouseListener { } async fn read_loop( - conns: Rc)>>>, + conns: Rc>>, addr: SocketAddr, - conn: Arc, + conn: ArcConn, dtls_tx: Sender<(ProtoEvent, SocketAddr)>, ) -> Result<(), Error> { let mut b = [0u8; MAX_EVENT_SIZE]; diff --git a/src/service.rs b/src/service.rs index 29f6d09..e0d6394 100644 --- a/src/service.rs +++ b/src/service.rs @@ -22,7 +22,6 @@ use std::{ net::{IpAddr, SocketAddr}, rc::Rc, sync::{Arc, RwLock}, - u64, }; use thiserror::Error; use tokio::{signal, sync::Notify}; @@ -56,6 +55,13 @@ enum IncomingEvent { }, } +#[derive(Debug)] +pub struct Incoming { + fingerprint: String, + addr: SocketAddr, + pos: Position, +} + #[derive(Clone)] pub struct Service { active: Rc>>, @@ -70,7 +76,7 @@ pub struct Service { capture_status: Rc>, pub(crate) emulation_status: Rc>, pub(crate) should_release: Rc>>, - incoming_conns: Rc>>, + incoming_conns: Rc>>, cert: Certificate, next_trigger_handle: u64, } @@ -203,8 +209,8 @@ impl Service { } }, handle = capture.entered() => { - if let Some((_fp, addr, _pos)) = self.incoming_conns.borrow().get(&handle) { - emulation.notify_release(*addr); + if let Some(incoming) = self.incoming_conns.borrow().get(&handle) { + emulation.notify_release(incoming.addr); } }, _ = self.cancelled() => break, @@ -237,9 +243,14 @@ impl Service { let handle = Self::ENTER_HANDLE_BEGIN + self.next_trigger_handle; self.next_trigger_handle += 1; capture.create(handle, pos); - self.incoming_conns - .borrow_mut() - .insert(handle, (fingerprint, addr, pos)); + self.incoming_conns.borrow_mut().insert( + handle, + Incoming { + fingerprint, + addr, + pos, + }, + ); } fn remove_incoming(&mut self, addr: SocketAddr, capture: &Capture) -> Option { @@ -247,20 +258,20 @@ impl Service { .incoming_conns .borrow() .iter() - .find(|(_, (_, a, _))| *a == addr) + .find(|(_, incoming)| incoming.addr == addr) .map(|(k, _)| *k)?; capture.destroy(handle); self.incoming_conns .borrow_mut() .remove(&handle) - .map(|(f, _, _)| f) + .map(|incoming| incoming.fingerprint) } pub(crate) fn get_incoming_pos(&self, handle: ClientHandle) -> Option { self.incoming_conns .borrow() .get(&handle) - .map(|&(_, _, p)| p) + .map(|incoming| incoming.pos) } fn notify_frontend(&self, event: FrontendEvent) {