-
Notifications
You must be signed in to change notification settings - Fork 49
Axum HTTP tracker: scrape
request in private
mode
#199
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
Merged
josecelano
merged 2 commits into
torrust:develop
from
josecelano:issue-196-axum-http-tracker-scrape-req-private-mode
Mar 1, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,79 @@ | ||
use std::panic::Location; | ||
use std::sync::Arc; | ||
|
||
use axum::extract::State; | ||
use axum::extract::{Path, State}; | ||
use axum::response::{IntoResponse, Response}; | ||
use log::debug; | ||
|
||
use super::auth::KeyIdParam; | ||
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::handlers::auth; | ||
use crate::http::axum_implementation::requests::scrape::Scrape; | ||
use crate::http::axum_implementation::{responses, services}; | ||
use crate::tracker::auth::KeyId; | ||
use crate::tracker::Tracker; | ||
|
||
#[allow(clippy::unused_async)] | ||
pub async fn handle( | ||
pub async fn handle_without_key( | ||
State(tracker): State<Arc<Tracker>>, | ||
ExtractRequest(scrape_request): ExtractRequest, | ||
remote_client_ip: RemoteClientIp, | ||
) -> Response { | ||
debug!("http scrape request: {:#?}", &scrape_request); | ||
|
||
let peer_ip = match peer_ip::resolve(tracker.config.on_reverse_proxy, &remote_client_ip) { | ||
if tracker.is_private() { | ||
return handle_fake_scrape(&tracker, &scrape_request, &remote_client_ip).await; | ||
} | ||
|
||
handle_real_scrape(&tracker, &scrape_request, &remote_client_ip).await | ||
} | ||
|
||
#[allow(clippy::unused_async)] | ||
pub async fn handle_with_key( | ||
State(tracker): State<Arc<Tracker>>, | ||
ExtractRequest(scrape_request): ExtractRequest, | ||
Path(key_id_param): Path<KeyIdParam>, | ||
remote_client_ip: RemoteClientIp, | ||
) -> Response { | ||
debug!("http scrape request: {:#?}", &scrape_request); | ||
|
||
let Ok(key_id) = key_id_param.value().parse::<KeyId>() else { | ||
return responses::error::Error::from( | ||
auth::Error::InvalidKeyFormat { | ||
location: Location::caller() | ||
}) | ||
.into_response() | ||
}; | ||
|
||
match auth::authenticate(&key_id, &tracker).await { | ||
Ok(_) => (), | ||
Err(_) => return handle_fake_scrape(&tracker, &scrape_request, &remote_client_ip).await, | ||
} | ||
|
||
handle_real_scrape(&tracker, &scrape_request, &remote_client_ip).await | ||
} | ||
|
||
async fn handle_real_scrape(tracker: &Arc<Tracker>, scrape_request: &Scrape, remote_client_ip: &RemoteClientIp) -> Response { | ||
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, &scrape_request.info_hashes, &peer_ip).await; | ||
|
||
responses::scrape::Bencoded::from(scrape_data).into_response() | ||
} | ||
|
||
/// When authentication fails in `private` mode the tracker returns empty swarm metadata for all the requested infohashes. | ||
async fn handle_fake_scrape(tracker: &Arc<Tracker>, scrape_request: &Scrape, remote_client_ip: &RemoteClientIp) -> Response { | ||
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; | ||
let scrape_data = services::scrape::fake_invoke(tracker, &scrape_request.info_hashes, &peer_ip).await; | ||
|
||
responses::scrape::Bencoded::from(scrape_data).into_response() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we return a 401?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I think all responses have status 200 and errors are just an special bencoded dictionary. But in this case this is not even an error in the current Warp implementation. @WarmBeer do you know it?. Where can we find the specification for private trackers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WarmBeer I think the Axum fork returns an error when the tracker runs in
private
mode, and thekey
is missing, invalid or unknown.announce
: https://github.com/Power2All/torrust-axum/blob/master/src/http_service.rs#L97-L125scrape
: https://github.com/Power2All/torrust-axum/blob/master/src/http_service.rs#L240-L268I can't find any official documentation:
For me, it makes sense to change this behaviour. I will merge the PR because it's the actual Warp implementation behavior but let me know if we should change it.