Skip to content

Commit 0dd413e

Browse files
authored
prevent authorization request spamming windows (#335)
1 parent 4e5a663 commit 0dd413e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

lan-mouse-gtk/src/window.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ impl Window {
474474
}
475475

476476
pub(super) fn request_authorization(&self, fingerprint: &str) {
477+
if let Some(w) = self.imp().authorization_window.borrow_mut().take() {
478+
w.close();
479+
}
477480
let window = AuthorizationWindow::new(fingerprint);
478481
window.set_transient_for(Some(self));
479482
window.connect_closure(
@@ -496,5 +499,6 @@ impl Window {
496499
}),
497500
);
498501
window.present();
502+
self.imp().authorization_window.replace(Some(window));
499503
}
500504
}

lan-mouse-gtk/src/window/imp.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use gtk::{gdk, gio, glib, Button, CompositeTemplate, Entry, Image, Label, ListBo
88

99
use lan_mouse_ipc::{FrontendRequestWriter, DEFAULT_PORT};
1010

11+
use crate::authorization_window::AuthorizationWindow;
12+
1113
#[derive(CompositeTemplate, Default)]
1214
#[template(resource = "/de/feschber/LanMouse/window.ui")]
1315
pub struct Window {
@@ -49,6 +51,7 @@ pub struct Window {
4951
pub port: Cell<u16>,
5052
pub capture_active: Cell<bool>,
5153
pub emulation_active: Cell<bool>,
54+
pub authorization_window: RefCell<Option<AuthorizationWindow>>,
5255
}
5356

5457
#[glib::object_subclass]

src/emulation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl ListenTask {
128128
async fn run(mut self) {
129129
let mut interval = tokio::time::interval(Duration::from_secs(5));
130130
let mut last_response = HashMap::new();
131+
let mut rejected_connections = HashMap::new();
131132
loop {
132133
select! {
133134
e = self.listener.next() => {match e {
@@ -156,7 +157,10 @@ impl ListenTask {
156157
self.event_tx.send(EmulationEvent::Connected { addr, fingerprint }).expect("channel closed");
157158
}
158159
Some(ListenEvent::Rejected { fingerprint }) => {
159-
self.event_tx.send(EmulationEvent::ConnectionAttempt { fingerprint }).expect("channel closed");
160+
if rejected_connections.insert(fingerprint.clone(), Instant::now())
161+
.is_none_or(|i| i.elapsed() >= Duration::from_secs(2)) {
162+
self.event_tx.send(EmulationEvent::ConnectionAttempt { fingerprint }).expect("channel closed");
163+
}
160164
}
161165
None => break
162166
}}

0 commit comments

Comments
 (0)