From d49ba36b34461e0cac4c8bc2d7c40dead738766a Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Mon, 27 Oct 2025 17:28:45 +0100 Subject: [PATCH] prevent authorization request spamming windows --- lan-mouse-gtk/src/window.rs | 4 ++++ lan-mouse-gtk/src/window/imp.rs | 3 +++ src/emulation.rs | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lan-mouse-gtk/src/window.rs b/lan-mouse-gtk/src/window.rs index ef94e91..7ce961f 100644 --- a/lan-mouse-gtk/src/window.rs +++ b/lan-mouse-gtk/src/window.rs @@ -474,6 +474,9 @@ impl Window { } pub(super) fn request_authorization(&self, fingerprint: &str) { + if let Some(w) = self.imp().authorization_window.borrow_mut().take() { + w.close(); + } let window = AuthorizationWindow::new(fingerprint); window.set_transient_for(Some(self)); window.connect_closure( @@ -496,5 +499,6 @@ impl Window { }), ); window.present(); + self.imp().authorization_window.replace(Some(window)); } } diff --git a/lan-mouse-gtk/src/window/imp.rs b/lan-mouse-gtk/src/window/imp.rs index f0a9b77..b64dad1 100644 --- a/lan-mouse-gtk/src/window/imp.rs +++ b/lan-mouse-gtk/src/window/imp.rs @@ -8,6 +8,8 @@ use gtk::{gdk, gio, glib, Button, CompositeTemplate, Entry, Image, Label, ListBo use lan_mouse_ipc::{FrontendRequestWriter, DEFAULT_PORT}; +use crate::authorization_window::AuthorizationWindow; + #[derive(CompositeTemplate, Default)] #[template(resource = "/de/feschber/LanMouse/window.ui")] pub struct Window { @@ -49,6 +51,7 @@ pub struct Window { pub port: Cell, pub capture_active: Cell, pub emulation_active: Cell, + pub authorization_window: RefCell>, } #[glib::object_subclass] diff --git a/src/emulation.rs b/src/emulation.rs index 4689556..c8f1331 100644 --- a/src/emulation.rs +++ b/src/emulation.rs @@ -128,6 +128,7 @@ impl ListenTask { async fn run(mut self) { let mut interval = tokio::time::interval(Duration::from_secs(5)); let mut last_response = HashMap::new(); + let mut rejected_connections = HashMap::new(); loop { select! { e = self.listener.next() => {match e { @@ -156,7 +157,10 @@ impl ListenTask { self.event_tx.send(EmulationEvent::Connected { addr, fingerprint }).expect("channel closed"); } Some(ListenEvent::Rejected { fingerprint }) => { - self.event_tx.send(EmulationEvent::ConnectionAttempt { fingerprint }).expect("channel closed"); + if rejected_connections.insert(fingerprint.clone(), Instant::now()) + .is_none_or(|i| i.elapsed() >= Duration::from_secs(2)) { + self.event_tx.send(EmulationEvent::ConnectionAttempt { fingerprint }).expect("channel closed"); + } } None => break }}