Skip to content

Commit

Permalink
Merge #422: Encode tracker API token in HTTP requests
Browse files Browse the repository at this point in the history
860de14 fix: [#419] url-encode tracker API token in the HTTP requests (Jose Celano)
2c9c3dd fix: [#391] patch to identify 'torrent not found' tracker API response (Jose Celano)

Pull request description:

  When the API token contains special characters like: `123g7#@agJtWFSgkdA5R$K22yeo$k6IhNq%T2$r`

  The value must be URL-encoded like this:

  http://tracker.torrust-demo.com:1212/v1/stats?token=123g7%23%40agJtWFSgkdA5R%24K22yeo%24k6IhNq%25T2%24r

  Instead of using the token directly:

  http://tracker.torrust-demo.com:1212/v1/stats?token=123g7#@agJtWFSgkdA5R$K22yeo$k6IhNq%T2$r

  This PR also fixes the issue #391

ACKs for top commit:
  josecelano:
    ACK 860de14

Tree-SHA512: 7c4e2361f3d27f9cfc05e38659a1bd1b7bd5668bed59f7b6bec0c8c05e9143398054d906a9bf9980fcda2db548562aec689ee3c76a535c98b0cd0db83aaabfce
  • Loading branch information
josecelano committed Dec 26, 2023
2 parents 373493f + 860de14 commit a471a5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
33 changes: 16 additions & 17 deletions src/tracker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!(
"{}/whitelist/{}?token={}",
self.base_url, info_hash, self.connection_info.token
);
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.post(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.post(request_url).query(&params).send().await
}

/// Remove a torrent from the tracker whitelist.
Expand All @@ -50,14 +49,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!(
"{}/whitelist/{}?token={}",
self.base_url, info_hash, self.connection_info.token
);
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.delete(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.delete(request_url).query(&params).send().await
}

/// Retrieve a new tracker key.
Expand All @@ -66,14 +64,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result<Response, Error> {
let request_url = format!(
"{}/key/{}?token={}",
self.base_url, token_valid_seconds, self.connection_info.token
);
let request_url = format!("{}/key/{}", self.base_url, token_valid_seconds);

let client = reqwest::Client::new();

client.post(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.post(request_url).query(&params).send().await
}

/// Retrieve the info for a torrent.
Expand All @@ -82,10 +79,12 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn get_torrent_info(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!("{}/torrent/{}?token={}", self.base_url, info_hash, self.connection_info.token);
let request_url = format!("{}/torrent/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.get(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.get(request_url).query(&params).send().await
}
}
4 changes: 2 additions & 2 deletions src/tracker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async fn map_torrent_info_response(response: Response) -> Result<TorrentInfo, Se
ServiceError::InternalServerError
})?;

if body == *"torrent not known" {
if body == "\"torrent not known\"" {
// todo: temporary fix. the service should return a 404 (StatusCode::NOT_FOUND).
return Err(ServiceError::TorrentNotFound);
}
Expand All @@ -209,7 +209,7 @@ mod tests {

#[tokio::test]
async fn it_should_return_a_torrent_not_found_response_when_the_tracker_returns_the_current_torrent_not_known_response() {
let tracker_response = Response::new("torrent not known");
let tracker_response = Response::new("\"torrent not known\"");

let result = map_torrent_info_response(tracker_response.into()).await.unwrap_err();

Expand Down

0 comments on commit a471a5b

Please sign in to comment.