From 5634686c5acef77762fc12f48ff23a508ec5d623 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Wed, 7 Oct 2026 22:17:16 +0800 Subject: [PATCH] feat(hbbs): make the punch-request record cap configurable MAX_PUNCH_REQUESTS sets how many records the punch-requests console command keeps (default 10000); 0 disables recording. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8 --- docs/environment-variables.md | 1 + src/rendezvous_server.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 488921f..d5633a3 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -48,6 +48,7 @@ in the inherited process environment. | `ALWAYS_USE_RELAY` 🅴 | *(none)* | `N` | `Y` forces every session through a relay (disables direct/hole‑punched connections). At runtime, send `always-use-relay Y` or `always-use-relay N` to the `hbbs` [loopback console](#runtime-console). | | `DB_URL` 🅴 | *(none)* | `./db_v2.sqlite3` | Path/URL of the SQLite database file. See [Database](#database). | | `MAX_DATABASE_CONNECTIONS` 🅴 | *(none)* | `1` | Size of the SQLite connection pool. | +| `MAX_PUNCH_REQUESTS` 🅴 | *(none)* | `10000` | Number of punch-request records kept in memory for the `punch-requests` [console](#runtime-console) command; the oldest is dropped when full. `0` disables recording. | 🅴 = set through the inherited process environment. diff --git a/src/rendezvous_server.rs b/src/rendezvous_server.rs index 50b55b5..57bc5b8 100644 --- a/src/rendezvous_server.rs +++ b/src/rendezvous_server.rs @@ -68,9 +68,12 @@ use tokio::sync::Mutex as TokioMutex; // differentiate if needed struct PunchReqEntry { tm: Instant, from_ip: String, to_ip: String, to_id: String } static PUNCH_REQS: Lazy>> = Lazy::new(|| TokioMutex::new(VecDeque::new())); const PUNCH_REQ_DEDUPE_SEC: u64 = 60; -const PUNCH_REQ_MAX: usize = 10_000; +static PUNCH_REQ_MAX: Lazy = + Lazy::new(|| get_arg("MAX_PUNCH_REQUESTS").parse().unwrap_or(10_000)); async fn record_punch_req(from_ip: String, to_ip: String, to_id: String) { + let max = *PUNCH_REQ_MAX; + if max == 0 { return; } let mut lock = PUNCH_REQS.lock().await; let mut dup = false; for e in lock.iter().rev().take(30) { // only check recent tail subset for speed @@ -80,7 +83,7 @@ async fn record_punch_req(from_ip: String, to_ip: String, to_id: String) { } } if !dup { - if lock.len() >= PUNCH_REQ_MAX { lock.pop_front(); } + if lock.len() >= max { lock.pop_front(); } lock.push_back(PunchReqEntry { tm: Instant::now(), from_ip, to_ip, to_id }); } } @@ -1430,7 +1433,7 @@ mod tests { #[hbb_common::tokio::test] async fn punch_reqs_bounded() { // 31 ids keep every request just outside the 30-entry dedupe window - for i in 0..PUNCH_REQ_MAX * 2 { + for i in 0..*PUNCH_REQ_MAX * 2 { record_punch_req( "1.2.3.4".to_owned(), "5.6.7.8".to_owned(), @@ -1438,6 +1441,6 @@ mod tests { ) .await; } - assert_eq!(PUNCH_REQS.lock().await.len(), PUNCH_REQ_MAX); + assert_eq!(PUNCH_REQS.lock().await.len(), *PUNCH_REQ_MAX); } }