fix(hbbr): match IPv4 clients against the relay lists in their IPv4 form (#748)

* Match IPv4 clients against the relay lists in their IPv4 form

hbbr listens dual-stack by default, so a direct IPv4 client reaches
relay() as ::ffff:a.b.c.d. relay() checked BLACKLIST and BLOCKLIST with
that form, while the lists hold the plain IPv4 that `ba` and `Ba` are
given. A blacklisted IPv4 client was therefore never throttled, and
adding it to the blocklist did not end its session. handle_connection
already normalizes with try_into_v4 for its accept-time check; relay()
now does the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq

* Keep IPv4-mapped list entries matching in relay()

The previous commit looked the lists up in the plain IPv4 form only, so
an entry in the ::ffff:a.b.c.d form stopped matching. That is the form
`usage` and the pairing logs print, so an admin copying an address from
them, or an existing list file holding one, lost the throttle and the
mid-session blocklist check. relay() now matches either form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq

* 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq

* Require a timeout in the list tests' unlisted cases

An unlisted client passed if relay() or the connection ended with an
error, not only if it stayed up until the timeout. Each case now names
the one outcome it expects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JAxkkiaGp2k8okvs4MpCUq

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RustDeskandClaude Opus 5.5 authored and GitHub committed 2026-11-08 15:33:32 +08:00
1 parent 0415b0e057
commit 680b60395e
1 file changed
+69 -3
+69 -3
View File
@@ -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<String>| list.contains(&ip) || list.contains(&reported);
if listed(&*BLOCKLIST.read().await) {
log::info!("{} blocked", ip);
return;
}
@@ -545,6 +547,9 @@ async fn relay(
id: String,
) -> ResultType<()> {
let ip = addr.ip().to_string();
// The dual-stack listener reports an IPv4 client as ::ffff:a.b.c.d; lists may hold either form.
let unmapped = hbb_common::try_into_v4(addr).ip().to_string();
let listed = |list: &HashSet<String>| list.contains(&ip) || list.contains(&unmapped);
let mut tm = std::time::Instant::now();
let mut elapsed = 0;
let mut total = 0;
@@ -608,11 +613,11 @@ async fn relay(
let n = tm.elapsed().as_millis() as usize;
if n >= 1_000 {
if BLOCKLIST.read().await.get(&ip).is_some() {
if listed(&*BLOCKLIST.read().await) {
log::info!("{} blocked", ip);
break;
}
blacked = BLACKLIST.read().await.get(&ip).is_some();
blacked = listed(&*BLACKLIST.read().await);
tm = std::time::Instant::now();
let speed = total_s / n;
if speed > highest_s {
@@ -693,6 +698,67 @@ mod tests {
async fn blocked_writes_in_both_directions_time_out() {
tokio::join!(check_blocked_send(false), check_blocked_send(true));
}
#[tokio::test]
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.
// 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,
blocked: false,
};
let mut peer: Box<dyn StreamTrait> = Box::new(TestStream {
data: None,
blocked: false,
});
let result = timeout(
if cut_off { 10_000 } else { 4_000 },
relay(
client.parse().unwrap(),
&mut stream,
&mut peer,
Limiter::new(1_000_000.),
"blocklist-test".to_owned(),
),
)
.await;
BLOCKLIST.write().await.remove(entry);
assert!(
matches!((cut_off, &result), (true, Ok(Ok(()))) | (false, Err(_))),
"client {client}, blocklist entry {entry}: {result:?}"
);
}
}
#[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!(
matches!((refused, &read), (true, Ok(Ok(0))) | (false, Err(_))),
"blocklist entry {entry}: {read:?}"
);
}
}
}
/// Returns the configured relay key or resolves a signing key to its public value.