Skip to content

Commit

Permalink
feat(http): [#191] add scrape app service
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 24, 2023
1 parent c4bee79 commit ae1a076
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 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 @@ -31,7 +31,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 assign_ip_address_to_peer(on_reverse_proxy: bool, remote_client_ip: &RemoteClientIp) -> Result<IpAddr, Response> {
pub fn resolve(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
4 changes: 2 additions & 2 deletions src/http/axum_implementation/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::response::{IntoResponse, Response};
use log::debug;

use crate::http::axum_implementation::extractors::announce_request::ExtractRequest;
use crate::http::axum_implementation::extractors::peer_ip::assign_ip_address_to_peer;
use crate::http::axum_implementation::extractors::peer_ip;
use crate::http::axum_implementation::extractors::remote_client_ip::RemoteClientIp;
use crate::http::axum_implementation::requests::announce::{Announce, Compact, Event};
use crate::http::axum_implementation::responses::announce;
Expand All @@ -24,7 +24,7 @@ pub async fn handle(
) -> Response {
debug!("http announce request: {:#?}", announce_request);

let peer_ip = match assign_ip_address_to_peer(tracker.config.on_reverse_proxy, &remote_client_ip) {
let peer_ip = match peer_ip::resolve(tracker.config.on_reverse_proxy, &remote_client_ip) {
Ok(peer_ip) => peer_ip,
Err(err) => return err,
};
Expand Down
21 changes: 15 additions & 6 deletions src/http/axum_implementation/handlers/scrape.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
use std::sync::Arc;

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use log::debug;

use crate::http::axum_implementation::extractors::peer_ip;
use crate::http::axum_implementation::extractors::remote_client_ip::RemoteClientIp;
use crate::http::axum_implementation::extractors::scrape_request::ExtractRequest;
use crate::http::axum_implementation::services;
use crate::tracker::Tracker;

#[allow(clippy::unused_async)]
pub async fn handle(
State(tracker): State<Arc<Tracker>>,
ExtractRequest(scrape_request): ExtractRequest,
_remote_client_ip: RemoteClientIp,
) -> String {
remote_client_ip: RemoteClientIp,
) -> Response {
debug!("http scrape request: {:#?}", &scrape_request);

/*
todo:
- Add the service that sends the event for statistics.
- Build the HTTP bencoded response.
- [x] Add the service that sends the event for statistics.
- [ ] Build the HTTP bencoded response.
*/

let scrape_data = tracker.scrape(&scrape_request.info_hashes).await;
let peer_ip = match peer_ip::resolve(tracker.config.on_reverse_proxy, &remote_client_ip) {
Ok(peer_ip) => peer_ip,
Err(err) => return err,
};

let scrape_data = services::scrape::invoke(tracker.clone(), &scrape_request.info_hashes, &peer_ip).await;

debug!("scrape data: {:#?}", &scrape_data);

"todo".to_string()
(StatusCode::OK, "todo").into_response()
}
4 changes: 2 additions & 2 deletions src/http/axum_implementation/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer)
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;
let announce_data = tracker.announce(&info_hash, peer, &original_peer_ip).await;

match original_peer_ip {
IpAddr::V4(_) => {
Expand All @@ -20,5 +20,5 @@ pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer)
}
}

response
announce_data
}
1 change: 1 addition & 0 deletions src/http/axum_implementation/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod announce;
pub mod scrape;
20 changes: 20 additions & 0 deletions src/http/axum_implementation/services/scrape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::net::IpAddr;
use std::sync::Arc;

use crate::protocol::info_hash::InfoHash;
use crate::tracker::{statistics, ScrapeData, Tracker};

pub async fn invoke(tracker: Arc<Tracker>, info_hashes: &Vec<InfoHash>, original_peer_ip: &IpAddr) -> ScrapeData {
let scrape_data = tracker.scrape(info_hashes).await;

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

scrape_data
}
2 changes: 2 additions & 0 deletions src/tracker/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const CHANNEL_BUFFER_SIZE: usize = 65_535;

#[derive(Debug, PartialEq, Eq)]
pub enum Event {
// code-review: consider one single event for request type with data: Event::Announce { scheme: HTTPorUDP, ip_version: V4orV6 }
// Attributes are enums too.
Tcp4Announce,
Tcp4Scrape,
Tcp6Announce,
Expand Down
6 changes: 2 additions & 4 deletions tests/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,8 +2354,7 @@ mod axum_http_tracker_server {
assert_scrape_response(response, &expected_scrape_response).await;
}

//#[tokio::test]
#[allow(dead_code)]
#[tokio::test]
async fn should_increase_the_number_ot_tcp4_scrape_requests_handled_in_statistics() {
let http_tracker = start_public_http_tracker(Version::Axum).await;

Expand All @@ -2374,8 +2373,7 @@ mod axum_http_tracker_server {
assert_eq!(stats.tcp4_scrapes_handled, 1);
}

//#[tokio::test]
#[allow(dead_code)]
#[tokio::test]
async fn should_increase_the_number_ot_tcp6_scrape_requests_handled_in_statistics() {
let http_tracker = start_ipv6_http_tracker(Version::Axum).await;

Expand Down

0 comments on commit ae1a076

Please sign in to comment.