Skip to content

Commit

Permalink
refactor(http): [#184] extract announce service in Axum tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 16, 2023
1 parent 02e2516 commit 99dbbe4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/http/axum_implementation/extractors/peer_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl From<ResolutionError> for responses::error::Error {
///
/// Will return an error if the peer IP cannot be obtained according to the configuration.
/// For example, if the IP is extracted from an HTTP header which is missing in the request.
pub fn peer_ip(on_reverse_proxy: bool, remote_client_ip: &RemoteClientIp) -> Result<IpAddr, Response> {
pub fn assign_ip_address_to_peer(on_reverse_proxy: bool, remote_client_ip: &RemoteClientIp) -> Result<IpAddr, Response> {
if on_reverse_proxy {
if let Some(ip) = remote_client_ip.right_most_x_forwarded_for {
Ok(ip)
Expand Down
24 changes: 6 additions & 18 deletions src/http/axum_implementation/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use axum::extract::State;
use axum::response::{IntoResponse, Response};
use log::debug;

use crate::http::axum_implementation::extractors::peer_ip::peer_ip;
use crate::http::axum_implementation::extractors::peer_ip::assign_ip_address_to_peer;
use crate::http::axum_implementation::extractors::remote_client_ip::RemoteClientIp;
use crate::http::axum_implementation::requests::announce::{Announce, Event, ExtractAnnounceRequest};
use crate::http::axum_implementation::responses;
use crate::http::axum_implementation::{responses, services};
use crate::protocol::clock::{Current, Time};
use crate::tracker::peer::Peer;
use crate::tracker::{statistics, Tracker};
use crate::tracker::Tracker;

#[allow(clippy::unused_async)]
pub async fn handle(
Expand All @@ -22,31 +22,19 @@ pub async fn handle(
) -> Response {
debug!("http announce request: {:#?}", announce_request);

let info_hash = announce_request.info_hash;

let peer_ip = peer_ip(tracker.config.on_reverse_proxy, &remote_client_ip);

let peer_ip = match peer_ip {
let peer_ip = match assign_ip_address_to_peer(tracker.config.on_reverse_proxy, &remote_client_ip) {
Ok(peer_ip) => peer_ip,
Err(err) => return err,
};

let mut peer = peer_from_request(&announce_request, &peer_ip);

let response = tracker.announce(&info_hash, &mut peer, &peer_ip).await;

match peer_ip {
IpAddr::V4(_) => {
tracker.send_stats_event(statistics::Event::Tcp4Announce).await;
}
IpAddr::V6(_) => {
tracker.send_stats_event(statistics::Event::Tcp6Announce).await;
}
}
let response = services::announce::invoke(tracker.clone(), announce_request.info_hash, &mut peer).await;

responses::announce::Announce::from(response).into_response()
}

/// It ignores the peer address in the announce request params.
#[must_use]
fn peer_from_request(announce_request: &Announce, peer_ip: &IpAddr) -> Peer {
Peer {
Expand Down
1 change: 1 addition & 0 deletions src/http/axum_implementation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod resources;
pub mod responses;
pub mod routes;
pub mod server;
pub mod services;
24 changes: 24 additions & 0 deletions src/http/axum_implementation/services/announce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::net::IpAddr;
use std::sync::Arc;

use crate::protocol::info_hash::InfoHash;
use crate::tracker::peer::Peer;
use crate::tracker::{statistics, AnnounceResponse, Tracker};

pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer) -> AnnounceResponse {
let original_peer_ip = peer.peer_addr.ip();

// The tracker could change the original peer ip
let response = tracker.announce(&info_hash, peer, &original_peer_ip).await;

match original_peer_ip {
IpAddr::V4(_) => {
tracker.send_stats_event(statistics::Event::Tcp4Announce).await;
}
IpAddr::V6(_) => {
tracker.send_stats_event(statistics::Event::Tcp6Announce).await;
}
}

response
}
1 change: 1 addition & 0 deletions src/http/axum_implementation/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod announce;
3 changes: 3 additions & 0 deletions src/http/warp_implementation/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub async fn handle_announce(

let mut peer = peer_builder::from_request(&announce_request, &remote_client_ip);

// todo: we should be use the http::axum_implementation::services::announce::announce service,
// but this Warp implementation is going to be removed.

let response = tracker.announce(&info_hash, &mut peer, &remote_client_ip).await;

match remote_client_ip {
Expand Down

0 comments on commit 99dbbe4

Please sign in to comment.