Skip to content

Commit a422e4e

Browse files
committed
refactor: extract type RemoteClientAddr
1 parent 7ed7500 commit a422e4e

File tree

5 files changed

+82
-60
lines changed

5 files changed

+82
-60
lines changed

packages/http-tracker-core/src/event/mod.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use torrust_tracker_metrics::label_name;
66
use torrust_tracker_primitives::peer::PeerAnnouncement;
77
use torrust_tracker_primitives::service_binding::ServiceBinding;
88

9+
use crate::services::RemoteClientAddr;
10+
911
pub mod sender;
1012

1113
/// A HTTP core event.
@@ -29,12 +31,9 @@ pub struct ConnectionContext {
2931

3032
impl ConnectionContext {
3133
#[must_use]
32-
pub fn new(client_ip_addr: IpAddr, opt_client_port: Option<u16>, server_service_binding: ServiceBinding) -> Self {
34+
pub fn new(remote_client_addr: RemoteClientAddr, server_service_binding: ServiceBinding) -> Self {
3335
Self {
34-
client: ClientConnectionContext {
35-
ip_addr: client_ip_addr,
36-
port: opt_client_port,
37-
},
36+
client: ClientConnectionContext { remote_client_addr },
3837
server: ServerConnectionContext {
3938
service_binding: server_service_binding,
4039
},
@@ -43,12 +42,12 @@ impl ConnectionContext {
4342

4443
#[must_use]
4544
pub fn client_ip_addr(&self) -> IpAddr {
46-
self.client.ip_addr
45+
self.client.ip_addr()
4746
}
4847

4948
#[must_use]
5049
pub fn client_port(&self) -> Option<u16> {
51-
self.client.port
50+
self.client.port()
5251
}
5352

5453
#[must_use]
@@ -59,10 +58,19 @@ impl ConnectionContext {
5958

6059
#[derive(Debug, PartialEq, Eq, Clone)]
6160
pub struct ClientConnectionContext {
62-
ip_addr: IpAddr,
61+
remote_client_addr: RemoteClientAddr,
62+
}
63+
64+
impl ClientConnectionContext {
65+
#[must_use]
66+
pub fn ip_addr(&self) -> IpAddr {
67+
self.remote_client_addr.ip
68+
}
6369

64-
/// It's provided if you use the `torrust-axum-http-tracker-server` crate.
65-
port: Option<u16>,
70+
#[must_use]
71+
pub fn port(&self) -> Option<u16> {
72+
self.remote_client_addr.port
73+
}
6674
}
6775

6876
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -96,6 +104,7 @@ pub mod test {
96104
use torrust_tracker_primitives::service_binding::Protocol;
97105

98106
use super::Event;
107+
use crate::services::RemoteClientAddr;
99108
use crate::tests::sample_info_hash;
100109

101110
#[must_use]
@@ -134,8 +143,7 @@ pub mod test {
134143

135144
let event1 = Event::TcpAnnounce {
136145
connection: ConnectionContext::new(
137-
remote_client_ip,
138-
Some(8080),
146+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
139147
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
140148
),
141149
info_hash,
@@ -144,8 +152,7 @@ pub mod test {
144152

145153
let event2 = Event::TcpAnnounce {
146154
connection: ConnectionContext::new(
147-
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
148-
Some(8080),
155+
RemoteClientAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), Some(8080)),
149156
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
150157
),
151158
info_hash,

packages/http-tracker-core/src/services/announce.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
//! It also sends an [`http_tracker_core::event::Event`]
99
//! because events are specific for the HTTP tracker.
10-
use std::net::IpAddr;
1110
use std::panic::Location;
1211
use std::sync::Arc;
1312

@@ -24,7 +23,7 @@ use torrust_tracker_primitives::core::AnnounceData;
2423
use torrust_tracker_primitives::peer::PeerAnnouncement;
2524
use torrust_tracker_primitives::service_binding::ServiceBinding;
2625

27-
use super::resolve_remote_client_ip;
26+
use super::{resolve_remote_client_addr, RemoteClientAddr};
2827
use crate::event;
2928
use crate::event::Event;
3029

@@ -79,22 +78,20 @@ impl AnnounceService {
7978

8079
self.authorize(announce_request.info_hash).await?;
8180

82-
let (remote_client_ip, opt_remote_client_port) =
83-
resolve_remote_client_ip(self.core_config.net.on_reverse_proxy, client_ip_sources)?;
81+
let remote_client_addr = resolve_remote_client_addr(self.core_config.net.on_reverse_proxy, client_ip_sources)?;
8482

85-
let mut peer = peer_from_request(announce_request, &remote_client_ip);
83+
let mut peer = peer_from_request(announce_request, &remote_client_addr.ip);
8684

8785
let peers_wanted = Self::peers_wanted(announce_request);
8886

8987
let announce_data = self
9088
.announce_handler
91-
.announce(&announce_request.info_hash, &mut peer, &remote_client_ip, &peers_wanted)
89+
.announce(&announce_request.info_hash, &mut peer, &remote_client_addr.ip, &peers_wanted)
9290
.await?;
9391

9492
self.send_event(
9593
announce_request.info_hash,
96-
remote_client_ip,
97-
opt_remote_client_port,
94+
remote_client_addr,
9895
server_service_binding.clone(),
9996
peer,
10097
)
@@ -130,14 +127,13 @@ impl AnnounceService {
130127
async fn send_event(
131128
&self,
132129
info_hash: InfoHash,
133-
remote_client_ip: IpAddr,
134-
opt_peer_ip_port: Option<u16>,
130+
remote_client_addr: RemoteClientAddr,
135131
server_service_binding: ServiceBinding,
136132
announcement: PeerAnnouncement,
137133
) {
138134
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
139135
let event = Event::TcpAnnounce {
140-
connection: event::ConnectionContext::new(remote_client_ip, opt_peer_ip_port, server_service_binding),
136+
connection: event::ConnectionContext::new(remote_client_addr, server_service_binding),
141137
info_hash,
142138
announcement,
143139
};
@@ -323,6 +319,7 @@ mod tests {
323319
MockHttpStatsEventSender,
324320
};
325321
use crate::services::announce::AnnounceService;
322+
use crate::services::RemoteClientAddr;
326323
use crate::tests::{sample_info_hash, sample_peer, sample_peer_using_ipv4, sample_peer_using_ipv6};
327324

328325
#[tokio::test]
@@ -383,7 +380,10 @@ mod tests {
383380
announcement.peer_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080);
384381

385382
let expected_event = Event::TcpAnnounce {
386-
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
383+
connection: ConnectionContext::new(
384+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
385+
server_service_binding.clone(),
386+
),
387387
info_hash: sample_info_hash(),
388388
announcement,
389389
};
@@ -457,7 +457,10 @@ mod tests {
457457
);
458458

459459
let expected_event = Event::TcpAnnounce {
460-
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
460+
connection: ConnectionContext::new(
461+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
462+
server_service_binding.clone(),
463+
),
461464
info_hash: sample_info_hash(),
462465
announcement: peer_announcement,
463466
};
@@ -504,7 +507,10 @@ mod tests {
504507
.expect_send_event()
505508
.with(predicate::function(move |event| {
506509
let expected_event = Event::TcpAnnounce {
507-
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
510+
connection: ConnectionContext::new(
511+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
512+
server_service_binding.clone(),
513+
),
508514
info_hash: sample_info_hash(),
509515
announcement: peer,
510516
};

packages/http-tracker-core/src/services/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@ use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::{self, Cli
1212
pub mod announce;
1313
pub mod scrape;
1414

15+
#[derive(Debug, PartialEq, Eq, Clone)]
16+
pub struct RemoteClientAddr {
17+
pub ip: IpAddr,
18+
pub port: Option<u16>,
19+
}
20+
21+
impl RemoteClientAddr {
22+
#[must_use]
23+
pub fn new(ip: IpAddr, port: Option<u16>) -> Self {
24+
Self { ip, port }
25+
}
26+
}
27+
1528
/// Resolves the client's real IP address considering proxy headers
1629
///
1730
/// # Errors
1831
///
1932
/// This function returns an error if the IP address cannot be resolved.
20-
pub fn resolve_remote_client_ip(
33+
pub fn resolve_remote_client_addr(
2134
on_reverse_proxy: bool,
2235
client_ip_sources: &ClientIpSources,
23-
) -> Result<(IpAddr, Option<u16>), PeerIpResolutionError> {
36+
) -> Result<RemoteClientAddr, PeerIpResolutionError> {
2437
let ip = match peer_ip_resolver::invoke(on_reverse_proxy, client_ip_sources) {
2538
Ok(peer_ip) => Ok(peer_ip),
2639
Err(error) => Err(error),
@@ -34,5 +47,5 @@ pub fn resolve_remote_client_ip(
3447
None
3548
};
3649

37-
Ok((ip, port))
50+
Ok(RemoteClientAddr { ip, port })
3851
}

packages/http-tracker-core/src/services/scrape.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
//! It also sends an [`http_tracker_core::statistics::event::Event`]
99
//! because events are specific for the HTTP tracker.
10-
use std::net::IpAddr;
1110
use std::sync::Arc;
1211

1312
use bittorrent_http_tracker_protocol::v1::requests::scrape::Scrape;
@@ -20,7 +19,7 @@ use torrust_tracker_configuration::Core;
2019
use torrust_tracker_primitives::core::ScrapeData;
2120
use torrust_tracker_primitives::service_binding::ServiceBinding;
2221

23-
use super::resolve_remote_client_ip;
22+
use super::{resolve_remote_client_addr, RemoteClientAddr};
2423
use crate::event;
2524
use crate::event::{ConnectionContext, Event};
2625

@@ -82,11 +81,9 @@ impl ScrapeService {
8281
self.scrape_handler.scrape(&scrape_request.info_hashes).await?
8382
};
8483

85-
let (remote_client_ip, opt_client_port) =
86-
resolve_remote_client_ip(self.core_config.net.on_reverse_proxy, client_ip_sources)?;
84+
let remote_client_addr = resolve_remote_client_addr(self.core_config.net.on_reverse_proxy, client_ip_sources)?;
8785

88-
self.send_event(remote_client_ip, opt_client_port, server_service_binding.clone())
89-
.await;
86+
self.send_event(remote_client_addr, server_service_binding.clone()).await;
9087

9188
Ok(scrape_data)
9289
}
@@ -103,15 +100,10 @@ impl ScrapeService {
103100
false
104101
}
105102

106-
async fn send_event(
107-
&self,
108-
original_peer_ip: IpAddr,
109-
opt_original_peer_port: Option<u16>,
110-
server_service_binding: ServiceBinding,
111-
) {
103+
async fn send_event(&self, remote_client_addr: RemoteClientAddr, server_service_binding: ServiceBinding) {
112104
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
113105
let event = Event::TcpScrape {
114-
connection: ConnectionContext::new(original_peer_ip, opt_original_peer_port, server_service_binding),
106+
connection: ConnectionContext::new(remote_client_addr, server_service_binding),
115107
};
116108

117109
tracing::debug!("Sending TcpScrape event: {:?}", event);
@@ -271,6 +263,7 @@ mod tests {
271263
initialize_services_with_configuration, sample_info_hashes, sample_peer, MockHttpStatsEventSender,
272264
};
273265
use crate::services::scrape::ScrapeService;
266+
use crate::services::RemoteClientAddr;
274267
use crate::tests::sample_info_hash;
275268
use crate::{event, statistics};
276269

@@ -342,8 +335,7 @@ mod tests {
342335
.expect_send_event()
343336
.with(eq(Event::TcpScrape {
344337
connection: ConnectionContext::new(
345-
IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)),
346-
Some(8080),
338+
RemoteClientAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), Some(8080)),
347339
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070))
348340
.unwrap(),
349341
),
@@ -394,8 +386,10 @@ mod tests {
394386
.expect_send_event()
395387
.with(eq(Event::TcpScrape {
396388
connection: ConnectionContext::new(
397-
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
398-
Some(8080),
389+
RemoteClientAddr::new(
390+
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
391+
Some(8080),
392+
),
399393
server_service_binding,
400394
),
401395
}))
@@ -453,6 +447,7 @@ mod tests {
453447
initialize_services_with_configuration, sample_info_hashes, sample_peer, MockHttpStatsEventSender,
454448
};
455449
use crate::services::scrape::ScrapeService;
450+
use crate::services::RemoteClientAddr;
456451
use crate::tests::sample_info_hash;
457452
use crate::{event, statistics};
458453

@@ -518,8 +513,7 @@ mod tests {
518513
.expect_send_event()
519514
.with(eq(Event::TcpScrape {
520515
connection: ConnectionContext::new(
521-
IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)),
522-
Some(8080),
516+
RemoteClientAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), Some(8080)),
523517
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070))
524518
.unwrap(),
525519
),
@@ -570,8 +564,10 @@ mod tests {
570564
.expect_send_event()
571565
.with(eq(Event::TcpScrape {
572566
connection: ConnectionContext::new(
573-
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
574-
Some(8080),
567+
RemoteClientAddr::new(
568+
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
569+
Some(8080),
570+
),
575571
server_service_binding,
576572
),
577573
}))

packages/http-tracker-core/src/statistics/event/handler.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ mod tests {
7777
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
7878

7979
use crate::event::{ConnectionContext, Event};
80+
use crate::services::RemoteClientAddr;
8081
use crate::statistics::event::handler::handle_event;
8182
use crate::statistics::repository::Repository;
8283
use crate::tests::{sample_info_hash, sample_peer_using_ipv4, sample_peer_using_ipv6};
@@ -91,8 +92,7 @@ mod tests {
9192
handle_event(
9293
Event::TcpAnnounce {
9394
connection: ConnectionContext::new(
94-
remote_client_ip,
95-
Some(8080),
95+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
9696
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
9797
),
9898
info_hash: sample_info_hash(),
@@ -115,8 +115,7 @@ mod tests {
115115
handle_event(
116116
Event::TcpScrape {
117117
connection: ConnectionContext::new(
118-
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
119-
Some(8080),
118+
RemoteClientAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), Some(8080)),
120119
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
121120
),
122121
},
@@ -139,8 +138,7 @@ mod tests {
139138
handle_event(
140139
Event::TcpAnnounce {
141140
connection: ConnectionContext::new(
142-
remote_client_ip,
143-
Some(8080),
141+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
144142
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
145143
),
146144
info_hash: sample_info_hash(),
@@ -163,8 +161,10 @@ mod tests {
163161
handle_event(
164162
Event::TcpScrape {
165163
connection: ConnectionContext::new(
166-
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
167-
Some(8080),
164+
RemoteClientAddr::new(
165+
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
166+
Some(8080),
167+
),
168168
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
169169
),
170170
},

0 commit comments

Comments
 (0)