Skip to content

Commit 2898a44

Browse files
committed
refactor: [torrust#1096] count bad UDP request where the error is captured
Instead of captured the mapped error in the caller function when the error is already converted into a UDP error reponse. This prevents from parsing the error message to filter the error we are interesting in.
1 parent 30cae9b commit 2898a44

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/servers/udp/handlers.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use aquatic_udp_protocol::{
1111
ResponsePeer, ScrapeRequest, ScrapeResponse, TorrentScrapeStatistics, TransactionId,
1212
};
1313
use bittorrent_primitives::info_hash::InfoHash;
14+
use bloom::{CountingBloomFilter, ASMS};
15+
use tokio::sync::RwLock;
1416
use torrust_tracker_clock::clock::Time as _;
1517
use tracing::{instrument, Level};
1618
use uuid::Uuid;
@@ -51,12 +53,13 @@ impl CookieTimeValues {
5153
/// - Delegating the request to the correct handler depending on the request type.
5254
///
5355
/// It will return an `Error` response if the request is invalid.
54-
#[instrument(fields(request_id), skip(udp_request, tracker, cookie_time_values), ret(level = Level::TRACE))]
56+
#[instrument(fields(request_id), skip(udp_request, tracker, cookie_time_values, cbf), ret(level = Level::TRACE))]
5557
pub(crate) async fn handle_packet(
5658
udp_request: RawRequest,
5759
tracker: &Tracker,
5860
local_addr: SocketAddr,
5961
cookie_time_values: CookieTimeValues,
62+
cbf: Arc<RwLock<CountingBloomFilter>>,
6063
) -> Response {
6164
tracing::Span::current().record("request_id", Uuid::new_v4().to_string());
6265
tracing::debug!("Handling Packets: {udp_request:?}");
@@ -68,6 +71,16 @@ pub(crate) async fn handle_packet(
6871
Ok(request) => match handle_request(request, udp_request.from, tracker, cookie_time_values.clone()).await {
6972
Ok(response) => return response,
7073
Err((e, transaction_id)) => {
74+
match &e {
75+
Error::CookieValueNotNormal { .. }
76+
| Error::CookieValueExpired { .. }
77+
| Error::CookieValueFromFuture { .. } => {
78+
// code-review: should we include `RequestParseError` and `BadRequest`?
79+
cbf.write().await.insert(&udp_request.from.ip().to_string());
80+
}
81+
_ => {}
82+
}
83+
7184
handle_error(
7285
udp_request.from,
7386
tracker,

src/servers/udp/server/processor.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::net::SocketAddr;
33
use std::sync::Arc;
44

55
use aquatic_udp_protocol::Response;
6-
use bloom::{CountingBloomFilter, ASMS};
6+
use bloom::CountingBloomFilter;
77
use tokio::sync::RwLock;
88
use tracing::{instrument, Level};
99

@@ -35,15 +35,10 @@ impl Processor {
3535
&self.tracker,
3636
self.socket.address(),
3737
CookieTimeValues::new(self.cookie_lifetime),
38+
cbf,
3839
)
3940
.await;
4041

41-
if let Response::Error(err) = &response {
42-
if err.message.contains("cookie value is expired") || err.message.contains("cookie value is from future") {
43-
cbf.write().await.insert(&from.ip().to_string());
44-
}
45-
}
46-
4742
self.send_response(from, response).await;
4843
}
4944

0 commit comments

Comments
 (0)