Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul stats events: merge HTTP core events with a different IP version #1374

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/dev-tools/git/hooks/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ cargo +nightly fmt --check &&
cargo +nightly machete &&
cargo +stable build &&
CARGO_INCREMENTAL=0 cargo +stable clippy --no-deps --tests --benches --examples --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious -D clippy::complexity -D clippy::perf -D clippy::style -D clippy::pedantic &&
cargo +stable test --doc --workspace &&
cargo +stable test --tests --benches --examples --workspace --all-targets --all-features
1 change: 1 addition & 0 deletions contrib/dev-tools/git/hooks/pre-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ cargo +nightly fmt --check &&
cargo +nightly machete &&
cargo +stable build &&
CARGO_INCREMENTAL=0 cargo +stable clippy --no-deps --tests --benches --examples --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious -D clippy::complexity -D clippy::perf -D clippy::style -D clippy::pedantic &&
cargo +stable test --doc --workspace &&
cargo +stable test --tests --benches --examples --workspace --all-targets --all-features &&
cargo +stable run --bin e2e_tests_runner -- --config-toml-path "./share/default/config/tracker.e2e.container.sqlite3.toml"
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ where
};

let connection_info_ip = match ConnectInfo::<SocketAddr>::from_request_parts(parts, state).await {
Ok(connection_info_socket_addr) => Some(connection_info_socket_addr.0.ip()),
Ok(connection_info_socket_addr) => Some(connection_info_socket_addr.0),
Err(_) => None,
};

Ok(Extract(ClientIpSources {
right_most_x_forwarded_for,
connection_info_ip,
connection_info_socket_address: connection_info_ip,
}))
}
}
Expand Down
51 changes: 42 additions & 9 deletions packages/axum-http-tracker-server/src/v1/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! The handlers perform the authentication and authorization of the request,
//! and resolve the client IP address.
use std::net::SocketAddr;
use std::sync::Arc;

use axum::extract::State;
Expand All @@ -22,27 +23,27 @@ use crate::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
/// authentication (no PATH `key` parameter required).
#[allow(clippy::unused_async)]
pub async fn handle_without_key(
State(state): State<Arc<AnnounceService>>,
State(state): State<(Arc<AnnounceService>, SocketAddr)>,
ExtractRequest(announce_request): ExtractRequest,
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
) -> Response {
tracing::debug!("http announce request: {:#?}", announce_request);

handle(&state, &announce_request, &client_ip_sources, None).await
handle(&state.0, &announce_request, &client_ip_sources, &state.1, None).await
}

/// It handles the `announce` request when the HTTP tracker requires
/// authentication (PATH `key` parameter required).
#[allow(clippy::unused_async)]
pub async fn handle_with_key(
State(state): State<Arc<AnnounceService>>,
State(state): State<(Arc<AnnounceService>, SocketAddr)>,
ExtractRequest(announce_request): ExtractRequest,
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
ExtractKey(key): ExtractKey,
) -> Response {
tracing::debug!("http announce request: {:#?}", announce_request);

handle(&state, &announce_request, &client_ip_sources, Some(key)).await
handle(&state.0, &announce_request, &client_ip_sources, &state.1, Some(key)).await
}

/// It handles the `announce` request.
Expand All @@ -53,9 +54,18 @@ async fn handle(
announce_service: &Arc<AnnounceService>,
announce_request: &Announce,
client_ip_sources: &ClientIpSources,
server_socket_addr: &SocketAddr,
maybe_key: Option<Key>,
) -> Response {
let announce_data = match handle_announce(announce_service, announce_request, client_ip_sources, maybe_key).await {
let announce_data = match handle_announce(
announce_service,
announce_request,
client_ip_sources,
server_socket_addr,
maybe_key,
)
.await
{
Ok(announce_data) => announce_data,
Err(error) => {
let error_response = responses::error::Error {
Expand All @@ -71,10 +81,11 @@ async fn handle_announce(
announce_service: &Arc<AnnounceService>,
announce_request: &Announce,
client_ip_sources: &ClientIpSources,
server_socket_addr: &SocketAddr,
maybe_key: Option<Key>,
) -> Result<AnnounceData, HttpAnnounceError> {
announce_service
.handle_announce(announce_request, client_ip_sources, maybe_key)
.handle_announce(announce_request, client_ip_sources, server_socket_addr, maybe_key)
.await
}

Expand Down Expand Up @@ -183,7 +194,7 @@ mod tests {
fn sample_client_ip_sources() -> ClientIpSources {
ClientIpSources {
right_most_x_forwarded_for: None,
connection_info_ip: None,
connection_info_socket_address: None,
}
}

Expand All @@ -196,6 +207,7 @@ mod tests {

mod with_tracker_in_private_mode {

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::str::FromStr;

use bittorrent_http_tracker_protocol::v1::responses;
Expand All @@ -209,12 +221,15 @@ mod tests {
async fn it_should_fail_when_the_authentication_key_is_missing() {
let http_core_tracker_services = initialize_private_tracker();

let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

let maybe_key = None;

let response = handle_announce(
&http_core_tracker_services.announce_service,
&sample_announce_request(),
&sample_client_ip_sources(),
&server_socket_addr,
maybe_key,
)
.await
Expand All @@ -236,12 +251,15 @@ mod tests {

let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();

let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

let maybe_key = Some(unregistered_key);

let response = handle_announce(
&http_core_tracker_services.announce_service,
&sample_announce_request(),
&sample_client_ip_sources(),
&server_socket_addr,
maybe_key,
)
.await
Expand All @@ -260,6 +278,8 @@ mod tests {

mod with_tracker_in_listed_mode {

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use bittorrent_http_tracker_protocol::v1::responses;

use super::{initialize_listed_tracker, sample_announce_request, sample_client_ip_sources};
Expand All @@ -272,10 +292,13 @@ mod tests {

let announce_request = sample_announce_request();

let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

let response = handle_announce(
&http_core_tracker_services.announce_service,
&announce_request,
&sample_client_ip_sources(),
&server_socket_addr,
None,
)
.await
Expand All @@ -297,6 +320,8 @@ mod tests {

mod with_tracker_on_reverse_proxy {

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use bittorrent_http_tracker_protocol::v1::responses;
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;

Expand All @@ -310,13 +335,16 @@ mod tests {

let client_ip_sources = ClientIpSources {
right_most_x_forwarded_for: None,
connection_info_ip: None,
connection_info_socket_address: None,
};

let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

let response = handle_announce(
&http_core_tracker_services.announce_service,
&sample_announce_request(),
&client_ip_sources,
&server_socket_addr,
None,
)
.await
Expand All @@ -335,6 +363,8 @@ mod tests {

mod with_tracker_not_on_reverse_proxy {

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use bittorrent_http_tracker_protocol::v1::responses;
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;

Expand All @@ -348,13 +378,16 @@ mod tests {

let client_ip_sources = ClientIpSources {
right_most_x_forwarded_for: None,
connection_info_ip: None,
connection_info_socket_address: None,
};

let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

let response = handle_announce(
&http_core_tracker_services.announce_service,
&sample_announce_request(),
&client_ip_sources,
&server_socket_addr,
None,
)
.await
Expand Down
Loading