From 511bba91f84cc79ce4b44876ba14c9ccd64fdfc5 Mon Sep 17 00:00:00 2001 From: rustdesk <71636191+rustdesk@users.noreply.github.com> Date: Sun, 8 Nov 2026 12:12:39 +0800 Subject: [PATCH] Match either form of an IPv4-mapped client at accept too handle_connection checked the blocklist in plain IPv4 form only, so an entry in the ::ffff: form, the one usage prints, let the client connect. If that client was the leg parked first, relay() never checks it and the whole session ran. It now matches either form, as relay() does. The relay() test gains a client that is not listed, which must outlive the first list check, and a new test covers the accept check. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq --- src/relay_server.rs | 47 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/relay_server.rs b/src/relay_server.rs index 877f885..78d501c 100644 --- a/src/relay_server.rs +++ b/src/relay_server.rs @@ -422,7 +422,9 @@ async fn handle_connection( return; } let ip = ip.to_string(); - if BLOCKLIST.read().await.get(&ip).is_some() { + let reported = addr.ip().to_string(); + let listed = |list: &HashSet| list.contains(&ip) || list.contains(&reported); + if listed(&*BLOCKLIST.read().await) { log::info!("{} blocked", ip); return; } @@ -698,9 +700,14 @@ mod tests { } #[tokio::test] - async fn a_blocklisted_ipv4_client_on_the_dual_stack_listener_is_cut_off() { + async fn only_a_blocklisted_ipv4_client_on_the_dual_stack_listener_is_cut_off() { // The plain form is what `ba` is usually given; the mapped one is what `usage` shows. - for entry in ["192.0.2.55", "::ffff:192.0.2.55"] { + // A client not listed must outlive the first list check, which runs at 3 s. + for (entry, client, cut_off) in [ + ("192.0.2.55", "[::ffff:192.0.2.55]:1", true), + ("::ffff:192.0.2.55", "[::ffff:192.0.2.55]:1", true), + ("192.0.2.55", "[::ffff:192.0.2.56]:1", false), + ] { BLOCKLIST.write().await.insert(entry.to_owned()); let mut stream = TestStream { data: None, @@ -711,9 +718,9 @@ mod tests { blocked: false, }); let result = timeout( - 10_000, + if cut_off { 10_000 } else { 4_000 }, relay( - "[::ffff:192.0.2.55]:1".parse().unwrap(), + client.parse().unwrap(), &mut stream, &mut peer, Limiter::new(1_000_000.), @@ -722,9 +729,35 @@ mod tests { ) .await; BLOCKLIST.write().await.remove(entry); - assert!( + assert_eq!( matches!(result, Ok(Ok(()))), - "the relay kept a client blocklisted as {entry}" + cut_off, + "client {client}, blocklist entry {entry}" + ); + } + } + + #[tokio::test] + async fn a_client_blocklisted_in_either_form_is_refused_at_accept() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + for (entry, refused) in [ + ("192.0.2.57", true), + ("::ffff:192.0.2.57", true), + ("192.0.2.58", false), + ] { + BLOCKLIST.write().await.insert(entry.to_owned()); + let mut client = TcpStream::connect(listener.local_addr().unwrap()) + .await + .unwrap(); + let (stream, _) = listener.accept().await.unwrap(); + let addr = "[::ffff:192.0.2.57]:1".parse().unwrap(); + handle_connection(stream, addr, &Limiter::new(1_000_000.), "", false).await; + BLOCKLIST.write().await.remove(entry); + let read = timeout(1_000, client.read(&mut [0; 1])).await; + assert_eq!( + matches!(read, Ok(Ok(0))), + refused, + "blocklist entry {entry}" ); } }