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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
This commit is contained in:
rustdesk
2026-10-07 22:17:16 +08:00
co-authored by Claude Opus 5.5
parent 1cc5895ac0
commit 5634686c5a
2 changed files with 8 additions and 4 deletions
+1
View File
@@ -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.
+7 -4
View File
@@ -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<TokioMutex<VecDeque<PunchReqEntry>>> = 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<usize> =
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);
}
}