Skip to content

Commit c4dc5e3

Browse files
committed
Merge #1472: Enrich HTTP core Event::TcpAnnounce event
a422e4e refactor: extract type RemoteClientAddr (Jose Celano) 7ed7500 refactor: extract fn for duplicate code (Jose Celano) bc02e9b feat: [#1376] add info-hash to bittorrent_http_tracker_core::event::TcpAnnounce (Jose Celano) 657a5d0 refactor: [#1376] remove initial peer state from event. Only the peer IP may change (Jose Celano) 92f049e tests: add tests for HTTP tracker core events comparison (Jose Celano) 4566ad5 test: add test for Peer comparison (Jose Celano) 5d479b7 chore: add logs for sending TcpScrape event (Jose Celano) a67e137 feat: [#1376] add peer info to TcpAnnounce event (Jose Celano) Pull request description: Added `info_hash` and `announcement` (peer announce request data): ```rust pub enum Event { TcpAnnounce { connection: ConnectionContext, info_hash: InfoHash, announcement: PeerAnnouncement, }, TcpScrape { connection: ConnectionContext, }, } ``` ACKs for top commit: josecelano: ACK a422e4e Tree-SHA512: 8e37284cc2171ed0dc71c791d4ac033a212050955e78f70345ed37ce5e3346ea62da4119b55ba4643b0366375114a5b296af4ced654b0908cff6baba01cdd3ef
2 parents 27c5f9b + a422e4e commit c4dc5e3

File tree

7 files changed

+324
-154
lines changed

7 files changed

+324
-154
lines changed

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

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
use std::net::{IpAddr, SocketAddr};
22

3+
use bittorrent_primitives::info_hash::InfoHash;
34
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
45
use torrust_tracker_metrics::label_name;
6+
use torrust_tracker_primitives::peer::PeerAnnouncement;
57
use torrust_tracker_primitives::service_binding::ServiceBinding;
68

9+
use crate::services::RemoteClientAddr;
10+
711
pub mod sender;
812

913
/// A HTTP core event.
1014
#[derive(Debug, PartialEq, Eq, Clone)]
1115
pub enum Event {
12-
TcpAnnounce { connection: ConnectionContext },
13-
TcpScrape { connection: ConnectionContext },
16+
TcpAnnounce {
17+
connection: ConnectionContext,
18+
info_hash: InfoHash,
19+
announcement: PeerAnnouncement,
20+
},
21+
TcpScrape {
22+
connection: ConnectionContext,
23+
},
1424
}
1525

1626
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -21,12 +31,9 @@ pub struct ConnectionContext {
2131

2232
impl ConnectionContext {
2333
#[must_use]
24-
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 {
2535
Self {
26-
client: ClientConnectionContext {
27-
ip_addr: client_ip_addr,
28-
port: opt_client_port,
29-
},
36+
client: ClientConnectionContext { remote_client_addr },
3037
server: ServerConnectionContext {
3138
service_binding: server_service_binding,
3239
},
@@ -35,12 +42,12 @@ impl ConnectionContext {
3542

3643
#[must_use]
3744
pub fn client_ip_addr(&self) -> IpAddr {
38-
self.client.ip_addr
45+
self.client.ip_addr()
3946
}
4047

4148
#[must_use]
4249
pub fn client_port(&self) -> Option<u16> {
43-
self.client.port
50+
self.client.port()
4451
}
4552

4653
#[must_use]
@@ -51,10 +58,19 @@ impl ConnectionContext {
5158

5259
#[derive(Debug, PartialEq, Eq, Clone)]
5360
pub struct ClientConnectionContext {
54-
ip_addr: IpAddr,
61+
remote_client_addr: RemoteClientAddr,
62+
}
5563

56-
/// It's provided if you use the `torrust-axum-http-tracker-server` crate.
57-
port: Option<u16>,
64+
impl ClientConnectionContext {
65+
#[must_use]
66+
pub fn ip_addr(&self) -> IpAddr {
67+
self.remote_client_addr.ip
68+
}
69+
70+
#[must_use]
71+
pub fn port(&self) -> Option<u16> {
72+
self.remote_client_addr.port
73+
}
5874
}
5975

6076
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -80,3 +96,72 @@ impl From<ConnectionContext> for LabelSet {
8096
])
8197
}
8298
}
99+
100+
#[cfg(test)]
101+
pub mod test {
102+
103+
use torrust_tracker_primitives::peer::Peer;
104+
use torrust_tracker_primitives::service_binding::Protocol;
105+
106+
use super::Event;
107+
use crate::services::RemoteClientAddr;
108+
use crate::tests::sample_info_hash;
109+
110+
#[must_use]
111+
pub fn events_match(event: &Event, expected_event: &Event) -> bool {
112+
match (event, expected_event) {
113+
(
114+
Event::TcpAnnounce {
115+
connection,
116+
info_hash,
117+
announcement,
118+
},
119+
Event::TcpAnnounce {
120+
connection: expected_connection,
121+
info_hash: expected_info_hash,
122+
announcement: expected_announcement,
123+
},
124+
) => {
125+
*connection == *expected_connection
126+
&& *info_hash == *expected_info_hash
127+
&& announcement.peer_addr == expected_announcement.peer_addr
128+
}
129+
_ => false,
130+
}
131+
}
132+
133+
#[test]
134+
fn events_should_be_comparable() {
135+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
136+
137+
use torrust_tracker_primitives::service_binding::ServiceBinding;
138+
139+
use crate::event::{ConnectionContext, Event};
140+
141+
let remote_client_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
142+
let info_hash = sample_info_hash();
143+
144+
let event1 = Event::TcpAnnounce {
145+
connection: ConnectionContext::new(
146+
RemoteClientAddr::new(remote_client_ip, Some(8080)),
147+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
148+
),
149+
info_hash,
150+
announcement: Peer::default(),
151+
};
152+
153+
let event2 = Event::TcpAnnounce {
154+
connection: ConnectionContext::new(
155+
RemoteClientAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), Some(8080)),
156+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
157+
),
158+
info_hash,
159+
announcement: Peer::default(),
160+
};
161+
162+
let event1_clone = event1.clone();
163+
164+
assert!(event1 == event1_clone);
165+
assert!(event1 != event2);
166+
}
167+
}

packages/http-tracker-core/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pub const HTTP_TRACKER_LOG_TARGET: &str = "HTTP TRACKER";
2020

2121
#[cfg(test)]
2222
pub(crate) mod tests {
23+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
24+
25+
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
2326
use bittorrent_primitives::info_hash::InfoHash;
27+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
2428

2529
/// # Panics
2630
///
@@ -31,4 +35,29 @@ pub(crate) mod tests {
3135
.parse::<InfoHash>()
3236
.expect("String should be a valid info hash")
3337
}
38+
39+
pub fn sample_peer_using_ipv4() -> peer::Peer {
40+
sample_peer()
41+
}
42+
43+
pub fn sample_peer_using_ipv6() -> peer::Peer {
44+
let mut peer = sample_peer();
45+
peer.peer_addr = SocketAddr::new(
46+
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
47+
8080,
48+
);
49+
peer
50+
}
51+
52+
pub fn sample_peer() -> peer::Peer {
53+
peer::Peer {
54+
peer_id: PeerId(*b"-qB00000000000000000"),
55+
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
56+
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
57+
uploaded: NumberOfBytes::new(0),
58+
downloaded: NumberOfBytes::new(0),
59+
left: NumberOfBytes::new(0),
60+
event: AnnounceEvent::Started,
61+
}
62+
}
3463
}

0 commit comments

Comments
 (0)